fill_tables.c.noftp

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· NOFTP 代码 · 共 993 行 · 第 1/2 页

NOFTP
993
字号
/*   NAME:         fill_tables  ** NO FTP VERSION **   DESCRIPTION:  FILLS THE GRIB TABLE STRUCTURES FOR USE BY PRINTER.   DATE:         07 DEC 1996   PROGRAMMER:   TODD J. KIENITZ, SAIC      REVISIONS:   03 FEB 1996 MODIFIED BY STEVE LOWE, SAIC   07 JUN 1996 Nakajima, SAIC	-changed Fill_tables;        -added new load_table2;	-added new load_table3;*/#include <stdio.h>#include <stdlib.h>  /* for malloc, System */#include <string.h>#include "grib.h"#include "tables.h"  /* includes table definitions for table2, its                        sub-tables, and table3 structures */extern table2  *parmtab;extern table3  *lvltab;extern tables  mgotab;extern int     debug;int read_file_entry (FILE *infile, long *line, char *final_block);int locate_sect_heading (FILE *infile, char *heading, long *line);int load_table2 (FILE *infile, table2 *parmtab);int load_table3 (FILE *infile, table3 *lvltab);char	*ptr,dummy[100],blk[100];char	heading[100];	/* holds heading of Section */int	sub,cnt,id, num, i;long	line=1;/* ***=======================================================================*   A.  FUNCTION  fill_tables*       PURPOSE:  *          fill the GRIB table structures from external file for use*          by printer routine.**       INPUT :*          filnam[80] file name for tables*          *pathnm    path name to tables**       RETURN CODE:*          0> success, structures are filled;*             Variables that have changed:*                 table2 *parmtab   *                     Pointer to 255 table2 structures; *                     Points to NULL if it's 1st time in fill_tbls;  *                     Storage is then allocated & then filled;*                     Elements 250-254 represents the Sub-tables and *                     each points to a malloced block of 256*sizeof(TABLE2);*                 table3 *lvltab    *                     pointer to 255 table3 structure; *                     Points to NULL if it's 1st time in fill_tbls;  *                     Storage is then allocated and then filled;*                 tables mgotab*                     already defined in gribsimp; *                     table structure for geom, model and orig_ctrs ID's*          1> failed;*=======================================================================*/int  fill_tables (char filnam[80], char *pathnm){  FILE    *infile;                   /* input file containing table info */  char    dummy[100];                /* input variable for entire line */  char    mybuff[100];               /* working var */  char    *ptr;                      /* working var */  char    dummy_char, dummy_char2;   /* tests for space and end of section */  char    string[100];  int     i, j, m;                   /* loop counters */  int	  pass;			     /* 2 passes to ck external table */  int     Index;  int     temp;  int     cnt;			     /* temp counter */  long    line;			     /* line number in external file */  DPRINT ("Enter fill_tables to load  %s/%s\n",pathnm, filnam);/** A.1       INITIALIZE originating center, model, and geom arrays */   memset ((void *) &mgotab, '\0', sizeof(tables)); /*** A.2       IF (parm table is not yet defined)* A.2.1	        ALLOCATE storage for 255 elements !exit on error*               !Table 2's elements 250 thru 254 are sub-tables and each *               !points to another sub-set of 256 elements;* A.2.2         FOR (each sub-table supported)*                  ALLOCATE storage for 5 Parameter Sub-Tables !exit on error*               ENDFOR* A.2       ENDIF*/     if (parmtab==NULL) {	parmtab= (table2 *) malloc(sizeof(table2)* NPARM);        if (parmtab==NULL) exit(1);        for (i=250; i <= 254; i++) {	/* sub-tables */           parmtab[i].sub_tab2= (table2 *)malloc(sizeof(table2)*NPARM);	   if (parmtab[i].sub_tab2==NULL) 	     {	       fprintf(stderr,	       "Error Allocating storage for Table2[%d]\n",i);	       exit(1);	     }	   }        }/*** A.3       FOR (each element 0 thru NPARM of Table 2)*               IF (it's not sub-tables) THEN*                   INITIALIZE that element to zero*               ELSE *                   INITIALIZE all 255 elements of sub-table to Zero*               ENDIF* A.3       ENDFOR*/     for (i=0; i< NPARM; i++) {/* zero out entire structure */        if (i < 250 || i == 255)		     memset ((void *)&parmtab[i], '\0', sizeof(table2));        else memset ((void *)parmtab[i].sub_tab2, '\0', 256*sizeof(table2));        }/*** A.4       IF (level table is not yet defined)*	        ALLOCATE storage for 255 elements !exit on error*           ENDIF*/     if (lvltab==NULL) {	lvltab= (table3 *) malloc(sizeof(table3)* NLEV);        if (lvltab==NULL) exit(1);        }/*** A.5       INITIALIZE all 255 elements of Table 3 to Zero*/     memset ((void *)lvltab, '\0', sizeof(table3));/* ****************************   ** Originating Center Table **   *****************************/   sprintf (dummy, "%s/orig_ctrs", pathnm);/*** A.6       IF (OPEN "orig_ctrs" from local "pathnm" failed)*              PRINT Warning message and skip to next table*           ELSE*              !File is ready to read*/      if ((infile = fopen(dummy, "r"))==NULL)             fprintf(stderr,"Warning: Unable to open Originating Centers file!\n");      else /* file ready */	{/* * A.6.1        GETS and discards the first 4 lines !comments*/	   for(i=0;i<3;++i) fgets(dummy, sizeof(dummy), infile);/** A.6.2        WHILE (not end of file yet)*/           cnt=0;            while (!feof(infile)) 	   {/** A.6.2.1         FUNCTION read_file_entry !get  next line from file orig_ctrs* A.6.2.2         IF (line doesn't start w/ a number) THEN*                    SKIP this line*                 ENDIF* A.6.2.3         IF (originatind center id is out of range) THEN*                    SKIP this line*                 ENDIF* A.6.2.4         EXTRACT Center Name, using ctr_id as element index*/   	        if (read_file_entry (infile, &line, dummy)) break;		if (!isdigit(dummy[0])) continue;	       	if (sscanf(dummy,"%d%s",&Index, mybuff) != 2)		    continue;	/* skip if doesn't begin w/ a number */		if (Index < 0 || Index == NOCTR)		   continue;	/* skip if out of range */		/* mybuff points to 1 word of ctrname (multiple words) */	       	strncpy(mgotab.orig_ctr[Index], strstr(dummy, mybuff), 60);                ++cnt;   	    } /* while not eof *//** A.6.2       ENDWHILE*//** A.6.3       CLOSE "orig_ctrs"*/	   DPRINT ("File 'orig_ctrs' has %d entries\n", cnt);	   close(infile);/** A.6       ENDIF   !file opened ok*/        } /* FILE opened OK*//******************************//* Opens table file *//*** A.7     FORM name of the local external GRIB table file*         !ie. "localpathn"/"filenam"*/   sprintf (dummy, "%s/%s", pathnm, filnam);   /*** A.8     IF (cannot open file for reading)*            PRINT correct usage*            EXIT 0*         ENDIF*/   if((infile = fopen(dummy, "r")) == NULL) {        fprintf(stderr,	"\n********* ERROR OPENING TABLE *************\n"        "\nOPTION1:\n"        "  If you have a table YOU know is correct, you may specify it with -s\n"        "\n      ie:  gribsimp -i infile -D -s table_name\n"        "\nOPTION2:\n"        "  Use only the GRIB standard table (with NO local definitions)\n"        "\n      ie:  gribsimp -i infile -D -t \n"        "\nOPTION3:\n"        "  You can always run the decoder without table look-ups to get at the\n"        "  data and grid definitions; contact the Originating Center to get the\n"        "  correct local table.\n");        exit(0);	/* abort program */     }/***********//* TABLE 2 *//***********//* ** A.9     FUNCTION load_table2  !FILL table 2 structure*         IF (error) THEN*            FUNCTION exit_handler;*         ENDIF*/   if (load_table2 (infile, parmtab))  exit_handler(1);/***********//* TABLE 3 *//***********//* ** A.10    FUNCTION load_table3  !FILL table 3 structure*         IF (error) THEN*            FUNCTION exit_handler;*         ENDIF*/   if (load_table3(infile, lvltab))  exit_handler(1);/*  ***************************************      MODEL ID SECTION  :     LOAD THE 'GENERATING PROCESS DEFNS; (MODEL_TYPE)     format:  model_id "model name and info"   ******************************************* A.11    SET up heading for Model section*         FUNCTION locate_sec_heading !locate heading in input file*         IF (didn't see it) THEN*             RETURN 1*         ENDIF*/   strcpy (heading, "Generating Process Def");   if (locate_sect_heading (infile, heading, &line)) {	DPRINT ("Leaving fill_tables with status=1\n"); 	return(1);	}/*** A.12    WHILE (not end of file yet) DO*/   for (cnt=0; !feof(infile); )      {/** A.12.1     FUNCTION read_file_entry  !get next line*            IF (error) *               BREAK !quit loading model section*            ENDIF*/        if (read_file_entry (infile, &line, blk)) break;/** A.12.2     IF (line begins w/not a nubmer) *               SKIP line*            ENDIF*/	if (!isdigit (blk[0])) continue;/** A.12.3     EXTRACT model id and a string*            IF (not successful) THEN*               PRINT error*               RETURN 1*/        if (sscanf (blk, "%d%s", &id, dummy) != 2)    	{ fprintf(stderr,	  "Line %d: fail to get model_id, model_type\n", line);	  DPRINT ("Leaving fill_tables with status=1\n");    	  return(1);}/**            ELSE if (id is out of range)*               PRINT error message*               RETURN 1*            ENDIF*/	else if (id<0 || id >255) {	  fprintf(stderr,	  "Line %d: Model_id=%d, must be between 0 and 255\n",	  line, id); 	  DPRINT ("Leaving fill_tables with error status=1\n");	  return(1); }/* * A.12.4     STORE model name, using model_id as index*/	strncpy (mgotab.model[id], strstr(blk, dummy), 60);	cnt++;    } /** A.12    ENDWHILE*/   DPRINT ("Model Defn table has %d entries\n", cnt);	/****************************************   GEOM Id TABLE :   LOAD THE GEOMETRY INFO   format:  geom_id  "geom_name"* * A.13    SET up heading for Geometry section*         FUNCTION locate_sec_heading !locate heading in input file*         IF (didn't see it) THEN*             RETURN 1*         ENDIF*/   strcpy (heading, "Pre-defined geometries");   if (locate_sect_heading (infile, heading, &line)) {	DPRINT ("Leaving fill_tables with error status=1\n");	return(1);	}/*** A.14    FOR (each line until end of file)*/   for (cnt=0; !feof(infile); )      {/** A.14.1      FUNCTION read_file_entry  !get next line*             IF (error) THEN*                 RETURN 1*             ENDIF*/        if (read_file_entry (infile, &line, blk)) break;/* * A.14.2      IF (line doesn't start with number) THEN*                 SKIP line !loop again*             ENDIF*/	if (!isdigit (blk[0])) continue;/** A.14.3      IF (not able to extract geom_id and string)*             THEN*                 PRINT error*                 RETURN 1*/        if (sscanf (blk, "%d%s", &id, dummy) != 2)    	{ fprintf(stderr,	  "Line %d: fail to get Geom_id, Geom_name\n", line);	DPRINT ("Leaving fill_tables with status=1\n");    	 return(1);}/**             ELSE  if (id is out of range )*                 PRINT error*                 RETURN 1*             ENDIF*/	else if (id<0 || id >255) {	  fprintf(stderr,	  "Line %d: Geom_id=%d, must be between 0 and 255\n",	  line, id); 	  DPRINT ("Leaving fill_tables with status=1\n"); 	  return(1); }/* * A.14.4      STORE geometry name and info, using id as index*/	strncpy (mgotab.geom_name[id], strstr(blk,dummy), 60);	cnt++;    }  /*FOr*//** A.14    ENDFOR*/   DPRINT ("Geometry Defn table has %d entries\n", cnt);	/* ** A.15    CLOSE the Table Defn file*/  fclose(infile);  infile= NULL;/** * A.16    RETURN 0 !success* END OF FUNCTION fill_tables*/  DPRINT ("Leaving fill_tables with no errors, status=0\n");   return (0); }/***** ===================================================================* B.  FUNCTION  locate_sect_heading*     PURPOSE:  search input file for the specified heading string*               until found/end of file;*     INPUT:    FILE *infile    Table file reading from*               char *heading    String to look for*               long *line       Line number w/in infile**     RETURN CODE:*     1>  got end of file;*     0>  successful, variable line has been updated * ===================================================================*/int	locate_sect_heading (FILE *infile, char *heading, long *line){  DPRINT ("Entering locate_sect_heading w/heading=(%s)\n", heading);/** B.1      WHILE (not end of file yet )*              READ next line*              INCREMENT line counter*              IF (find Heading in line just read) THEN*                  BREAK out of while loop*              ENDIF*          ENDWHILE*/   while (!feof(infile)) /* locate the correct section first */      {  fgets(blk, sizeof(blk), infile);	 *line += 1;         if (strstr (blk, heading) != NULL) break;      }/* ** B.2      IF (end of file already) THEN*              PRINT cannot locate message*              CLOSE file*              RETURN 1*          ENDIF*/   if (feof(infile))       {  fprintf(stderr, "Error, cannot locate '%s'\n",heading);	 fclose(infile);	DPRINT ("Leaving locate_sect_heacing with bad status=1\n");	return(1);       }/*** B.3      RETURN 0 !success* END OF locate_sect_heading **/   DPRINT ("located (%s)\n", heading);    return(0);}    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?