Services_Weather
[ class tree: Services_Weather ] [ index: Services_Weather ] [ all elements ]

Source for file Common.php

Documentation is available at Common.php

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

Documentation generated on Mon, 11 Mar 2019 13:52:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.