/**
  * Returns the weather-data for the supplied location
  *
  * @param   string                      $id
  * @param   string                      $unitsFormat
  * @return  PEAR_Error|array
  * @throws  PEAR_Error
  * @access  public
  */
 function getWeather($id = "", $unitsFormat = "")
 {
     static $clouds;
     if (!isset($clouds)) {
         $clouds = array("sky clear", "few", "scattered", "broken", "overcast");
     }
     $status = $this->_checkLocationID($id);
     if (Services_Weather::isError($status)) {
         return $status;
     }
     // Get other data
     $units = $this->getUnitsFormat($unitsFormat);
     $weatherReturn = array();
     if ($this->_cacheEnabled && ($weather = $this->_cache->get("GW-" . $id, "weather"))) {
         // Same procedure...
         $this->_weather = $weather;
         $weatherReturn["cache"] = "HIT";
     } else {
         // Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
         if (!$this->_weatherSoap) {
             $status = $this->_connectServer();
             if (Services_Weather::isError($status)) {
                 return $status;
             }
         }
         // ...as last function
         $weather = $this->_weatherSoap->getWeatherReport($id);
         if (Services_Weather::isError($weather)) {
             return $weather;
         }
         $this->_weather = $weather;
         if ($this->_cacheEnabled) {
             // ...and cache it
             $expire = constant("SERVICES_WEATHER_EXPIRES_WEATHER");
             $this->_cache->extSave("GW-" . $id, $this->_weather, "", $expire, "weather");
         }
         $weatherReturn["cache"] = "MISS";
     }
     $update = trim(str_replace(array("T", "Z"), " ", $this->_weather->timestamp)) . " GMT";
     $weatherReturn["update"] = gmdate(trim($this->_dateFormat . " " . $this->_timeFormat), strtotime($update));
     $weatherReturn["updateRaw"] = $this->_weather->timestamp;
     if (strlen($this->_weather->station->region) && strlen($this->_weather->station->country)) {
         $locname = $this->_weather->station->name . ", " . $this->_weather->station->region . ", " . $this->_weather->station->country;
     } elseif (strlen($this->_weather->station->country)) {
         $locname = $this->_weather->station->name . ", " . $this->_weather->station->country;
     } else {
         $locname = $this->_weather->station->name;
     }
     $weatherReturn["station"] = $locname;
     $weatherReturn["wind"] = $this->convertSpeed($this->_weather->wind->prevailing_speed, "mps", $units["wind"]);
     $weatherReturn["windDegrees"] = $this->_weather->wind->prevailing_direction->degrees;
     $weatherReturn["windDirection"] = $this->_weather->wind->prevailing_direction->compass;
     if ($this->_weather->wind->prevailing_speed != $this->_weather->wind->gust_speed) {
         $weatherReturn["windGust"] = $this->convertSpeed($this->_weather->wind->gust_speed, "mps", $units["wind"]);
     }
     if ($this->_weather->wind->varying_from_direction != "" && $this->_weather->wind->varying_to_direction != "") {
         $weatherReturn["windVar"] = array("from" => $this->_weather->wind->varying_from_direction, "to" => $this->_weather->wind->varying_to_direction);
     }
     $weatherReturn["visibility"] = $this->convertDistance($this->_weather->visibility->distance / 1000, "km", $units["vis"]);
     $weatherReturn["visQualifier"] = $this->_weather->visibility->qualifier;
     $condition = array();
     for ($i = 0; $i < sizeof($this->_weather->phenomena); $i++) {
         $condition[] = $this->_weather->phenomena[$i]->string;
     }
     $weatherReturn["condition"] = implode(", ", $condition);
     if (is_array($this->_weather->sky->layers)) {
         $layers = array();
         for ($i = 0; $i < sizeof($this->_weather->sky->layers); $i++) {
             if (strtoupper($this->_weather->sky->layers[$i]->type) != "CLEAR") {
                 $layers[$i] = array();
                 $layers[$i]["amount"] = $clouds[$this->_weather->sky->layers[$i]->extent];
                 $layers[$i]["height"] = $this->convertDistance($this->_weather->sky->layers[$i]->altitude, "m", $units["height"]);
                 if (strtoupper($this->_weather->sky->layers[$i]->type) != "CLOUD") {
                     $layers[$i]["type"] = ucwords(str_replace("_", "", $this->_weather->sky->layers[$i]->type));
                 }
             }
         }
         if (sizeof($layers)) {
             $weatherReturn["clouds"] = $layers;
         }
     }
     $weatherReturn["temperature"] = $this->convertTemperature($this->_weather->temperature->ambient, "c", $units["temp"]);
     $feltTemperature = $this->calculateWindChill($this->convertTemperature($weatherReturn["temperature"], $units["temp"], "f"), $this->convertSpeed($weatherReturn["wind"], $units["wind"], "mph"));
     $weatherReturn["feltTemperature"] = $this->convertTemperature($feltTemperature, "f", $units["temp"]);
     $weatherReturn["dewPoint"] = $this->convertTemperature($this->_weather->temperature->dewpoint, "c", $units["temp"]);
     $weatherReturn["humidity"] = $this->_weather->temperature->relative_humidity;
     $weatherReturn["pressure"] = $this->convertPressure($this->_weather->pressure->altimeter, "hpa", $units["pres"]);
     return $weatherReturn;
 }
Example #2
0
 /**
  * Creates error, same as in PEAR with a customized flavor
  *
  * @param   int                         $code
  * @param   string                      $file
  * @param   int                         $line
  * @return  PEAR_Error
  * @access  private
  */
 function &raiseError($code = SERVICES_WEATHER_ERROR_UNKNOWN_ERROR, $file = "", $line = 0)
 {
     // This should improve the performance of the script, as PEAR is only included, when
     // really needed.
     include_once "PEAR.php";
     $message = "Services_Weather";
     if ($file != "" && $line > 0) {
         $message .= " (" . basename($file) . ":" . $line . ")";
     }
     $message .= ": " . Services_Weather::_errorMessage($code);
     $error = PEAR::raiseError($message, $code, PEAR_ERROR_RETURN, E_USER_NOTICE, "Services_Weather_Error", null, false);
     return $error;
 }
Example #3
0
//
// $Id: ejse-basic.php,v 1.3 2004/05/07 14:41:36 eru Exp $
require_once "Services/Weather.php";
// Object initialization - error checking is important, because of
// handling exceptions such as missing PEAR modules or not being online
$ejse =& Services_Weather::service("Ejse", array("debug" => 2));
if (Services_Weather::isError($ejse)) {
    die("Error: " . $ejse->getMessage() . "\n");
}
/* Erase comments to enable caching
$status = $ejse->setCache("file", array("cache_dir" => "/tmp/cache/"));
if (Services_Weather::isError($status)) {
    echo "Error: ".$status->getMessage()."\n";
}
*/
$ejse->setUnitsFormat("metric");
$ejse->setDateTimeFormat("d.m.Y", "H:i");
$location = "81611";
// Aspen, CO
//$location = "02115"; // Boston, MA
//$location = "96799"; // Pago Pago, AS
//$location = "09009"; // Armed Forces Europe -> Error
// Now iterate through available functions for retrieving data
foreach (array("getLocation", "getWeather", "getForecast") as $function) {
    $data = $ejse->{$function}($location);
    if (Services_Weather::isError($data)) {
        echo "Error: " . $data->getMessage() . "\n";
        continue;
    }
    var_dump($data);
}
Example #4
0
 /**
  * Get the forecast for the next days
  *
  * @param   string                      $int
  * @param   int                         $days           Values between 1 and 9
  * @param   string                      $unitsFormat
  * @return  PEAR_Error|array
  * @throws  PEAR_Error
  * @access  public
  */
 function getForecast($id = "", $days = 2, $unitsFormat = "")
 {
     $status = $this->_checkLocationID($id);
     if (Services_Weather::isError($status)) {
         return $status;
     }
     if (!in_array($days, range(1, 9))) {
         $days = 2;
     }
     // Get other data
     $units = $this->getUnitsFormat($unitsFormat);
     $forecastReturn = array();
     if ($this->_cacheEnabled && ($forecast = $this->_getCache($id, "forecast"))) {
         // Same procedure...
         $this->_forecast = $forecast;
         $forecastReturn["cache"] = "HIT";
     } else {
         // Check, if the weatherSoap-Object is present. If not, connect to the Server and retrieve the WDSL data
         if (!$this->_weatherSoap) {
             $status = $this->_connectServer();
             if (Services_Weather::isError($status)) {
                 return $status;
             }
         }
         // ...as last function
         $forecast = $this->_weatherSoap->GetNineDayForecastInfo2($this->_username, $this->_password, $id);
         if (Services_Weather::isError($forecast)) {
             return $forecast;
         }
         $this->_forecast = $forecast;
         if ($this->_cacheEnabled) {
             // ...and cache it
             $this->_saveCache($id, $this->_forecast, "", "forecast");
         }
         $forecastReturn["cache"] = "MISS";
     }
     $forecastReturn["days"] = array();
     // Initialize some arrays
     $temperatureHigh = array();
     $temperatureLow = array();
     for ($i = 1; $i <= $days; $i++) {
         preg_match("/(-?\\d+)\\D+/", $this->_forecast->{"Day" . $i}->High, $temperatureHigh);
         preg_match("/(-?\\d+)\\D+/", $this->_forecast->{"Day" . $i}->Low, $temperatureLow);
         $day = array("tempertureHigh" => $this->convertTemperature($temperatureHigh[1], "f", $units["temp"]), "temperatureLow" => $this->convertTemperature($temperatureLow[1], "f", $units["temp"]), "day" => array("condition" => $this->_forecast->{"Day" . $i}->Forecast, "conditionIcon" => $this->_forecast->{"Day" . $i}->IconIndex, "precipitation" => trim(str_replace("%", "", $this->_forecast->{"Day" . $i}->PrecipChance))));
         $forecastReturn["days"][] = $day;
     }
     return $forecastReturn;
 }
 /**
  * Calculates sunrise and sunset for a location
  *
  * The sun position algorithm taken from the 'US Naval Observatory's
  * Almanac for Computers', implemented by Ken Bloom <kekabloom[at]ucdavis[dot]edu>
  * for the zmanim project, converted to C by Moshe Doron <mosdoron[at]netvision[dot]net[dot]il>
  * and finally taken from the PHP5 sources and converted to native PHP as a wrapper.
  *
  * The date has to be entered as a timestamp!
  *
  * @param   int                         $date
  * @param   int                         $retformat
  * @param   float                       $latitude
  * @param   float                       $longitude
  * @param   float                       $zenith
  * @param   float                       $gmt_offset
  * @param   bool                        $sunrise
  * @return  PEAR_Error|mixed
  * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_SUNFUNCS_DATE_INVALID
  * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_SUNFUNCS_RETFORM_INVALID
  * @throws  PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_ERROR
  * @access  public
  */
 function calculateSunRiseSet($date, $retformat = null, $latitude = null, $longitude = null, $zenith = null, $gmt_offset = null, $sunrise = true)
 {
     // Date must be timestamp for now
     if (!is_int($date)) {
         return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SUNFUNCS_DATE_INVALID, __FILE__, __LINE__);
     }
     // Check for proper return format
     if ($retformat === null) {
         $retformat = SUNFUNCS_RET_STRING;
     } elseif (!in_array($retformat, array(SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING, SUNFUNCS_RET_DOUBLE))) {
         return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_SUNFUNCS_RETFORM_INVALID, __FILE__, __LINE__);
     }
     // Set default values for coordinates
     if ($latitude === null) {
         $latitude = SUNFUNCS_DEFAULT_LATITUDE;
     } else {
         $latitude = (double) $latitude;
     }
     if ($longitude === null) {
         $longitude = SUNFUNCS_DEFAULT_LONGITUDE;
     } else {
         $longitude = (double) $longitude;
     }
     if ($zenith === null) {
         if ($sunrise) {
             $zenith = SUNFUNCS_SUNRISE_ZENITH;
         } else {
             $zenith = SUNFUNCS_SUNSET_ZENITH;
         }
     } else {
         $zenith = (double) $zenith;
     }
     // Default value for GMT offset
     if ($gmt_offset === null) {
         $gmt_offset = date("Z", $date) / 3600;
     } else {
         $gmt_offset = (double) $gmt_offset;
     }
     // If we have PHP5, then act as wrapper for the appropriate functions
     if ($sunrise && function_exists("date_sunrise")) {
         return date_sunrise($date, $retformat, $latitude, $longitude, $zenith, $gmt_offset);
     }
     if (!$sunrise && function_exists("date_sunset")) {
         return date_sunset($date, $retformat, $latitude, $longitude, $zenith, $gmt_offset);
     }
     // Apparently we have PHP4, so calculate the neccessary steps in native PHP
     // Step 1: First calculate the day of the year
     $N = date("z", $date) + 1;
     // Step 2: Convert the longitude to hour value and calculate an approximate time
     $lngHour = $longitude / 15;
     // Use 18 for sunset instead of 6
     if ($sunrise) {
         // Sunrise
         $t = $N + (6 - $lngHour) / 24;
     } else {
         // Sunset
         $t = $N + (18 - $lngHour) / 24;
     }
     // Step 3: Calculate the sun's mean anomaly
     $M = 0.9856 * $t - 3.289;
     // Step 4: Calculate the sun's true longitude
     $L = $M + 1.916 * sin(deg2rad($M)) + 0.02 * sin(deg2rad(2 * $M)) + 282.634;
     while ($L < 0) {
         $Lx = $L + 360;
         assert($Lx != $L);
         // askingtheguru: really needed?
         $L = $Lx;
     }
     while ($L >= 360) {
         $Lx = $L - 360;
         assert($Lx != $L);
         // askingtheguru: really needed?
         $L = $Lx;
     }
     // Step 5a: Calculate the sun's right ascension
     $RA = rad2deg(atan(0.91764 * tan(deg2rad($L))));
     while ($RA < 0) {
         $RAx = $RA + 360;
         assert($RAx != $RA);
         // askingtheguru: really needed?
         $RA = $RAx;
     }
     while ($RA >= 360) {
         $RAx = $RA - 360;
         assert($RAx != $RA);
         // askingtheguru: really needed?
         $RA = $RAx;
     }
     // Step 5b: Right ascension value needs to be in the same quadrant as L
     $Lquadrant = floor($L / 90) * 90;
     $RAquadrant = floor($RA / 90) * 90;
     $RA = $RA + ($Lquadrant - $RAquadrant);
     // Step 5c: Right ascension value needs to be converted into hours
     $RA /= 15;
     // Step 6: Calculate the sun's declination
     $sinDec = 0.39782 * sin(deg2rad($L));
     $cosDec = cos(asin($sinDec));
     // Step 7a: Calculate the sun's local hour angle
     $cosH = (cos(deg2rad($zenith)) - $sinDec * sin(deg2rad($latitude))) / ($cosDec * cos(deg2rad($latitude)));
     // XXX: What's the use of this block.. ?
     // if (sunrise && cosH > 1 || !sunrise && cosH < -1) {
     //     throw doesnthappen();
     // }
     // Step 7b: Finish calculating H and convert into hours
     if ($sunrise) {
         // Sunrise
         $H = 360 - rad2deg(acos($cosH));
     } else {
         // Sunset
         $H = rad2deg(acos($cosH));
     }
     $H = $H / 15;
     // Step 8: Calculate local mean time
     $T = $H + $RA - 0.06571 * $t - 6.622;
     // Step 9: Convert to UTC
     $UT = $T - $lngHour;
     while ($UT < 0) {
         $UTx = $UT + 24;
         assert($UTx != $UT);
         // askingtheguru: really needed?
         $UT = $UTx;
     }
     while ($UT >= 24) {
         $UTx = $UT - 24;
         assert($UTx != $UT);
         // askingtheguru: really needed?
         $UT = $UTx;
     }
     $UT = $UT + $gmt_offset;
     // Now bring the result into the chosen format and return
     switch ($retformat) {
         case SUNFUNCS_RET_TIMESTAMP:
             return intval($date - $date % (24 * 3600) + 3600 * $UT);
         case SUNFUNCS_RET_STRING:
             $N = floor($UT);
             return sprintf("%02d:%02d", $N, floor(60 * ($UT - $N)));
         case SUNFUNCS_RET_DOUBLE:
             return $UT;
         default:
             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_ERROR, __FILE__, __LINE__);
     }
 }
 /**
  * METAR provides no forecast per se, we use the TAF reports to generate
  * a forecast for the announced timeperiod
  *
  * @param   string                      $id
  * @param   int                         $days           Ignored, not applicable
  * @param   string                      $unitsFormat
  * @return  PEAR_Error|array
  * @throws  PEAR_Error
  * @access  public
  */
 function getForecast($id = "", $days = null, $unitsFormat = "")
 {
     $id = strtoupper($id);
     $status = $this->_checkLocationID($id);
     if (Services_Weather::isError($status)) {
         return $status;
     }
     // Get other data
     $units = $this->getUnitsFormat($unitsFormat);
     $location = $this->getLocation($id);
     if (Services_Weather::isError($location)) {
         return $location;
     }
     if ($this->_cacheEnabled && ($forecast = $this->_cache->get("METAR-" . $id, "forecast"))) {
         // Wee... it was cached, let's have it...
         $forecastReturn = $forecast;
         $this->_forecast = $forecastReturn;
         $forecastReturn["cache"] = "HIT";
     } else {
         // Download forecast
         $forecastData = $this->_retrieveServerData($id, "taf");
         if (Services_Weather::isError($forecastData)) {
             return $forecastData;
         } elseif (!is_array($forecastData) || sizeof($forecastData) < 2) {
             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA, __FILE__, __LINE__);
         }
         // Parse forecast
         $forecastReturn = $this->_parseForecastData($forecastData);
         if (Services_Weather::isError($forecastReturn)) {
             return $forecastReturn;
         }
         if ($this->_cacheEnabled) {
             // Cache weather
             $expire = constant("SERVICES_WEATHER_EXPIRES_FORECAST");
             $this->_cache->extSave("METAR-" . $id, $forecastReturn, $unitsFormat, $expire, "forecast");
         }
         $this->_forecast = $forecastReturn;
         $forecastReturn["cache"] = "MISS";
     }
     $this->_convertReturn($forecastReturn, $units, $location);
     return $forecastReturn;
 }
Example #7
0
 /**
  * Get the forecast for the next days
  *
  * @param   string                      $id
  * @param   int                         $days           Values between 1 and 5
  * @param   string                      $unitsFormat
  * @return  PEAR_Error|array
  * @throws  PEAR_Error
  * @access  public
  */
 function getForecast($id = "", $days = 5, $unitsFormat = "")
 {
     $status = $this->_checkLocationID($id);
     if (Services_Weather::isError($status)) {
         return $status;
     }
     if (!is_int($days) || $days < 1 || $days > 5) {
         $days = 5;
     }
     // Get other data
     $units = $this->getUnitsFormat($unitsFormat);
     $forecastReturn = array();
     if (is_object($this->_forecast)) {
         $forecastReturn["cache"] = "MEM";
     } elseif ($this->_cacheEnabled && ($forecast = $this->_getCache($id, "forecast"))) {
         // Encore...
         $this->_forecast = $forecast;
         $forecastReturn["cache"] = "HIT";
     } else {
         // ...
         $status = $this->_parseWeatherData($id, "forecast");
         if (Services_Weather::isError($status)) {
             return $status;
         }
         $forecastReturn["cache"] = "MISS";
     }
     // Make sure the location object has been loaded
     if (!is_object($this->_location)) {
         $this->getLocation($id);
     }
     // Some explanation for the next two lines: (same as above)
     // weather.com isn't always supplying the timezone in the update string, but
     // uses "Local Time" as reference, which is imho utterly stupid, because it's
     // inconsistent. Well, what I do here is check for this string and if I can
     // find it, I calculate the difference between the timezone at the location
     // and this computers timezone. This amount of seconds is then subtracted from
     // the time the update-string has delivered.
     $update = str_replace("Local Time", "", $this->_forecast->lsup);
     $adjustTZ = $update == $this->_forecast->lsup ? 0 : $this->_location->zone * 3600 - date("Z");
     $forecastReturn["update"] = gmdate($this->_dateFormat . " " . $this->_timeFormat, strtotime($update) - $adjustTZ);
     $forecastReturn["updateRaw"] = $this->_forecast->lsup;
     $forecastReturn["days"] = array();
     for ($i = 0; $i < $days; $i++) {
         $day = array("temperatureHigh" => round($this->convertTemperature($this->_forecast->day[$i]->hi, "f", $units["temp"]), 2), "temperatureLow" => round($this->convertTemperature($this->_forecast->day[$i]->low, "f", $units["temp"]), 2), "sunrise" => date($this->_timeFormat, strtotime($this->_forecast->day[$i]->sunr)), "sunset" => date($this->_timeFormat, strtotime($this->_forecast->day[$i]->suns)), "day" => array("condition" => $this->_forecast->day[$i]->part[0]->t, "conditionIcon" => $this->_forecast->day[$i]->part[0]->icon, "wind" => round($this->convertSpeed($this->_forecast->day[$i]->part[0]->wind->s, "mph", $units["wind"]), 2), "windGust" => round($this->convertSpeed($this->_forecast->day[$i]->part[0]->wind->gust, "mph", $units["wind"]), 2), "windDegrees" => $this->_forecast->day[$i]->part[0]->wind->d, "windDirection" => $this->_forecast->day[$i]->part[0]->wind->t, "precipitation" => $this->_forecast->day[$i]->part[0]->ppcp, "humidity" => round($this->_forecast->day[$i]->part[0]->hmid, 1)), "night" => array("condition" => $this->_forecast->day[$i]->part[1]->t, "conditionIcon" => $this->_forecast->day[$i]->part[1]->icon, "wind" => round($this->convertSpeed($this->_forecast->day[$i]->part[1]->wind->s, "mph", $units["wind"]), 2), "windGust" => round($this->convertSpeed($this->_forecast->day[$i]->part[1]->wind->gust, "mph", $units["wind"]), 2), "windDegrees" => $this->_forecast->day[$i]->part[1]->wind->d, "windDirection" => $this->_forecast->day[$i]->part[1]->wind->t, "precipitation" => $this->_forecast->day[$i]->part[1]->ppcp, "humidity" => round($this->_forecast->day[$i]->part[1]->hmid, 1)));
         $forecastReturn["days"][] = $day;
     }
     return $forecastReturn;
 }
Example #8
0
 /**
  */
 protected function _content()
 {
     global $conf;
     static $metarLocs;
     if (empty($this->_params['location'])) {
         throw new Horde_Exception(_("No location is set."));
     }
     if (!is_array($metarLocs)) {
         $metarLocs = $this->getParams();
     }
     $metar = Services_Weather::service('METAR', array('debug' => 0));
     $metar->setMetarDB($conf['sql']);
     $metar->setUnitsFormat($this->_params['units']);
     $metar->setDateTimeFormat('M j, Y', 'H:i');
     $metar->setMetarSource('http');
     $units = $metar->getUnitsFormat($this->_params['units']);
     $weather = $metar->getWeather($this->_params['location']);
     if (is_a($weather, 'PEAR_Error')) {
         $html = $weather->getMessage();
         return $html;
     }
     $html = '<table width="100%" cellspacing="0">' . '<tr><td class="control"><strong>' . sprintf('%s, %s (%s)', $metarLocs['location']['values'][$this->_params['__location']][$this->_params['location']], $this->_params['__location'], $this->_params['location']) . '</strong></td></tr></table><strong>' . _("Last Updated:") . '</strong> ' . $weather['update'] . '<br /><br />';
     // Wind.
     if (isset($weather['wind'])) {
         $html .= '<strong>' . _("Wind:") . '</strong> ';
         if ($weather['windDirection'] == 'Variable') {
             if (!empty($this->_params['knots'])) {
                 $html .= sprintf(_("%s at %s %s"), $weather['windDirection'], round($metar->convertSpeed($weather['wind'], $units['wind'], 'kt')), 'kt');
             } else {
                 $html .= sprintf(_("%s at %s %s"), $weather['windDirection'], round($weather['wind']), $units['wind']);
             }
         } elseif ($weather['windDegrees'] == '000' && $weather['wind'] == '0') {
             $html .= sprintf(_("calm"));
         } else {
             $html .= sprintf(_("from the %s (%s) at %s %s"), $weather['windDirection'], $weather['windDegrees'], empty($this->_params['knots']) ? round($weather['wind']) : round($metar->convertSpeed($weather['wind'], $units['wind'], 'kt')), empty($this->_params['knots']) ? $units['wind'] : 'kt');
         }
     }
     if (isset($weather['windGust'])) {
         if ($weather['windGust']) {
             if (!empty($this->_params['knots'])) {
                 $html .= sprintf(_(", gusting %s %s"), round($metar->convertSpeed($weather['windGust'], $units['wind'], 'kt')), 'kt');
             } else {
                 $html .= sprintf(_(", gusting %s %s"), round($weather['windGust']), $units['wind']);
             }
         }
     }
     if (isset($weather['windVariability'])) {
         if ($weather['windVariability']['from']) {
             $html .= sprintf(_(", variable from %s to %s"), $weather['windVariability']['from'], $weather['windVariability']['to']);
         }
     }
     // Visibility.
     if (isset($weather['visibility'])) {
         $html .= $this->_sameRow(_("Visibility"), $weather['visibility'] . ' ' . $units['vis']);
     }
     // Temperature/DewPoint.
     if (isset($weather['temperature'])) {
         $html .= $this->_row(_("Temperature"), round($weather['temperature']) . '&deg;' . Horde_String::upper($units['temp']));
     }
     if (isset($weather['dewPoint'])) {
         $html .= $this->_sameRow(_("Dew Point"), round($weather['dewPoint']) . '&deg;' . Horde_String::upper($units['temp']));
     }
     if (isset($weather['feltTemperature'])) {
         $html .= $this->_sameRow(_("Feels Like"), round($weather['feltTemperature']) . '&deg;' . Horde_String::upper($units['temp']));
     }
     // Pressure.
     if (isset($weather['pressure'])) {
         $html .= $this->_row(_("Pressure"), $weather['pressure'] . ' ' . $units['pres']);
     }
     // Humidity.
     if (isset($weather['humidity'])) {
         $html .= $this->_sameRow(_("Humidity"), round($weather['humidity']) . '%');
     }
     // Clouds.
     if (isset($weather['clouds'])) {
         $clouds = '';
         foreach ($weather['clouds'] as $cloud) {
             $clouds .= '<br />';
             if (isset($cloud['height'])) {
                 $clouds .= sprintf(_("%s at %s %s"), $cloud['amount'], $cloud['height'], $units['height']);
             } else {
                 $clouds .= $cloud['amount'];
             }
         }
         $html .= $this->_row(_("Clouds"), $clouds);
     }
     // Conditions.
     if (isset($weather['condition'])) {
         $html .= $this->_row(_("Conditions"), $weather['condition']);
     }
     // Remarks.
     if (isset($weather['remark'])) {
         $remarks = '';
         $other = '';
         foreach ($weather['remark'] as $remark => $value) {
             switch ($remark) {
                 case 'seapressure':
                     $remarks .= '<br />' . _("Pressure at sea level: ") . $value . ' ' . $units['pres'];
                     break;
                 case 'precipitation':
                     foreach ($value as $precip) {
                         if (is_numeric($precip['amount'])) {
                             $remarks .= '<br />' . sprintf(ngettext("Precipitation for last %d hour: ", "Precipitation for last %d hours: ", $precip['hours']), $precip['hours']) . $precip['amount'] . ' ' . $units['rain'];
                         } else {
                             $remarks .= '<br />' . sprintf(ngettext("Precipitation for last %d hour: ", "Precipitation for last %d hours: ", $precip['hours']), $precip['hours']) . $precip['amount'];
                         }
                     }
                     break;
                 case 'snowdepth':
                     $remarks .= '<br />' . _("Snow depth: ") . $value . ' ' . $units['rain'];
                     break;
                 case 'snowequiv':
                     $remarks .= '<br />' . _("Snow equivalent in water: ") . $value . ' ' . $units['rain'];
                     break;
                 case 'sunduration':
                     $remarks .= '<br />' . sprintf(_("%d minutes"), $value);
                     break;
                 case '1htemp':
                     $remarks .= '<br />' . _("Temp for last hour: ") . round($value) . '&deg;' . Horde_String::upper($units['temp']);
                     break;
                 case '1hdew':
                     $remarks .= '<br />' . _("Dew Point for last hour: ") . round($value) . '&deg;' . Horde_String::upper($units['temp']);
                     break;
                 case '6hmaxtemp':
                     $remarks .= '<br />' . _("Max temp last 6 hours: ") . round($value) . '&deg;' . Horde_String::upper($units['temp']);
                     break;
                 case '6hmintemp':
                     $remarks .= '<br />' . _("Min temp last 6 hours: ") . round($value) . '&deg;' . Horde_String::upper($units['temp']);
                     break;
                 case '24hmaxtemp':
                     $remarks .= '<br />' . _("Max temp last 24 hours: ") . round($value) . '&deg;' . Horde_String::upper($units['temp']);
                     break;
                 case '24hmintemp':
                     $remarks .= '<br />' . _("Min temp last 24 hours: ") . round($value) . '&deg;' . Horde_String::upper($units['temp']);
                     break;
                 case 'sensors':
                     foreach ($value as $sensor) {
                         $remarks .= '<br />' . _("Sensor: ") . $sensor;
                     }
                     break;
                 default:
                     $other .= '<br />' . $value;
                     break;
             }
         }
         $html .= $this->_row(_("Remarks"), $remarks . $other);
     }
     // TAF
     if (!empty($this->_params['taf'])) {
         $taf = $metar->getForecast($this->_params['location']);
         if (!is_a($taf, 'PEAR_Error')) {
             $forecast = '<table width="100%" cellspacing="0">';
             $forecast .= '<tr><td class="control" colspan="2"><center><strong>' . _("Forecast (TAF)") . '</strong></td></tr></table>';
             $forecast .= '<strong>Valid: </strong>' . $taf['validFrom'] . ' - ' . $taf['validTo'] . '<br /><br />';
             $item = 0;
             foreach ($taf['time'] as $time => $entry) {
                 $item++;
                 $forecast .= '<table width="100%" cellspacing="0">';
                 $forecast .= '<tr class="item' . $item % 2 . '">';
                 $forecast .= '<td align="center" width="50">' . $time . '</td><td><strong>Wind:</strong> ';
                 if (isset($entry['wind'])) {
                     if ($entry['windDirection'] == 'Variable') {
                         if (!empty($this->_params['knots'])) {
                             $forecast .= sprintf(_("%s at %s %s"), strtolower($entry['windDirection']), round($metar->convertSpeed($entry['wind'], $units['wind'], 'kt')), 'kt');
                         } else {
                             $forecast .= sprintf(_("%s at %s %s"), $entry['windDirection'], round($entry['wind']), $units['wind']);
                         }
                     } elseif ($entry['windDegrees'] == '000' && $entry['wind'] == '0') {
                         $forecast .= sprintf(_("calm"));
                     } else {
                         $forecast .= sprintf(_("from the %s (%s) at %s %s"), $entry['windDirection'], $entry['windDegrees'], empty($this->_params['knots']) ? round($entry['wind']) : round($metar->convertSpeed($entry['wind'], $units['wind'], 'kt')), empty($this->_params['knots']) ? $units['wind'] : 'kt');
                     }
                     $forecast .= '<br />';
                 }
                 if (isset($entry['temperatureLow']) || isset($entry['temperatureHigh'])) {
                     $forecast .= '<strong>Temperature</strong>';
                     if (isset($entry['temperatureLow'])) {
                         $forecast .= '<strong> Low:</strong>';
                         $forecast .= $entry['temperatureLow'];
                     }
                     if (isset($entry['temperatureHigh'])) {
                         $forecast .= '<strong> High:</strong>';
                         $forecast .= $entry['temperatureHigh'];
                     }
                     $forecast .= '<br />';
                 }
                 if (isset($entry['windshear'])) {
                     $forecast .= '<strong>Windshear:</strong>';
                     $forecast .= sprintf(_("from the %s (%s) at %s %s"), $entry['windshearDirection'], $entry['windshearDegrees'], $entry['windshearHeight'], $units['height']);
                     $forecast .= '<br />';
                 }
                 if (isset($entry['visibility'])) {
                     $forecast .= '<strong>Visibility:</strong> ';
                     $forecast .= strtolower($entry['visQualifier']) . ' ' . $entry['visibility'] . ' ' . $units['vis'];
                     $forecast .= '<br />';
                 }
                 if (isset($entry['condition'])) {
                     $forecast .= '<strong>Conditions:</strong> ';
                     $forecast .= $entry['condition'];
                     $forecast .= '<br />';
                 }
                 $forecast .= '<strong>Clouds:</strong> ';
                 foreach ($entry['clouds'] as $clouds) {
                     if (isset($clouds['type'])) {
                         $forecast .= ' ' . $clouds['type'];
                     }
                     $forecast .= ' ' . $clouds['amount'];
                     if (isset($clouds['height'])) {
                         $forecast .= ' at ' . $clouds['height'] . ' ' . $units['height'];
                     } else {
                         $forecast .= ' ';
                     }
                 }
                 $forecast .= '</td></tr>';
                 if (isset($entry['fmc'])) {
                     $item++;
                     foreach ($entry['fmc'] as $fmcEntry) {
                         $forecast .= '<tr class="item' . $item % 2 . '">';
                         $forecast .= '<td align="center" width="50">';
                         $forecast .= '* ' . $fmcEntry['from'] . '<br /> - ' . $fmcEntry['to'] . '</td>';
                         $forecast .= '<td>';
                         $forecast .= '<strong>Type: </strong>' . $fmcEntry['type'];
                         if (isset($fmcEntry['probability'])) {
                             $forecast .= ' <strong> Prob: </strong>' . $fmcEntry['probability'] . '%';
                         }
                         if (isset($fmcEntry['condition'])) {
                             $forecast .= ' <strong> Conditions: </strong>' . $fmcEntry['condition'];
                         }
                         if (isset($fmcEntry['clouds'])) {
                             $forecast .= ' <strong>Clouds:</strong>';
                             foreach ($fmcEntry['clouds'] as $fmcClouds) {
                                 if (isset($fmcClouds['type'])) {
                                     $forecast .= ' ' . $fmcClouds['type'];
                                 }
                                 if (isset($fmcClouds['height'])) {
                                     $forecast .= ' ' . $fmcClouds['amount'];
                                     $forecast .= ' ' . $fmcClouds['height'];
                                     $forecast .= ' ' . $units['height'];
                                 } else {
                                     $forecast .= ' ' . $fmcClouds['amount'];
                                 }
                             }
                         }
                         if (isset($fmcEntry['visQualifier'])) {
                             $forecast .= ' <strong>Visibility:</strong> ';
                             $forecast .= strtolower($fmcEntry['visQualifier']) . ' ';
                             $forecast .= $fmcEntry['visibility'] . ' ' . $units['vis'];
                         }
                         $forecast .= '</td></tr>';
                     }
                 }
             }
             $forecast .= '</table>';
             $html .= $forecast;
         }
     }
     return $html;
 }
Example #9
0
 /**
  * The content to go in this block.
  *
  * @return string   The content
  */
 function _content()
 {
     if (!@(include_once 'Services/Weather.php')) {
         Horde::logMessage('The metar block will not work without Services_Weather from PEAR. Run pear install Services_Weather.', __FILE__, __LINE__, PEAR_LOG_ERR);
         return _("Metar block not available. Details have been logged for the administrator.");
     }
     global $conf;
     static $metarLocs;
     if (!isset($conf['sql'])) {
         return _("A database backend is required for this block.");
     }
     if (empty($this->_params['location'])) {
         return _("No location is set.");
     }
     if (!is_array($metarLocs)) {
         $metarLocs = $this->getParams();
     }
     require_once 'Services/Weather.php';
     $metar =& Services_Weather::service('METAR', array('debug' => 0));
     $dbString = $conf['sql']['phptype'] . '://';
     $dbString .= $conf['sql']['username'] . ':';
     $dbString .= $conf['sql']['password'] . '@';
     $dbString .= $conf['sql']['hostspec'] . '/';
     $dbString .= $conf['sql']['database'];
     $metar->setMetarDB($dbString);
     $metar->setUnitsFormat($this->_params['units']);
     $metar->setDateTimeFormat('M j, Y', 'H:i');
     $metar->setMetarSource('http');
     $units = $metar->getUnits(0, $this->_params['units']);
     $weather = $metar->getWeather($this->_params['location']);
     $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0">' . '<tr><td class="control"><b>' . sprintf('%s, %s (%s)', $metarLocs['location']['values'][$this->_params['__location']][$this->_params['location']], $this->_params['__location'], $this->_params['location']) . '</td></tr></table><b>' . _("Last Updated:") . '</b> ' . $weather['update'] . '<br /><br />';
     // Wind.
     if (isset($weather['wind'])) {
         $html .= '<b>' . _("Wind:") . '</b> ';
         if ($weather['windDirection'] == 'Variable') {
             if (!empty($this->_params['knots'])) {
                 $html .= sprintf(_('%s at %s %s'), $weather['windDirection'], round($metar->convertSpeed($weather['wind'], $units['wind'], 'kt')), 'kt');
             } else {
                 $html .= sprintf(_('%s at %s %s'), $weather['windDirection'], round($weather['wind']), $units['wind']);
             }
         } elseif ($weather['windDegrees'] == '000' && $weather['wind'] == '0') {
             $html .= sprintf(_("calm"));
         } else {
             $html .= sprintf(_("from the %s (%s) at %s %s"), $weather['windDirection'], $weather['windDegrees'], empty($this->_params['knots']) ? round($weather['wind']) : round($metar->convertSpeed($weather['wind'], $units['wind'], 'kt')), empty($this->_params['knots']) ? $units['wind'] : 'kt');
         }
     }
     if (isset($weather['windGust'])) {
         if ($weather['windGust']) {
             if (!empty($this->_params['knots'])) {
                 $html .= sprintf(_(", gusting %s %s"), round($metar->convertSpeed($weather['windGust'], $units['wind'], 'kt')), 'kt');
             } else {
                 $html .= sprintf(_(", gusting %s %s"), round($weather['windGust']), $units['wind']);
             }
         }
     }
     if (isset($weather['windVariability'])) {
         if ($weather['windVariability']['from']) {
             $html .= sprintf(_(", variable from %s to %s"), $weather['windVariability']['from'], $weather['windVariability']['to']);
         }
     }
     // Visibility.
     if (isset($weather['visibility'])) {
         $html .= $this->_row(_("Visibility"), $weather['visibility'] . ' ' . $units['vis']);
     }
     // Temperature/DewPoint.
     if (isset($weather['temperature'])) {
         $html .= $this->_row(_("Temperature"), round($weather['temperature']) . '&deg;' . String::upper($units['temp']));
     }
     if (isset($weather['dewPoint'])) {
         $html .= $this->_row(_("Dew Point"), round($weather['dewPoint']) . '&deg;' . String::upper($units['temp']));
     }
     if (isset($weather['feltTemperature'])) {
         $html .= $this->_row(_("Feels Like"), round($weather['feltTemperature']) . '&deg;' . String::upper($units['temp']));
     }
     // Pressure.
     if (isset($weather['pressure'])) {
         $html .= $this->_row(_("Pressure"), $weather['pressure'] . ' ' . $units['pres']);
     }
     // Humidity.
     if (isset($weather['humidity'])) {
         $html .= $this->_row(_("Humidity"), round($weather['humidity']) . '%');
     }
     // Clouds.
     if (isset($weather['clouds'])) {
         $clouds = '';
         foreach ($weather['clouds'] as $cloud) {
             $clouds .= '<br />';
             if (isset($cloud['height'])) {
                 $clouds .= sprintf(_("%s at %s ft"), $cloud['amount'], $cloud['height']);
             } else {
                 $clouds .= $cloud['amount'];
             }
         }
         $html .= $this->_row(_("Clouds"), $clouds);
     }
     // Conditions.
     if (isset($weather['condition'])) {
         $html .= $this->_row(_("Conditions"), $weather['condition']);
     }
     // Remarks.
     if (isset($weather['remark'])) {
         $remarks = '';
         $other = '';
         foreach ($weather['remark'] as $remark => $value) {
             switch ($remark) {
                 case 'seapressure':
                     $remarks .= '<br />' . _("Pressure at sea level: ") . $value . ' ' . $units['pres'];
                     break;
                 case 'precipitation':
                     foreach ($value as $precip) {
                         if (is_numeric($precip['amount'])) {
                             $remarks .= '<br />' . sprintf(_("Precipitation for last %s hour(s): "), $precip['hours']) . $precip['amount'] . ' ' . $units['rain'];
                         } else {
                             $remarks .= '<br />' . sprintf(_("Precipitation for last %s hour(s): "), $precip['hours']) . $precip['amount'];
                         }
                     }
                     break;
                 case 'snowdepth':
                     $remarks .= '<br />' . _("Snow depth: ") . $value . ' ' . $units['rain'];
                     break;
                 case 'snowequiv':
                     $remarks .= '<br />' . _("Snow equivalent in water: ") . $value . ' ' . $units['rain'];
                     break;
                 case 'sunduration':
                     $remarks .= '<br />' . sprintf(_("%s minutes"), $value);
                     break;
                 case '1htemp':
                     $remarks .= '<br />' . _("Temp for last hour: ") . round($value) . '&deg;' . String::upper($units['temp']);
                     break;
                 case '1hdew':
                     $remarks .= '<br />' . _("Dew Point for last hour: ") . round($value) . '&deg;' . String::upper($units['temp']);
                     break;
                 case '6hmaxtemp':
                     $remarks .= '<br />' . _("Max temp last 6 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
                     break;
                 case '6hmintemp':
                     $remarks .= '<br />' . _("Min temp last 6 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
                     break;
                 case '24hmaxtemp':
                     $remarks .= '<br />' . _("Max temp last 24 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
                     break;
                 case '24hmintemp':
                     $remarks .= '<br />' . _("Min temp last 24 hours: ") . round($value) . '&deg;' . String::upper($units['temp']);
                     break;
                 case 'sensors':
                     foreach ($value as $sensor) {
                         $remarks .= '<br />' . _("Sensor: ") . $sensor;
                     }
                     break;
                 default:
                     $other .= '<br />' . $value;
                     break;
             }
         }
         $html .= $this->_row(_("Remarks"), $remarks . $other);
     }
     return $html;
 }
Example #10
0
 /**
  * Get the forecast for the next days
  *
  * @param    string                      $int
  * @param    int                         $days           Values between 1 and 9
  * @param    string                      $unitsFormat
  * @return   PEAR_Error|array
  * @throws   PEAR_Error
  * @access   public
  */
 function getForecast($id = "", $days = 2, $unitsFormat = "")
 {
     $status = $this->_checkLocationID($id);
     if (Services_Weather::isError($status)) {
         return $status;
     }
     if (!in_array($days, range(1, 9))) {
         $days = 2;
     }
     // Get other data
     $units = $this->getUnitsFormat($unitsFormat);
     $forecastReturn = array();
     if ($this->_cacheEnabled && ($forecast = $this->_cache->get($id, "forecast"))) {
         // Same procedure...
         $this->_forecast = $forecast;
         $forecastReturn["cache"] = "HIT";
     } else {
         // ...as last function
         $forecast = $this->_weatherSoap->GetNineDayForecastInfo($id);
         if (Services_Weather::isError($forecast)) {
             return $forecast;
         }
         $this->_forecast = $forecast;
         if ($this->_cacheEnabled) {
             // ...and cache it
             $expire = constant("SERVICES_WEATHER_EXPIRES_FORECAST");
             $this->_cache->extSave($id, $this->_forecast, "", $expire, "forecast");
         }
         $forecastReturn["cache"] = "MISS";
     }
     $forecastReturn["days"] = array();
     for ($i = 1; $i <= $days; $i++) {
         preg_match("/(-?\\d+)\\D+/", $this->_forecast->{"Day" . $i}->High, $temperatureHigh);
         preg_match("/(-?\\d+)\\D+/", $this->_forecast->{"Day" . $i}->Low, $temperatureLow);
         $day = array("tempertureHigh" => $this->convertTemperature($temperatureHigh[1], "f", $units["temp"]), "temperatureLow" => $this->convertTemperature($temperatureLow[1], "f", $units["temp"]), "day" => array("condition" => $this->_forecast->{"Day" . $i}->Forecast, "conditionIcon" => $this->_forecast->{"Day" . $i}->IconIndex, "precipitation" => trim(str_replace("%", "", $this->_forecast->{"Day" . $i}->PrecipChance))));
         $forecastReturn["days"][] = $day;
     }
     return $forecastReturn;
 }
Example #11
0
 /**
  * Enables caching the data, usage strongly recommended
  *
  * Requires Cache to be installed
  *
  * @param    string                      $cacheType
  * @param    array                       $cacheOptions
  * @return   PEAR_Error|bool
  * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED
  * @access   public
  */
 function setCache($cacheType = "file", $cacheOptions = array())
 {
     // The error handling in Cache is a bit crummy (read: not existent)
     // so we have to do that on our own...
     @(include_once "Cache.php");
     @($cache = new Cache($cacheType, $cacheOptions));
     if (is_object($cache) && (strtolower(get_class($cache)) == "cache" || is_subclass_of($cache, "cache"))) {
         $this->_cache = $cache;
         $this->_cacheEnabled = true;
     } else {
         $this->_cache = null;
         $this->_cacheEnabled = false;
         return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED, __FILE__, __LINE__);
     }
     return true;
 }
Example #12
0
 /**
  * METAR provides no forecast per se, we use the TAF reports to generate
  * a forecast for the announced timeperiod
  *
  * @param    string                      $id
  * @param    int                         $days           Ignored, not applicable
  * @param    string                      $unitsFormat
  * @return   PEAR_Error|array
  * @throws   PEAR_Error
  * @access   public
  */
 function getForecast($id = "", $days = null, $unitsFormat = "")
 {
     $id = strtoupper($id);
     $status = $this->_checkLocationID($id);
     if (Services_Weather::isError($status)) {
         return $status;
     }
     // Get other data
     $units = $this->getUnitsFormat($unitsFormat);
     $location = $this->getLocation($id);
     if ($this->_cacheEnabled && ($forecast = $this->_cache->get("METAR-" . $id, "forecast"))) {
         // Wee... it was cached, let's have it...
         $forecastReturn = $forecast;
         $this->_forecast = $forecastReturn;
         $forecastReturn["cache"] = "HIT";
     } else {
         // Set the source
         if ($this->_sourceTaf == "file") {
             $source = realpath($this->_sourcePathTaf . "/" . $id . ".TXT");
         } else {
             $source = $this->_sourcePathTaf . "/" . $id . ".TXT";
         }
         // Download and parse weather
         $forecastReturn = $this->_parseForecastData($source);
         if (Services_Weather::isError($forecastReturn)) {
             return $forecastReturn;
         }
         if ($this->_cacheEnabled) {
             // Cache weather
             $expire = constant("SERVICES_WEATHER_EXPIRES_FORECAST");
             $this->_cache->extSave("METAR-" . $id, $forecastReturn, $unitsFormat, $expire, "forecast");
         }
         $this->_forecast = $forecastReturn;
         $forecastReturn["cache"] = "MISS";
     }
     $this->_convertReturn($forecastReturn, $units, $location);
     return $forecastReturn;
 }
$units["pres"] = "&nbsp;" . $units["pres"];
$units["rain"] = "&nbsp;" . $units["rain"];
$metar->setMetarSource($sourceMetar, $sourcePathMetar, $sourceTaf, $sourcePathTaf);
// Set date-/time-format
$metar->setDateTimeFormat($dateFormat, $timeFormat);
// Search for defined location and fetch the first item found.
// Bail out if something bad happens...
$search = $metar->searchLocation($location, true);
if (Services_Weather::isError($search)) {
    die("Error: " . $search->getMessage() . "\n");
}
// Retrieve data, store in variables, bail out on error
$fetch = array("location" => "getLocation", "weather" => "getWeather", "forecast" => "getForecast");
foreach ($fetch as $variable => $function) {
    ${$variable} = $metar->{$function}($search);
    if (Services_Weather::isError(${$variable})) {
        echo "Error: " . ${$variable}->getMessage() . "\n";
        continue;
    }
}
// Calculate sunrise/-set for our location - UTC is used here!
$gmt_offset = 0;
$timestamp = gmmktime();
$location["sunrise"] = date_sunrise($timestamp, SUNFUNCS_RET_STRING, $location["latitude"], $location["longitude"], SUNFUNCS_SUNRISE_ZENITH, $gmt_offset);
$location["sunset"] = date_sunset($timestamp, SUNFUNCS_RET_STRING, $location["latitude"], $location["longitude"], SUNFUNCS_SUNSET_ZENITH, $gmt_offset);
// Now we output all the data, please don't expect extensive comments here, this is basic
// HTML/CSS stuff. Also this isn't a very fancy design, it's just to show you, what
// the script is able to do (and more ;-))...
?>
<html>
<head>
 /**
  * serendipity_plugin_weather::generate_content()
  *
  * @param  $title
  * @return
  */
 function generate_content(&$title)
 {
     global $serendipity;
     $title = $this->get_config('title');
     $metar_site = $this->get_config('metar', 'EDDK');
     $timezone = $this->get_config('timezone', 0);
     $unitString = $this->get_config('units', 'metric');
     $caching = $this->get_config('caching', 0);
     $directory = $this->get_config('cache_directory', '/tmp');
     $pixdir = $this->get_config('pixel_directory');
     if (@(include_once 'Services/Weather.php')) {
         $metar =& Services_Weather::service('METAR', array('debug' => 0));
         if (Services_Weather::isError($metar)) {
             echo 'Weather Error: ' . $metar->getMessage();
         }
         // Set the unit format for the data
         $metar->setUnitsFormat($unitString);
         // Set the time/date format
         // Do we have the date/time format set somewhere in s9y? then we should take this here
         // $metar->setDateTimeFormat('d.m.Y', 'H:i');
         //    $metar_data->setDateTimeFormat('j M Y', 'H:i');
         if ($caching) {
             if (@(include_once "Cache.php")) {
                 $status = $metar->setCache('file', array('cache_dir' => $serendipity['serendipityPath'] . $directory));
             } else {
                 echo 'Caching is enabled but PEAR:Cache does not seem to be installed.';
             }
         }
         if (Services_Weather::isError($status)) {
             echo 'Error: ' . $status->getMessage();
         }
         switch ($unitString) {
             case "metric":
                 $units = array('wind' => 'km/h', 'vis' => 'km', 'height' => 'km', 'temp' => '&deg;C', 'pres' => 'mb', 'rain' => 'mm');
                 break;
             case "standard":
                 $units = array('wind' => 'mph', 'vis' => 'mi', 'height' => 'mi', 'temp' => '&deg;F', 'pres' => 'in', 'rain' => 'in');
                 break;
         }
         $weather_data = $metar->getWeather($metar_site);
         if (Services_Weather::isError($weather_data)) {
             echo 'Error: ' . $weather_data->getMessage();
         }
         $location_data = $metar->getLocation($metar_site);
         if (Services_Weather::isError($location_data)) {
             echo 'Error: ' . $location_data->getMessage();
         }
         $forecast_data = $metar->getForecast($metar_site);
         if (Services_Weather::isError($forecast_data)) {
             echo 'Error: ' . $forecast_data->getMessage();
         }
         // Do all that icon-stuff
         // FIXXME: URL-Prefix
         $windDir = $weather_data["windDirection"];
         switch ($windDir) {
             case 'S':
                 $windDirIcon = $pixdir . '/sss.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_S;
                 break;
             case 'SSW':
                 $windDirIcon = $pixdir . '/ssw.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_SSW;
                 break;
             case 'SSE':
                 $windDirIcon = $pixdir . '/sse.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_SSE;
                 break;
             case 'SW':
                 $windDirIcon = $pixdir . '/sw.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_SW;
                 break;
             case 'WSW':
                 $windDirIcon = $pixdir . '/sww.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_WSW;
                 break;
             case 'E':
                 $windDirIcon = $pixdir . '/eee.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_E;
                 break;
             case 'ESE':
                 $windDirIcon = $pixdir . '/see.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_ESE;
                 break;
             case 'ENE':
                 $windDirIcon = $pixdir . '/nee.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_ENE;
                 break;
             case 'N':
                 $windDirIcon = $pixdir . '/nnn.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_N;
                 break;
             case 'NNW':
                 $windDirIcon = $pixdir . '/nnw.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_NNW;
                 break;
             case 'NNE':
                 $windDirIcon = $pixdir . '/nne.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_NNE;
                 break;
             case 'NW':
                 $windDirIcon = $pixdir . '/nw.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_NW;
                 break;
             case 'NE':
                 $windDirIcon = $pixdir . '/ne.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_NE;
                 break;
             case 'SE':
                 $windDirIcon = $pixdir . '/se.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_SE;
                 break;
             case 'W':
                 $windDirIcon = $pixdir . '/www.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_W;
                 break;
             case 'WNW':
                 $windDirIcon = $pixdir . '/nww.png';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_WNW;
                 break;
             case 'Variable':
                 $windDirIcon = $pixdir . '/vrb.gif';
                 $windDir = PLUGIN_SIDEBAR_WEATHER_DATA_V;
                 break;
             default:
                 $windDirIcon = $pixdir . '/wind_nodata.png';
                 $windDir = "No recorded data.";
         }
         // Turn the GMT time from the update into a local time
         $localTime = date('j M Y H:i', strtotime($weather_data['updateRaw']) + 3600 * $timezone);
         // Get local hour to determing if it is night
         $hour = date('H', strtotime($weather_data['updateRaw']) + 3600 * $tz);
         if ($hour > 18 || $hour < 6) {
             $night = 'n_';
         } else {
             $night = '';
         }
         // Handle cloud data
         // We could be dealing with cloud at several levels, so find the heaviest
         // cover and go with that.
         $cloudData = $weather_data['clouds'];
         // See if we are dealing with an array of arrays or some information
         $cloudKeys = array_keys($cloudData);
         $testKey = $cloudKeys[0];
         if (!is_array($cloudData["{$testKey}"])) {
             // we have information
             $amount = $cloudData['amount'];
         } else {
             // we have information on several levels - get highest
             $key = count($cloudKeys) - 1;
             $useArray = $cloudData[$key];
             $amount = $useArray['amount'];
         }
         switch ($amount) {
             case "Clear Below":
             case "clear sky":
             case "no significant cloud":
             case "clear below 12,000 ft":
             case "vertical visibility":
                 $cloudLevel = "0cloud";
                 break;
             case "few":
             case "scattered":
                 $cloudLevel = "1cloud";
                 break;
             case "Cumulonimbus":
                 $cloudLevel = "2cloud";
                 break;
             case "Towering Cumulus":
             case "broken":
                 $cloudLevel = "3cloud";
                 break;
             case "overcast":
                 $cloudLevel = "4cloud";
                 $night = "";
                 break;
             default:
                 $cloudLevel = "0cloud";
         }
         // Determine weather conditions (rain, snow etc);
         // We need some way to translate this
         $conditions = $weather_data["condition"];
         switch ($cloudLevel) {
             case "0cloud":
                 if (strstr($conditions, "fog") !== FALSE) {
                     $condUse = "_fog";
                 } else {
                     $condUse = "";
                 }
                 break;
             case "1cloud":
                 if (strstr($conditions, "fog") !== FALSE) {
                     $condUse = "_fog";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "light") !== FALSE) {
                     $condUse = "_lightrain";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "heavy") !== FALSE) {
                     $condUse = "_heavyrain";
                 } elseif (strstr($conditions, "rain") !== FALSE) {
                     $condUse = "_modrain";
                 } else {
                     $condUse = "_norain";
                 }
                 break;
             case "2cloud":
                 if (strstr($conditions, "fog") !== FALSE) {
                     $condUse = "_fog";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "light") !== FALSE) {
                     $condUse = "_lightrain";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "heavy") !== FALSE) {
                     $condUse = "_heavyrain";
                 } elseif (strstr($conditions, "rain") !== FALSE) {
                     $condUse = "_modrain";
                 } elseif (strstr($conditions, "snow") !== FALSE) {
                     $condUse = "_snow";
                 } elseif (strstr($conditions, "thunderstorm") !== FALSE) {
                     $condUse = "_thunders";
                 } else {
                     $condUse = "_norain";
                 }
                 break;
             case "3cloud":
                 if (strstr($conditions, "fog") !== FALSE) {
                     $condUse = "_fog";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "light") !== FALSE) {
                     $condUse = "_lightrain";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "heavy") !== FALSE) {
                     $condUse = "_heavyrain";
                 } elseif (strstr($conditions, "rain") !== FALSE) {
                     $condUse = "_modrain";
                 } elseif (strstr($conditions, "snow") !== FALSE) {
                     $condUse = "_snow";
                 } elseif (strstr($conditions, "thunderstorm") !== FALSE) {
                     $condUse = "_thunders";
                 } elseif (strstr($conditions, "hail") !== FALSE) {
                     $condUse = "_hail";
                 } else {
                     $condUse = "_norain";
                 }
                 break;
             case "4cloud":
                 if (strstr($conditions, "fog") !== FALSE) {
                     $condUse = "_fog";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "light") !== FALSE) {
                     $condUse = "_lightrain";
                 } elseif (strstr($conditions, "rain") !== FALSE && strstr($conditions, "heavy") !== FALSE) {
                     $condUse = "_heavyrain";
                 } elseif (strstr($conditions, "rain") !== FALSE) {
                     $condUse = "_modrain";
                 } elseif (strstr($conditions, "snow") !== FALSE && strstr($conditions, "light") !== FALSE) {
                     $condUse = "_lightsnow";
                 } elseif (strstr($conditions, "snow") !== FALSE && strstr($conditions, "heavy") !== FALSE) {
                     $condUse = "_heavysnow";
                 } elseif (strstr($conditions, "snow") !== FALSE) {
                     $condUse = "_snow";
                 } elseif (strstr($conditions, "thunderstorm") !== FALSE) {
                     $condUse = "_thunders";
                 } elseif (strstr($conditions, "hail") !== FALSE && strstr($conditions, "light") !== FALSE) {
                     $condUse = "_lighthail";
                 } elseif (strstr($conditions, "hail") !== FALSE && strstr($conditions, "heavy") !== FALSE) {
                     $condUse = "_heavyhail";
                 } elseif (strstr($conditions, "hail") !== FALSE) {
                     $condUse = "_hail";
                 } else {
                     $condUse = "_norain";
                 }
                 break;
             default:
                 $condUse = "_norain";
         }
         // Construct icon name
         $conditionIcon = $pixdir . '/' . $night . $cloudLevel . $condUse . '.png';
         $content = '';
         $content .= '<img src="' . $conditionIcon . '" alt="" /><br />' . $conditions . '<br />';
         // FIXXME: Translate the Winddirection
         $content .= '<dl><dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_WINDDIRECTION . '</dt><dd><img src="' . $windDirIcon . '" alt="" /><dd>' . $windDir . ' at <dd>' . $weather_data["wind"] . ' ' . $units['wind'] . '</dd></dt>';
         $content .= '<dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_TEMPERATURE . '</dt><dd>' . $weather_data["temperature"] . ' ' . $units['temp'] . '</dd>';
         $content .= '<dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_FELT_TEMPERATURE . '</dt><dd>' . $weather_data["feltTemperature"] . ' ' . $units['temp'] . '</dd>';
         $content .= '<dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_HUMIDITY . '</dt><dd>' . $weather_data["humidity"] . ' ' . '%</dd>';
         $content .= '<dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_PRESSURE . '</dt><dd>' . $weather_data['pressure'] . ' ' . $units['pres'] . '</dd>';
         $content .= '<dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_VISIBILITY . '</dt><dd>' . $weather_data["visibility"] . ' ' . $units['vis'] . '</dd>';
         $content .= '<dt>' . PLUGIN_SIDEBAR_WEATHER_DATA_UPDATE . '</dt><dd>' . $localTime . '</dd></dl>';
     } else {
         $content = 'Loading the  <a href=http://pear.php.net/package/Services_Weather/>PEAR Services/Weather module</a> failed.  Please insure that the module is installed.';
     }
     echo $content;
 }
Example #15
0
 /**
  * The content to go in this block.
  *
  * @return string   The content
  */
 function _content()
 {
     if (!@(include_once 'Services/Weather.php')) {
         Horde::logMessage('The weather.com block will not work without Services_Weather from PEAR. Run pear install Services_Weather.', __FILE__, __LINE__, PEAR_LOG_ERR);
         return _("The weather.com block is not available.");
     }
     global $conf;
     $cacheDir = Horde::getTempDir();
     $html = '';
     if (empty($this->_params['location'])) {
         return _("No location is set.");
     }
     $weatherDotCom =& Services_Weather::service("WeatherDotCom");
     $weatherDotCom->setAccountData(isset($conf['weatherdotcom']['partner_id']) ? $conf['weatherdotcom']['partner_id'] : '', isset($conf['weatherdotcom']['license_key']) ? $conf['weatherdotcom']['license_key'] : '');
     if (!$cacheDir) {
         return PEAR::raiseError(_("No temporary directory available for cache."), 'horde.error');
     } else {
         $weatherDotCom->setCache("file", array("cache_dir" => $cacheDir . '/'));
     }
     $weatherDotCom->setDateTimeFormat("m.d.Y", "H:i");
     $weatherDotCom->setUnitsFormat($this->_params['units']);
     $units = $weatherDotCom->getUnitsFormat();
     // If the user entered a zip code for the location, no need to
     // search (weather.com accepts zip codes as location IDs).
     // The location ID should already have been validated in
     // getParams.
     $search = preg_match('/\\b(?:\\d{5}(-\\d{5})?)|(?:[A-Z]{4}\\d{4})\\b/', $this->_params['location'], $matches) ? $matches[0] : $weatherDotCom->searchLocation($this->_params['location']);
     if (is_a($search, 'PEAR_Error')) {
         return $search->getmessage();
     }
     if (is_array($search)) {
         // Several locations returned due to imprecise location parameter
         $html = _("Several locations possible with the parameter: ");
         $html .= $this->_params['location'];
         $html .= "<br/><ul>";
         foreach ($search as $id_weather => $real_location) {
             $html .= "<li>{$real_location} ({$id_weather})</li>\n";
         }
         $html .= "</ul>";
         return $html;
     }
     $location = $weatherDotCom->getLocation($search);
     if (is_a($location, 'PEAR_Error')) {
         return $location->getmessage();
     }
     $weather = $weatherDotCom->getWeather($search);
     if (is_a($weather, 'PEAR_Error')) {
         return $weather->getmessage();
     }
     $forecast = $weatherDotCom->getForecast($search, $this->_params['days']);
     if (is_a($forecast, 'PEAR_Error')) {
         return $forecast->getmessage();
     }
     // Location and local time.
     $html .= "<table width=100%><tr><td class=control>";
     $html .= '<b>' . $location['name'] . '</b>' . ' local time ' . $location['time'];
     $html .= "</b></td></tr></table>";
     // Sunrise/sunset.
     $html .= '<b>' . _("Sunrise: ") . '</b>' . Horde::img('block/sunrise/sunrise.gif', _("Sunrise")) . $location['sunrise'];
     $html .= ' <b>' . _("Sunset: ") . '</b>' . Horde::img('block/sunrise/sunset.gif', _("Sunset")) . $location['sunset'];
     // Temperature.
     $html .= '<br /><b>' . _("Temperature: ") . '</b>';
     $html .= $weather['temperature'] . '&deg;' . String::upper($units['temp']);
     // Dew point.
     $html .= ' <b>' . _("Dew point: ") . '</b>';
     $html .= $weather['dewPoint'] . '&deg;' . String::upper($units['temp']);
     // Feels like temperature.
     $html .= ' <b>' . _("Feels like: ") . '</b>';
     $html .= $weather['feltTemperature'] . '&deg;' . String::upper($units['temp']);
     // Pressure and trend.
     $html .= '<br /><b>' . _("Pressure: ") . '</b>';
     $html .= number_format($weather['pressure'], 2) . ' ' . $units['pres'];
     $html .= _(" and ") . $weather['pressureTrend'];
     // Wind.
     $html .= '<br /><b>' . _("Wind: ") . '</b>';
     if ($weather['windDirection'] == 'VAR') {
         $html .= _("Variable");
     } elseif ($weather['windDirection'] == 'CALM') {
         $html .= _("Calm");
     } else {
         $html .= _("From the ") . $weather['windDirection'];
         $html .= ' (' . $weather['windDegrees'] . ')';
     }
     $html .= _(" at ") . $weather['wind'] . ' ' . $units['wind'];
     // Humidity.
     $html .= '<br /><b>' . _("Humidity: ") . '</b>';
     $html .= $weather['humidity'] . '%';
     // Visibility.
     $html .= ' <b>' . _("Visibility: ") . '</b>';
     $html .= $weather['visibility'] . (is_numeric($weather['visibility']) ? ' ' . $units['vis'] : '');
     // UV index.
     $html .= ' <b>' . _("U.V. index: ") . '</b>';
     $html .= $weather['uvIndex'] . ' - ' . $weather['uvText'];
     // Current condition.
     $html .= '<br /><b>' . _("Current condition: ") . '</b>' . Horde::img('block/weatherdotcom/32x32/' . $weather['conditionIcon'] . '.png', _(String::lower($weather['condition'])), 'align="middle"');
     $html .= ' ' . $weather['condition'];
     // Do the forecast now.
     $html .= '<table border="0" width="100%" align="center"><tr>';
     $html .= '<tr><td class="control" colspan="' . $this->_params['days'] * 2 . '"><center><b>' . $this->_params['days'] . '-day forecast</b></center></td></tr><tr>';
     $futureDays = 0;
     foreach ($forecast['days'] as $which => $day) {
         $html .= '<td colspan="2" align="center">';
         // Day name.
         $html .= '<b>';
         if ($which == 0) {
             $html .= _("Today");
         } elseif ($which == 1) {
             $html .= _("Tomorrow");
         } else {
             $html .= strftime('%A', mktime(0, 0, 0, date('m'), date('d') + $futureDays, date('Y')));
         }
         $html .= '</b><br />';
         $futureDays++;
         // High/low temp. If after 2 p.m. local time, the "day"
         // forecast is no longer valid.
         if ($which > 0 || $which == 0 && strtotime($location['time']) < strtotime('14:00')) {
             $html .= '<span style="color:red">' . $day['tempertureHigh'] . '&deg;' . String::upper($units['temp']) . '</span>/';
         }
         $html .= '<span style="color:blue">' . $day['temperatureLow'] . '&deg;' . String::upper($units['temp']) . '</span>';
         $html .= '</td>';
     }
     $html .= '</tr><tr>';
     $elementWidth = 100 / ($this->_params['days'] * 2);
     foreach ($forecast['days'] as $which => $day) {
         // Day forecast.
         $html .= '<td align="center" valign="top" width="' . $elementWidth . '%">';
         if ($which > 0 || $which == 0 && strtotime($location['time']) < strtotime('14:00')) {
             $html .= '<b><i>Day</i></b><br />';
             $html .= Horde::img('block/weatherdotcom/23x23/' . $day['day']['conditionIcon'] . '.png', $day['day']['condition']);
             $html .= '<br />' . $day['day']['condition'];
         } else {
             $html .= '&nbsp;';
         }
         $html .= '</td>';
         // Night forecast.
         $html .= '<td align="center" valign="top" width="' . $elementWidth . '%">';
         $html .= '<b><i>Night</i></b><br />';
         $html .= Horde::img('block/weatherdotcom/23x23/' . $day['night']['conditionIcon'] . '.png', $day['night']['condition']);
         $html .= '<br />' . $day['night']['condition'];
         $html .= '</td>';
     }
     $html .= '</tr></table>';
     // Display a bar at the bottom of the block with the required
     // attribution to weather.com and the logo, both linked to
     // weather.com with the partner ID.
     $html .= '<table width=100%><tr>';
     $html .= '<td align=right class=control>';
     $html .= 'Weather data provided by ';
     $html .= Horde::link('http://www.weather.com/?prod=xoap&par=' . $weatherDotCom->_partnerID, 'weather.com', '', '_blank', '', 'weather.com');
     $html .= '<i>weather.com</i>&reg; ';
     $html .= Horde::img('block/weatherdotcom/32x32/TWClogo_32px.png', 'weather.com logo');
     $html .= '</a></td></tr></table>';
     return $html;
 }