OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
HDF_Lib.c
Go to the documentation of this file.
1 #include "HDF_Lib.h"
2 #include "PGS_Error_Codes.h"
3 
4 /*
5  * The following global variables are used in all functions within this module
6  * for forming short message strings and assigning the error code.
7  */
8 
9 static char errmsgbuf[PGS_SMF_MAX_MSGBUF_SIZE];
10 static PGSt_SMF_status errcode;
11 
12 /*
13  * The following are commonly used reasons which the low-level function
14  * could fail (these are oriented toward operations -- in development, there
15  * is always the possibility of a code error).
16  */
17 
18 char *invalidinputfile = "This is most likely due to an invalid file.\n"
19  "(does not meet file specs or is corrupted)";
20 char *corruptinputfile = "Operationally, this should never occur.\n"
21  "The file may be incomplete or corrupted.";
22 static char *corruptoutputfile = "Operationally, this should never occur.\n"
23  "The file or file pointer may have become corrupted.";
24 static char *nowriteoutputfile = "May be out of disk space or file became corrupted.";
25 
26 /*
27  * The following function (prototype is below, function is later in module)
28  * is local in scope to this module.
29  */
30 static
31 PGSt_SMF_status assign_data_type(char *, int32 *);
32 
33 PGSt_SMF_status read_attribute (int32 s_id,
34  char *attr_name,
35  int32 TypeID,
36  void *buffer)
37 /*
38 !C**************************************************************************
39 !Description: Reads an attribute into buffer from a HDF file.
40 
41 !Input Parameters:
42  int32 s_id sd_id for global, or sds_id for local, attrbute
43  char *attr_name name of the attribute
44  int32 TypeID DFNT Type (used for safety checking only)
45 
46 !Output Parameters:
47  void *buffer buffer to hold the value(s) of the attribute.
48  If the attribute is a string, the buffer size
49  in the caller should be at least one more than
50  the string length.
51 
52 !Revision History:
53  $Log: HDF_Lib.c,v $
54  Revision 1.8 2005-01-18 16:57:34-05 ltan
55  Added new file attributes prefixed with HDFEOS_FractionalOffset
56 
57 
58  Revision 01.11 October 16, 2004 Razor Issue #200
59  Casted Int32 variables in sprintf calls to "long" with the
60  format specifier "%ld" for better code portability.
61  Liqin Tan, SAIC GSO (ltan@saicmodis.com)
62 
63  Revision 01.10 Feb. 1997
64  Changed return value to PGSt_SMF_status.
65  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
66 
67  Revision 01.00 1996/03/19
68  Initial development
69  Neal Devine(neal.devine@gsfc.nasa.gov)
70 
71 !Team-unique Header:
72 
73 !References and Credits:
74  This software is developed by the MODIS Science Data Support
75  Team for the National Aeronautics and Space Administration,
76  Goddard Space Flight Center, under contract NAS5-32373.
77 
78  HDF portions developed at the National Center for Supercomputing
79  Applications at the University of Illinois at Urbana-Champaign.
80 
81 !Design Notes:
82 
83 !END********************************************************************
84 */
85 {
86  int32 attr_index = 0;
87  int32 count = 0;
88  intn res = 0;
89  int32 data_type = 0;
90  char attr_name_buf[MAX_NC_NAME];
91 
92  attr_index = SDfindattr(s_id, attr_name);
93  if (attr_index == FAIL) {
94  errcode = MODIS_F_READ_ERROR;
95  sprintf(errmsgbuf, "Could not find attribute \"%s\" in the file.",
96  attr_name);
97  L1BErrorMsg("read_attribute", errcode, errmsgbuf, "SDfindattr", 0,
99  return errcode;
100  }
101 
102  res = SDattrinfo(s_id, attr_index, attr_name_buf, &data_type, &count);
103  if (res == FAIL) {
104  errcode = MODIS_F_HDF_ERROR;
105  sprintf(errmsgbuf, "Could not get attribute information for \"%s\".",
106  attr_name);
107  L1BErrorMsg("read_attribute", errcode, errmsgbuf, "SDattrinfo", 0,
109  return errcode;
110  }
111 
112  /*Make sure data_type is as expected*/
113 
114  if (data_type != TypeID) {
115  errcode = MODIS_F_READ_ERROR;
116  sprintf(errmsgbuf, "Attribute \"%s\" has data type mismatch.\n"
117  "File data type = %ld, expected type = %ld\n",
118  attr_name, (long)data_type, (long)TypeID);
119  L1BErrorMsg("read_attribute", errcode, errmsgbuf, NULL, 0,
121  return errcode;
122  }
123 
124  res = SDreadattr(s_id, attr_index, buffer);
125  if (res == FAIL) {
126  errcode = MODIS_F_HDF_ERROR;
127  sprintf(errmsgbuf, "Could not read attribute \"%s\" from file.",
128  attr_name);
129  L1BErrorMsg("read_attribute", errcode, errmsgbuf, "SDreadattr", 0,
131  return errcode;
132  }
133 
134  /*
135  * SDreadattr does not add a '\0' at the end of string. It should be
136  * added explicitly if the attribute is a string.
137  */
138 
139  if (data_type == DFNT_CHAR8)
140  {
141  char *buffer_ptr;
142  buffer_ptr = buffer;
143  buffer_ptr[count] = '\0';
144  }
145 
146  return(MODIS_S_OK);
147 }
148 
149 
150 PGSt_SMF_status read_part_sds_rank2 (int32 sd_id,
151  char *sds_name,
152  int32 start0,
153  int32 start1,
154  int32 edge0,
155  int32 edge1,
156  void *data)
157 /*
158 !C***************************************************************
159 !Description: Reads a part of a any data type of 2D sds from
160  a HDF file.
161 
162 !Input Parameters:
163  int32 sd_id
164  char *sds_name
165  int32 start0
166  int32 start1
167  int32 edge0
168  int32 edge1
169 
170 !Output Parameters:
171  void *data
172 
173 !Revision History:
174  Revision 01.00 Jan 1999
175  Zhenying Gu (zgu@mcst.gsfc.nasa.gov)
176 
177 !Team-unique Header:
178 
179 !References and Credits:
180  This software is developed by the MODIS Science Data Support
181  Team for the National Aeronautics and Space Administration,
182  Goddard Space Flight Center, under contract NAS5-32373.
183 
184  HDF portions developed at the National Center for Supercomputing
185  Applications at the University of Illinois at Urbana-Champaign.
186 
187 !Design Notes:
188  This function read part of sds data into a array and remain other part
189  of the array. The array size should be the same as the SDS data in the
190  HDF file. Otherwise, the location of the read data in the array is
191  meaningless.
192 !END********************************************************************
193 */
194 {
195  int32 sds_index = 0;
196  int32 sds_id = 0;
197  int32 start[2] = {0, 0};
198  int32 edge[2] = {0, 0};
199 
200  start[0] = start0;
201  start[1] = start1;
202 
203  edge[0] = edge0;
204  edge[1] = edge1;
205 
206  sds_index = SDnametoindex (sd_id, sds_name);
207  if (sds_index == FAIL) {
208  errcode = MODIS_F_READ_ERROR;
209  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
210  sds_name);
211  L1BErrorMsg("read_part_sds_rank2", errcode, errmsgbuf, "SDnametoindex", 0,
213  return errcode;
214  }
215 
216  sds_id = SDselect (sd_id, sds_index);
217  if (sds_id == FAIL) {
218  errcode = MODIS_F_HDF_ERROR;
219  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
220  sds_name);
221  L1BErrorMsg("read_part_sds_rank2", errcode, errmsgbuf, "SDselect", 0,
223  return errcode;
224  }
225 
226  if (SDreaddata(sds_id, start, NULL, edge, data) == FAIL) {
227  SDendaccess(sds_id);
228  errcode = MODIS_F_HDF_ERROR;
229  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
230  sds_name);
231  L1BErrorMsg("read_part_sds_rank2", errcode, errmsgbuf, "SDreaddata", 0,
233  return errcode;
234  }
235 
236  if (SDendaccess(sds_id)== FAIL) {
237  errcode = MODIS_F_HDF_ERROR;
238  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
239  sds_name);
240  L1BErrorMsg("read_part_sds_rank2", errcode, errmsgbuf, "SDendaccess", 0,
242  return errcode;
243  }
244 
245  return (MODIS_S_OK);
246 }
247 
248 
249 PGSt_SMF_status read_part_sds_rank3 (int32 sd_id,
250  char *sds_name,
251  int32 start0,
252  int32 start1,
253  int32 start2,
254  int32 edge0,
255  int32 edge1,
256  int32 edge2,
257  void *data)
258 /*
259 !C***************************************************************
260 !Description: Reads a part of a any data type of 3D sds from
261  a HDF file.
262 
263 !Input Parameters:
264  int32 sd_id
265  char *sds_name
266  int32 start0
267  int32 start1
268  int32 start2
269  int32 edge0
270  int32 edge1
271  int32 edge2
272 
273 !Output Parameters:
274  void *data
275 
276 !Revision History:
277  Revision 02.10 April 1998
278  Changed the interface
279  Zhenying Gu (zgu@gscmail.gsfc.nasa.gov)
280 
281  Revision 01.00 March 1997
282  Initial development.
283  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
284 
285 !Team-unique Header:
286 
287 !References and Credits:
288  This software is developed by the MODIS Science Data Support
289  Team for the National Aeronautics and Space Administration,
290  Goddard Space Flight Center, under contract NAS5-32373.
291 
292  HDF portions developed at the National Center for Supercomputing
293  Applications at the University of Illinois at Urbana-Champaign.
294 
295 !Design Notes:
296  This function read part of sds data into a array and remain other part
297  of the array. The array size should be the same as the SDS data in the
298  HDF file. Otherwise, the location of the read data in the array is
299  meaningless.
300 !END********************************************************************
301 */
302 {
303  int32 sds_index = 0;
304  int32 sds_id = 0;
305  int32 start[3] = {0, 0, 0};
306  int32 edge[3] = {0, 0, 0};
307 
308  start[0] = start0;
309  start[1] = start1;
310  start[2] = start2;
311 
312  edge[0] = edge0;
313  edge[1] = edge1;
314  edge[2] = edge2;
315 
316  sds_index = SDnametoindex (sd_id, sds_name);
317  if (sds_index == FAIL) {
318  errcode = MODIS_F_READ_ERROR;
319  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
320  sds_name);
321  L1BErrorMsg("read_part_sds_rank3", errcode, errmsgbuf, "SDnametoindex", 0,
323  return errcode;
324  }
325 
326  sds_id = SDselect (sd_id, sds_index);
327  if (sds_id == FAIL) {
328  errcode = MODIS_F_HDF_ERROR;
329  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
330  sds_name);
331  L1BErrorMsg("read_part_sds_rank3", errcode, errmsgbuf, "SDselect", 0,
333  return errcode;
334  }
335 
336  if (SDreaddata(sds_id, start, NULL, edge, data) == FAIL) {
337  SDendaccess(sds_id);
338  errcode = MODIS_F_HDF_ERROR;
339  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
340  sds_name);
341  L1BErrorMsg("read_part_sds_rank3", errcode, errmsgbuf, "SDreaddata", 0,
343  return errcode;
344  }
345 
346  if (SDendaccess(sds_id)== FAIL) {
347  errcode = MODIS_F_HDF_ERROR;
348  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
349  sds_name);
350  L1BErrorMsg("read_part_sds_rank3", errcode, errmsgbuf, "SDendaccess", 0,
352  return errcode;
353  }
354 
355  return (MODIS_S_OK);
356 }
357 
358 
359 PGSt_SMF_status read_sds_rank1 (int32 file_id,
360  char *sds_name,
361  int32 dim,
362  void *data)
363 /*
364 !C***************************************************************
365 !Description: Reads a 1D array data from a HDF file.
366 
367 !Input Parameters:
368  int32 file_id * HDF file ID *
369  char *sds_name * sds name in HDF file *
370  int32 dim * dim of sds array *
371 
372 !Output Parameters:
373  void *data * 1D array *
374 
375 !Revision History:
376  Revision 01.00 October 1996
377  Initial development.
378  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
379 
380 !Team-unique Header:
381 
382 !References and Credits:
383  This software is developed by the MODIS Science Data Support
384  Team for the National Aeronautics and Space Administration,
385  Goddard Space Flight Center, under contract NAS5-32373.
386 
387  HDF portions developed at the National Center for Supercomputing
388  Applications at the University of Illinois at Urbana-Champaign.
389 
390 !Design Notes:
391  Warning: The data type passed in must be the same as the data in hdf file
392  Otherwise, the return results will be wrong.
393 !END********************************************************************
394 */
395 {
396  int32 sds_index = 0;
397  int32 sds_id = 0;
398  intn result = 0;
399  int32 start[1] = {0};
400  int32 edge[1] = {0};
401 
402  edge[0] = dim;
403 
404  sds_index = SDnametoindex (file_id, sds_name);
405  if (sds_index == FAIL) {
406  errcode = MODIS_F_READ_ERROR;
407  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
408  sds_name);
409  L1BErrorMsg("read_sds_rank1", errcode, errmsgbuf, "SDnametoindex", 0,
411  return errcode;
412  }
413 
414  sds_id = SDselect (file_id, sds_index);
415  if (sds_id == FAIL) {
416  errcode = MODIS_F_HDF_ERROR;
417  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
418  sds_name);
419  L1BErrorMsg("read_sds_rank1", errcode, errmsgbuf, "SDselect", 0,
421  return errcode;
422  }
423 
424  result = SDreaddata (sds_id, start, NULL, edge, data);
425  if (result == FAIL) {
426  SDendaccess(sds_id);
427  errcode = MODIS_F_HDF_ERROR;
428  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
429  sds_name);
430  L1BErrorMsg("read_sds_rank1", errcode, errmsgbuf, "SDreaddata", 0,
432  return errcode;
433  }
434 
435  result = SDendaccess (sds_id);
436  if (result == FAIL) {
437  errcode = MODIS_F_HDF_ERROR;
438  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
439  sds_name);
440  L1BErrorMsg("read_sds_rank1", errcode, errmsgbuf, "SDendaccess", 0,
442  return errcode;
443  }
444 
445  return (MODIS_S_OK);
446 }
447 
448 
449 PGSt_SMF_status read_sds_rank2 (int32 file_id,
450  char *sds_name,
451  int32 dim1,
452  int32 dim2,
453  void *data)
454 /*
455 !C***************************************************************
456 !Description: Reads a 2D array.
457 
458 !Input Parameters:
459  int32 file_id * HDF file ID *
460  char *sds_name * sds name in HDF file *
461  int32 dim1 * size of dimension 1 *
462  int32 dim2 * size of dimension 2 *
463 
464 !Output Parameters:
465  void data[][]
466 
467 !Revision History:
468  Revision 02.10 April 1998
469  This function is base on read_sds_rank2_float32()
470  Zhenying Gu(zgu@gscmail.gsfc.nasa.gov
471 
472  Revision 01.00 October 1996
473  Initial development.
474  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
475 
476 !Team-unique Header:
477 
478 !References and Credits:
479  This software is developed by the MODIS Science Data Support
480  Team for the National Aeronautics and Space Administration,
481  Goddard Space Flight Center, under contract NAS5-32373.
482 
483  HDF portions developed at the National Center for Supercomputing
484  Applications at the University of Illinois at Urbana-Champaign.
485 
486 !Design Notes:
487  The data type passed in should be the same as the data type in the
488  hdf file. Otherwise the results will be wrong.
489 !END********************************************************************
490 */
491 {
492  int32 sds_index = 0;
493  int32 sds_id = 0;
494  intn result = 0;
495  int32 start[2] = {0, 0};
496  int32 edge[2] = {0, 0};
497 
498  edge[0] = dim1;
499  edge[1] = dim2;
500 
501  sds_index = SDnametoindex (file_id, sds_name);
502  if (sds_index == FAIL) {
503  errcode = MODIS_F_READ_ERROR;
504  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
505  sds_name);
506  L1BErrorMsg("read_sds_rank2", errcode, errmsgbuf, "SDnametoindex", 0,
508  return errcode;
509  }
510 
511  sds_id = SDselect (file_id, sds_index);
512  if (sds_id == FAIL) {
513  errcode = MODIS_F_HDF_ERROR;
514  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
515  sds_name);
516  L1BErrorMsg("read_sds_rank2", errcode, errmsgbuf, "SDselect", 0,
518  return errcode;
519  }
520 
521  result = SDreaddata (sds_id, start, NULL, edge, data);
522  if (result == FAIL) {
523  SDendaccess(sds_id);
524  errcode = MODIS_F_HDF_ERROR;
525  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
526  sds_name);
527  L1BErrorMsg("read_sds_rank2", errcode, errmsgbuf, "SDreaddata", 0,
529  return errcode;
530  }
531 
532  result = SDendaccess(sds_id);
533  if (result == FAIL) {
534  errcode = MODIS_F_HDF_ERROR;
535  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
536  sds_name);
537  L1BErrorMsg("read_sds_rank2", errcode, errmsgbuf, "SDendaccess", 0,
539  return errcode;
540  }
541 
542  return (MODIS_S_OK);
543 }
544 
545 
546 PGSt_SMF_status read_sds_rank3 (int32 file_id,
547  char *sds_name,
548  int32 dim1,
549  int32 dim2,
550  int32 dim3,
551  void *data)
552 /*
553 !C***************************************************************
554 !Description: Reads a 3D sds array.
555 !Input Parameters:
556  int32 file_id * HDF file ID *
557  char *sds_name * sds name in HDF file *
558  int32 dim1 * size of dimension 1 *
559  int32 dim2 * size of dimension 2 *
560  int32 dim3 * size of dimension 3 *
561 
562 !Output Parameters:
563  void data[][][]
564 
565 !Revision History:
566  Revision 02.10 April 1998
567  Change from read float32 *** array to read general type three dimention array. Name changed
568  from read_sds_rank3_float32() to read_sds_rand3_1p.
569  Zhenying Gu(zgu@gscmail.gsfc.nasa.gov)
570 
571  Revision 01.00 October 1996
572  Initial development.
573  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
574 
575 !Team-unique Header:
576 
577 !References and Credits:
578  This software is developed by the MODIS Science Data Support
579  Team for the National Aeronautics and Space Administration,
580  Goddard Space Flight Center, under contract NAS5-32373.
581 
582  HDF portions developed at the National Center for Supercomputing
583  Applications at the University of Illinois at Urbana-Champaign.
584 
585 !Design Notes:
586 
587 !END********************************************************************
588 */
589 {
590  int32 sds_index = 0;
591  int32 sds_id = 0;
592  intn result = 0;
593  int32 start[3] = {0, 0, 0};
594  int32 edge[3] = {0, 0, 0};
595 
596  edge[0] = dim1;
597  edge[1] = dim2;
598  edge[2] = dim3;
599 
600  sds_index = SDnametoindex (file_id, sds_name);
601  if (sds_index == FAIL) {
602  errcode = MODIS_F_READ_ERROR;
603  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
604  sds_name);
605  L1BErrorMsg("read_sds_rank3", errcode, errmsgbuf, "SDnametoindex", 0,
607  return errcode;
608  }
609 
610  sds_id = SDselect (file_id, sds_index);
611  if (sds_id == FAIL) {
612  errcode = MODIS_F_HDF_ERROR;
613  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
614  sds_name);
615  L1BErrorMsg("read_sds_rank3", errcode, errmsgbuf, "SDselect", 0,
617  return errcode;
618  }
619 
620  result = SDreaddata (sds_id, start, NULL, edge, data);
621  if (result == FAIL) {
622  SDendaccess(sds_id);
623  errcode = MODIS_F_HDF_ERROR;
624  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
625  sds_name);
626  L1BErrorMsg("read_sds_rank3", errcode, errmsgbuf, "SDreaddata", 0,
628  return errcode;
629  }
630 
631  result = SDendaccess(sds_id);
632  if (result == FAIL) {
633  errcode = MODIS_F_HDF_ERROR;
634  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
635  sds_name);
636  L1BErrorMsg("read_sds_rank3", errcode, errmsgbuf, "SDendaccess", 0,
638  return errcode;
639  }
640 
641  return (MODIS_S_OK);
642 }
643 
644 PGSt_SMF_status read_sds_rank4(int32 file_id,
645  char *sds_name,
646  int32 dim1,
647  int32 dim2,
648  int32 dim3,
649  int32 dim4,
650  void *data)
651 /*
652 !C*********************************************************************
653 !Description: Reads a 4D array data.
654 
655 !Input Parameters:
656  int32 file_id
657  char *sds_name
658  int32 dim1
659  int32 dim2
660  int32 dim3
661  int32 dim4
662 
663 !Output Parameters:
664  void data[][][][]
665 
666 !Revision History:
667  Revision 02.10 April 1998
668  Change reading to float32 **** to general type 4 dimention array. And change
669  the name from read_sds_rank4_float32() to read_sds_rank4_1p.
670  Zhenying Gu (zgu@gscmail.gsfc.nasa.gov)
671 
672  Revision 01.00 October 1996
673  Initial development.
674  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
675 
676 !Team-unique Header:
677 
678  !References and Credits
679  This software is developed by the MODIS Science Data Support
680  Team for the National Aeronautics and Space Administration,
681  Goddard Space Flight Center, under contract NAS5-32373.
682 
683  HDF portions developed at the National Center for Supercomputing
684  Applications at the University of Illinois at Urbana-Champaign.
685 
686  !Design Notes:
687 
688 !END********************************************************************
689 */
690 {
691  int32 sds_index = 0;
692  int32 sds_id = 0;
693  intn result = 0;
694  int32 start[4] = {0, 0, 0, 0};
695  int32 edge[4] = {0, 0, 0, 0};
696 
697  edge[0] = dim1;
698  edge[1] = dim2;
699  edge[2] = dim3;
700  edge[3] = dim4;
701 
702  sds_index = SDnametoindex (file_id, sds_name);
703  if (sds_index == FAIL) {
704  errcode = MODIS_F_READ_ERROR;
705  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
706  sds_name);
707  L1BErrorMsg("read_sds_rank4", errcode, errmsgbuf, "SDnametoindex", 0,
709  return errcode;
710  }
711 
712  sds_id = SDselect (file_id, sds_index);
713  if (sds_id == FAIL) {
714  errcode = MODIS_F_HDF_ERROR;
715  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
716  sds_name);
717  L1BErrorMsg("read_sds_rank4", errcode, errmsgbuf, "SDselect", 0,
719  return errcode;
720  }
721 
722  result = SDreaddata (sds_id, start, NULL, edge, data);
723  if (result == FAIL) {
724  SDendaccess(sds_id);
725  errcode = MODIS_F_HDF_ERROR;
726  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
727  sds_name);
728  L1BErrorMsg("read_sds_rank4", errcode, errmsgbuf, "SDreaddata", 0,
730  return errcode;
731  }
732 
733  result = SDendaccess(sds_id);
734  if (result == FAIL) {
735  errcode = MODIS_F_HDF_ERROR;
736  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
737  sds_name);
738  L1BErrorMsg("read_sds_rank4", errcode, errmsgbuf, "SDendaccess", 0,
740  return errcode;
741  }
742 
743  return(MODIS_S_OK);
744 }
745 
746 
747 
748 PGSt_SMF_status read_vdata (int32 v_id,
749  int32 start_record,
750  int32 records,
751  char *vname,
752  char *fname,
753  void *buffer)
754 /*
755 !C**************************************************************************
756 !Description: Reads vdata for given vdata_name and field_name from a HDF file.
757 
758 !Input Parameters:
759  int32 v_id file id for the vdata interfaces
760  int32 start_record starting record
761  int32 records number of records to read
762  char *vname vdata_name
763  char *fname field_name
764  void *buffer buffer to hold the data
765 
766 !Output Parameters:
767  void *buffer buffer to hold the data
768 
769 !Revision History:
770  Revision 01.00 Aug 1997
771  Initial development
772  Zhidong Hao (hao@barebackride.gsfc.nasa.gov)
773 
774 !Team-unique Header:
775 
776  This software is developed by the MODIS Science Data Support
777  Team for the National Aeronautics and Space Administration,
778  Goddard Space Flight Center, under contract NAS5-32373.
779 
780 !References and Credits:
781  HDF portions developed at the National Center for Supercomputing
782  Applications at the University of Illinois at Urbana-Champaign.
783 
784 !Design Notes:
785 
786 !END********************************************************************
787 */
788 {
789  intn hdf_return = FAIL;
790  int32 vd_ref = 0;
791  int32 vd_id = 0;
792  int32 n_records = 0;
793  int32 interlace = 0;
794 
795  vd_ref = VSfind(v_id, vname);
796  /*VSfind() returns the vdata reference number if successful and 0 otherwise.*/
797  if (vd_ref == 0) {
798  errcode = MODIS_F_READ_ERROR;
799  sprintf(errmsgbuf, "Could not find Vdata \"%s\" in the file.",
800  vname);
801  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSfind", 0,
803  return errcode;
804  }
805 
806  vd_id = VSattach(v_id, vd_ref, "r");
807  if (vd_id == FAIL) {
808  errcode = MODIS_F_HDF_ERROR;
809  sprintf(errmsgbuf, "Could not attach to Vdata \"%s\".",
810  vname);
811  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSattach", 0,
813  return errcode;
814  }
815 
816  hdf_return = VSinquire(vd_id, &n_records, &interlace, NULL, NULL, NULL);
817  if (hdf_return == FAIL) {
818  errcode = MODIS_F_HDF_ERROR;
819  sprintf(errmsgbuf, "Could not get information about Vdata \"%s\".",
820  vname);
821  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSinquire", 0,
823  return errcode;
824  }
825 
826  if (records == -1)
827  records = n_records - start_record;
828 
829  if (n_records < records + start_record ) {
830  errcode = MODIS_F_OUT_OF_RANGE;
831  sprintf(errmsgbuf, "Vdata \"%s\" contains too few records based on input arguments.",
832  vname);
833  L1BErrorMsg("read_vdata", errcode, errmsgbuf, NULL, 0,
835  return errcode;
836  }
837 
838  hdf_return = VSseek (vd_id, start_record);
839  if (hdf_return == FAIL) {
840  errcode = MODIS_F_HDF_ERROR;
841  sprintf(errmsgbuf, "For Vdata \"%s\", could not set current record to desired start_record.",
842  vname);
843  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSseek", 0,
845  return errcode;
846  }
847 
848  hdf_return = VSsetfields (vd_id, fname);
849  if (hdf_return == FAIL) {
850  errcode = MODIS_F_READ_ERROR;
851  sprintf(errmsgbuf, "For Vdata \"%s\", could not set field to \"%s\".",
852  vname, fname);
853  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSsetfields", 0,
855  return errcode;
856  }
857 
858  hdf_return = VSread (vd_id, buffer, records, interlace);
859  if (hdf_return == FAIL) {
860  errcode = MODIS_F_HDF_ERROR;
861  sprintf(errmsgbuf, "Could not read data from Vdata \"%s\".",
862  vname);
863  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSread", 0,
865  return errcode;
866  }
867 
868  hdf_return = VSdetach(vd_id);
869  if (hdf_return == FAIL) {
870  errcode = MODIS_F_HDF_ERROR;
871  sprintf(errmsgbuf, "Could not detach from Vdata \"%s\".",
872  vname);
873  L1BErrorMsg("read_vdata", errcode, errmsgbuf, "VSdetach", 0,
875  return errcode;
876  }
877 
878  return MODIS_S_OK;
879 }
880 
881 static
882 PGSt_SMF_status assign_data_type(char *data_type, int32 *number_type)
883 /*
884 !C**************************************************************************
885 !Description: Reads vdata for given vdata_name and field_name from a HDF file.
886 
887 !Input Parameters:
888  char *data_type
889 
890 !Output Parameters:
891  int32 *number_type
892 
893 !Revision History:
894  Revision 01.00 April 1998
895  This is based on David Catozzi's L1A code.
896  Initial development
897  Zhenying Gu (zgu@ltpmail.gsfc.nasa.gov)
898 
899 !Team-unique Header:
900 
901 !References and Credits:
902  This software is developed by the MODIS Science Data Support
903  Team for the National Aeronautics and Space Administration,
904  Goddard Space Flight Center, under contract NAS5-32373.
905 
906  HDF portions developed at the National Center for Supercomputing
907  Applications at the University of Illinois at Urbana-Champaign.
908 
909 !Design Notes:
910 
911 !END********************************************************************
912 */
913 {
914  PGSt_SMF_status returnStatus = MODIS_S_OK;
915 
916  if (!data_type || !number_type) {
917  errcode = MODIS_F_INVALID_ARGUMENT;
918  L1BErrorMsg("assign_data_type", errcode, "NULL input pointer(s)",
919  NULL, 0, "Coding error.", False);
920  return errcode;
921  }
922  else if (strcmp(data_type, "int8") == 0) *number_type = DFNT_INT8;
923  else if (strcmp(data_type, "uint8") == 0) *number_type = DFNT_UINT8;
924  else if (strcmp(data_type, "int16") == 0) *number_type = DFNT_INT16;
925  else if (strcmp(data_type, "uint16") == 0) *number_type = DFNT_UINT16;
926  else if (strcmp(data_type, "int32") == 0) *number_type = DFNT_INT32;
927  else if (strcmp(data_type, "uint32") == 0) *number_type = DFNT_UINT32;
928  else if (strcmp(data_type, "float32") == 0) *number_type = DFNT_FLOAT32;
929  else if (strcmp(data_type, "float64") == 0) *number_type = DFNT_FLOAT64;
930  else if (strcmp(data_type, "char") == 0) *number_type = DFNT_CHAR;
931  else {
932  errcode = MODIS_F_INVALID_ARGUMENT;
933  sprintf(errmsgbuf,
934  "data_type \"%s\" does not match any allowed type in function.",
935  data_type);
936  L1BErrorMsg("assign_data_type", errcode, errmsgbuf, NULL, 0,
937  "Coding error.", False);
938  return errcode;
939  }
940  return(returnStatus);
941 }
942 
943 
944 PGSt_SMF_status write_sds_rank1 (int32 file_id,
945  char *sds_name,
946  char *dim_name,
947  int32 dim,
948  char *datatype,
949  void *data)
950 /*
951 !C*********************************************************************
952 !Description: Writes a 1D array
953 
954 !Input Parameters:
955  int32 file_id
956  char *sds_name
957  char *dim_name
958  int32 dim
959  char *datatype
960  int8 *data
961 
962 !Output Parameters:
963 
964 !Revision History:
965  Revision 01.00 April. 1998
966  Initial development.
967  Zhenying Gu (zgu@barebackride.gsfc.nasa.gov)
968 
969 !Team-unique Header:
970 
971  !References and Credits
972  This software is developed by the MODIS Science Data Support
973  Team for the National Aeronautics and Space Administration,
974  Goddard Space Flight Center, under contract NAS5-32373.
975 
976  HDF portions developed at the National Center for Supercomputing
977  Applications at the University of Illinois at Urbana-Champaign.
978 
979  !Design Notes:
980  This function is based on Zhidong hao's write_sds_rank2_float32() and
981  David Catozzi's assign_data_type() with modification.
982 
983 !END********************************************************************
984 */
985 {
986  PGSt_SMF_status returnStatus = MODIS_S_OK;
987  intn result = 0;
988  int32 sds_id = 0;
989  int32 rank = 1;
990  int32 dim_id = 0;
991  int32 start[1] = {0};
992  int32 edge[1] = {0};
993  int32 dim_size[1] = {0};
994  int32 number_type;
995 
996  dim_size[0] = dim;
997  edge[0] = dim;
998 
999  returnStatus = assign_data_type(datatype, &number_type);
1000  if (returnStatus != MODIS_S_OK) {
1001  L1BErrorMsg("write_sds_rank1", returnStatus, NULL, "assign_data_type", 0,
1002  NULL, False);
1003  return returnStatus;
1004  }
1005 
1006  sds_id = SDcreate(file_id, sds_name, number_type, rank, dim_size);
1007  if (sds_id == FAIL) {
1008  errcode = MODIS_F_WRITE_ERROR;
1009  sprintf(errmsgbuf, "Could not create SDS \"%s\" in the file.",
1010  sds_name);
1011  L1BErrorMsg("write_sds_rank1", errcode, errmsgbuf, "SDcreate", 0,
1012  corruptoutputfile, False);
1013  return errcode;
1014  }
1015 
1016  dim_id = SDgetdimid(sds_id, 0);
1017  if (dim_id == FAIL) {
1018  errcode = MODIS_F_HDF_ERROR;
1019  sprintf(errmsgbuf, "Could not retrieve dimension id for SDS \"%s\".",
1020  sds_name);
1021  L1BErrorMsg("write_sds_rank1", errcode, errmsgbuf, "SDgetdimid", 0,
1022  corruptoutputfile, False);
1023  SDendaccess(sds_id);
1024  return errcode;
1025  }
1026 
1027  result = SDsetdimname (dim_id, dim_name);
1028  if (result == FAIL) {
1029  errcode = MODIS_F_HDF_ERROR;
1030  sprintf(errmsgbuf, "Could not set dimension name for SDS \"%s\".",
1031  sds_name);
1032  L1BErrorMsg("write_sds_rank1", errcode, errmsgbuf, "SDsetdimname", 0,
1033  corruptoutputfile, False);
1034  SDendaccess(sds_id);
1035  return errcode;
1036  }
1037 
1038  result = SDwritedata(sds_id, start, NULL, edge, data);
1039  if (result == FAIL) {
1040  errcode = MODIS_F_WRITE_ERROR;
1041  sprintf(errmsgbuf, "Could not write data to SDS \"%s\".",
1042  sds_name);
1043  L1BErrorMsg("write_sds_rank1", errcode, errmsgbuf, "SDwritedata", 0,
1044  nowriteoutputfile, False);
1045  SDendaccess(sds_id);
1046  return errcode;
1047  }
1048 
1049  result = SDendaccess(sds_id);
1050  if (result == FAIL) {
1051  errcode = MODIS_F_HDF_ERROR;
1052  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
1053  sds_name);
1054  L1BErrorMsg("write_sds_rank1", errcode, errmsgbuf, "SDendaccess", 0,
1055  corruptoutputfile, False);
1056  return errcode;
1057  }
1058 
1059  return(MODIS_S_OK);
1060 }
1061 
1062 PGSt_SMF_status write_sds_rank2 (int32 file_id,
1063  char *sds_name,
1064  char *dim_name1,
1065  char *dim_name2,
1066  int32 dim1,
1067  int32 dim2,
1068  char *datatype,
1069  void *data)
1070 /*
1071 !C*********************************************************************
1072 !Description: Writes a 2D sds
1073 
1074 !Input Parameters:
1075  int32 file_id
1076  char *sds_name
1077  char *dim_name1
1078  char *dim_name2
1079  int32 dim1
1080  int32 dim2
1081  char *datatype
1082  void data[][]
1083 
1084 !Output Parameters:
1085 
1086 !Revision History:
1087  Revision 01.00 April 1998
1088  Initial development.
1089  Zhenying Gu (zgu@barebackride.gsfc.nasa.gov)
1090 
1091 !Team-unique Header:
1092 
1093  !References and Credits
1094  This software is developed by the MODIS Science Data Support
1095  Team for the National Aeronautics and Space Administration,
1096  Goddard Space Flight Center, under contract NAS5-32373.
1097 
1098  HDF portions developed at the National Center for Supercomputing
1099  Applications at the University of Illinois at Urbana-Champaign.
1100 
1101  !Design Notes:
1102  This function is based on Zhidong hao's write_sds_rank2_float32() and
1103  David Catozzi's assign_data_type() with modification.
1104 !END********************************************************************
1105 */
1106 {
1107  PGSt_SMF_status returnStatus = MODIS_S_OK;
1108  int i;
1109  int32 sds_id = 0;
1110  intn result = 0;
1111  int32 rank = 2;
1112  int32 dim_id[2] = {0, 0};
1113  int32 start[2] = {0, 0};
1114  int32 edge[2];
1115  int32 dim_size[2];
1116  char *dim_name[2];
1117  int32 number_type;
1118 
1119  edge[0] = dim1;
1120  edge[1] = dim2;
1121  dim_size[0] = dim1;
1122  dim_size[1] = dim2;
1123  dim_name[0] = dim_name1;
1124  dim_name[1] = dim_name2;
1125 
1126  returnStatus = assign_data_type(datatype, &number_type);
1127  if (returnStatus != MODIS_S_OK) {
1128  L1BErrorMsg("write_sds_rank2", returnStatus, NULL, "assign_data_type", 0,
1129  NULL, False);
1130  return returnStatus;
1131  }
1132 
1133  sds_id = SDcreate(file_id, sds_name, number_type, rank, dim_size);
1134  if (sds_id == FAIL) {
1135  errcode = MODIS_F_WRITE_ERROR;
1136  sprintf(errmsgbuf, "Could not create SDS \"%s\" in the file.",
1137  sds_name);
1138  L1BErrorMsg("write_sds_rank2", errcode, errmsgbuf, "SDcreate", 0,
1139  corruptoutputfile, False);
1140  return errcode;
1141  }
1142 
1143  for (i = 0; i < rank; i++)
1144  {
1145  dim_id[i] = SDgetdimid(sds_id, i);
1146  if (dim_id[i] == FAIL) {
1147  errcode = MODIS_F_HDF_ERROR;
1148  sprintf(errmsgbuf, "Could not retrieve dimension #%d id for SDS \"%s\".",
1149  (i+1), sds_name);
1150  L1BErrorMsg("write_sds_rank2", errcode, errmsgbuf, "SDgetdimid", 0,
1151  corruptoutputfile, False);
1152  SDendaccess(sds_id);
1153  return errcode;
1154  }
1155 
1156  result = SDsetdimname (dim_id[i], dim_name[i]);
1157  if (result == FAIL) {
1158  errcode = MODIS_F_HDF_ERROR;
1159  sprintf(errmsgbuf, "Could not set dimension #%d name for SDS \"%s\".",
1160  (i+1), sds_name);
1161  L1BErrorMsg("write_sds_rank2", errcode, errmsgbuf, "SDsetdimname", 0,
1162  corruptoutputfile, False);
1163  SDendaccess(sds_id);
1164  return errcode;
1165  }
1166  }
1167 
1168  result = SDwritedata(sds_id, start, NULL, edge, data);
1169  if (result == FAIL) {
1170  errcode = MODIS_F_WRITE_ERROR;
1171  sprintf(errmsgbuf, "Could not write data to SDS \"%s\".",
1172  sds_name);
1173  L1BErrorMsg("write_sds_rank2", errcode, errmsgbuf, "SDwritedata", 0,
1174  nowriteoutputfile, False);
1175  SDendaccess(sds_id);
1176  return errcode;
1177  }
1178 
1179  result = SDendaccess(sds_id);
1180  if (result == FAIL) {
1181  errcode = MODIS_F_HDF_ERROR;
1182  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
1183  sds_name);
1184  L1BErrorMsg("write_sds_rank2", errcode, errmsgbuf, "SDendaccess", 0,
1185  corruptoutputfile, False);
1186  return errcode;
1187  }
1188 
1189  return(MODIS_S_OK);
1190 }
1191 
1192 PGSt_SMF_status write_sds_rank3 (int32 file_id,
1193  char *sds_name,
1194  char *dim_name1,
1195  char *dim_name2,
1196  char *dim_name3,
1197  int32 dim1,
1198  int32 dim2,
1199  int32 dim3,
1200  char *datatype,
1201  void *data)
1202 /*
1203 !C*********************************************************************
1204 !Description: Writes a 3D sds
1205 
1206 !Input Parameters:
1207  int32 file_id
1208  char *sds_name
1209  char *dim_name1
1210  char *dim_name2
1211  char *dim_name3
1212  int32 dim1
1213  int32 dim2
1214  int32 dim3
1215  char *datatype
1216  void data[][][]
1217 
1218 !Output Parameters:
1219 
1220 !Revision History:
1221  Revision 01.00 April 1998
1222  Initial development.
1223  Zhenying Gu (zgu@barebackride.gsfc.nasa.gov)
1224 
1225 !Team-unique Header:
1226 
1227  !References and Credits
1228  This software is developed by the MODIS Science Data Support
1229  Team for the National Aeronautics and Space Administration,
1230  Goddard Space Flight Center, under contract NAS5-32373.
1231 
1232  HDF portions developed at the National Center for Supercomputing
1233  Applications at the University of Illinois at Urbana-Champaign.
1234 
1235  !Design Notes:
1236  This function is based on Zhidong hao's write_sds_rank3_float32() and
1237  David Catozzi's assign_data_type() with modification.
1238 !END********************************************************************
1239 */
1240 {
1241  PGSt_SMF_status returnStatus = MODIS_S_OK;
1242  int i;
1243  int32 sds_id = 0;
1244  intn result = 0;
1245  int32 rank = 3;
1246  int32 dim_id[3] = {0, 0, 0};
1247  int32 start[3] = {0, 0, 0};
1248  int32 edge[3];
1249  int32 dim_size[3];
1250  char *dim_name[3];
1251  int32 number_type;
1252 
1253  edge[0] = dim1;
1254  edge[1] = dim2;
1255  edge[2] = dim3;
1256  dim_size[0] = dim1;
1257  dim_size[1] = dim2;
1258  dim_size[2] = dim3;
1259  dim_name[0] = dim_name1;
1260  dim_name[1] = dim_name2;
1261  dim_name[2] = dim_name3;
1262 
1263  returnStatus = assign_data_type(datatype, &number_type);
1264  if (returnStatus != MODIS_S_OK) {
1265  L1BErrorMsg("write_sds_rank3", returnStatus, NULL, "assign_data_type", 0,
1266  NULL, False);
1267  return returnStatus;
1268  }
1269 
1270  sds_id = SDcreate(file_id, sds_name, number_type, rank, dim_size);
1271  if (sds_id == FAIL) {
1272  errcode = MODIS_F_WRITE_ERROR;
1273  sprintf(errmsgbuf, "Could not create SDS \"%s\" in the file.",
1274  sds_name);
1275  L1BErrorMsg("write_sds_rank3", errcode, errmsgbuf, "SDcreate", 0,
1276  corruptoutputfile, False);
1277  return errcode;
1278  }
1279 
1280  for (i = 0; i < rank; i++)
1281  {
1282  dim_id[i] = SDgetdimid(sds_id, i);
1283  if (dim_id[i] == FAIL) {
1284  errcode = MODIS_F_HDF_ERROR;
1285  sprintf(errmsgbuf, "Could not retrieve dimension #%d id for SDS \"%s\".",
1286  (i+1), sds_name);
1287  L1BErrorMsg("write_sds_rank3", errcode, errmsgbuf, "SDgetdimid", 0,
1288  corruptoutputfile, False);
1289  SDendaccess(sds_id);
1290  return errcode;
1291  }
1292 
1293  result = SDsetdimname (dim_id[i], dim_name[i]);
1294  if (result == FAIL) {
1295  errcode = MODIS_F_HDF_ERROR;
1296  sprintf(errmsgbuf, "Could not set dimension #%d name for SDS \"%s\".",
1297  (i+1), sds_name);
1298  L1BErrorMsg("write_sds_rank3", errcode, errmsgbuf, "SDsetdimname", 0,
1299  corruptoutputfile, False);
1300  SDendaccess(sds_id);
1301  return errcode;
1302  }
1303  }
1304 
1305  result = SDwritedata(sds_id, start, NULL, edge, data);
1306  if (result == FAIL) {
1307  errcode = MODIS_F_WRITE_ERROR;
1308  sprintf(errmsgbuf, "Could not write data to SDS \"%s\".",
1309  sds_name);
1310  L1BErrorMsg("write_sds_rank3", errcode, errmsgbuf, "SDwritedata", 0,
1311  nowriteoutputfile, False);
1312  SDendaccess(sds_id);
1313  return errcode;
1314  }
1315 
1316  result = SDendaccess(sds_id);
1317  if (result == FAIL) {
1318  errcode = MODIS_F_HDF_ERROR;
1319  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
1320  sds_name);
1321  L1BErrorMsg("write_sds_rank3", errcode, errmsgbuf, "SDendaccess", 0,
1322  corruptoutputfile, False);
1323  return errcode;
1324  }
1325 
1326  return(MODIS_S_OK);
1327 }
1328 
1329 PGSt_SMF_status write_sds_rank4 (int32 file_id,
1330  char *sds_name,
1331  char *dim_name1,
1332  char *dim_name2,
1333  char *dim_name3,
1334  char *dim_name4,
1335  int32 dim1,
1336  int32 dim2,
1337  int32 dim3,
1338  int32 dim4,
1339  char *datatype,
1340  void *data)
1341 /*
1342 !C*********************************************************************
1343 !Description: Writes a 4D sds
1344 
1345 !Input Parameters:
1346  int32 file_id
1347  char *sds_name
1348  char *dim_name1
1349  char *dim_name2
1350  char *dim_name3
1351  char *dim_name4
1352  int32 dim1
1353  int32 dim2
1354  int32 dim3
1355  int32 dim4
1356  char *datatype
1357  void data[][][][]
1358 
1359 !Output Parameters:
1360 
1361 !Revision History:
1362  Revision 01.01 April 2002 Razor Issue #183
1363  Changed definition of number_type to be consistent with other routines.
1364  Gwyn Fireman, SAIC GSO (fireman@mcst.gsfc.nasa.gov)
1365 
1366  Revision 01.00 April 1998
1367  Initial development.
1368  Zhenying Gu (zgu@barebackride.gsfc.nasa.gov)
1369 
1370 !Team-unique Header:
1371 
1372  !References and Credits
1373  This software is developed by the MODIS Science Data Support
1374  Team for the National Aeronautics and Space Administration,
1375  Goddard Space Flight Center, under contract NAS5-32373.
1376 
1377  HDF portions developed at the National Center for Supercomputing
1378  Applications at the University of Illinois at Urbana-Champaign.
1379 
1380  !Design Notes:
1381  This function is based on Zhidong hao's write_sds_rank4_float32() and
1382  David Catozzi's assign_data_type() with modification.
1383 !END********************************************************************
1384 */
1385 {
1386  PGSt_SMF_status returnStatus = MODIS_S_OK;
1387  int i;
1388  int32 sds_id = 0;
1389  intn result = 0;
1390  int32 rank = 4;
1391  int32 dim_id[4] = {0, 0, 0, 0};
1392  int32 start[4] = {0, 0, 0, 0};
1393  int32 edge[4];
1394  int32 dim_size[4];
1395  char *dim_name[4];
1396  int32 number_type;
1397 
1398  edge[0] = dim1;
1399  edge[1] = dim2;
1400  edge[2] = dim3;
1401  edge[3] = dim4;
1402  dim_size[0] = dim1;
1403  dim_size[1] = dim2;
1404  dim_size[2] = dim3;
1405  dim_size[3] = dim4;
1406  dim_name[0] = dim_name1;
1407  dim_name[1] = dim_name2;
1408  dim_name[2] = dim_name3;
1409  dim_name[3] = dim_name4;
1410 
1411  returnStatus = assign_data_type(datatype, &number_type);
1412  if (returnStatus != MODIS_S_OK) {
1413  L1BErrorMsg("write_sds_rank4", returnStatus, NULL, "assign_data_type", 0,
1414  NULL, False);
1415  return returnStatus;
1416  }
1417 
1418  sds_id = SDcreate(file_id, sds_name, number_type, rank, dim_size);
1419  if (sds_id == FAIL) {
1420  errcode = MODIS_F_WRITE_ERROR;
1421  sprintf(errmsgbuf, "Could not create SDS \"%s\" in the file.",
1422  sds_name);
1423  L1BErrorMsg("write_sds_rank4", errcode, errmsgbuf, "SDcreate", 0,
1424  corruptoutputfile, False);
1425  return errcode;
1426  }
1427 
1428  for (i = 0; i < rank; i++)
1429  {
1430  dim_id[i] = SDgetdimid(sds_id, i);
1431  if (dim_id[i] == FAIL) {
1432  errcode = MODIS_F_HDF_ERROR;
1433  sprintf(errmsgbuf, "Could not retrieve dimension #%d id for SDS \"%s\".",
1434  (i+1), sds_name);
1435  L1BErrorMsg("write_sds_rank4", errcode, errmsgbuf, "SDgetdimid", 0,
1436  corruptoutputfile, False);
1437  SDendaccess(sds_id);
1438  return errcode;
1439  }
1440 
1441  result = SDsetdimname (dim_id[i], dim_name[i]);
1442  if (result == FAIL) {
1443  errcode = MODIS_F_HDF_ERROR;
1444  sprintf(errmsgbuf, "Could not set dimension #%d name for SDS \"%s\".",
1445  (i+1), sds_name);
1446  L1BErrorMsg("write_sds_rank4", errcode, errmsgbuf, "SDsetdimname", 0,
1447  corruptoutputfile, False);
1448  SDendaccess(sds_id);
1449  return errcode;
1450  }
1451  }
1452 
1453  result = SDwritedata(sds_id, start, NULL, edge, data);
1454  if (result == FAIL) {
1455  errcode = MODIS_F_WRITE_ERROR;
1456  sprintf(errmsgbuf, "Could not write data to SDS \"%s\".",
1457  sds_name);
1458  L1BErrorMsg("write_sds_rank4", errcode, errmsgbuf, "SDwritedata", 0,
1459  nowriteoutputfile, False);
1460  SDendaccess(sds_id);
1461  return errcode;
1462  }
1463 
1464  result = SDendaccess(sds_id);
1465  if (result == FAIL) {
1466  errcode = MODIS_F_HDF_ERROR;
1467  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
1468  sds_name);
1469  L1BErrorMsg("write_sds_rank4", errcode, errmsgbuf, "SDendaccess", 0,
1470  corruptoutputfile, False);
1471  return errcode;
1472  }
1473 
1474  return(MODIS_S_OK);
1475 }
1476 
1477 
1478 PGSt_SMF_status read_sds_rankn (int32 sd_id,
1479  char *sds_name,
1480  int32 data_type,
1481  int32 rank,
1482  int32 *start,
1483  int32 *edge,
1484  void *data)
1485 /*
1486 !C**************************************************************************
1487 !Description:
1488  Read part or all of an n-dimensional SDS into a 1D buffer. If part of
1489  the SDS is to be read, then elements within a dimension must be
1490  consecutive (the "stride" is assumed to be 1 for all dimensions).
1491 
1492 !Input Parameters:
1493  int32 sd_id File ID for Science Data (SD) access.
1494  char *sds_name SDS name in the HDF file (must be non-NULL)
1495  int32 data_type Data type of the SDS (for consistency checking)
1496  int32 rank Rank of the SDS (dimensions of start and edge,
1497  for consistency checking)
1498  int32 *start Starting element index (0-based) for each dimension
1499  int32 *edge Number of elements to read in each dimension
1500 
1501 !Output Parameters:
1502  void *data One-dimension buffer holding the data.
1503 
1504 !Revision History:
1505  Revision 01.01 October 16, 2004 Razor Issue #200
1506  Casted Int32 variables in sprintf calls to "long" with the
1507  format specifier "%ld" for better code portability.
1508  Liqin Tan, SAIC GSO (ltan@saicmodis.com)
1509 
1510  Revision 01.00 November 6, 1999
1511  Initial development
1512  Jim Rogers (rogers@mst.gsfc.nasa.gov)
1513 
1514 !Team-unique Header:
1515  This software is developed by the MODIS Science Data Support
1516  Team for the National Aeronautics and Space Administration,
1517  Goddard Space Flight Center, under contract NAS5-32373.
1518 
1519 !References and Credits:
1520  Based on the L1B functions "read_sds_rank1", and 2, and 3, etc.
1521 
1522  HDF portions developed at the National Center for Supercomputing
1523  Applications at the University of Illinois at Urbana-Champaign.
1524 
1525 !Design Notes:
1526 
1527 !END********************************************************************
1528 */
1529 {
1530  int32 sds_index = 0;
1531  int32 sds_id = 0;
1532  intn result = 0;
1533 
1534  /* The following used to retrieve SDS information */
1535 
1536  char f_sds_name[MAX_NC_NAME];
1537  int32 f_rank = 0;
1538  int32 f_dim_sizes[MAX_VAR_DIMS];
1539  int32 f_data_type = 0;
1540  int32 f_nattrs = 0;
1541 
1542 
1543  /*
1544  * Check input arguments.
1545  */
1546 
1547  if (sd_id == FAIL || !sds_name || rank <= 0 || !start || !edge || !data) {
1548  errcode = MODIS_F_INVALID_ARGUMENT;
1549  sprintf(errmsgbuf, "One or more input arguments are invalid.");
1550  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, NULL, 0,
1551  "This is a code defect.", False);
1552  return errcode;
1553  }
1554 
1555  /*
1556  * Convert the name of the SDS to the index in the file.
1557  */
1558 
1559  sds_index = SDnametoindex (sd_id, sds_name);
1560  if (sds_index == FAIL) {
1561  errcode = MODIS_F_READ_ERROR;
1562  sprintf(errmsgbuf, "Could not find SDS \"%s\" in the file.",
1563  sds_name);
1564  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, "SDnametoindex", 0,
1566  return errcode;
1567  }
1568 
1569  /*
1570  * Open SDS access to the data set.
1571  */
1572 
1573  sds_id = SDselect (sd_id, sds_index);
1574  if (sds_id == FAIL) {
1575  errcode = MODIS_F_HDF_ERROR;
1576  sprintf(errmsgbuf, "Could not open access to SDS \"%s\".",
1577  sds_name);
1578  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, "SDselect", 0,
1580  return errcode;
1581  }
1582 
1583  /*
1584  * Get info about the SDS. Check the type and rank vs. inputs.
1585  */
1586 
1587  result = SDgetinfo (sds_id, f_sds_name, &f_rank, f_dim_sizes,
1588  &f_data_type, &f_nattrs);
1589  if (result == FAIL) {
1590  errcode = MODIS_F_HDF_ERROR;
1591  sprintf(errmsgbuf, "Could not retrieve info for SDS \"%s\".",
1592  sds_name);
1593  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, "SDgetinfo", 0,
1595  return errcode;
1596  }
1597  if (data_type != f_data_type) {
1598  errcode = MODIS_F_READ_ERROR;
1599  sprintf(errmsgbuf, "SDS \"%s\" has data type mismatch.\n"
1600  "File data type = %ld, expected type = %ld\n",
1601  sds_name, (long)f_data_type, (long)data_type);
1602  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, NULL, 0,
1604  return errcode;
1605  }
1606  if (rank != f_rank) {
1607  errcode = MODIS_F_READ_ERROR;
1608  sprintf(errmsgbuf, "SDS \"%s\" has matrix rank mismatch.\n"
1609  "File data rank = %ld, expected rank = %ld\n",
1610  sds_name, (long)f_rank, (long)rank);
1611  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, NULL, 0,
1613  return errcode;
1614  }
1615 
1616  /*
1617  * Read the data.
1618  */
1619 
1620  result = SDreaddata (sds_id, start, NULL, edge, data);
1621  if (result == FAIL) {
1622  SDendaccess(sds_id);
1623  errcode = MODIS_F_HDF_ERROR;
1624  sprintf(errmsgbuf, "Could not read data from SDS \"%s\".",
1625  sds_name);
1626  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, "SDreaddata", 0,
1628  return errcode;
1629  }
1630 
1631  /*
1632  * End SDS access.
1633  */
1634 
1635  result = SDendaccess(sds_id);
1636  if (result == FAIL) {
1637  errcode = MODIS_F_HDF_ERROR;
1638  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
1639  sds_name);
1640  L1BErrorMsg("read_sds_rankn", errcode, errmsgbuf, "SDendaccess", 0,
1642  return errcode;
1643  }
1644 
1645  return (MODIS_S_OK);
1646 }
1647 
1648 PGSt_SMF_status write_sds_rankn (int32 file_id,
1649  char *sds_name,
1650  int32 data_type,
1651  int32 rank,
1652  int32 *edge,
1653  char **dim_name,
1654  void *data)
1655 /*
1656 !C**************************************************************************
1657 !Description:
1658  Write all of an n-dimensional SDS into an HDF file. (The maximum
1659  rank is MAXRANK, defined below)
1660 
1661 !Input Parameters:
1662  int32 file_id File ID for Science Data (SD) access.
1663  char *sds_name SDS name in the HDF file (must be non-NULL)
1664  int32 data_type HDF Data type of the SDS
1665  int32 rank Rank of the SDS (maximum of MAXRANK)
1666  int32 *edge Array holding the size of each dimension.
1667  char **dim_name Array of dimension names.
1668  void *data One-dimension buffer holding the data.
1669 
1670 !Output Parameters:
1671  (none)
1672 
1673 !Revision History:
1674  Revision 01.01 October 16, 2004 Razor Issue #200
1675  Casted variables of type Int32 in sprintf calls to "long"
1676  with format specifier "%ld" for better code portability.
1677  Liqin Tan, SAIC GSO (ltan@saicmodis.com)
1678 
1679  Revision 01.00 November 22, 1999
1680  Initial development.
1681  Jim Rogers (rogers@mcst.gsfc.nasa.gov)
1682 
1683 !Team-unique Header:
1684  This software is developed by the MODIS Science Data Support
1685  Team for the National Aeronautics and Space Administration,
1686  Goddard Space Flight Center, under contract NAS5-32373.
1687 
1688  !References and Credits
1689  HDF portions developed at the National Center for Supercomputing
1690  Applications at the University of Illinois at Urbana-Champaign.
1691 
1692  !Design Notes:
1693 
1694 !END********************************************************************
1695 */
1696 {
1697  int i;
1698  int32 sds_id = 0;
1699  intn result = 0;
1700 #define MAXRANK 7
1701  int32 dim_id[MAXRANK];
1702  int32 start[MAXRANK];
1703  char *location = "write_sds_rankn";
1704 
1705  if (rank > MAXRANK)
1706  {
1707  errcode = MODIS_F_NOK;
1708  sprintf(errmsgbuf, "Could not create SDS \"%s\". Rank (%ld) exceeds max (%d)",
1709  sds_name, (long)rank, MAXRANK);
1710  L1BErrorMsg(location, errcode, errmsgbuf, NULL, 0,
1711  "Code bug.", False);
1712  return errcode;
1713  }
1714 
1715  for (i = 0; i < rank; i++)
1716  start[i] = 0;
1717 
1718  sds_id = SDcreate(file_id, sds_name, data_type, rank, edge);
1719  if (sds_id == FAIL) {
1720  errcode = MODIS_F_WRITE_ERROR;
1721  sprintf(errmsgbuf, "Could not create SDS \"%s\" in the file.",
1722  sds_name);
1723  L1BErrorMsg(location, errcode, errmsgbuf, "SDcreate", 0,
1724  corruptoutputfile, False);
1725  return errcode;
1726  }
1727 
1728  for (i = 0; i < rank; i++)
1729  {
1730  dim_id[i] = SDgetdimid(sds_id, i);
1731  if (dim_id[i] == FAIL) {
1732  errcode = MODIS_F_HDF_ERROR;
1733  sprintf(errmsgbuf, "Could not retrieve dimension #%d id for SDS \"%s\".",
1734  (i+1), sds_name);
1735  L1BErrorMsg(location, errcode, errmsgbuf, "SDgetdimid", 0,
1736  corruptoutputfile, False);
1737  SDendaccess(sds_id);
1738  return errcode;
1739  }
1740 
1741  result = SDsetdimname (dim_id[i], dim_name[i]);
1742  if (result == FAIL) {
1743  errcode = MODIS_F_HDF_ERROR;
1744  sprintf(errmsgbuf, "Could not set dimension #%d name for SDS \"%s\".",
1745  (i+1), sds_name);
1746  L1BErrorMsg(location, errcode, errmsgbuf, "SDsetdimname", 0,
1747  corruptoutputfile, False);
1748  SDendaccess(sds_id);
1749  return errcode;
1750  }
1751  }
1752 
1753  result = SDwritedata(sds_id, start, NULL, edge, data);
1754  if (result == FAIL) {
1755  errcode = MODIS_F_WRITE_ERROR;
1756  sprintf(errmsgbuf, "Could not write data to SDS \"%s\".",
1757  sds_name);
1758  L1BErrorMsg(location, errcode, errmsgbuf, "SDwritedata", 0,
1759  nowriteoutputfile, False);
1760  SDendaccess(sds_id);
1761  return errcode;
1762  }
1763 
1764  result = SDendaccess(sds_id);
1765  if (result == FAIL) {
1766  errcode = MODIS_F_HDF_ERROR;
1767  sprintf(errmsgbuf, "Could not end access to SDS \"%s\".",
1768  sds_name);
1769  L1BErrorMsg(location, errcode, errmsgbuf, "SDendaccess", 0,
1770  corruptoutputfile, False);
1771  return errcode;
1772  }
1773 
1774  return(MODIS_S_OK);
1775 }
1776 
1777 
1778 #include <math.h> /* for the fabs function */
1779 #include <stdlib.h> /* for the atoi, atof and atol functions */
1780 
1781 
1782 PGSt_SMF_status Check_Valid_Range (char *data_name,
1783  int32 data_type,
1784  char *a_lb,
1785  char *a_ub,
1786  char *a_fillvalue,
1787  int32 count,
1788  void *buffer)
1789 /*
1790 !C**************************************************************************
1791 !Description:
1792  Check that all values of an array are within a valid range or are a fill
1793  value (which could be outside the defined valid range). If all values
1794  meet the above described criteria, then return MODIS_S_OK. If any value
1795  is found not to meet the above criteria, then write an error message and
1796  return a value of MODIS_F_OUT_OF_RANGE. This function only checks
1797  integer or floating point types, not CHAR8 arrays.
1798 
1799 !Input Parameters:
1800  char *data_name Name the data item, for writing to error message
1801  int32 data_type DFNT type of data in the array (should be one of
1802  the integer or floating types, not CHAR8).
1803  char *a_lb ASCII representation of the lower bound on the
1804  values (NULL means no lower bound)
1805  char *a_ub ASCII representation of the upper bound on the
1806  valid range of values (NULL means no upper bound)
1807  char *a_fillvalue ASCII representation of the fill value on the
1808  valid range of values (NULL means no fill value)
1809  int32 count Size of array
1810  void *buffer buffer holding the value(s) of the array
1811 
1812 !Output Parameters:
1813  (none)
1814 
1815 !Revision History:
1816 
1817  Revision 1.0.2 March 27, 2003, Razor Issue #173
1818  In macros CHECK_FVALS_ON_LB and CHECK_FVALS_ON_UB, remove the length character
1819  "l" in the type code "%lf" of the double type array element data[i] for ANSI-C
1820  compliance.
1821  Liqin Tan, SAIC GSO (ltan@saicmodis.com)
1822 
1823  Revision 1.0.1 January 17, 2002 Razor Issue #172
1824  Improve portability of code to 64-bit mode.
1825  Change variable i to type long
1826  Rework macros so that explicit casts are used based on data type
1827  (especially ASSIGN_INT_VALUE)
1828  Cast atol output to int32
1829  Alice Isaacman, SAIC GSO (Alice.R.Isaacman.1@gsfc.nasa.gov)
1830 
1831  Revision 01.00 November 6, 1999
1832  Initial development
1833  Jim Rogers (rogers@mst.gsfc.nasa.gov)
1834 
1835 !Team-unique Header:
1836  This software is developed by the MODIS Science Data Support
1837  Team for the National Aeronautics and Space Administration,
1838  Goddard Space Flight Center, under contract NAS5-32373.
1839 
1840 !References and Credits:
1841  HDF portions developed at the National Center for Supercomputing
1842  Applications at the University of Illinois at Urbana-Champaign.
1843 
1844 !Design Notes:
1845  The lower bound, upper bound and fill values are ASCII representations
1846  of the actual numbers (e.g., ".011"). atol or atof are used to convert
1847  to actual values consistent with the data type of the attribute.
1848  Checking of values is not accomplished on char type attributes.
1849  The fill value may be any one value that lies outside the valid range.
1850 
1851 !END********************************************************************
1852 */
1853 {
1854  long i;
1855  char *fvptr;
1856  char *unknown_data_name = "(unknown)";
1857  double del, absd, absdfv;
1858 
1859 /***************** MACRO ASSIGN_INT_VALUE(aptr,a,alb,aub)*****************
1860 For one of the integer types holding less than 32 bits precision, check
1861 the input value against the limits of the precision of the variable.
1862 If OK, then assign to the variable (implicit cast occurs). Note: the
1863 atol operation is assumed done with at least 32 bits of precision.
1864 Global variables used:
1865  errcode, errmsgbuf
1866 Function variables used:
1867  i, data_name
1868 Macro variables:
1869  aptr = pointer to the string holding the value.
1870  a = variable to assign
1871  alb = lower extremum of data type range
1872  aub = upper extremum of data type range
1873 **************************************************************************/
1874 
1875 #define ASSIGN_INT_VALUE(aptr, a, alb, aub, type_of_data) \
1876  i = atol(aptr); \
1877  if (i < (int32) alb || i > (int32) aub) { \
1878  errcode = MODIS_F_NOK; \
1879  sprintf(errmsgbuf, \
1880  "Checking valid range of \"%s\", the value \"%s\" is not\n" \
1881  "a valid number for the precision of the data type.", \
1882  data_name, aptr); \
1883  L1BErrorMsg("Check_Valid_Range", errcode, errmsgbuf, NULL, 0, \
1884  "Code defect.", False); \
1885  return errcode; \
1886  } \
1887  else \
1888  { \
1889  switch (type_of_data) \
1890  { case DFNT_INT8: a = (int8) i; break; \
1891  case DFNT_UINT8: a = (uint8) i; break; \
1892  case DFNT_INT16: a = (int16) i; break; \
1893  case DFNT_UINT16: a = (uint16) i; break; \
1894  case DFNT_INT32: a = (int32) i; break; \
1895  case DFNT_UINT32: a = (uint32) i; \
1896  } \
1897  }
1898 
1899 /********************* MACRO CHECK_IVALS_ON_LB ************
1900 Loop through one of the integer type arrays and check each value
1901 against the lower bound and the fill value.
1902 Global variables used:
1903  errcode, errmsgbuf
1904 Function variables used:
1905  i, data_name, a_lb, data, lb, fillvalue
1906 **********************************************************************/
1907 #define CHECK_IVALS_ON_LB \
1908 for (i = 0; i < count; i++) { \
1909  if (data[i] < lb && data[i] != fillvalue) { \
1910  errcode = MODIS_F_OUT_OF_RANGE; \
1911  sprintf(errmsgbuf, \
1912  "One or more values of \"%s\" is less than the lower bound.\n" \
1913  "value[%ld] = %ld\nlower bnd = %s\n", \
1914  data_name, i, (long) data[i], a_lb); \
1915  L1BErrorMsg("Check_Valid_Range", errcode, errmsgbuf, NULL, 0, \
1916  invalidinputfile, False); \
1917  return errcode; \
1918  } \
1919 }
1920 
1921 /********************* MACRO CHECK_FVALS_ON_LB ************
1922 Loop through one of the floating point type arrays and check each value
1923 against the lower bound and the fill value.
1924 Global variables used:
1925  errcode, errmsgbuf
1926 Function variables used:
1927  i, data_name, a_lb, absd, absdfv, del, data, lb, fillvalue
1928 **********************************************************************/
1929 #define CHECK_FVALS_ON_LB \
1930 for (i = 0; i < count; i++) { \
1931  absd = fabs((double) data[i]); absdfv = fabs((double)fillvalue); \
1932  del = fabs((double) (data[i] - fillvalue)); \
1933  if (absd > 0) del /= absd; else if(absdfv > 0) del /= absdfv; \
1934  if (data[i] < lb && del > 1.e-05) { \
1935  errcode = MODIS_F_OUT_OF_RANGE; \
1936  sprintf(errmsgbuf, \
1937  "One or more values of \"%s\" is less than the lower bound.\n" \
1938  "value[%ld] = %f\nlower bnd = %s\n", \
1939  data_name, i, (double) data[i], a_lb); \
1940  L1BErrorMsg("Check_Valid_Range", errcode, errmsgbuf, NULL, 0, \
1941  invalidinputfile, False); \
1942  return errcode; \
1943  } \
1944 }
1945 
1946 /********************* MACRO CHECK_IVALS_ON_UB ************
1947 Loop through one of the integer type arrays and check each value
1948 against the lower bound and the fill value.
1949 Global variables used:
1950  errcode, errmsgbuf
1951 Function variables used:
1952  i, data_name, a_ub, data, ub, fillvalue
1953 **********************************************************************/
1954 #define CHECK_IVALS_ON_UB \
1955 for (i = 0; i < count; i++) { \
1956  if (data[i] > ub && data[i] != fillvalue) { \
1957  errcode = MODIS_F_OUT_OF_RANGE; \
1958  sprintf(errmsgbuf, \
1959  "One or more values of \"%s\" is greater than the upper bound.\n" \
1960  "value[%ld] = %ld\nupper bnd = %s\n", \
1961  data_name, i, (long) data[i], a_ub); \
1962  L1BErrorMsg("Check_Valid_Range", errcode, errmsgbuf, NULL, 0, \
1963  invalidinputfile, False); \
1964  return errcode; \
1965  } \
1966 }
1967 
1968 /********************* MACRO CHECK_FVALS_ON_UB ************
1969 Loop through one of the floating point type arrays and check each value
1970 against the lower bound and the fill value.
1971 Global variables used:
1972  errcode, errmsgbuf
1973 Function variables used:
1974  i, data_name, a_ub, absd, absdfv, del, data, ub, fillvalue
1975 **********************************************************************/
1976 #define CHECK_FVALS_ON_UB \
1977 for (i = 0; i < count; i++) { \
1978  absd = fabs((double) data[i]); absdfv = fabs((double)fillvalue); \
1979  del = fabs((double) (data[i] - fillvalue)); \
1980  if (absd > 0) del /= absd; else if(absdfv > 0) del /= absdfv; \
1981  if (data[i] > ub && del > 1.e-05) { \
1982  errcode = MODIS_F_OUT_OF_RANGE; \
1983  sprintf(errmsgbuf, \
1984  "One or more values of \"%s\" is greater than the upper bound.\n" \
1985  "value[%ld] = %f\nupper bnd = %s\n", \
1986  data_name, i, (double) data[i], a_ub); \
1987  L1BErrorMsg("Check_Valid_Range", errcode, errmsgbuf, NULL, 0, \
1988  invalidinputfile, False); \
1989  return errcode; \
1990  } \
1991 }
1992 
1993  /*
1994  * Check that array exists.
1995  */
1996 
1997  if (count <= 0 || !buffer) {
1998  errcode = MODIS_F_INVALID_ARGUMENT;
1999  sprintf(errmsgbuf, "Input arguments are invalid. \"count\" is zero "
2000  "or \"buffer\" is NULL.");
2001  L1BErrorMsg("Check_Valid_Range", errcode, errmsgbuf, NULL, 0,
2002  "Code defect (check that data buffer pointer is assigned).",
2003  False);
2004  return errcode;
2005  }
2006 
2007  /*
2008  * The data_name should be non-NULL. If not, set it to a default value
2009  * (only important for writing error messages).
2010  */
2011 
2012 
2013  if (!data_name) data_name = unknown_data_name;
2014 
2015  /*
2016  * Check values against lower bound.
2017  */
2018 
2019  if (a_lb) {
2020 
2021  /*
2022  * Set the temporary fillvalue pointer (fvptr) to a valid value.
2023  */
2024 
2025  if (a_fillvalue)
2026  fvptr = a_fillvalue;
2027  else
2028  fvptr = a_lb;
2029 
2030  /*
2031  * Step through each data type and perform checking.
2032  */
2033 
2034  if (data_type == DFNT_INT8) {
2035  int8 *data = (int8 *) buffer;
2036  int8 lb, fillvalue;
2037  ASSIGN_INT_VALUE(a_lb,lb,-128,127, data_type);
2038  ASSIGN_INT_VALUE(fvptr,fillvalue,-128,127, data_type)
2040  }
2041  else if (data_type == DFNT_UINT8) {
2042  uint8 *data = (uint8 *) buffer;
2043  uint8 lb, fillvalue;
2044  ASSIGN_INT_VALUE(a_lb,lb,0,256, data_type);
2045  ASSIGN_INT_VALUE(fvptr,fillvalue,0,256, data_type)
2047  }
2048  else if (data_type == DFNT_INT16) {
2049  int16 *data = (int16 *) buffer;
2050  int16 lb, fillvalue;
2051  ASSIGN_INT_VALUE(a_lb,lb,-32768,32767, data_type);
2052  ASSIGN_INT_VALUE(fvptr,fillvalue,-32768,32767, data_type)
2054  }
2055  else if (data_type == DFNT_UINT16) {
2056  uint16 *data = (uint16 *) buffer;
2057  uint16 lb, fillvalue;
2058  ASSIGN_INT_VALUE(a_lb,lb,0,65535, data_type);
2059  ASSIGN_INT_VALUE(fvptr,fillvalue,0,65535, data_type)
2061  }
2062  else if (data_type == DFNT_INT32) {
2063  int32 *data = (int32 *) buffer;
2064  int32 lb, fillvalue;
2065  lb = (int32) atol(a_lb);
2066  fillvalue = (int32) atol(fvptr);
2068  }
2069  else if (data_type == DFNT_UINT32) {
2070  uint32 *data = (uint32 *) buffer;
2071  uint32 lb, fillvalue;
2072  ASSIGN_INT_VALUE(a_lb,lb,0,2147483647, data_type);
2073  ASSIGN_INT_VALUE(fvptr,fillvalue,0,2147483647, data_type);
2075  }
2076  else if (data_type == DFNT_FLOAT32) {
2077  float32 *data = (float32 *) buffer;
2078  float32 lb, fillvalue;
2079  lb = (float32) atof(a_lb);
2080  fillvalue = (float32) atof(fvptr);
2082  }
2083  else if (data_type == DFNT_FLOAT64) {
2084  float64 *data = (float64 *) buffer;
2085  float64 lb, fillvalue;
2086  lb = atof(a_lb);
2087  fillvalue = atof(fvptr);
2089  }
2090  else {
2091  return MODIS_S_OK;
2092  }
2093  }
2094 
2095  /*
2096  * Check values against upper bound.
2097  */
2098 
2099  if (a_ub) {
2100 
2101  /*
2102  * Set the temporary fillvalue pointer (fvptr) to a valid value.
2103  */
2104 
2105  if (a_fillvalue)
2106  fvptr = a_fillvalue;
2107  else
2108  fvptr = a_ub;
2109 
2110  /*
2111  * Step through each data type and perform checking.
2112  */
2113 
2114  if (data_type == DFNT_INT8) {
2115  int8 *data = (int8 *) buffer;
2116  int8 ub, fillvalue;
2117  ASSIGN_INT_VALUE(a_ub,ub,-128,127, data_type);
2118  ASSIGN_INT_VALUE(fvptr,fillvalue,-128,127, data_type)
2120  }
2121  else if (data_type == DFNT_UINT8) {
2122  uint8 *data = (uint8 *) buffer;
2123  uint8 ub, fillvalue;
2124  ASSIGN_INT_VALUE(a_ub,ub,0,256, data_type);
2125  ASSIGN_INT_VALUE(fvptr,fillvalue,0,256, data_type)
2127  }
2128  else if (data_type == DFNT_INT16) {
2129  int16 *data = (int16 *) buffer;
2130  int16 ub, fillvalue;
2131  ASSIGN_INT_VALUE(a_ub,ub,-32768,32767, data_type);
2132  ASSIGN_INT_VALUE(fvptr,fillvalue,-32768,32767, data_type)
2134  }
2135  else if (data_type == DFNT_UINT16) {
2136  uint16 *data = (uint16 *) buffer;
2137  uint16 ub, fillvalue;
2138  ASSIGN_INT_VALUE(a_ub,ub,0,65535, data_type);
2139  ASSIGN_INT_VALUE(fvptr,fillvalue,0,65535, data_type)
2141  }
2142  else if (data_type == DFNT_INT32) {
2143  int32 *data = (int32 *) buffer;
2144  int32 ub, fillvalue;
2145  ub = (int32) atol(a_ub);
2146  fillvalue = (int32) atol(fvptr);
2148  }
2149  else if (data_type == DFNT_UINT32) {
2150  uint32 *data = (uint32 *) buffer;
2151  uint32 ub, fillvalue;
2152  ASSIGN_INT_VALUE(a_ub,ub,0,2147483647, data_type);
2153  ASSIGN_INT_VALUE(fvptr,fillvalue,0,2147483647, data_type);
2155  }
2156  else if (data_type == DFNT_FLOAT32) {
2157  float32 *data = (float32 *) buffer;
2158  float32 ub, fillvalue;
2159  ub = (float32) atof(a_ub);
2160  fillvalue = (float32) atof(fvptr);
2162  }
2163  else if (data_type == DFNT_FLOAT64) {
2164  float64 *data = (float64 *) buffer;
2165  float64 ub, fillvalue;
2166  ub = atof(a_ub);
2167  fillvalue = atof(fvptr);
2169  }
2170  else {
2171  return MODIS_S_OK;
2172  }
2173  }
2174  return MODIS_S_OK;
2175 }
2176 
#define MODIS_S_OK
integer, parameter int16
Definition: cubeio.f90:3
#define MODIS_F_WRITE_ERROR
char * invalidinputfile
Definition: HDF_Lib.c:18
PGSt_SMF_status write_sds_rank3(int32 file_id, char *sds_name, char *dim_name1, char *dim_name2, char *dim_name3, int32 dim1, int32 dim2, int32 dim3, char *datatype, void *data)
Definition: HDF_Lib.c:1192
#define FAIL
Definition: ObpgReadGrid.h:18
#define MAX_NC_NAME
Definition: Granule.h:9
#define NULL
Definition: decode_rs.h:63
#define MODIS_F_OUT_OF_RANGE
PGSt_SMF_status read_sds_rank3(int32 file_id, char *sds_name, int32 dim1, int32 dim2, int32 dim3, void *data)
Definition: HDF_Lib.c:546
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT32
PGSt_SMF_status write_sds_rank2(int32 file_id, char *sds_name, char *dim_name1, char *dim_name2, int32 dim1, int32 dim2, char *datatype, void *data)
Definition: HDF_Lib.c:1062
#define CHECK_FVALS_ON_LB
PGSt_SMF_status write_sds_rank1(int32 file_id, char *sds_name, char *dim_name, int32 dim, char *datatype, void *data)
Definition: HDF_Lib.c:944
PGSt_SMF_status read_sds_rank1(int32 file_id, char *sds_name, int32 dim, void *data)
Definition: HDF_Lib.c:359
#define MODIS_F_READ_ERROR
PGSt_SMF_status Check_Valid_Range(char *data_name, int32 data_type, char *a_lb, char *a_ub, char *a_fillvalue, int32 count, void *buffer)
Definition: HDF_Lib.c:1782
PGSt_SMF_status read_part_sds_rank3(int32 sd_id, char *sds_name, int32 start0, int32 start1, int32 start2, int32 edge0, int32 edge1, int32 edge2, void *data)
Definition: HDF_Lib.c:249
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
#define MAXRANK
PGSt_SMF_status read_attribute(int32 s_id, char *attr_name, int32 TypeID, void *buffer)
Definition: HDF_Lib.c:33
#define MODIS_F_NOK
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
PGSt_SMF_status write_sds_rank4(int32 file_id, char *sds_name, char *dim_name1, char *dim_name2, char *dim_name3, char *dim_name4, int32 dim1, int32 dim2, int32 dim3, int32 dim4, char *datatype, void *data)
Definition: HDF_Lib.c:1329
PGSt_SMF_status write_sds_rankn(int32 file_id, char *sds_name, int32 data_type, int32 rank, int32 *edge, char **dim_name, void *data)
Definition: HDF_Lib.c:1648
PGSt_SMF_status read_sds_rank2(int32 file_id, char *sds_name, int32 dim1, int32 dim2, void *data)
Definition: HDF_Lib.c:449
char * corruptinputfile
Definition: HDF_Lib.c:20
#define CHECK_FVALS_ON_UB
PGSt_SMF_status read_sds_rank4(int32 file_id, char *sds_name, int32 dim1, int32 dim2, int32 dim3, int32 dim4, void *data)
Definition: HDF_Lib.c:644
Extra metadata that will be written to the HDF4 file l2prod rank
#define MODIS_F_HDF_ERROR
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_FLOAT32
void L1BErrorMsg(char *L1B_location, PGSt_SMF_code code, char *input_message, char *assoc_function, int32 lun, char *other_msg, boolean error_out)
Definition: Granule.c:918
#define CHECK_IVALS_ON_LB
#define CHECK_IVALS_ON_UB
PGSt_SMF_status read_vdata(int32 v_id, int32 start_record, int32 records, char *vname, char *fname, void *buffer)
Definition: HDF_Lib.c:748
int i
Definition: decode_rs.h:71
#define MODIS_F_INVALID_ARGUMENT
#define MAX_VAR_DIMS
Definition: Granule.h:10
PGSt_SMF_status read_sds_rankn(int32 sd_id, char *sds_name, int32 data_type, int32 rank, int32 *start, int32 *edge, void *data)
Definition: HDF_Lib.c:1478
PGSt_SMF_status read_part_sds_rank2(int32 sd_id, char *sds_name, int32 start0, int32 start1, int32 edge0, int32 edge1, void *data)
Definition: HDF_Lib.c:150
#define False
Definition: Granule.h:538
#define ASSIGN_INT_VALUE(aptr, a, alb, aub, type_of_data)
int count
Definition: decode_rs.h:79