#include #include #include #include #include #define MAX_LINES 500 #define MAX_STRING_LENGTH 512 char DATA_DIR[MAX_STRING_LENGTH]; char WGRIB2_EXEC[MAX_STRING_LENGTH]; void getInventory( void ); void convert_pgrb_file( char *pFilename, char *pFilenameOut ); void convert_sflux_file( char *pFilename, char *pFilenameOut ); void getInventory2( void ); void printError( char *pStr1, char *pStr2, float *pFloat, char *pInString ); int main( int argc, char *argv[] ); int main( int argc, char *argv[] ) { char FilenameOut[MAX_STRING_LENGTH]; char stem[MAX_STRING_LENGTH]; char *Str = NULL; int len = 0; /* Scan inputs */ if( 3 != argc ){ printError( "convert_grib_files", "Not enough arguments: USAGE: convert_grib_files.x grib_file output_directory", NULL,NULL ); } /* Setup executable/data directory */ if( NULL == getenv("HOME") ) printError("convert_grib_files", "HOME environment variable not defined",NULL,NULL); strcpy(WGRIB2_EXEC,getenv("HOME")); strcat(WGRIB2_EXEC,"/mcidas/bin/wgrib2.x"); strcpy(DATA_DIR,argv[2]); /* Work out if pgrb or sflux */ strcpy(FilenameOut,DATA_DIR); strcat(FilenameOut,"/"); if( NULL != strstr(argv[1],"pgrb") ){ Str = strstr(argv[1],".1p0deg"); if( NULL != Str ){ len = Str - argv[1]; strncpy(stem,argv[1],len); stem[len]='\0'; strcat(FilenameOut,stem); } else strcat(FilenameOut,argv[1]); strcat(FilenameOut,".dat"); #if 0 printf("%s\n",FilenameOut); #endif convert_pgrb_file( argv[1], FilenameOut ); } else if( NULL != strstr(argv[1],"sflux") ){ Str = strstr(argv[1],".grib2"); if( NULL != Str ){ len = Str - argv[1]; strncpy(stem,argv[1],len); stem[len]='\0'; strcat(FilenameOut,stem); } else strcat(FilenameOut,argv[1]); strcat(FilenameOut,".dat"); #if 0 printf("%s\n",FilenameOut); #endif convert_sflux_file( argv[1], FilenameOut ); } else printError("convert_grib_files","Cannot find a pgrb or sflux file", NULL,NULL); } /* Convert to GRIB model file to a simple binary format using wgrib.x */ /* getInventory parses the inventory list and makes a list of only * those parameters we need - have to do it this way because the * entries in the grib file can be in any order */ /* These are the entries we need from the GRIB file */ #define NLINES_DEFINED 86 char chosenLines[NLINES_DEFINED][MAX_STRING_LENGTH] = {"HGT:1000 mb", /* Height profile */ "HGT:975 mb", "HGT:950 mb", "HGT:925 mb", "HGT:900 mb", "HGT:850 mb", "HGT:800 mb", "HGT:750 mb", "HGT:700 mb", "HGT:650 mb", "HGT:600 mb", "HGT:550 mb", "HGT:500 mb", "HGT:450 mb", "HGT:400 mb", "HGT:350 mb", "HGT:300 mb", "HGT:250 mb", "HGT:200 mb", "HGT:150 mb", "HGT:100 mb", "HGT:70 mb", "HGT:50 mb", "HGT:30 mb", "HGT:20 mb", "HGT:10 mb", "TMP:1000 mb", /* Temperature profile */ "TMP:975 mb", "TMP:950 mb", "TMP:925 mb", "TMP:900 mb", "TMP:850 mb", "TMP:800 mb", "TMP:750 mb", "TMP:700 mb", "TMP:650 mb", "TMP:600 mb", "TMP:550 mb", "TMP:500 mb", "TMP:450 mb", "TMP:400 mb", "TMP:350 mb", "TMP:300 mb", "TMP:250 mb", "TMP:200 mb", "TMP:150 mb", "TMP:100 mb", "TMP:70 mb", "TMP:50 mb", "TMP:30 mb", "TMP:20 mb", "TMP:10 mb", "RH:1000 mb", /* Relative Humidity profile */ "RH:975 mb", "RH:950 mb", "RH:925 mb", "RH:900 mb", "RH:850 mb", "RH:800 mb", "RH:750 mb", "RH:700 mb", "RH:650 mb", "RH:600 mb", "RH:550 mb", "RH:500 mb", "RH:450 mb", "RH:400 mb", "RH:350 mb", "RH:300 mb", "RH:250 mb", "RH:200 mb", "RH:150 mb", "RH:100 mb", "O3MR:100 mb", /* Ozone profile */ "O3MR:70 mb", "O3MR:50 mb", "O3MR:30 mb", "O3MR:20 mb", "O3MR:10 mb", "PRES:surface", /* Surface Pressure */ "TMP:surface", /* Surfece Tempereature */ "LAND:surface", /* Land flag */ "ICEC:surface", /* Ice fraction */ "TMP:2 m above ground", /* Temperature 2m above ground */ "UGRD:10 m above ground", /* Wind speed */ "VGRD:10 m above ground" }; void getInventory( void ) { int i = 0; int j = 0; int nlines = 0; FILE *fp = NULL; char **pLines = NULL; char invDir[MAX_STRING_LENGTH]; /* Make lines array */ if( NULL == (pLines = (char **) calloc(MAX_LINES,sizeof(char *))) ) printError("L2P","getInventory: Cannot allocate pLines",NULL,NULL); for(i=0;i %s",runCommand,pFilename,invDir); if( -1 == system(command) ) printError("L2P","wgrib2 failed",NULL,NULL); /* getInventory parses the inventory list and makes a list of only * those parameters we need - have to do it this way because the * entries in the grib file can be in any order */ getInventory(); /* Output filename */ #if 0 sprintf(checkFilename,"/%s.dat",pFilename); strcpy(pFilenameOut,DATA_DIR); strcat(pFilenameOut,checkFilename); #endif /* Use new inventory list to extract only those parameters we need */ strcpy(invDir,DATA_DIR); strcat(invDir,"/new-inventory.txt"); sprintf(command,"%s -i -bin %s %s < %s >& /dev/null", runCommand,pFilenameOut,pFilename,invDir); #ifdef DEBUG printf("COMMAND: %s\n",command); #endif if( -1 == system(command) ) printError("L2P","wgrib2 failed",NULL,NULL); /* Check that output filename has been created */ if( NULL == (fpScript = fopen(pFilenameOut,"r")) ) printError("L2P","Cannot open file %s",NULL,pFilenameOut); fclose(fpScript); } /* Converts sfile GRIB file (pFilename) to putput file simple binary * format stored in pFilenameOut */ void convert_sflux_file( char *pFilename, char *pFilenameOut ) { char command[MAX_STRING_LENGTH]; char runCommand[MAX_STRING_LENGTH]; char invDir[MAX_STRING_LENGTH]; FILE *fpScript = NULL; strcpy(runCommand,WGRIB2_EXEC); /* Get inventory */ strcpy(invDir,DATA_DIR); strcat(invDir,"/inventory.txt"); sprintf(command, "%s -s %s > %s",runCommand,pFilename,invDir); if( -1 == system(command) ) printError("convert_sflux_file","wgrib2 failed",NULL,NULL); /* This routine parses the output inventory file and picks out the required * entry - must do it this way because GRIB files can be written in any * order * Output inventory list is written to new-invenotry.txt */ getInventory2(); strcpy(invDir,DATA_DIR); strcat(invDir,"/new-inventory.txt"); sprintf(command,"%s -i -lola 0:360:1 -90:181:1 %s bin %s < %s >& /dev/null", runCommand,pFilenameOut,pFilename,invDir); #ifdef DEBUG printf("COMMAND: %s\n",command); #endif /* Run wgrib2.x to convert GRIB file to a simple binary format */ if( -1 == system(command) ) printError("convert_sflux_file","wgrib2 failed",NULL,NULL); /* Check to see if wgrib run above has worked */ if( NULL == (fpScript = fopen(pFilenameOut,"r")) ) printError("convert_sflux_file","Cannot open file %s",NULL,pFilenameOut); fclose(fpScript); } /* Routine to parse inventory list of a GRIB file and make a new input * list to be used to strip out the relevant entries into a simple * binary format to be read in */ /* max lines is used by both getInventory and getInventory2 */ #define MAX_LINES 500 /* Define ehich entries to use - in this case just the SSI */ #define NLINES_DEFINED2 1 char chosenLines2[NLINES_DEFINED2][MAX_STRING_LENGTH] = {"DSWRF:surface"}; char chosenLines2_second[NLINES_DEFINED2][MAX_STRING_LENGTH] = {"hour ave fcst"}; void getInventory2( void ) { int i = 0; int j = 0; int nlines = 0; FILE *fp = NULL; char **pLines = NULL; char invDir[MAX_STRING_LENGTH]; /* Make lines array to read in inventory file */ if( NULL == (pLines = (char **) calloc(MAX_LINES,sizeof(char *))) ) printError("convert_grib","getInventory2: Cannot allocate pLines", NULL,NULL); for(i=0;i