OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
mscal_struc.c
Go to the documentation of this file.
1 /* ========================================================================
2  * Procedures create, read, and write mscalibration pixel data
3  *
4  * int crosscal_append(char *xcalfile, mscalstr calstr) - creates file, if
5  * it does not exist already, and appends data
6  * int crosscal_read(char *xcalfile, int32_t subsmpl, mscalstr *calstr) -
7  * reads subsampled pixel cross-calibration data and
8  * allocates memory and data to the calstr structure
9  * int crosscal_add(char *xcalfile, int32_t subsmpl, mscalstr *calstr, int32_t *npixs, int32_t *ngranuls)
10  * reads cross-calibration data from an hdf file,
11  * subsamples if needed, and adds pixel and granule
12  * data to an already existing calstr structure
13  *
14  * Programmer Organization Date Description of change
15  * -------------- ------------ -------- ---------------------
16  * Ewa Kwiatkowska SAIC 10 September 2003 Original development
17  * Joel Gales Futuretech 24 October 2012 Add ds_id.fftype
18  * Joel Gales Futuretech 14 June 2013 Add support for NETCDF4 output
19  * Joel Gales Futuretech 31 July 2013 Remove dead code
20  *
21  * ======================================================================== */
22 
23 
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <libgen.h>
33 #include <math.h>
34 
35 #include "l12_proto.h" // Must be first to find netcdf.h
36 #include "passthebuck.h"
37 #include "l2prod_struc.h"
38 #include "filehandle.h"
39 #include "hdf_utils.h"
40 #include "mfhdf.h"
41 #include "mscal_struc.h"
42 
43 static int32_t nbands;
44 
45 //int rdstrideSDS(int32 fileID, char *sdsname, int32 stride[2], VOIDP array_data);
46 int read_glbl_attr(idDS ds_id, char *name, VOIDP ptr);
47 int crosscal_create(char *xcalfile, idDS *ds_id, mscalstr calstr, int32_t npixs);
48 int crosscal_readblocks(char *xcalfile, idDS *ds_id, int32_t *spix, int32_t *totalpixs, mscalstr *calstr);
49 int crosscal_writeblocks(char *xcalfile, idDS ds_id, int32_t *spix, mscalstr calstr, int32_t nfiles);
50 
51 int crosscal_create(char *xcalfile, idDS *ds_id, mscalstr calstr, int32_t npixs) {
52 
53  char title[255];
54  l2prodstr p, *ptr;
55  int j;
56  int dumdim;
57  int32 dm[3];
58  const char dm_name[3][80];
59 
60  sprintf(title, "%s Level-2 cross-calibration pixels", sensorName[calstr.sensorID]);
61 
62  if (strcmp(calstr.oformat, "netCDF4") == 0) {
63  *ds_id = startDS(xcalfile, DS_NCDF, DS_WRITE, 0);
64  if (nc_def_dim((*ds_id).fid, "Number_of_Pixels", npixs, &dumdim)
65  != NC_NOERR) exit(1);
66  } else {
67  *ds_id = startDS(xcalfile, DS_HDF, DS_WRITE, 0);
68  }
69 
70  dm[0] = npixs;
71  strcpy((char *) dm_name[0], "Number of Pixels");
72  /* */
73  /* Create the pixel SDSes */
74  /* ---------------------------------------------------------------- */
75  /* */
76  PTB(SetChrGA(*ds_id, "title", title));
77  PTB(SetI32GA(*ds_id, "sensorID\0", (int32) calstr.sensorID));
78  PTB(SetChrGA(*ds_id, "Input Parameters", calstr.input_parms));
79 
80  PTB(createDS(*ds_id, (int) calstr.sensorID, "fileID", dm, dm_name));
81 
82  PTB(createDS(*ds_id, (int) calstr.sensorID, "year", dm, dm_name));
83  PTB(createDS(*ds_id, (int) calstr.sensorID, "day", dm, dm_name));
84  PTB(createDS(*ds_id, (int) calstr.sensorID, "msec", dm, dm_name));
85  PTB(createDS(*ds_id, (int) calstr.sensorID, "iscan", dm, dm_name));
86 
87  PTB(createDS(*ds_id, (int) calstr.sensorID, "mside", dm, dm_name));
88  PTB(createDS(*ds_id, (int) calstr.sensorID, "detnum", dm, dm_name));
89  PTB(createDS(*ds_id, (int) calstr.sensorID, "pixnum", dm, dm_name));
90 
91  PTB(createDS(*ds_id, (int) calstr.sensorID, "clon", dm, dm_name));
92  PTB(createDS(*ds_id, (int) calstr.sensorID, "clat", dm, dm_name));
93 
94  /* */
95  /* Create the geophysical SDSes for the L2 xcalibration products */
96  /* ---------------------------------------------------------------- */
97  /* */
98  ptr = &p;
99  for (j = 0; j < calstr.nprods; j++) {
100 
101  if (strcmp(calstr.l2prods[j], "l2_flags") == 0);
102  else
103  if (strcmp(calstr.l2prods[j], "mside") == 0);
104  else
105  if (strcmp(calstr.l2prods[j], "detnum") == 0);
106  else
107  if (strcmp(calstr.l2prods[j], "pixnum") == 0);
108  else {
109 
110  if ((ptr = get_l2prod_index((char *) calstr.l2prods[j], (int32) calstr.sensorID,
111  (int32) calstr.nbands, 0, 1, calstr.Lambda)) == NULL) {
112 
113  fprintf(stderr,
114  "-E- %s line %d: product index failure.\n",
115  __FILE__, __LINE__);
116  return (FATAL_ERROR);
117  };
118 
119  PTB(createDS(*ds_id, (int) calstr.sensorID, calstr.l2prods[j], dm, dm_name));
120  }
121  }
122 
123 
124  return (LIFE_IS_GOOD);
125 
126 }
127 
128 int crosscal_append(char *xcalfile, mscalstr calstr) {
129 
130  idDS ds_id_old, ds_id;
131  char command[2 * FILENAME_MAX], tempname[FILENAME_MAX];
132  int exists;
133  int32_t spix_old, spix, total_old, i;
134  mscalstr calstr_old;
135 
136  nbands = NBANDS; /* calstr.nbands; */
137 
138  spix_old = -1;
139  total_old = 0;
140  calstr_old.data = NULL;
141  calstr_old.npixs = calstr.npixs;
142  strcpy(calstr_old.oformat, calstr.oformat);
143  if (crosscal_readblocks(xcalfile, &ds_id_old, &spix_old, &total_old, &calstr_old) != LIFE_IS_GOOD) {
144  printf("-E- %s line %d: Cannot read already existing SDS data %s\n", __FILE__, __LINE__, xcalfile);
145  return (HDF_FUNCTION_ERROR);
146  }
147 
148  if (calstr_old.npixs == 0L) exists = 0;
149  else {
150  exists = 1;
151  if (calstr_old.sensorID != calstr.sensorID) {
152  printf("-E- %s line %d: Attempt to write into a file whose data come from a different sensor %s\n", __FILE__, __LINE__, sensorName[calstr_old.sensorID]);
153  free_calstr(calstr_old, 1);
154  endDS(ds_id_old);
155  return (HDF_FUNCTION_ERROR);
156  }
157  }
158 
159  if (exists) {
160 
161  /*
162  p = (char *)name;
163  strcpy(tempname, xcalfile);
164  p = dirname(tempname);
165  strcpy(tempname, p);
166  strcat(tempname, "/temp.hdf");
167  */
168  strcpy(tempname, xcalfile);
169  strcat(tempname, "_tmp");
170 
171  if (crosscal_create(tempname, &ds_id, calstr, total_old + calstr.npixs) != LIFE_IS_GOOD) {
172  printf("-E- %s line %d: Cannot read already existing SDS data %s\n", __FILE__, __LINE__, tempname);
173  free_calstr(calstr_old, 1);
174  endDS(ds_id);
175  endDS(ds_id_old);
176  return (HDF_FUNCTION_ERROR);
177  }
178 
179  spix = 0L;
180 
181  do {
182 
183  if (crosscal_writeblocks(tempname, ds_id, &spix, calstr_old, 0L) != LIFE_IS_GOOD) {
184  printf("-E- %s line %d: Cannot write existing SDS data into the temp file %s\n", __FILE__, __LINE__, tempname);
185  free_calstr(calstr_old, 1);
186  endDS(ds_id);
187  endDS(ds_id_old);
188  return (HDF_FUNCTION_ERROR);
189  }
190 
191  if (crosscal_readblocks(xcalfile, &ds_id_old, &spix_old, &total_old, &calstr_old) != LIFE_IS_GOOD) {
192  printf("-E- %s line %d: Cannot read already existing SDS data %s\n", __FILE__, __LINE__, xcalfile);
193  free_calstr(calstr_old, 1);
194  endDS(ds_id);
195  return (HDF_FUNCTION_ERROR);
196  }
197 
198  } while (calstr_old.npixs > 0L);
199 
200 
201  for (i = 0; i < calstr.npixs; i++) calstr.fileID[i] += (int16) calstr_old.nfiles;
202 
203  if (crosscal_writeblocks(tempname, ds_id, &spix, calstr, calstr_old.nfiles) != LIFE_IS_GOOD) {
204  printf("-E- %s line %d: Cannot write new SDS data into the temp file %s\n", __FILE__, __LINE__, tempname);
205  endDS(ds_id);
206  return (HDF_FUNCTION_ERROR);
207  }
208 
209  if (endDS(ds_id) == FAIL) {
210  printf("-E- %s line %d: Could not close HDF file, %s.\n", __FILE__, __LINE__, tempname);
211  return (HDF_FUNCTION_ERROR);
212  }
213 
214  sprintf(command, "mv %s %s", tempname, xcalfile);
215  system(command);
216 
217  } else {
218 
219  if (crosscal_create(xcalfile, &ds_id, calstr, calstr.npixs) != LIFE_IS_GOOD) {
220  printf("-E- %s line %d: Could not open HDF file, %s .\n", __FILE__, __LINE__, xcalfile);
221  endDS(ds_id);
222  return (HDF_FUNCTION_ERROR);
223  }
224 
225  spix = 0L;
226 
227  if (crosscal_writeblocks(xcalfile, ds_id, &spix, calstr, 0L) != LIFE_IS_GOOD) {
228  printf("-E- %s line %d: Cannot write new SDS data into the file %s\n", __FILE__, __LINE__, xcalfile);
229  endDS(ds_id);
230  return (HDF_FUNCTION_ERROR);
231  }
232 
233  if (endDS(ds_id) == FAIL) {
234  printf("-E- %s line %d: Could not close HDF file, %s.\n", __FILE__, __LINE__, xcalfile);
235  return (HDF_FUNCTION_ERROR);
236  }
237  }
238 
239 
240  return (LIFE_IS_GOOD);
241 
242 }
243 
244 
245 
246 /* ------------------------------------------------------ */
247 /* crosscal_readblocks() - reads consecutive cross-calibration */
248 /* blocks of data of the size of 400,000 pixels */
249 /* calptr comprises the actual data for the 400,0000 */
250 /* pixels, memory is allocated with the first use */
251 
252 /* ------------------------------------------------------ */
253 
254 int crosscal_readblocks(char *xcalfile, idDS *ds_id, int32_t *spix, int32_t *totalpixs, mscalstr *calstr) {
255 
256  int32 dim_sizes[H4_MAX_VAR_DIMS], sds_id;
257  char name[H4_MAX_NC_NAME];
258  int32_t nfiles = 0, i, length, l;
259  int32 n_datasets, n_file_attr, rank, num_type, attributes;
260  char input_parms[16384];
261  int *varids;
262 
263  if (*spix >= *totalpixs) {
264  if (endDS(*ds_id) == FAIL) {
265  printf("-E- %s line %d: Could not close HDF file, %s .\n", __FILE__, __LINE__, xcalfile);
266  return (HDF_FUNCTION_ERROR);
267  }
268  free_calstr(*calstr, 1);
269  calstr->npixs = 0L;
270  return (LIFE_IS_GOOD);
271  }
272 
273 
274  if (*spix < 0L) {
275 
276  calstr->npixs = 0L;
277  calstr->nfiles = 0L;
278 
279  if (strcmp(calstr->oformat, "netCDF4") == 0) {
280  *ds_id = startDS(xcalfile, DS_NCDF, DS_READ, 0);
281  } else {
282  *ds_id = startDS(xcalfile, DS_HDF, DS_READ, 0);
283  }
284  if ((*ds_id).fid == FAIL) {
285  return (LIFE_IS_GOOD);
286  }
287 
288  if (strcmp(calstr->oformat, "netCDF4") == 0) {
289  nc_inq_varids((*ds_id).fid, &n_datasets, NULL);
290  varids = (int *) calloc(n_datasets, sizeof (int));
291  nc_inq_varids((*ds_id).fid, &n_datasets, varids);
292  } else {
293  SDfileinfo((*ds_id).fid, &n_datasets, &n_file_attr);
294  }
295 
296  strcpy(name, "sensorID\0");
297  if (read_glbl_attr(*ds_id, name, (VOIDP) &(calstr->sensorID)) != LIFE_IS_GOOD) {
298  printf("-E- %s line %d: Could not read HDF sensor attribute, %s .\n", __FILE__, __LINE__, xcalfile);
299  endDS(*ds_id);
300  return (HDF_FUNCTION_ERROR);
301  }
302 
303  strcpy(name, "Input Parameters\0");
304  read_glbl_attr(*ds_id, name, (VOIDP) input_parms);
305  length = strlen(input_parms);
306  if ((calstr->input_parms = (char *) malloc((length + 1) * sizeof (char))) == NULL) {
307  printf("-E- %s line %d: Error allocating memory to MScalmerge input parameter text.\n", __FILE__, __LINE__);
308  endDS(*ds_id);
309  return (HDF_FUNCTION_ERROR);
310  }
311  strncpy(calstr->input_parms, input_parms, length);
312  calstr->input_parms[length] = '\x0';
313 
314  nfiles = 0L;
315  do {
316  sprintf(name, "filename%d", nfiles);
317 
318  if (findAttr(*ds_id, name) == FAIL) break;
319  else ++nfiles;
320 
321  } while (1);
322 
323 
324  strcpy(name, "fileID\0");
325  if (getDimsDS(*ds_id, name, dim_sizes) != NC_NOERR) {
326  printf("-E- %s line %d: Could not read file dimensions, %s .\n", __FILE__, __LINE__, xcalfile);
327  endDS(*ds_id);
328  return (HDF_FUNCTION_ERROR);
329  }
330  if (dim_sizes[0] < 0) {
331  printf("-E- %s line %d: The rank of the requested parameter (%s) is incorrect\n", __FILE__, __LINE__, name);
332  endDS(*ds_id);
333  return (HDF_FUNCTION_ERROR);
334  }
335  *totalpixs = (int32_t) dim_sizes[0];
336 
337  if (nfiles == 0L || *totalpixs == 0L) {
338  printf("-E- %s line %d: There are no data in the file, %s .\n", __FILE__, __LINE__, name);
339  endDS(*ds_id);
340  return (LIFE_IS_GOOD);
341  }
342 
343  calstr->nprods = 1000;
344  if ((calstr->l2prods = (prname *) malloc(calstr->nprods * sizeof (prname))) == NULL) {
345  printf("-E- %s line %d: Error allocating memory to l2 product names.\n", __FILE__, __LINE__);
346  exit(FATAL_ERROR);
347  }
348  calstr->nprods = 0;
349 
350  for (i = 0; i < n_datasets; i++) {
351 
352  if (strcmp(calstr->oformat, "netCDF4") == 0) {
353  nc_inq_varname((*ds_id).fid, varids[i], name);
354  } else {
355  sds_id = SDselect((*ds_id).fid, i);
356  SDgetinfo(sds_id, name, &rank, dim_sizes, &num_type, &attributes);
357  }
358 
359  if (strcmp(name, "fileID") == 0);
360  else
361  if (strcmp(name, "l2_flags") == 0);
362  else
363  if (strcmp(name, "year") == 0);
364  else
365  if (strcmp(name, "day") == 0);
366  else
367  if (strcmp(name, "msec") == 0);
368  else
369  if (strcmp(name, "iscan") == 0);
370  else
371  if (strcmp(name, "mside") == 0);
372  else
373  if (strcmp(name, "detnum") == 0);
374  else
375  if (strcmp(name, "pixnum") == 0);
376  else
377  if (strcmp(name, "lon") == 0);
378  else
379  if (strcmp(name, "lat") == 0);
380  else {
381 
382  strcpy(calstr->l2prods[calstr->nprods], name);
383  calstr->nprods++;
384  }
385  }
386  free(varids);
387 
388  if ((calstr->l2prods = (prname *) realloc((void *) calstr->l2prods, calstr->nprods * sizeof (prname))) == NULL) {
389  printf("-E- %s line %d: Error reallocating memory to l2 product names.\n", __FILE__, __LINE__);
390  return (HDF_FUNCTION_ERROR);
391  }
392  calstr->Lambda = NULL;
393 
394  if (*totalpixs > 1000000)
395  length = alloc_calstr(nfiles, 1000000L, calstr);
396  else
397  length = alloc_calstr(nfiles, *totalpixs, calstr);
398 
399 
400  for (i = 0; i < nfiles; i++) {
401  sprintf(name, "filename%d", i);
402 
403  if (read_glbl_attr(*ds_id, name, (VOIDP) calstr->filenames[i]) != SUCCESS) {
404  printf("-E- %s line %d: Error reading filename attributes (%s)\n", __FILE__, __LINE__, name);
405  endDS(*ds_id);
406  free_calstr(*calstr, 1);
407  calstr->npixs = 0L;
408  calstr->nfiles = 0L;
409  return (HDF_FUNCTION_ERROR);
410  }
411  }
412 
413  *spix = 0L;
414 
415  } else {
416 
417  if (*spix + calstr->npixs > *totalpixs) calstr->npixs = *totalpixs - *spix;
418  }
419  int32 start[3] = {*spix, 0, 0};
420  int32 count[3] = {calstr->npixs, 0, 0};
421  int32 stride[3] = {1, 1, 1};
422 
423  PTB(readDS(*ds_id, "fileID\0", start, stride, count, (VOIDP) calstr->fileID));
424  PTB(readDS(*ds_id, "year\0", start, stride, count, (VOIDP) calstr->year));
425  PTB(readDS(*ds_id, "day\0", start, stride, count, (VOIDP) calstr->day));
426  PTB(readDS(*ds_id, "msec\0", start, stride, count, (VOIDP) calstr->msec));
427  PTB(readDS(*ds_id, "iscan\0", start, stride, count, (VOIDP) calstr->iscan));
428  PTB(readDS(*ds_id, "mside\0", start, stride, count, (VOIDP) calstr->mside));
429  PTB(readDS(*ds_id, "detnum\0", start, stride, count, (VOIDP) calstr->detnum));
430  PTB(readDS(*ds_id, "pixnum\0", start, stride, count, (VOIDP) calstr->pixnum));
431  PTB(readDS(*ds_id, "lon\0", start, stride, count, (VOIDP) calstr->lon));
432  PTB(readDS(*ds_id, "lat\0", start, stride, count, (VOIDP) calstr->lat));
433 
434  l = 0;
435  for (i = 0; i < calstr->nprods; i++) {
436  if (strcmp(calstr->l2prods[i], "l2_flags") == 0);
437  else
438  if (strcmp(calstr->l2prods[i], "mside") == 0);
439  else
440  if (strcmp(calstr->l2prods[i], "detnum") == 0);
441  else
442  if (strcmp(calstr->l2prods[i], "pixnum") == 0);
443  else {
444  PTB(readDS(*ds_id, calstr->l2prods[i], start, stride, count,
445  (VOIDP)&(calstr->ddata[l * calstr->npixs])));
446  ++l;
447  }
448  }
449 
450 
451  *spix += calstr->npixs;
452 
453  return (LIFE_IS_GOOD);
454 
455 }
456 
457 
458 /* ------------------------------------------------------ */
459 /* crosscal_writeblocks() - writes consecutive cross-calibration */
460 /* blocks of data */
461 /* calptr comprises the actual data for the 400,0000 */
462 /* pixels, memory is allocated with the first use */
463 
464 /* ------------------------------------------------------ */
465 
466 int crosscal_writeblocks(char *xcalfile, idDS ds_id, int32_t *spix, mscalstr calstr, int32_t nfiles) {
467 
468  char name[H4_MAX_NC_NAME];
469  int32_t i, j, l;
470 
471  if (nfiles < 0) nfiles = 0L;
472 
473  if (*spix <= 0 || nfiles > 0) {
474  for (i = 0; i < calstr.nfiles; i++) {
475 
476  j = i + nfiles;
477  sprintf(name, "filename%d", j);
478 
479  PTB(SetChrGA(ds_id, name, (char *) calstr.filenames[i]));
480  }
481  }
482 
483  PTB(writeDS(ds_id, "fileID\0", (VOIDP) calstr.fileID, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
484  PTB(writeDS(ds_id, "year\0", (VOIDP) calstr.year, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
485  PTB(writeDS(ds_id, "day\0", (VOIDP) calstr.day, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
486  PTB(writeDS(ds_id, "msec\0", (VOIDP) calstr.msec, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
487  PTB(writeDS(ds_id, "iscan\0", (VOIDP) calstr.iscan, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
488  PTB(writeDS(ds_id, "mside\0", (VOIDP) calstr.mside, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
489  PTB(writeDS(ds_id, "detnum\0", (VOIDP) calstr.detnum, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
490  PTB(writeDS(ds_id, "pixnum\0", (VOIDP) calstr.pixnum, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
491  PTB(writeDS(ds_id, "lon\0", (VOIDP) calstr.lon, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
492  PTB(writeDS(ds_id, "lat\0", (VOIDP) calstr.lat, (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
493 
494  l = 0;
495  for (i = 0; i < calstr.nprods; i++) {
496  if (strcmp(calstr.l2prods[i], "l2_flags\0") == 0);
497  else
498  if (strcmp(calstr.l2prods[i], "mside\0") == 0);
499  else
500  if (strcmp(calstr.l2prods[i], "detnum\0") == 0);
501  else
502  if (strcmp(calstr.l2prods[i], "pixnum\0") == 0);
503  else {
504  PTB(writeDS(ds_id, calstr.l2prods[i], (VOIDP) (calstr.ddata + l * calstr.npixs),
505  (int32) * spix, 0, 0, (int32) calstr.npixs, 1, 1));
506  ++l;
507  }
508  }
509 
510 
511  *spix += calstr.npixs;
512 
513  return (LIFE_IS_GOOD);
514 
515 }
516 
517 int read_glbl_attr(idDS ds_id, char *name, VOIDP ptr) {
518  if (readAttr(ds_id, name, (VOIDP) ptr)) {
519  printf("-E- %s line %d: Could not get global attribute, %s.\n", __FILE__, __LINE__, (name));
520  return (HDF_FUNCTION_ERROR);
521  }
522 
523  return (LIFE_IS_GOOD);
524 }
525 
526 int32_t alloc_calstr(int32_t nfiles, int32_t npixs, mscalstr *calstr) {
527 
528  int32_t len, l;
529  unsigned char *p;
530 
531 
532  l = 5 * sizeof (int16) + sizeof (int32) + 2 * sizeof (uint8) + 2 * sizeof (float);
533  len = nfiles * sizeof (stname) + npixs*l;
534 
535  for (l = 0; l < calstr->nprods; l++) {
536 
537  if (strcmp(calstr->l2prods[l], "l2_flags") == 0);
538  else
539  if (strcmp(calstr->l2prods[l], "mside") == 0);
540  else
541  if (strcmp(calstr->l2prods[l], "detnum") == 0);
542  else
543  if (strcmp(calstr->l2prods[l], "pixnum") == 0);
544  else
545  len += npixs * sizeof (float32);
546  }
547 
548  if ((p = (unsigned char *) malloc(len)) == NULL) {
549  printf("%s -Error: Cannot allocate memory to cross-calibration data\n", __FILE__);
550  exit(FATAL_ERROR);
551  }
552  calstr->nfiles = nfiles;
553  calstr->npixs = npixs;
554  calstr->data = p;
555  calstr->filenames = (stname *) p;
556  p += nfiles * sizeof (stname);
557  calstr->fileID = (int16 *) p;
558  p += npixs * sizeof (int16);
559  calstr->year = (int16 *) p;
560  p += npixs * sizeof (int16);
561  calstr->day = (int16 *) p;
562  p += npixs * sizeof (int16);
563  calstr->msec = (int32 *) p;
564  p += npixs * sizeof (int32);
565  calstr->iscan = (int16 *) p;
566  p += npixs * sizeof (int16);
567  calstr->mside = (uint8 *) p;
568  p += npixs * sizeof (uint8);
569  calstr->detnum = (uint8 *) p;
570  p += npixs * sizeof (uint8);
571  calstr->pixnum = (int16 *) p;
572  p += npixs * sizeof (int16);
573  calstr->lon = (float *) p;
574  p += npixs * sizeof (float);
575  calstr->lat = (float *) p;
576  p += npixs * sizeof (float);
577  calstr->ddata = (float32 *) p;
578 
579  return (len);
580 
581 }
582 
583 void free_calstr(mscalstr calstr, int all) {
584  if (calstr.data != NULL) free(calstr.data);
585  calstr.data = NULL;
586 
587  if (all) {
588  if (calstr.Lambda != NULL) free(calstr.Lambda);
589  calstr.Lambda = NULL;
590  if (calstr.l2prods != NULL) free(calstr.l2prods);
591  calstr.l2prods = NULL;
592  if (calstr.input_parms != NULL) free(calstr.input_parms);
593  calstr.input_parms = NULL;
594  }
595 }
596 
597 
@ DS_READ
Definition: dfutils.h:24
integer, parameter int16
Definition: cubeio.f90:3
#define SUCCESS
Definition: ObpgReadGrid.h:15
int getDimsDS(idDS ds_id, const char sdsname[], int32_t dims[])
int j
Definition: decode_rs.h:73
#define L(lambda, T)
Definition: PreprocessP.h:185
#define FAIL
Definition: ObpgReadGrid.h:18
#define NULL
Definition: decode_rs.h:63
int crosscal_readblocks(char *xcalfile, idDS *ds_id, int32_t *spix, int32_t *totalpixs, mscalstr *calstr)
Definition: mscal_struc.c:254
int read_glbl_attr(idDS ds_id, char *name, VOIDP ptr)
Definition: mscal_struc.c:517
int crosscal_create(char *xcalfile, idDS *ds_id, mscalstr calstr, int32_t npixs)
Definition: mscal_struc.c:51
int32_t alloc_calstr(int32_t nfiles, int32_t npixs, mscalstr *calstr)
Definition: mscal_struc.c:526
void free_calstr(mscalstr calstr, int all)
Definition: mscal_struc.c:583
string command
Definition: color_dtdb.py:196
int createDS(idDS ds_id, int sensorId, const char *sname, int32_t dm[3], const char dm_name[3][80])
Definition: wrapper.c:344
int readAttr(idDS ds_id, const char *nam, void *data)
#define LIFE_IS_GOOD
Definition: passthebuck.h:4
idDS startDS(const char *filename, ds_format_t format, ds_access_t accessmode, int32_t deflate)
Definition: wrapper.c:561
char prname[PRODSTRLEN]
Definition: mscal_struc.h:10
int8_t findAttr(idDS ds_id, const char *nam)
Definition: wrapper.c:49
#define FATAL_ERROR
Definition: swl0_parms.h:5
int writeDS(idDS ds_id, const char *name, const void *data, int32_t s0, int32_t s1, int32_t s2, int32_t e0, int32_t e1, int32_t e2)
Definition: wrapper.c:475
@ DS_NCDF
Definition: dfutils.h:20
#define PTB(function)
Definition: passthebuck.h:16
int readDS(idDS ds_id, const char *name, int32_t *start, int32_t *stride, int32_t *count, void *data)
int SetI32GA(idDS ds_id, const char *name, int32_t value)
Definition: wrapper.c:326
int32_t nbands
@ DS_WRITE
Definition: dfutils.h:25
int SetChrGA(idDS ds_id, const char *name, const char *value)
Definition: wrapper.c:236
int32 spix
Definition: l1_czcs_hdf.c:21
Extra metadata that will be written to the HDF4 file l2prod rank
PARAM_TYPE_NONE Default value No parameter is buried in the product name name_prefix is case insensitive string compared to the product name PARAM_TYPE_VIS_WAVE The visible wavelength bands from the sensor are buried in the product name The product name is compared by appending and name_suffix ie aph_412_giop where prod_ix will be set to PARAM_TYPE_IR_WAVE same search method as PARAM_TYPE_VIS_WAVE except only wavelength above are looped through but prod_ix is still based ie aph_2_giop for the second and prod_ix set to PARAM_TYPE_INT name_prefix is compared with the beginning of the product name If name_suffix is not empty the it must match the end of the product name The characters right after the prefix are read as an integer and prod_ix is set to that number strncpy(l2prod->name_prefix, "myprod", UNITLEN)
@ DS_HDF
Definition: dfutils.h:19
Definition: dfutils.h:28
int endDS(idDS ds_id)
Definition: wrapper.c:634
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
@ NBANDS
Definition: make_L3_v1.1.c:53
#define HDF_FUNCTION_ERROR
Definition: passthebuck.h:7
char stname[SHORTNAME]
Definition: mscal_struc.h:9
float p[MODELMAX]
Definition: atrem_corl1.h:131
int crosscal_writeblocks(char *xcalfile, idDS ds_id, int32_t *spix, mscalstr calstr, int32_t nfiles)
Definition: mscal_struc.c:466
int crosscal_append(char *xcalfile, mscalstr calstr)
Definition: mscal_struc.c:128
a context in which it is NOT documented to do so subscript which cannot be easily calculated when extracting TONS attitude data from the Terra L0 files Corrected several defects in extraction of entrained ephemeris and and as HDF file attributes
Definition: HISTORY.txt:65
int32_t get_l2prod_index(const l2_prod &l2, const char *prodname)
Definition: l2bin.cpp:345
int count
Definition: decode_rs.h:79