PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wrfv2_fire/external/io_grib1/MEL_grib1/ld_enc_lookup.c

http://github.com/jbeezley/wrf-fire
C | 503 lines | 295 code | 44 blank | 164 comment | 81 complexity | aac6c2a33727eb02c8bde9f35c5e57d6 MD5 | raw file
Possible License(s): AGPL-1.0
  1. /* Revision logs:
  2. 16Jul97 /atn: only clr Encoder section of db tables; chg warning to stdout;
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include "grib_lookup.h" /* combined lookup structs */
  10. #include "dprints.h" /* for dprints */
  11. #include "gribfuncs.h" /* prototypes */
  12. extern PARM_DEFN db_parm_tbl[]; /* parm conversion info */
  13. extern LVL_DEFN db_lvl_tbl[]; /* level conversion info */
  14. extern MODEL_DEFN db_mdl_tbl[]; /* model conversion info */
  15. extern GEOM_DEFN db_geom_tbl[]; /* Geom conversion info */
  16. /*
  17. ****************************************************************************
  18. * A. FUNCTION: ld_enc_lookup
  19. * This function reads in the information from an external Lookup
  20. * table used by the GRIB Encoder (ie: neons2grib.tab). This info
  21. * is used to convert Databse codes to the GRIB Code Numbers.
  22. *
  23. * INTERFACE:
  24. * int ld_enc_lookup (lookup_fn, errmsg)
  25. *
  26. * ARGUMENTS (I=input, O=output, I&O=input and output):
  27. * (I) char *lookup_fn; Name of Lookup file to read from;
  28. * (O) char *errmsg REturned filled if error occurred;
  29. *
  30. * RETURN CODE:
  31. * 0> successful, the following pre-defined arrays required for
  32. * encoding GRIB messages are filled=
  33. * PARM_DEFN db_parm_tbl[NPARM * MAX_PARM_TBLS]; (parameter info)
  34. * LVL_DEFN db_lvl_tbl[NLEV]; (level info)
  35. * MODEL_DEFN db_mdl_tbl[NMODEL]; (model info)
  36. * GEOM_DEFN db_geom_tbl[NGEOM]; (geometry info)
  37. * 1> file open error or got error/eof while reading; errmsg filled;
  38. ****************************************************************************
  39. - only break out of curr Loop if sees next section's header string;
  40. */
  41. #if PROTOTYPE_NEEDED
  42. int ld_enc_lookup ( char *lookup_fn, char *errmsg)
  43. #else
  44. int ld_enc_lookup ( lookup_fn, errmsg)
  45. char *lookup_fn; char *errmsg;
  46. #endif
  47. {
  48. FILE *infile;
  49. char *func="ld_enc_lookup"; /* name of function */
  50. char *ptr, temp[200], TableCode, dummy[100];
  51. int stat=1, num, LineRead, cnt, code=0, indx0= 0;
  52. int Indx, indxA=0, indxB=0, indxC=0, indxD=0, indxE= 0;
  53. int *indxptr;
  54. char *px;
  55. char achDBsField[30]; /* DBs Field Name */
  56. char strDSF[50],strGribCode[50], strScale[50], strOffset[50];
  57. char subtbl[20]; /* (0:maintbl) or (a/b/c/d/e:subtabl)*/
  58. int GribCode; /* GRIB Code Number */
  59. float fScale; /* Scale */
  60. float fOffset; /* Offset */
  61. short sDSF; /* DSF */
  62. PARM_DEFN *parmptr; /* ptr to desired cell w/in Parm arr*/
  63. DPRINT2 ("Entering %s\nlookup %s\n", func, lookup_fn);
  64. /*
  65. *
  66. * A.0 CLEAR out all lookup arrays ! Encoder section only
  67. */
  68. for (num=0; num < NPARM ; num++) {
  69. db_parm_tbl[num].usParm_id= num;
  70. db_parm_tbl[num].usParm_sub= 0; /* not used for main tbl */
  71. db_parm_tbl[num].db_name[0] = '\0';
  72. db_parm_tbl[num].fScale = 1.;
  73. db_parm_tbl[num].fOffset = 0.;
  74. db_parm_tbl[num].sDSF = 0;
  75. }
  76. for (num=NPARM; num < NPARM * MAX_PARM_TBLS; num++) { /* for sub-tbls */
  77. db_parm_tbl[num].usParm_id= 250 + num / NPARM;
  78. db_parm_tbl[num].usParm_sub= num % NPARM;
  79. db_parm_tbl[num].db_name[0] = '\0';
  80. db_parm_tbl[num].fScale = 1.;
  81. db_parm_tbl[num].fOffset = 0.;
  82. db_parm_tbl[num].sDSF = 0;
  83. }
  84. for (num=0; num < NLEV; num++) {
  85. db_lvl_tbl[num].usLevel_id = num;
  86. db_lvl_tbl[num].db_name[0] = '\0';
  87. db_lvl_tbl[num].fScale = 1.;
  88. db_lvl_tbl[num].fOffset = 0.;
  89. }
  90. for (num=0; num < NMODEL; num++) {
  91. db_mdl_tbl[num].usModel_id = num;
  92. db_mdl_tbl[num].db_name[0] = '\0';
  93. }
  94. for (num=0; num < NGEOM; num++) {
  95. db_geom_tbl[num].usGeom_id = num;
  96. db_geom_tbl[num].db_name[0] = '\0';
  97. }
  98. /*
  99. *
  100. * A.1 OPEN Lookup file for reading
  101. * RETURN 1 if fails;
  102. */
  103. infile = fopen(lookup_fn, "r");
  104. if (infile==NULL) {
  105. DPRINT2 ("%s: failed to open %s\n", func, lookup_fn);
  106. sprintf (errmsg ,"%s: failed to open %s\n", func, lookup_fn);
  107. goto BYE;
  108. }
  109. /****Database's PARM TABLE -- (0/A/B/C/D/E) ***
  110. Sample:
  111. NEONS to GRIB Parameter Table
  112. NEONS FIELD TABLE CODE GRIB CODE SCALE OFFSET DSF
  113. =========== ========== ========= ===== ====== ===
  114. pres 0 001 1.0 0.0 1
  115. pres 0 002 1.0 0.0 1
  116. *
  117. * *** Database's Parameter Defn conversion info ***
  118. * A.2 KEEP reading until last of Header/Comment line (see '===')
  119. * RETURN error if fails
  120. */
  121. LineRead = 0;
  122. while (strstr (temp, "===") == NULL) {
  123. fgets(temp, sizeof(temp), infile); /* skip Comment/Header lines */
  124. LineRead++;
  125. if (feof(infile) || ferror(infile)) {
  126. DPRINT1 ("%s: got EOF/ERROR before loading DBs PARM info\n", func);
  127. sprintf(errmsg,
  128. "%s: got EOF/ERROR before loading DBs PARM info (%s:line %d)\n",
  129. func , lookup_fn, LineRead);
  130. goto BYE;
  131. }
  132. }
  133. /*
  134. *
  135. * A.3 FOR (successfully read a line from file) DO
  136. * BREAK out of loop if sees Header of next Table
  137. * EXTRACT next DBs's Parameter info from line read
  138. * IF (fails) SKIP rest of Parm defn ;
  139. * !parm_name, tblcode, parmid, scalefctr, offset, Decsclfctr;
  140. * DROP line if Parm id is out of range
  141. * DROP line if Table Code is not (0/A/B/C/D/E)
  142. * DROP defn if Code has already been defined;
  143. * STORE in Parm Array cell whose index equals Parmid
  144. * ENDFOR
  145. */
  146. for (cnt=0; fgets(temp, sizeof(temp), infile)!=NULL; ) {
  147. LineRead++;
  148. for (ptr=temp; *ptr==' '; ptr++) ; if (*ptr == '#') continue;
  149. if (strstr(temp, "to GRIB Level Table") != NULL)
  150. break; /* END OF CURR SECT */
  151. if ((num= sscanf (temp,"%s%s%s%s%s%s%s", achDBsField, subtbl,
  152. strGribCode, strScale, strOffset, strDSF, dummy)) != 6)
  153. {
  154. if (num>0)
  155. fprintf(stdout,"Warning: unmatched Parm args, drop line %d in %s\n",
  156. LineRead, lookup_fn);
  157. continue;
  158. }
  159. for (px=strGribCode; *px; px++)
  160. if (!isdigit(*px)) {
  161. fprintf(stdout,
  162. "Warning: invalid GRIB code, drop line %d in %s\n",
  163. LineRead, lookup_fn);
  164. continue;
  165. }
  166. GribCode = atoi(strGribCode);
  167. fScale = (float) atof(strScale);
  168. fOffset = (float) atof(strOffset);
  169. sDSF = (short) atoi(strDSF);
  170. if (GribCode < 0 || GribCode >= NPARM) {
  171. fprintf(stdout,
  172. "Warning: ParmId '%d' out of range, drop line %d in %s\n",
  173. GribCode, LineRead, lookup_fn); continue; }
  174. if (strlen(subtbl) > 1) { fprintf(stdout,
  175. "Warning: Invalid bad TableCode '%s', drop line %d in %s;\n",
  176. subtbl, LineRead, lookup_fn); continue; }
  177. /* depending on which Table Code it is, entry will be stored in
  178. its corresponding Parameter Table;
  179. >>> if Tablecode is '0', store at array index PARM_ID;
  180. >>> if Tablecode is 'A/B/C/D/E',
  181. >>> then store at array index [NPARM*(PARM_ID-249) + PARM_SUB];
  182. >>> UNDEFINED IDS WILL HAVE EMPTY CELLS;
  183. */
  184. TableCode = (isalpha(*subtbl) ? tolower(*subtbl) : *subtbl);
  185. if (TableCode=='0') {
  186. Indx = 0;
  187. parmptr = db_parm_tbl + GribCode;
  188. parmptr->usParm_id = (unsigned short) GribCode;
  189. parmptr->usParm_sub = 0;
  190. }
  191. else if (TableCode>= 'a' && TableCode <= 'e') {
  192. if (GribCode==0) {
  193. fprintf(stdout,
  194. "Warning: Cannot use Parmid 0 for sub-tbl, drop line %d in %s\n"
  195. , LineRead, lookup_fn);
  196. continue;
  197. }
  198. Indx = 256 * (TableCode-'a'+1); /* 'a':256-.. 'b':512-.. */
  199. parmptr = db_parm_tbl + Indx + GribCode; /* actual Index */
  200. parmptr->usParm_id = 250 + (TableCode-'a');
  201. parmptr->usParm_sub = (unsigned short) GribCode;
  202. }
  203. else { fprintf(stdout,
  204. "Warning: Invalid Table '%s' (0,A-E only), drop line %d in %s\n",
  205. subtbl, LineRead, lookup_fn);
  206. continue;
  207. }
  208. if (parmptr->db_name[0] != '\0')
  209. { /* drop line if Duplicate */
  210. fprintf(stdout,
  211. "Warning: duplic Parm %d in Tbl %c (Index=%d), drop line %d in %s\n",
  212. parmptr->usParm_id, TableCode,
  213. PARMTBL_INDX(parmptr->usParm_id, parmptr->usParm_sub),
  214. LineRead, lookup_fn);
  215. continue;
  216. }
  217. strcpy (parmptr->db_name, achDBsField);
  218. parmptr->fScale = fScale;
  219. parmptr->fOffset = fOffset;
  220. parmptr->sDSF = sDSF;
  221. ++cnt; /* keep track of #entries loaded */
  222. DPRINT9(
  223. "(+)T2-%c cd=%d: Parm=%d sub=%d, INDX=%d, dbnm=%s Scl=%.2f Ofs=%.2f D=%d\n"
  224. , TableCode, GribCode,
  225. parmptr->usParm_id, parmptr->usParm_sub,Indx+GribCode,
  226. parmptr->db_name, parmptr->fScale, parmptr->fOffset, parmptr->sDSF);
  227. }
  228. /*
  229. * DEBUG print
  230. */
  231. DPRINT1("Parameter table has %d entries\n", cnt);
  232. /******** Database's LEVEL TABLE *******
  233. Sample:
  234. NEONS to GRIB Level Table (FNMOC VERSION)
  235. NEONS Level Type GRIB CODE SCALE OFFSET
  236. ================ ========= ===== ======
  237. surface 001 1.0 0.0
  238. isth_lvl 004 0.0 273.16
  239. trpp_lvl 007 1.0 0.0
  240. *
  241. * *** DBs's Level Defn conversion info ***
  242. * A.4 KEEP reading until last of Header/Comment line (see '===')
  243. * RETURN error if fails
  244. */
  245. while (strstr (temp, "====") == NULL) {
  246. fgets(temp, sizeof(temp), infile); /* skip Comment/Header lines */
  247. LineRead++;
  248. if (feof(infile) || ferror(infile)) {
  249. DPRINT1 ("%s: got EOF/ERROR before loading DBs LEVEL info\n",func);
  250. sprintf(errmsg,
  251. "%s: got EOF/ERROR before loading LEVEL info (line %d in %s)\n",
  252. func , LineRead, lookup_fn);
  253. goto BYE;
  254. }
  255. }
  256. /*
  257. * A.5 FOR (successfully read a line from file) DO
  258. * BREAK out of loop if sees Header of next Table
  259. * EXTRACT next DBs's Level info into Level Array:
  260. * !format= level_type, level_id, scale, offset
  261. * DROP line if not enough arguments
  262. * DROP line if Duplicate Parm defn
  263. * STORE in Level Array cell whose index equals Level id
  264. * ENDFOR
  265. */
  266. for (cnt=0; fgets(temp, sizeof(temp), infile) != NULL; )
  267. {
  268. LineRead++;
  269. if (strstr(temp, "to GRIB Model Table") != NULL)
  270. break; /* Assume end of section */
  271. for (ptr=temp; *ptr==' '; ptr++) ; if (*ptr == '#') continue;
  272. if ((num= sscanf (temp, "%s%s%s%s%s",
  273. achDBsField, strGribCode, strScale, strOffset, dummy)) != 4)
  274. {
  275. if (num>0)
  276. fprintf(stdout,"Warning: unmatched Level args, drop line %d in %s\n",
  277. LineRead, lookup_fn);
  278. continue;
  279. }
  280. GribCode = atoi(strGribCode);
  281. fScale = (float) atof(strScale);
  282. fOffset = (float) atof(strOffset);
  283. if (GribCode < 0 || GribCode >= NLEV) {
  284. fprintf(stdout,
  285. "Warning: Level_id '%d' out of range, drop line %d in %s\n"
  286. ,GribCode, LineRead, lookup_fn);
  287. continue;
  288. }
  289. if (db_lvl_tbl[GribCode].db_name[0] != '\0') {
  290. fprintf(stdout,
  291. "Warning: duplic Level %d, drop defn ending on line %d in %s\n",
  292. GribCode, LineRead, lookup_fn);
  293. continue;
  294. }
  295. db_lvl_tbl[GribCode].usLevel_id = (unsigned short) GribCode;
  296. strncpy (db_lvl_tbl[GribCode].db_name, achDBsField,
  297. sizeof(db_lvl_tbl[GribCode].db_name)-1);
  298. db_lvl_tbl[GribCode].fScale = fScale;
  299. db_lvl_tbl[GribCode].fOffset = fOffset;
  300. ++cnt; /* number loaded */
  301. DPRINT4("(+) Level=%d, db_name=%s, Scl=%f, Offs=%f\n",
  302. db_lvl_tbl[GribCode].usLevel_id, db_lvl_tbl[GribCode].db_name,
  303. db_lvl_tbl[GribCode].fScale, db_lvl_tbl[GribCode].fOffset);
  304. }
  305. /*
  306. * DEBUG print
  307. */
  308. DPRINT1("Level table has %d entries\n", cnt);
  309. /*** Database's MODEL TABLE***
  310. Sample:
  311. NEONS to GRIB Model ID Table (FNMOC VERSION)
  312. NEONS Model Name GRIB CODE
  313. ================ =========
  314. NORAPS 001
  315. COAMPS 002
  316. *
  317. * *** Database 's Model Defn conversion info ***
  318. * A.6 KEEP reading until last of Header/Comment line (see '===')
  319. * RETURN error if fails
  320. */
  321. while (strstr (temp, "====") == NULL) {
  322. fgets(temp, sizeof(temp), infile); /* skip Comment/Header lines */
  323. LineRead++;
  324. if (feof(infile) || ferror(infile)) {
  325. DPRINT1 ("%s: got EOF/ERROR before loading MODEL info\n",func);
  326. sprintf(errmsg,
  327. "%s: got EOF/ERROR before loading MODEL info (line %d in %s)\n",
  328. func , LineRead, lookup_fn);
  329. goto BYE;
  330. }
  331. }
  332. /*
  333. * A.7 FOR (successfully read a line from file) DO
  334. * BREAK out of loop if sees Header of next Table
  335. * EXTRACT next DBs's Model info into Model Array;
  336. * DROP line if not enough arguments
  337. * DROP line if Duplicate model defn
  338. * ! format= model_name, model_id;
  339. * STORE in Model Array cell whose index equals Model id
  340. * ENDFOR
  341. */
  342. for (cnt=0; fgets(temp, sizeof(temp), infile)!=NULL; )
  343. {
  344. LineRead++;
  345. for (ptr=temp; *ptr==' '; ptr++) ; if (*ptr == '#') continue;
  346. if (strstr(temp, "to GRIB Geometry Table") != NULL)
  347. break; /* Assume end of section */
  348. if ((num= sscanf (temp, "%s%s%s", achDBsField, strGribCode,dummy)) !=2) {
  349. if (num>0)
  350. fprintf(stdout,"Warning: unmatched Model args, drop line %d in %s\n",
  351. LineRead, lookup_fn);
  352. continue;
  353. }
  354. GribCode = atoi(strGribCode);
  355. if (GribCode < 0 || GribCode >= NMODEL) {
  356. fprintf(stdout,
  357. "Warning: Model_id '%d' out of range, drop line %d in %s",
  358. GribCode, LineRead, lookup_fn); continue; }
  359. if (db_mdl_tbl[GribCode].db_name[0] != '\0') {
  360. fprintf(stdout,
  361. "Warning: duplic Model %d, drop defn ending on line %d in %s\n",
  362. GribCode, LineRead, lookup_fn);
  363. continue;
  364. }
  365. db_mdl_tbl[GribCode].usModel_id = (unsigned short) GribCode;
  366. strcpy (db_mdl_tbl[GribCode].db_name, achDBsField);
  367. ++cnt; /* number loaded */
  368. DPRINT2("(+) Model=%d, db_name=%s\n",
  369. db_mdl_tbl[GribCode].usModel_id, db_mdl_tbl[GribCode].db_name);
  370. }
  371. /*
  372. * DEBUG print
  373. */
  374. DPRINT1("Model table has %d entries\n", cnt);
  375. /*** Database's GEOMETRY TABLE***
  376. Sample:
  377. NEONS to GRIB Geometry Table
  378. NEONS Geometry Name GRIB CODE
  379. =================== =========
  380. mediterranean_109x82 001
  381. persian_gulf_NORAPS_63x63 002
  382. global_144x288 003
  383. *
  384. * *** Database's Geometry Defn conversion info ***
  385. * A.8 KEEP reading until last of Header/Comment line (see '===')
  386. * RETURN error if fails
  387. */
  388. while (strstr (temp, "====") == NULL) {
  389. fgets(temp, sizeof(temp), infile); /* skip Comment/Header lines */
  390. LineRead++;
  391. if (feof(infile) || ferror(infile)) {
  392. DPRINT1 ("%s: got EOF/ERROR before loading DBs GEOM info\n",func);
  393. sprintf(errmsg,
  394. "%s: got EOF/ERROR before loading GEOM info (line %d in %s)\n",
  395. func , LineRead, lookup_fn);
  396. goto BYE;
  397. }
  398. }
  399. /*
  400. * A.9 FOR (successfully read a line from file) DO
  401. * EXTRACT next DBs's Geometry info into Geometry Array;
  402. * IF (fails) SKIP rest of Geometry defn ;
  403. * !format= parm_name, parm_id, scalefctr, offset, dec scale factor;
  404. * STORE in Geom Array cell whose index equals Geom id
  405. * ENDFOR
  406. */
  407. for (cnt=0; fgets(temp, sizeof(temp), infile)!=NULL; ) {
  408. LineRead++;
  409. for (ptr=temp; *ptr==' '; ptr++) ; if (*ptr == '#') continue;
  410. if ((num= sscanf (temp, "%s%s%s", achDBsField, strGribCode, dummy)) !=2) {
  411. if (num>0)
  412. fprintf(stdout,"Warning: unmatched Geom args, drop line %d in %s\n",
  413. LineRead, lookup_fn);
  414. continue;
  415. }
  416. GribCode = atoi(strGribCode);
  417. if (GribCode < 0 || GribCode >= NGEOM) {
  418. fprintf(stdout,
  419. "Warning: Geom_id '%d' out of range, drop line %d in %s\n",
  420. GribCode, LineRead, lookup_fn); continue; }
  421. if (db_geom_tbl[GribCode].db_name[0] != '\0') {
  422. fprintf(stdout,
  423. "Warning: duplic Geom %d, drop defn ending on line %d in %s\n",
  424. GribCode, LineRead, lookup_fn);
  425. continue;
  426. }
  427. db_geom_tbl[GribCode].usGeom_id= (unsigned short) GribCode;
  428. strcpy (db_geom_tbl[GribCode].db_name, achDBsField);
  429. ++cnt;
  430. DPRINT2("(+) geom=%d, db_name=%s\n",
  431. db_geom_tbl[GribCode].usGeom_id, db_geom_tbl[GribCode].db_name);
  432. }
  433. /*
  434. * DEBUG print
  435. */
  436. DPRINT1("Geometry table has %d entries\n", cnt);
  437. /*
  438. *
  439. * A.10 SET status to 0 !success
  440. */
  441. stat=0;
  442. /*
  443. *
  444. * A.11 CLOSE Lookup file;
  445. */
  446. BYE:
  447. if (infile) fclose(infile);
  448. DPRINT2 ("Leaving %s(), stat=%d\n", func,stat);
  449. /*
  450. *
  451. * A.12 RETURN with status
  452. */
  453. return (stat);
  454. /*
  455. * END OF FUNCTION
  456. *
  457. */
  458. }