PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wrfv2_fire/external/io_grib1/MEL_grib1/prt_badmsg.c

http://github.com/jbeezley/wrf-fire
C | 328 lines | 171 code | 30 blank | 127 comment | 25 complexity | 35dc16ab897ab0d1623ccb7a1e16f94f MD5 | raw file
Possible License(s): AGPL-1.0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "gribfuncs.h"
  4. /*
  5. **************************************************************************
  6. * A. FUNCTION: prt_badmsg
  7. * Print out as much information as possible from the GRIB message
  8. * currently in GRIB_HDR structure. This may be an erroneous or
  9. * a partial message.
  10. *
  11. * INTERFACE:
  12. * int prt_badmsg (gh, errmsg)
  13. *
  14. * ARGUMENTS (I=input, O=output, I&O=input and output):
  15. * (I) GRIB_HDR *gh;
  16. * pointer to Grib header structure.
  17. * (O) char *errmsg;
  18. * Empty array, Returned filled if error is found;
  19. *
  20. * RETURN CODE:
  21. * 0> decoded message without any errors;
  22. * 1> error, errmsg buffer filled;
  23. ***********************************************************************
  24. */
  25. #if PROTOTYPE_NEEDED
  26. int prt_badmsg (GRIB_HDR *gh, char *errmsg)
  27. #else
  28. int prt_badmsg (gh, errmsg)
  29. GRIB_HDR *gh; /*input= Grib Header struct */
  30. char *errmsg; /* output= empty unless Error happens */
  31. #endif
  32. {
  33. PDS_INPUT pds; /* local PDS struct */
  34. grid_desc_sec gds; /* local GDS struct */
  35. BDS_HEAD_INPUT bds_head; /* local BDS header struct */
  36. BMS_INPUT bms; /* local bitmap section struct */
  37. float *grib_data; /* local ptr to float data */
  38. PDS_INPUT *Tpds = NULL; /* Null until valid PDS found*/
  39. grid_desc_sec *Tgds = NULL; /* Null until valid GDS found*/
  40. BDS_HEAD_INPUT *Tbds= NULL; /* Null until valid BDS found*/
  41. BMS_INPUT *Tbms = NULL; /* Null until valid BMS found*/
  42. char *func="prt_badmsg"; /* Function name */
  43. char *curr_ptr; /* pts to beginning of GRIB message */
  44. unsigned long lMessageSize; /* message length */
  45. unsigned long edition; /* GRIB edition number */
  46. int bms_flag = 0; /* set if Bms present*/
  47. int gds_flag = 0; /* set if Gds present */
  48. int nReturn = 1; /* status, default is bad */
  49. unsigned long skip;
  50. /*
  51. *
  52. * A.1 CLEAR out local structures
  53. */
  54. fprintf(stdout,"\nEntering %s: getting info from GRIB Header\n", func);
  55. memset ((void *)&pds, '\0', sizeof(PDS_INPUT));
  56. memset ((void *)&gds, '\0', sizeof(grid_desc_sec));
  57. memset ((void *)&bds_head, '\0', sizeof(BDS_HEAD_INPUT));
  58. memset ((void *)&bms, '\0', sizeof(BMS_INPUT));
  59. grib_data = (float *)NULL;
  60. /*
  61. *
  62. * A.2 IF (incoming pointer is not at 'GRIB')
  63. * RETURN 1 !errmsg filled
  64. * ENDIF
  65. */
  66. curr_ptr = (char *)gh->entire_msg;
  67. if(strncmp(curr_ptr,"GRIB",4) != 0) {
  68. sprintf(errmsg,"%s: no 'GRIB' at beg. of this msg. Cannot continue.\n",
  69. func);
  70. goto BYE;
  71. }
  72. fprintf(stdout,"See 'GRIB'\n");
  73. /*
  74. *
  75. * A.3 FUNCTION gbyte !get total message length from IDS
  76. */
  77. skip=32;
  78. gbyte(curr_ptr,&lMessageSize,&skip,24);
  79. if (lMessageSize <= 8) {
  80. sprintf(errmsg,"Message length too short (%ld), cannot continue\n",
  81. lMessageSize);
  82. goto BYE;
  83. }
  84. fprintf(stdout,"Message Length = %ld\n", lMessageSize);
  85. /*
  86. *
  87. * A.4 PRINT warning message if Message length > Buffer size
  88. */
  89. if (lMessageSize > gh->abs_size)
  90. fprintf(stdout,
  91. "*** Messagelen (%ld) > buffersize (%ld), MAY BE CORRUPTED ***\n",
  92. lMessageSize, gh->abs_size);
  93. /*
  94. *
  95. * A.5 EXTRACT the GRIB edition out of Section 0
  96. */
  97. gbyte (curr_ptr,&edition, &skip,8);
  98. fprintf(stdout,"Edition = %ld\n", edition);
  99. /*
  100. *
  101. * A.6 MOVE pointer to the Product Definition section
  102. */
  103. curr_ptr = curr_ptr + 8;
  104. fprintf(stdout,"Expect PDS to start at offset %ld\n",
  105. (long)curr_ptr - (long)gh->entire_msg);
  106. /*
  107. *
  108. * A.7 FUNCTION gribgetpds !decode the PDS
  109. * RETURN error code if fails !errmsg filled
  110. * SAVE pointer to PDS block for printing later
  111. */
  112. if( nReturn= gribgetpds(curr_ptr, &pds, errmsg)) {
  113. fprintf(stdout,"%s: %s;\n", func, errmsg);
  114. goto BYE;
  115. }
  116. fprintf(stdout,"got PDS\n");
  117. Tpds = &pds;
  118. /*
  119. *
  120. * A.8 PRINT warning if PDS length < 28 bytes
  121. */
  122. if (pds.uslength < 28)
  123. fprintf(stdout,"*** PDS (%ld) < 28 bytes, MAY BE CORRUPTED ***\n",
  124. pds.uslength);
  125. /*
  126. *
  127. * A.9 MOVE pointer to the end of PDS
  128. */
  129. curr_ptr += pds.uslength;
  130. if ((long)curr_ptr > (long)gh->entire_msg + (gh->abs_size -1L)) {
  131. fprintf(stdout,"PDS size is much too big, cannot step past it\n");
  132. goto BYE;
  133. }
  134. fprintf(stdout,"Expect next section to start at offset %ld\n",
  135. (long)curr_ptr - (long)gh->entire_msg);
  136. /*
  137. *
  138. * A.10 IF (GDS is present)
  139. */
  140. if ((gds_flag = pds.usGds_bms_id >> 7 & 1))
  141. {
  142. /*
  143. * A.10.1 FUNCTION gribgetgds !Exit on error
  144. * SAVE pointer to GDS block for printing later
  145. */
  146. if ((nReturn=gribgetgds(curr_ptr, &gds, errmsg)) != 0) goto BYE;
  147. fprintf(stdout,"got GDS\n");
  148. Tgds = &gds;
  149. /*
  150. * A.10.2 SET ulGrid_size based on Projection type
  151. */
  152. switch(gds.head.usData_type)
  153. {
  154. case LATLON_PRJ: /* Lat/Lon Grid */
  155. case GAUSS_PRJ: /* Gaussian Latitude/Longitude grid */
  156. case ROT_LATLON_PRJ: /* Rotated Lat/Lon */
  157. case ROT_GAUSS_PRJ: /* Rotated Gaussian */
  158. case STR_LATLON_PRJ: /* Stretched Lat/Lon */
  159. case STR_GAUSS_PRJ : /* Stretched Gaussian */
  160. case STR_ROT_LATLON_PRJ : /* Stretched and Rotated Lat/Lon */
  161. case STR_ROT_GAUSS_PRJ : /* Stretched and Rotated Gaussian */
  162. bds_head.ulGrid_size = gds.llg.usNi * gds.llg.usNj; break;
  163. case MERC_PRJ: /* Mercator Grid */
  164. bds_head.ulGrid_size = gds.merc.cols * gds.merc.rows; break;
  165. case LAMB_PRJ: /* Lambert Conformal */
  166. case ALBERS_PRJ: /* Albers equal-area */
  167. case OBLIQ_LAMB_PRJ: /* Oblique Lambert Conformal */
  168. bds_head.ulGrid_size = gds.lam.iNx * gds.lam.iNy; break;
  169. case POLAR_PRJ: /* Polar Stereographic */
  170. bds_head.ulGrid_size = gds.pol.usNx * gds.pol.usNy; break;
  171. default:
  172. fprintf(stdout,"%s: unsupported usData_type=%d\n",
  173. func, gds.head.usData_type);
  174. sprintf(errmsg,"%s: unsupported usData_type=%d\n",
  175. func, gds.head.usData_type);
  176. nReturn= (1); goto BYE;
  177. }
  178. /*
  179. * A.10.3 PRINT warning if GDS length < 32 bytes
  180. */
  181. if (gds.head.uslength < 32)
  182. fprintf(stdout,"*** GDS (%d bytes) < 32 bytes, MAY BE CORRUPTED ***\n",
  183. gds.head.uslength);
  184. /*
  185. * A.10.4 MOVE the cursor to the next section (either BMS/BDS)
  186. */
  187. curr_ptr += gds.head.uslength;
  188. if ((long)curr_ptr > (long)gh->entire_msg + (gh->abs_size -1L)) {
  189. fprintf(stdout,"GDS size is much too big, cannot step past it\n");
  190. goto BYE;
  191. }
  192. /*
  193. * A.10 ENDIF (GDS is present)
  194. */
  195. } /* gds present */
  196. else fprintf(stdout,"Flag shows NO Grid Defn Sect included\n");
  197. fprintf(stdout,"Expect next section to start at offset %ld\n",
  198. (long)curr_ptr - (long)gh->entire_msg);
  199. bms_flag = pds.usGds_bms_id >> 6 & 1;
  200. /*
  201. *
  202. * A.11 IF (bitmap Section is present)
  203. */
  204. if(bms_flag) /* bit map section present */
  205. {
  206. /*
  207. * A.11.1 FUNCTION gribgetbms !decode BMS
  208. * RETURN error code if fails !errmsg filled
  209. * SAVE pointer to BMS block for printing later
  210. */
  211. if( nReturn=
  212. gribgetbms(curr_ptr,&bms,gds_flag, bds_head.ulGrid_size,errmsg))
  213. {
  214. fprintf(stdout,"%s: error=%d in grib get BMS;\n",func,nReturn);
  215. goto BYE;
  216. }
  217. fprintf(stdout,"got BMS\n");
  218. Tbms = &bms;
  219. /*
  220. * A.11.2 PRINT warning if BMS length < 7 bytes
  221. */
  222. if (bms.uslength < 7)
  223. fprintf(stdout,"*** BMS (%d bytes) < 7 bytes, MAY BE CORRUPTED ***\n",
  224. bms.uslength);
  225. /*
  226. * A.11.3 MOVE the cursor to beginning of Binary Data Section
  227. */
  228. curr_ptr += bms.uslength;
  229. if ((long)curr_ptr > (long)gh->entire_msg + (gh->abs_size -1L)) {
  230. fprintf(stdout,"BMS size is much too big, cannot step past it\n");
  231. goto BYE;
  232. }
  233. /*
  234. * A.11 ENDIF !bms present
  235. */
  236. } /* Bms present */
  237. else fprintf(stdout,"Flag shows NO Bit Map Section included\n");
  238. fprintf(stdout,"Expect BDS to start at offset %ld\n",
  239. (long)curr_ptr - (long)gh->entire_msg);
  240. /*
  241. *
  242. * A.12 FUNCTION gribgetbds()
  243. * RETURN error code if failed !errmsg filled
  244. * SAVE pointer to BDS for printing later
  245. */
  246. if(nReturn=gribgetbds(curr_ptr, pds.sDec_sc_fctr, &bms, &gds, &grib_data,
  247. &bds_head, errmsg))
  248. { fprintf(stdout,"%s: error=%d in grib get BDS;\n",func,nReturn);
  249. goto BYE;
  250. }
  251. fprintf(stdout,"got BDS\n");
  252. Tbds= &bds_head;
  253. /*
  254. * A.13 PRINT warning if BDS < 11 bytes
  255. */
  256. if (bds_head.length < 11)
  257. fprintf(stdout,"*** BDS (%d bytes) < 11 bytes, MAY BE CORRUPTED ***\n",
  258. bds_head.length);
  259. /*
  260. * A.14 BUMP pointer to next section !return on failure
  261. */
  262. curr_ptr += bds_head.length;
  263. if ((long)curr_ptr > (long)gh->entire_msg + (gh->abs_size -1L)) {
  264. fprintf(stdout,"BDS size is much too big, cannot step past it\n");
  265. goto BYE;
  266. }
  267. /*
  268. * A.15 CHECK for '7777' string
  269. * SET return code to 0 if found string
  270. */
  271. fprintf(stdout,"Expect 7777 to start at offset %ld\n",
  272. (long)curr_ptr - (long)gh->entire_msg);
  273. if (strncmp (curr_ptr, "7777", 4))
  274. fprintf(stdout,"'7777' is NOT at expected location\n");
  275. else {
  276. fprintf(stdout,"see '7777' at offset %ld\n",
  277. (long)curr_ptr - (long)gh->entire_msg);
  278. nReturn = 0;
  279. }
  280. BYE:
  281. /*
  282. *
  283. * A.16 FUNCTION prt_inp_struct !print as many sections as possible
  284. */
  285. fprintf(stdout,"\nNow will print all avail sections=\n");
  286. prt_inp_struct (Tpds, Tgds, Tbms, Tbds, &grib_data);
  287. /*
  288. *
  289. * A.17 FREE data array
  290. */
  291. if (grib_data != NULL) free (grib_data);
  292. /*
  293. *
  294. * A.18 RETURN with exit status
  295. */
  296. fprintf(stdout,"Exiting %s\n", func);
  297. return (nReturn);
  298. }