comparison web/db2.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: db2.php,v 1.1 2009/12/23 23:48:03 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: db2.php,v 1.1 2009/12/23 23:48:03 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 $startdate = 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 $startdate = date ("Y-m-d");
54 }
55 }
56 else
57 {
58 $mydate = getdate ();
59 $startdate = date ("Y-m-d");
60 }
61
62 $oneday = 1; /* Readings for a single day until proven otherwise */
63 if (array_key_exists ("enddate", $_GET))
64 {
65 $myenddate = validdate ($_GET ["enddate"], "date");
66 if (is_array ($myenddate)) /* valid date */
67 {
68 $oneday = 0; /* Multidate XXX check more carefully */
69 $enddate = formatdate ("Y-m-d", $myenddate);
70 }
71 else
72 {
73 $enddate = date ("Y-m-d", addsecs ($mydate, 86400));
74 print <<< EOS
75 <p>
76 <font color="red">Invalid date: $myenddate. Using $enddate</font>
77 </p>
78 EOS;
79 }
80 }
81 else
82 {
83 $myenddate = addsecs ($mydate, 86400);
84 $enddate = formatdate ("Y-m-d", $myenddate);
85 }
86
87 /*
88 * If today, get current readings. In this case, we ignore enddate, since it
89 * can't mean anything. */
90 $istoday = $startdate == date ("Y-m-d");
91
92 /* Environment to check for graphs */
93 $yesterday = formatdate ("Ymd", (addsecs ($mydate, -86400)));
94 $tomorrow = formatdate ("Ymd", (addsecs ($mydate, 86400)));
95
96
97 print <<< EOS
98
99 <!-- Select new date -->
100 <table>
101 <tr>
102 <td align="left">
103 <form action="$me" method="get">
104 <input type="submit" value="Previous day"/>
105 <input size="20" maxlength="20" type="hidden" name="date" value="$yesterday"/>
106 </form>
107 </td>
108
109 <td>
110 <form action="$me" method="get">
111 <table summary="Parameter input" cellspacing="2" border="0">
112 <tr>
113 <td>
114 <input type="submit" value="New date:"/>
115 </td>
116
117 <!-- value -->
118 <td>
119 <input size="20" maxlength="20" type="text" name="date" value="$startdate" />
120 </td>
121 </tr>
122 </table>
123 </form>
124 </td>
125 EOS;
126
127 if (! $istoday)
128 print <<< EOS
129 <td align="right">
130 <form action="$me" method="get">
131 <input type="submit" value="Next day"/>
132 <input size="20" maxlength="20" type="hidden" name="date" value="$tomorrow"/>
133 </form>
134 </td>
135
136 <td align="right">
137 <form action="$me" method="get">
138 <input type="submit" value="Today"/>
139 </form>
140 </td>
141
142 EOS;
143 print <<< EOS
144
145 </tr>
146 </table>
147
148 EOS;
149
150 /* Set up database stuff */
151 /* XXX This stuff should come from config */
152 require "db.inc";
153 $hostname = "localhost";
154 $username = "grog";
155 $password = "";
156 $database = "weather";
157 $dbtable = "observations";
158 $station_id = "Dereel";
159
160 /* Connect to the server */
161 if (! ($connection = @ mysql_pconnect ($hostname, $username, $password)))
162 showerror ();
163
164 if (! mysql_select_db ($database, $connection))
165 showerror ();
166
167 if ($istoday)
168 /* get current readings. These are really the average of the last 5 minutes */
169 {
170 $now = time ();
171 $start = time () - 600; /* 5 minutes ago */
172
173 $wind_directions = array ("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
174 "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");
175 $result = mysql_query (<<< EOS
176 SELECT AVG(inside_humidity),
177 AVG(inside_temp),
178 AVG(inside_dewpoint),
179 AVG(outside_humidity),
180 AVG(outside_temp),
181 AVG(outside_dewpoint),
182 AVG(pressure_msl),
183 AVG(wind_speed),
184 AVG(wind_gust),
185 AVG(wind_direction),
186 SUM(rain)
187 FROM $dbtable
188 WHERE unix_timestamp(timestamp(date,time)) >= $start
189 AND unix_timestamp(timestamp(date, time)) <= $now
190 AND station_id = "$station_id"
191 EOS
192 , $connection );
193
194 if (! $result)
195 showerror ();
196 if ($row = mysql_fetch_array ($result, MYSQL_NUM))
197 {
198 $inside_humidity = sprintf ("%2.0f", $row [0]);
199 $inside_temperature = sprintf ("%2.1f", $row [1]);
200 $inside_dewpoint = sprintf ("%2.1f", $row [2]);
201 $outside_humidity = sprintf ("%2.1f", $row [3]);
202 $outside_temperature = sprintf ("%2.1f", $row [4]);
203 $outside_dewpoint = sprintf ("%2.1f", $row [5]);
204 $pressure_msl = sprintf ("%2.1f", $row [6]);
205 $wind_speed = sprintf ("%2.1f", $row [7]);
206 $wind_gust = sprintf ("%2.1f", $row [8]);
207 $wind_direction = sprintf ("%2.1f", $row [9]);
208 $rain = sprintf ("%2.1f", $row [10]);
209 $wind_direction_text = $wind_directions [($row [9] + 11.25) / 22.5];
210 }
211 }
212
213 $result = mysql_query (<<< EOS
214 SELECT @max_inside_humidity := MAX(inside_humidity),
215 @min_inside_humidity := MIN(inside_humidity),
216 @max_inside_temp := MAX(inside_temp),
217 @min_inside_temp := MIN(inside_temp),
218 @max_inside_dewpoint := MAX(inside_dewpoint),
219 @min_inside_dewpoint := MIN(inside_dewpoint),
220 @max_outside_humidity := MAX(outside_humidity),
221 @min_outside_humidity := MIN(outside_humidity),
222 @max_outside_temp := MAX(outside_temp),
223 @min_outside_temp := MIN(outside_temp),
224 @max_outside_dewpoint := MAX(outside_dewpoint),
225 @min_outside_dewpoint := MIN(outside_dewpoint),
226 @max_pressure_msl := MAX(pressure_msl),
227 @min_pressure_msl := MIN(pressure_msl),
228 @max_wind_speed := MAX(wind_speed),
229 @min_wind_speed := MIN(wind_speed),
230 @max_wind_gust := MAX(wind_gust),
231 @min_wind_gust := MIN(wind_gust),
232 SUM(rain)
233 FROM $dbtable
234 WHERE date >= "$startdate"
235 AND date < "$enddate"
236 AND station_id = "$station_id"
237 EOS
238 , $connection );
239
240 if (! $result)
241 showerror ();
242 if ($row = mysql_fetch_array ($result, MYSQL_NUM))
243 {
244 $max_inside_humidity = sprintf ("%2.1f", $row [0]);
245 $min_inside_humidity = sprintf ("%2.1f", $row [1]);
246 $max_inside_temp = sprintf ("%2.1f", $row [2]);
247 $min_inside_temp = sprintf ("%2.1f", $row [3]);
248 $max_inside_dewpoint = sprintf ("%2.1f", $row [4]);
249 $min_inside_dewpoint = sprintf ("%2.1f", $row [5]);
250 $max_outside_humidity = sprintf ("%2.1f", $row [6]);
251 $min_outside_humidity = sprintf ("%2.1f", $row [7]);
252 $max_outside_temp = sprintf ("%2.1f", $row [8]);
253 $min_outside_temp = sprintf ("%2.1f", $row [9]);
254 $max_outside_dewpoint = sprintf ("%2.1f", $row [10]);
255 $min_outside_dewpoint = sprintf ("%2.1f", $row [11]);
256 $max_pressure_msl = sprintf ("%2.1f", $row [12]);
257 $min_pressure_msl = sprintf ("%2.1f", $row [13]);
258 $max_wind_speed = sprintf ("%2.1f", $row [14]);
259 $min_wind_speed = sprintf ("%2.1f", $row [15]);
260 $max_wind_gust = sprintf ("%2.1f", $row [16]);
261 $min_wind_gust = sprintf ("%2.1f", $row [17]);
262 $sum_rain = sprintf ("%2.1f", $row [18]);
263 }
264
265 $vars = array ("inside_humidity",
266 "inside_temp",
267 "inside_dewpoint",
268 "outside_humidity",
269 "outside_temp",
270 "outside_dewpoint",
271 "pressure_msl",
272 "wind_speed",
273 "wind_gust");
274 foreach ($vars as $var)
275 {
276 $result = mysql_query (<<< EOS
277 SELECT date, time from $dbtable
278 WHERE date >= "$startdate"
279 AND date < "$enddate"
280 AND station_id = "$station_id"
281 AND $var = @max_$var
282 LIMIT 1
283 EOS
284 , $connection );
285
286 if (! $result)
287 showerror ();
288 if ($row = mysql_fetch_array ($result, MYSQL_NUM))
289 {
290 $max = "max_{$var}_time";
291 if ($oneday)
292 $$max = $row [1]; /* omit time */
293 else
294 $$max = $row [0] . " " . $row [1];
295 }
296 else
297 {
298 print <<< EOS
299 <p>
300 No data found for $startdate.
301 </p>
302 </body>
303 </html>
304 EOS;
305 exit;
306 }
307 $result = mysql_query (<<< EOS
308 SELECT date, time from $dbtable
309 WHERE date >= "$startdate"
310 AND date < "$enddate"
311 AND station_id = "$station_id"
312 AND $var = @min_$var
313 LIMIT 1
314 EOS
315 , $connection );
316
317 if (! $result)
318 showerror ();
319 if ($row = mysql_fetch_array ($result, MYSQL_NUM))
320 {
321 $min = "min_{$var}_time";
322 if ($oneday)
323 $$min = $row [1]; /* omit time */
324 else
325 $$min = $row [0] . " " . $row [1];
326 }
327 }
328 }
329 ?>
330
331 <h2>
332 <?php
333 if ($istoday)
334 {
335 $timetext = date ("H:i:s");
336 print "Readings for today at $timetext";
337 }
338 else if ($oneday)
339 print "Readings for $startdate";
340 else
341 print "Readings for period $startdate to $enddate";
342 ?>
343
344 </h2>
345 <table>
346 <?php
347 if ($istoday) /* include current readings */
348 makerows (<<< EOS
349 - Current Minimum At Maximum At
350 Outside temperature (°C) $outside_temperature $min_outside_temp $min_outside_temp_time $max_outside_temp $max_outside_temp_time
351 Outside dewpoint (°C) $outside_dewpoint $min_outside_dewpoint $min_outside_dewpoint_time $max_outside_dewpoint $max_outside_dewpoint_time
352 Outside humidity (%) $outside_humidity $min_outside_humidity $min_outside_humidity_time $max_outside_humidity $max_outside_humidity_time
353
354 Inside temperature (°C) $inside_temperature $min_inside_temp $min_inside_temp_time $max_inside_temp $max_inside_temp_time
355 Inside dewpoint (°C) $inside_dewpoint $min_inside_dewpoint $min_inside_dewpoint_time $max_inside_dewpoint $max_inside_dewpoint_time
356 Inside humidity (%) $inside_humidity $min_inside_humidity $min_inside_humidity_time $max_inside_humidity $max_inside_humidity_time
357
358 Pressure (hPa) $pressure_msl $min_pressure_msl $min_pressure_msl_time $max_pressure_msl $max_pressure_msl_time
359 Wind speed (km/h) $wind_speed $min_wind_speed $min_wind_speed_time $max_wind_speed $max_wind_speed_time
360 Wind gust (km/h) $wind_gust $min_wind_gust $min_wind_gust_time $max_wind_gust $max_wind_gust_time
361 Wind direction (°) $wind_direction ($wind_direction_text)
362 Day's rainfall (mm) $sum_rain
363
364 EOS
365 , "lrrcrc");
366 else
367 makerows (<<< EOS
368 - Minimum At Maximum At
369 Outside temperature (°C) $min_outside_temp $min_outside_temp_time $max_outside_temp $max_outside_temp_time
370 Outside dewpoint (°C) $min_outside_dewpoint $min_outside_dewpoint_time $max_outside_dewpoint $max_outside_dewpoint_time
371 Outside humidity (%) $min_outside_humidity $min_outside_humidity_time $max_outside_humidity $max_outside_humidity_time
372
373 Inside temperature (°C) $min_inside_temp $min_inside_temp_time $max_inside_temp $max_inside_temp_time
374 Inside dewpoint (°C) $min_inside_dewpoint $min_inside_dewpoint_time $max_inside_dewpoint $max_inside_dewpoint_time
375 Inside humidity (%) $min_inside_humidity $min_inside_humidity_time $max_inside_humidity $max_inside_humidity_time
376
377 Pressure (hPa) $min_pressure_msl $min_pressure_msl_time $max_pressure_msl $max_pressure_msl_time
378 Wind speed (km/h) $min_wind_speed $min_wind_speed_time $max_wind_speed $max_wind_speed_time
379 Wind gust (km/h) $min_wind_gust $min_wind_gust_time $max_wind_gust $max_wind_gust_time
380 Day's rainfall (mm) $sum_rain
381
382 EOS
383 , "lrcrc");
384 ?>
385 </table>
386
387 <h2>
388 Graphs for <?php echo $startdate ?>
389 </h2>
390 <?php
391 /*
392 * Ensure that we have the graph files. Well, ensure that we have at least the first.
393 */
394 if ($oneday)
395 $range = $startdate;
396 else
397 $range = "$startdate-$enddate";
398 if ( ! file_exists ("$weatherdir/rain-$range-small.png"))
399 {
400 if ($oneday)
401 system ("$doplot $startdate 2>/dev/null >/dev/null");
402 else
403 system ("$doplot $startdate $enddate 2>/dev/null >/dev/null");
404 }
405 ?>
406 <div align="left">
407 <?php showgraphs (<<< EOS
408 outside-temp-$range Outside temperature
409 inside-temp-$range inside-temp
410 inside-outside-temp-$range inside-outside-temp
411 humidity-$range humidity
412 wind-$range wind
413 pressure-$range pressure
414 rain-$range rain
415 BoM-Dereel-$range BoM-Dereel
416 EOS
417 , $startdate, $enddate);
418 ?>
419 </div>
420 </div>
421
422 <?php pagefooter ($id); ?>