OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
epr_dataset.c
Go to the documentation of this file.
1 /*
2  * $Id: epr_dataset.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 #include <time.h>
24 
25 #include "epr_api.h"
26 #include "epr_core.h"
27 #include "epr_string.h"
28 #include "epr_ptrarray.h"
29 #include "epr_swap.h"
30 #include "epr_field.h"
31 #include "epr_dataset.h"
32 #include "epr_record.h"
33 #include "epr_param.h"
34 #include "epr_dsd.h"
35 #include "epr_msph.h"
36 #include "epr_band.h"
37 #include "epr_bitmask.h"
38 
39 #include "epr_dddb.h"
40 
41 
42 EPR_SDatasetId* epr_create_dataset_id(EPR_SProductId* product_id,
43  const EPR_SDSD* dsd,
44  const char* dataset_name,
45  const struct RecordDescriptor* record_descriptor,
46  const char* dsd_name,
47  const char* description)
48 {
49  EPR_SDatasetId* dataset_id = (EPR_SDatasetId*) calloc(1, sizeof (EPR_SDatasetId));
50  if (dataset_id == NULL) {
52  "epr_create_dataset_id: out of memory");
53  return NULL;
54  }
55  dataset_id->magic = EPR_MAGIC_DATASET_ID;
56 
57  dataset_id->product_id = product_id;
58  dataset_id->dsd = dsd;
59  dataset_id->record_info = NULL;
60 
61  epr_assign_string(&dataset_id->dataset_name, dataset_name);
62  dataset_id->record_descriptor = record_descriptor;
63  epr_assign_string(&dataset_id->dsd_name, dsd_name);
64  epr_assign_string(&dataset_id->description, description);
65 
66  return dataset_id;
67 }
68 
76 void epr_free_dataset_id(EPR_SDatasetId* dataset_id)
77 {
78  if (dataset_id == NULL)
79  return;
80 
81  /* Don't free the product ID, it is NOT owned by a dataset ID */
82  dataset_id->product_id = NULL;
83  /* Don't free the record information, it is NOT owned by a dataset ID */
84  dataset_id->record_info = NULL;
85  /* Don't free the DSD, it is NOT owned by a dataset ID */
86  dataset_id->dsd = NULL;
87 
88  epr_free_and_null_string(&dataset_id->dataset_name);
89  epr_free_and_null_string(&dataset_id->dsd_name);
90  epr_free_and_null_string(&dataset_id->description);
91 
92  free(dataset_id);
93 }
94 
95 
102 EPR_SPtrArray* epr_create_dataset_ids(EPR_SProductId* product_id)
103 {
104  EPR_SPtrArray* dataset_ids = NULL;
105  EPR_SDatasetId* dataset_id = NULL;
106  const EPR_SDSD* dsd = NULL;
107  epr_uint dsd_index;
108  int i;
109  const struct DatasetDescriptorTable* p_tables;
110  int pt_index;
111  int num_descr;
112 
113  if (product_id == NULL) {
115  "create_dataset_ids: product_id must not be NULL");
116  return NULL;
117  }
118 
119  /* @DDDB */
120 
121  p_tables = dddb_product_tables;
122  pt_index = -1;
123  for (i = 0; i < EPR_NUM_PRODUCT_TABLES; i++) {
124  const char* id = p_tables[i].name;
125  if (strncmp(product_id->id_string, id, 10) == 0) {
126  if (product_id->meris_iodd_version == 5) {
127  if (strcmp(id, "MER_RR__1P_IODD5") == 0 ||
128  strcmp(id, "MER_FR__1P_IODD5") == 0) {
129  pt_index = i;
130  }
131  } else if (product_id->meris_iodd_version == 6) {
132  if (strcmp(id, "MER_RR__2P_IODD6") == 0 ||
133  strcmp(id, "MER_FR__2P_IODD6") == 0) {
134  pt_index = i;
135  }
136  } else {
137  pt_index = i;
138  }
139  }
140  if (pt_index != -1) {
141  break;
142  }
143  }
144  if (pt_index == -1) {
146  "create_dataset_ids: unknown product type");
147  return NULL;
148  }
149 
150  dataset_ids = epr_create_ptr_array(16);
151  num_descr = p_tables[pt_index].num_descriptors;
152  for (i = 0; i < num_descr; i++) {
153  for (dsd_index = 0; dsd_index < product_id->dsd_array->length; dsd_index++) {
154  dsd = (EPR_SDSD*)epr_get_ptr_array_elem_at(product_id->dsd_array, dsd_index);
155  if (strncmp(dsd->ds_name, p_tables[pt_index].descriptors[i].ds_name, strlen(epr_strip_string_r(dsd->ds_name))) == 0) {
156  dataset_id = epr_create_dataset_id(product_id,
157  dsd,
158  p_tables[pt_index].descriptors[i].id,
159  p_tables[pt_index].descriptors[i].rec_descriptor,
160  p_tables[pt_index].descriptors[i].ds_name,
161  p_tables[pt_index].descriptors[i].description);
162  epr_add_ptr_array_elem(dataset_ids, dataset_id);
163  break;
164  }
165  }
166  }
167 
168  return dataset_ids;
169 }
170 
171 
172 const char* epr_get_dataset_name(EPR_SDatasetId* dataset_id)
173 {
174  epr_clear_err();
175 
176  if (dataset_id == NULL) {
178  "epr_get_dataset_name: band_id must not be NULL");
179  return NULL;
180  }
181  return epr_trim_string(dataset_id->dataset_name);
182 }
183 
184 epr_uint epr_get_num_records(const EPR_SDatasetId* dataset_id)
185 {
186  epr_clear_err();
187 
188  if (dataset_id == NULL) {
190  "epr_get_num_records: invalid dataset name");
191  return (epr_uint)-1;
192  }
193  return dataset_id->dsd->num_dsr;
194 }
195 
196 const EPR_SDSD* epr_get_dsd(const EPR_SDatasetId* dataset_id)
197 {
198  epr_clear_err();
199 
200  if (dataset_id == NULL) {
202  "epr_get_dsd: invalid dataset name");
203  return NULL;
204  }
205  return dataset_id->dsd;
206 }
207 
208 const char* epr_get_dsd_name(const EPR_SDatasetId* dataset_id)
209 {
210  epr_clear_err();
211 
212  if (dataset_id == NULL) {
214  "epr_get_dsd_name: invalid dataset name");
215  return NULL;
216  }
217  return epr_trim_string(dataset_id->dsd_name);
218 }
219 
220 epr_uint epr_get_dataset_offset(EPR_SDatasetId* dataset_id)
221 {
222  if (dataset_id == NULL) {
224  "epr_get_dataset_offset: dataset_id must not be NULL");
225  return (epr_uint)0;
226  }
227  return dataset_id->dsd->ds_offset;
228 }
229 
230 /*********************************** RECORD ***********************************/
231 
232 /*
233  Function: epr_create_record
234  Access: public API
235  Changelog: 2002/01/23 mp initial version
236  */
241 EPR_SRecord* epr_create_record(EPR_SDatasetId* dataset_id)
242 {
243  EPR_SRecord* record = NULL;
244 
245  epr_clear_err();
246 
247  if (dataset_id == NULL) {
249  "epr_create_record: dataset ID must not be NULL");
250  return NULL;
251  }
252  if (dataset_id->record_info == NULL) {
253  dataset_id->record_info = epr_get_record_info(dataset_id);
254  }
255 
256  record = epr_create_record_from_info(dataset_id->record_info);
257  if (record == NULL) {
259  "epr_create_record: invalid record name");
260  return NULL;
261  }
262  return record;
263 }
264 
265 
269 EPR_SRecord* epr_read_record(EPR_SDatasetId* dataset_id,
270  epr_uint record_index,
271  EPR_SRecord* record)
272 {
273  epr_uint field_index;
274  epr_uint dsd_offset;
275  epr_uint record_size;
276  epr_uint data_type_size;
277  epr_uint elements_to_read;
278  epr_uint elements_read;
279  EPR_SField* field = NULL;
280 
281  epr_clear_err();
282 
283  if (dataset_id == NULL) {
285  "epr_read_record: invalid dataset name");
286  return NULL;
287 
288  }
289  if ((record_index < 0) || (record_index >= dataset_id->dsd->num_dsr)) {
291  "epr_read_record: invalid record_index parameter, must be >=0 and <num_dsr");
292  return NULL;
293  }
294 
295  if (record == NULL) {
296  record = epr_create_record(dataset_id);
297  } else if (record->info != dataset_id->record_info) {
299  "epr_read_record: invalid record name");
300  return NULL;
301  }
302 
303  /*READ FILE with: dataset_id->product_id->istream after the setting it to begin*/
304  rewind(dataset_id->product_id->istream);
305 
306  /*GET OFFSET*/
307  dsd_offset = dataset_id->dsd->ds_offset;
308 
309  record_size = record->info->tot_size;
310  if (record_size != dataset_id->dsd->dsr_size) {
312  "epr_read_record: wrong record size");
313  return NULL;
314  }
315 
316  /* Set file pointer to begin of demanded record */
317  if (fseek(dataset_id->product_id->istream, dsd_offset + record_size * record_index, SEEK_SET) != 0) {
319  "epr_read_record: file seek failed");
320  return NULL;
321  }
322 
323  for (field_index = 0; field_index < record->num_fields; field_index++) {
324  field = record->fields[field_index];
325  elements_to_read = field->info->num_elems ;
326  data_type_size = epr_get_data_type_size(field->info->data_type_id);
327  assert(data_type_size != 0);
328  assert(field->elems != NULL);
329 
330  if (elements_to_read * data_type_size != field->info->tot_size) {
331  /*epr_log(e_log_info, "Spare");*/
332  data_type_size = field->info->tot_size / elements_to_read;
333  }
334 
335  elements_read = fread(field->elems, data_type_size, elements_to_read, dataset_id->product_id->istream);
336  if (elements_read != elements_to_read) {
338  "epr_read_record: file read failed");
339  return NULL;
340  }
341 
342  /*
343  * SWAP bytes on little endian (LE) order architectures (I368, Pentium Processors).
344  * ENVISAT products are stored in big endian (BE) order.
345  */
346  if (epr_api.little_endian_order) {
347  epr_swap_endian_order(field);
348  }
349  }
350  return record;
351 }
EPR_SDatasetId * epr_create_dataset_id(EPR_SProductId *product_id, const EPR_SDSD *dsd, const char *dataset_name, const struct RecordDescriptor *record_descriptor, const char *dsd_name, const char *description)
Definition: epr_dataset.c:42
const EPR_SDSD * epr_get_dsd(const EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:196
char * epr_strip_string_r(char *str)
Definition: epr_string.c:294
@ e_err_out_of_memory
Definition: epr_api.h:83
char * epr_trim_string(char *str)
Definition: epr_string.c:247
@ e_err_illegal_arg
Definition: epr_api.h:81
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
#define EPR_MAGIC_DATASET_ID
Definition: epr_api.h:195
#define NULL
Definition: decode_rs.h:63
const char * name
Definition: epr_dddb.h:49
#define EPR_NUM_PRODUCT_TABLES
Definition: epr_dddb.h:83
EPR_SAPI epr_api
Definition: epr_core.c:43
EPR_SRecord * epr_create_record_from_info(EPR_SRecordInfo *record_info)
Definition: epr_record.c:315
char * epr_assign_string(char **str_clone, const char *str)
Definition: epr_string.c:29
@ e_err_file_access_denied
Definition: epr_api.h:90
void epr_free_dataset_id(EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:76
@ e_err_invalid_data_format
Definition: epr_api.h:107
EPR_SPtrArray * epr_create_dataset_ids(EPR_SProductId *product_id)
Definition: epr_dataset.c:102
@ e_err_null_pointer
Definition: epr_api.h:80
@ e_err_file_read_error
Definition: epr_api.h:91
const char * ds_name
Definition: epr_dddb.h:21
@ e_err_invalid_value
Definition: epr_api.h:108
const char * epr_get_dataset_name(EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:172
What value is used by your function when the data value is bad Default is BAD_FLT l2prod product_id[0]
EPR_SRecord * epr_read_record(EPR_SDatasetId *dataset_id, epr_uint record_index, EPR_SRecord *record)
Definition: epr_dataset.c:269
void * epr_get_ptr_array_elem_at(const EPR_SPtrArray *ptr_array, unsigned int index)
Definition: epr_ptrarray.c:122
@ e_err_invalid_record_name
Definition: epr_api.h:104
@ e_err_invalid_dataset_name
Definition: epr_api.h:102
const struct DatasetDescriptor * descriptors
Definition: epr_dddb.h:52
const char * epr_get_dsd_name(const EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:208
int epr_add_ptr_array_elem(EPR_SPtrArray *ptr_array, void *elem)
Definition: epr_ptrarray.c:75
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
const struct DatasetDescriptorTable dddb_product_tables[46]
Definition: epr_dddb.c:4190
void epr_swap_endian_order(const EPR_SField *field)
Definition: epr_swap.c:192
description
Definition: setup.py:16
EPR_SRecord * epr_create_record(EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:241
epr_uint epr_get_dataset_offset(EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:220
epr_uint epr_get_num_records(const EPR_SDatasetId *dataset_id)
Definition: epr_dataset.c:184
void epr_free_and_null_string(char **str)
Definition: epr_string.c:370
EPR_SRecordInfo * epr_get_record_info(EPR_SDatasetId *dataset_id)
Definition: epr_record.c:151
int i
Definition: decode_rs.h:71
EPR_SPtrArray * epr_create_ptr_array(unsigned int capacity)
Definition: epr_ptrarray.c:29