1 : #include <stdio.h>
2 : #include <stdlib.h>
3 : #include "grib2.h"
4 :
5 2 : g2int g2_unpack6(unsigned char *cgrib,g2int *iofst,g2int ngpts,g2int *ibmap,
6 : g2int **bmap)
7 : //$$$ SUBPROGRAM DOCUMENTATION BLOCK
8 : // . . . .
9 : // SUBPROGRAM: g2_unpack6
10 : // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-10-31
11 : //
12 : // ABSTRACT: This subroutine unpacks Section 6 (Bit-Map Section)
13 : // as defined in GRIB Edition 2.
14 : //
15 : // PROGRAM HISTORY LOG:
16 : // 2002-10-31 Gilbert
17 : //
18 : // USAGE: int g2_unpack6(unsigned char *cgrib,g2int *iofst,g2int ngpts,
19 : // g2int *ibmap,g2int **bmap)
20 : // INPUT ARGUMENTS:
21 : // cgrib - char array containing Section 6 of the GRIB2 message
22 : // iofst - Bit offset of the beginning of Section 6 in cgrib.
23 : // ngpts - Number of grid points specified in the bit-map
24 : //
25 : // OUTPUT ARGUMENTS:
26 : // iofst - Bit offset at the end of Section 6, returned.
27 : // ibmap - Bitmap indicator ( see Code Table 6.0 )
28 : // 0 = bitmap applies and is included in Section 6.
29 : // 1-253 = Predefined bitmap applies
30 : // 254 = Previously defined bitmap applies to this field
31 : // 255 = Bit map does not apply to this product.
32 : // bmap - Pointer to an integer array containing decoded bitmap.
33 : // ( if ibmap=0 )
34 : //
35 : // RETURN VALUES:
36 : // ierr - Error return code.
37 : // 0 = no error
38 : // 2 = Not Section 6
39 : // 4 = Unrecognized pre-defined bit-map.
40 : // 6 = memory allocation error
41 : //
42 : // REMARKS: None
43 : //
44 : // ATTRIBUTES:
45 : // LANGUAGE: C
46 : // MACHINE:
47 : //
48 : //$$$//
49 : {
50 : g2int j,ierr,isecnum;
51 2 : g2int *lbmap=0;
52 : g2int *intbmap;
53 :
54 2 : ierr=0;
55 2 : *bmap=0; //NULL
56 :
57 2 : *iofst=*iofst+32; // skip Length of Section
58 2 : gbit(cgrib,&isecnum,*iofst,8); // Get Section Number
59 2 : *iofst=*iofst+8;
60 :
61 2 : if ( isecnum != 6 ) {
62 0 : ierr=2;
63 0 : fprintf(stderr,"g2_unpack6: Not Section 6 data.\n");
64 0 : return(ierr);
65 : }
66 :
67 2 : gbit(cgrib,ibmap,*iofst,8); // Get bit-map indicator
68 2 : *iofst=*iofst+8;
69 :
70 2 : if (*ibmap == 0) { // Unpack bitmap
71 0 : if (ngpts > 0) lbmap=(g2int *)calloc(ngpts,sizeof(g2int));
72 0 : if (lbmap == 0) {
73 0 : ierr=6;
74 0 : return(ierr);
75 : }
76 : else {
77 0 : *bmap=lbmap;
78 : }
79 0 : intbmap=(g2int *)calloc(ngpts,sizeof(g2int));
80 0 : gbits(cgrib,intbmap,*iofst,1,0,ngpts);
81 0 : *iofst=*iofst+ngpts;
82 0 : for (j=0;j<ngpts;j++) {
83 0 : lbmap[j]=(g2int)intbmap[j];
84 : }
85 0 : free(intbmap);
86 : // else if (*ibmap.eq.254) ! Use previous bitmap
87 : // return(ierr);
88 : // else if (*ibmap.eq.255) ! No bitmap in message
89 : // bmap(1:ngpts)=.true.
90 : // else {
91 : // print *,'gf_unpack6: Predefined bitmap ',*ibmap,' not recognized.'
92 : // ierr=4;
93 : }
94 :
95 2 : return(ierr); // End of Section 6 processing
96 :
97 : }
|