MISR Toolkit  1.5.1
MtkReadRaw.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkReadRaw =
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 #include "MisrReadData.h"
18 #include "MisrCoordQuery.h"
19 #include "MisrFileQuery.h"
20 #include "MisrError.h"
21 #include "MisrCache.h"
22 #include "MisrUtil.h" /* NOTE: remove when below note is removed */
23 #include "misrproj.h"
24 #include <hdf.h>
25 #include <HdfEosDef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
50  const char *filename,
51  const char *gridname,
52  const char *fieldname,
53  MTKt_Region region,
54  MTKt_DataBuffer *databuf,
55  MTKt_MapInfo *mapinfo )
56 {
57  MTKt_status status; /* Return status */
58 
59  status = MtkReadRawNC(filename, gridname, fieldname, region, databuf, mapinfo); // try netCDF
60  if (status != MTK_NETCDF_OPEN_FAILED) return status;
61 
62  return MtkReadRawHDF(filename, gridname, fieldname, region, databuf, mapinfo); // try HDF
63 }
64 
66  const char *filename,
67  const char *gridname,
68  const char *fieldname,
69  MTKt_Region region,
70  MTKt_DataBuffer *databuf,
71  MTKt_MapInfo *mapinfo )
72 {
73  MTKt_status status; /* Return status */
74  MTKt_status status_code; /* Return status code for error macros */
75  int32 fid = FAIL; /* HDF-EOS File id */
76  intn hdfstatus; /* HDF return status */
77 
78  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
79 
80  /* Open file. */
81  fid = GDopen((char*)filename, DFACC_READ);
83 
84  /* Read data. */
85  status = MtkReadRawFid(fid, gridname, fieldname, region, databuf, mapinfo);
86  MTK_ERR_COND_JUMP(status);
87 
88  /* Close file. */
89  hdfstatus = GDclose(fid);
91  fid = FAIL;
92 
93  return MTK_SUCCESS;
94  ERROR_HANDLE:
95  if (fid != FAIL) GDclose(fid);
96  return status_code;
97 }
98 
100  const char *filename,
101  const char *gridname,
102  const char *fieldname,
103  MTKt_Region region,
104  MTKt_DataBuffer *databuf,
105  MTKt_MapInfo *mapinfo )
106 {
107  MTKt_status status; /* Return status */
108  MTKt_status status_code; /* Return status code for error macros */
109  int ncid = 0;
110 
111  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
112 
113  /* Open file. */
114  {
115  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
117  }
118 
119  /* Read data. */
120  status = MtkReadRawNcid(ncid, gridname, fieldname, region, databuf, mapinfo);
121  MTK_ERR_COND_JUMP(status);
122 
123  /* Close file. */
124  {
125  int nc_status = nc_close(ncid);
127  }
128  ncid = 0;
129 
130  return MTK_SUCCESS;
131  ERROR_HANDLE:
132  if (ncid != 0) nc_close(ncid);
133  return status_code;
134 }
135 
142  int32 fid,
143  const char *gridname,
144  const char *fieldname,
145  MTKt_Region region,
146  MTKt_DataBuffer *databuf,
147  MTKt_MapInfo *mapinfo )
148 {
149  MTKt_status status; /* Return status */
150  MTKt_status status_code; /* Return status code for error macros */
152  /* Data buffer structure */
154  /* Map info structure */
156  /* Temporary buffer for projection parameters Path_number grid attr. */
157  int path; /* Path */
158  int resolution; /* Resolution */
159  MTKt_DataType datatype; /* Datatype */
160  MTKt_Cache blockcache = MTKT_CACHE_INIT;
161  /* Block cache */
162  intn hdfstatus; /* HDF return status */
163  int32 HDFfid; /* HDF file identifier (not used) */
164  int32 sid; /* HDF SD identifier */
165 
166  if (gridname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
167  if (fieldname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
168 
169  /* Check for valid file/grid/field/dim */
170  status = MtkFileGridFieldCheckFid(fid, gridname, fieldname);
171  MTK_ERR_COND_JUMP(status);
172 
173  /* Get the HDF SD identifier. */
174  hdfstatus = EHidinfo(fid, &HDFfid, &sid);
175  if (hdfstatus == FAIL)
177 
178  /* Determine path of this filename */
179  if (strcmp("Projection_Parameters", gridname) == 0) {
180  status = MtkGridAttrGetFid(fid, gridname, "Path_number", &pathbuf);
181  MTK_ERR_COND_JUMP(status);
182  path = pathbuf.data.i32[0][0];
183  } else {
184  status = MtkFileToPathFid(sid, &path);
185  MTK_ERR_COND_JUMP(status);
186  }
187 
188  /* Determine resolution from filename and gridname */
189  status = MtkFileGridToResolutionFid(fid, gridname, &resolution);
190  MTK_ERR_COND_JUMP(status);
191 
192  /* Determine datatype from filenname, gridname and fieldname */
193  status =MtkFileGridFieldToDataTypeFid(fid, gridname, fieldname, &datatype);
194  MTK_ERR_COND_JUMP(status);
195 
196  /* Snap region to som grid for this path */
197  status = MtkSnapToGrid(path, resolution, region, &map);
198  MTK_ERR_COND_JUMP(status);
199 
200  /* Allocate the data buffer */
201  status = MtkDataBufferAllocate(map.nline, map.nsample, datatype, &buf);
202  MTK_ERR_COND_JUMP(status);
203 
204  /* Inital block cache */
205  status = MtkCacheInitFid(fid, gridname, fieldname, &blockcache);
206  MTK_ERR_COND_JUMP(status);
207 
208  {
209  int b; /* Block */
210  float l; /* Line */
211  float s; /* Sample */
212  double x; /* Som x */
213  double y; /* Som y */
214  char *dptr; /* Data pointer to buffer */
215  MTKt_MisrProjParam pp; /* Projection parameters */
216  /* (this could come from map, instead of MtkPathToProjParam call) */
217 
218  MtkPathToProjParam(map.path, map.resolution, &pp);
219  misr_init(pp.nblock, pp.nline, pp.nsample, pp.reloffset, pp.ulc, pp.lrc);
220 
221  dptr = (char *)buf.dataptr;
222  for (x = map.som.ulc.x; x <= map.som.lrc.x; x += map.resolution) {
223  for (y = map.som.ulc.y; y <= map.som.lrc.y; y += map.resolution) {
224  misrfor(x, y, &b, &l, &s);
225  MtkCachePixelGet(&blockcache, b, l, s, dptr);
226  dptr += buf.datasize;
227  }
228  }
229  }
230 
231  MtkCacheFree(&blockcache);
232 
233  *databuf = buf;
234  *mapinfo = map;
235 
236  return MTK_SUCCESS;
237  ERROR_HANDLE:
238  MtkCacheFree(&blockcache);
239  MtkDataBufferFree(&buf);
240  return status_code;
241 }
242 
249  int ncid,
250  const char *gridname,
251  const char *fieldname,
252  MTKt_Region region,
253  MTKt_DataBuffer *databuf,
254  MTKt_MapInfo *mapinfo )
255 {
256  MTKt_status status; /* Return status */
257  MTKt_status status_code; /* Return status code for error macros */
259  /* Data buffer structure */
261  /* Map info structure */
262  MTKt_DataType datatype; /* Datatype */
263  MTKt_Cache blockcache = MTKT_CACHE_INIT;
264  /* Block cache */
265 
266  if (gridname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
267  if (fieldname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
268 
269  /* Check for valid file/grid/field/dim */
270  status = MtkFileGridFieldCheckNcid(ncid, gridname, fieldname);
271  MTK_ERR_COND_JUMP(status);
272 
273  int path;
274  {
275  int nc_status = nc_get_att_int(ncid, NC_GLOBAL, "Path_number", &path);
277  }
278 
279  int resolution;
280  status = MtkFileGridToResolutionNcid(ncid, gridname, &resolution);
281  MTK_ERR_COND_JUMP(status);
282 
283  status = MtkFileGridFieldToDataTypeNcid(ncid, gridname, fieldname, &datatype);
284  MTK_ERR_COND_JUMP(status);
285 
286  /* Snap region to som grid for this path */
287  status = MtkSnapToGrid(path, resolution, region, &map);
288  MTK_ERR_COND_JUMP(status);
289 
290  /* Allocate the data buffer */
291  status = MtkDataBufferAllocate(map.nline, map.nsample, datatype, &buf);
292  MTK_ERR_COND_JUMP(status);
293 
294  /* Inital block cache */
295  status = MtkCacheInitNcid(ncid, gridname, fieldname, &blockcache);
296  MTK_ERR_COND_JUMP(status);
297 
298  {
299  int b; /* Block */
300  float l; /* Line */
301  float s; /* Sample */
302  double x; /* Som x */
303  double y; /* Som y */
304  char *dptr; /* Data pointer to buffer */
305  MTKt_MisrProjParam pp; /* Projection parameters */
306  /* (this could come from map, instead of MtkPathToProjParam call) */
307 
308  MtkPathToProjParam(map.path, map.resolution, &pp);
309  misr_init(pp.nblock, pp.nline, pp.nsample, pp.reloffset, pp.ulc, pp.lrc);
310 
311  dptr = (char *)buf.dataptr;
312  for (x = map.som.ulc.x; x <= map.som.lrc.x; x += map.resolution) {
313  for (y = map.som.ulc.y; y <= map.som.lrc.y; y += map.resolution) {
314  misrfor(x, y, &b, &l, &s);
315  status = MtkCachePixelGet(&blockcache, b, l, s, dptr);
316  if (status != MTK_SUCCESS && status != MTK_OUTBOUNDS) {
317  MTK_ERR_CODE_JUMP(status);
318  }
319  dptr += buf.datasize;
320  }
321  }
322  }
323  MtkCacheFree(&blockcache);
324 
325  *databuf = buf;
326  *mapinfo = map;
327 
328  return MTK_SUCCESS;
329  ERROR_HANDLE:
330  MtkCacheFree(&blockcache);
331  MtkDataBufferFree(&buf);
332  return status_code;
333 }
MTKt_status MtkReadRawHDF(const char *filename, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Definition: MtkReadRaw.c:65
MTKt_status MtkFileGridToResolutionNcid(int ncid, const char *gridname, int *resolution)
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
HDFFCLIBAPI _fcd _fcd intf intf * datatype
int misr_init(const int nblock, const int nline, const int nsample, const float relOff[NOFFSET], const double ulc_coord[], const double lrc_coord[])
Definition: misr_init.c:18
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_DataBufferType data
Definition: MisrUtil.h:104
intn GDclose(int32)
MTKt_DataType
Definition: MisrUtil.h:36
#define MTKT_MAPINFO_INIT
Definition: MisrMapQuery.h:79
MISR Projection Parameters.
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
EXTERNL int nc_get_att_int(int ncid, int varid, const char *name, int *ip)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_status MtkFileGridFieldToDataTypeNcid(int ncid, const char *gridname, const char *fieldname, MTKt_DataType *datatype)
Map Information.
Definition: MisrMapQuery.h:65
#define NC_GLOBAL
Definition: netcdf.h:214
2-dimensional Data Buffer
Definition: MisrUtil.h:98
#define MTKT_CACHE_INIT
Definition: MisrCache.h:46
long b
Definition: jpegint.h:371
float reloffset[179]
MTKt_status MtkFileGridFieldToDataTypeFid(int32 fid, const char *gridname, const char *fieldname, MTKt_DataType *datatype)
Version of MtkFileGridFieldToDataType that takes an HDF-EOS file id rather than a filename...
#define NC_NOERR
Definition: netcdf.h:313
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_status MtkReadRawNcid(int ncid, 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:248
int misrfor(const double x, const double y, int *block, float *line, float *sample)
Definition: misrfor.c:18
MTKt_status MtkReadRawNC(const char *filename, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Definition: MtkReadRaw.c:99
MTKt_status MtkCacheInitNcid(int ncid, const char *gridname, const char *fieldname, MTKt_Cache *cache)
Definition: MtkCache.c:79
MTKt_status MtkPathToProjParam(int path, int resolution_meters, MTKt_MisrProjParam *pp)
Get projection parameters.
MTKt_status MtkCachePixelGet(MTKt_Cache *cache, int block, float line, float sample, void *pixel)
Get pixel from cache.
Definition: MtkCache.c:134
MTKt_status MtkFileGridFieldCheckFid(int32 Fid, const char *gridname, const char *fieldname)
Version of MtkFileGridFieldCheck that takes an HDF-EOS file identifier rather than a filename...
MTKt_status MtkGridAttrGetFid(int32 fid, const char *gridname, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFileGridAttrGet that takes an HDF-EOS file ID rather than a filename.
MTKt_status MtkFileToPathFid(int32 sid, int *path)
Version of MtkFileToPath that takes an HDF SD identifier rather than a filename.
void MtkCacheFree(MTKt_Cache *cache)
Free Cache.
Definition: MtkCache.c:229
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkFileGridFieldCheckNcid(int ncid, const char *gridname, const char *fieldname)
intn EHidinfo(int32, int32 *, int32 *)
MTKt_SomCoord lrc
Definition: MisrMapQuery.h:58
MTKt_status MtkReadRaw(const char *filename, const char *gridname, const char *fieldname, MTKt_Region region, MTKt_DataBuffer *databuf, MTKt_MapInfo *mapinfo)
Reads any native grid/field from a MISR product file without unpacking or unscaling.
Definition: MtkReadRaw.c:49
Cache (Low Level)
Definition: MisrCache.h:36
MTKt_status MtkCacheInitFid(int32 fid, const char *gridname, const char *fieldname, MTKt_Cache *cache)
Initialize Cache.
Definition: MtkCache.c:33
MTKt_int32 ** i32
Definition: MisrUtil.h:89
MTKt_status MtkSnapToGrid(int path, int resolution, MTKt_Region region, MTKt_MapInfo *mapinfo)
Snap a region to a MISR grid based on path number and resolution.
Definition: MtkSnapToGrid.c:40
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_SomRegion som
Definition: MisrMapQuery.h:74
Region of interest.
Definition: MisrSetRegion.h:41
int32 GDopen(char *, intn)
EXTERNL int nc_close(int ncid)
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
MTKt_status MtkFileGridToResolutionFid(int32 fid, const char *gridname, int *resolution)
Version of MtkFileGridToResolution that takes an HDF-EOS file id rather than a filename.
HDFFCLIBAPI intf * buf
MTKt_SomCoord ulc
Definition: MisrMapQuery.h:56
#define FAIL
Definition: hdf.h:94

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