wmpager: drop tooltip suport
[dockapps.git] / wmweather+ / convert.c
blob66cd42ff7ef052c638a4338dffd7838227bc73b2
1 #include "config.h"
3 /* Copyright (C) 2002 Brad Jorsch <anomie@users.sourceforge.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <math.h>
21 #if TM_IN_SYS_TIME
22 # if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 # else
26 # if HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 # else
29 # include <time.h>
30 # endif
31 # endif
32 #else
33 #include <time.h>
34 #endif
35 #include <ctype.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include "convert.h"
41 * To indicate unavailable data
42 * 999 is used for temperature
43 * x<0 is used for rh, pressure, and windspeed
47 /* Calculations */
49 int rh_C(int temp_C, int dewpt_C){
50 float f;
52 if(temp_C==999 || dewpt_C==999) return 999;
54 f=1782.75*(dewpt_C-temp_C)/((237.7+dewpt_C)*(237.7+temp_C));
55 return round(pow(10, f+2));
58 int rh_F(int temp_F, int dewpt_F){
59 float f;
61 if(temp_F==999 || dewpt_F==999) return 999;
63 f=3208.95*(dewpt_F-temp_F)/((395.86+dewpt_F)*(395.86+temp_F));
64 return round(pow(10, f+2));
67 int heatindex_C(int temp_C, int rh){
68 #if 1
69 if(temp_C==999 || temp_C<21 || rh<0) return 999;
70 return heatindex_F(temp_C2F(temp_C), rh);
71 #else
72 int temp2, rh2;
74 if(temp_C==999 || temp_C<38 || rh<0) return 999;
76 temp2=temp_C*temp_C;
77 rh2=rh*rh;
78 return round(16.18754948 + 2.900509394*temp_C - 0.0221545692*temp2 + 4.20938791*rh - 0.26300889*temp_C*rh + 0.0039811176*temp2*rh - 0.02956469*rh2 + 0.001305828*temp_C*rh2 - 6.4476e-06*temp2*rh2);
79 #endif
82 int heatindex_F(int temp_F, int rh){
83 int temp2, temp3, rh2, rh3;
85 if(temp_F==999 || temp_F<70 || rh<0) return 999;
87 temp2=temp_F*temp_F;
88 temp3=temp2*temp_F;
89 rh2=rh*rh;
90 rh3=rh2*rh;
91 return round(16.923 + .185212*temp_F + 5.37941*rh - .100254*temp_F*rh + (9.41695e-3)*temp2 + (7.28898e-3)*rh2 + (3.45372e-4)*temp2*rh - (8.14971e-4)*temp_F*rh2 + (1.02102e-5)*temp2*rh2 - (3.8646e-5)*temp3 + (2.91583e-5)*rh3 + (1.42721e-6)*temp3*rh + (1.97483e-7)*temp_F*rh3 - (2.18429e-8)*temp3*rh2 + (8.43296e-10)*temp2*rh3 - (4.81975e-11)*temp3*rh3);
92 #if 0
93 return round(-42.379 + 2.04901523*temp_F + 10.14333127*rh - 0.22475541*temp_F*rh - .00683783*temp2 - .05481717*rh2 + .00122874*temp2*rh + .00085282*temp_F*rh2 - .00000199*temp2*rh2);
94 #endif
97 int windchill_C(int temp_C, int windspeed){
98 if(temp_C==999 || windspeed<0) return 999;
100 return windchill_F(temp_C2F(temp_C), windspeed);
103 int windchill_F(int temp_F, int windspeed){
104 double ret;
105 if(temp_F==999 || windspeed<0) return 999;
107 ret=35.74 + 0.6215*temp_F + (-35.75 + 0.4275*temp_F)*pow(windspeed*50292/57875.0, 0.16);
108 if(ret>temp_F) return temp_F;
109 return round(ret);
112 /* Length Conversions */
114 int in2cm(int in){
115 if(in<0) return in;
116 return round(in*2.54);
119 float m2mi(int meters){
120 if(meters<0) return meters;
121 return meters*125/201168;
124 /* Windspeed Conversions */
126 int knots2mph(int knots){
127 if(knots<0) return knots;
128 return round(knots*57875/50292.0);
131 int knots2kph(int knots){
132 if(knots<0) return knots;
133 return round(knots*463/250.0);
136 int kph2knots(int kph){
137 if(kph<0) return kph;
138 return round(kph*250/463.0);
141 int knots2mps(int knots){
142 if(knots<0) return knots;
143 return round(knots*463/900.0);
146 int mps2knots(int mps){
147 if(mps<0) return mps;
148 return round(mps*900/463.0);
151 int knots2beaufort(int knots){
152 if(knots<0) return knots;
153 if(knots<1) return 0;
154 if(knots<=3) return 1;
155 if(knots<=6) return 2;
156 if(knots<=10) return 3;
157 if(knots<=16) return 4;
158 if(knots<=21) return 5;
159 if(knots<=27) return 6;
160 if(knots<=33) return 7;
161 if(knots<=40) return 8;
162 if(knots<=47) return 9;
163 if(knots<=55) return 10;
164 if(knots<=63) return 11;
165 return 12;
169 /* Temperature Conversions */
171 int temp_C2F(int temp_C){
172 if(temp_C==999) return 999;
173 return round(temp_C*9/5.0+32);
176 int temp_F2C(int temp_F){
177 if(temp_F==999) return 999;
178 return round((temp_F-32)*5/9.0);
182 /* Pressure Conversions */
184 float inHg2mmHg(float inHg){
185 if(inHg<0) return inHg;
186 return inHg*25.4;
189 float inHg2hPa(float inHg){
190 if(inHg<0) return inHg;
191 return inHg*33.8639;
194 float inHg2atm(float inHg){
195 if(inHg<0) return inHg;
196 return inHg*.033421052632;
199 float hPa2inHg(float hPa){
200 if(hPa<0) return hPa;
201 return hPa/33.8639;
205 /* Time Conversions */
207 /* NOTE: y%400==100 because y=year-1900 */
208 #define is_leap(y) (y%4==0 && (y%100!=0 || y%400==100))
210 /* mktime for UTC, more or less.
211 * Differences:
212 * - no range checking
213 * - never recalculates tm_wday or tm_yday
215 time_t mkgmtime(struct tm *tm){
216 static long msec[]={0, 2678400, 5097600, 7776000, 10368000, 13046400, 15638400, 18316800, 20995200, 23587200, 26265600, 28857600};
217 time_t t;
218 int i;
220 t=0;
221 if(tm->tm_year>70){
222 for(i=70; i<tm->tm_year; i++){
223 t+=31536000;
224 if(is_leap(i)) t+=86400;
226 } else if(tm->tm_year<70){
227 for(i=69; i>=tm->tm_year; i--){
228 t-=31536000;
229 if(is_leap(i)) t-=86400;
232 t+=msec[tm->tm_mon];
233 if(tm->tm_mon>1 && is_leap(tm->tm_year)) t+=86400;
234 t+=(((tm->tm_mday-1)*24+tm->tm_hour)*60+tm->tm_min)*60+tm->tm_sec;
236 return t;
239 int utc2local(int hm, int *month, int *day, int *year, int *wday){
240 time_t t=time(NULL);
241 struct tm *tm;
243 tm=gmtime(&t);
244 tm->tm_hour=hm/100;
245 tm->tm_min=hm%100;
246 if(month!=NULL && *month!=-1) tm->tm_mon=*month-1;
247 if(day!=NULL && *day!=-1) tm->tm_mday=*day;
248 if(year!=NULL && *year!=-1) tm->tm_year=*year;
250 t=mkgmtime(tm);
251 tm=localtime(&t);
253 if(month!=NULL) *month=tm->tm_mon+1;
254 if(day!=NULL) *day=tm->tm_mday;
255 if(year!=NULL) *year=tm->tm_year;
256 if(wday!=NULL) *wday=tm->tm_wday;
257 return tm->tm_hour*100+tm->tm_min;
260 int local2utc(int hm, int *month, int *day, int *year, int *wday){
261 time_t t=time(NULL);
262 struct tm *tm;
264 tm=localtime(&t);
265 tm->tm_hour=hm/100;
266 tm->tm_min=hm%100;
267 if(month!=NULL && *month!=-1) tm->tm_mon=*month-1;
268 if(day!=NULL && *day!=-1) tm->tm_mday=*day;
269 if(year!=NULL && *year!=-1) tm->tm_year=*year;
271 t=mktime(tm);
272 tm=gmtime(&t);
274 if(month!=NULL) *month=tm->tm_mon+1;
275 if(day!=NULL) *day=tm->tm_mday;
276 if(year!=NULL) *year=tm->tm_year;
277 if(wday!=NULL) *wday=tm->tm_wday;
278 return tm->tm_hour*100+tm->tm_min;
281 void fix_date(int *month, int *day, int *year, int *wday){
282 time_t t=time(NULL);
283 struct tm *tm;
285 tm=gmtime(&t);
286 if(month!=NULL && *month!=-1) tm->tm_mon=*month-1;
287 if(day!=NULL && *day!=-1) tm->tm_mday=*day;
288 if(year!=NULL && *year!=-1) tm->tm_year=*year;
290 t=mkgmtime(tm);
291 tm=gmtime(&t);
293 if(month!=NULL) *month=tm->tm_mon+1;
294 if(day!=NULL) *day=tm->tm_mday;
295 if(year!=NULL) *year=tm->tm_year;
296 if(wday!=NULL) *wday=tm->tm_wday;
299 int hm2min(int hm){
300 return hm/100*60+hm%100;
303 /* Letter Case (destructive!) */
305 char *str_upper(char *str){
306 char *c;
308 for(c=str; *c!='\0'; c++){
309 *c=toupper(*c);
311 return str;
314 char *str_lower(char *str){
315 char *c;
317 for(c=str; *c!='\0'; c++){
318 *c=tolower(*c);
320 return str;
324 /* Angle conversions */
326 /* Convert radian angle to degrees */
327 double rad2deg(double angle) {
328 return 180.0*angle/PI;
331 /* Convert degree angle to radians */
332 double deg2rad(double angle) {
333 return PI*angle/180.0;
337 /* Date conversions */
339 /* Numerical day-of-year from month, day and year */
340 int mdy2doy(int mn, int dy, int y) {
341 return 275*mn/9 - ((y%4==0 && (y%100!=0 || y%400==100))?1:2)*(mn + 9)/12 + dy-30;
344 /* Julian day from month/day/year */
345 double mdy2jd(int year, int month, int day) {
346 int A, B;
348 year+=1900;
349 if (month <= 2) {
350 year -= 1;
351 month += 12;
353 A=year/100;
354 B=2 - A + A/4;
356 return (int)(365.25*(year + 4716)) + (int)(30.6001*(month+1)) + day + B - 1524.5;
359 /* convert Julian Day to centuries since J2000.0. */
360 double jd2jcent(double jd) {
361 return (jd - 2451545.0)/36525.0;
364 /* convert centuries since J2000.0 to Julian Day. */
365 double jcent2jd(double t) {
366 return t * 36525.0 + 2451545.0;
370 /* Lat/Long conversions */
372 static double parse_dd_or_dms(char *s, char **e){
373 double deg;
375 *e=s;
376 if(strchr(s, 'x') || strchr(s, 'X')) return NAN;
377 if(!strchr(s, '\'')){
378 if(!isdigit(*s) && *s!='.') return NAN;
379 return strtod(s, e);
382 if(!isdigit(*s)) return NAN;
383 deg=strtol(s, e, 10);
384 if(*e==s || *e==NULL || **e!='\'') return deg;
385 s=++(*e);
386 if(!isdigit(*s)) return deg;
387 deg+=strtol(s, e, 10)/60.0;
388 if(*e==s || *e==NULL || **e!='\'') return deg;
389 s=++(*e);
390 if(!isdigit(*s) && *s!='.') return NAN;
391 deg+=strtod(s, e)/3600.0;
392 if(*e!=s && *e!=NULL && **e=='\'') (*e)++;
393 return deg;
396 int str2dd(char *s, double *lat, double *lon){
397 char *e;
398 int dir=0;
399 char c;
401 c=toupper(*s);
402 if(c=='+' || c=='N'){
403 s++; dir=1;
405 if(c=='-' || c=='S'){
406 s++; dir=-1;
409 *lat=parse_dd_or_dms(s, &e);
410 if(isnan(*lat) || e==NULL || e==s || *e=='\0') return 0;
411 if(!dir){
412 c=toupper(*e);
413 if(c=='N') dir=1;
414 if(c=='S') dir=-1;
415 if(dir) e++;
417 if(dir<0) *lat=-*lat;
419 while(isspace(*e)) e++;
420 if(*e=='\0') return 0;
422 s=e; dir=0;
423 c=toupper(*s);
424 if(c=='+' || c=='W'){
425 s++; dir=1;
427 if(c=='-' || c=='E'){
428 s++; dir=-1;
431 *lon=parse_dd_or_dms(s, &e);
432 if(isnan(*lon) || e==s) return 0;
433 if(e==NULL || *e=='\0') return 1;
434 if(dir==0){
435 c=toupper(*e);
436 if(c=='W') dir=1;
437 if(c=='E') dir=-1;
438 if(dir!=0) e++;
440 if(dir<0) *lon=-*lon;
442 return (*e=='\0');