NCEPLIBS-g2c  1.6.4
g2_unpack7.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <memory.h>
8 #include <string.h>
9 #include "grib2.h"
10 
11 g2int simunpack(unsigned char *,g2int *, g2int,g2float *);
12 int comunpack(unsigned char *,g2int,g2int,g2int *,g2int,g2float *);
13 g2int specunpack(unsigned char *,g2int *,g2int,g2int,g2int, g2int, g2float *);
14 #ifdef USE_PNG
15 g2int pngunpack(unsigned char *,g2int,g2int *,g2int, g2float *);
16 #endif /* USE_PNG */
17 #if defined USE_JPEG2000 || defined USE_OPENJPEG
18 g2int jpcunpack(unsigned char *,g2int,g2int *,g2int, g2float *);
19 #endif /* USE_JPEG2000 */
20 
64 g2int
65 g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
66  g2int idrsnum, g2int *idrstmpl, g2int ndpts, g2float **fld)
67 {
68  g2int ierr, isecnum;
69  g2int ipos, lensec;
70  g2float *lfld;
71 
72  ierr = 0;
73  *fld = 0; /*NULL*/
74 
75  gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
76  *iofst = *iofst + 32;
77  gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
78  *iofst = *iofst + 8;
79 
80  if (isecnum != 7)
81  {
82  ierr = 2;
83  /*fprintf(stderr, "g2_unpack7: Not Section 7 data.\n"); */
84  return ierr;
85  }
86 
87  ipos = (*iofst / 8);
88  lfld = calloc(ndpts ? ndpts : 1, sizeof(g2float));
89  if (lfld == 0)
90  {
91  ierr = 6;
92  return ierr;
93  }
94  else
95  {
96  *fld = lfld;
97  }
98 
99  if (idrsnum == 0)
100  simunpack(cgrib + ipos, idrstmpl, ndpts, lfld);
101  else if (idrsnum == 2 || idrsnum == 3)
102  {
103  if (comunpack(cgrib+ipos, lensec, idrsnum, idrstmpl, ndpts, lfld) != 0)
104  {
105  return 7;
106  }
107  }
108  else if (idrsnum == 50)
109  { /* Spectral Simple */
110  simunpack(cgrib + ipos, idrstmpl, ndpts - 1, lfld + 1);
111  rdieee(idrstmpl + 4, lfld, 1);
112  }
113  else if (idrsnum == 51) /* Spectral complex */
114  if (igdsnum>=50 && igdsnum <=53)
115  specunpack(cgrib + ipos, idrstmpl, ndpts, igdstmpl[0], igdstmpl[2],
116  igdstmpl[2], lfld);
117  else
118  {
119  fprintf(stderr, "g2_unpack7: Cannot use GDT 3.%d to unpack Data Section 5.51.\n",
120  (int)igdsnum);
121  ierr = 5;
122  if (lfld)
123  free(lfld);
124  *fld = NULL;
125  return ierr;
126  }
127 #if defined USE_JPEG2000 || defined USE_OPENJPEG
128  else if (idrsnum == 40 || idrsnum == 40000)
129  {
130  jpcunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, lfld);
131  }
132 #endif /* USE_JPEG2000 */
133 #ifdef USE_PNG
134  else if (idrsnum == 41 || idrsnum == 40010)
135  {
136  pngunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, lfld);
137  }
138 #endif /* USE_PNG */
139  else
140  {
141  fprintf(stderr, "g2_unpack7: Data Representation Template 5.%d not yet "
142  "implemented.\n", (int)idrsnum);
143  ierr = 4;
144  if (lfld)
145  free(lfld);
146  *fld = NULL;
147  return ierr;
148  }
149 
150  *iofst = *iofst + (8 * lensec);
151 
152  return ierr; /* End of Section 7 processing */
153 }
g2int simunpack(unsigned char *, g2int *, g2int, g2float *)
This subroutine unpacks a data field that was packed using a simple packing algorithm as defined in t...
Definition: simunpack.c:26
g2int jpcunpack(unsigned char *, g2int, g2int *, g2int, g2float *)
This subroutine unpacks a data field that was packed into a JPEG2000 code stream using info from the ...
Definition: jpcunpack.c:31
g2int pngunpack(unsigned char *, g2int, g2int *, g2int, g2float *)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition: pngunpack.c:30
int comunpack(unsigned char *, g2int, g2int, g2int *, g2int, g2float *)
This subroutine unpacks a data field that was packed using a complex packing algorithm as defined in ...
Definition: comunpack.c:42
g2int g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl, g2int idrsnum, g2int *idrstmpl, g2int ndpts, g2float **fld)
This subroutine unpacks Section 7 (Data Section) as defined in GRIB Edition 2.
Definition: g2_unpack7.c:65
g2int specunpack(unsigned char *, g2int *, g2int, g2int, g2int, g2int, g2float *)
This subroutine unpacks a spectral data field that was packed using the complex packing algorithm for...
Definition: specunpack.c:35
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
Header file for NCEPLIBS-g2c library.
float g2float
Float type.
Definition: grib2.h:22
int64_t g2int
Long integer type.
Definition: grib2.h:20
void rdieee(g2int *rieee, g2float *a, g2int num)
This subroutine reads a list of real values in 32-bit IEEE floating point format.
Definition: rdieee.c:20