4 #include "dprints.h" /* debug prints */
5 #include "gribfuncs.h" /* prototypes */
9 *********************************************************************
10 * A. FUNCTION: apply_bitmap
11 * apply the bitmap to the float array. The input float array is
12 * expanded and filled with 'fill_value' in places where data
16 * int apply_bitmap (bms, pgrib_data, fill_value, bds_head, errmsg)
18 * ARGUMENTS (I=input, O=output, I&O=input and output):
20 * pointer to the internal BitMap header structure; bit set means
21 * datapoint is present, bit clear means datapoint is missing.
22 * (I&O) float **pgrib_data;
23 * pointer to Data that was unpacked from BDS's bitstr; Incoming
24 * size is bms->ulbits_set or (ROW*COL - #missingpts) elements;
25 * (I) float fill_value;
26 * float value used for missing datapoints in expanded array;
27 * (O) BDS_HEAD_INPUT *bds_head;
28 * attribute 'ulGrid_size' to be updated;
30 * Empty array that's returned filled if error occurred;
33 * 0> Success; float **pgrib_data probably have been expanded, OR
34 * Predefined bitmap used, no action taken (float array unchanged);
35 * 1> NULL bitmap encountered, errmsg filled;
36 * 2> Error Mallocing space for data array, errmsg filled;
37 * 3> Tried to access more than available in message, errmsg filled;
38 * 4> No bits set in BMS, errmsg filled;
39 **********************************************************************
43 int apply_bitmap ( BMS_INPUT
*bms
, float **pgrib_data
, float fill_value
,
44 BDS_HEAD_INPUT
*bds_head
, char *errmsg
)
47 int apply_bitmap ( bms
, pgrib_data
, fill_value
, bds_head
, errmsg
)
51 BDS_HEAD_INPUT
*bds_head
;
56 char *func
= "apply_bitmap";
57 int i
,j
; /* temp var */
58 int val
; /* temp var */
59 int buf_indx
; /* index for expanded float *buff array */
60 int gribdata_indx
; /* index for float *Grid_data array */
61 int tot_bits_set
; /* must be < expanded size */
62 char *pbms
; /* BIT ptr beg. at BMS data array */
63 float *fbuff
; /* holds expanded float array */
64 int gridsize
; /* expanded size r*c */
70 DPRINT1 ("Enter %s()\n", func
);
74 * A.1 IF (using pre-defined bitmap)
75 * FILL errmsg ! 'case not supported'
79 if (bms
->uslength
== 6) /* References pre-defined bitmap */
81 /* Not currently supported. User can add code inside this IF
82 * to retreive the bitmap from local storage if available.
83 * For now, code prints warning and leaves data array alone */
85 "\n%s Warning: Predefined bitmap encountered! Not supported; " \
86 "Must apply bitmap to data externally.\n", func
);
87 DPRINT1("Leaving %s: Predefined bitmap used, no action taken\n",func
);
93 * A.2 IF (Bitmap pointer is NULL)
94 * FILL errmsg !null pointer
98 if (bms
->bit_map
==NULL
) {
99 DPRINT1 ("Leaving %s: bitmap is Null, no action taken\n", func
);
105 * A.3 IF (count of bits set in BMS is Zero)
107 * RETURN 4 !no bits set
110 if ((tot_bits_set
=bms
->ulbits_set
) == 0) {
111 sprintf(errmsg
,"%s: No bits set in bitmap. No data retrieved!!\n",func
);
112 DPRINT1("Leaving %s: No bits set in bitmap\n",func
);
118 * A.4 CALCULATE grid_size from total number of bits in BMS;
120 /* = (BMS length)*8 bits - 48 header bits - # of unsused bits */
121 gridsize
=(bms
->uslength
)*8 - 48 - bms
->usUnused_bits
;
123 DPRINT2 ("Apply Bitmap: expanding array from [%d] to [%d]; ",
124 tot_bits_set
, gridsize
);
128 * A.5 ALLOCATE storage for expanded array
133 fbuff
= (float *)malloc (gridsize
* sizeof(float));
134 if (fbuff
==(float *)NULL
)
136 sprintf(errmsg
, "%s: Error mallocing %ld bytes\n", func
,gridsize
);
137 DPRINT1 ("Leaving %s, malloc error\n",func
);
143 * A.6 FOR (each point expected)
145 pbms
= bms
->bit_map
; /* pts to char arry bstr of BMS */
146 gribdata_indx
=0; /* index for incoming float arr */
147 for (buf_indx
=0; buf_indx
< gridsize
; ++pbms
) {
150 * A.6.1 GET next byte from Bitmap Section Data
152 val
= (int)*pbms
& 0x0000ff ; /* BMS bitstream */
155 * A.6.2 LOOP, check each Bit of byte read (left to rightmost)
157 for (j
=7; j
>=0 && buf_indx
< gridsize
; j
--) {
159 * A.6.2.1 IF (bit is set) !means datapoint is present
164 * A.6.2.1.1 IF (trying to access more than #elements in Incoming Array)
166 * RETURN 3 ! incoming float array unchanged
169 if (gribdata_indx
== tot_bits_set
) {
171 "%s: accessing more than %d elements in Grib_data[]\n",
173 DPRINT1("Leaving %s, access out of range element\n",func
);
174 return(3); /* incoming Float array is unchanged */
178 * A.6.2.1.2 !still within range
179 * STORE datapoint at correct place in expanded array
181 fbuff
[buf_indx
++]= (*pgrib_data
)[gribdata_indx
++];
184 * ELSE ! means data point is missing
188 * A.6.2.2 STORE Missing Value at correct place in expanded array
190 fbuff
[buf_indx
++]= fill_value
;
200 } /* for every datapt */
202 * A.6 ENDFOR !for every datapoint
207 * A.7 STORE the grid size into caller's argument
209 bds_head
->ulGrid_size
= (unsigned long)gridsize
; /* store new sz */
213 * A.8 FREE old float array
219 * A.9 ASSIGN new expanded array to pointer
221 *pgrib_data
= fbuff
; /* give it addr of expanded arr */
224 * A.10 RETURN 0 !success
226 DPRINT1("Leaving %s, Stat=0", func
);