1 : #include <stdio.h>
2 : #include <stdlib.h>
3 : #include "grib2.h"
4 :
5 0 : g2int g2_unpack2(unsigned char *cgrib,g2int *iofst,g2int *lencsec2,unsigned char **csec2)
6 : ////$$$ SUBPROGRAM DOCUMENTATION BLOCK
7 : // . . . .
8 : // SUBPROGRAM: g2_unpack2
9 : // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-10-31
10 : //
11 : // ABSTRACT: This subroutine unpacks Section 2 (Local Use Section)
12 : // as defined in GRIB Edition 2.
13 : //
14 : // PROGRAM HISTORY LOG:
15 : // 2002-10-31 Gilbert
16 : //
17 : // USAGE: int g2_unpack2(unsigned char *cgrib,g2int *iofst,g2int *lencsec2,
18 : // unsigned char **csec2)
19 : // INPUT ARGUMENT LIST:
20 : // cgrib - char array containing Section 2 of the GRIB2 message
21 : // iofst - Bit offset for the beginning of Section 2 in cgrib.
22 : //
23 : // OUTPUT ARGUMENT LIST:
24 : // iofst - Bit offset at the end of Section 2, returned.
25 : // lencsec2 - Length (in octets) of Local Use data
26 : // csec2 - Pointer to a char array containing local use data
27 : //
28 : // RETURN VALUES:
29 : // ierr - Error return code.
30 : // 0 = no error
31 : // 2 = Array passed is not section 2
32 : // 6 = memory allocation error
33 : //
34 : // REMARKS: None
35 : //
36 : // ATTRIBUTES:
37 : // LANGUAGE: C
38 : // MACHINE:
39 : //
40 : //$$$//
41 : {
42 :
43 : g2int ierr,isecnum;
44 : g2int lensec,ipos,j;
45 :
46 0 : ierr=0;
47 0 : *lencsec2=0;
48 0 : *csec2=0; // NULL
49 :
50 0 : gbit(cgrib,&lensec,*iofst,32); // Get Length of Section
51 0 : *iofst=*iofst+32;
52 0 : *lencsec2=lensec-5;
53 0 : gbit(cgrib,&isecnum,*iofst,8); // Get Section Number
54 0 : *iofst=*iofst+8;
55 0 : ipos=(*iofst/8);
56 :
57 0 : if ( isecnum != 2 ) {
58 0 : ierr=2;
59 0 : *lencsec2=0;
60 0 : fprintf(stderr,"g2_unpack2: Not Section 2 data.\n");
61 0 : return(ierr);
62 : }
63 :
64 0 : *csec2=(unsigned char *)malloc(*lencsec2);
65 0 : if (*csec2 == 0) {
66 0 : ierr=6;
67 0 : *lencsec2=0;
68 0 : return(ierr);
69 : }
70 :
71 : //printf(" SAGIPO %d \n",(int)ipos);
72 0 : for (j=0;j<*lencsec2;j++) {
73 0 : *(*csec2+j)=cgrib[ipos+j];
74 : }
75 0 : *iofst=*iofst+(*lencsec2*8);
76 :
77 0 : return(ierr); // End of Section 2 processing
78 :
79 : }
|