OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
hdf_utils.c
Go to the documentation of this file.
1 /* =========================================================== */
2 /* Module hdf_utils.c */
3 /* */
4 /* HDF I/O utilities. */
5 /* */
6 /* Written By: */
7 /* Norman Kuring, NASA/GSFC */
8 /* */
9 /* Modification History: */
10 /* B. A. Franz, SAIC GSC, January 1999, */
11 /* Moved general utilities from SWl01 to this module. */
12 /* Eliminated global usage of sds_id. */
13 /* =========================================================== */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <math.h>
20 #include <hdf4utils.h>
21 
22 #include <hdf.h>
23 #include <mfhdf.h>
24 
25 /************************************************************************
26 return the size of the HDF data type in bytes. Return 0 if not found.
27  ************************************************************************/
28 int32_t hdf_sizeof(int32_t dtype) {
29  switch (dtype) {
30  case DFNT_CHAR8:
31  case DFNT_UCHAR8:
32  case DFNT_INT8:
33  case DFNT_UINT8:
34  case DFNT_NINT8:
35  case DFNT_NUINT8:
36  case DFNT_LINT8:
37  case DFNT_LUINT8:
38  return 1;
39  case DFNT_INT16:
40  case DFNT_UINT16:
41  case DFNT_NINT16:
42  case DFNT_NUINT16:
43  case DFNT_LINT16:
44  case DFNT_LUINT16:
45  return 2;
46  case DFNT_INT32:
47  case DFNT_UINT32:
48  case DFNT_FLOAT32:
49  case DFNT_NINT32:
50  case DFNT_NUINT32:
51  case DFNT_NFLOAT32:
52  case DFNT_LINT32:
53  case DFNT_LUINT32:
54  case DFNT_LFLOAT32:
55  return 4;
56  case DFNT_INT64:
57  case DFNT_UINT64:
58  case DFNT_FLOAT64:
59  case DFNT_NINT64:
60  case DFNT_NUINT64:
61  case DFNT_NFLOAT64:
62  case DFNT_LINT64:
63  case DFNT_LUINT64:
64  case DFNT_LFLOAT64:
65  return 8;
66  default:
67  return 0;
68  }
69  return 0;
70 }
71 
72 
73 /* -------------------------------------------------------- */
74 /* Create an SDS using the wrappers for the HDF routines */
75 
76 /* -------------------------------------------------------- */
78  int32_t sd_id,
79  const char *sname, /* short name */
80  const char *lname, /* long name */
81  const char *standard_name, /* NetCDF standard name (not set if passed NULL or "") */
82  const char *units, /* units (not set if passed NULL or "") */
83  double low, /* low end of valid range */
84  double high, /* high end of range (no range set if low >= high) */
85  float slope, /* scale factor (not set if 1) */
86  float offset, /* scaling offset (not set if 0) */
87  int32_t nt, /* HDF number type */
88  int32_t rank, /* number of dimensions (must be <= 3) */
89  int32_t d0, /* size of 1st dimension */
90  int32_t d1, /* size of 2nd dimension */
91  int32_t d2, /* size of 3rd dimension (1 if rank < 2) */
92  const char *dn0, /* name of 1st dimension */
93  const char *dn1, /* name of 2nd dimension (NULL if rank < 2) */
94  const char *dn2 /* name of 3rd dimension (NULL if rank < 3) */
95  ) {
96 
97  int32_t sds_id;
98 
99  if (rank < 3) {
100  d2 = 1;
101  dn2 = NULL;
102  }
103  if (rank < 2) {
104  d1 = 1;
105  dn1 = NULL;
106  }
107 
108  /* Create the SDS */
109  PTB(sd_create(sd_id, sname, nt, rank, d0, d1, d2, &sds_id));
110 
111  /* Name its dimensions */
112  if (d0 != SD_UNLIMITED) {
113  PTB(sd_setdimnames(sds_id, dn0, dn1, dn2));
114  } else if (SDsetblocksize(sds_id, 4194304) == FAIL) {
115  printf("-E- %s line %d: Could not enlarge the HDF block size.\n", __FILE__, __LINE__);
116  return (HDF_FUNCTION_ERROR);
117  }
118 
119  /* Add a "long_name" attribute */
120  PTB(sd_setattr(sds_id, "long_name", DFNT_CHAR, strlen(lname) + 1, lname));
121 
122  /* Add a "valid_range" attribute if one is specified */
123  if (low < high) {
124  switch (nt) { /* Use the appropriate number type */
125  case DFNT_INT8:
126  {
127  int8_t vr[2];
128  vr[0] = (int8_t) low;
129  vr[1] = (int8_t) high;
130  PTB(sd_setattr(sds_id, "valid_range", DFNT_INT8, 2, vr));
131  }
132  break;
133  case DFNT_UINT8:
134  {
135  uint8_t vr[2];
136  vr[0] = (uint8_t) low;
137  vr[1] = (uint8_t) high;
138  PTB(sd_setattr(sds_id, "valid_range", DFNT_UINT8, 2, vr));
139  }
140  break;
141  case DFNT_INT16:
142  {
143  int16 vr[2];
144  vr[0] = (int16) low;
145  vr[1] = (int16) high;
146  PTB(sd_setattr(sds_id, "valid_range", DFNT_INT16, 2, vr));
147  }
148  break;
149  case DFNT_UINT16:
150  {
151  uint16_t vr[2];
152  vr[0] = (uint16_t) low;
153  vr[1] = (uint16_t) high;
154  PTB(sd_setattr(sds_id, "valid_range", DFNT_UINT16, 2, vr));
155  }
156  break;
157  case DFNT_INT32:
158  {
159  int32_t vr[2];
160  vr[0] = (int32_t) low;
161  vr[1] = (int32_t) high;
162  PTB(sd_setattr(sds_id, "valid_range", DFNT_INT32, 2, vr));
163  }
164  break;
165  case DFNT_UINT32:
166  {
167  uint32_t vr[2];
168  vr[0] = (uint32_t) low;
169  vr[1] = (uint32_t) high;
170  PTB(sd_setattr(sds_id, "valid_range", DFNT_UINT32, 2, vr));
171  }
172  break;
173  case DFNT_FLOAT32:
174  {
175  float32 vr[2];
176  vr[0] = (float32) low;
177  vr[1] = (float32) high;
178  PTB(sd_setattr(sds_id, "valid_range", DFNT_FLOAT32, 2, vr));
179  }
180  break;
181  default:
182  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
183  fprintf(stderr, "Got unsupported number type (%d) ", nt);
184  fprintf(stderr, "while trying to create SDS, \"%s\", ", sname);
185  return (PROGRAMMER_BOOBOO);
186  }
187  }
188 
189  /* Add a "slope" attribute if one is specified, and also
190  an intercept attribute */
191  if (slope != 1.0 || offset != 0.0) {
192  PTB(sd_setattr(sds_id, "slope", DFNT_FLOAT, 1, &slope));
193  PTB(sd_setattr(sds_id, "intercept", DFNT_FLOAT, 1, &offset));
194  }
195 
196  /* Add a "units" attribute if one is specified */
197  if (units != NULL && *units != 0) {
198  PTB(sd_setattr(sds_id, "units", DFNT_CHAR, strlen(units) + 1, units));
199  }
200 
201  /* Add a "standard_name" attribute if one is specified */
202  if (standard_name != NULL && *standard_name != 0) {
203  PTB(sd_setattr(sds_id, "standard_name", DFNT_CHAR, strlen(standard_name) + 1, standard_name));
204  }
205 
206  /* Release this SDS */
207  PTB(sd_endaccess(sds_id));
208 
209  return (LIFE_IS_GOOD);
210 }
211 
212 /************************************************************************
213 The following functions are just wrappers for the corresponding HDF
214 functions.
215  ************************************************************************/
216 int sd_setattr(int32_t id, const char *nam, int32_t typ, int32_t cnt, const void* data) {
217  if (SDsetattr(id, nam, typ, cnt, data)) {
218 
219  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
220  fprintf(stderr, "SDsetattr(%d,\"%s\",%d,%d,data) failed. ",
221  id, nam, typ, cnt);
222  return (HDF_FUNCTION_ERROR);
223  }
224  return (LIFE_IS_GOOD);
225 }
226 
228  int32_t id,
229  const char *nam,
230  int32_t typ,
231  int32_t rank,
232  int32_t d0,
233  int32_t d1,
234  int32_t d2,
235  int32_t *sds_id
236  ) {
237  int32_t dimsizes[3];
238  if (rank > 3) {
239  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
240  fprintf(stderr, "sd_create() expects to be passed a rank <= 3. ");
241  return (PROGRAMMER_BOOBOO);
242  }
243  dimsizes[0] = d0;
244  dimsizes[1] = d1;
245  dimsizes[2] = d2;
246  if ((*sds_id = SDcreate(id, nam, typ, rank, dimsizes)) == FAIL) {
247  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
248  fprintf(stderr, "SDcreate(%d,\"%s\",%d,%d,[%d,%d,%d]) failed. ",
249  id, nam, typ, rank, d0, d1, d2);
250  return (HDF_FUNCTION_ERROR);
251  }
252  return (LIFE_IS_GOOD);
253 }
254 
255 int sd_endaccess(int32_t id) {
256  if (SDendaccess(id)) {
257  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
258  fprintf(stderr, "SDendaccess(%d) failed. ", id);
259  return (HDF_FUNCTION_ERROR);
260  }
261  return (LIFE_IS_GOOD);
262 }
263 
264 int sd_setdimnames(int32_t id, const char *d0, const char *d1, const char *d2) {
265  PTB(sd_setdimname(id, 0, d0));
266  if (d1 != NULL && *d1 != 0) {
267  PTB(sd_setdimname(id, 1, d1));
268  }
269  if (d2 != NULL && *d2 != 0) {
270  PTB(sd_setdimname(id, 2, d2));
271  }
272  return (LIFE_IS_GOOD);
273 }
274 
275 int sd_setdimname(int32_t sds_id, int32_t dim_number, const char *name) {
276  int32_t dim_id;
277  dim_id = SDgetdimid(sds_id, dim_number);
278  if (dim_id == FAIL) {
279  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
280  fprintf(stderr, "SDgetdimid(%d,%d) failed.\n",
281  sds_id, dim_number);
282  return (HDF_FUNCTION_ERROR);
283  }
284  if (SDsetdimname(dim_id, name)) {
285  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
286  fprintf(stderr, "SDsetdimname(%d,\"%s\") failed.\n",
287  dim_id, name);
288  return (HDF_FUNCTION_ERROR);
289  }
290  return (LIFE_IS_GOOD);
291 }
292 
294  int32_t sd_id,
295  const char *name,
296  const void* data,
297  int32_t s0,
298  int32_t s1,
299  int32_t s2,
300  int32_t e0,
301  int32_t e1,
302  int32_t e2
303  ) {
304  int32_t sds_id, start[3], edge[3];
305 
306  PTB(sd_select(sd_id, name, &sds_id));
307  start[0] = s0;
308  edge[0] = e0;
309  start[1] = s1;
310  edge[1] = e1;
311  start[2] = s2;
312  edge[2] = e2;
313  if (SDwritedata(sds_id, start, NULL, edge, (void*)data) == FAIL) {
314  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
315  fprintf(stderr, "SDwritedata(%d,[%d,%d,%d],NULL,[%d,%d,%d],0x%p) ",
316  sds_id, s0, s1, s2, e0, e1, e2, data);
317  fprintf(stderr, "failed.\n");
318  return (HDF_FUNCTION_ERROR);
319  }
320  PTB(sd_endaccess(sds_id));
321  return (LIFE_IS_GOOD);
322 }
323 
325  int32_t sd_id,
326  const char *name,
327  VOIDP data,
328  int32_t s0,
329  int32_t s1,
330  int32_t s2,
331  int32_t e0,
332  int32_t e1,
333  int32_t e2
334  ) {
335  int32_t sds_id, start[3], edge[3];
336 
337  PTB(sd_select(sd_id, name, &sds_id));
338  start[0] = s0;
339  edge[0] = e0;
340  start[1] = s1;
341  edge[1] = e1;
342  start[2] = s2;
343  edge[2] = e2;
344  if (SDreaddata(sds_id, start, NULL, edge, data) == FAIL) {
345  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
346  fprintf(stderr, "SDreaddata(%d,[%d,%d,%d],NULL,[%d,%d,%d],0x%p) ",
347  sds_id, s0, s1, s2, e0, e1, e2, data);
348  fprintf(stderr, "failed.\n");
349  return (HDF_FUNCTION_ERROR);
350  }
351  PTB(sd_endaccess(sds_id));
352  return (LIFE_IS_GOOD);
353 }
354 
355 int sd_select(int32_t sd_id, const char *name, int32_t *sds_id) {
356 
357  int32_t index;
358 
359  index = SDnametoindex(sd_id, name);
360  if (index == FAIL) {
361  /*
362  fprintf(stderr,"-E- %s line %d: ",__FILE__,__LINE__);
363  fprintf(stderr,"SDnametoindex(%d,\"%s\") failed.\n",
364  sd_id, name);
365  */
366  return (HDF_FUNCTION_ERROR);
367  }
368  *sds_id = SDselect(sd_id, index);
369  if (*sds_id == FAIL) {
370  fprintf(stderr, "-E- %s line %d: ", __FILE__, __LINE__);
371  fprintf(stderr, "SDselect(%d,%d) failed.\n",
372  sd_id, index);
373  return (HDF_FUNCTION_ERROR);
374  }
375  return (LIFE_IS_GOOD);
376 }
377 
378 /****************************************************************************
379 Add the named SDS to the Vgroup specified by its Vgroup identifier.
380 This function uses the global variable sd_id which is set elsewhere
381 by a call to SDstart().
382  *****************************************************************************/
383 int AddSdsToVgroup(int32_t sd_id, int32_t v_id, const char *name) {
384 
385  int32_t sds_ref, sds_id;
386 
387  PTB(sd_select(sd_id, name, &sds_id));
388  sds_ref = SDidtoref(sds_id);
389  if (sds_ref == FAIL) {
390  fprintf(stderr,
391  "-E- %s line %d: SDidtoref(%d) failed.\n",
392  __FILE__, __LINE__, sds_id);
393  return (HDF_FUNCTION_ERROR);
394  }
395  if (Vaddtagref(v_id, DFTAG_NDG, sds_ref) == FAIL) {
396  fprintf(stderr,
397  "-E- %s line %d: Vaddtagref(%d,%d,%d) failed.\n",
398  __FILE__, __LINE__, v_id, DFTAG_SD, sds_ref);
399  return (HDF_FUNCTION_ERROR);
400  }
401  return (LIFE_IS_GOOD);
402 }
403 
404 int v_attach(int32_t h_id, int32_t *v_id) {
405  *v_id = Vattach(h_id, -1, "w");
406  if (*v_id == FAIL) {
407  fprintf(stderr,
408  "-E- %s line %d: Vattach(%d,-1,\"w\") failed.\n",
409  __FILE__, __LINE__, h_id);
410  return (HDF_FUNCTION_ERROR);
411  }
412  return (LIFE_IS_GOOD);
413 }
414 
415 
416 /* ------------------------------------------------------ */
417 /* rdSDS() - reads a SDS (scientific data set), and */
418 /* returns the data */
419 /* */
420 
421 /* ------------------------------------------------------ */
422 int rdSDS(
423  int32_t fileID,
424  const char *sdsname,
425  int32_t start1, /* 1st dimension of starting point */
426  int32_t start2, /* 2nd dimension of starting point */
427  int32_t edges1, /* 1st dim of length of subset( 0 if reading entire SDS) */
428  int32_t edges2, /* 2nd dim of length of subset */
429  VOIDP array_data
430  ) {
431  int32_t sds_id, numtype;
432  int32_t sds_index, rank, dims[H4_MAX_VAR_DIMS], nattrs;
433  int32_t start[2], edges[2];
434  char tmp_sdsname[H4_MAX_NC_NAME];
435 
436  start[0] = 0;
437  start[1] = 0;
438  edges[0] = 0;
439  edges[1] = 0;
440 
441  /* Get the SDS index */
442  sds_index = SDnametoindex(fileID, sdsname);
443 
444  if (sds_index < 0) {
445  printf("-E- %s: SDS \"%s\" not found.\n", "rdSDS", sdsname);
446  SDend(fileID);
447  exit(1);
448  }
449 
450 
451  /* Select the SDS */
452  sds_id = SDselect(fileID, sds_index);
453 
454  /* Get the rank and number type of the array */
455  SDgetinfo(sds_id, tmp_sdsname, &rank, dims, &numtype, &nattrs);
456 
457  /*
458  if (strcmp(sdsname,"pxl") == 0) {
459  ncol = dims[0];
460  }
461  */
462 
463  /* Define the location, pattern and size of the data to read */
464  /* set 1st dimension */
465  start[0] = start1;
466  if (edges1 == 0) {
467  edges[0] = dims[0];
468  } else {
469  edges[0] = edges1;
470  }
471  /* if rank > 1, set 2nd dimension */
472  if (rank > 1) {
473  start[1] = start2;
474  if (edges2 == 0) {
475  edges[1] = dims[1];
476  } else {
477  edges[1] = edges2;
478  }
479  }
480 
481  /* Based on number type, call the corresponding wrapper
482  for the HDF SDreaddata function */
483  SDreaddata(sds_id, start, NULL, edges, array_data);
484 
485  /* Terminate access to the array */
486  SDendaccess(sds_id);
487 
488  return (0);
489 }
490 
491 /* ------------------------------------------------------ */
492 /* getDims() - gets a SDS (scientific data set), and */
493 /* returns the dimensions of the data */
494 /* */
495 
496 /* ------------------------------------------------------ */
498  int32_t fileID,
499  const char sdsname[H4_MAX_NC_NAME],
500  int32_t dims[H4_MAX_VAR_DIMS]
501  ) {
502  int32_t sds_id, numtype;
503  int32_t sds_index, rank, nattrs;
504  char tmp_sdsname[H4_MAX_NC_NAME];
505 
506 
507  /* Get the SDS index */
508  sds_index = SDnametoindex(fileID, sdsname);
509 
510  /* Check that the SDS exists */
511  if (sds_index == -1) {
512  printf("-E- %s: Error seeking SDS\n", __FILE__);
513  return (1);
514  }
515 
516  /* Select the SDS */
517  sds_id = SDselect(fileID, sds_index);
518 
519  /* Get the rank and number type of the array */
520  SDgetinfo(sds_id, tmp_sdsname, &rank, dims, &numtype, &nattrs);
521 
522  /* Terminate access to the array */
523  SDendaccess(sds_id);
524 
525  return (0);
526 }
527 
528 
529 /* ------------------------------------------------------ */
530 /* get_type() - gets a SDS (scientific data set), and */
531 /* returns the data type */
532 /* */
533 
534 /* ------------------------------------------------------ */
536  int32_t fileID,
537  const char sdsname[H4_MAX_NC_NAME],
538  int32_t *dtype
539  ) {
540  int32_t sds_id;
541  int32_t sds_index, rank, nattrs;
542  char tmp_sdsname[H4_MAX_NC_NAME];
543  int32_t dims[H4_MAX_VAR_DIMS];
544 
545  /* Get the SDS index */
546  sds_index = SDnametoindex(fileID, sdsname);
547 
548  /* Check that the SDS exists */
549  if (sds_index == -1) {
550  printf("-E- %s: Error seeking SDS\n", __FILE__);
551  return (1);
552  }
553 
554  /* Select the SDS */
555  sds_id = SDselect(fileID, sds_index);
556 
557  /* Get the rank and number type of the array */
558  SDgetinfo(sds_id, tmp_sdsname, &rank, dims, dtype, &nattrs);
559 
560  /* Terminate access to the array */
561  SDendaccess(sds_id);
562 
563  return (0);
564 }
565 
566 
567 /* ------------------------------------------------------ */
568 /* getHDFattr() - gets an HDF SDS attribute or */
569 /* file attribute (if sdsname is "") */
570 /* */
571 
572 /* ------------------------------------------------------ */
574  int32_t fileID,
575  const char *attrname,
576  const char *sdsname,
577  VOIDP data
578  ) {
579  int32_t id, attr_index, sds_index;
580  int32_t data_type, count;
581  char tmp_attrname[H4_MAX_NC_NAME];
582 
583  /* get the SDS identifier for SDS attributes */
584  if (strcmp(sdsname, "") != 0) {
585  /* get the # of the SDS from the SDS name */
586  sds_index = SDnametoindex(fileID, sdsname);
587 
588  /* Check that the SDS exists */
589  if (sds_index == -1) {
590  printf("-E- %s: Error seeking SDS\n", __FILE__);
591  return (1);
592  }
593 
594  id = SDselect(fileID, sds_index);
595  } else {
596  /* identifier = fileID for file (global) attributes */
597  id = fileID;
598  }
599 
600  /* get the attribute index */
601  attr_index = SDfindattr(id, attrname);
602  if (attr_index == -1) {
603  return (-1);
604  }
605 
606  /* get the information about the file attribute */
607  SDattrinfo(id, attr_index, tmp_attrname,
608  &data_type, &count);
609 
610  /* read the attribute */
611  SDreadattr(id, attr_index, data);
612 
613  /* Terminate access to the SDS */
614  if (strcmp(sdsname, "") != 0) {
615  SDendaccess(id);
616  }
617  return (0);
618 }
619 
620 /*-----------------------------------------------------------------------------
621  Function: rdvdata
622 
623  Returns: intn (status)
624 
625  Description:
626  The function rdvdata reads the requested vdata into the given buffer
627  and returns the status.
628 
629  Arguments: (in calling order)
630  Type Name I/O Description
631  ---- ---- --- -----------
632  int32 vskey I ID number of the vdata
633  char * fields I field names of the data to read
634  int32 start I start element position
635  int32 nelt I number of elements to read
636  uchar * databuf O buffer to read the data
637 
638  Notes:
639 
640  Modification history:
641  Programmer Organization Date Description of change
642  -------------- ------------ -------- ---------------------
643  Lakshmi Kumar Hughes STX 03/11/94 Original development
644 
645 ------------------------------------------------------------------------------*/
646 intn rdvdata(int32 vskey, const char *fields, int32 start, int32 nelt,
647  unsigned char *databuf) {
648  int32 ret;
649 
650  if ((ret = VSsetfields(vskey, fields)) < 0)
651  return FAIL;
652 
653  if ((ret = VSseek(vskey, start)) < 0)
654  return FAIL;
655 
656  if ((ret = VSread(vskey, databuf, nelt, FULL_INTERLACE)) < 0)
657  return FAIL;
658 
659  return ret;
660 
661 }
662 
663 /*-----------------------------------------------------------------------------
664  Function: attach_vdata
665 
666  Returns: intn (Status)
667 
668  Description:
669  The function attach_vdata attaches to the requested vdata
670 
671  Arguments: (in calling order)
672  Type Name I/O Description
673  ---- ---- --- -----------
674  int32 fid I HDF file ID
675  char * sname I vdata name
676 
677  Notes:
678 
679  Modification history:
680  Programmer Organization Date Description of change
681  -------------- ------------ -------- ---------------------
682  Lakshmi Kumar Hughes STX 03/11/94 Original development
683 
684 
685 ------------------------------------------------------------------------------*/
686 intn attach_vdata(int32 fid, const char *sname) {
687  int32 vsid, vskey;
688 
689  vsid = VSfind(fid, sname);
690  if ((vskey = VSattach(fid, vsid, "r")) < 0)
691  return FAIL;
692  return vskey;
693 }
694 
695 /*-----------------------------------------------------------------------------
696  Function: read_SDS
697 
698  Returns: intn (status)
699 
700  Description:
701  The function read_SDS reads the requested SDS/NDG into the given
702  buffer and returns the status.
703 
704  Arguments: (in calling order)
705  Type Name I/O Description
706  ---- ---- --- -----------
707  int32 sdfid I ID number
708  char * sds_name I SDS name
709  void * buffer O data buffer
710 
711  Notes:
712 
713  Modification history:
714  Programmer Organization Date Description of change
715  -------------- ------------ -------- ---------------------
716  Lakshmi Kumar Hughes STX 06/07/94 Original development
717 
718 ------------------------------------------------------------------------------*/
719 int32 read_SDS(int32 sdfid, const char *sds_name, void *buffer) {
720 
721  int32 index, sdsid, rank, numbertype, nattrs;
722  int32 dimsizes[2], start[2];
723  char name[255];
724 
725  start[0] = start[1] = 0;
726 
727  if ((index = SDnametoindex(sdfid, sds_name)) < 0)
728  return -2;
729 
730  if ((sdsid = SDselect(sdfid, index)) < 0)
731  return -2;
732 
733  if ((SDgetinfo(sdsid, name, &rank, dimsizes, &numbertype, &nattrs)) < 0)
734  return -2;
735 
736  if ((SDreaddata(sdsid, start, NULL, dimsizes, buffer)) < 0)
737  return -2;
738 
739  if ((SDendaccess(sdsid)) < 0)
740  return -2;
741 
742  return SUCCEED;
743 }
744 
745 
746 /* ------------------------------------------------------ */
747 /* GetFileDesc() - reads the HDF file descriptions into */
748 /* a buffer. */
749 /* */
750 
751 /* ------------------------------------------------------ */
752 char *GetFileDesc(const char *filename) {
753  static char *desc_buffer = NULL;
754  int32 file_id, desc_length, fds_len;
755 
756  /* Open the file */
757  file_id = Hopen(filename, DFACC_READ, 0);
758 
759  /* Get the length of the file description */
760  desc_length = DFANgetfdslen(file_id, 1);
761  if (desc_length == 0)
762  return (desc_buffer);
763 
764  /* Create a buffer for the file description */
765  desc_buffer = HDgetspace(desc_length);
766  if (desc_buffer == NULL)
767  return (desc_buffer);
768  /* Read the file description */
769  fds_len = DFANgetfds(file_id, desc_buffer, desc_length, 1);
770  if (fds_len == 0)
771  return (desc_buffer);
772 
773  /* Add ';' if not last non-whitespace character */
774  while (desc_buffer[fds_len] <= 32) fds_len--;
775  if (desc_buffer[fds_len] != ';') desc_buffer[fds_len + 1] = ';';
776 
777  /* Close the file */
778  Hclose(file_id);
779 
780  return (desc_buffer);
781 }
782 
783 
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
integer, parameter int16
Definition: cubeio.f90:3
intn attach_vdata(int32 fid, const char *sname)
Definition: hdf_utils.c:686
int getDims(int32_t fileID, const char sdsname[H4_MAX_NC_NAME], int32_t dims[H4_MAX_VAR_DIMS])
Definition: hdf_utils.c:497
intn rdvdata(int32 vskey, const char *fields, int32 start, int32 nelt, unsigned char *databuf)
Definition: hdf_utils.c:646
int16_t fileID
int32 read_SDS(int32 sdfid, const char *sds_name, void *buffer)
Definition: hdf_utils.c:719
#define FAIL
Definition: ObpgReadGrid.h:18
#define NULL
Definition: decode_rs.h:63
int AddSdsToVgroup(int32_t sd_id, int32_t v_id, const char *name)
Definition: hdf_utils.c:383
int32_t hdf_sizeof(int32_t dtype)
return the sizeof dtype in bytes
Definition: hdf_utils.c:28
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT32
int v_attach(int32_t h_id, int32_t *v_id)
Definition: hdf_utils.c:404
int get_type(int32_t fileID, const char sdsname[H4_MAX_NC_NAME], int32_t *dtype)
Definition: hdf_utils.c:535
int sd_readdata(int32_t sd_id, const char *name, VOIDP data, int32_t s0, int32_t s1, int32_t s2, int32_t e0, int32_t e1, int32_t e2)
Definition: hdf_utils.c:324
#define LIFE_IS_GOOD
Definition: passthebuck.h:4
int sd_setattr(int32_t id, const char *nam, int32_t typ, int32_t cnt, const void *data)
Definition: hdf_utils.c:216
int sd_endaccess(int32_t id)
Definition: hdf_utils.c:255
int sd_select(int32_t sd_id, const char *name, int32_t *sds_id)
Definition: hdf_utils.c:355
float32 slope[]
Definition: l2lists.h:30
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE fields
Definition: HISTORY.txt:400
int getHDFattr(int32_t fileID, const char *attrname, const char *sdsname, VOIDP data)
Definition: hdf_utils.c:573
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
#define PTB(function)
Definition: passthebuck.h:16
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
int sd_writedata(int32_t sd_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: hdf_utils.c:293
int CreateSDS(int32_t sd_id, const char *sname, const char *lname, const char *standard_name, const char *units, double low, double high, float slope, float offset, int32_t nt, int32_t rank, int32_t d0, int32_t d1, int32_t d2, const char *dn0, const char *dn1, const char *dn2)
Definition: hdf_utils.c:77
dtype
Definition: DDataset.hpp:31
int rdSDS(int32_t fileID, const char *sdsname, int32_t start1, int32_t start2, int32_t edges1, int32_t edges2, VOIDP array_data)
Definition: hdf_utils.c:422
Extra metadata that will be written to the HDF4 file l2prod rank
char * GetFileDesc(const char *filename)
Definition: hdf_utils.c:752
l2prod offset
int sd_setdimnames(int32_t id, const char *d0, const char *d1, const char *d2)
Definition: hdf_utils.c:264
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_FLOAT32
These two strings are used for the product XML output If product_id is not set then prefix is used If the last char of the name_prefix is _ then it is removed If algorithm_id is not set then name_suffix is used If the first char is _ then it is removed l2prod standard_name[0]
#define PROGRAMMER_BOOBOO
Definition: passthebuck.h:8
int sd_setdimname(int32_t sds_id, int32_t dim_number, const char *name)
Definition: hdf_utils.c:275
int sd_create(int32_t id, const char *nam, int32_t typ, int32_t rank, int32_t d0, int32_t d1, int32_t d2, int32_t *sds_id)
Definition: hdf_utils.c:227
#define HDF_FUNCTION_ERROR
Definition: passthebuck.h:7
int count
Definition: decode_rs.h:79