/** Close an open product file. * This function will close the file associated with the file handle and release the memory for the handle. * The file handle will be released even if unmapping or closing of the product file produced an error. * \param product Pointer to a product file handle. * \return * \arg \c 0, Success. * \arg \c -1, Error occurred (check #coda_errno). */ LIBCODA_API int coda_close(coda_product *product) { if (product == NULL) { coda_set_error(CODA_ERROR_INVALID_ARGUMENT, "product file argument is NULL (%s:%u)", __FILE__, __LINE__); return -1; } /* remove product variable information */ if (product->product_variable_size != NULL) { free(product->product_variable_size); product->product_variable_size = NULL; } if (product->product_variable != NULL) { int i; for (i = 0; i < product->product_definition->num_product_variables; i++) { if (product->product_variable[i] != NULL) { free(product->product_variable[i]); } } free(product->product_variable); product->product_variable = NULL; } switch (product->format) { case coda_format_ascii: return coda_ascii_close(product); case coda_format_binary: return coda_bin_close(product); case coda_format_xml: return coda_xml_close(product); case coda_format_cdf: return coda_cdf_close(product); case coda_format_netcdf: return coda_netcdf_close(product); case coda_format_grib1: case coda_format_grib2: return coda_grib_close(product); case coda_format_hdf4: #ifdef HAVE_HDF4 return coda_hdf4_close(product); #else coda_set_error(CODA_ERROR_NO_HDF4_SUPPORT, NULL); return -1; #endif case coda_format_hdf5: #ifdef HAVE_HDF5 return coda_hdf5_close(product); #else coda_set_error(CODA_ERROR_NO_HDF5_SUPPORT, NULL); return -1; #endif case coda_format_rinex: return coda_rinex_close(product); case coda_format_sp3: return coda_sp3_close(product); } assert(0); exit(1); }
int coda_hdf4_reopen(coda_product **product) { coda_hdf4_product *product_file; product_file = (coda_hdf4_product *)malloc(sizeof(coda_hdf4_product)); if (product_file == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", sizeof(coda_hdf4_product), __FILE__, __LINE__); coda_close(*product); return -1; } product_file->filename = NULL; product_file->file_size = (*product)->file_size; product_file->format = coda_format_hdf4; product_file->root_type = NULL; product_file->product_definition = NULL; product_file->product_variable_size = NULL; product_file->product_variable = NULL; product_file->mem_size = 0; product_file->mem_ptr = NULL; product_file->is_hdf = 0; product_file->file_id = -1; product_file->gr_id = -1; product_file->sd_id = -1; product_file->an_id = -1; product_file->num_gr_file_attributes = 0; product_file->num_sd_file_attributes = 0; product_file->num_sds = 0; product_file->sds = NULL; product_file->num_images = 0; product_file->gri = NULL; product_file->num_vgroup = 0; product_file->vgroup = NULL; product_file->num_vdata = 0; product_file->vdata = NULL; product_file->filename = strdup((*product)->filename); if (product_file->filename == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate filename string) (%s:%u)", __FILE__, __LINE__); coda_hdf4_close((coda_product *)product_file); coda_close(*product); return -1; } coda_close(*product); product_file->is_hdf = Hishdf(product_file->filename); /* is this a real HDF4 file or a (net)CDF file */ if (product_file->is_hdf) { product_file->file_id = Hopen(product_file->filename, DFACC_READ, 0); if (product_file->file_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } if (Vstart(product_file->file_id) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } product_file->gr_id = GRstart(product_file->file_id); if (product_file->gr_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } product_file->an_id = ANstart(product_file->file_id); if (product_file->an_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } } product_file->sd_id = SDstart(product_file->filename, DFACC_READ); if (product_file->sd_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } product_file->root_type = NULL; if (init_SDSs(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } if (product_file->is_hdf) { if (init_GRImages(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } if (init_Vdatas(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } /* initialization of Vgroup entries should happen last, so we can build the structural tree */ if (init_Vgroups(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } } if (coda_hdf4_create_root(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } *product = (coda_product *)product_file; return 0; }