Common.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.29 2004/01/06 19:36:27 eru Exp $
  20. require_once "Services/Weather.php";
  21. // {{{ constants
  22. // {{{ natural constants and measures
  23. define("SERVICES_WEATHER_RADIUS_EARTH", 6378.15);
  24. // }}}
  25. // }}}
  26. // {{{ class Services_Weather_Common
  27. /**
  28. * PEAR::Services_Weather_Common
  29. *
  30. * Parent class for weather-services. Defines common functions for unit
  31. * conversions, checks for astronomy-class, cache enabling and does other
  32. * miscellaneous things.
  33. *
  34. * @author Alexander Wirtz <alex@pc4p.net>
  35. * @package Services_Weather
  36. * @license http://www.php.net/license/2_02.txt
  37. * @version 1.2
  38. */
  39. class Services_Weather_Common {
  40. // {{{ properties
  41. /**
  42. * Format of the units provided (standard/metric/custom)
  43. *
  44. * @var string $_unitsFormat
  45. * @access private
  46. */
  47. var $_unitsFormat = "s";
  48. /**
  49. * Custom format of the units
  50. *
  51. * @var array $_customUnitsFormat
  52. * @access private
  53. */
  54. var $_customUnitsFormat = array(
  55. "temp" => "f",
  56. "vis" => "sm",
  57. "wind" => "mph",
  58. "pres" => "in",
  59. "rain" => "in"
  60. );
  61. /**
  62. * Format of the used dates
  63. *
  64. * @var string $_dateFormat
  65. * @access private
  66. */
  67. var $_dateFormat = "m/d/y";
  68. /**
  69. * Format of the used times
  70. *
  71. * @var string $_timeFormat
  72. * @access private
  73. */
  74. var $_timeFormat = "G:i A";
  75. /**
  76. * Object containing the location-data
  77. *
  78. * @var object stdClass $_location
  79. * @access private
  80. */
  81. var $_location;
  82. /**
  83. * Object containing the weather-data
  84. *
  85. * @var object stdClass $_weather
  86. * @access private
  87. */
  88. var $_weather;
  89. /**
  90. * Object containing the forecast-data
  91. *
  92. * @var object stdClass $_forecast
  93. * @access private
  94. */
  95. var $_forecast;
  96. /**
  97. * Cache, containing the data-objects
  98. *
  99. * @var object Cache $_cache
  100. * @access private
  101. */
  102. var $_cache;
  103. /**
  104. * Provides check for Cache
  105. *
  106. * @var bool $_cacheEnabled
  107. * @access private
  108. */
  109. var $_cacheEnabled = false;
  110. // }}}
  111. // {{{ constructor
  112. /**
  113. * Constructor
  114. *
  115. * @param array $options
  116. * @param mixed $error
  117. * @throws PEAR_Error
  118. * @see Science_Weather::Science_Weather
  119. * @access private
  120. */
  121. function Services_Weather_Common($options, &$error)
  122. {
  123. // Set options accordingly
  124. if (isset($options["cacheType"])) {
  125. if (isset($options["cacheOptions"])) {
  126. $status = $this->setCache($options["cacheType"], $options["cacheOptions"]);
  127. } else {
  128. $status = $this->setCache($options["cacheType"]);
  129. }
  130. }
  131. if (Services_Weather::isError($status)) {
  132. $error = $status;
  133. return;
  134. }
  135. if (isset($options["unitsFormat"])) {
  136. if (isset($options["customUnitsFormat"])) {
  137. $this->setUnitsFormat($options["unitsFormat"], $options["customUnitsFormat"]);
  138. } else {
  139. $this->setUnitsFormat($options["unitsFormat"]);
  140. }
  141. }
  142. if (isset($options["dateFormat"])) {
  143. $this->setDateTimeFormat($options["dateFormat"], "");
  144. }
  145. if (isset($options["timeFormat"])) {
  146. $this->setDateTimeFormat("", $options["timeFormat"]);
  147. }
  148. }
  149. // }}}
  150. // {{{ setCache()
  151. /**
  152. * Enables caching the data, usage strongly recommended
  153. *
  154. * Requires Cache to be installed
  155. *
  156. * @param string $cacheType
  157. * @param array $cacheOptions
  158. * @return PEAR_Error|bool
  159. * @throws PEAR_Error::SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED
  160. * @access public
  161. */
  162. function setCache($cacheType = "file", $cacheOptions = array())
  163. {
  164. // The error handling in Cache is a bit crummy (read: not existent)
  165. // so we have to do that on our own...
  166. @include_once "Cache.php";
  167. @$cache = new Cache($cacheType, $cacheOptions);
  168. if (is_object($cache) && (get_class($cache) == "cache" || is_subclass_of($cache, "cache"))) {
  169. $this->_cache = $cache;
  170. $this->_cacheEnabled = true;
  171. } else {
  172. $this->_cache = null;
  173. $this->_cacheEnabled = false;
  174. return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED);
  175. }
  176. return true;
  177. }
  178. // }}}
  179. // {{{ setUnitsFormat()
  180. /**
  181. * Changes the representation of the units (standard/metric)
  182. *
  183. * @param string $unitsFormat
  184. * @param array $customUnitsFormat
  185. * @access public
  186. */
  187. function setUnitsFormat($unitsFormat, $customUnitsFormat = array())
  188. {
  189. static $acceptedFormats;
  190. if (!isset($acceptedFormats)) {
  191. $acceptedFormats = array(
  192. "temp" => array("c", "f"),
  193. "vis" => array("km", "ft", "sm"),
  194. "wind" => array("mph", "kmh", "kt", "mps", "fps"),
  195. "pres" => array("in", "hpa", "mb", "mm", "atm"),
  196. "rain" => array("in", "mm")
  197. );
  198. }
  199. if (strlen($unitsFormat) && in_array(strtolower($unitsFormat{0}), array("c", "m", "s"))) {
  200. $this->_unitsFormat = strtolower($unitsFormat{0});
  201. if ($this->_unitsFormat == "c" && is_array($customUnitsFormat)) {
  202. foreach ($customUnitsFormat as $key => $value) {
  203. if (array_key_exists($key, $acceptedFormats) && in_array($value, $acceptedFormats[$key])) {
  204. $this->_customUnitsFormat[$key] = $value;
  205. }
  206. }
  207. } elseif ($this->_unitsFormat == "c") {
  208. $this->_unitsFormat = "s";
  209. }
  210. }
  211. }
  212. // }}}
  213. // {{{ getUnitsFormat()
  214. /**
  215. * Returns the selected units format
  216. *
  217. * @param string $unitsFormat
  218. * @return array
  219. * @access public
  220. */
  221. function getUnitsFormat($unitsFormat = "")
  222. {
  223. // This is cheap'o stuff
  224. if (strlen($unitsFormat) && in_array(strtolower($unitsFormat{0}), array("c", "m", "s"))) {
  225. $unitsFormat = strtolower($unitsFormat{0});
  226. } else {
  227. $unitsFormat = $this->_unitsFormat;
  228. }
  229. $c = $this->_customUnitsFormat;
  230. $m = array(
  231. "temp" => "c",
  232. "vis" => "km",
  233. "wind" => "kmh",
  234. "pres" => "mb",
  235. "rain" => "mm"
  236. );
  237. $s = array(
  238. "temp" => "f",
  239. "vis" => "sm",
  240. "wind" => "mph",
  241. "pres" => "in",
  242. "rain" => "in"
  243. );
  244. return ${$unitsFormat};
  245. }
  246. // }}}
  247. // {{{ setDateTimeFormat()
  248. /**
  249. * Changes the representation of time and dates (see http://www.php.net/date)
  250. *
  251. * @param string $dateFormat
  252. * @param string $timeFormat
  253. * @access public
  254. */
  255. function setDateTimeFormat($dateFormat = "", $timeFormat = "")
  256. {
  257. if (strlen($dateFormat)) {
  258. $this->_dateFormat = $dateFormat;
  259. }
  260. if (strlen($timeFormat)) {
  261. $this->_timeFormat = $timeFormat;
  262. }
  263. }
  264. // }}}
  265. // {{{ convertTemperature()
  266. /**
  267. * Convert temperature between f and c
  268. *
  269. * @param float $temperature
  270. * @param string $from
  271. * @param string $to
  272. * @return float
  273. * @access public
  274. */
  275. function convertTemperature($temperature, $from, $to)
  276. {
  277. $from = strtolower($from{0});
  278. $to = strtolower($to{0});
  279. $result = array(
  280. "f" => array(
  281. "f" => $temperature, "c" => ($temperature - 32) / 1.8
  282. ),
  283. "c" => array(
  284. "f" => 1.8 * $temperature + 32, "c" => $temperature
  285. )
  286. );
  287. return round($result[$from][$to], 2);
  288. }
  289. // }}}
  290. // {{{ convertSpeed()
  291. /**
  292. * Convert speed between mph, kmh, kt, mps and fps
  293. *
  294. * @param float $speed
  295. * @param string $from
  296. * @param string $to
  297. * @return float
  298. * @access public
  299. */
  300. function convertSpeed($speed, $from, $to)
  301. {
  302. $from = strtolower($from);
  303. $to = strtolower($to);
  304. static $factor;
  305. if (!isset($factor)) {
  306. $factor = array(
  307. "mph" => array(
  308. "mph" => 1, "kmh" => 1.609344, "kt" => 0.8689762, "mps" => 0.44704, "fps" => 1.4666667
  309. ),
  310. "kmh" => array(
  311. "mph" => 0.6213712, "kmh" => 1, "kt" => 0.5399568, "mps" => 0.2777778, "fps" => 0.9113444
  312. ),
  313. "kt" => array(
  314. "mph" => 1.1507794, "kmh" => 1.852, "kt" => 1, "mps" => 0.5144444, "fps" => 1.6878099
  315. ),
  316. "mps" => array(
  317. "mph" => 2.2369363, "kmh" => 3.6, "kt" => 1.9438445, "mps" => 1, "fps" => 3.2808399
  318. ),
  319. "fps" => array(
  320. "mph" => 0.6818182, "kmh" => 1.09728, "kt" => 0.5924838, "mps" => 0.3048, "fps" => 1
  321. )
  322. );
  323. }
  324. return round($speed * $factor[$from][$to], 2);
  325. }
  326. // }}}
  327. // {{{ convertPressure()
  328. /**
  329. * Convert pressure between in, hpa, mb, mm and atm
  330. *
  331. * @param float $pressure
  332. * @param string $from
  333. * @param string $to
  334. * @return float
  335. * @access public
  336. */
  337. function convertPressure($pressure, $from, $to)
  338. {
  339. $from = strtolower($from);
  340. $to = strtolower($to);
  341. static $factor;
  342. if (!isset($factor)) {
  343. $factor = array(
  344. "in" => array(
  345. "in" => 1, "hpa" => 33.863887, "mb" => 33.863887, "mm" => 25.4, "atm" => 0.0334213
  346. ),
  347. "hpa" => array(
  348. "in" => 0.02953, "hpa" => 1, "mb" => 1, "mm" => 0.7500616, "atm" => 0.0009869
  349. ),
  350. "mb" => array(
  351. "in" => 0.02953, "hpa" => 1, "mb" => 1, "mm" => 0.7500616, "atm" => 0.0009869
  352. ),
  353. "mm" => array(
  354. "in" => 0.0393701, "hpa" => 1.3332239, "mb" => 1.3332239, "mm" => 1, "atm" => 0.0013158
  355. ),
  356. "atm" => array(
  357. "in" => 29,921258, "hpa" => 1013.2501, "mb" => 1013.2501, "mm" => 759.999952, "atm" => 1
  358. )
  359. );
  360. }
  361. return round($pressure * $factor[$from][$to], 2);
  362. }
  363. // }}}
  364. // {{{ convertDistance()
  365. /**
  366. * Convert distance between km, ft and sm
  367. *
  368. * @param float $distance
  369. * @param string $from
  370. * @param string $to
  371. * @return float
  372. * @access public
  373. */
  374. function convertDistance($distance, $from, $to)
  375. {
  376. $to = strtolower($to);
  377. $from = strtolower($from);
  378. static $factor;
  379. if (!isset($factor)) {
  380. $factor = array(
  381. "km" => array(
  382. "km" => 1, "ft" => 3280.839895, "sm" => 0.6213699
  383. ),
  384. "ft" => array(
  385. "km" => 0.0003048, "ft" => 1, "sm" => 0.0001894
  386. ),
  387. "sm" => array(
  388. "km" => 1.6093472, "ft" => 5280.0106, "sm" => 1
  389. )
  390. );
  391. }
  392. return round($distance * $factor[$from][$to], 2);
  393. }
  394. // }}}
  395. // {{{ calculateWindChill()
  396. /**
  397. * Calculate windchill from temperature and windspeed (enhanced formula)
  398. *
  399. * Temperature has to be entered in deg F, speed in mph!
  400. *
  401. * @param float $temperature
  402. * @param float $speed
  403. * @return float
  404. * @access public
  405. * @link http://www.nws.noaa.gov/om/windchill/
  406. */
  407. function calculateWindChill($temperature, $speed)
  408. {
  409. return round(35.74 + 0.6215 * $temperature - 35.75 * pow($speed, 0.16) + 0.4275 * $temperature * pow($speed, 0.16));
  410. }
  411. // }}}
  412. // {{{ calculateHumidity()
  413. /**
  414. * Calculate humidity from temperature and dewpoint
  415. * This is only an approximation, there is no exact formula, this
  416. * one here is called Magnus-Formula
  417. *
  418. * Temperature and dewpoint have to be entered in deg C!
  419. *
  420. * @param float $temperature
  421. * @param float $dewPoint
  422. * @return float
  423. * @access public
  424. * @link http://www.faqs.org/faqs/meteorology/temp-dewpoint/
  425. */
  426. function calculateHumidity($temperature, $dewPoint)
  427. {
  428. // First calculate saturation steam pressure for both temperatures
  429. if ($temperature >= 0) {
  430. $a = 7.5;
  431. $b = 237.3;
  432. } else {
  433. $a = 7.6;
  434. $b = 240.7;
  435. }
  436. $tempSSP = 6.1078 * pow(10, ($a * $temperature) / ($b + $temperature));
  437. if ($dewPoint >= 0) {
  438. $a = 7.5;
  439. $b = 237.3;
  440. } else {
  441. $a = 7.6;
  442. $b = 240.7;
  443. }
  444. $dewSSP = 6.1078 * pow(10, ($a * $dewPoint) / ($b + $dewPoint));
  445. return round(100 * $dewSSP / $tempSSP, 1);
  446. }
  447. // }}}
  448. // {{{ calculateDewPoint()
  449. /**
  450. * Calculate dewpoint from temperature and humidity
  451. * This is only an approximation, there is no exact formula, this
  452. * one here is called Magnus-Formula
  453. *
  454. * Temperature has to be entered in deg C!
  455. *
  456. * @param float $temperature
  457. * @param float $humidity
  458. * @return float
  459. * @access public
  460. * @link http://www.faqs.org/faqs/meteorology/temp-dewpoint/
  461. */
  462. function calculateDewPoint($temperature, $humidity)
  463. {
  464. if ($temperature >= 0) {
  465. $a = 7.5;
  466. $b = 237.3;
  467. } else {
  468. $a = 7.6;
  469. $b = 240.7;
  470. }
  471. // First calculate saturation steam pressure for temperature
  472. $SSP = 6.1078 * pow(10, ($a * $temperature) / ($b + $temperature));
  473. // Steam pressure
  474. $SP = $humidity / 100 * $SSP;
  475. $v = log($SP / 6.1078, 10);
  476. return round($b * $v / ($a - $v), 1);
  477. }
  478. // }}}
  479. // {{{ polar2cartesian()
  480. /**
  481. * Convert polar coordinates to cartesian coordinates
  482. *
  483. * @param float $latitude
  484. * @param float $longitude
  485. * @return array
  486. * @access public
  487. */
  488. function polar2cartesian($latitude, $longitude)
  489. {
  490. $theta = deg2rad($latitude);
  491. $phi = deg2rad($longitude);
  492. $x = SERVICES_WEATHER_RADIUS_EARTH * cos($phi) * cos($theta);
  493. $y = SERVICES_WEATHER_RADIUS_EARTH * sin($phi) * cos($theta);
  494. $z = SERVICES_WEATHER_RADIUS_EARTH * sin($theta);
  495. return array($x, $y, $z);
  496. }
  497. // }}}
  498. }
  499. // }}}
  500. ?>