Mercurial > ~darius > hgwebdir.cgi > wh1080
comparison util.c @ 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 | b22a888eb975 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9dab44dcb331 |
---|---|
1 /* | |
2 * WH-1080 data input utility. | |
3 * Utility functions | |
4 * | |
5 * Greg Lehey, 22 November 2009 | |
6 * | |
7 * $Id: util.c,v 1.6 2009/12/18 02:13:23 grog Exp $ | |
8 */ | |
9 | |
10 #include "wh1080.h" | |
11 | |
12 /* Used in many places, but not here */ | |
13 char *wind_directions [] = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", | |
14 "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW "}; | |
15 | |
16 float print_previous_rain; /* counter for print_readings */ | |
17 | |
18 /* Print out readings */ | |
19 void print_readings (struct readings *readings) | |
20 { | |
21 static int lines = 0; | |
22 char timestamp [STAMPSIZE]; | |
23 | |
24 if (lines == 0) | |
25 printf ("\n" | |
26 " Page sv IT IH IDP OT OH ODP hPa SL abs rel Wind Gust Dir Rain CR PR \n"); | |
27 | |
28 strftime (timestamp, STAMPSIZE, "%F %T", localtime (&readings->timestamp)); /* format time and date */ | |
29 | |
30 printf ("%17s %5x %3d %5.1f %3d %5.1f %5.1f %3d %5.1f %6.1f %6.1f %6.1f %6.1f %5.1f %5.1f %3s %4.1f %4.1f %4.1f\n", | |
31 timestamp, /* current time */ | |
32 readings->page, /* machine page number */ | |
33 readings->last_save_mins, /* last save minutes (?) */ | |
34 readings->inside_temp, /* inside temperature */ | |
35 readings->inside_humidity, /* humidity in percent */ | |
36 readings->inside_dewpoint, /* inside dewpoint */ | |
37 readings->outside_temp, /* outside temperature */ | |
38 readings->outside_humidity, /* humidity in percent */ | |
39 readings->outside_dewpoint, /* outside dewpoint */ | |
40 readings->pressure, /* absolute pressure, hPa */ | |
41 readings->pressure_sea_level, /* sea level pressure, hPa */ | |
42 readings->page1_abs_pressure, /* absolute pressure, hPa */ | |
43 readings->page1_rel_pressure, /* relative pressure, hPa */ | |
44 readings->wind_speed, /* wind speed in km/h */ | |
45 readings->wind_gust, /* wind gust speed in km/h */ | |
46 readings->wind_direction_text, /* wind direction */ | |
47 readings->rain - print_previous_rain, /* delta rainfall in mm */ | |
48 readings->rain, /* current rainfall in mm */ | |
49 readings->previous_rain ); /* previous rainfall in mm */ | |
50 print_previous_rain = readings->rain; /* reported this far */ | |
51 if (++lines > 60) | |
52 lines = 0; | |
53 } | |
54 | |
55 /* | |
56 * Calculate dewpoint temperature. Replace this with something with better references. | |
57 */ | |
58 float dewpoint (float temp, int humidity) | |
59 { | |
60 float Es; /* saturated water vapour pressure at dry-bulb temperature */ | |
61 float Ep; /* partial pressure at this humidity */ | |
62 | |
63 Es = 6.11 * pow (10.0, 7.5 * (temp / (237.7 + temp))); | |
64 Ep = (humidity * Es) / 100; | |
65 return (-430.22 + 237.7 * log (Ep)) / (-log (Ep) + 19.08); | |
66 } | |
67 | |
68 /* | |
69 * Calculate inside and outside dewpoints and store in the record. | |
70 */ | |
71 void set_dewpoints (struct readings *readings) | |
72 { | |
73 readings->inside_dewpoint = dewpoint (readings->inside_temp, readings->inside_humidity); | |
74 readings->outside_dewpoint = dewpoint (readings->outside_temp, readings->outside_humidity); | |
75 } | |
76 | |
77 /* | |
78 * Convert pressure to sea-level pressure. The barometer is in the inside unit, | |
79 * so we use the internal temperature as a base. | |
80 */ | |
81 void set_sea_level_pressure (struct readings *readings) | |
82 { | |
83 float e = - config.elevation / (KELVIN (readings->inside_temp) * 29.263); | |
84 | |
85 e = exp (e); | |
86 | |
87 readings->pressure_sea_level = readings->pressure / e; | |
88 } | |
89 | |
90 /* | |
91 * Simplified date conversion. | |
92 * text must be big enough for format; we don't check. | |
93 */ | |
94 void datetext (time_t date, char *text, char *format) | |
95 { | |
96 struct tm *date_tm; /* struct tm format of date */ | |
97 | |
98 date_tm = localtime (&date); /* and in struct tm format */ | |
99 strftime (text, 64, format, date_tm); | |
100 } |