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