/* cc -O3 -Wall hdf2raw.c -o hdf2raw -I$HDFINC -L$HDFLIB -lmfhdf -ldf -lz -lm -ljpeg */ /** * @file hdf2raw.c * @brief Command line tool that extracts the data of a SDS issued of an HDF file, and write it as binary. * Usage: %s [-v] [-overwrite] -isds= -of= * @author Nicolas PASCAL, CGTD ICARE * @version 1.0.0 * @date 2006-03-15 * * ---------------------------------------------------------------------------- * * History : * 2006-03-15 : creation * * ---------------------------------------------------------------------------- * */ #include #include "mfhdf.h" #define MAX_NAME_LENGTH 200 /** * @brief returns the memory size used by the given hdf data type * @param data_type the type code used in HDF files (DFNT_CHAR8, DFNT_UCHAR8...) * @return the size (in bytes) used by one value of this type */ size_t hdf_sizeof(const int32 data_type) { switch(data_type) { case DFNT_CHAR8 : { return sizeof(char8) ; break ; } case DFNT_UCHAR8 : { return sizeof(uchar8); break ; } case DFNT_INT8 : { return sizeof(int8); break ; } case DFNT_UINT8 : { return sizeof(uint8); break ; } case DFNT_INT16 : { return sizeof(int16); break ; } case DFNT_UINT16 : { return sizeof(uint16); break ; } case DFNT_INT32 : { return sizeof(int32); break ; } case DFNT_UINT32 : { return sizeof(uint32); break ; } case DFNT_FLOAT32 : { return sizeof(float32); break ; } case DFNT_FLOAT64 : { return sizeof(float64); break ; } default : { fprintf(stderr,"%s: %d: unknown hdf type code: %ld\n", __FILE__, __LINE__, data_type); abort(); /* should never arrive there */ break; } } } /** * @brief command line tool entry point * @param argc as usual * @param argv[] as usual * @return 0 if all works fine, -1 either */ int main(int argc, char *argv[]) { /* command line parametres */ char input_file[MAX_NAME_LENGTH]; char output_file[MAX_NAME_LENGTH]; unsigned short int verbose=FALSE; unsigned short int overwrite=FALSE; /* generic loop iterator */ unsigned int i=0; /* index of the sds to dump */ int32 sds_to_dump=-1; /* HDF identifiers */ int32 sd_id=-1; int32 sds_id=-1; /* file pointer */ FILE *fd; /* sds get infos parametres */ char sds_name[MAX_NC_NAME]; int32 sds_rank=-1; int32 dim_sizes[MAX_VAR_DIMS]; int32 data_type=-1; int32 n_attrs=-1; int32 data_size=-1; intn status=-1; /* sds read datas */ int32 start[MAX_VAR_DIMS]; int32 edges[MAX_VAR_DIMS]; /* datas read buffer */ void *data; size_t data_size_t; /* variables initialization */ input_file[0] = (char)NULL; output_file[0] = (char)NULL; if (argc < 3) { fprintf(stderr, "Usage: %s [-v] [-overwrite] -isds= -of= \n", argv[0]); exit(-1); } /***************************************************/ /************* command line parsing **************/ /***************************************************/ for (i=1; i close the hdf file */ SDendaccess(sds_id); SDend(sd_id); /**** write the binary datas to the output file ****/ /* open the output file */ if ( (fd = fopen(output_file, "wb")) == NULL ) { fprintf(stderr, "Cannot open the file %s.\n", output_file); free(data); fclose(fd); exit(-1); } /* put the datas in the output file */ if (verbose) fprintf(stdout, "Start writing the datas to output file %s.\n", output_file); if ( fwrite(data,data_size_t,data_size,fd) != data_size) { fprintf(stderr, "An error occured while writing the output file %s.\n", output_file); free(data); fclose(fd); exit(-1); } else if (verbose) { fprintf(stdout, "Output file %s written\n", output_file); } /* free allocations */ free(data); fclose(fd); return(0); }