int main ( void ) /************************************************************************ * GDGRIB2 * * * * This program encodes a selected GEMPAK grid into a GRIB2 message. * * * * Command line: * * gdgrib2 * ** * * Log: * * S. Gilbert/NCEP 5/2005 Orig * * S. Gilbert/NCEP 3/2006 Replaced dg_clal with dg_nend * * T. Piper/SAIC 01/08 Added GD_INIT; removed from IN_BDTA * * B. Yin/ERT 09/15 Added bulk processing from table * ***********************************************************************/ { int respond, ret, ier, mode=1, done, skip, j; int g2len; /* Length of GRIB2 message */ unsigned char *g2msg; /* GRIB2 message */ char cur_gbfile[LLMXLN]=""; char chdr[22]; /* WMO Header */ FILE *gbfptr=0; GDG2_input input; /* user input variables */ GDG2_gemgrid gemgrid; /* GEMPAK grid and info */ /*---------------------------------------------------------------------*/ /* * Initialize TAE. */ ip_init ( &respond, &ret ); if ( ret == 0 ) { ip_idnt ( "GDGRIB2", &done, 7 ); /* * Initialize GEMPLT. */ gg_init ( &mode, &ret ); if ( ret == 0 ) { /* * Initialize grid library common area grdcmn.cmn */ gd_init ( &ier ); /* * Initialize the DG library */ dg_intl ( &ret ); done = 0; skip = 1; } else { done = 1; } } else { done = 1; } /* * Process next request, if user has one. */ while ( done == 0 ) { /* * Wait for user input, if not first time through this loop */ if ( skip == 0 ) ip_dynm( &done, &ret ); skip = 0; if ( done != 0 ) break; /* Exit out of interactive loop */ /* * Get user input info */ gdg2in( &input, &ret ); if ( ret != 0 ) { er_wmsg("GDGRIB2",&ret," ",&ier,7,1); continue; } else if ((strlen(input.g2conv) != (size_t)0) && (ret == 0)) { /* * Skip program prompt and get input from conversion table */ skip = 1; } if ( strlen(input.g2file) == (size_t)0 ) { ret=-28; er_wmsg("GDGRIB2",&ret," ",&ier,7,1); continue; } /* * Get requested grid */ gdgetgrid( &input, &gemgrid, &ret ); if ( ret != 0 ) { er_wmsg("GDGRIB2",&ret," ",&ier,7,1); continue; } /* * Make GRIB2 field */ gdmakeg2( &input, &gemgrid, &g2msg, &g2len, &ret ); if ( ret != 0 ) { er_wmsg("GDGRIB2",&ret," ",&ier,7,1); continue; } /* * Open GRIB2 file, if not already open. */ if ( strncmp(input.g2file, cur_gbfile, LLMXLN) != 0 ) { /* * If output GRIB file is different, must close previous one first */ if ( strlen(cur_gbfile) != (size_t)0 ) { cfl_clos( gbfptr, &ret); gbfptr=0; } /* * Open GRIB file */ gbfptr = cfl_aopn( input.g2file, &ret); if ( ret == 0 ) { strncpy( cur_gbfile, input.g2file, LLMXLN); } else { er_wmsg("CFL",&ret,input.g2file,&ier,7,strlen(input.g2file)); ret = -24; er_wmsg("GDGRIB2",&ret," ",&ier,7,1); gbfptr = 0; continue; } } /* * Write out WMO Header, if requested */ gdmakewmo( &input, &gemgrid, chdr, &ret ); if ( strlen(chdr) == (size_t)21 ) { cfl_writ( gbfptr, strlen(chdr), (unsigned char*)chdr, &ret ); } /* * Write out GRIB2 message. */ cfl_writ( gbfptr, g2len, g2msg, &ret ); if ( ret != 0 ) { er_wmsg("CFL",&ret,input.g2file,&ier,7,strlen(input.g2file)); ret = -25; er_wmsg("GDGRIB2",&ret," ",&ier,7,1); continue; } /* * Free no longer needed allocated space */ if ( gemgrid.grid != 0 ) free(gemgrid.grid); if ( g2msg != 0 ) free(g2msg); } /* * Clean up files */ dg_nend( &ret ); if ( gbfptr != 0 ) cfl_clos( gbfptr, &ret); /* * Exit the GEMPAK user interface */ ip_exit( &ret ); return(0); }
void spf_open ( char *filnam, Boolean crt, FILE **fptr, int *flen, int *iret ) /************************************************************************ * spf_open * * * * This function opens an SPF file for update. If the file doesn't * * exist and flag "crt" is TRUE, then it will be created. If the file * * suffix is not ".spf", it will be added. If the user has only read * * permission to the SPF file, it will be opened as "read-only". * * * * spf_open ( filnam, crt, fptr, flen, iret ) * * * * Input parameters: * * *filnam char File name with full path * * crt Boolean Flag, create a new file or not * * * * Output parameters: * * **fptr FILE Pointer to a file pointer * * *flen int File size * * *iret int Return code * * 1 - File opened as READ_ONLY * * 0 - Normal * * -1 - File does not exist * * -2 - File cannot be opened * * -3 - Failure to create SPF file * * * ** * * Log: * * J. Wu/GSC 6/01 create * * J. Wu/GSC 6/01 use cst_srch() * * J. Wu/GSC 7/01 allow access to read-only SPF files * * E. Safford/SAIC 02/06 fix cfl_inqr call, make 3rd param long * ***********************************************************************/ { int ipos, ier, ierr; char newfil[FILE_FULLSZ]; char filepart[MXFLSZ], pathpart[LLPATH]; long localFlen; /*---------------------------------------------------------------------*/ *iret = G_NORMAL; *flen = 0; /* * Check if the file suffix is ".spf". If not, add it. */ cfl_path ( filnam, pathpart, filepart, &ier ) ; cst_srch ( 0, strlen(filepart), ".spf", filepart, &ipos, &ier ); if ( ier == -4 ) { strcat (filnam, ".spf"); } /* * Check the file status, if it exists, open it; otherwise, create * it if authorized. */ cfl_inqr( filnam, NULL, &localFlen, newfil, &ier ); *flen = ( int ) localFlen; if ( ier == 0 ) { *fptr = cfl_aopn( filnam, &ierr ); if ( *fptr == NULL ) { *fptr = cfl_ropn( filnam, NULL, &ierr ); if ( *fptr == NULL ) { *iret = -2; er_wmsg ( "CFL", &ierr, filnam, &ier, 3, strlen( filnam ) ); } else { *iret = 1; } } } else { *iret = -1; if ( crt == TRUE ) { *fptr = ( FILE *) spf_create( filnam, flen, iret ); } } }
void dc_dlog ( char *messag, int *lenm, int *iret ) /************************************************************************ * dc_dlog * * * * This routine opens the decoder log file, writes the given message * * and closes the file. * * * * dc_dlog ( messag, lenm, iret ) * * * * Input parameters: * * *messag char Message to write to the log * * *lenm int Number of chars in the message * * * * Output parameters: * * *iret int Return code * * 0 = normal return * * -2 = error writing to log * * * ** * * Log: * * S. Jacobs/NMC 7/95 * * S. Jacobs/NCEP 6/96 Updated documentation; Removed check * * for log file not opened; Changed to use * * a FILE stream with CFL_WRIT * * S. Jacobs/NCEP 7/96 Added open and close of log file * * K. Tyle/GSC 1/97 Remove prog. name and dattim; remove * * loglev variable * * S. Jacobs/NCEP 2/01 Removed all references to ulog * ***********************************************************************/ { char mesg[DCMXLN+8], tstr[12]; int lens, ier; FILE *fplog; /*---------------------------------------------------------------------*/ *iret = 0; /* ** Start the output message with the process ID. */ cst_inch ( ipid, tstr, &ier ); strcpy ( mesg, "[" ); strcat ( mesg, tstr ); strcat ( mesg, "] " ); /* ** Concatenate the input message to the output string. */ strcat ( mesg, messag ); cst_lstr ( mesg, &lens, &ier ); /* ** Get the length of the message and add a Line Feed and ** a NULL to the end. */ if ( lens > (DCMXLN+8) - 2 ) { mesg [lens-1] = CHLF; mesg [lens] = CHNULL; } else { mesg [lens] = CHLF; mesg [++lens] = CHNULL; } /* ** Open the decoder log file. */ fplog = NULL; if ( dcdlog[0] == '-' ) { /* ** If the file name is "-", open standard error for logging. */ fplog = stderr; } else { /* ** Try to open the decoder log file. */ fplog = cfl_aopn ( dcdlog, &ier ); if ( ier != 0 ) { /* ** If there is an error opening the file, open standard ** error for real-time processing, otherwise write an ** error message to the LDM log. */ if ( irltim ) { fplog = stderr; } else { *iret = -3; return; } } } /* ** Write the message to the log file. */ cfl_writ ( fplog, lens, (unsigned char *)mesg, &ier ); if ( ier != 0 ) { *iret = -2; } /* ** Close the decoder log file. */ if ( fplog == stderr ) { /* ** If standard error is being used for logging, do nothing. */ } else if ( fplog != NULL ) { /* ** Otherwise, close the log file. */ cfl_clos ( fplog, &ier ); } }