int ecl_util_get_sizeof_ctype_fortio(ecl_type_enum ecl_type) { int size = ecl_util_get_sizeof_ctype ( ecl_type ); if (ecl_type == ECL_CHAR_TYPE) size = ECL_STRING_LENGTH * sizeof(char); return size; }
void gen_data_read_from_buffer(gen_data_type * gen_data , buffer_type * buffer , int report_step, state_enum state) { int size; enkf_util_assert_buffer_type(buffer , GEN_DATA); size = buffer_fread_int(buffer); buffer_fskip_int( buffer ); /* Skipping report_step from the buffer - was a mistake to store it - I think ... */ { size_t byte_size = size * ecl_util_get_sizeof_ctype( gen_data_config_get_internal_type ( gen_data->config )); size_t compressed_size = buffer_get_remaining_size( buffer ); gen_data->data = util_realloc( gen_data->data , byte_size ); buffer_fread_compressed( buffer , compressed_size , gen_data->data , byte_size ); } gen_data_assert_size( gen_data , size , report_step ); gen_data_config_load_active( gen_data->config , report_step , false ); }
void ecl_util_memcpy_typed_data(void *_target_data , const void * _src_data , ecl_type_enum target_type , ecl_type_enum src_type, int size) { int i; if (target_type == src_type) memcpy(_target_data , _src_data , size * ecl_util_get_sizeof_ctype(src_type)); else { switch (target_type) { case(ECL_DOUBLE_TYPE): { double * target_data = (double *) _target_data; switch(src_type) { case(ECL_FLOAT_TYPE): util_float_to_double(target_data , (const float *) _src_data , size); break; case(ECL_INT_TYPE): for (i = 0; i < size; i++) target_data[i] = ((int *) _src_data)[i]; break; default: util_abort("%s: double type can only load from int/float/double - aborting \n",__func__); } break; } case(ECL_FLOAT_TYPE): { float * target_data = (float *) _target_data; switch(src_type) { case(ECL_FLOAT_TYPE): util_double_to_float(target_data , (const double *) _src_data , size); break; case(ECL_INT_TYPE): for (i = 0; i < size; i++) target_data[i] = ((int *) _src_data)[i]; break; default: util_abort("%s: float type can only load from int/float/double - aborting \n",__func__); } break; } default: util_abort("%s con not convert %d -> %d \n",__func__ , src_type , target_type); } } }
static void gen_data_set_data__(gen_data_type * gen_data , int size, int report_step , ecl_type_enum load_type , const void * data) { gen_data_assert_size(gen_data , size, report_step); if (gen_data_config_is_dynamic( gen_data->config )) gen_data_config_update_active( gen_data->config , report_step , gen_data->active_mask); gen_data_realloc_data(gen_data); if (size > 0) { ecl_type_enum internal_type = gen_data_config_get_internal_type( gen_data->config ); int byte_size = ecl_util_get_sizeof_ctype( internal_type ) * size ; if (load_type == internal_type) memcpy(gen_data->data , data , byte_size ); else { if (load_type == ECL_FLOAT_TYPE) util_float_to_double((double *) gen_data->data , data , size); else util_double_to_float((float *) gen_data->data , data , size); } } }
void field_config_set_ecl_type(field_config_type * config , ecl_type_enum ecl_type) { config->internal_ecl_type = ecl_type; config->sizeof_ctype = ecl_util_get_sizeof_ctype(ecl_type); }
static char * fscanf_alloc_grdecl_data( const char * header , bool strict , ecl_type_enum ecl_type , int * kw_size , FILE * stream ) { char newline = '\n'; bool atEOF = false; int init_size = 32; int buffer_size = 64; int data_index = 0; int sizeof_ctype = ecl_util_get_sizeof_ctype( ecl_type ); int data_size = init_size; char * buffer = util_calloc( (buffer_size + 1) , sizeof * buffer ); char * data = util_calloc( sizeof_ctype * data_size , sizeof * data ); while (true) { if (fscanf(stream , "%32s" , buffer) == 1) { if (strcmp(buffer , ECL_COMMENT_STRING) == 0) { // We have read a comment marker - just read up to the end of line. char c; while (true) { c = fgetc( stream ); if (c == newline) break; if (c == EOF) { atEOF = true; break; } } } else if (strcmp(buffer , ECL_DATA_TERMINATION) == 0) break; else { // We have read a valid input string; scan numerical input values from it. // The multiplier algorithm will fail hard if there are spaces on either side // of the '*'. int multiplier; void * value_ptr = NULL; bool char_input = false; if (ecl_type == ECL_INT_TYPE) { int value; if (sscanf(buffer , "%d*%d" , &multiplier , &value) == 2) {} else if (sscanf( buffer , "%d" , &value) == 1) multiplier = 1; else { char_input = true; if (strict) util_abort("%s: Malformed content:\"%s\" when reading keyword:%s \n",__func__ , buffer , header); } value_ptr = &value; } else if (ecl_type == ECL_FLOAT_TYPE) { float value; if (sscanf(buffer , "%d*%g" , &multiplier , &value) == 2) {} else if (sscanf( buffer , "%g" , &value) == 1) multiplier = 1; else { char_input = true; if (strict) util_abort("%s: Malformed content:\"%s\" when reading keyword:%s \n",__func__ , buffer , header); } value_ptr = &value; } else if (ecl_type == ECL_DOUBLE_TYPE) { double value; if (sscanf(buffer , "%d*%lg" , &multiplier , &value) == 2) {} else if (sscanf( buffer , "%lg" , &value) == 1) multiplier = 1; else { char_input = true; if (strict) util_abort("%s: Malformed content:\"%s\" when reading keyword:%s \n",__func__ , buffer , header); } value_ptr = &value; } else util_abort("%s: sorry type:%s not supported \n",__func__ , ecl_util_get_type_name(ecl_type)); if (char_input) fprintf(stderr,"Warning: character string: \'%s\' ignored when reading keyword:%s \n",buffer , header); else { if (data_index + multiplier >= data_size) { data_size = 2*(data_index + multiplier); data = util_realloc( data , sizeof_ctype * data_size * sizeof * data); } iset_range( data , data_index , sizeof_ctype , value_ptr , multiplier ); data_index += multiplier; } } if (atEOF) break; } else break; } free( buffer ); *kw_size = data_index; data = util_realloc( data , sizeof_ctype * data_index * sizeof * data ); return data; }
int gen_data_config_get_byte_size( const gen_data_config_type * config , int report_step) { int byte_size = gen_data_config_get_data_size( config , report_step ) * ecl_util_get_sizeof_ctype( config->internal_type ); return byte_size; }
static void gen_data_ecl_write_binary(const gen_data_type * gen_data , const char * file , ecl_type_enum export_type) { FILE * stream = util_fopen(file , "w"); int sizeof_ctype = ecl_util_get_sizeof_ctype( export_type ); util_fwrite( gen_data->data , sizeof_ctype , gen_data_config_get_data_size( gen_data->config , gen_data->current_report_step) , stream , __func__); fclose(stream); }