fill_tables.c.ftp
来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· FTP 代码 · 共 1,224 行 · 第 1/3 页
FTP
1,224 行
/* NAME: fill_tables 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 15 APR 1996 added call to ftp remote table /Nakajima, 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);int FTP_getfile (char *filenm, char *loc_pathnm);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 LOOP two times !ftp may be needed*/ for (pass=1; pass <= 2 ; pass++) {/** A.6.1.a IF (OPEN "orig_ctrs" from local "pathnm" failed)* THEN ! file not ready, ftp it down*/ if ((infile = fopen(dummy, "r"))==NULL) { /* file not ready. ftp it down if not already done so */ if (pass== 1) { /* 1st pass: * no local file, try to Ftp it down *//** A.6.1.a.1 IF (haven't ftp-ed yet) THEN !1st pass* FUNCTION FTP_getfile !ftp "orig_ctrs" fr remote's pathnm* IF (ftp failed) THEN* JUST skip* ENDIF*/ DPRINT ("File %s not avail locally. Will attempt to FTP.\n", dummy); if (FTP_getfile ("orig_ctrs", pathnm) != 0) break; } else { /* 2nd pass: * tried ftp, but file still not avail, skip; *//** A.6.1.a.2 ELSE !2nd pass: already ftp-ed but still cannot open file* SKIP* ENDIF*/ fprintf(stderr,"Warning: Unable to open Originating Centers file!\n"); break; } }/** A.6.1.b ELSE !file opened ok*/ else /* file ready */ { if (pass==2) DPRINT ("Successfully FTP '%s';\n",dummy);/* * A.6.1.b.1 GETS and discards the first 4 lines !comments*/ for(i=0;i<3;++i) fgets(dummy, sizeof(dummy), infile);/** A.6.1.b.2 WHILE (not end of file yet)*/ cnt=0; while (!feof(infile)) {/** A.6.1.b.2.1 FUNCTION read_file_entry !get next line from file orig_ctrs* A.6.1.b.2.2 IF (line doesn't start w/ a number) THEN* SKIP this line* ENDIF* A.6.1.b.2.3 IF (originatind center id is out of range) THEN* SKIP this line* ENDIF* A.6.1.b.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; } /* whiule not eof *//** A.6.1.b.2 ENDWHILE*//** A.6.1.b.3 CLOSE "orig_ctrs"* A.6.1.b.4 BREAK out of 2-pass loop !already done*/ DPRINT ("File 'orig_ctrs' has %d entries\n", cnt); close(infile); break; /* don't loop */ } /* FILE opened OK*//** A.6.1.b ENDIF !file opened ok*/ } /* PASS loop *//** A.6 ENDLOOP! pass loop*//******************************//* 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)* THEN*/ if((infile = fopen(dummy, "r")) == NULL) { DPRINT ("File %s not avail locally. Will attempt to FTP\n",dummy);/** A.8.1 FUNCTION FTP_getfile !filename from remote "pathnm"* IF (ftp failed OR unable to open file for reading) * THEN*/ if ( FTP_getfile (filnam, pathnm) != 0 || (infile = fopen(dummy, "r")) == NULL) {/** PRINT correct usage* EXIT 0* ENDIF*/ 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 */ } DPRINT ("Loading ftp-ed Defn file: '%s'\n",dummy); }/** A.8 ENDIF*//***********//* 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"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?