NASA Logo
Ocean Color Science Software

ocssw V2022
ncdfbin_utils.c
Go to the documentation of this file.
1 /* =========================================================== */
2 /* Module ncdf_utils.c */
3 /* */
4 /* NCDF4 I/O utilities. */
5 /* */
6 /* Written By: */
7 /* Joel Gales, Futurtech */
8 /* */
9 /* Modification History: */
10 /* Joel Gales, Futuretech, OBPG Project, Nov 2013. */
11 /* Add support for CF-compliant metadata */
12 /* =========================================================== */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <time.h>
19 #include <math.h>
20 #include <netcdf.h>
21 #include <nc4utils.h>
22 #include <ncdfbin_utils.h>
23 
24 #define MAX_PRODS 2048
25 #define FILE_IS_32 0
26 #define FILE_IS_64 1
27 
28 
29 // had to define some evil file level global variables
30 static nc_type binListType = NC_NAT;
31 static int binListDim = -1;
32 static int binListVarid = -1;
33 static int binDataDim = -1;
34 static int numberOfProducts = 0;
35 static int binDataVarid[MAX_PRODS];
36 static int binIndexDim = -1;
37 static int binIndexVarid = -1;
38 static int qualityDim = -1;
39 static int qualityVarid = -1;
40 static int sizeOfFile = -1;
41 
42 int defineBinList_nc(int32_t deflate, int32_t grpid) {
43  // Define BinList DataSet
44  int status;
45 
46  // make sure we do not bounce between 32 and 64 bit bin file
47  if(sizeOfFile == FILE_IS_64) {
48  (void) fprintf(stderr, "line %d of %s: defining 32bit BinList when already in 64bit mode\n", __LINE__, __FILE__);
49  exit(1);
50  }
51  sizeOfFile = FILE_IS_32;
52 
53  if (nc_inq_varid(grpid, "BinList", &binListVarid) == NC_NOERR) {
54  (void) fprintf(stderr, "line %d of %s: BinList is already defined\n", __LINE__, __FILE__);
55  exit(1);
56  }
57 
58  status = nc_def_compound(grpid, sizeof (binListStruct_nc), "binListType", &binListType);
59  check_err(status, __LINE__, __FILE__);
60 
61  status = nc_insert_compound(grpid, binListType, "bin_num",
62  NC_COMPOUND_OFFSET(binListStruct_nc, binnum),
63  NC_UINT);
64  check_err(status, __LINE__, __FILE__);
65 
66  status = nc_insert_compound(grpid, binListType, "nobs",
67  NC_COMPOUND_OFFSET(binListStruct_nc, nobs),
68  NC_SHORT);
69  check_err(status, __LINE__, __FILE__);
70 
71  status = nc_insert_compound(grpid, binListType, "nscenes",
72  NC_COMPOUND_OFFSET(binListStruct_nc, nscenes),
73  NC_SHORT);
74  check_err(status, __LINE__, __FILE__);
75 
76  status = nc_insert_compound(grpid, binListType, "weights",
77  NC_COMPOUND_OFFSET(binListStruct_nc, weights),
78  NC_FLOAT);
79  check_err(status, __LINE__, __FILE__);
80 
81  status = nc_insert_compound(grpid, binListType, "time_rec",
82  NC_COMPOUND_OFFSET(binListStruct_nc, time_rec),
83  NC_FLOAT);
84  check_err(status, __LINE__, __FILE__);
85 
86  status = nc_def_dim(grpid, "binListDim", NC_UNLIMITED, &binListDim);
87  check_err(status, __LINE__, __FILE__);
88 
89  status = nc_def_var(grpid, "BinList", binListType, 1, &binListDim, &binListVarid);
90  check_err(status, __LINE__, __FILE__);
91 
92  /* First set chunking */
93  size_t chunksize = 256;
94  nc_init_compress(grpid, binListVarid, &binListDim, 1, &chunksize, deflate);
95 
96  return 0;
97 }
98 
99 int defineBinList64_nc(int32_t deflate, int32_t grpid) {
100  // Define BinList DataSet
101  int status;
102 
103  // make sure we do not bounce between 32 and 64 bit bin file
104  if(sizeOfFile == FILE_IS_32) {
105  (void) fprintf(stderr, "line %d of %s: defining 64bit BinList when already in 32bit mode\n", __LINE__, __FILE__);
106  exit(1);
107  }
108  sizeOfFile = FILE_IS_64;
109 
110  if (nc_inq_varid(grpid, "BinList", &binListVarid) == NC_NOERR) {
111  (void) fprintf(stderr, "line %d of %s: BinList is already defined\n", __LINE__, __FILE__);
112  exit(1);
113  }
114 
115  //Turns out the size of the structure is 24 even though it's 20 if you count up the bytes
116  //size_t offset = sizeof (binListStruct64_nc);
117  status = nc_def_compound(grpid, sizeof (binListStruct64_nc), "binListType", &binListType);
118  check_err(status, __LINE__, __FILE__);
119 
120  status = nc_insert_compound(grpid, binListType, "bin_num",
121  NC_COMPOUND_OFFSET(binListStruct64_nc, binnum),
122  NC_UINT64);
123  check_err(status, __LINE__, __FILE__);
124 
125  status = nc_insert_compound(grpid, binListType, "nobs",
126  NC_COMPOUND_OFFSET(binListStruct64_nc, nobs),
127  NC_SHORT);
128  check_err(status, __LINE__, __FILE__);
129 
130  status = nc_insert_compound(grpid, binListType, "nscenes",
131  NC_COMPOUND_OFFSET(binListStruct64_nc, nscenes),
132  NC_SHORT);
133  check_err(status, __LINE__, __FILE__);
134 
135  status = nc_insert_compound(grpid, binListType, "weights",
136  NC_COMPOUND_OFFSET(binListStruct64_nc, weights),
137  NC_FLOAT);
138  check_err(status, __LINE__, __FILE__);
139 
140  status = nc_insert_compound(grpid, binListType, "time_rec",
141  NC_COMPOUND_OFFSET(binListStruct64_nc, time_rec),
142  NC_FLOAT);
143  check_err(status, __LINE__, __FILE__);
144 
145  status = nc_def_dim(grpid, "binListDim", NC_UNLIMITED, &binListDim);
146  check_err(status, __LINE__, __FILE__);
147 
148  status = nc_def_var(grpid, "BinList", binListType, 1, &binListDim, &binListVarid);
149  check_err(status, __LINE__, __FILE__);
150 
151  /* First set chunking */
152  size_t chunksize = 256;
153  nc_init_compress(grpid, binListVarid, &binListDim, 1, &chunksize, deflate);
154 
155  return 0;
156 }
157 
158 int writeBinList_nc(int32_t grpid, int32_t nbins_to_write, const void *data) {
159  // Write BinList DataSet
160  int status;
161 
162  static size_t startp = 0;
163  size_t countp;
164 
165  if (binListVarid == -1) {
166  (void) fprintf(stderr, "line %d of %s: BinList variable needs to be defined first\n",
167  __LINE__, __FILE__);
168  exit(1);
169  }
170 
171  countp = nbins_to_write;
172  status = nc_put_vara(grpid, binListVarid, &startp, &countp, data);
173  check_err(status, __LINE__, __FILE__);
174  startp += countp;
175 
176  return 0;
177 }
178 
179 int defineBinData_nc(int32_t deflate, int32_t grpid, int32_t nprods, char** prodnames) {
180  int status = 0;
181 
182  int varid;
183  nc_type binDataType = -1;
184 
185  status = nc_inq_typeid(grpid, "binDataType", &binDataType);
186 
187  if (status != NC_NOERR) {
188  status = nc_def_compound(grpid, 8, "binDataType", &binDataType);
189  check_err(status, __LINE__, __FILE__);
190 
191  status = nc_insert_compound(grpid, binDataType, "sum", 0, NC_FLOAT);
192  check_err(status, __LINE__, __FILE__);
193 
194  status = nc_insert_compound(grpid, binDataType, "sum_squared", 4, NC_FLOAT);
195  check_err(status, __LINE__, __FILE__);
196 
197  status = nc_def_dim(grpid, "binDataDim", NC_UNLIMITED, &binDataDim);
198  check_err(status, __LINE__, __FILE__);
199  }
200 
201  int prod;
202  for (prod = 0; prod < nprods; prod++) {
203  if (nc_inq_varid(grpid, prodnames[prod], &varid) == NC_NOERR) {
204  fprintf(stderr, "line %d of %s: BinData for %s is already defined\n",
205  __LINE__, __FILE__, prodnames[prod]);
206  exit(1);
207  }
208 
209  status = nc_def_var(grpid, prodnames[prod], binDataType, 1, &binDataDim, &varid);
210  if (status != NC_NOERR) {
211  report_err(status, __LINE__, __FILE__);
212  fprintf(stderr, "trying to create binData for product %s\n", prodnames[prod]);
213  exit(1);
214  }
215  binDataVarid[numberOfProducts++] = varid;
216  if (numberOfProducts > MAX_PRODS) {
217  fprintf(stderr, "line %d of %s: Max number of output products exceeded\n",
218  __LINE__, __FILE__);
219  exit(1);
220  }
221 
222  /* First set chunking */
223  size_t chunksize = 256;
224  nc_init_compress(grpid, varid, &binDataDim, 1, &chunksize, deflate);
225 
226  }
227 
228  return 0;
229 }
230 
231 int writeBinData_nc(int32_t grpid, int32_t nbins_to_write, int32_t iprod, const void *data) {
232  int status = 0;
233 
234  static size_t startp[MAX_PRODS];
235  size_t countp;
236 
237  if (iprod >= numberOfProducts) {
238  fprintf(stderr, "line %d of %s: product index %d out of range\n",
239  __LINE__, __FILE__, iprod);
240  exit(1);
241  }
242 
243  countp = nbins_to_write;
244  status = nc_put_vara(grpid, binDataVarid[iprod], &startp[iprod], &countp, data);
245  check_err(status, __LINE__, __FILE__);
246  startp[iprod] += countp;
247 
248  return 0;
249 }
250 
251 int defineBinIndex_nc(int32_t deflate, int32_t grpid) {
252  // Define BinIndex DataSet
253  int status;
254 
255  nc_type binIndexType;
256  int varid;
257 
258  // make sure we do not bounce between 32 and 64 bit bin file
259  if(sizeOfFile == FILE_IS_64) {
260  (void) fprintf(stderr, "line %d of %s: defining 32bit BinIndex when already in 64bit mode\n", __LINE__, __FILE__);
261  exit(1);
262  }
263  sizeOfFile = FILE_IS_32;
264 
265  if (nc_inq_varid(grpid, "BinIndex", &varid) == NC_NOERR) {
266  fprintf(stderr, "line %d of %s: BinIndex is already defined\n",
267  __LINE__, __FILE__);
268  exit(1);
269  }
270 
271  status = nc_def_compound(grpid, sizeof(binIndexStruct_nc), "binIndexType", &binIndexType);
272  check_err(status, __LINE__, __FILE__);
273 
274  status = nc_insert_compound(grpid, binIndexType, "start_num",
275  NC_COMPOUND_OFFSET(binIndexStruct_nc, start_num), NC_UINT);
276  check_err(status, __LINE__, __FILE__);
277 
278  status = nc_insert_compound(grpid, binIndexType, "begin",
279  NC_COMPOUND_OFFSET(binIndexStruct_nc, begin), NC_UINT);
280  check_err(status, __LINE__, __FILE__);
281 
282  status = nc_insert_compound(grpid, binIndexType, "extent",
283  NC_COMPOUND_OFFSET(binIndexStruct_nc, extent), NC_UINT);
284  check_err(status, __LINE__, __FILE__);
285 
286  status = nc_insert_compound(grpid, binIndexType, "max",
287  NC_COMPOUND_OFFSET(binIndexStruct_nc, max), NC_UINT);
288  check_err(status, __LINE__, __FILE__);
289 
290  status = nc_def_dim(grpid, "binIndexDim", NC_UNLIMITED, &binIndexDim);
291  check_err(status, __LINE__, __FILE__);
292 
293  status = nc_def_var(grpid, "BinIndex", binIndexType, 1, &binIndexDim,
294  &binIndexVarid);
295  check_err(status, __LINE__, __FILE__);
296 
297  /* First set chunking */
298  size_t chunksize = 256;
299  nc_init_compress(grpid, binIndexVarid, &binIndexDim, 1, &chunksize, deflate);
300 
301  return 0;
302 }
303 
304 int defineBinIndex64_nc(int32_t deflate, int32_t grpid) {
305  // Define BinIndex DataSet
306  int status;
307 
308  nc_type binIndexType;
309  int varid;
310 
311  // make sure we do not bounce between 32 and 64 bit bin file
312  if(sizeOfFile == FILE_IS_32) {
313  (void) fprintf(stderr, "line %d of %s: defining 64bit BinIndex when already in 32bit mode\n", __LINE__, __FILE__);
314  exit(1);
315  }
316  sizeOfFile = FILE_IS_64;
317 
318  if (nc_inq_varid(grpid, "BinIndex", &varid) == NC_NOERR) {
319  fprintf(stderr, "line %d of %s: BinIndex is already defined\n",
320  __LINE__, __FILE__);
321  exit(1);
322  }
323 
324  status = nc_def_compound(grpid, sizeof(binIndexStruct64_nc), "binIndexType", &binIndexType);
325  check_err(status, __LINE__, __FILE__);
326 
327  status = nc_insert_compound(grpid, binIndexType, "start_num",
328  NC_COMPOUND_OFFSET(binIndexStruct64_nc, start_num), NC_UINT64);
329  check_err(status, __LINE__, __FILE__);
330 
331  status = nc_insert_compound(grpid, binIndexType, "begin",
332  NC_COMPOUND_OFFSET(binIndexStruct64_nc, begin), NC_UINT64);
333  check_err(status, __LINE__, __FILE__);
334 
335  status = nc_insert_compound(grpid, binIndexType, "extent",
336  NC_COMPOUND_OFFSET(binIndexStruct64_nc, extent), NC_UINT);
337  check_err(status, __LINE__, __FILE__);
338 
339  status = nc_insert_compound(grpid, binIndexType, "max",
340  NC_COMPOUND_OFFSET(binIndexStruct64_nc, max), NC_UINT64);
341  check_err(status, __LINE__, __FILE__);
342 
343  status = nc_def_dim(grpid, "binIndexDim", NC_UNLIMITED, &binIndexDim);
344  check_err(status, __LINE__, __FILE__);
345 
346  status = nc_def_var(grpid, "BinIndex", binIndexType, 1, &binIndexDim,
347  &binIndexVarid);
348  check_err(status, __LINE__, __FILE__);
349 
350  /* First set chunking */
351  size_t chunksize = 256;
352  nc_init_compress(grpid, binIndexVarid, &binIndexDim, 1, &chunksize, deflate);
353 
354 
355  return 0;
356 }
357 
358 int writeBinIndex_nc(int32_t grpid, int32_t n_write, const void *data) {
359  // Write BinIndex DataSet
360  int status;
361 
362  static size_t start = 0;
363  size_t count = n_write;
364 
365  status = nc_put_vara(grpid, binIndexVarid, &start, &count, data);
366  check_err(status, __LINE__, __FILE__);
367  start += count;
368 
369  return 0;
370 }
371 
372 int defineQuality_nc(int32_t deflate, int32_t grpid) {
373  // Define Quality DataSet
374  int status;
375  int varid;
376 
377  if (nc_inq_varid(grpid, "qual_l3", &varid) == NC_NOERR) {
378  fprintf(stderr, "line %d of %s: BinIndex is already defined\n",
379  __LINE__, __FILE__);
380  exit(1);
381  }
382 
383  status = nc_def_dim(grpid, "qualityDim", NC_UNLIMITED, &qualityDim);
384  check_err(status, __LINE__, __FILE__);
385 
386  status = nc_def_var(grpid, "qual_l3", NC_BYTE, 1, &qualityDim, &qualityVarid);
387  check_err(status, __LINE__, __FILE__);
388 
389  /* First set chunking */
390  size_t chunksize = 256;
391  nc_init_compress(grpid, qualityVarid, &qualityDim, 1, &chunksize, deflate);
392 
393  return 0;
394 }
395 
396 int writeQuality_nc(int32_t grpid, int32_t nbins_to_write, const void *data) {
397  // Write Quality DataSet
398  int status;
399  static size_t startp;
400  size_t countp;
401 
402  countp = nbins_to_write;
403  status = nc_put_vara(grpid, qualityVarid, &startp, &countp, data);
404  check_err(status, __LINE__, __FILE__);
405  startp += countp;
406 
407  return 0;
408 }
409 
#define FILE_IS_32
Definition: ncdfbin_utils.c:25
int status
Definition: l1_czcs_hdf.c:32
void check_err(const int stat, const int line, const char *file)
Definition: nc4utils.c:35
int defineBinList64_nc(int32_t deflate, int32_t grpid)
Definition: ncdfbin_utils.c:99
int defineBinData_nc(int32_t deflate, int32_t grpid, int32_t nprods, char **prodnames)
void nc_init_compress(int32_t nc_id, int32_t var_id, int32_t *dimids, int32_t rank, size_t *chunksize, int deflate_level)
int writeBinIndex_nc(int32_t grpid, int32_t n_write, const void *data)
int32_t nobs
Definition: atrem_cor.h:93
int writeBinList_nc(int32_t grpid, int32_t nbins_to_write, const void *data)
int defineQuality_nc(int32_t deflate, int32_t grpid)
void report_err(const int stat, const int line, const char *file)
Definition: nc4utils.c:28
#define FILE_IS_64
Definition: ncdfbin_utils.c:26
int defineBinIndex_nc(int32_t deflate, int32_t grpid)
#define MAX_PRODS
Definition: ncdfbin_utils.c:24
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
int writeQuality_nc(int32_t grpid, int32_t nbins_to_write, const void *data)
int writeBinData_nc(int32_t grpid, int32_t nbins_to_write, int32_t iprod, const void *data)
int defineBinList_nc(int32_t deflate, int32_t grpid)
Definition: ncdfbin_utils.c:42
int16_t * nscenes
Definition: l2bin.cpp:80
int defineBinIndex64_nc(int32_t deflate, int32_t grpid)
l2prod max
int count
Definition: decode_rs.h:79