/*----------------------------------------------------------------------*\ | Program to depict data coverage for GEOMET stations. | | | | Peter N. Schweitzer (U.S. Geological Survey, Reston, VA 22092) | \*----------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include "cvtdate.h" #include "dw.h" static char msg [128]; #ifdef MS_DOS #define SEPARATOR '\\' #else #ifdef MAC #define SEPARATOR ':' #else #define SEPARATOR '/' #endif #endif static char *month[] = { "01","02","03","04","05","06","07","08","09","10","11","12" }; struct picture { char *filename; char *label; char *count; }; static struct picture ws20 = {"ws20.gif","wind speed at 6.1m", NULL}; static struct picture ws88 = {"ws88.gif","wind speed at 2.7m", NULL}; static struct picture ws4 = {"ws4_.gif","wind speed at 1.2m", NULL}; static struct picture pg20 = {"pg20.gif","peak gust at 6.1m", NULL}; static struct picture pg4 = {"pg4_.gif","peak gust at 1.2m", NULL}; static struct picture wd = {"wd__.gif","wind direction", NULL}; static struct picture ppt = {"ppt_.gif","precipitation", NULL}; static struct picture at20 = {"at20.gif","air temperature at 6.1m", NULL}; static struct picture at4 = {"at4_.gif","air temperature at 1.2m", NULL}; static struct picture hum = {"hum_.gif","humidity", NULL}; static struct picture bp = {"bp__.gif","barometric pressure", NULL}; static struct picture st4 = {"st4_.gif","soil temperature at 4cm",NULL}; static struct picture st10 = {"st10.gif","soil temperature at 10cm",NULL}; static struct picture st20 = {"st20.gif","soil temperature at 20cm",NULL}; static struct picture *pic[] = { &ws20, &ws88, &ws4, &pg20, &pg4, &wd, &ppt, &at20, &at4, &hum, &bp, &st4, &st10, &st20 }; static int pic_count = 14; static char *year_label[] = { "79","80","81","82","83","84","85","86","87","88","89","90","91","92" }; static char *month_label[] = { "J","F","M","A","M","J","J","A","S","O","N","D" }; static char station_name[] = "Gold Spring, AZ"; /*----------------------------------------------------------------------*\ \*----------------------------------------------------------------------*/ void main (int argc, char *argv[]) { char *station_code = "gs"; struct tm *begin_tm; struct tm *end_tm; char *data_path = "."; char *output_file; int i,j,k; long n; char begin_day[6], end_day[6], current_day[6]; int year; struct tm current_tm; struct dwd dwd; struct dw_file *f; FILE *out; long vdim,hdim; int width,height,limx,limy; int black,blue,cyan,green,yellow,red,white,brown,navy; int color[40]; int x,y,x0,y0; gdImagePtr im; gdFontPtr font; /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ begin_tm = get_tm ("791027","0000"); end_tm = get_tm ("921231","2359"); /*------------------------------------------------------------------*\ | Create strings representing the yyddd part of the file names for | | the beginning and end of the time interval desired. | \*------------------------------------------------------------------*/ tm_to_yd (begin_day,begin_tm); tm_to_yd (end_day,end_tm); /*------------------------------------------------------------------*\ \*------------------------------------------------------------------*/ hdim = 12L * 10L; vdim = 14L * 24L; for (i=0; i < pic_count; i++) { n = hdim*vdim; if (pic[i]->count = (char *) malloc (n)) memset (pic[i]->count,0,n); else { fprintf (stderr,"Error could not allocate space for count array\n"); exit (1); } } /*--------------------------------------------------------------*\ | Keep track of the current day in a struct tm. | \*--------------------------------------------------------------*/ current_tm = *begin_tm; do { char name[FILENAME_MAX]; int is_begin_day, is_end_day; char *s,*t; int m; /*----------------------------------------------------------*\ | Compose the input file name | \*----------------------------------------------------------*/ tm_to_yd (current_day,¤t_tm); strcpy (name,data_path); s = name + strlen (name); if (*(s-1) != SEPARATOR) { *s++ = SEPARATOR; *s = 0; } strcpy (s,"data"); s += strlen ("data"); *s++ = SEPARATOR; strcpy (s,station_code); s += strlen (station_code); *s++ = SEPARATOR; t = current_day; *s++ = *t++; *s++ = *t++; *s++ = SEPARATOR; m = current_tm.tm_mon; strcpy (s,month[m]); s += strlen (month[m]); *s++ = SEPARATOR; strcpy (s,station_code); s += strlen (station_code); strcpy (s,current_day); s += strlen (current_day); strcpy (s,".dat"); /*----------------------------------------------------------*\ | If the current day is the same as the beginning day or | | the ending day, you'll need to check the time of each | | record in the input file against the time specified by | | begin_tm, end_tm, or both. But you only need to do the | | day check once per file, not once per record. | \*----------------------------------------------------------*/ is_begin_day = ~strcmp (current_day,begin_day); is_end_day = ~strcmp (current_day,end_day); /*----------------------------------------------------------*\ | Open the input file and read records one at a time. | \*----------------------------------------------------------*/ if (f = dw_open (name,"rb",NULL)) { while (dw_read (f,&dwd)) { x = 10 * current_tm.tm_mon + dwd.minute/6; y = 24 * (dwd.year - 79) + dwd.hour; k = y * hdim + x; if (dwd.ws20 != MISSING_VALUE) ws20.count[k]++; if (dwd.ws88 != MISSING_VALUE) ws88.count[k]++; if (dwd.ws4 != MISSING_VALUE) ws4.count [k]++; if (dwd.pg20 != MISSING_VALUE) pg20.count[k]++; if (dwd.pg4 != MISSING_VALUE) pg4.count [k]++; if (dwd.wd != MISSING_VALUE) wd.count [k]++; if (dwd.ppt != MISSING_VALUE) ppt.count [k]++; if (dwd.at20 != MISSING_VALUE) at20.count[k]++; if (dwd.at4 != MISSING_VALUE) at4.count [k]++; if (dwd.hum != MISSING_VALUE) hum.count [k]++; if (dwd.bp != MISSING_VALUE) bp.count [k]++; if (dwd.st4 != MISSING_VALUE) st4.count [k]++; if (dwd.st10 != MISSING_VALUE) st10.count[k]++; if (dwd.st20 != MISSING_VALUE) st20.count[k]++; } dw_close (f); } else { /*\ sprintf (msg,"Warning: could not open input file %s",name); dw_warn (msg); \*/ } /*----------------------------------------------------------*\ | When done with that file, adjust current_tm by adding | | one day, and repeat until you have processed the file | | corresponding to end_tm. | \*----------------------------------------------------------*/ if (current_tm.tm_year > year) { fprintf (stderr," %d",current_tm.tm_year); year = current_tm.tm_year; } next_day (¤t_tm); } while (strcmp (current_day,end_day) != 0); fprintf (stderr,"\n"); /*------------------------------------------------------------------*/ k = 0; for (y=0; y < vdim; y++) for (x=0; x < hdim; x++) { if (ws20.count[k] > 36) fprintf (stderr,"At (%d,%d), count = %d\n",x,y,ws20.count[k]); k++; } /*------------------------------------------------------------------*/ width = 256; height = 480; im = gdImageCreate (width,height); black = gdImageColorAllocate (im,0x00,0x00,0x00); blue = gdImageColorAllocate (im,0x00,0x00,0xFF); cyan = gdImageColorAllocate (im,0x00,0xFF,0xFF); green = gdImageColorAllocate (im,0x00,0xFF,0x00); yellow = gdImageColorAllocate (im,0xFF,0xFF,0x00); red = gdImageColorAllocate (im,0xFF,0x00,0x00); brown = gdImageColorAllocate (im,0xAA,0x88,0x00); white = gdImageColorAllocate (im,0xFF,0xFF,0xFF); navy = gdImageColorAllocate (im,0x00,0x00,0xBF); for (j=0; j <= 32; j++) { int r,g,b; r = ((j * 0xFF) / 32); g = r; b = r; color[j] = gdImageColorAllocate (im,r,g,b); } color[33] = color[32]; color[34] = color[32]; color[35] = color[32]; color[36] = color[32]; x0 = 3 * gdFont9x15b->w; y0 = 2 * (8 + gdFont9x15b->h) + gdFont9x15b->h; limx = width - 1; limy = height - 1; for (i=0; i < pic_count; i++) { char *count = pic[i]->count; gdImageFilledRectangle (im,0,0,limx,limy,navy); font = gdFont9x15b; x = (width - font->w * strlen (station_name))/2; y = 4; gdImageString (im,font,x,y,station_name,yellow); x = (width - font->w * strlen (pic[i]->label))/2; y = font->h + 8; gdImageString (im,font,x,y,pic[i]->label,yellow); font = gdFont7x13; k = (24 - font->h)/2; for (j=79; j < 93; j++) { int px,py; px = font->w; py = y0 + 28 * (j - 79) + k; gdImageString (im,font,px,py,year_label[j-79],yellow); } k = (10 - font->w)/2; for (j=0; j < 12; j++) { int px,py; px = x0 + 18 * j + k; py = y0 - font->h - 2; gdImageString (im,font,px,py,month_label[j],yellow); } k = 0; for (y=0; y < vdim; y++) for (x=0; x < hdim; x++) { int px,py; px = x0 + x + 8*(x/10); py = y0 + y + 4*(y/24); gdImageSetPixel (im,px,py,color[count[k++]]); } if (out = fopen (pic[i]->filename,"wb")) { gdImageGif (im,out); fclose (out); } else { fprintf (stderr,"Error: could not create output file %s\n",pic[i]->filename); exit (1); } } gdImageDestroy (im); } /*----------------------------------------------------------------------*\ \*----------------------------------------------------------------------*/