MISR Toolkit  1.5.1
MtkCache.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkCache =
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 "MisrCache.h"
18 #include "MisrFileQuery.h"
19 #include "MisrError.h"
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 
24 /* ----------------------------------------------- */
25 /* MtkCacheInit */
26 /* ----------------------------------------------- */
27 
34  const char *gridname,
35  const char *fieldname,
36  MTKt_Cache *cache ) {
37 
38  MTKt_status status; /* Return status */
39  MTKt_status status_code; /* Return code of this function */
41  /* Temp block data buffer */
42  MTKt_DataType datatype; /* Mtk datatype */
43  int i; /* Loop index */
44 
45 
46  cache->fid = fid;
47  cache->ncid = 0;
48  cache->gridname = (char *)malloc(strlen(gridname)+1);
49  strcpy(cache->gridname, gridname);
50  cache->fieldname = (char *)malloc(strlen(fieldname)+1);
51  strcpy(cache->fieldname, fieldname);
52 
53  cache->block_cnt = 0;
54 
55  /* Block cache has NBLOCK+1 elements to make it 1-based, thus i <= NBLOCK */
56 
57  for (i = 0; i <= NBLOCK; i++) {
58  cache->block[i].valid = MTK_FALSE;
59  cache->block[i].buf = buf_tmp;
60  }
61 
62  /* Set fill value, if available otherwise set to zero */
63 
64  status = MtkFillValueGetFid(fid, gridname, fieldname, &(cache->fill));
65  if (status != MTK_SUCCESS) {
66  status = MtkFileGridFieldToDataTypeFid(fid, gridname, fieldname,
67  &datatype);
68  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
69  status = MtkDataBufferAllocate(1, 1, datatype, &(cache->fill));
70  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
71  /* Calloc is used to clear the fill buffer, so zero is the default fill */
72  }
73 
74  return MTK_SUCCESS;
75  ERROR_HANDLE:
76  return status_code;
77 }
78 
80  const char *gridname,
81  const char *fieldname,
82  MTKt_Cache *cache ) {
83 
84  MTKt_status status; /* Return status */
85  MTKt_status status_code; /* Return code of this function */
87  /* Temp block data buffer */
88  MTKt_DataType datatype; /* Mtk datatype */
89  int i; /* Loop index */
90 
91 
92  cache->ncid = ncid;
93  cache->fid = FAIL;
94  cache->gridname = (char *)malloc(strlen(gridname)+1);
95  strcpy(cache->gridname, gridname);
96  cache->fieldname = (char *)malloc(strlen(fieldname)+1);
97  strcpy(cache->fieldname, fieldname);
98 
99  cache->block_cnt = 0;
100 
101  /* Block cache has NBLOCK+1 elements to make it 1-based, thus i <= NBLOCK */
102 
103  for (i = 0; i <= NBLOCK; i++) {
104  cache->block[i].valid = MTK_FALSE;
105  cache->block[i].buf = buf_tmp;
106  }
107 
108  /* Set fill value, if available otherwise set to zero */
109 
110  status = MtkFillValueGetNcid(ncid, gridname, fieldname, &(cache->fill));
111  if (status != MTK_SUCCESS) {
112  status = MtkFileGridFieldToDataTypeNcid(ncid, gridname, fieldname,
113  &datatype);
114  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
115  status = MtkDataBufferAllocate(1, 1, datatype, &(cache->fill));
116  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
117  /* Calloc is used to clear the fill buffer, so zero is the default fill */
118  }
119 
120  return MTK_SUCCESS;
121  ERROR_HANDLE:
122  return status_code;
123 }
124 
125 /* ----------------------------------------------- */
126 /* MtkCachePixelGet */
127 /* ----------------------------------------------- */
128 
135  int block,
136  float line,
137  float sample,
138  void *pixel ) {
139 
140  MTKt_status status; /* Return status */
141  MTKt_status status_code; /* Return code of this function */
142  int rline; /* Rounded line */
143  int rsample; /* Rounded sample */
144  int nline; /* Number of lines */
145  int nsample; /* Number of samples */
146  int datasize; /* Element data size */
147  char *dptr; /* Pointer to data buffer */
148 
149  if (block <= 0) MTK_ERR_CODE_JUMP(MTK_OUTBOUNDS);
150 
151  if (!cache->block[block].valid) {
152  status = MtkCacheLoad(cache, block);
153  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
154  }
155 
156  nline = cache->block[block].buf.nline;
157  nsample = cache->block[block].buf.nsample;
158  datasize = cache->block[block].buf.datasize;
159  rline = (int)floor(line+.5);
160  rsample = (int)floor(sample+.5);
161 
162  if (rline < 0 || sample < 0 ||
163  rline >= nline || rsample >= nsample) MTK_ERR_CODE_JUMP(MTK_OUTBOUNDS);
164 
165  dptr = cache->block[block].buf.dataptr;
166  memcpy(pixel, (void *)&(dptr[rline*nsample*datasize + rsample*datasize]),
167  datasize);
168 
169  return MTK_SUCCESS;
170  ERROR_HANDLE:
171  memcpy(pixel, cache->fill.dataptr, cache->fill.datasize);
172  return status_code;
173 }
174 
175 /* ----------------------------------------------- */
176 /* MtkCacheLoad */
177 /* ----------------------------------------------- */
178 
185  int block ) {
186 
187  MTKt_status status; /* Return status */
188  MTKt_status status_code; /* Return code of this function */
189  int iblock; /* Block to release */
190 
191  /* Free a block if cache is full */
192 
193  if (cache->block_cnt >= BLOCK_CACHE_MAX) {
194  iblock = block - BLOCK_CACHE_MAX;
195  MtkDataBufferFree(&(cache->block[iblock].buf));
196  cache->block[iblock].valid = MTK_FALSE;
197  cache->block_cnt--;
198  }
199 
200  /* Read a block */
201 
202  if (cache->ncid > 0) {
203  status = MtkReadBlockNcid(cache->ncid, cache->gridname,
204  cache->fieldname, block,
205  &(cache->block[block].buf));
206  } else {
207  status = MtkReadBlockFid(cache->fid, cache->gridname,
208  cache->fieldname, block,
209  &(cache->block[block].buf));
210  }
211  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
212 
213  cache->block[block].valid = MTK_TRUE;
214  cache->block_cnt++;
215 
216  return MTK_SUCCESS;
217  ERROR_HANDLE:
218  return status_code;
219 }
220 
221 /* ----------------------------------------------- */
222 /* MtkCacheFree */
223 /* ----------------------------------------------- */
224 
229 void MtkCacheFree( MTKt_Cache *cache ) {
230 
231  int i; /* Loop index */
232 
233  /* Block cache has NBLOCK+1 elements to make it 1-based, thus i <= NBLOCK */
234 
235  for (i = 0; i <= NBLOCK; i++) {
236  if (cache->block[i].valid) {
237  MtkDataBufferFree(&(cache->block[i].buf));
238  cache->block[i].valid = MTK_FALSE;
239  cache->block_cnt--;
240  }
241  }
242  MtkDataBufferFree(&(cache->fill));
243  free((void *)cache->gridname);
244  free((void *)cache->fieldname);
245 }
MTKt_status MtkCacheLoad(MTKt_Cache *cache, int block)
Load Cache.
Definition: MtkCache.c:184
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
HDFFCLIBAPI _fcd _fcd intf intf * datatype
MTKt_status MtkCacheInitFid(int32 fid, const char *gridname, const char *fieldname, MTKt_Cache *cache)
Initialize Cache.
Definition: MtkCache.c:33
MTKt_DataType
Definition: MisrUtil.h:36
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_status MtkFileGridFieldToDataTypeNcid(int ncid, const char *gridname, const char *fieldname, MTKt_DataType *datatype)
MTKt_DataBuffer fill
Definition: MisrCache.h:42
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_status MtkReadBlockNcid(int ncid, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Definition: MtkReadBlock.c:220
MTKt_status MtkCachePixelGet(MTKt_Cache *cache, int block, float line, float sample, void *pixel)
Get pixel from cache.
Definition: MtkCache.c:134
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 MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
MTKt_status MtkFillValueGetFid(int32 fid, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
Version of MtkFillValueGet that takes an HDF-EOS file ID rather than a filename.
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_status MtkReadBlockFid(int32 fid, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Version of MtkReadBlock that takes an HDF-EOS file identifier rather than a filename.
Definition: MtkReadBlock.c:141
char * fieldname
Definition: MisrCache.h:40
#define NBLOCK
Definition: MisrProjParam.h:51
void MtkCacheFree(MTKt_Cache *cache)
Free Cache.
Definition: MtkCache.c:229
MTKt_DataBuffer buf
Definition: MisrCache.h:30
int ncid
Definition: MisrCache.h:38
MTKt_boolean valid
Definition: MisrCache.h:29
MTKt_status MtkFillValueGetNcid(int ncid, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
int32 fid
Definition: MisrCache.h:37
Cache (Low Level)
Definition: MisrCache.h:36
char * gridname
Definition: MisrCache.h:39
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkCacheInitNcid(int ncid, const char *gridname, const char *fieldname, MTKt_Cache *cache)
Definition: MtkCache.c:79
#define BLOCK_CACHE_MAX
Definition: MisrCache.h:25
int block_cnt
Definition: MisrCache.h:41
MTKt_Block block[NBLOCK+1]
Definition: MisrCache.h:43
#define FAIL
Definition: hdf.h:94

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