MISR Toolkit  1.5.1
MtkTimeMetaRead.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkTimeMetaRead =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2006, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrFileQuery.h"
18 #include "MisrUtil.h"
19 #include "MisrError.h"
20 #include <string.h>
21 #include <hdf.h>
22 #include <mfhdf.h>
23 
40  const char *filename,
41  MTKt_TimeMetaData *time_metadata )
42 {
43  MTKt_status status; /* Return status */
44  MTKt_status status_code; /* Return code of this function */
45  int32 hdf_status; /* HDF-EOS return status */
46  int32 sd_id = FAIL; /* HDF SD file identifier. */
47  int32 hdf_id = FAIL;
48 
49  if (filename == NULL)
51 
52  /* Open HDF File */
53  hdf_id = HDFopen(filename, DFACC_READ, 0);
54  if (hdf_id == FAIL)
56 
57  /* Open HDF File */
58  hdf_status = sd_id = SDstart(filename, DFACC_READ);
59  if (hdf_status == FAIL)
61 
62  /* Read time metadata. */
63  status = MtkTimeMetaReadFid(hdf_id, sd_id, time_metadata);
64  MTK_ERR_COND_JUMP(status);
65 
66  /* Close HDF File */
67  hdf_status = SDend(sd_id);
68  if (hdf_status == FAIL)
70  sd_id = FAIL;
71 
72  /* Close HDF file */
73  hdf_status = HDFclose(hdf_id);
74  if (hdf_status == FAIL)
76  hdf_id = FAIL;
77 
78  return MTK_SUCCESS;
79 
80 ERROR_HANDLE:
81  if (hdf_id != FAIL)
82  HDFclose(hdf_id);
83 
84  if (sd_id != FAIL)
85  SDend(sd_id);
86 
87  return status_code;
88 }
89 
96  int32 hdf_id,
97  int32 sd_id,
98  MTKt_TimeMetaData *time_metadata )
99 {
100  MTKt_status status; /* Return status */
101  MTKt_status status_code; /* Return code of this function */
102  int block;
103  int i;
104  MTKt_DataBuffer number_transform = MTKT_DATABUFFER_INIT;
112  const char *cam_list[9] = {"DF", "CF", "BF", "AF", "AN", "AA", "BA", "CA", "DA"};
113 
114  if (time_metadata == NULL)
116 
117  /* Read block metadata */
118  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
119  "number_transform", &number_transform);
120  MTK_ERR_COND_JUMP(status);
121 
122  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
123  "transform.ref_time", &ref_time);
124  MTK_ERR_COND_JUMP(status);
125 
126  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
127  "transform.start_line", &start_line);
128  MTK_ERR_COND_JUMP(status);
129 
130  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
131  "transform.number_line", &number_line);
132  MTK_ERR_COND_JUMP(status);
133 
134  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
135  "transform.coeff_line", &coeff_line);
136  MTK_ERR_COND_JUMP(status);
137 
138  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
139  "transform.som_ctr.x", &som_ctr_x);
140  MTK_ERR_COND_JUMP(status);
141 
142  status = MtkFileBlockMetaFieldReadFid(hdf_id, "PerBlockMetadataRad",
143  "transform.som_ctr.y", &som_ctr_y);
144  MTK_ERR_COND_JUMP(status);
145 
146  status = MtkFileToPathFid(sd_id, &time_metadata->path);
147  MTK_ERR_COND_JUMP(status);
148 
149  status = MtkFileToBlockRangeFid(sd_id, &time_metadata->start_block,
150  &time_metadata->end_block);
151  MTK_ERR_COND_JUMP(status);
152 
153  status = MtkFileAttrGetFid(sd_id, "Camera", &camera);
154  MTK_ERR_COND_JUMP(status);
155 
156  strcpy(time_metadata->camera, cam_list[camera.data.i32[0][0] - 1]);
157 
158  /* Block is 0-based when read from Per Block Metadata convert
159  to 1-based when copying to Time Metadata structure */
160 
161  memcpy(time_metadata->number_transform + 1, number_transform.dataptr,
162  sizeof(MTKt_int32) * number_transform.nline);
163 
164  for (block = 0; block < ref_time.nline; ++block)
165  {
166  /* ref_time is stored as two strings concatenated together, split
167  when copying into Time Metadata structure */
168 
169  strncpy(time_metadata->ref_time[block + 1][0], ref_time.data.c8[block],
170  MTKd_DATETIME_LEN - 1);
171  time_metadata->ref_time[block + 1][0][MTKd_DATETIME_LEN - 1] = '\0';
172 
173  strncpy(time_metadata->ref_time[block + 1][1], ref_time.data.c8[block] + MTKd_DATETIME_LEN - 1,
174  MTKd_DATETIME_LEN - 1);
175  time_metadata->ref_time[block + 1][1][MTKd_DATETIME_LEN - 1] = '\0';
176  }
177 
178  memcpy(time_metadata->start_line + 1, start_line.dataptr,
179  sizeof(MTKt_int32) * start_line.nline * start_line.nsample);
180 
181  memcpy(time_metadata->number_line + 1, number_line.dataptr,
182  sizeof(MTKt_int32) * number_line.nline * number_line.nsample);
183 
184  for (block = 0; block < coeff_line.nline; ++block)
185  for (i = 0; i < 6; ++i)
186  {
187  /* reorder coeff_line by grid cell */
188  time_metadata->coeff_line[block + 1][i][0] = coeff_line.data.d[block][i];
189  time_metadata->coeff_line[block + 1][i][1] = coeff_line.data.d[block][i + 6];
190  }
191 
192  memcpy(time_metadata->som_ctr_x + 1, som_ctr_x.dataptr,
193  sizeof(MTKt_double) * som_ctr_x.nline * som_ctr_x.nsample);
194 
195  memcpy(time_metadata->som_ctr_y + 1, som_ctr_y.dataptr,
196  sizeof(MTKt_double) * som_ctr_y.nline * som_ctr_y.nsample);
197 
198  MtkDataBufferFree(&number_transform);
199  MtkDataBufferFree(&ref_time);
200  MtkDataBufferFree(&start_line);
201  MtkDataBufferFree(&number_line);
202  MtkDataBufferFree(&coeff_line);
203  MtkDataBufferFree(&som_ctr_x);
204  MtkDataBufferFree(&som_ctr_y);
205  MtkDataBufferFree(&camera);
206 
207  return MTK_SUCCESS;
208 
209 ERROR_HANDLE:
210  MtkDataBufferFree(&number_transform);
211  MtkDataBufferFree(&ref_time);
212  MtkDataBufferFree(&start_line);
213  MtkDataBufferFree(&number_line);
214  MtkDataBufferFree(&coeff_line);
215  MtkDataBufferFree(&som_ctr_x);
216  MtkDataBufferFree(&som_ctr_y);
217  MtkDataBufferFree(&camera);
218 
219  return status_code;
220 }
MTKt_double ** d
Definition: MisrUtil.h:94
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_int32 number_transform[NBLOCK+1]
Definition: MisrFileQuery.h:82
MTKt_DataBufferType data
Definition: MisrUtil.h:104
int32_t MTKt_int32
Definition: MisrUtil.h:72
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_double som_ctr_y[NBLOCK+1][NGRIDCELL]
Definition: MisrFileQuery.h:88
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_char8 ref_time[NBLOCK+1][NGRIDCELL][MTKd_DATETIME_LEN]
Definition: MisrFileQuery.h:83
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
MTKt_double coeff_line[NBLOCK+1][6][NGRIDCELL]
Definition: MisrFileQuery.h:86
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_char8 ** c8
Definition: MisrUtil.h:83
#define HDFopen(f, a, d)
Definition: hdf.h:160
MTKt_status MtkFileToBlockRangeFid(int32 sid, int *start_block, int *end_block)
Version of MtkFileToBlockRange that takes an HDF SD file identifier rather than a filename...
HDFLIBAPI intn SDend(int32 fid)
MTKt_int32 number_line[NBLOCK+1][NGRIDCELL]
Definition: MisrFileQuery.h:85
MTKt_status MtkTimeMetaReadFid(int32 hdf_id, int32 sd_id, MTKt_TimeMetaData *time_metadata)
Version of MtkTimeMetaRead that takes an HDF SD file identifier and HDF file identifier rather than a...
MTKt_status MtkFileToPathFid(int32 sid, int *path)
Version of MtkFileToPath that takes an HDF SD identifier rather than a filename.
MTKt_double som_ctr_x[NBLOCK+1][NGRIDCELL]
Definition: MisrFileQuery.h:87
MTKt_status MtkFileBlockMetaFieldReadFid(int32 file_id, const char *blockmetaname, const char *fieldname, MTKt_DataBuffer *blockmetabuf)
Version of MtkFileBlockMetaFileRead that takes an HDF file identifier rather than a filename...
MTKt_status MtkTimeMetaRead(const char *filename, MTKt_TimeMetaData *time_metadata)
Read time metadata from L1B2 Ellipsoid product file.
MTKt_int32 end_block
Definition: MisrFileQuery.h:80
MTKt_char8 camera[3]
Definition: MisrFileQuery.h:81
Time Metadata.
Definition: MisrFileQuery.h:77
MTKt_int32 start_line[NBLOCK+1][NGRIDCELL]
Definition: MisrFileQuery.h:84
MTKt_int32 ** i32
Definition: MisrUtil.h:89
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkFileAttrGetFid(int32 sds_id, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFileAttrGet that takes an HDF SD file identifier rather than a filename.
double MTKt_double
Definition: MisrUtil.h:77
MTKt_int32 start_block
Definition: MisrFileQuery.h:79
#define MTKd_DATETIME_LEN
Definition: MisrUtil.h:149
#define FAIL
Definition: hdf.h:94
#define HDFclose(f)
Definition: hdf.h:161

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51