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.39 2004/10/01 13:01:34 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""bft"),
  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, fps and bft
  357.     *
  358.     * Function will return "false" when trying to convert from
  359.     * Beaufort, as it is a scale and not a true measurement
  360.     *
  361.     * @param    float                       $speed 
  362.     * @param    string                      $from 
  363.     * @param    string                      $to 
  364.     * @return   float|int|bool
  365.     * @access   public
  366.     * @link     http://www.spc.noaa.gov/faq/tornado/beaufort.html
  367.     */
  368.     function convertSpeed($speed$from$to)
  369.     {
  370.         $from strtolower($from);
  371.         $to   strtolower($to);
  372.  
  373.         static $factor;
  374.         static $beaufort;
  375.         if (!isset($factor)) {
  376.             $factor = array(
  377.                 "mph" => array(
  378.                     "mph" => 1,         "kmh" => 1.609344"kt" => 0.8689762"mps" => 0.44704,   "fps" => 1.4666667
  379.                 ),
  380.                 "kmh" => array(
  381.                     "mph" => 0.6213712"kmh" => 1,        "kt" => 0.5399568"mps" => 0.2777778"fps" => 0.9113444
  382.                 ),
  383.                 "kt"  => array(
  384.                     "mph" => 1.1507794"kmh" => 1.852,    "kt" => 1,         "mps" => 0.5144444"fps" => 1.6878099
  385.                 ),
  386.                 "mps" => array(
  387.                     "mph" => 2.2369363"kmh" => 3.6,      "kt" => 1.9438445"mps" => 1,         "fps" => 3.2808399
  388.                 ),
  389.                 "fps" => array(
  390.                     "mph" => 0.6818182"kmh" => 1.09728,  "kt" => 0.5924838"mps" => 0.3048,    "fps" => 1
  391.                 )
  392.             );
  393.  
  394.             // Beaufort scale, measurements are in knots
  395.             $beaufort = array(
  396.                   1,   3,   6,  10
  397.                  16,  21,  27,  33,
  398.                  40,  47,  55,  63    
  399.             );
  400.         }
  401.         
  402.         if ($from == "bft"{
  403.             return false;
  404.         elseif ($to == "bft"{
  405.             $speed round($speed $factor[$from]["kt"]0)
  406.             for ($i = 0; $i sizeof($beaufort)$i++{
  407.                 if ($speed <= $beaufort[$i]{
  408.                     return $i;
  409.                 }
  410.             }
  411.             return sizeof($beaufort);
  412.         else {
  413.             return round($speed $factor[$from][$to]2);
  414.         }
  415.     }
  416.     // }}}
  417.  
  418.     // {{{ convertPressure()
  419.     /**
  420.     * Convert pressure between in, hpa, mb, mm and atm
  421.     *
  422.     * @param    float                       $pressure 
  423.     * @param    string                      $from 
  424.     * @param    string                      $to 
  425.     * @return   float 
  426.     * @access   public
  427.     */
  428.     function convertPressure($pressure$from$to)
  429.     {
  430.         $from strtolower($from);
  431.         $to   strtolower($to);
  432.  
  433.         static $factor;
  434.         if (!isset($factor)) {
  435.             $factor = array(
  436.                 "in"   => array(
  437.                     "in" => 1,         "hpa" => 33.863887"mb" => 33.863887"mm" => 25.4,      "atm" => 0.0334213
  438.                 ),
  439.                 "hpa"  => array(
  440.                     "in" => 0.02953,   "hpa" => 1,         "mb" => 1,         "mm" => 0.7500616"atm" => 0.0009869
  441.                 ),
  442.                 "mb"   => array(
  443.                     "in" => 0.02953,   "hpa" => 1,         "mb" => 1,         "mm" => 0.7500616"atm" => 0.0009869
  444.                 ),
  445.                 "mm"   => array(
  446.                     "in" => 0.0393701"hpa" => 1.3332239"mb" => 1.3332239"mm" => 1,         "atm" => 0.0013158
  447.                 ),
  448.                 "atm"  => array(
  449.                     "in" => 29,921258"hpa" => 1013.2501"mb" => 1013.2501"mm" => 759.999952"atm" => 1
  450.                 )
  451.             );
  452.         }
  453.  
  454.         return round($pressure $factor[$from][$to]2);
  455.     }
  456.     // }}}
  457.  
  458.     // {{{ convertDistance()
  459.     /**
  460.     * Convert distance between km, ft and sm
  461.     *
  462.     * @param    float                       $distance 
  463.     * @param    string                      $from 
  464.     * @param    string                      $to 
  465.     * @return   float 
  466.     * @access   public
  467.     */
  468.     function convertDistance($distance$from$to)
  469.     {
  470.         $to   strtolower($to);
  471.         $from strtolower($from);
  472.  
  473.         static $factor;
  474.         if (!isset($factor)) {
  475.             $factor = array(
  476.                 "m" => array(
  477.                     "m" => 1,            "km" => 1000,      "ft" => 3.280839895"sm" => 0.0006213699
  478.                 ),
  479.                 "km" => array(
  480.                     "m" => 0.001,        "km" => 1,         "ft" => 3280.839895"sm" => 0.6213699
  481.                 ),
  482.                 "ft" => array(
  483.                     "m" => 0.3048,       "km" => 0.0003048"ft" => 1,           "sm" => 0.0001894
  484.                 ),
  485.                 "sm" => array(
  486.                     "m" => 0.0016093472"km" => 1.6093472"ft" => 5280.0106,   "sm" => 1
  487.                 )
  488.             );
  489.         }
  490.  
  491.         return round($distance $factor[$from][$to]2);
  492.     }
  493.     // }}}
  494.  
  495.     // {{{ calculateWindChill()
  496.     /**
  497.     * Calculate windchill from temperature and windspeed (enhanced formula)
  498.     *
  499.     * Temperature has to be entered in deg F, speed in mph!
  500.     *
  501.     * @param    float                       $temperature 
  502.     * @param    float                       $speed 
  503.     * @return   float 
  504.     * @access   public
  505.     * @link     http://www.nws.noaa.gov/om/windchill/
  506.     */
  507.     function calculateWindChill($temperature$speed)
  508.     {
  509.         return round(35.74 + 0.6215 * $temperature - 35.75 * pow($speed0.16+ 0.4275 * $temperature pow($speed0.16));
  510.     }
  511.     // }}}
  512.  
  513.     // {{{ calculateHumidity()
  514.     /**
  515.     * Calculate humidity from temperature and dewpoint
  516.     * This is only an approximation, there is no exact formula, this
  517.     * one here is called Magnus-Formula
  518.     *
  519.     * Temperature and dewpoint have to be entered in deg C!
  520.     *
  521.     * @param    float                       $temperature 
  522.     * @param    float                       $dewPoint 
  523.     * @return   float 
  524.     * @access   public
  525.     * @link     http://www.faqs.org/faqs/meteorology/temp-dewpoint/
  526.     */
  527.     function calculateHumidity($temperature$dewPoint)
  528.     {   
  529.         // First calculate saturation steam pressure for both temperatures
  530.         if ($temperature >= 0{
  531.             $a = 7.5;
  532.             $b = 237.3;
  533.         else {
  534.             $a = 7.6;
  535.             $b = 240.7;
  536.         }
  537.         $tempSSP = 6.1078 * pow(10($a $temperature($b $temperature));
  538.  
  539.         if ($dewPoint >= 0{
  540.             $a = 7.5;
  541.             $b = 237.3;
  542.         else {
  543.             $a = 7.6;
  544.             $b = 240.7;
  545.         }
  546.         $dewSSP  = 6.1078 * pow(10($a $dewPoint($b $dewPoint));
  547.         
  548.         return round(100 * $dewSSP $tempSSP1);
  549.     }
  550.     // }}}
  551.  
  552.     // {{{ calculateDewPoint()
  553.     /**
  554.     * Calculate dewpoint from temperature and humidity
  555.     * This is only an approximation, there is no exact formula, this
  556.     * one here is called Magnus-Formula
  557.     *
  558.     * Temperature has to be entered in deg C!
  559.     *
  560.     * @param    float                       $temperature 
  561.     * @param    float                       $humidity 
  562.     * @return   float 
  563.     * @access   public
  564.     * @link     http://www.faqs.org/faqs/meteorology/temp-dewpoint/
  565.     */
  566.     function calculateDewPoint($temperature$humidity)
  567.     {   
  568.         if ($temperature >= 0{
  569.             $a = 7.5;
  570.             $b = 237.3;
  571.         else {
  572.             $a = 7.6;
  573.             $b = 240.7;
  574.         }
  575.  
  576.         // First calculate saturation steam pressure for temperature
  577.         $SSP = 6.1078 * pow(10($a $temperature($b $temperature));
  578.  
  579.         // Steam pressure
  580.         $SP  $humidity / 100 * $SSP;
  581.  
  582.         $v   log($SP / 6.107810);
  583.  
  584.         return round($b $v ($a $v)1);
  585.     }
  586.     // }}}
  587.  
  588.     // {{{ polar2cartesian()
  589.     /**
  590.     * Convert polar coordinates to cartesian coordinates
  591.     *
  592.     * @param    float                       $latitude 
  593.     * @param    float                       $longitude 
  594.     * @return   array 
  595.     * @access   public
  596.     */
  597.     function polar2cartesian($latitude$longitude)
  598.     {
  599.         $theta deg2rad($latitude);
  600.         $phi   deg2rad($longitude);
  601.  
  602.         $x SERVICES_WEATHER_RADIUS_EARTH * cos($phicos($theta);
  603.         $y SERVICES_WEATHER_RADIUS_EARTH * sin($phicos($theta);
  604.         $z SERVICES_WEATHER_RADIUS_EARTH             * sin($theta);
  605.  
  606.         return array($x$y$z);
  607.     }
  608.     // }}}
  609. }
  610. // }}}
  611. ?>

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