/src/gdal/frmts/grib/degrib/g2clib/g2_unpack1.c
Line | Count | Source |
1 | | #include <stdio.h> |
2 | | #include <stdlib.h> |
3 | | #include "grib2.h" |
4 | | |
5 | | g2int g2_unpack1(unsigned char *cgrib,g2int *iofst,g2int **ids,g2int *idslen) |
6 | | /*//$$$ SUBPROGRAM DOCUMENTATION BLOCK |
7 | | // . . . . |
8 | | // SUBPROGRAM: g2_unpack1 |
9 | | // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-10-29 |
10 | | // |
11 | | // ABSTRACT: This subroutine unpacks Section 1 (Identification Section) |
12 | | // as defined in GRIB Edition 2. |
13 | | // |
14 | | // PROGRAM HISTORY LOG: |
15 | | // 2002-10-29 Gilbert |
16 | | // |
17 | | // USAGE: int g2_unpack1(unsigned char *cgrib,g2int *iofst,g2int **ids, |
18 | | // g2int *idslen) |
19 | | // INPUT ARGUMENTS: |
20 | | // cgrib - char array containing Section 1 of the GRIB2 message |
21 | | // iofst - Bit offset for the beginning of Section 1 in cgrib. |
22 | | // |
23 | | // OUTPUT ARGUMENTS: |
24 | | // iofst - Bit offset at the end of Section 1, returned. |
25 | | // ids - address of pointer to integer array containing information |
26 | | // read from Section 1, the Identification section. |
27 | | // ids[0] = Identification of originating Centre |
28 | | // ( see Common Code Table C-1 ) |
29 | | // ids[1] = Identification of originating Sub-centre |
30 | | // ids[2] = GRIB Master Tables Version Number |
31 | | // ( see Code Table 1.0 ) |
32 | | // ids[3] = GRIB Local Tables Version Number |
33 | | // ( see Code Table 1.1 ) |
34 | | // ids[4] = Significance of Reference Time (Code Table 1.2) |
35 | | // ids[5] = Year ( 4 digits ) |
36 | | // ids[6] = Month |
37 | | // ids[7] = Day |
38 | | // ids[8] = Hour |
39 | | // ids[9] = Minute |
40 | | // ids[10] = Second |
41 | | // ids[11] = Production status of processed data |
42 | | // ( see Code Table 1.3 ) |
43 | | // ids[12] = Type of processed data ( see Code Table 1.4 ) |
44 | | // idslen - Number of elements in ids[]. |
45 | | // |
46 | | // RETURN VALUES: |
47 | | // ierr - Error return code. |
48 | | // 0 = no error |
49 | | // 2 = Array passed is not section 1 |
50 | | // 6 = memory allocation error |
51 | | // |
52 | | // REMARKS: |
53 | | // |
54 | | // ATTRIBUTES: |
55 | | // LANGUAGE: C |
56 | | // MACHINE: |
57 | | // |
58 | | //$$$ |
59 | | */ |
60 | 15.8k | { |
61 | | |
62 | 15.8k | g2int i,lensec,nbits,ierr,isecnum = 0; |
63 | 15.8k | g2int mapid[13]={2,2,1,1,1,2,1,1,1,1,1,1,1}; |
64 | | |
65 | 15.8k | ierr=0; |
66 | 15.8k | *idslen=13; |
67 | 15.8k | *ids=0; |
68 | | |
69 | 15.8k | gbit(cgrib,&lensec,*iofst,32); // Get Length of Section |
70 | 15.8k | *iofst=*iofst+32; |
71 | 15.8k | gbit(cgrib,&isecnum,*iofst,8); // Get Section Number |
72 | 15.8k | *iofst=*iofst+8; |
73 | | |
74 | 15.8k | if ( isecnum != 1 ) { |
75 | 0 | ierr=2; |
76 | 0 | *idslen=13; |
77 | 0 | fprintf(stderr,"g2_unpack1: Not Section 1 data.\n"); |
78 | 0 | return(ierr); |
79 | 0 | } |
80 | | |
81 | | // |
82 | | // Unpack each value into array ids from the |
83 | | // the appropriate number of octets, which are specified in |
84 | | // corresponding entries in array mapid. |
85 | | // |
86 | 15.8k | *ids=(g2int *)calloc(*idslen,sizeof(g2int)); |
87 | 15.8k | if (*ids == 0) { |
88 | 0 | ierr=6; |
89 | 0 | return(ierr); |
90 | 0 | } |
91 | | |
92 | 221k | for (i=0;i<*idslen;i++) { |
93 | 205k | nbits=mapid[i]*8; |
94 | 205k | gbit(cgrib,*ids+i,*iofst,nbits); |
95 | 205k | *iofst=*iofst+nbits; |
96 | 205k | } |
97 | | |
98 | 15.8k | return(ierr); // End of Section 1 processing |
99 | 15.8k | } |