NCEPLIBS-g2c  1.8.0
aecunpack.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "grib2_int.h"
9 
42 static int
43 aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
44  void *fld, int fld_is_double, int verbose)
45 {
46  g2int *ifld;
47  g2int j, ctemplen, nbits;
48  g2int ccsds_flags, ccsds_block_size, ccsds_rsi;
49  int ret = 0;
50  float ref, bscale, dscale;
51  float *ffld = fld;
52  double *dfld = fld;
53  unsigned char *ctemp;
54 
55  ctemplen = 0;
56 
57  LOG((2, "aecunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
58 
59  rdieee(idrstmpl, &ref, 1);
60  bscale = int_power(2.0, idrstmpl[1]);
61  dscale = int_power(10.0, -idrstmpl[2]);
62  nbits = idrstmpl[3];
63  ccsds_flags = idrstmpl[5];
64  ccsds_block_size = idrstmpl[6];
65  ccsds_rsi = idrstmpl[7];
66 
67  /* If nbits equals 0, we have a constant field where the reference
68  * value is the data value at each gridpoint. */
69  if (nbits != 0)
70  {
71  if (!(ifld = calloc(ndpts, sizeof(g2int))))
72  {
73  if (verbose)
74  fprintf(stderr, "Could not allocate space in aecunpack.\n Data field NOT upacked.\n");
75  return G2C_ENOMEM;
76  }
77 
78  ctemplen = ((nbits + 7)/8) * (size_t) ndpts;
79  if ((ctemp = (unsigned char *) malloc(ctemplen)) == NULL)
80  {
81  if (verbose)
82  fprintf(stderr, "Allocation error.\n");
83  return G2C_ENOMEM;
84  }
85  ret = dec_aec(cpack, len, nbits, ccsds_flags, ccsds_block_size, ccsds_rsi, ctemp, ctemplen);
86  if (ret < 0)
87  return ret;
88  gbits(ctemp, ifld, 0, nbits, 0, ndpts);
89  if (fld_is_double)
90  {
91  for (j = 0; j < ndpts; j++)
92  dfld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
93  }
94  else
95  {
96  for (j = 0; j < ndpts; j++)
97  ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
98  }
99  free(ctemp);
100  free(ifld);
101  }
102  else
103  {
104  if (fld_is_double)
105  {
106  for (j = 0; j < ndpts; j++)
107  dfld[j] = ref;
108  }
109  else
110  {
111  for (j = 0; j < ndpts; j++)
112  ffld[j] = ref;
113  }
114  }
115 
116  return G2C_NOERROR;
117 }
118 
138 g2int
139 aecunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
140  float *fld)
141 {
142  int ret;
143 
144  LOG((2, "g2c_aecunpack len %lld ndpts %lld", len, ndpts));
145 
146  ret = aecunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1);
147 
148  return ret;
149 }
150 
171 int
172 g2c_aecunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
173  float *fld)
174 {
175  g2int idrstmpl8[G2C_AEC_DRS_TEMPLATE_LEN];
176  g2int len8 = len, ndpts8 = ndpts;
177  int i;
178 
179  LOG((2, "g2c_aecunpackf len %d ndpts %lld", len, ndpts));
180 
181  for (i = 0; i < G2C_AEC_DRS_TEMPLATE_LEN; i++)
182  idrstmpl8[i] = idrstmpl[i];
183 
184  return aecunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
185 }
186 
209 int
210 g2c_aecunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
211  double *fld)
212 {
213  g2int idrstmpl8[G2C_AEC_DRS_TEMPLATE_LEN];
214  g2int len8 = len, ndpts8 = ndpts;
215  int i;
216 
217  LOG((2, "g2c_aecunpackd len %lld ndpts %lld", len, ndpts));
218 
219  for (i = 0; i < G2C_AEC_DRS_TEMPLATE_LEN; i++)
220  idrstmpl8[i] = idrstmpl[i];
221 
222  return aecunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
223 }
int g2c_aecunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, double *fld)
Unpack AEC compressed data into an array of doubles, using info from the GRIB2 Data Representation Te...
Definition: aecunpack.c:210
g2int aecunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack AEC compressed data into an array of floats, using info from the GRIB2 Data Representation Tem...
Definition: aecunpack.c:139
static int aecunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double, int verbose)
This internal function will unpack AEC compressed data into an array of floats or doubles,...
Definition: aecunpack.c:43
int g2c_aecunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, float *fld)
Unpack AEC compressed data into an array of floats, using info from the GRIB2 Data Representation Tem...
Definition: aecunpack.c:172
int dec_aec(unsigned char *cpack, g2int len, g2int nbits, g2int flags, g2int block_size, g2int rsi, unsigned char *cfld, g2int cfldlen)
This Function decodes an AEC code stream specified in the CCSDS 121.0-B-3 Blue Book.
Definition: decenc_aec.c:43
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits, g2int nskip, g2int n)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:57
#define G2C_ENOMEM
Out of memory.
Definition: grib2.h:500
#define G2C_AEC_DRS_TEMPLATE_LEN
Length of the idrstmpl array for AEC packing.
Definition: grib2.h:422
int64_t g2int
Long integer type.
Definition: grib2.h:33
#define G2C_NOERROR
No error.
Definition: grib2.h:491
Header file with internal function prototypes NCEPLIBS-g2c library.
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition: int_power.c:18
#define LOG(e)
Ignore logging to stdout.
Definition: grib2_int.h:426
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