OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
epr_core.c
Go to the documentation of this file.
1 /*
2  * $Id: epr_core.c,v 1.1.1.1 2004-10-28 19:22:22 norman Exp $
3  *
4  * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation. This program is distributed in the hope it will
9  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "epr_api.h"
25 #include "epr_core.h"
26 #include "epr_string.h"
27 #include "epr_ptrarray.h"
28 #include "epr_swap.h"
29 #include "epr_field.h"
30 #include "epr_record.h"
31 #include "epr_param.h"
32 #include "epr_dsd.h"
33 #include "epr_msph.h"
34 #include "epr_band.h"
35 #include "epr_bitmask.h"
36 
37 #include "epr_dddb.h"
38 
39 
43 EPR_SAPI epr_api;
44 
45 /*
46  Function: epr_str_to_data_type_id
47  Access: private API implementation helper
48  Changelog: 2002/01/05 nf initial version
49  */
59 {
60  assert(str != NULL);
61  if (epr_equal_names(str, "UChar") || epr_equal_names(str, "uchar"))
62  return e_tid_uchar;
63  else if (epr_equal_names(str, "AChar") || epr_equal_names(str, "SChar") || epr_equal_names(str, "char"))
64  return e_tid_char;
65  else if (epr_equal_names(str, "UShort") || epr_equal_names(str, "ushort"))
66  return e_tid_ushort;
67  else if (epr_equal_names(str, "SShort") || epr_equal_names(str, "short"))
68  return e_tid_short;
69  else if (epr_equal_names(str, "UInt") || epr_equal_names(str, "uint"))
70  return e_tid_uint;
71  else if (epr_equal_names(str, "SInt") || epr_equal_names(str, "int"))
72  return e_tid_int;
73  else if (epr_equal_names(str, "ULong") || epr_equal_names(str, "ulong"))
74  return e_tid_uint;
75  else if (epr_equal_names(str, "SLong") || epr_equal_names(str, "long"))
76  return e_tid_int;
77  else if (epr_equal_names(str, "Float") || epr_equal_names(str, "float"))
78  return e_tid_float;
79  else if (epr_equal_names(str, "Double") || epr_equal_names(str, "double"))
80  return e_tid_double;
81  else if (epr_equal_names(str, "@/types/UTC.dd") || epr_equal_names(str, "time"))
82  return e_tid_time;
83  else if (epr_equal_names(str, "String") || epr_equal_names(str, "string"))
84  return e_tid_string;
85  else if (epr_equal_names(str, "Spare") || epr_equal_names(str, "spare"))
86  return e_tid_spare;
87  else
88  return e_tid_unknown;
89 }
90 
91 /*
92  Function: epr_data_type_id_to_str
93  Access: public API implementation helper
94  Changelog: 2003/07/10 nf initial version
95  */
104 const char* epr_data_type_id_to_str(EPR_EDataTypeId data_type_id)
105 {
106  switch (data_type_id)
107  {
108  case e_tid_uchar:
109  return "uchar";
110  case e_tid_char:
111  return "char";
112  case e_tid_ushort:
113  return "ushort";
114  case e_tid_short:
115  return "short";
116  case e_tid_uint:
117  return "uint";
118  case e_tid_int:
119  return "int";
120  case e_tid_float:
121  return "float";
122  case e_tid_double:
123  return "double";
124  case e_tid_string:
125  return "string";
126  case e_tid_spare:
127  return "spare";
128  case e_tid_time:
129  return "time";
130  default:
131  return "";
132  }
133 }
134 
135 /*
136  Function: epr_get_data_type_size
137  Access: private API implementation helper
138  Changelog: 2002/01/24 nf initial version
139  */
149 {
150  switch (data_type_id)
151  {
152  case e_tid_uchar:
153  return sizeof(epr_uchar);
154  case e_tid_char:
155  return sizeof(char);
156  case e_tid_ushort:
157  return sizeof(epr_ushort);
158  case e_tid_short:
159  return sizeof(short);
160  case e_tid_uint:
161  return sizeof(epr_uint);
162  case e_tid_int:
163  return sizeof(int);
164  case e_tid_float:
165  return sizeof(float);
166  case e_tid_double:
167  return sizeof(double);
168  case e_tid_string:
169  return sizeof(char);
170  case e_tid_spare:
171  return sizeof(epr_uchar);
172  case e_tid_time:
173  return sizeof (int) /* days */
174  + sizeof (epr_uint) /* seconds */
175  + sizeof (epr_uint); /* microseconds */
176  default:
177  return 0;
178  }
179 }
180 /*
181  * ===================== Logging ==============================
182  */
183 
184 /*
185  Function: epr_log
186  Access: private API implementation helper
187  Changelog: 2002/01/05 mp initial version
188  */
199 void epr_log(EPR_ELogLevel log_level, const char* log_message)
200 {
201  if (epr_api.log_handler != NULL && log_level >= epr_api.log_level)
202  epr_api.log_handler(log_level, log_message);
203 }
204 
205 /*
206  * ===================== Error Handling ==============================
207  */
208 
209 /*
210  Function: epr_set_error
211  Access: private API implementation helper
212  Changelog: 2002/01/05 mp initial version
213  */
221 void epr_set_err(EPR_EErrCode err_code, const char* err_message)
222 {
223  epr_api.last_err_code = err_code;
224  epr_assign_string(&epr_api.last_err_message, err_message);
225 
226  if (epr_api.log_handler != NULL)
227  {
228  epr_api.log_handler(e_log_error, err_message);
229  }
230 
231  if (epr_api.err_handler != NULL)
232  {
233  epr_api.err_handler(err_code, err_message);
234  }
235 }
236 
237 /*
238  Function: epr_set_error
239  Access: private API implementation helper
240  Changelog: 2002/01/05 mp initial version
241  */
248 {
249  epr_api.last_err_code = e_err_none;
250  epr_free_string(epr_api.last_err_message);
251  epr_api.last_err_message = NULL;
252 }
253 
254 /*
255  Function: epr_get_last_err_code
256  Access: private API implementation helper
257  Changelog: 2002/01/05 nf initial version
258  */
266 {
267  return epr_api.last_err_code;
268 }
269 
270 /*
271  Function: epr_get_last_err_message
272  Access: private API implementation helper
273  Changelog: 2002/01/05 nf initial version
274  */
282 {
283  return epr_api.last_err_message;
284 }
285 
286 
295 FILE* epr_open_file(char* file_path)
296 {
297  FILE* db_file;
298  epr_log(e_log_debug, "about to open file: ");
299  epr_log(e_log_debug, file_path);
300 
301  db_file = fopen(epr_trim_string(file_path), "rb");
302  if (db_file == NULL)
303  {
304  epr_log(e_log_debug, "open failed");
305  if (errno == ENOENT)
306  {
308  "epr_open_file: file not found");
309  }
310  else {
312  "epr_open_file: file open failed");
313  }
314  }
315  /*epr_log(e_log_debug, "open successful");*/
316  return db_file;
317 }
318 
319 
328 int epr_str_to_number(const char* str)
329 {
330  char *stopstring;
331  int l;
332  assert(str != NULL);
333 
334  if (strcmp(str, "*") == 0) return 1;
335  if (strcmp(str, "") == 0) return 1;
336 
337  errno = 0;
338  l = strtol( str, &stopstring, 10 );
339 
340  if (errno == EDOM)
341  {
343  "failed to convert string to integer: errno = EDOM");
344  return -1;
345  }
346  if (errno == ERANGE)
347  {
349  "failed to convert string to integer: errno = ERANGE");
350  return -1;
351  }
352 
353  return l;
354 }
355 
356 
373 epr_uint epr_parse_value_count(EPR_SProductId* product_id, const char* count)
374 {
375  char seps[] = EPR_COUNT_SEPARATOR_ARRAY;
376  char * token;
377  char * str;
378  epr_uint c;
379  int pos = 0;
380  epr_uint comes_from_convert;
381 
382  c = 1;
383 
385  if (str == NULL)
386  {
388  "epr_parse_value_count: cannot allocate memory");
389  return 16111;/*8999*/ /* @todo check: why in the hell 16111 ? */
390  }
391 
392  while ((token = epr_str_tok(str, seps, &pos)) != NULL)
393  {
394  comes_from_convert = epr_str_to_number(token);
395  if (comes_from_convert == 0 && (strcmp(token, "0") != 0))
396  {
397  /* check if "token" belongs to param_table*/
398  comes_from_convert = epr_param_to_value(token, product_id->param_table);
399  if (comes_from_convert == -1)
400  {
402  "epr_parse_value_count: parameter not found");
403  return 16111; /* @todo check: why in the hell 16111 ? */
404  }
405  }
406  c *= comes_from_convert;
408  token = NULL;
409  }
411  return c;
412 }
413 
414 
424 epr_uint epr_param_to_value(const char* str, EPR_SPtrArray* param_table)
425 {
426  EPR_SParamElem* param_elem = NULL;
427  int elem_index;
428  int param_table_length;
429 
430  param_table_length = (int)epr_get_ptr_array_length(param_table);
431  for (elem_index = 0; elem_index < param_table_length; elem_index++)
432  {
433  param_elem = (EPR_SParamElem*)epr_get_ptr_array_elem_at(param_table, elem_index);
434  if (epr_equal_names(param_elem->param_name, str))
435  {
436  return param_elem->param_value;
437  }
438  }
439  return (epr_uint) -1; /*error*/
440 }
441 
442 
450 {
451  if (path != NULL)
452  {
453  char* pc = path;
454  while (*pc != '\0')
455  {
456 #ifdef WIN32
457  if (*pc == '/')
458  *pc = '\\';
459 
460 #elif _M_MPPC
461  if (*pc == '/')
462  *pc = ':'; /* UK note: the colon is an old-style path separator of the Mac OS */
463  /* possibly not used any more but supported for Classic compatibility */
464  /* @to do: check whether the forward slash / should be used in Mac OS X */
465 #else
466  if (*pc == '\\')
467  *pc = '/';
468 #endif
469  pc++;
470  }
471  }
472 }
473 
474 
476  if (!epr_api.init_flag) {
478  "epr_open_product: API not initialized (forgot to call epr_init_api?)");
479  return EPR_FALSE;
480  }
481  return EPR_TRUE;
482 }
@ e_err_out_of_memory
Definition: epr_api.h:83
char * epr_trim_string(char *str)
Definition: epr_string.c:247
unsigned int epr_get_ptr_array_length(const EPR_SPtrArray *ptr_array)
Definition: epr_ptrarray.c:115
@ e_log_debug
Definition: epr_api.h:125
epr_uint epr_param_to_value(const char *str, EPR_SPtrArray *param_table)
Definition: epr_core.c:424
enum EPR_LogLevel EPR_ELogLevel
Definition: epr_api.h:163
epr_uint epr_get_data_type_size(EPR_EDataTypeId data_type_id)
Definition: epr_core.c:148
unsigned int epr_uint
Definition: epr_api.h:188
int epr_str_to_number(const char *str)
Definition: epr_core.c:328
enum EPR_ErrCode EPR_EErrCode
Definition: epr_api.h:162
unsigned short epr_ushort
Definition: epr_api.h:187
#define NULL
Definition: decode_rs.h:63
@ e_err_none
Definition: epr_api.h:77
EPR_SAPI epr_api
Definition: epr_core.c:43
float32 * pos
Definition: l1_czcs_hdf.c:35
char * epr_assign_string(char **str_clone, const char *str)
Definition: epr_string.c:29
int epr_boolean
Definition: epr_api.h:185
@ e_err_file_access_denied
Definition: epr_api.h:90
@ e_tid_float
Definition: epr_api.h:60
epr_boolean epr_equal_names(const char *name1, const char *name2)
Definition: epr_string.c:91
@ e_tid_int
Definition: epr_api.h:58
@ e_tid_ushort
Definition: epr_api.h:52
const char * epr_get_last_err_message()
Definition: epr_core.c:281
@ e_err_api_not_initialized
Definition: epr_api.h:97
FILE * epr_open_file(char *file_path)
Definition: epr_core.c:295
char * epr_clone_string(const char *str)
Definition: epr_string.c:43
@ e_err_illegal_conversion
Definition: epr_api.h:85
string path
Definition: color_dtdb.py:221
@ e_log_error
Definition: epr_api.h:128
@ e_tid_uint
Definition: epr_api.h:56
#define EPR_COUNT_SEPARATOR_ARRAY
Definition: epr_core.h:56
What value is used by your function when the data value is bad Default is BAD_FLT l2prod product_id[0]
const char * epr_data_type_id_to_str(EPR_EDataTypeId data_type_id)
Definition: epr_core.c:104
@ e_err_file_not_found
Definition: epr_api.h:89
@ e_tid_spare
Definition: epr_api.h:66
void * epr_get_ptr_array_elem_at(const EPR_SPtrArray *ptr_array, unsigned int index)
Definition: epr_ptrarray.c:122
int errno
void epr_make_os_compatible_path(char *path)
Definition: epr_core.c:449
integer, parameter double
@ e_tid_char
Definition: epr_api.h:50
void epr_log(EPR_ELogLevel log_level, const char *log_message)
Definition: epr_core.c:199
void epr_clear_err()
Definition: epr_core.c:247
void epr_set_err(EPR_EErrCode err_code, const char *err_message)
Definition: epr_core.c:221
@ e_tid_double
Definition: epr_api.h:62
EPR_EDataTypeId epr_str_to_data_type_id(const char *str)
Definition: epr_core.c:58
const char * str
Definition: l1c_msi.cpp:35
@ e_tid_string
Definition: epr_api.h:64
@ e_tid_short
Definition: epr_api.h:54
char * epr_str_tok(const char *str, const char *seps, int *pos)
Definition: epr_string.c:122
epr_uint epr_parse_value_count(EPR_SProductId *product_id, const char *count)
Definition: epr_core.c:373
@ e_tid_time
Definition: epr_api.h:68
enum EPR_DataTypeId EPR_EDataTypeId
Definition: epr_api.h:161
EPR_EErrCode epr_get_last_err_code()
Definition: epr_core.c:265
@ e_tid_unknown
Definition: epr_api.h:46
unsigned char epr_uchar
Definition: epr_api.h:186
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed as required for compatibility with version of the SDP toolkit Corrected test output file names to end in per delivery and then split off a new MYD_PR03 pcf file for Aqua Added AssociatedPlatformInstrumentSensor to the inventory metadata in MOD01 mcf and MOD03 mcf Created new versions named MYD01 mcf and MYD03 where AssociatedPlatformShortName is rather than Terra The program itself has been changed to read the Satellite Instrument validate it against the input L1A and LUT and to use it determine the correct files to retrieve the ephemeris and attitude data from Changed to produce a LocalGranuleID starting with MYD03 if run on Aqua data Added the Scan Type file attribute to the Geolocation copied from the L1A and attitude_angels to radians rather than degrees The accumulation of Cumulated gflags was moved from GEO_validate_earth_location c to GEO_locate_one_scan c
Definition: HISTORY.txt:464
@ e_tid_uchar
Definition: epr_api.h:48
#define EPR_FALSE
Definition: epr_api.h:203
epr_boolean epr_check_api_init_flag()
Definition: epr_core.c:475
#define EPR_TRUE
Definition: epr_api.h:202
void epr_free_string(char *str)
Definition: epr_string.c:100
char * err_message
Definition: epr_bitmask.h:304
int count
Definition: decode_rs.h:79