Source for file Common.php
Documentation is available at Common.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Alexander Wirtz <alex@pc4p.net> |
// +----------------------------------------------------------------------+
// $Id: Common.php,v 1.39 2004/10/01 13:01:34 eru Exp $
* @package Services_Weather
require_once "Services/Weather.php";
// {{{ natural constants and measures
define("SERVICES_WEATHER_RADIUS_EARTH", 6378.15 );
// {{{ class Services_Weather_Common
* PEAR::Services_Weather_Common
* Parent class for weather-services. Defines common functions for unit
* conversions, checks for cache enabling and does other miscellaneous
* @author Alexander Wirtz <alex@pc4p.net>
* @package Services_Weather
* @license http://www.php.net/license/2_02.txt
* Format of the units provided (standard/metric/custom)
* @var string $_unitsFormat
* Custom format of the units
* @var array $_customUnitsFormat
var $_customUnitsFormat = array (
* Timeout for HTTP requests
* Format of the used dates
* @var string $_dateFormat
var $_dateFormat = "m/d/y";
* Format of the used times
* @var string $_timeFormat
var $_timeFormat = "G:i A";
* Object containing the location-data
* @var object stdClass $_location
* Object containing the weather-data
* @var object stdClass $_weather
* Object containing the forecast-data
* @var object stdClass $_forecast
* Cache, containing the data-objects
* @var object Cache $_cache
* Provides check for Cache
* @var bool $_cacheEnabled
var $_cacheEnabled = false;
* @see Science_Weather::Science_Weather
function Services_Weather_Common ($options, &$error)
// Set options accordingly
if (isset ($options["cacheType"])) {
if (isset ($options["cacheOptions"])) {
$status = $this->setCache($options["cacheType"], $options["cacheOptions"]);
$status = $this->setCache($options["cacheType"]);
if (isset ($options["unitsFormat"])) {
if (isset ($options["customUnitsFormat"])) {
$this->setUnitsFormat($options["unitsFormat"], $options["customUnitsFormat"]);
if (isset ($options["httpTimeout"])) {
if (isset ($options["dateFormat"])) {
if (isset ($options["timeFormat"])) {
* 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
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);
$this->_cacheEnabled = true;
$this->_cacheEnabled = false;
* Changes the representation of the units (standard/metric)
* @param string $unitsFormat
* @param array $customUnitsFormat
if (!isset ($acceptedFormats)) {
$acceptedFormats = array (
"temp" => array ("c", "f"),
"vis" => array ("m", "km", "ft", "sm"),
"height" => array ("m", "ft"),
"wind" => array ("mph", "kmh", "kt", "mps", "fps", "bft"),
"pres" => array ("in", "hpa", "mb", "mm", "atm"),
"rain" => array ("in", "mm")
$this->_unitsFormat = strtolower($unitsFormat{0 });
if ($this->_unitsFormat == "c" && is_array($customUnitsFormat)) {
foreach ($customUnitsFormat as $key => $value) {
$this->_customUnitsFormat[$key] = $value;
} elseif ($this->_unitsFormat == "c") {
$this->_unitsFormat = "s";
* Sets the timeout in seconds for HTTP requests
* @param int $httpTimeout
$this->_httpTimeout = $httpTimeout;
* Returns the selected units format
* @param string $unitsFormat
$unitsFormat = $this->_unitsFormat;
$c = $this->_customUnitsFormat;
// {{{ setDateTimeFormat()
* Changes the representation of time and dates (see http://www.php.net/date)
* @param string $dateFormat
* @param string $timeFormat
$this->_dateFormat = $dateFormat;
$this->_timeFormat = $timeFormat;
// {{{ convertTemperature()
* Convert temperature between f and c
* @param float $temperature
"f" => $temperature, "c" => ($temperature - 32 ) / 1.8
"f" => 1.8 * $temperature + 32 , "c" => $temperature
return round($result[$from][$to], 2 );
* Convert speed between mph, kmh, kt, mps, fps and bft
* Function will return "false" when trying to convert from
* Beaufort, as it is a scale and not a true measurement
* @link http://www.spc.noaa.gov/faq/tornado/beaufort.html
"mph" => 1 , "kmh" => 1.609344 , "kt" => 0.8689762 , "mps" => 0.44704 , "fps" => 1.4666667
"mph" => 0.6213712 , "kmh" => 1 , "kt" => 0.5399568 , "mps" => 0.2777778 , "fps" => 0.9113444
"mph" => 1.1507794 , "kmh" => 1.852 , "kt" => 1 , "mps" => 0.5144444 , "fps" => 1.6878099
"mph" => 2.2369363 , "kmh" => 3.6 , "kt" => 1.9438445 , "mps" => 1 , "fps" => 3.2808399
"mph" => 0.6818182 , "kmh" => 1.09728 , "kt" => 0.5924838 , "mps" => 0.3048 , "fps" => 1
// Beaufort scale, measurements are in knots
} elseif ($to == "bft") {
$speed = round($speed * $factor[$from]["kt"], 0 );
for ($i = 0; $i < sizeof($beaufort); $i++ ) {
if ($speed <= $beaufort[$i]) {
return round($speed * $factor[$from][$to], 2 );
* Convert pressure between in, hpa, mb, mm and atm
"in" => 1 , "hpa" => 33.863887 , "mb" => 33.863887 , "mm" => 25.4 , "atm" => 0.0334213
"in" => 0.02953 , "hpa" => 1 , "mb" => 1 , "mm" => 0.7500616 , "atm" => 0.0009869
"in" => 0.02953 , "hpa" => 1 , "mb" => 1 , "mm" => 0.7500616 , "atm" => 0.0009869
"in" => 0.0393701 , "hpa" => 1.3332239 , "mb" => 1.3332239 , "mm" => 1 , "atm" => 0.0013158
"in" => 29 ,921258 , "hpa" => 1013.2501 , "mb" => 1013.2501 , "mm" => 759.999952 , "atm" => 1
return round($pressure * $factor[$from][$to], 2 );
* Convert distance between km, ft and sm
"m" => 1 , "km" => 1000 , "ft" => 3.280839895 , "sm" => 0.0006213699
"m" => 0.001 , "km" => 1 , "ft" => 3280.839895 , "sm" => 0.6213699
"m" => 0.3048 , "km" => 0.0003048 , "ft" => 1 , "sm" => 0.0001894
"m" => 0.0016093472 , "km" => 1.6093472 , "ft" => 5280.0106 , "sm" => 1
return round($distance * $factor[$from][$to], 2 );
// {{{ calculateWindChill()
* Calculate windchill from temperature and windspeed (enhanced formula)
* Temperature has to be entered in deg F, speed in mph!
* @param float $temperature
* @link http://www.nws.noaa.gov/om/windchill/
return round(35.74 + 0.6215 * $temperature - 35.75 * pow($speed, 0.16 ) + 0.4275 * $temperature * pow($speed, 0.16 ));
// {{{ calculateHumidity()
* Calculate humidity from temperature and dewpoint
* This is only an approximation, there is no exact formula, this
* one here is called Magnus-Formula
* Temperature and dewpoint have to be entered in deg C!
* @param float $temperature
* @link http://www.faqs.org/faqs/meteorology/temp-dewpoint/
// First calculate saturation steam pressure for both temperatures
$tempSSP = 6.1078 * pow(10 , ($a * $temperature) / ($b + $temperature));
$dewSSP = 6.1078 * pow(10 , ($a * $dewPoint) / ($b + $dewPoint));
return round(100 * $dewSSP / $tempSSP, 1 );
// {{{ calculateDewPoint()
* Calculate dewpoint from temperature and humidity
* This is only an approximation, there is no exact formula, this
* one here is called Magnus-Formula
* Temperature has to be entered in deg C!
* @param float $temperature
* @link http://www.faqs.org/faqs/meteorology/temp-dewpoint/
// First calculate saturation steam pressure for temperature
$SSP = 6.1078 * pow(10 , ($a * $temperature) / ($b + $temperature));
$SP = $humidity / 100 * $SSP;
$v = log($SP / 6.1078 , 10 );
return round($b * $v / ($a - $v), 1 );
* Convert polar coordinates to cartesian coordinates
* @param float $longitude
return array ($x, $y, $z);
Documentation generated on Mon, 11 Mar 2019 14:17:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|