ocssw  1.0
/disk01/web/ocssw/build/src/libmeris/epr_dataset.c (r10089/r7671)
Go to the documentation of this file.
00001 /*
00002  * $Id: epr_dataset.c,v 1.1.1.1 2004-10-28 19:22:22 norman Exp $
00003  *
00004  * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de)
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU General Public License as published by the
00008  * Free Software Foundation. This program is distributed in the hope it will
00009  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00010  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011  * See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016  */
00017 
00018 #include <assert.h>
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <time.h>
00024 
00025 #include "epr_api.h"
00026 #include "epr_core.h"
00027 #include "epr_string.h"
00028 #include "epr_ptrarray.h"
00029 #include "epr_swap.h"
00030 #include "epr_field.h"
00031 #include "epr_dataset.h"
00032 #include "epr_record.h"
00033 #include "epr_param.h"
00034 #include "epr_dsd.h"
00035 #include "epr_msph.h"
00036 #include "epr_band.h"
00037 #include "epr_bitmask.h"
00038 
00039 #include "epr_dddb.h"
00040 
00041 
00042 EPR_SDatasetId* epr_create_dataset_id(EPR_SProductId* product_id,
00043                                       const EPR_SDSD* dsd,
00044                                       const char* dataset_name,
00045                                       const struct RecordDescriptor* record_descriptor,
00046                                       const char* dsd_name,
00047                                       const char* description)
00048 {
00049     EPR_SDatasetId* dataset_id = (EPR_SDatasetId*) calloc(1, sizeof (EPR_SDatasetId));
00050     if (dataset_id == NULL) {
00051         epr_set_err(e_err_out_of_memory,
00052                     "epr_create_dataset_id: out of memory");
00053         return NULL;
00054     }
00055     dataset_id->magic = EPR_MAGIC_DATASET_ID;
00056 
00057     dataset_id->product_id = product_id;
00058     dataset_id->dsd = dsd;
00059     dataset_id->record_info = NULL;
00060 
00061     epr_assign_string(&dataset_id->dataset_name, dataset_name);
00062     dataset_id->record_descriptor = record_descriptor;
00063     epr_assign_string(&dataset_id->dsd_name, dsd_name);
00064     epr_assign_string(&dataset_id->description, description);
00065 
00066     return dataset_id;
00067 }
00068 
00076 void epr_free_dataset_id(EPR_SDatasetId* dataset_id)
00077 {
00078     if (dataset_id == NULL)
00079         return;
00080 
00081     /* Don't free the product ID, it is NOT owned by a dataset ID */
00082     dataset_id->product_id = NULL;
00083     /* Don't free the record information, it is NOT owned by a dataset ID */
00084     dataset_id->record_info = NULL;
00085     /* Don't free the DSD, it is NOT owned by a dataset ID */
00086     dataset_id->dsd = NULL;
00087 
00088     epr_free_and_null_string(&dataset_id->dataset_name);
00089     epr_free_and_null_string(&dataset_id->dsd_name);
00090     epr_free_and_null_string(&dataset_id->description);
00091 
00092     free(dataset_id);
00093 }
00094 
00095 
00102 EPR_SPtrArray* epr_create_dataset_ids(EPR_SProductId* product_id)
00103 {
00104     EPR_SPtrArray* dataset_ids = NULL;
00105     EPR_SDatasetId* dataset_id = NULL;
00106     const EPR_SDSD* dsd = NULL;
00107     epr_uint dsd_index;
00108     int i;
00109     const struct DatasetDescriptorTable* p_tables;
00110     int pt_index;
00111     int num_descr;
00112 
00113     if (product_id == NULL) {
00114         epr_set_err(e_err_null_pointer,
00115                     "create_dataset_ids: product_id must not be NULL");
00116         return NULL;
00117     }
00118 
00119     /* @DDDB */
00120 
00121     p_tables = dddb_product_tables;
00122     pt_index = -1;
00123     for (i = 0; i < EPR_NUM_PRODUCT_TABLES; i++) {
00124         const char* id = p_tables[i].name;
00125         if (strncmp(product_id->id_string, id, 10) == 0) {
00126             if (product_id->meris_iodd_version == 5) {
00127                 if (strcmp(id, "MER_RR__1P_IODD5") == 0 ||
00128                     strcmp(id, "MER_FR__1P_IODD5") == 0) {
00129                     pt_index = i;
00130                 }
00131             } else if (product_id->meris_iodd_version == 6) {
00132                 if (strcmp(id, "MER_RR__2P_IODD6") == 0 ||
00133                     strcmp(id, "MER_FR__2P_IODD6") == 0) {
00134                     pt_index = i;
00135                 }
00136             } else {
00137                 pt_index = i;
00138             }
00139         }
00140         if (pt_index != -1) {
00141             break;
00142         }
00143     }
00144     if (pt_index == -1) {
00145         epr_set_err(e_err_null_pointer,
00146                     "create_dataset_ids: unknown product type");
00147         return NULL;
00148     }
00149 
00150     dataset_ids = epr_create_ptr_array(16);
00151     num_descr = p_tables[pt_index].num_descriptors;
00152     for (i = 0; i < num_descr; i++) {
00153         for (dsd_index = 0; dsd_index < product_id->dsd_array->length; dsd_index++) {
00154             dsd = (EPR_SDSD*)epr_get_ptr_array_elem_at(product_id->dsd_array, dsd_index);
00155             if (strncmp(dsd->ds_name, p_tables[pt_index].descriptors[i].ds_name, strlen(epr_strip_string_r(dsd->ds_name))) == 0) {
00156                 dataset_id = epr_create_dataset_id(product_id,
00157                                                     dsd,
00158                                                     p_tables[pt_index].descriptors[i].id,
00159                                                     p_tables[pt_index].descriptors[i].rec_descriptor,
00160                                                     p_tables[pt_index].descriptors[i].ds_name,
00161                                                     p_tables[pt_index].descriptors[i].description);
00162                 epr_add_ptr_array_elem(dataset_ids, dataset_id);
00163                 break;
00164             }
00165         }
00166     }
00167 
00168     return dataset_ids;
00169 }
00170 
00171 
00172 const char* epr_get_dataset_name(EPR_SDatasetId* dataset_id)
00173 {
00174     epr_clear_err();
00175 
00176     if (dataset_id == NULL) {
00177         epr_set_err(e_err_null_pointer,
00178                     "epr_get_dataset_name: band_id must not be NULL");
00179         return NULL;
00180     }
00181     return epr_trim_string(dataset_id->dataset_name);
00182 }
00183 
00184 epr_uint epr_get_num_records(const EPR_SDatasetId* dataset_id)
00185 {
00186     epr_clear_err();
00187 
00188     if (dataset_id == NULL) {
00189         epr_set_err(e_err_invalid_dataset_name,
00190                     "epr_get_num_records: invalid dataset name");
00191         return (epr_uint)-1;
00192     }
00193     return dataset_id->dsd->num_dsr;
00194 }
00195 
00196 const EPR_SDSD* epr_get_dsd(const EPR_SDatasetId* dataset_id)
00197 {
00198     epr_clear_err();
00199 
00200     if (dataset_id == NULL) {
00201         epr_set_err(e_err_invalid_dataset_name,
00202                     "epr_get_dsd: invalid dataset name");
00203         return NULL;
00204     }
00205     return dataset_id->dsd;
00206 }
00207 
00208 const char* epr_get_dsd_name(const EPR_SDatasetId* dataset_id)
00209 {
00210     epr_clear_err();
00211 
00212     if (dataset_id == NULL) {
00213         epr_set_err(e_err_invalid_dataset_name,
00214                     "epr_get_dsd_name: invalid dataset name");
00215         return NULL;
00216     }
00217     return epr_trim_string(dataset_id->dsd_name);
00218 }
00219 
00220 epr_uint epr_get_dataset_offset(EPR_SDatasetId* dataset_id)
00221 {
00222     if (dataset_id == NULL) {
00223         epr_set_err(e_err_null_pointer,
00224                     "epr_get_dataset_offset: dataset_id must not be NULL");
00225         return (epr_uint)0;
00226     }
00227     return dataset_id->dsd->ds_offset;
00228 }
00229 
00230 /*********************************** RECORD ***********************************/
00231 
00232 /*
00233    Function:    epr_create_record
00234    Access:      public API
00235    Changelog:   2002/01/23  mp initial version
00236  */
00241 EPR_SRecord* epr_create_record(EPR_SDatasetId* dataset_id)
00242 {
00243     EPR_SRecord* record = NULL;
00244 
00245     epr_clear_err();
00246 
00247     if (dataset_id == NULL) {
00248         epr_set_err(e_err_illegal_arg,
00249                     "epr_create_record: dataset ID must not be NULL");
00250         return NULL;
00251     }
00252     if (dataset_id->record_info == NULL) {
00253         dataset_id->record_info = epr_get_record_info(dataset_id);
00254     }
00255 
00256     record = epr_create_record_from_info(dataset_id->record_info);
00257     if (record == NULL) {
00258         epr_set_err(e_err_invalid_record_name,
00259                     "epr_create_record: invalid record name");
00260         return NULL;
00261     }
00262     return record;
00263 }
00264 
00265 
00269 EPR_SRecord* epr_read_record(EPR_SDatasetId* dataset_id,
00270                              epr_uint record_index,
00271                              EPR_SRecord* record)
00272 {
00273     epr_uint field_index;
00274     epr_uint dsd_offset;
00275     epr_uint record_size;
00276     epr_uint data_type_size;
00277     epr_uint elements_to_read;
00278     epr_uint elements_read;
00279     EPR_SField* field = NULL;
00280 
00281     epr_clear_err();
00282 
00283     if (dataset_id == NULL) {
00284         epr_set_err(e_err_invalid_dataset_name,
00285                     "epr_read_record: invalid dataset name");
00286         return NULL;
00287 
00288     }
00289     if ((record_index < 0) || (record_index >= dataset_id->dsd->num_dsr)) {
00290         epr_set_err(e_err_invalid_value,
00291                     "epr_read_record: invalid record_index parameter, must be >=0 and <num_dsr");
00292         return NULL;
00293     }
00294 
00295     if (record == NULL) {
00296         record = epr_create_record(dataset_id);
00297     } else if (record->info != dataset_id->record_info) {
00298         epr_set_err(e_err_invalid_record_name,
00299                     "epr_read_record: invalid record name");
00300         return NULL;
00301     }
00302 
00303     /*READ FILE with: dataset_id->product_id->istream after the setting it to begin*/
00304     rewind(dataset_id->product_id->istream);
00305 
00306     /*GET OFFSET*/
00307     dsd_offset = dataset_id->dsd->ds_offset;
00308 
00309     record_size = record->info->tot_size;
00310     if (record_size != dataset_id->dsd->dsr_size) {
00311         epr_set_err(e_err_invalid_data_format,
00312             "epr_read_record: wrong record size");
00313         return NULL;
00314     }
00315 
00316     /* Set file pointer to begin of demanded record */
00317     if (fseek(dataset_id->product_id->istream, dsd_offset + record_size * record_index, SEEK_SET) != 0) {
00318         epr_set_err(e_err_file_access_denied,
00319             "epr_read_record: file seek failed");
00320         return NULL;
00321     }
00322 
00323     for (field_index = 0; field_index < record->num_fields; field_index++) {
00324         field = record->fields[field_index];
00325         elements_to_read = field->info->num_elems ;
00326         data_type_size = epr_get_data_type_size(field->info->data_type_id);
00327         assert(data_type_size != 0);
00328         assert(field->elems != NULL);
00329 
00330         if (elements_to_read * data_type_size != field->info->tot_size) {
00331             /*epr_log(e_log_info, "Spare");*/
00332             data_type_size = field->info->tot_size / elements_to_read;
00333         }
00334 
00335         elements_read = fread(field->elems, data_type_size, elements_to_read, dataset_id->product_id->istream);
00336         if (elements_read != elements_to_read) {
00337             epr_set_err(e_err_file_read_error,
00338                 "epr_read_record: file read failed");
00339             return NULL;
00340         }
00341 
00342         /*
00343          * SWAP bytes on little endian (LE) order architectures (I368, Pentium Processors).
00344          * ENVISAT products are stored in big endian (BE) order.
00345          */
00346         if (epr_api.little_endian_order) {
00347             epr_swap_endian_order(field);
00348         }
00349     }
00350     return record;
00351 }