/* * in process of conversion to gribw */ #include #include #include #include #include #include #include "gribwlib.h" #include "pds4.h" /* PDS units */ #define HOUR 1 /* * take a 6-hourly time series, and make a daily time series * * 5/96 Wesley Ebisuzaki * * 11/97: added time range = 2 * * 2/98: renamed from daily_ts.c to 6hr2daily.c * made into gribw example program * read sorted wgrib inventory instead of in sequential order */ #define N 100000 #define DHOUR 6 /* special code for TMIN and TMAX .. center PDS dependant numbers */ /* set TMIN and TMAX to -1 for no TMIN/TMAX processing */ #define TMAX 15 #define TMIN 16 int main(int argc, char **argv) { long int len_grib, pos = 0; unsigned char *pds, *gds; FILE *input, *output; int i; float *sum; int *count; int array_size, hr; int tr, p1, p2, units, nave, nmissing, expected_count; unsigned int field; unsigned int time0, time; float *data; int ndata; /* preliminaries .. open up all files */ if (argc != 3) { fprintf(stderr, "%s [in gribfile] [out gribfile]\n", argv[0]); exit(8); } if ((input = fopen(argv[1],"rb")) == NULL) { fprintf(stderr,"could not open file: %s\n", argv[1]); exit(7); } if ((output = fopen(argv[2],"wb")) == NULL) { fprintf(stderr,"could not open file: %s\n", argv[2]); exit(7); } count = (int *) malloc(N * sizeof(int)); sum = (float *) malloc(N * sizeof(float)); array_size = N; if (count == NULL || sum == NULL) { fprintf(stderr,"memeory allocation failed\n"); exit(8); } pos = 0; for(;;) { hr = 0; while (hr < 24) { len_grib = rd_grib_rec(input, pos, &pds, &gds, &data, &ndata); if (len_grib <= 0) break; time = get_InitYYYYMMDDHH(pds); /* is it the expected time */ if (hr == 0) { if (time % 100 != 0) { fprintf(stderr,"expecting 00Z not %d\n", time); pos += len_grib; continue; } } else { if (time != time0 + hr) { fprintf(stderr,"missing time %d %d\n",time,time0+hr); hr = 0; continue; } } if (hr == 0) { time0 = time; if (ndata > array_size) { count = (int *) realloc(count, ndata * sizeof(int)); sum = (float *) realloc(sum, ndata * sizeof(float)); array_size = ndata; if (count == NULL || sum == NULL) { fprintf(stderr,"memeory allocation failed\n"); exit(8); } } for (i = 0; i < ndata; i++) { count[i] = 0; sum[i] = 0.0; } field = PDS_Field(pds); } else { if (field != PDS_Field(pds)) { fprintf(stderr,"changed field parameter\n"); hr = 0; continue; } } if (PDS_PARAM(pds) == TMAX) { for (i = 0; i < ndata; i++) { if (! UNDEFINED_VAL(data[i])) { if (count[i] == 0) { sum[i] = data[i]; count[i] = 1; } else { if (sum[i] < data[i]) sum[i] = data[i]; } } } } else if (PDS_PARAM(pds) == TMIN) { for (i = 0; i < ndata; i++) { if (! UNDEFINED_VAL(data[i])) { if (count[i] == 0) { sum[i] = data[i]; count[i] = 1; } else { if (sum[i] > data[i]) sum[i] = data[i]; } } } } else { for (i = 0; i < ndata; i++) { if (! UNDEFINED_VAL(data[i])) { sum[i] += data[i]; count[i]++; } } } pos += len_grib; hr += DHOUR; } if (len_grib <= 0) break; expected_count = 24 / DHOUR; if (PDS_PARAM(pds) == TMIN || PDS_PARAM(pds) == TMAX) expected_count = 1; for (i = 0; i < ndata; i++) { if (count[i] == expected_count) { sum[i] /= (double) expected_count; } else { sum[i] = UNDEFINED; } } printf("processing %d\n",time0); set_InitYYYYMMDDHH(pds, time0); get_TimeRange(pds, &tr, &p1, &p2, &units, &nave, &nmissing); if (tr == 3) { p1 = 0; p2 = 24; units = HOUR; nave = (24/DHOUR); nmissing = 0; } else if (tr == 10) { tr = 113; p1 = p2; p2 = DHOUR; units = HOUR; nave = (24/DHOUR); nmissing = 0; } else if (tr == 2 && p1 == 0 && p2 == 6 && units == HOUR) { tr = 2; p1 = 0; p2 = 24; units = HOUR; nave = 0; nmissing = 0; } else { fprintf(stderr,"unexpected time range %d p=(%d,%d) units=%d\n", tr, p1, p2, units); continue; } set_TimeRange(pds, tr, p1, p2, units, nave, nmissing); wrt_grib_rec(pds, gds, data, ndata, output); } fclose(output); fclose(input); return 0; }