|
|
1.1 root 1: # include "stdio.h"
2: # include "ctype.h"
3: # include "assert.h"
4: # include "cbt.h"
5: # include "weath.h"
6: # define SURFACE 1
7: # define FORECAST 2
8: mbuf key, rec, rkey;
9: char kb[100], rb[100], rkb[100];
10: bfile *bf;
11: char *index(), *vis(), *airport();
12: double atof();
13:
14: main()
15: {
16: char line[200], line2[200];
17: int kind=0, k;
18: bf = bopen("/usr/spool/weather/air", 0);
19: assert(bf!=NULL);
20: while (gets(line))
21: {
22: k = strlen(line);
23: if (k==0) continue;
24: if (k==8 && strncmp(line+3, "00", 2)==0)
25: {
26: switch(atoi(line+5))
27: {
28: case 200: /* new england */
29: case 201: /* eastern states */
30: case 202: /* midatlantic */
31: case 203: /* southeastern */
32: case 204: /* great lakes */
33: case 205: /* ohio valley */
34: case 206: /* northern plains */
35: case 207: /* great plains */
36: case 208: /* gulf coast */
37: case 209: /* northern rockies */
38: case 210: /* southwestern */
39: case 211: /* pacific northwest */
40: case 212: /* pacific */
41: case 290: /* alaska */
42: case 291: /* hawaii */
43: kind = SURFACE;
44: continue;
45: case 431: kind = FORECAST; continue;
46: }
47: kind=0;
48: continue;
49: }
50: switch(kind)
51: {
52: case SURFACE:
53: surf(line); break;
54: case FORECAST:
55: if (strlen(line)>5 && line[3]==' ' &&
56: isupper(line[0]) && isupper(line[1]))
57: {
58: gets(line2);
59: fore(line, line2);
60: }
61: break;
62: }
63: }
64: }
65: surf(line)
66: char *line;
67: {
68: int nv, nw, i; char *v[50], *w[50], *s, *p, *aname;
69: double north, west;
70: if (strlen(line)<10) return;
71: if (!isalpha(line[0])) return;
72: s = index(line, '/');
73: if (s==NULL) return;
74: while (s>line && *s!= ' ')s--;
75: *s++ = 0;
76: nv = getargs(line, v);
77: nw = getargs(s, w);
78: aname = airport(v[0]);
79: if (aname==NULL) return;
80: for(i=0; i<nw; i++)
81: if (strlen(w[i])==4 && isdigit(w[i][0]))
82: break;
83: if (i==nw || i<2) return;
84: p = vis(v[nv-1]);
85: if (p==NULL ) return;
86: printf("at %s (%s)\n temp %s, weather%s\n", v[0], aname, w[i-2], p);
87: north = atof(aname);
88: west = atof(index(aname, ' '));
89: update( v[0], north, west, w[i-2], v[nv-1]);
90: }
91: char *
92: vis(s)
93: char *s;
94: {
95: int d;
96: static char temp[50];
97: d= atoi(s);
98: while (isdigit(*s)) s++;
99: if (*s==0)
100: {
101: sprintf(temp, " clear, visibility %d miles", d);
102: return(temp);
103: }
104: temp[0]=0;
105: for( ; *s; s++)
106: switch(*s)
107: {
108: case 'A': strcat(temp, " hail"); break;
109: case 'D': strcat(temp, " dust"); break;
110: case 'F': strcat(temp, " fog"); break;
111: case 'I': if (s[1]=='P') strcat(temp, " sleet"); break;
112: case 'K': strcat(temp, " smoke"); break;
113: case 'L': strcat(temp, " drizzle"); break;
114: case 'R': strcat(temp, " rain"); break;
115: case 'W': strcat(temp, " showers"); break;
116: case 'S': strcat(temp, " snow"); break;
117: case 'T': strcat(temp, " thunderstorms"); break;
118: case 'Z': strcat(temp, " freezing"); break;
119: case '-': break;
120: default: return(NULL);
121: }
122: return(temp);
123: }
124: fore( l1, l2)
125: char *l1, *l2;
126: {
127: char *s;
128: int p1, p2, p3, p4, max1, min1, max2, min2;
129: l1[3]=0;
130: s=airport(l1);
131: if(s==NULL) return;
132: printf("For airport %s, %s\n", l1, s);
133: sscanf(l1+4, "%*s %d %d %d %d", &p1, &p2, &p3, &p4);
134: sscanf(l2, "%*s %d %d %d %d", &max1, &min1, &max2, &min2);
135: printf("next 24 hours temps max %d min %d, prob precip %d %d\n",max1, min1, p1, p2);
136: }
137: char *
138: airport(s)
139: char *s;
140: {
141: int i;
142: key.mdata = s;
143: key.mlen = strlen(s);
144: i=bseek(bf, key);
145: if (i!=1) return(NULL);
146: rkey.mdata =rkb; rec.mdata = rb;
147: bread(bf, &rkey, &rec);
148: assert(strcmp(rkey.mdata, key.mdata)==0);
149: rkey.mdata[rkey.mlen]=0;
150: rec.mdata[rec.mlen]=0;
151: return(rec.mdata);
152: }
153: update (acode, north, west, temp, weath)
154: double north, west;
155: char *acode, *temp, *weath;
156: {
157: char fname[30], odata[2000], *p, *s;
158: struct wline *wp;
159: int n, w, f;
160: n = north;
161: n = n-n%4;
162: w = west;
163: w = w-w%4;
164: sprintf(fname, "/usr/spool/weather/o%.2d.%.2d", n, w);
165: f = open(fname, 2);
166: if (f>=0)
167: {
168: n=read(f, odata, 2000);
169: assert(n<2000);
170: for(wp=odata; wp<odata+n; wp++)
171: {
172: if (strncmp(wp->anam, acode, 3)==0)
173: {
174: strncpy(wp->wtemp, temp, 3);
175: strncpy(wp->wweath, weath, 7);
176: lseek (f, 0L, 0);
177: write(f, odata, n);
178: return;
179: }
180: }
181: }
182: else
183: {
184: f = creat(fname, 0666);
185: wp = odata;
186: n=0;
187: }
188: sprintf(wp->anam, "%.3s%5.2f%6.2f%3.3s%7.7s\n", acode, north,
189: west, temp, weath);
190: lseek (f, 0L, 0);
191: write(f, odata, n+sizeof(*wp));
192: close(f);
193: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.