MISR Toolkit  1.5.1
MtkReadL2TCCloud.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkReadL2TCCloud =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2005, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 /* For strcasestr prototype in string.h on Linux64 */
18 #define _GNU_SOURCE
19 
20 #include "MisrReadData.h"
21 #include "MisrFileQuery.h"
22 #include "MisrUtil.h"
23 #include "MisrError.h"
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <hdf.h>
28 #include <HdfEosDef.h>
29 
40  const char *filename,
41  const char *gridname,
42  const char *fieldname,
43  MTKt_Region region,
44  MTKt_DataBuffer *databuf,
45  MTKt_MapInfo *mapinfo )
46 {
47  MTKt_status status; /* Return status */
48  MTKt_status status_code; /* Return status code for error macros */
49  int32 fid = FAIL; /* HDF-EOS File id */
50  intn hdfstatus; /* HDF return status */
51 
52  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
53 
54  /* Open file. */
55  fid = GDopen((char*)filename, DFACC_READ);
57 
58  /* Read data. */
59  status = MtkReadL2TCCloudFid(fid, gridname, fieldname, region, databuf, mapinfo);
60  MTK_ERR_COND_JUMP(status);
61 
62  /* Close file. */
63  hdfstatus = GDclose(fid);
65  fid = FAIL;
66 
67  return MTK_SUCCESS;
68  ERROR_HANDLE:
69  if (fid != FAIL) GDclose(fid);
70  return status_code;
71 }
72 
79  int32 fid,
80  const char *gridname,
81  const char *fieldname,
82  MTKt_Region region,
83  MTKt_DataBuffer *databuf,
84  MTKt_MapInfo *mapinfo )
85 {
86  MTKt_status status; /* Return status */
87  MTKt_status status_code; /* Return status code for error macros */
89  /* Map info structure */
91  /* Data buffer structure */
93  /* Raw data buffer structure */
95  /* Minimum for unscaling */
97  /* Maximum for unscaling */
99  /* Offset for unscaling */
101  /* Scale for unscaling */
103  /* Fill flag value */
104  char *field; /* Pointer to field to read */
105  char fieldstr[MAXSTR]; /* Field string */
106  char *fieldarr[2]; /* Field array */
107  char *sp; /* Pointer to string */
108  char attr[MAXSTR]; /* Attribute name */
109  int attrcnt = 0; /* Attribute count */
110  MTKt_boolean flag; /* Flag field requested */
111  MTKt_boolean raw; /* Raw field requested */
112  int l; /* Line index */
113  int s; /* Sample index */
114  char *basefield = NULL; /* Base fieldname */
115  int nextradims; /* Number of extra dimensions */
116  int *extradims = NULL; /* Extra dimension list */
117 
118  if (gridname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
119  if (fieldname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
120 
121  /* ---------------------------------------------------------- */
122  /* Parse fieldname to determine unpacking or unscaling method */
123  /* ---------------------------------------------------------- */
124 
125  /* Make a working copy of fieldname */
126  strncpy(fieldstr, fieldname, MAXSTR);
127  fieldarr[0] = fieldstr;
128 
129  /* Separate raw/flag from base field */
130  if ((sp = strchr(fieldstr, ' ')) != NULL) {
131  *sp = '\0';
132  fieldarr[1] = ++sp;
133  } else if ((sp = strchr(fieldstr, '\0')) != NULL) {
134  fieldarr[1] = sp;
135  } else {
137  }
138 
139  /* ------------------------------------------------ */
140  /* Determine if raw/native or flags is in fieldname */
141  /* ------------------------------------------------ */
142 
143  if ((sp = strcasestr(fieldname, "raw")) != NULL ||
144  (sp = strcasestr(fieldname, "native")) != NULL) {
145  flag = MTK_FALSE;
146  raw = MTK_TRUE;
147  field = fieldarr[1];
148  } else if ((sp = strcasestr(fieldname, "flag")) != NULL) {
149  flag = MTK_TRUE;
150  raw = MTK_FALSE;
151  field = fieldarr[1];
152  } else {
153  flag = MTK_FALSE;
154  raw = MTK_FALSE;
155  field = fieldarr[0];
156  }
157 
158  /* -------------------------------------------------------------------- */
159  /* Read scale, offset, fill, underflow and overflow value, if available */
160  /* -------------------------------------------------------------------- */
161 
162  status = MtkParseFieldname(field, &basefield, &nextradims, &extradims);
163  MTK_ERR_COND_JUMP(status);
164  // int num_attrs; /* Number of attributes */
165 // char **attrlist; /* Attribute list */
166 /* status = MtkFieldAttrListFid(fid,"CloudMotionCrossTrack",&num_attrs,&attrlist);
167  status = MtkFieldAttrGetFid(fid, "CloudMotionCrossTrack", "scale_factor", &scale); */
168 
169  if (raw == MTK_FALSE) {
170  strcpy(attr, "valid_min");
171  status = MtkFieldAttrGetFid(fid, basefield, attr, &min);
172  if (status == MTK_SUCCESS) attrcnt++;
173 
174  strcpy(attr, "valid_max");
175  status = MtkFieldAttrGetFid(fid, basefield, attr, &max);
176  if (status == MTK_SUCCESS) attrcnt++;
177 
178  strcpy(attr, "scale_factor");
179  status = MtkFieldAttrGetFid(fid, basefield, attr, &scale);
180  if (status == MTK_SUCCESS) attrcnt++;
181 
182  strcpy(attr, "add_offset");
183  status = MtkFieldAttrGetFid(fid, basefield, attr, &offset);
184  if (status == MTK_SUCCESS) attrcnt++;
185 
186  strcpy(attr, "_FillValue");
187  status = MtkFieldAttrGetFid(fid, basefield, attr, &fill);
188  if (status == MTK_SUCCESS) attrcnt++;
189  }
190 
191  /* --------------------------------------------------------------- */
192  /* Determine which unpacking or unscaling method to do, then do it */
193  /* --------------------------------------------------------------- */
194 
195 
196  if (attrcnt == 5 && flag == MTK_FALSE && raw == MTK_FALSE) {
197 
198  status = MtkReadRawFid(fid, gridname, field, region, &rawbuf, &map);
199  MTK_ERR_COND_JUMP(status);
200 
201  status = MtkDataBufferAllocate(rawbuf.nline, rawbuf.nsample, MTKe_float,
202  &buf);
203  MTK_ERR_COND_JUMP(status);
204 
205  switch(rawbuf.datatype) {
206  case MTKe_uint8:
207  for (l = 0; l < buf.nline; l++) {
208  for (s = 0; s < buf.nsample; s++) {
209  if (rawbuf.data.u8[l][s] == fill.data.u8[0][0]) {
210  buf.data.f[l][s] = (MTKt_float)fill.data.u8[0][0];
211  } else {
212  buf.data.f[l][s] = (MTKt_float)(rawbuf.data.u8[l][s] *
213  scale.data.f[0][0]) + offset.data.f[0][0];
214  }
215  }
216  }
217  break;
218  case MTKe_uint16:
219  for (l = 0; l < buf.nline; l++) {
220  for (s = 0; s < buf.nsample; s++) {
221  if (rawbuf.data.u16[l][s] == fill.data.u16[0][0]) {
222  buf.data.f[l][s] = (MTKt_float)fill.data.u16[0][0];
223  } else {
224  buf.data.f[l][s] = (MTKt_float)(rawbuf.data.u16[l][s] *
225  scale.data.f[0][0]) + offset.data.f[0][0];
226  }
227  }
228  }
229  break;
230 
231  case MTKe_int16:
232  for (l = 0; l < buf.nline; l++) {
233  for (s = 0; s < buf.nsample; s++) {
234  if (rawbuf.data.i16[l][s] == fill.data.i16[0][0]) {
235  buf.data.f[l][s] = (MTKt_float)fill.data.i16[0][0];
236  } else {
237  buf.data.f[l][s] = (MTKt_float)(rawbuf.data.i16[l][s] *
238  scale.data.f[0][0]) + offset.data.f[0][0];
239  }
240  }
241  }
242  if ((sp = strcasestr(basefield, "ERImageContrast_WithoutWindCorrection")) != NULL ) {
243  for (l = 0; l < buf.nline; l++) {
244  for (s = 0; s < buf.nsample; s++) {
245  if (rawbuf.data.i16[l][s] == fill.data.i16[0][0]) {
246  buf.data.f[l][s] = (MTKt_float)fill.data.i16[0][0];
247  } else {
248  buf.data.f[l][s] = (MTKt_float)pow(10.0,(buf.data.f[l][s]));
249  }
250  }
251  }
252  }
253  break;
254 
255  default:
257  break;
258  }
259 
260  /* -------------------------------------------------------------- */
261  /* Read all other fields that don't require unpacking/ unscaling. */
262  /* -------------------------------------------------------------- */
263 
264  } else if (flag == MTK_FALSE) {
265 
266  status = MtkReadRawFid(fid, gridname, field, region, &buf, &map);
267  MTK_ERR_COND_JUMP(status);
268 
269  } else {
271  }
272 
273  free(basefield);
274  free(extradims);
275  MtkDataBufferFree(&rawbuf);
276  MtkDataBufferFree(&min);
277  MtkDataBufferFree(&max);
278  MtkDataBufferFree(&offset);
279  MtkDataBufferFree(&scale);
280  MtkDataBufferFree(&fill);
281 
282  *databuf = buf;
283  *mapinfo = map;
284 
285  return MTK_SUCCESS;
286  ERROR_HANDLE:
287  if (basefield != NULL) free(basefield);
288  if (extradims != NULL) free(extradims);
289  MtkDataBufferFree(&buf);
290  MtkDataBufferFree(&rawbuf);
291  MtkDataBufferFree(&min);
292  MtkDataBufferFree(&max);
293  MtkDataBufferFree(&offset);
294  MtkDataBufferFree(&scale);
295  MtkDataBufferFree(&fill);
296  return status_code;
297 }
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_DataBufferType data
Definition: MisrUtil.h:104
intn GDclose(int32)
#define MTKT_MAPINFO_INIT
Definition: MisrMapQuery.h:79
MTKt_status MtkFieldAttrGetFid(int32 sds_id, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFieldAttrGet that takes an HDF SD file identifier rather than a filename.
MTKt_status MtkParseFieldname(const char *fieldname, char **basefieldname, int *ndim, int **dimlist)
Parses extra dimensions from fieldnames.
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
Map Information.
Definition: MisrMapQuery.h:65
MTKt_boolean
Definition: MisrError.h:6
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_DataType datatype
Definition: MisrUtil.h:102
MTKt_uint8 ** u8
Definition: MisrUtil.h:86
HDFFCLIBAPI int32 float32 * max
MTKt_uint16 ** u16
Definition: MisrUtil.h:88
MTKt_status MtkReadL2TCCloud(const char *filename, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Reads, unpacks and unscales TC_CLOUD grid/fields from a MISR L2 TC_CLOUD product file.
HDFFCLIBAPI intf void * scale
MTKt_status MtkReadRawFid(int32 fid, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Version of MtkReadRaw that takes an HDF-EOS file identifier rather than a filename.
Definition: MtkReadRaw.c:141
MTKt_status MtkReadL2TCCloudFid(int32 fid, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Version of MtkReadL2TCCloud that takes and HDF-EOS file identifier rather than a filename.
float MTKt_float
Definition: MisrUtil.h:76
#define MAXSTR
Definition: MisrUtil.h:28
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
HDFFCLIBAPI intf * offset
Region of interest.
Definition: MisrSetRegion.h:41
int32 GDopen(char *, intn)
MTKt_int16 ** i16
Definition: MisrUtil.h:87
HDFFCLIBAPI void * min
HDFFCLIBAPI _fcd field
HDFFCLIBAPI intf * buf
#define FAIL
Definition: hdf.h:94

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