From d4acd85c37eac35c6ee1eb1ad46b339202bc4e97 Mon Sep 17 00:00:00 2001 From: Gregor Weiss Date: Tue, 5 Nov 2024 18:45:40 +0100 Subject: [PATCH] bwc_to_eas3 and writing decompressed data, tested output can be parsed to compress again --- include/interfaces/reader/eas3.h | 3 + src/interfaces/reader/eas3.c | 120 +++++++++++++++++++++++++++++++ src/tools/bwccmdl.c | 27 +------ 3 files changed, 126 insertions(+), 24 deletions(-) diff --git a/include/interfaces/reader/eas3.h b/include/interfaces/reader/eas3.h index c7d647b..266d91c 100644 --- a/include/interfaces/reader/eas3.h +++ b/include/interfaces/reader/eas3.h @@ -359,6 +359,9 @@ void eas3_free_data(eas3_data* data); + uchar + bwc_to_eas3(bwc_stream *const stream, eas3_data *const data); + /*----------------------------------------------------------------------------------------------------------*\ ! FUNCTION NAME: bwc_data* read_eas3(const char* const filename) ! ! -------------- ! diff --git a/src/interfaces/reader/eas3.c b/src/interfaces/reader/eas3.c index 8b817ba..8bc999e 100644 --- a/src/interfaces/reader/eas3.c +++ b/src/interfaces/reader/eas3.c @@ -317,6 +317,118 @@ eas3_add_param_name(eas3_data *const data, char *name) strcpy(data->param_names->name, name ? name : "undefined"); } +/*================================================================================================*/ +/** + * @details Parses the uncompressed output from bwc_stream into the eas3_data. + * + * @param[in] stream Pointer to uncompressed data set. + * @param[inout] data Pointer to eas3_data structure to be filled. + * + * @retval uchar + */ +/*================================================================================================*/ +uchar +bwc_to_eas3(bwc_stream *const stream, eas3_data *const data) +{ + /*-----------------------*\ + ! DEFINE INT VARIABLES: ! + \*-----------------------*/ + uint64 Lread; + uint64 size; + uint64 i; + + /*-----------------------*\ + ! DEFINE CHAR VARIABLES: ! + \*-----------------------*/ + uchar *buffer_char; + char param_name[ATTRLEN + 1] = {}; + + /*-----------------------*\ + ! DEFINE STRUCTS: ! + \*-----------------------*/ + eas3_std_params *params; + + params = &data->params; + + data->aux.ptr = calloc(stream->codestream.aux->size, sizeof(uchar)); + data->aux.pos = 0; + data->aux.len = stream->codestream.aux->size; + if(!data->aux.ptr) + { + // memory allocation error + fprintf(stderr, MEMERROR); + free(data->aux.ptr); + return 1; + } + memcpy(data->aux.ptr, stream->codestream.aux->memory, + stream->codestream.aux->size); + + aux_dequeue(data->aux, (uchar*)params, 176); + + endian_conversion(¶ms->nts, 8); + endian_conversion(¶ms->npar, 8); + endian_conversion(¶ms->ndim1, 8); + endian_conversion(¶ms->ndim2, 8); + endian_conversion(¶ms->ndim3, 8); + endian_conversion(¶ms->accuracy, 8); + if(params->accuracy != 1 && params->accuracy != 2) + { + fprintf(stderr, "o##########################################################o\n"\ + "| ERROR: The accuracy of the specified dataset is not sup- |\n"\ + "| ported by the compression algorithm. |\n"\ + "o##########################################################o\n"); + return 1; + } + + buffer_char = calloc(params->nts, sizeof(uint64)); + if(!buffer_char) + { + // memory allocation error + fprintf(stderr, MEMERROR); + free(buffer_char); + return 1; + } + aux_dequeue(data->aux, buffer_char, params->nts * sizeof(uint64)); + + if(params->attribute_mode == EAS3_ALL_ATTR) + { + buffer_char = realloc(buffer_char, params->nts * ATTRLEN * sizeof(char)); + if(!buffer_char) + { + // memory allocation error + fprintf(stderr, MEMERROR); + free(buffer_char); + return 1; + } + aux_dequeue(data->aux, buffer_char, params->nts * ATTRLEN * sizeof(char)); + + for(i = 0; i < params->npar; ++i) + { + aux_dequeue(data->aux, param_name, ATTRLEN * sizeof(char)); + eas3_add_param_name(data, param_name); + memset(param_name, 0, ATTRLEN + 1); + } + } + + size = params->ndim1 * params->ndim2 * params->ndim3 * + params->nts * params->npar; + + if(params->accuracy == 1) + { + data->field.d = NULL; + data->field.f = calloc(size, sizeof(float)); + memcpy(data->field.f, stream->out, size); + } + else if(params->accuracy == 2) + { + data->field.f = NULL; + data->field.d = calloc(size, sizeof(double)); + memcpy(data->field.d, stream->out, size); + } + + return 0; +} + /*----------------------------------------------------------------------------------------------------------*\ ! FUNCTION NAME: uchar read_eas3_header(bwc_data *const data) ! ! -------------- ! @@ -756,7 +868,13 @@ write_eas3_header(FILE *const fp, eas3_data *const data) ! Convert the size parameters, used to write the rest of ! ! the header, to little endian. ! \*--------------------------------------------------------*/ + endian_conversion(¶ms->file_type, 8); endian_conversion(¶ms->accuracy, 8); + endian_conversion(¶ms->nts, 8); + endian_conversion(¶ms->npar, 8); + endian_conversion(¶ms->ndim1, 8); + endian_conversion(¶ms->ndim2, 8); + endian_conversion(¶ms->ndim3, 8); endian_conversion(¶ms->size_time, 8); endian_conversion(¶ms->size_parameter, 8); endian_conversion(¶ms->size_dim1, 8); @@ -766,6 +884,8 @@ write_eas3_header(FILE *const fp, eas3_data *const data) endian_conversion(¶ms->udef_int_size, 8); endian_conversion(¶ms->udef_real_size, 8); + data->params = *params; + /*--------------------------------------------------------*\ ! Allocate the buffer character array. If successful, get ! ! the timestep array from the auxiliary information block ! diff --git a/src/tools/bwccmdl.c b/src/tools/bwccmdl.c index 38de588..2651b2c 100644 --- a/src/tools/bwccmdl.c +++ b/src/tools/bwccmdl.c @@ -1554,30 +1554,9 @@ int main(int argc, char *argv[]) bwc_decompress(coder, stream); - size = coder->info.nX * coder->info.nY * coder->info.nZ * - coder->info.nTS * coder->info.nPar; - - data->params.ndim1 = coder->info.nX; - data->params.ndim2 = coder->info.nY; - data->params.ndim3 = coder->info.nZ; - data->params.nts = coder->info.nTS; - data->params.npar = coder->info.nPar; - if (coder->info.data_prec == bwc_precision_single) - { - data->params.accuracy = 1; - data->field.d = NULL; - data->field.f = calloc(size, sizeof(float)); - memcpy(data->field.f, stream->out, size); - } - else if (coder->info.data_prec == bwc_precision_double) - { - data->params.accuracy = 2; - data->field.f = NULL; - data->field.d = calloc(size, sizeof(double)); - memcpy(data->field.d, stream->out, size); - } - - //write_eas3(data, cli_output); + data = calloc(1, sizeof(eas3_data)); + bwc_to_eas3(stream, data); + write_eas3(data, cli_output); goto OUT; }