Update version info for release v4.6.1 (#2122)
[WRF.git] / external / io_grib1 / MEL_grib1 / gribgetbms.c
blobdad610f8d499f82564f107ce339710aa4c9b69d8
1 /* gribgetbms.c June 17, 1996 by Alice Nakajima, SAIC */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "dprints.h" /* for dprints */
6 #include "gribfuncs.h" /* prototypes */
8 /*
10 ************************************************************************
11 * A. FUNCTION: gribgetbms
12 * decode the Bitmap Section from the provided pointer location
13 * and store its info in the internal BMS structure.
14 * Pre-defined Bitmap case is not supported.
16 * INTERFACE:
17 * int gribgetbms ( curr_ptr, bms, gds_flag, ulGrid_size, errmsg)
19 * ARGUMENTS (I=input, O=output, I&O=input and output):
20 * (I) char *curr_ptr;
21 * pointer to location where Bitmap Section to decode is expected;
22 * (O) BMS_INPUT *bms;
23 * pointer to empty BMS structure; will hold decoded BMS info;
24 * (I) int gds_flag;
25 * flag set if GDS is present
26 * (I) unsigned long ulGrid_size;
27 * size of grid as in Binary Data Section struct
28 * (O) char *errmsg
29 * returned filled if error occurred;
31 * RETURN CODE:
32 * 0> BMS info stored in BMS structure if not using pre-defined bitmap;
33 * 1> error, corrupted bms; msg in errmsg;
34 ************************************************************************
37 #if PROTOTYPE_NEEDED
38 int gribgetbms ( char *curr_ptr, BMS_INPUT *bms, int gds_flag,
39 unsigned long ulGrid_size, char *errmsg)
40 #else
41 int gribgetbms (curr_ptr, bms, gds_flag, ulGrid_size, errmsg)
42 char *curr_ptr;
43 BMS_INPUT *bms;
44 int gds_flag;
45 unsigned long ulGrid_size;
46 char *errmsg;
47 #endif
49 char *func= "gribgetbms";
50 char *pp;
51 int totbits,val, bitpos,stopbit; /* tmp working vars */
52 unsigned long SectLength; /* message and section size */
53 unsigned long ulvar; /* tmp var */
54 unsigned long skip=0;
58 * A.0 INIT Status to no error
60 int status=0;
62 DPRINT0 ("Entering gribgetbms():\n");
63 /*
65 * A.1 FUNCTION gbyte !get bitmap length
67 skip=0;
68 gbyte(curr_ptr,(unsigned long *)&SectLength,&skip,24);
69 DPRINT0 ("SectLength\n");
70 bms->uslength= (unsigned long) SectLength;
72 /*
74 * A.2 FUNCTION gbyte !get number of unused bits
76 gbyte(curr_ptr,&ulvar,&skip,8);
77 DPRINT0 ("bms->usUnused_bits\n");
78 bms->usUnused_bits= (unsigned short) ulvar;
80 /*
82 * A.3 FUNCTION gbyte !get bitmap id (non-zero for a pre-defined bitmap)
84 gbyte(curr_ptr,&ulvar,&skip,16);
85 DPRINT0 ("bms->usBMS_id\n");
86 bms->usBMS_id= (unsigned short) ulvar;
90 * A.4 IF (Bitmap follows) !not a predefined bitmap
92 if ( bms->uslength > 6) /* Bitmap follows */
95 /*
96 * A.4.1 CALCULATE Num of bits in bitmap
98 /* = (BMS length)*8 bits - 48 header bits - # of unsused bits */
99 totbits=SectLength*8 - 48 - bms->usUnused_bits;
102 * A.4.2 IF (GDS is present AND
103 * #bits differs from Grid Size) !Corrupted BMS
104 * RETURN 1
105 * ENDIF
107 if (gds_flag && totbits != ulGrid_size) {
108 DPRINT3( "%s: corrupted BMS, gds_flag set but "\
109 "totbits %d != ulgrid_sz %d\n"
110 , func, totbits, ulGrid_size);
112 sprintf(errmsg, "%s: corrupted BMS, gds_flag set but "\
113 "totbits %d != ulgrid_sz %d\n" , func, totbits, ulGrid_size);
114 status= (1); /* Corrupted BMS */
115 goto BYE;
119 * A.4.3 ASSIGN bitmap pointer to 6th byte of BMS
121 bms->bit_map = curr_ptr + 6;
122 pp= bms->bit_map;
123 bms->ulbits_set= 0;
126 * A.4.4 !SUM up total number of bits set
127 * FOR (Each 8-bit block of Total Bits Present in BMS)
129 for ( ; totbits > 0 ; totbits-=8)
132 * A.4.4.1 IF (any of the 8 bits are set)
134 if ((val=(int)*pp++) != 0)
138 * A.4.4.1.1 IF (not within 8 bits of end of bitmap)
139 * SET stopbit to 0
140 * ELSE
141 * SET stopbit to end of bitmap
142 * ENDIF
144 if (totbits > 8) stopbit=0; /* check all 8 bits */
145 else stopbit= 7-totbits+1; /* stop at end of bitmap */
148 * A.4.4.1.2 SUM up number of bits set in this BMS byte
150 for (bitpos= 7; bitpos >= stopbit; bitpos--)
151 if (val >> bitpos & 0x0001) bms->ulbits_set += 1;
153 * A.4.4.1 ENDIF ! any of 8 exists
157 * A.4.4 ENDFOR !each 8-bit loop
161 * A.4 ENDIF !Bitmap follows
165 /* else {
166 / * Predefined Bitmap - not supported!! Could add function here
167 to load a bitmap from local storage * /
168 bms->uslength=6;
169 bms->bit_map= Load_predefined_bms (bms->usBMS_id);
174 BYE:
177 * A.5 DEBUG Print
179 * A.6 RETURN Status
181 DPRINT2 ("Exiting %s, Status=%d;\n", func, status);
182 return (status);
184 * END OF FUNCTION