/* Transform Station Data for GrADS Station Model Plot Into Binary Initial C program provided by the Center for Ocean-Land-Atmosphere Studies at George Mason University Code expanded and modified by Dorian J. Burnette Name of Input File = stationdata.txt Name of Output File = stationdata.dat Structure of Input File: Space-delimited with No Header Variables In Following Order by Columns: -Year Month Day Hour StationID Lat Lon Temperature Dewpoint Pressure WindDirection WindSpeed CloudCover Visibility WeatherSymbol PressureChange StationID Character Limit = 6 Units: Time (UTC), Temperature (degrees F), Dewpoint (degrees F), Pressure (mb), Wind Direction (degrees), Wind Speed (knots), Visibility (SM) Cloud Cover (enter GrADS values): 20=clear, 21=scattered, 22=broken, 23=overcast, 24=obscured, 25=missing (M plotted) Weather Symbols (enter GrADS values, see chart): -thunder/sleet/freezing/hurricane [used in symbols 1-13,24-27,31-32,38,40,41] -rain/drizzle [used in symbols 3,8,12-18,24,26-30,37] -snow [used in symbols 4,9,19-23,37] -fog [used in symbol 34] -blowing snow/sand/smoke [used in symbols 33,35,36,39] If weather variables (temperature, dewpoint, etc.) are missing, enter -999 in the respective columns */ #include #include #define PI 3.141592654 /* Station Data Report Header Structure */ struct rpthdr { char id[6]; /* Station ID */ float lat; /* Latitude of Station */ float lon; /* Longitude of Station */ float t; /* Time in grid-relative units */ int nlev; /* Number of levels following */ int flag; /* Level independent var set flag */ } hdr; main () { FILE *ifile, *ofile; char rec[150]; int flag,year,month,day,hour,yrsav,mnsav,ddsav,hhsav,i; float temp,dp,press,winddir,windsp,cld,vis,wx,pressdelta,windu,windv; /* Open Files */ ifile = fopen ("stationdata.txt","r"); ofile = fopen ("stationdata.dat","wb"); if (ifile==NULL) { printf("\nError opening input text file.\n"); return; } if (ofile==NULL) { printf("\nError opening output binary file.\n"); return; } /* Convert Station Data Text File Into GrADS Binary File */ printf("\nConverting station data to binary..."); flag = 1; while (fgets(rec,sizeof(rec),ifile)!=NULL) { /* Read Station Report */ sscanf (rec,"%i %i %i %i %s %g %g %g %g %g %g %g %g %g %g %g",&year,&month,&day,&hour,hdr.id,&hdr.lat,&hdr.lon,&temp,&dp,&press,&winddir,&windsp,&cld,&vis,&wx,&pressdelta); /* Time Group Terminator...If Needed */ if (flag) { yrsav = year; mnsav = month; ddsav = day; hhsav = hour; flag = 0; } if (yrsav!=year || mnsav!=month || ddsav!=day || hhsav!=hour) { hdr.nlev = 0; fwrite(&hdr,sizeof(struct rpthdr), 1, ofile); } yrsav = year; mnsav = month; ddsav = day; hhsav = hour; /* Transform Wind Direction and Wind Speed Into U and V Vectors for GrADS */ winddir = 270-winddir; if (winddir<0) winddir = winddir + 360; winddir = (winddir * PI) / 180; windu = windsp * cos(winddir); windv = windsp * sin(winddir); /* Write Station Report */ hdr.nlev = 1; hdr.flag = 1; hdr.t = 0.0; fwrite (&hdr,sizeof(struct rpthdr), 1, ofile); fwrite (&temp,sizeof(float), 1, ofile); fwrite (&dp,sizeof(float), 1, ofile); fwrite (&press,sizeof(float), 1, ofile); fwrite (&windu,sizeof(float), 1, ofile); fwrite (&windv,sizeof(float), 1, ofile); fwrite (&cld,sizeof(float), 1, ofile); fwrite (&vis,sizeof(float), 1, ofile); fwrite (&wx,sizeof(float), 1, ofile); fwrite (&pressdelta,sizeof(float), 1, ofile); } /* End of Station Data...Write Final Terminator */ hdr.nlev = 0; fwrite (&hdr,sizeof(struct rpthdr), 1, ofile); /* Close Station Data Files */ fclose (ifile); fclose (ofile); printf("done\n"); printf("\nRun completed successfully.\n"); /* Display STNMAP Utility Reminder */ printf("Reminder: Run the STNMAP utility within the GrADS directory to create a station map file that allows GrADS to access the station data more efficiently.\n\n"); }