OB.DAAC Logo
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 256
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  if (numberOfProducts > MAX_PRODS) {
186  fprintf(stderr, "line %d of %s: Max number of output products exceeded\n",
187  __LINE__, __FILE__);
188  exit(1);
189  }
190 
191  status = nc_inq_typeid(grpid, "binDataType", &binDataType);
192 
193  if (status != NC_NOERR) {
194  status = nc_def_compound(grpid, 8, "binDataType", &binDataType);
195  check_err(status, __LINE__, __FILE__);
196 
197  status = nc_insert_compound(grpid, binDataType, "sum", 0, NC_FLOAT);
198  check_err(status, __LINE__, __FILE__);
199 
200  status = nc_insert_compound(grpid, binDataType, "sum_squared", 4, NC_FLOAT);
201  check_err(status, __LINE__, __FILE__);
202 
203  status = nc_def_dim(grpid, "binDataDim", NC_UNLIMITED, &binDataDim);
204  check_err(status, __LINE__, __FILE__);
205  }
206 
207  int prod;
208  for (prod = 0; prod < nprods; prod++) {
209  if (nc_inq_varid(grpid, prodnames[prod], &varid) == NC_NOERR) {
210  fprintf(stderr, "line %d of %s: BinData for %s is already defined\n",
211  __LINE__, __FILE__, prodnames[prod]);
212  exit(1);
213  }
214 
215  status = nc_def_var(grpid, prodnames[prod], binDataType, 1, &binDataDim, &varid);
216  if (status != NC_NOERR) {
217  report_err(status, __LINE__, __FILE__);
218  fprintf(stderr, "trying to create binData for product %s\n", prodnames[prod]);
219  exit(1);
220  }
221  binDataVarid[numberOfProducts++] = varid;
222 
223  /* First set chunking */
224  size_t chunksize = 256;
225  nc_init_compress(grpid, varid, &binDataDim, 1, &chunksize, deflate);
226 
227  }
228 
229  return 0;
230 }
231 
232 int writeBinData_nc(int32_t grpid, int32_t nbins_to_write, int32_t iprod, const void *data) {
233  int status = 0;
234 
235  static size_t startp[MAX_PRODS];
236  size_t countp;
237 
238  if (iprod >= numberOfProducts) {
239  fprintf(stderr, "line %d of %s: product index %d out of range\n",
240  __LINE__, __FILE__, iprod);
241  exit(1);
242  }
243 
244  countp = nbins_to_write;
245  status = nc_put_vara(grpid, binDataVarid[iprod], &startp[iprod], &countp, data);
246  check_err(status, __LINE__, __FILE__);
247  startp[iprod] += countp;
248 
249  return 0;
250 }
251 
252 int defineBinIndex_nc(int32_t deflate, int32_t grpid) {
253  // Define BinIndex DataSet
254  int status;
255 
256  nc_type binIndexType;
257  int varid;
258 
259  // make sure we do not bounce between 32 and 64 bit bin file
260  if(sizeOfFile == FILE_IS_64) {
261  (void) fprintf(stderr, "line %d of %s: defining 32bit BinIndex when already in 64bit mode\n", __LINE__, __FILE__);
262  exit(1);
263  }
264  sizeOfFile = FILE_IS_32;
265 
266  if (nc_inq_varid(grpid, "BinIndex", &varid) == NC_NOERR) {
267  fprintf(stderr, "line %d of %s: BinIndex is already defined\n",
268  __LINE__, __FILE__);
269  exit(1);
270  }
271 
272  status = nc_def_compound(grpid, sizeof(binIndexStruct_nc), "binIndexType", &binIndexType);
273  check_err(status, __LINE__, __FILE__);
274 
275  status = nc_insert_compound(grpid, binIndexType, "start_num",
276  NC_COMPOUND_OFFSET(binIndexStruct_nc, start_num), NC_UINT);
277  check_err(status, __LINE__, __FILE__);
278 
279  status = nc_insert_compound(grpid, binIndexType, "begin",
280  NC_COMPOUND_OFFSET(binIndexStruct_nc, begin), NC_UINT);
281  check_err(status, __LINE__, __FILE__);
282 
283  status = nc_insert_compound(grpid, binIndexType, "extent",
284  NC_COMPOUND_OFFSET(binIndexStruct_nc, extent), NC_UINT);
285  check_err(status, __LINE__, __FILE__);
286 
287  status = nc_insert_compound(grpid, binIndexType, "max",
288  NC_COMPOUND_OFFSET(binIndexStruct_nc, max), NC_UINT);
289  check_err(status, __LINE__, __FILE__);
290 
291  status = nc_def_dim(grpid, "binIndexDim", NC_UNLIMITED, &binIndexDim);
292  check_err(status, __LINE__, __FILE__);
293 
294  status = nc_def_var(grpid, "BinIndex", binIndexType, 1, &binIndexDim,
295  &binIndexVarid);
296  check_err(status, __LINE__, __FILE__);
297 
298  /* First set chunking */
299  size_t chunksize = 256;
300  nc_init_compress(grpid, binIndexVarid, &binIndexDim, 1, &chunksize, deflate);
301 
302  return 0;
303 }
304 
305 int defineBinIndex64_nc(int32_t deflate, int32_t grpid) {
306  // Define BinIndex DataSet
307  int status;
308 
309  nc_type binIndexType;
310  int varid;
311 
312  // make sure we do not bounce between 32 and 64 bit bin file
313  if(sizeOfFile == FILE_IS_32) {
314  (void) fprintf(stderr, "line %d of %s: defining 64bit BinIndex when already in 32bit mode\n", __LINE__, __FILE__);
315  exit(1);
316  }
317  sizeOfFile = FILE_IS_64;
318 
319  if (nc_inq_varid(grpid, "BinIndex", &varid) == NC_NOERR) {
320  fprintf(stderr, "line %d of %s: BinIndex is already defined\n",
321  __LINE__, __FILE__);
322  exit(1);
323  }
324 
325  status = nc_def_compound(grpid, sizeof(binIndexStruct64_nc), "binIndexType", &binIndexType);
326  check_err(status, __LINE__, __FILE__);
327 
328  status = nc_insert_compound(grpid, binIndexType, "start_num",
329  NC_COMPOUND_OFFSET(binIndexStruct64_nc, start_num), NC_UINT64);
330  check_err(status, __LINE__, __FILE__);
331 
332  status = nc_insert_compound(grpid, binIndexType, "begin",
333  NC_COMPOUND_OFFSET(binIndexStruct64_nc, begin), NC_UINT64);
334  check_err(status, __LINE__, __FILE__);
335 
336  status = nc_insert_compound(grpid, binIndexType, "extent",
337  NC_COMPOUND_OFFSET(binIndexStruct64_nc, extent), NC_UINT);
338  check_err(status, __LINE__, __FILE__);
339 
340  status = nc_insert_compound(grpid, binIndexType, "max",
341  NC_COMPOUND_OFFSET(binIndexStruct64_nc, max), NC_UINT64);
342  check_err(status, __LINE__, __FILE__);
343 
344  status = nc_def_dim(grpid, "binIndexDim", NC_UNLIMITED, &binIndexDim);
345  check_err(status, __LINE__, __FILE__);
346 
347  status = nc_def_var(grpid, "BinIndex", binIndexType, 1, &binIndexDim,
348  &binIndexVarid);
349  check_err(status, __LINE__, __FILE__);
350 
351  /* First set chunking */
352  size_t chunksize = 256;
353  nc_init_compress(grpid, binIndexVarid, &binIndexDim, 1, &chunksize, deflate);
354 
355 
356  return 0;
357 }
358 
359 int writeBinIndex_nc(int32_t grpid, int32_t n_write, const void *data) {
360  // Write BinIndex DataSet
361  int status;
362 
363  static size_t start = 0;
364  size_t count = n_write;
365 
366  status = nc_put_vara(grpid, binIndexVarid, &start, &count, data);
367  check_err(status, __LINE__, __FILE__);
368  start += count;
369 
370  return 0;
371 }
372 
373 int defineQuality_nc(int32_t deflate, int32_t grpid) {
374  // Define Quality DataSet
375  int status;
376  int varid;
377 
378  if (nc_inq_varid(grpid, "qual_l3", &varid) == NC_NOERR) {
379  fprintf(stderr, "line %d of %s: BinIndex is already defined\n",
380  __LINE__, __FILE__);
381  exit(1);
382  }
383 
384  status = nc_def_dim(grpid, "qualityDim", NC_UNLIMITED, &qualityDim);
385  check_err(status, __LINE__, __FILE__);
386 
387  status = nc_def_var(grpid, "qual_l3", NC_BYTE, 1, &qualityDim, &qualityVarid);
388  check_err(status, __LINE__, __FILE__);
389 
390  /* First set chunking */
391  size_t chunksize = 256;
392  nc_init_compress(grpid, qualityVarid, &qualityDim, 1, &chunksize, deflate);
393 
394  return 0;
395 }
396 
397 int writeQuality_nc(int32_t grpid, int32_t nbins_to_write, const void *data) {
398  // Write Quality DataSet
399  int status;
400  static size_t startp;
401  size_t countp;
402 
403  countp = nbins_to_write;
404  status = nc_put_vara(grpid, qualityVarid, &startp, &countp, data);
405  check_err(status, __LINE__, __FILE__);
406  startp += countp;
407 
408  return 0;
409 }
410 
#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:86
int defineBinIndex64_nc(int32_t deflate, int32_t grpid)
l2prod max
int count
Definition: decode_rs.h:79