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

Documentation generated on Mon, 11 Mar 2019 10:14:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.