|
|
1.1 root 1: # include "stdio.h"
2: # include "ctype.h"
3: # include "assert.h"
4: # include "cbt.h"
5: # include "weath.h"
6: # include "time.h"
7: struct tm *gmtime();
8: # define SURFACE 1
9: # define LFMFORE 2
10: # define FORECAST 3
11: # define MARINE 4
12: mbuf key, rec, rkey;
13: char kb[100], rb[100], rkb[100];
14: int day;
15: bfile *bf;
16: char *index(), *vis(), *airport();
17: double atof();
18:
19: main()
20: {
21: char line[400], line2[400];
22: int kind=0, k;
23: long clock;
24: time(&clock);
25: day = gmtime(&clock)->tm_yday;
26: chdir ("/usr/spool/weather");
27: bf = bopen("airn", 0);
28: assert(bf!=NULL);
29: while (gets(line))
30: {
31: k = strlen(line);
32: if (k==0) continue;
33: if (k==8 && strncmp(line+3, "00", 2)==0)
34: {
35: printf("found something, line %s\n",line);
36: unfore();
37: switch(atoi(line+5))
38: {
39: case 200: /* new england */
40: case 201: /* eastern states */
41: case 202: /* midatlantic */
42: case 203: /* southeastern */
43: case 204: /* great lakes */
44: case 205: /* ohio valley */
45: case 206: /* northern plains */
46: case 207: /* great plains */
47: case 208: /* gulf coast */
48: case 209: /* northern rockies */
49: case 210: /* southwestern */
50: case 211: /* pacific northwest */
51: case 212: /* pacific */
52: case 290: /* alaska */
53: case 291: /* hawaii */
54: kind = SURFACE;
55: continue;
56: case 431:
57: kind = LFMFORE; continue;
58: case 307:
59: case 308:
60: case 309:
61: kind = FORECAST; continue;
62: case 313:
63: kind = MARINE; continue;
64: }
65: kind=0;
66: continue;
67: }
68: switch(kind)
69: {
70: case SURFACE:
71: surf(line); break;
72: case LFMFORE:
73: if (strlen(line)>5 && line[3]==' ' &&
74: isupper(line[0]) && isupper(line[1]))
75: {
76: gets(line2);
77: if (startwith(line2, "POF"))
78: gets(line2);
79: lfmfore(line, line2);
80: }
81: else
82: if (strncmp(line, "DATE/GMT", 8)==0)
83: lfmtimeset(line);
84: break;
85: case FORECAST:
86: case MARINE:
87: realfore(line, kind); break;
88: }
89: }
90: }
91: surf(line)
92: char *line;
93: {
94: int nv, nw, i, cloud; char *v[50], *w[50], *s, *p, *aname;
95: double north, west;
96: printf("surface lin %s\n",line);
97: if (strlen(line)<10) return;
98: if (!isalpha(line[0])) return;
99: s=line;
100: while ( (s=index (s, '/')) && !hasnxt(s))
101: s++;
102: if (s==NULL) return;
103: while (s>line && *s!= ' ')s--;
104: *s++ = 0;
105: nv = getargs(line, v);
106: nw = getargs(s, w);
107: aname = airport(v[0]);
108: printf("airport %s aname %s\n", v[0], aname? aname: "NULL");
109: if (aname==NULL) return;
110: for(i=0; i<nw; i++)
111: { int q;
112: q = strlen(w[i]);
113: if (q==4 && isdigit(w[i][0])) break;
114: if (q>4 && isdigit(w[i][0]) && w[i][4]=='G') break;
115: }
116: if (i==nw || i<2) return;
117: p = vis(v[nv-1]);
118: if (p==NULL ) return;
119: north = atof(aname);
120: west = atof(index(aname, ' '));
121: s= v[nv-2]; /* last kind of cloud cover */
122: while (isdigit(*s) || *s=='-') s++;
123: if (strcmp(s, "OVC")==0)
124: cloud = 'O';
125: else
126: if (strcmp(s, "BKN")==0)
127: cloud = 'P';
128: else
129: if (strcmp(s, "SCT")==0)
130: cloud = 'P';
131: else
132: if (strcmp(s, "CLR")==0)
133: cloud = 'C';
134: else
135: cloud = 'X';
136: if (nottime(v[2])) return;
137: if (!alldigs(w[i-2])) return; /* temp must be digits */
138: if (!alldigs(w[i-1])) return; /* dewpoint must be digits */
139: update( v[0], north, west, v[2], w[i-2], w[i-1], v[nv-1], w[i], cloud, w[i+1]);
140: }
141: char *
142: vis(s)
143: char *s;
144: {
145: int d;
146: static char temp[50];
147: d= atoi(s);
148: while (isdigit(*s)) s++;
149: if (*s==0)
150: {
151: sprintf(temp, " ordinary, visibility %d miles", d);
152: return(temp);
153: }
154: temp[0]=0;
155: for( ; *s; s++)
156: switch(*s)
157: {
158: case 'A': strcat(temp, " hail"); break;
159: case 'D': strcat(temp, " dust"); break;
160: case 'F': strcat(temp, " fog"); break;
161: case 'H': strcat(temp, " haze"); break;
162: case 'I': if (s[1]=='P') strcat(temp, " sleet"); break;
163: case 'K': strcat(temp, " smoke"); break;
164: case 'L': strcat(temp, " drizzle"); break;
165: case 'R': strcat(temp, " rain"); break;
166: case 'W': strcat(temp, " showers"); break;
167: case 'S': strcat(temp, " snow"); break;
168: case 'T': strcat(temp, " thunderstorms"); break;
169: case 'Z': strcat(temp, " freezing"); break;
170: case '-': break;
171: default: return(NULL);
172: }
173: return(temp);
174: }
175: char lfmt1[10], lfmt2[10];
176: lfmfore( l1, l2)
177: char *l1, *l2;
178: {
179: char *s, fname[100], xl[20], *p, *t;
180: FILE *tf;
181: int p1, p2, p3, p4, max1, min1, max2, min2;
182: l1[3]=0;
183: s=airport(l1);
184: if(s==NULL) return;
185: sscanf(l1+4, "%*s %d %d %d %d", &p1, &p2, &p3, &p4);
186: sscanf(l2, "%s %d %d %d %d", xl, &max1, &min1, &max2, &min2);
187: if (p1<0 || p1>100) return;
188: if (p2<0 || p2>100) return;
189: if (p3<0 || p3>100) return;
190: if (p4<0 || p4>100) return;
191: if (max1>150 || max2>150 || min1>150 || min2>150) return;
192: if (strncmp(xl, "MN", 2)==0)
193: {
194: int t;
195: t = max1; max1 = min1; min1 = t;
196: t = max2; max2 = min2; min2 = t;
197: }
198: sprintf(fname, "lfm/%s", l1);
199: tf = fopen(fname, "w");
200: if (tf)
201: {
202: p = index(s, '\t');
203: if (p==NULL) return;
204: fprintf(tf, "%s\n", p+1);
205: fprintf(tf, "%s %d %d %d %d\n", lfmt1, max1, min1, p1, p2);
206: fprintf(tf, "%s %d %d %d %d\n", lfmt2, max2, min2, p3, p4);
207: fclose(tf);
208: airmark ("lfm/", l1);
209: }
210: return;
211: }
212: lfmtimeset(s)
213: char *s;
214: {
215: sscanf(s, "%*s %s %*s %s", lfmt1, lfmt2);
216: }
217: char *
218: airport(s)
219: char *s;
220: {
221: int i;
222: key.mdata = s;
223: key.mlen = strlen(s);
224: i=bseek(bf, key);
225: if (i!=1) return(NULL);
226: rkey.mdata =rkb; rec.mdata = rb;
227: bread(bf, &rkey, &rec);
228: assert(strcmp(rkey.mdata, key.mdata)==0);
229: rkey.mdata[rkey.mlen]=0;
230: rec.mdata[rec.mlen]=0;
231: return(rec.mdata);
232: }
233: update (acode, north, west, wtime, temp, humid, weath, wind, cloud, press)
234: double north, west;
235: char *acode, *temp, *weath, *wind, *humid, *wtime, *press;
236: {
237: char fname[30], odata[2000], *p, *s, wtt[100];
238: struct wline *wp;
239: int n, w, f;
240: n = north;
241: n = n-n%4;
242: w = west;
243: w = w-w%4;
244: sprintf(fname, "obs/%.2d.%.2d", n, w);
245: f = open(fname, 2);
246: if (f>=0)
247: {
248: n=read(f, odata, 2000);
249: assert(n<2000);
250: for(wp=(struct wline *)odata; ( (char *)wp ) <odata+n; wp++)
251: {
252: if (atoi(wp->wday)+1<day)
253: strncpy(wp->anam, "---", 3);
254: }
255: for(wp=(struct wline *)odata; ( (char *)wp ) <odata+n; wp++)
256: {
257: if (strncmp(wp->anam, acode, 3)==0)
258: break;
259: }
260: if ((char *)wp >=odata+n)
261: for(wp=(struct wline *)odata; ( (char *)wp ) <odata+n; wp++)
262: {
263: if (strncmp(wp->anam, "---", 3)==0)
264: break;
265: }
266: }
267: else
268: {
269: f = creat(fname, 0666);
270: wp = (struct wline *)odata;
271: n=0;
272: }
273: sprintf(wtt, "%.3s%5.2f%6.2f%4.4s%3.3s%3.3s%-6.6s%-7.7s%c%3.3s%03d\n",
274: acode, north, west, wtime, temp, humid, weath, wind, cloud, press, day);
275: strncpy(wp->anam, wtt, sizeof(*wp));
276: lseek (f, 0L, 0);
277: if (n<= ( (char *)wp - odata )) n+= sizeof(*wp);
278: write(f, odata, n);
279: close(f);
280: }
281: nottime(s)
282: char *s;
283: {
284: int c;
285: if (strlen(s)!=4) return(1);
286: while ( c = *s++ )
287: if (!isdigit(c)) return(1);
288: return(0);
289: }
290: static FILE *ff = NULL;
291: realfore(line, kind)
292: char *line;
293: {
294: char fname[50];
295: if (kind==FORECAST && strncmp(line+3, " FP", 3)==0)
296: {
297: unfore();
298: line[3]=0;
299: sprintf(fname, "fore/f_%s", line);
300: ff = fopen(fname, "w");
301: airmark ("fore/f_", line);
302: }
303: if (kind==FORECAST && strncmp(line, "FPUS", 4)==0)
304: {
305: unfore();
306: line[10]=0;
307: sprintf(fname, "fore/f_%s", line+7);
308: ff = fopen(fname, "w");
309: airmark ("fore/f_", line+7);
310: }
311: if (kind==MARINE && strncmp(line+3, " FZ", 3)==0)
312: {
313: unfore();
314: line[3]=0;
315: sprintf(fname, "fore/m_%s", line);
316: ff = fopen(fname, "w");
317: airmark ("fore/f_", line);
318: }
319: if (ff!=NULL)
320: fprintf(ff, "%s\n", line);
321: }
322: unfore()
323: {
324: if (ff!=NULL) fclose(ff);
325: }
326: airmark(code, aname)
327: char *code, *aname;
328: {
329: char fname[100], *s, xl[30];
330: double nor, wes; int n, w;
331: FILE *tf;
332: s=airport(aname);
333: if (s==NULL) return;
334: sscanf(s, "%lfN %lfW", &nor, &wes);
335: n = nor; n = n - n%4;
336: w = wes; w = w - w%4;
337: sprintf(fname, "%s%.2d.%.2d", code, n, w);
338: tf = fopen(fname, "r");
339: if (tf!=NULL)
340: {
341: while (fgets(xl, 20, tf))
342: {
343: xl[3]=0;
344: if (strcmp(xl, aname)==0)
345: {
346: fclose(tf);
347: return;
348: }
349: }
350: fclose(tf);
351: }
352: tf = fopen(fname, "a");
353: if (tf==NULL) return;
354: fprintf(tf, "%3.3s %5.2f %6.2f\n", aname, nor, wes);
355: fclose(tf);
356: }
357: hasnxt(s)
358: char *s;
359: { /* checks for more than one slash */
360: if (s==NULL) return(1);
361: s++;
362: while (*s && !isspace(*s))
363: if (*s=='/')
364: return(1);
365: else
366: s++;
367: return(0);
368: }
369: alldigs(s)
370: char *s;
371: {
372: int c;
373: while (c = *s++)
374: if (!isdigit(c) && c!='-')
375: return(0);
376: return(1);
377: }
378: startwith(s, t)
379: char *s, *t;
380: {/* true if s starts with t*/
381: while (isspace(*s)) s++;
382: if (strncmp(s, t, strlen(t)) == 0)
383: return(1);
384: return(0);
385: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.