NCEPLIBS-g2c  1.7.0
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_int.h"
10 
54 g2int
55 g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl,
56  g2int idrsnum, g2int *idrstmpl, g2int ndpts, float **fld)
57 {
58  g2int isecnum;
59  g2int ipos, lensec;
60  float *lfld;
61 
62  *fld = NULL;
63 
64  gbit(cgrib, &lensec, *iofst, 32); /* Get Length of Section */
65  *iofst = *iofst + 32;
66  gbit(cgrib, &isecnum, *iofst, 8); /* Get Section Number */
67  *iofst = *iofst + 8;
68 
69  if (isecnum != 7)
70  return G2_UNPACK_BAD_SEC;
71 
72  ipos = *iofst / 8;
73  if (!(lfld = calloc(ndpts ? ndpts : 1, sizeof(float))))
74  return G2_UNPACK_NO_MEM;
75 
76  *fld = lfld;
77 
78  if (idrsnum == 0)
79  simunpack(cgrib + ipos, idrstmpl, ndpts, lfld);
80  else if (idrsnum == 2 || idrsnum == 3)
81  {
82  if (comunpack(cgrib+ipos, lensec, idrsnum, idrstmpl, ndpts, lfld))
84  }
85  else if (idrsnum == 50)
86  { /* Spectral Simple */
87  simunpack(cgrib + ipos, idrstmpl, ndpts - 1, lfld + 1);
88  rdieee(idrstmpl + 4, lfld, 1);
89  }
90  else if (idrsnum == 51) /* Spectral complex */
91  {
92  if (igdsnum >= 50 && igdsnum <= 53)
93  specunpack(cgrib + ipos, idrstmpl, ndpts, igdstmpl[0], igdstmpl[2],
94  igdstmpl[2], lfld);
95  else
96  {
97  fprintf(stderr, "g2_unpack7: Cannot use GDT 3.%d to unpack Data Section 5.51.\n",
98  (int)igdsnum);
99  if (lfld)
100  free(lfld);
101  *fld = NULL;
102  return G2_UNPACK7_WRONG_GDT;
103  }
104  }
105 #if defined USE_JPEG2000 || defined USE_OPENJPEG
106  else if (idrsnum == 40 || idrsnum == 40000)
107  {
108  jpcunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, lfld);
109  }
110 #endif /* USE_JPEG2000 */
111 #ifdef USE_PNG
112  else if (idrsnum == 41 || idrsnum == 40010)
113  {
114  pngunpack(cgrib + ipos, lensec - 5, idrstmpl, ndpts, lfld);
115  }
116 #endif /* USE_PNG */
117  else
118  {
119  fprintf(stderr, "g2_unpack7: Data Representation Template 5.%d not yet "
120  "implemented.\n", (int)idrsnum);
121  if (lfld)
122  free(lfld);
123  *fld = NULL;
124  return G2_UNPACK7_BAD_DRT;
125  }
126 
127  *iofst = *iofst + (8 * lensec);
128 
129  return G2_NO_ERROR;
130 }
G2_UNPACK_NO_MEM
#define G2_UNPACK_NO_MEM
Error allocating memory in unpack function.
Definition: grib2.h:302
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition: grib2.h:275
G2_UNPACK7_CORRUPT_SEC
#define G2_UNPACK7_CORRUPT_SEC
In g2_unpack7(), corrupt section 7.
Definition: grib2.h:307
rdieee
void rdieee(g2int *rieee, float *a, g2int num)
This subroutine reads a list of real values in 32-bit IEEE floating point format.
Definition: rdieee.c:21
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
jpcunpack
g2int jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition: jpcunpack.c:108
gbit
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
G2_UNPACK7_BAD_DRT
#define G2_UNPACK7_BAD_DRT
In g2_unpack7(), unrecognized Data Representation Template.
Definition: grib2.h:309
pngunpack
g2int pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition: pngunpack.c:105
simunpack
g2int simunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts, float *fld)
This subroutine unpacks a data field that was packed using a simple packing algorithm as defined in t...
Definition: simunpack.c:26
comunpack
int comunpack(unsigned char *cpack, g2int lensec, g2int idrsnum, g2int *idrstmpl, g2int ndpts, float *fld)
This subroutine unpacks a data field that was packed using a complex packing algorithm as defined in ...
Definition: comunpack.c:42
g2_unpack7
g2int g2_unpack7(unsigned char *cgrib, g2int *iofst, g2int igdsnum, g2int *igdstmpl, g2int idrsnum, g2int *idrstmpl, g2int ndpts, float **fld)
This subroutine unpacks Section 7 (Data Section) as defined in GRIB Edition 2.
Definition: g2_unpack7.c:55
G2_UNPACK_BAD_SEC
#define G2_UNPACK_BAD_SEC
Bad section number in unpacking function.
Definition: grib2.h:301
specunpack
g2int specunpack(unsigned char *cpack, g2int *idrstmpl, g2int ndpts, g2int JJ, g2int KK, g2int MM, float *fld)
This subroutine unpacks a spectral data field that was packed using the complex packing algorithm for...
Definition: specunpack.c:35
G2_UNPACK7_WRONG_GDT
#define G2_UNPACK7_WRONG_GDT
In g2_unpack7(), need one of GDT 3.50 through 3.53 to decode DRT 5.51.
Definition: grib2.h:308