Mercurial > ~darius > hgwebdir.cgi > wh1080
comparison web/db.php @ 0:9dab44dcb331
Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 09 Feb 2010 13:44:25 +1030 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9dab44dcb331 |
---|---|
1 <!-- for Emacs, this is a -*- mode: html-fill; coding: utf-8 -*- document --> | |
2 <!-- $Id: db.php,v 1.6 2010/02/07 03:25:04 grog Exp $ --> | |
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
5 | |
6 <?php | |
7 { | |
8 $title = "Dereel weather observations"; | |
9 $subtitle = ""; | |
10 include "header.php"; | |
11 include "weathergraph.php"; | |
12 $id = '$Id: db.php,v 1.6 2010/02/07 03:25:04 grog Exp $'; | |
13 } | |
14 ?> | |
15 | |
16 <html xmlns="http://www.w3.org/1999/xhtml"> | |
17 <head> | |
18 <?php pagetitle0 ($title); ?> | |
19 <meta http-equiv="refresh" content="900" ;=""> | |
20 </head> | |
21 | |
22 <body> | |
23 <?php pageheader0 ($title); ?> | |
24 | |
25 <div align="justify"> | |
26 <p> | |
27 This is an experimental page that I'm working on as part of my weather reporting software. | |
28 It'll grow over time. In the meantime, you can get more complete version of this | |
29 information from the <?php href | |
30 ("http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IVICTORI124", | |
31 "Wunderground page"); ?> for this station. | |
32 </p> | |
33 | |
34 <?php | |
35 | |
36 { | |
37 $doplot = "/home/grog/src/weather/WH-1080/plots/doplots"; | |
38 $weatherdir = "/home/grog/public_html/weather"; | |
39 $me = basename ($_SERVER ["SCRIPT_FILENAME"]); | |
40 | |
41 if (array_key_exists ("date", $_GET)) | |
42 { | |
43 $mydate = validdate ($_GET ["date"], "date"); | |
44 if (is_array ($mydate)) /* valid date */ | |
45 $date = formatdate ("Y-m-d", $mydate); | |
46 else | |
47 { | |
48 print <<< EOS | |
49 <p> | |
50 <font color="red">Invalid date: $mydate. Using today's date</font> | |
51 </p> | |
52 EOS; | |
53 $date = date ("Y-m-d"); | |
54 } | |
55 } | |
56 else | |
57 { | |
58 $mydate = getdate (); | |
59 $date = date ("Y-m-d"); | |
60 } | |
61 | |
62 $istoday = $date == date ("Y-m-d"); /* if today, get current readings */ | |
63 | |
64 /* Environment to check for graphs */ | |
65 $yesterday = formatdate ("Ymd", (addsecs ($mydate, -86400))); | |
66 $tomorrow = formatdate ("Ymd", (addsecs ($mydate, 86400))); | |
67 | |
68 | |
69 print <<< EOS | |
70 | |
71 <!-- Select new date --> | |
72 <table> | |
73 <tr> | |
74 <td align="left"> | |
75 <form action="$me" method="get"> | |
76 <input type="submit" value="Previous day"/> | |
77 <input size="20" maxlength="20" type="hidden" name="date" value="$yesterday"/> | |
78 </form> | |
79 </td> | |
80 | |
81 <td> | |
82 <form action="$me" method="get"> | |
83 <table summary="Parameter input" cellspacing="2" border="0"> | |
84 <tr> | |
85 <td> | |
86 <input type="submit" value="New date:"/> | |
87 </td> | |
88 | |
89 <!-- value --> | |
90 <td> | |
91 <input size="20" maxlength="20" type="text" name="date" value="$date" /> | |
92 </td> | |
93 </tr> | |
94 </table> | |
95 </form> | |
96 </td> | |
97 EOS; | |
98 | |
99 if (! $istoday) | |
100 print <<< EOS | |
101 <td align="right"> | |
102 <form action="$me" method="get"> | |
103 <input type="submit" value="Next day"/> | |
104 <input size="20" maxlength="20" type="hidden" name="date" value="$tomorrow"/> | |
105 </form> | |
106 </td> | |
107 | |
108 <td align="right"> | |
109 <form action="$me" method="get"> | |
110 <input type="submit" value="Today"/> | |
111 </form> | |
112 </td> | |
113 | |
114 EOS; | |
115 print <<< EOS | |
116 | |
117 </tr> | |
118 </table> | |
119 | |
120 EOS; | |
121 | |
122 /* Set up database stuff */ | |
123 /* XXX This stuff should come from config */ | |
124 require "db.inc"; | |
125 $hostname = "localhost"; | |
126 $username = "grog"; | |
127 $password = ""; | |
128 $database = "weather"; | |
129 $dbtable = "observations"; | |
130 $station_id = "Dereel"; | |
131 | |
132 /* Connect to the server */ | |
133 if (! ($connection = @ mysql_pconnect ($hostname, $username, $password))) | |
134 showerror (); | |
135 | |
136 if (! mysql_select_db ($database, $connection)) | |
137 showerror (); | |
138 | |
139 if ($istoday) | |
140 { | |
141 $now = time (); | |
142 $start = time () - 600; /* 5 minutes ago */ | |
143 | |
144 $wind_directions = array ("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", | |
145 "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"); | |
146 $result = mysql_query (<<< EOS | |
147 SELECT AVG(inside_humidity), | |
148 AVG(inside_temp), | |
149 AVG(inside_dewpoint), | |
150 AVG(outside_humidity), | |
151 AVG(outside_temp), | |
152 AVG(outside_dewpoint), | |
153 AVG(pressure_msl), | |
154 AVG(wind_speed), | |
155 AVG(wind_gust), | |
156 AVG(wind_direction), | |
157 SUM(rain) | |
158 FROM $dbtable | |
159 WHERE unix_timestamp(timestamp(date,time)) >= $start | |
160 AND unix_timestamp(timestamp(date, time)) <= $now | |
161 AND station_id = "$station_id" | |
162 EOS | |
163 , $connection ); | |
164 | |
165 if (! $result) | |
166 showerror (); | |
167 if ($row = mysql_fetch_array ($result, MYSQL_NUM)) | |
168 { | |
169 $inside_humidity = sprintf ("%2.0f", $row [0]); | |
170 $inside_temperature = sprintf ("%2.1f", $row [1]); | |
171 $inside_dewpoint = sprintf ("%2.1f", $row [2]); | |
172 $outside_humidity = sprintf ("%2.1f", $row [3]); | |
173 $outside_temperature = sprintf ("%2.1f", $row [4]); | |
174 $outside_dewpoint = sprintf ("%2.1f", $row [5]); | |
175 $pressure_msl = sprintf ("%2.1f", $row [6]); | |
176 $wind_speed = sprintf ("%2.1f", $row [7]); | |
177 $wind_gust = sprintf ("%2.1f", $row [8]); | |
178 $wind_direction = sprintf ("%2.1f", $row [9]); | |
179 $rain = sprintf ("%2.1f", $row [10]); | |
180 $wind_direction_text = $wind_directions [($row [9] + 11.25) / 22.5]; | |
181 } | |
182 } | |
183 | |
184 $result = mysql_query (<<< EOS | |
185 SELECT @max_inside_humidity := MAX(inside_humidity), | |
186 @min_inside_humidity := MIN(inside_humidity), | |
187 @max_inside_temp := MAX(inside_temp), | |
188 @min_inside_temp := MIN(inside_temp), | |
189 @max_inside_dewpoint := MAX(inside_dewpoint), | |
190 @min_inside_dewpoint := MIN(inside_dewpoint), | |
191 @max_outside_humidity := MAX(outside_humidity), | |
192 @min_outside_humidity := MIN(outside_humidity), | |
193 @max_outside_temp := MAX(outside_temp), | |
194 @min_outside_temp := MIN(outside_temp), | |
195 @max_outside_dewpoint := MAX(outside_dewpoint), | |
196 @min_outside_dewpoint := MIN(outside_dewpoint), | |
197 @max_pressure_msl := MAX(pressure_msl), | |
198 @min_pressure_msl := MIN(pressure_msl), | |
199 @max_wind_speed := MAX(wind_speed), | |
200 @min_wind_speed := MIN(wind_speed), | |
201 @max_wind_gust := MAX(wind_gust), | |
202 @min_wind_gust := MIN(wind_gust), | |
203 SUM(rain) | |
204 FROM $dbtable | |
205 WHERE date = "$date" | |
206 AND station_id = "$station_id" | |
207 EOS | |
208 , $connection ); | |
209 | |
210 if (! $result) | |
211 showerror (); | |
212 if ($row = mysql_fetch_array ($result, MYSQL_NUM)) | |
213 { | |
214 $max_inside_humidity = sprintf ("%2.1f", $row [0]); | |
215 $min_inside_humidity = sprintf ("%2.1f", $row [1]); | |
216 $max_inside_temp = sprintf ("%2.1f", $row [2]); | |
217 $min_inside_temp = sprintf ("%2.1f", $row [3]); | |
218 $max_inside_dewpoint = sprintf ("%2.1f", $row [4]); | |
219 $min_inside_dewpoint = sprintf ("%2.1f", $row [5]); | |
220 $max_outside_humidity = sprintf ("%2.1f", $row [6]); | |
221 $min_outside_humidity = sprintf ("%2.1f", $row [7]); | |
222 $max_outside_temp = sprintf ("%2.1f", $row [8]); | |
223 $min_outside_temp = sprintf ("%2.1f", $row [9]); | |
224 $max_outside_dewpoint = sprintf ("%2.1f", $row [10]); | |
225 $min_outside_dewpoint = sprintf ("%2.1f", $row [11]); | |
226 $max_pressure_msl = sprintf ("%2.1f", $row [12]); | |
227 $min_pressure_msl = sprintf ("%2.1f", $row [13]); | |
228 $max_wind_speed = sprintf ("%2.1f", $row [14]); | |
229 $min_wind_speed = sprintf ("%2.1f", $row [15]); | |
230 $max_wind_gust = sprintf ("%2.1f", $row [16]); | |
231 $min_wind_gust = sprintf ("%2.1f", $row [17]); | |
232 $sum_rain = sprintf ("%2.1f", $row [18]); | |
233 } | |
234 | |
235 $vars = array ("inside_humidity", | |
236 "inside_temp", | |
237 "inside_dewpoint", | |
238 "outside_humidity", | |
239 "outside_temp", | |
240 "outside_dewpoint", | |
241 "pressure_msl", | |
242 "wind_speed", | |
243 "wind_gust"); | |
244 foreach ($vars as $var) | |
245 { | |
246 $result = mysql_query (<<< EOS | |
247 SELECT time from $dbtable | |
248 WHERE date = "$date" | |
249 AND station_id = "$station_id" | |
250 AND $var = @max_$var | |
251 LIMIT 1 | |
252 EOS | |
253 , $connection ); | |
254 | |
255 if (! $result) | |
256 showerror (); | |
257 if ($row = mysql_fetch_array ($result, MYSQL_NUM)) | |
258 { | |
259 $max = "max_{$var}_time"; | |
260 $$max = $row [0]; | |
261 } | |
262 else | |
263 { | |
264 print <<< EOS | |
265 <p> | |
266 No data found for $date. | |
267 </p> | |
268 </body> | |
269 </html> | |
270 EOS; | |
271 exit; | |
272 } | |
273 $result = mysql_query (<<< EOS | |
274 SELECT time from $dbtable | |
275 WHERE date = "$date" | |
276 AND station_id = "$station_id" | |
277 AND $var = @min_$var | |
278 LIMIT 1 | |
279 EOS | |
280 , $connection ); | |
281 | |
282 if (! $result) | |
283 showerror (); | |
284 if ($row = mysql_fetch_array ($result, MYSQL_NUM)) | |
285 { | |
286 $min = "min_{$var}_time"; | |
287 $$min = $row [0]; | |
288 } | |
289 } | |
290 } | |
291 ?> | |
292 | |
293 <h2> | |
294 <?php | |
295 if ($istoday) | |
296 { | |
297 $timetext = date ("H:i:s"); | |
298 print "Readings for today at $timetext"; | |
299 } | |
300 else | |
301 print "Readings for $date"; | |
302 ?> | |
303 | |
304 </h2> | |
305 <table> | |
306 <?php | |
307 if ($istoday) /* include current readings */ | |
308 makerows (<<< EOS | |
309 - Minimum At Maximum At Current | |
310 Outside temperature (°C) $min_outside_temp $min_outside_temp_time $max_outside_temp $max_outside_temp_time $outside_temperature | |
311 Outside dewpoint (°C) $min_outside_dewpoint $min_outside_dewpoint_time $max_outside_dewpoint $max_outside_dewpoint_time $outside_dewpoint | |
312 Outside humidity (%) $min_outside_humidity $min_outside_humidity_time $max_outside_humidity $max_outside_humidity_time $outside_humidity | |
313 | |
314 Inside temperature (°C) $min_inside_temp $min_inside_temp_time $max_inside_temp $max_inside_temp_time $inside_temperature | |
315 Inside dewpoint (°C) $min_inside_dewpoint $min_inside_dewpoint_time $max_inside_dewpoint $max_inside_dewpoint_time $inside_dewpoint | |
316 Inside humidity (%) $min_inside_humidity $min_inside_humidity_time $max_inside_humidity $max_inside_humidity_time $inside_humidity | |
317 | |
318 Pressure (hPa) $min_pressure_msl $min_pressure_msl_time $max_pressure_msl $max_pressure_msl_time $pressure_msl | |
319 Wind speed (km/h) $min_wind_speed $min_wind_speed_time $max_wind_speed $max_wind_speed_time $wind_speed | |
320 Wind gust (km/h) $min_wind_gust $min_wind_gust_time $max_wind_gust $max_wind_gust_time $wind_gust | |
321 Wind direction (°) $wind_direction ($wind_direction_text) | |
322 Day's rainfall (mm) $sum_rain | |
323 | |
324 EOS | |
325 , "lrcrcr"); | |
326 else | |
327 makerows (<<< EOS | |
328 - Minimum At Maximum At | |
329 Outside temperature (°C) $min_outside_temp $min_outside_temp_time $max_outside_temp $max_outside_temp_time | |
330 Outside dewpoint (°C) $min_outside_dewpoint $min_outside_dewpoint_time $max_outside_dewpoint $max_outside_dewpoint_time | |
331 Outside humidity (%) $min_outside_humidity $min_outside_humidity_time $max_outside_humidity $max_outside_humidity_time | |
332 | |
333 Inside temperature (°C) $min_inside_temp $min_inside_temp_time $max_inside_temp $max_inside_temp_time | |
334 Inside dewpoint (°C) $min_inside_dewpoint $min_inside_dewpoint_time $max_inside_dewpoint $max_inside_dewpoint_time | |
335 Inside humidity (%) $min_inside_humidity $min_inside_humidity_time $max_inside_humidity $max_inside_humidity_time | |
336 | |
337 Pressure (hPa) $min_pressure_msl $min_pressure_msl_time $max_pressure_msl $max_pressure_msl_time | |
338 Wind speed (km/h) $min_wind_speed $min_wind_speed_time $max_wind_speed $max_wind_speed_time | |
339 Wind gust (km/h) $min_wind_gust $min_wind_gust_time $max_wind_gust $max_wind_gust_time | |
340 Day's rainfall (mm) $sum_rain | |
341 | |
342 EOS | |
343 , "lrcrc"); | |
344 ?> | |
345 </table> | |
346 | |
347 <h2> | |
348 Graphs for <?php echo $date ?> | |
349 </h2> | |
350 <?php | |
351 /* | |
352 * Ensure that we have the graph files. Well, ensure that we have at least the first. | |
353 */ | |
354 if ( ! file_exists ("$weatherdir/rain-$date-small.png")) | |
355 system ("$doplot $date 2>/dev/null >/dev/null"); | |
356 ?> | |
357 <div align="left"> | |
358 <?php showgraphs (<<< EOS | |
359 temperatures-$date Temperatures | |
360 5days-$date Five day temperatures | |
361 humidity-$date humidity | |
362 wind-$date wind | |
363 pressure-$date pressure | |
364 rain-$date rain | |
365 BoM-Dereel-$date BoM-Dereel | |
366 BoM-Dereel-pressure-$date BoM-Dereel-pressure | |
367 EOS | |
368 ); | |
369 ?> | |
370 | |
371 </div> | |
372 </div> | |
373 | |
374 <?php pagefooter ($id); ?> |