/src/gdal/frmts/grib/degrib/g2clib/g2_unpack6.c
Line | Count | Source |
1 | | #include <stdio.h> |
2 | | #include <stdlib.h> |
3 | | #include "grib2.h" |
4 | | |
5 | | g2int g2_unpack6(unsigned char *cgrib,g2int cgrib_length,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 | 6.44k | { |
50 | 6.44k | g2int j,ierr,isecnum; |
51 | 6.44k | g2int *lbmap=0; |
52 | 6.44k | g2int *intbmap; |
53 | | |
54 | 6.44k | ierr=0; |
55 | 6.44k | *bmap=0; //NULL |
56 | | |
57 | 6.44k | *iofst=*iofst+32; // skip Length of Section |
58 | 6.44k | gbit(cgrib,&isecnum,*iofst,8); // Get Section Number |
59 | 6.44k | *iofst=*iofst+8; |
60 | | |
61 | 6.44k | if ( isecnum != 6 ) { |
62 | 0 | ierr=2; |
63 | 0 | fprintf(stderr,"g2_unpack6: Not Section 6 data.\n"); |
64 | 0 | return(ierr); |
65 | 0 | } |
66 | | |
67 | 6.44k | gbit(cgrib,ibmap,*iofst,8); // Get bit-map indicator |
68 | 6.44k | *iofst=*iofst+8; |
69 | | |
70 | 6.44k | 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 | 0 | } |
76 | 0 | else { |
77 | 0 | *bmap=lbmap; |
78 | 0 | } |
79 | 0 | intbmap=(g2int *)calloc(ngpts,sizeof(g2int)); |
80 | 0 | gbits(cgrib,cgrib_length,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 | 0 | } |
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 | 0 | } |
94 | | |
95 | 6.44k | return(ierr); // End of Section 6 processing |
96 | | |
97 | 6.44k | } |