]> git.mxchange.org Git - friendica-addons.git/blob - curweather/getweather.php
added more weather data, change curtemp to curweather
[friendica-addons.git] / curweather / getweather.php
1 <?php
2 /*
3 File Name: getweather.php
4 Author: Gary White
5 Original version: May 12, 2005
6 Last modified: August 7, 2008
7
8 Copyright (C) 2004-2008 Gary White
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License in the included gpl.txt file for
18 details.
19
20 See http://apptools.com/phptools/xml/weather/ for usage information
21
22 See http://weather.gov/data/current_obs/
23 Also see 
24         http://weather.gov/alerts/
25         http://weather.gov/forecasts/xml/
26
27 Complete list of Weather stations available at 
28         http://weather.gov/data/current_obs/index.xml
29
30 */
31 class GetWeather {
32
33     // Initialize some variables
34     static $itemdata;
35     static $itemname;
36     static $wxdata;
37
38
39     function get($rpt) {
40         
41         // URL for the XML file
42         $xmlurl="http://www.weather.gov/data/current_obs/$rpt.xml";
43
44         // Base url for the icons
45         $imgpath="http://weather.gov/weather/images/fcicons";
46
47         
48         self::$itemdata="";
49         self::$itemname="";
50         self::$wxdata=array();
51   
52         $icons=self::defineIcons();
53         $icon="";
54         $data="";
55         $report="";
56         
57         // create a new CURL resource
58         if($ch = curl_init()) {
59
60             // set URL and other appropriate options
61             curl_setopt($ch, CURLOPT_URL, $xmlurl);
62             curl_setopt($ch, CURLOPT_HEADER, trus);
63             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
64             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
65             
66             // grab URL and pass it to the browser
67             $data=curl_exec($ch);
68
69             $r=curl_getinfo($ch); //,CURLINFO_HTTP_CODE);
70
71             // close CURL resource, and free up system resources
72             curl_close($ch);
73
74             // Create an XML parser
75             $xml_parser = xml_parser_create();
76             
77             // Use case-folding so we are sure to find the tag in $map_array
78             // This will force all tags to upper case so we don't have to worry
79             // about matching the case of the original in our tests.
80             xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
81             
82             // Assign the element starting and ending event handlers
83             xml_set_element_handler($xml_parser, array(self,"startElement"), array(self,"endElement"));
84             
85             // Assign a function to handle character data
86             xml_set_character_data_handler($xml_parser, array(self,"characterData"));
87             
88             // Parse the file. This will place the data into an associative
89             // array assigned to the self::$wxdata variable
90             xml_parse($xml_parser,$data,true);
91             
92             // Free the parser object
93             xml_parser_free($xml_parser);
94             
95             // The OBSERVATION_TIME field of the returned XML will be in the
96             // format "Last Updated on May 18, 8:53 am CDT"
97             // We're going to change the format a bit.
98
99             // Strip out the "Last Updated on " portion of the date/time
100             // so we can display that separately in our tabular output
101             $datetime=str_replace("Last Updated on ","",self::$wxdata['OBSERVATION_TIME']);
102             
103             // We now have the format as "May 18, 8:53 am CDT"
104             // Now, get the time zone. It will be everything from
105             // the last space character to the end of the string.
106             $z=strrchr($datetime," ");
107
108             // Get the current year
109             $year=date("Y");
110
111             // Now, we stuff the year into the string, following the comma.
112             $datetime=str_replace(",",", $year",$datetime);
113
114             // This does leave a small potential issue where, if you get a 
115             // report between midnight and 1 a.m. on January 1, or the server
116             // is in a significantly different time zone than the report it 
117             // could be as late as 4 a.m. the year will be wrong because the 
118             // report will be from the previous year. I suppose it would be 
119             // possible to correct for that, but for that little bit, I'm 
120             // not going to worry about it.
121
122             // Now, strip out the time zone
123             $datetime=str_replace($z,"",$datetime);
124
125             // Format the date and time the way we want it and add
126             // back the time zone
127             $datetime=date("l F j, Y g:i A",strtotime($datetime)).$z;
128             self::$wxdata['OBSERVATION_TIME']=$datetime;
129             
130             // Get the WEATHER element
131             $wx=trim(self::$wxdata['WEATHER']);
132
133             // Now, get the icon to match the weather
134             foreach($icons as $k=>$i){
135                 $a=explode(" | ",$i);
136                 if(is_numeric(array_search($wx,$a))){
137                     self::$wxdata['ICON']="$imgpath/$k.jpg";
138                     break;
139                 }
140             }
141
142             // Replace any null elements with "Not available"
143             foreach(array_keys(self::$wxdata) as $key){
144                 self::$wxdata[$key]=self::$wxdata[$key]=="NULL"?"Not available":self::$wxdata[$key];
145             }
146
147             // If we got humidity
148             if(is_numeric(self::$wxdata['RELATIVE_HUMIDITY']))
149                 // Append a percent sign
150                 self::$wxdata['RELATIVE_HUMIDITY'].="%";
151
152             // Do some formatting to make the output a little friendlier
153             if(self::$wxdata['VISIBILITY_MI']=="NA")
154                 self::$wxdata['VISIBILITY']="Not available";
155             if(self::$wxdata['VISIBILITY']!="Not available")
156                 self::$wxdata['VISIBILITY']=(1*self::$wxdata['VISIBILITY_MI'])." miles";
157
158             // If we got wind data
159             if(is_numeric(self::$wxdata['WIND_MPH'])){
160                 // We're going to output wind data as both MPH from a cardinal direction
161                 // and as Knots from a direction in degrees
162
163                 // Calculate the value for Knots
164                 self::$wxdata['WIND_KNOTS']=self::$wxdata['WIND_MPH']/1.15;
165
166                 // Format the output
167                 $wind=sprintf("From the %s at %d mph (%03.0f&deg; at %d knots)",self::$wxdata['WIND_DIR'],self::$wxdata['WIND_MPH'],self::$wxdata['WIND_DEGREES'],self::$wxdata['WIND_KNOTS']);
168
169                 // If we got a value for wind gusts
170                 if(is_numeric(self::$wxdata['WIND_GUST_MPH']) && self::$wxdata['WIND_GUST_MPH']>0){
171                     // add it into the wind string
172                     $wind=str_replace("mph","gusting to ".self::$wxdata['WIND_GUST_MPH']." mph<br>", $wind);
173                     $knots=sprintf("%d",self::$wxdata['WIND_GUST_MPH']/1.15);
174                     $wind=str_replace("knots","gusting to $knots knots",$wind);
175                 }
176             } else {
177                 // Otherwise, if wind is zero, we'll show "Calm"
178                 $wind=self::$wxdata['WIND_MPH']=="Not available"?"Not available":"Calm";
179             } // Done with wind
180             self::$wxdata['WIND_STRING']=$wind;
181
182         } // Done getting and formatting the data
183         return self::$wxdata;
184     }
185     
186     function startElement($parser, $name, $attrs) {
187         self::$itemname=$name;
188         self::$itemdata="";
189     }
190
191     function endElement($parser, $name) {
192         self::$wxdata[self::$itemname]=self::$itemdata;
193         self::$itemdata="";
194     }
195
196     function characterData($parser, $data) {
197         self::$itemdata.=$data;
198     }
199
200     function defineIcons(){
201         // See http://weather.gov/data/current_obs/weather.php for source data for this function
202         $retVal['bkn']="Mostly Cloudy | Mostly Cloudy with Haze | Mostly Cloudy and Breezy";
203         $retVal['skc']="Fair | Clear | Fair with Haze | Clear with Haze | Fair and Breezy | Clear and Breezy";
204         $retVal['few']="A Few Clouds | A Few Clouds with Haze | A Few Clouds and Breezy";
205         $retVal['sct']="Partly Cloudy | Party Cloudy with Haze | Partly Cloudy and Breezy";
206         $retVal['ovc']="Overcast | Overcast with Haze | Overcast and Breezy";
207         $retVal['nfg']="Fog/Mist | Fog | Freezing Fog | Shallow Fog | Partial Fog | Patches of Fog | Fog in Vicinity | Freezing Fog in Vicinity | Shallow Fog in Vicinity | Partial Fog in Vicinity | Patches of Fog in Vicinity | Showers in Vicinity Fog | Light Freezing Fog | Heavy Freezing Fog";
208         $retVal['smoke']="Smoke";
209         $retVal['fzra']="Freezing Rain | Freezing Drizzle | Light Freezing Rain | Light Freezing Drizzle | Heavy Freezing Rain | Heavy Freezing Drizzle | Freezing Rain in Vicinity | Freezing Drizzle in Vicinity";
210         $retVal['ip']="Ice Pellets | Light Ice Pellets | Heavy Ice Pellets | Ice Pellets in Vicinity | Showers Ice Pellets | Thunderstorm Ice Pellets | Ice Crystals | Hail | Small Hail/Snow Pellets | Light Small Hail/Snow Pellets | Heavy Small Hail/Snow Pellets | Showers Hail | Hail Showers";
211         $retVal['mix']="Freezing Rain Snow | Light Freezing Rain Snow | Heavy Freezing Rain Snow | Freezing Drizzle Snow | Light Freezing Drizzle Snow | Heavy Freezing Drizzle Snow | Snow Freezing Rain| Light Snow Freezing Rain | Heavy Snow Freezing Rain | Snow Freezing Drizzle | Light Snow Freezing Drizzle | Heavy Snow Freezing Drizzle";
212         $retVal['raip']="Rain Ice Pellets | Light Rain Ice Pellets | Heavy Rain Ice Pellets | Drizzle Ice Pellets | Light Drizzle Ice Pellets | Heavy Drizzle Ice Pellets | Ice Pellets Rain | Light Ice Pellets Rain | Heavy Ice Pellets Rain | Ice Pellets Drizzle | Light Ice Pellets Drizzle | Heavy Ice Pellets Drizzle";
213         $retVal['rasn']="Rain Snow | Light Rain Snow | Heavy Rain Snow | Snow Rain | Light Snow Rain | Heavy Snow Rain | Drizzle Snow | Light Drizzle Snow | Heavy Drizzle Snow | Snow Drizzle | Light Snow Drizzle | Heavy Snow Drizzle";
214         $retVal['shra']="Rain Showers | Light Rain Showers | Heavy Rain Showers | Rain Showers in Vicinity | Light Showers Rain | Heavy Showers Rain | Showers Rain | Showers Rain in Vicinity | Rain Showers Fog/Mist | Light Rain Showers Fog/Mist | Heavy Rain Showers Fog/Mist | Rain Showers in Vicinity Fog/Mist | Light Showers Rain Fog/Mist | Heavy Showers Rain Fog/Mist | Showers Rain Fog/Mist | Showers Rain in Vicinity Fog/Mist";
215         $retVal['tsra']="Thunderstorm | Light Thunderstorm Rain | Heavy Thunderstorm Rain | Thunderstorm Rain Fog/Mist | Light Thunderstorm Rain Fog/Mist | Heavy Thunderstorm Rain Fog/Mist | Thunderstorm Showers in Vicinity | | Light Thunderstorm Rain Haze | Heavy Thunderstorm Rain Haze | Thunderstorm Fog | Light Thunderstorm Rain Fog | Heavy Thunderstorm Rain Fog | Thunderstorm Light Rain | Thunderstorm Heavy Rain | Thunderstorm Rain Fog/Mist | Thunderstorm Light Rain Fog/Mist | Thunderstorm Heavy Rain Fog/Mist | Thunderstorm in Vicinity Fog/Mist | Thunderstorm Showers in Vicinity | Thunderstorm in Vicinity | Thunderstorm in Vicinity Haze | Thunderstorm Haze in Vicinity | Thunderstorm Light Rain Haze | Thunderstorm Heavy Rain Haze | Thunderstorm Fog | Thunderstorm Light Rain Fog | Thunderstorm Heavy Rain Fog | Thunderstorm Hail | Light Thunderstorm Rain Hail | Heavy Thunderstorm Rain Hail | Thunderstorm Rain Hail Fog/Mist | Light Thunderstorm Rain Hail Fog/Mist | Heavy Thunderstorm Rain Hail Fog/Mist | Thunderstorm Showers in Vicinity Hail | | Light Thunderstorm Rain Hail Haze | Heavy Thunderstorm Rain Hail Haze | Thunderstorm Hail Fog | Light Thunderstorm Rain Hail Fog | Heavy Thunderstorm Rain Hail Fog | Thunderstorm Light Rain Hail | Thunderstorm Heavy Rain Hail | Thunderstorm Rain Hail Fog/Mist | Thunderstorm Light Rain Hail Fog/Mist | Thunderstorm Heavy Rain Hail Fog/Mist | Thunderstorm in Vicinity Hail Fog/Mist | Thunderstorm Showers in Vicinity Hail | Thunderstorm in Vicinity Hail | Thunderstorm in Vicinity Hail Haze | Thunderstorm Haze in Vicinity Hail | Thunderstorm Light Rain Hail Haze | Thunderstorm Heavy Rain Hail Haze | Thunderstorm Hail Fog | Thunderstorm Light Rain Hail Fog | Thunderstorm Heavy Rain Hail Fog | Thunderstorm Small Hail/Snow Pellets | Thunderstorm Rain Small Hail/Snow Pellets | Light Thunderstorm Rain Small Hail/Snow Pellets | Heavy Thunderstorm Rain Small Hail/Snow Pellets";
216         $retVal['sn']="Snow | Light Snow | Heavy Snow | Snow Showers | Light Snow Showers | Heavy Snow Showers | Showers Snow | Light Showers Snow | Heavy Showers Snow | Snow Fog/Mist | Light Snow Fog/Mist | Heavy Snow Fog/Mist | Snow Showers Fog/Mist | Light Snow Showers Fog/Mist | Heavy Snow Showers Fog/Mist | Showers Snow Fog/Mist | Light Showers Snow Fog/Mist | Heavy Showers Snow Fog/Mist | Snow Fog | Light Snow Fog | Heavy Snow Fog | Snow Showers Fog | Light Snow Showers Fog | Heavy Snow Showers Fog | Showers Snow Fog | Light Showers Snow Fog | Heavy Showers Snow Fog | Showers in Vicinity Snow | Snow Showers in Vicinity | Snow Showers in Vicinity Fog/Mist | Snow Showers in Vicinity Fog | Low Drifting Snow | Blowing Snow | Snow Low Drifting Snow | Snow Blowing Snow | Light Snow Low Drifting Snow | Light Snow Blowing Snow | Heavy Snow Low Drifting Snow | Heavy Snow Blowing Snow | Thunderstorm Snow | Light Thunderstorm Snow | Heavy Thunderstorm Snow | Snow Grains | Light Snow Grains | Heavy Snow Grains | Heavy Blowing Snow | Blowing Snow in Vicinity";
217         $retVal['wind']="Windy | Fair and Windy | A Few Clouds and Windy | Partly Cloudy and Windy | Mostly Cloudy and Windy | Overcast and Windy";
218         $retVal['hi_shwrs']="Showers in Vicinity | Showers in Vicinity Fog/Mist | Showers in Vicinity Fog | Showers in Vicinity Haze";
219         $retVal['fzrara']="Freezing Rain Rain | Light Freezing Rain Rain | Heavy Freezing Rain Rain | Rain Freezing Rain | Light Rain Freezing Rain | Heavy Rain Freezing Rain | Freezing Drizzle Rain | Light Freezing Drizzle Rain | Heavy Freezing Drizzle Rain | Rain Freezing Drizzle | Light Rain Freezing Drizzle | Heavy Rain Freezing Drizzle";
220         $retVal['hi_tsra']="Thunderstorm in Vicinity | Thunderstorm in Vicinity Fog/Mist | Thunderstorm in Vicinity Fog | Thunderstorm Haze in Vicinity | Thunderstorm in Vicinity Haze";
221         $retVal['ra1']="Light Rain | Drizzle | Light Drizzle | Heavy Drizzle | Light Rain Fog/Mist | Drizzle Fog/Mist | Light Drizzle Fog/Mist | Heavy Drizzle Fog/Mist | Light Rain Fog | Drizzle Fog | Light Drizzle Fog | Heavy Drizzle Fog";
222         $retVal['ra']="Rain | Heavy Rain | Rain Fog/Mist | Heavy Rain Fog/Mist | Rain Fog | Heavy Rain Fog";
223         $retVal['nsvrtsra']="Funnel Cloud | Funnel Cloud in Vicinity | Tornado/Water Spout";
224         $retVal['dust']="Dust | Low Drifting Dust | Blowing Dust | Sand | Blowing Sand | Low Drifting Sand | Dust/Sand Whirls | Dust/Sand Whirls in Vicinity | Dust Storm | Heavy Dust Storm | Dust Storm in Vicinity | Sand Storm | Heavy Sand Storm | Sand Storm in Vicinity";
225         $retVal['mist']="Haze";
226         return $retVal;
227     }
228 // end CLASS
229 }
230 ?>