void test_write_header() { int nx = 10; int ny = 10; int nz = 5; int_vector_type * actnum = int_vector_alloc( nx*ny*nz , 1 ); test_work_area_type * test_area = test_work_area_alloc( "ecl_init_file" ); time_t start_time = util_make_date(15 , 12 , 2010 ); ecl_grid_type * ecl_grid; int_vector_iset( actnum , 10 , 0 ); int_vector_iset( actnum , 100 , 0 ); ecl_grid = ecl_grid_alloc_rectangular(nx, ny, nz, 1, 1, 1, int_vector_get_ptr( actnum )); // Write poro with global size. { fortio_type * f = fortio_open_writer( "FOO1.INIT" , false , ECL_ENDIAN_FLIP ); ecl_kw_type * poro = ecl_kw_alloc( "PORO" , ecl_grid_get_global_size( ecl_grid ) , ECL_FLOAT_TYPE ); ecl_kw_scalar_set_float( poro , 0.10 ); ecl_init_file_fwrite_header( f , ecl_grid , poro , 7 , start_time ); ecl_kw_free( poro ); fortio_fclose( f ); } // Write poro with nactive size. { fortio_type * f = fortio_open_writer( "FOO2.INIT" , false , ECL_ENDIAN_FLIP ); ecl_kw_type * poro = ecl_kw_alloc( "PORO" , ecl_grid_get_global_size( ecl_grid ) , ECL_FLOAT_TYPE ); ecl_kw_scalar_set_float( poro , 0.10 ); ecl_init_file_fwrite_header( f , ecl_grid , poro , 7 , start_time ); ecl_kw_free( poro ); fortio_fclose( f ); } { ecl_file_type * file1 = ecl_file_open( "FOO1.INIT" , 0 ); ecl_file_type * file2 = ecl_file_open( "FOO2.INIT" , 0 ); test_assert_true( ecl_kw_equal( ecl_file_iget_named_kw( file1 , "PORV" , 0 ) , ecl_file_iget_named_kw( file2 , "PORV" , 0))); ecl_file_close( file2 ); ecl_file_close( file1 ); } // Poro == NULL { fortio_type * f = fortio_open_writer( "FOO3.INIT" , false , ECL_ENDIAN_FLIP ); ecl_init_file_fwrite_header( f , ecl_grid , NULL , 7 , start_time ); fortio_fclose( f ); } test_work_area_free( test_area ); }
void test_at_eof() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated2" ); { fortio_type * fortio = fortio_open_writer("PRESSURE" , false , true); void * buffer = util_malloc( 100 ); fortio_fwrite_record( fortio , buffer , 100); free( buffer ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("PRESSURE" , false , true); test_assert_false( fortio_read_at_eof( fortio )); fortio_fseek( fortio , 50 , SEEK_SET ); test_assert_false( fortio_read_at_eof( fortio )); fortio_fseek( fortio , 0 , SEEK_END ); test_assert_true( fortio_read_at_eof( fortio )); fortio_fclose( fortio ); } test_work_area_free( work_area ); }
void test_create_simple() { test_work_area_type * work_area = test_work_area_alloc("nnc-INIT"); { int nx = 10; int ny = 10; int nz = 10; ecl_grid_type * grid0 = ecl_grid_alloc_rectangular(nx,ny,nz,1,1,1,NULL); ecl_grid_add_self_nnc(grid0, 0 ,nx*ny + 0, 0 ); ecl_grid_add_self_nnc(grid0, 1 ,nx*ny + 1, 1 ); ecl_grid_add_self_nnc(grid0, 2 ,nx*ny + 2, 2 ); { ecl_nnc_geometry_type * nnc_geo = ecl_nnc_geometry_alloc( grid0 ); test_assert_int_equal( ecl_nnc_geometry_size( nnc_geo ) , 3 ); /* Create a dummy INIT file which *ony* contains a TRANNC keyword with the correct size. */ { ecl_kw_type * trann_nnc = ecl_kw_alloc(TRANNNC_KW , ecl_nnc_geometry_size( nnc_geo ), ECL_FLOAT); fortio_type * f = fortio_open_writer( "TEST.INIT" , false, ECL_ENDIAN_FLIP ); for (int i=0; i < ecl_kw_get_size( trann_nnc); i++) ecl_kw_iset_float( trann_nnc , i , i*1.0 ); ecl_kw_fwrite( trann_nnc , f ); fortio_fclose( f ); ecl_kw_free( trann_nnc ); } } ecl_grid_free( grid0 ); } test_work_area_free( work_area ); }
void test_fread_alloc() { test_work_area_type * work_area = test_work_area_alloc("ecl_kw_fread" ); { ecl_kw_type * kw1 = ecl_kw_alloc( "INT" , 100 , ECL_INT ); int i; for (i=0; i < 100; i++) ecl_kw_iset_int( kw1 , i , i ); { fortio_type * fortio = fortio_open_writer("INT" , false , true ); ecl_kw_fwrite( kw1 , fortio ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("INT" , false , true ); ecl_kw_type * kw2 = ecl_kw_fread_alloc( fortio ); test_assert_true( ecl_kw_is_instance( kw2 )); test_assert_true( ecl_kw_equal( kw1 , kw2 )); ecl_kw_free( kw2 ); fortio_fclose( fortio ); } { offset_type file_size = util_file_size("INT"); test_truncated("INT" , file_size - 4 ); test_truncated("INT" , file_size - 25 ); test_truncated("INT" , 5 ); test_truncated("INT" , 0 ); } ecl_kw_free( kw1 ); } test_work_area_free( work_area ); }
void test_fseek() { test_work_area_type * work_area = test_work_area_alloc("fortio_fseek" ); { fortio_type * fortio = fortio_open_writer("PRESSURE" , false , true); void * buffer = util_malloc( 100 ); fortio_fwrite_record( fortio , buffer , 100); free( buffer ); fortio_fclose( fortio ); } { fortio_type * fortio = fortio_open_reader("PRESSURE" , false , true); printf("Starting fssek test \n"); test_assert_true( fortio_fseek( fortio , 0 , SEEK_SET )); test_assert_true( fortio_fseek( fortio , 0 , SEEK_END )); test_assert_false( fortio_fseek( fortio , 100000 , SEEK_END)); test_assert_false( fortio_fseek( fortio , 100000 , SEEK_SET)); fortio_fclose( fortio ); } test_work_area_free( work_area ); }
void test_fread_truncated_tail() { test_work_area_type * work_area = test_work_area_alloc("fortio_truncated2" ); { const size_t buffer_size = 1000; void * buffer = util_malloc( buffer_size ); { fortio_type * fortio = fortio_open_writer( "PRESSURE" , false , true ); fortio_fwrite_record( fortio , buffer , buffer_size ); fortio_fseek( fortio , 0 , SEEK_SET); util_ftruncate( fortio_get_FILE(fortio) , buffer_size + 4); fortio_fclose( fortio ); } test_assert_long_equal( util_file_size( "PRESSURE") , buffer_size + 4); { fortio_type * fortio = fortio_open_reader( "PRESSURE" , false , true ); test_assert_false( fortio_fread_buffer( fortio , buffer , buffer_size )); fortio_fclose( fortio ); } free( buffer ); } test_work_area_free( work_area ); }
void test_write( const char * filename , bool path_exists) { fortio_type * fortio = fortio_open_writer( filename , false , ECL_ENDIAN_FLIP); if (path_exists) { test_assert_not_NULL( fortio ); fortio_fclose( fortio ); } else test_assert_NULL( fortio ); }
FortIO::FortIO(const std::string& filename , std::ios_base::openmode mode , bool fmt_file , bool endian_flip_header) { if (mode == std::ios_base::in) { if (util_file_exists( filename.c_str() )) { fortio_type * c_ptr = fortio_open_reader( filename.c_str() , fmt_file , endian_flip_header); m_fortio.reset( c_ptr , fortio_fclose ); } else throw std::invalid_argument("File " + filename + " does not exist"); } else { fortio_type * c_ptr = fortio_open_writer( filename.c_str() , fmt_file , endian_flip_header); m_fortio.reset( c_ptr , fortio_fclose ); } }
static void ecl_sum_data_fwrite_unified( const ecl_sum_data_type * data , const char * ecl_case , bool fmt_case ) { char * filename = ecl_util_alloc_filename( NULL , ecl_case , ECL_UNIFIED_SUMMARY_FILE , fmt_case , 0 ); fortio_type * fortio = fortio_open_writer( filename , fmt_case , ECL_ENDIAN_FLIP ); int report_step; for (report_step = data->first_report_step; report_step <= data->last_report_step; report_step++) { if (ecl_sum_data_has_report_step( data , report_step )) ecl_sum_data_fwrite_report__( data , report_step , fortio ); } fortio_fclose( fortio ); free( filename ); }
void ecl_file_fwrite(const ecl_file_type * ecl_file , const char * filename, bool fmt_file) { bool __fmt_file; ecl_file_enum file_type; file_type = ecl_util_get_file_type( filename , &__fmt_file , NULL); if (file_type == ECL_OTHER_FILE) __fmt_file = fmt_file; { fortio_type * target = fortio_open_writer( filename , __fmt_file , ECL_ENDIAN_FLIP); ecl_file_fwrite_fortio( ecl_file , target , 0); fortio_fclose( target ); } }
void ecl_rft_file_update(const char * rft_file_name, ecl_rft_node_type ** nodes,int num_nodes, ert_ecl_unit_enum unit_set){ ecl_rft_file_type * rft_file; if(util_file_exists(rft_file_name)){ int node_index; rft_file = ecl_rft_file_alloc( rft_file_name ); for(node_index = 0; node_index < num_nodes; node_index++) { ecl_rft_node_type * new_node = nodes[node_index]; int storage_index = ecl_rft_file_get_node_index_time_rft(rft_file, ecl_rft_node_get_well_name(new_node), ecl_rft_node_get_date(new_node)); if (storage_index == -1) { ecl_rft_file_add_node(rft_file, new_node); } else { vector_iset_owned_ref(rft_file->data, storage_index, new_node,ecl_rft_node_free__); } } }else{ int node_index; rft_file = ecl_rft_file_alloc_empty( rft_file_name ); for(node_index = 0; node_index < num_nodes; node_index++) { ecl_rft_file_add_node(rft_file, nodes[node_index]); } } { bool fmt_file = false; fortio_type * fortio = fortio_open_writer( rft_file_name , fmt_file , ECL_ENDIAN_FLIP ); int node_index; /** The sorting here works directly on the internal node storage rft_file->data; that might in principle ruin the indexing of the ecl_file object - it is therefor absolutely essential that this ecl_rft_file object does not live beyond this function, and also that the ecl_rft_file api functions are avoided for the rest of this function. */ vector_sort(rft_file->data,(vector_cmp_ftype *) ecl_rft_node_cmp); for(node_index=0; node_index < vector_get_size( rft_file->data ); node_index++) { const ecl_rft_node_type *new_node = vector_iget_const(rft_file->data, node_index); ecl_rft_node_fwrite(new_node, fortio, unit_set); } fortio_fclose( fortio ); } ecl_rft_file_free(rft_file); }
void test_kw_io_charlength() { test_work_area_type * work_area = test_work_area_alloc("ecl_kw_io_charlength"); { const char * KW0 = "QWERTYUI"; const char * KW1 = "ABCDEFGHIJTTTTTTTTTTTTTTTTTTTTTTABCDEFGHIJKLMNOP"; ecl_kw_type * ecl_kw_out0 = ecl_kw_alloc(KW0 , 5, ECL_FLOAT); ecl_kw_type * ecl_kw_out1 = ecl_kw_alloc(KW1 , 5, ECL_FLOAT); for (int i=0; i < ecl_kw_get_size( ecl_kw_out1); i++) { ecl_kw_iset_float( ecl_kw_out0 , i , i*1.5 ); ecl_kw_iset_float( ecl_kw_out1 , i , i*1.5 ); } { fortio_type * f = fortio_open_writer( "TEST1" , false, ECL_ENDIAN_FLIP ); test_assert_true( ecl_kw_fwrite( ecl_kw_out0, f )); test_assert_false(ecl_kw_fwrite( ecl_kw_out1, f )); fortio_fclose( f ); } { test_assert_false( util_file_exists( "TEST1")); } { FILE * file = util_fopen("TEST2", "w"); ecl_kw_fprintf_grdecl(ecl_kw_out1 , file); fclose(file); } { FILE * file = util_fopen("TEST2", "r"); ecl_kw_type * ecl_kw_in = ecl_kw_fscanf_alloc_grdecl( file , KW1 , -1 , ECL_FLOAT); test_assert_string_equal(KW1, ecl_kw_get_header(ecl_kw_in) ); test_assert_int_equal(5, ecl_kw_get_size( ecl_kw_in) ); test_assert_double_equal(ecl_kw_iget_as_double(ecl_kw_in, 0), 0.0); test_assert_double_equal(ecl_kw_iget_as_double(ecl_kw_in, 4), 6.0); fclose(file); } ecl_kw_free( ecl_kw_out0 ); ecl_kw_free( ecl_kw_out1 ); } test_work_area_free( work_area ); }
void test_send_fortio_to_gen_kw_ecl_write(void * arg) { enkf_main_type * enkf_main = arg; test_assert_not_NULL(enkf_main); fortio_type * fortio = fortio_open_writer("my_new_file", false, ECL_ENDIAN_FLIP); test_assert_not_NULL(fortio); enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); test_assert_not_NULL(state); enkf_node_type * enkf_node = enkf_state_get_node( state , "MULTFLT" ); test_assert_not_NULL(enkf_node); const enkf_config_node_type * config_node = enkf_node_get_config(enkf_node); test_assert_not_NULL(config_node); if (GEN_KW == enkf_config_node_get_impl_type(config_node)) { const char * dummy_path = "dummy_path"; enkf_node_ecl_write(enkf_node, dummy_path, fortio, 0); } }
int main(int argc, char ** argv) { if (argc < 4) { fprintf(stderr,"%s src_file target_file kw1 kw2 kw3 \n",argv[0]); exit(0); } { const char * src_file = argv[1]; const char * target_file = argv[2]; const char ** kw_list = (const char **) &argv[3]; int num_kw = argc - 3; fortio_type * fortio_src; fortio_type * fortio_target; bool fmt_src , fmt_target; set_type * kw_set = set_alloc( num_kw , kw_list ); if (!ecl_util_fmt_file(src_file, &fmt_src)) util_exit("Hmm - could not determine formatted/unformatted status for:%s \n",src_file); fmt_target = fmt_src; /* Can in principle be different */ fortio_src = fortio_open_reader(src_file , fmt_src , ECL_ENDIAN_FLIP); fortio_target = fortio_open_writer(target_file , fmt_target , ECL_ENDIAN_FLIP); { ecl_kw_type * ecl_kw = ecl_kw_alloc_empty(); while (true) { if (ecl_kw_fread_header( ecl_kw , fortio_src )) { const char * header = ecl_kw_get_header( ecl_kw ); if (set_has_key( kw_set , header )) { ecl_kw_fread_realloc_data(ecl_kw , fortio_src ); ecl_kw_fwrite( ecl_kw , fortio_target ); } else ecl_kw_fskip_data( ecl_kw , fortio_src ); } else break; /* We have reached EOF */ } ecl_kw_free( ecl_kw ); } fortio_fclose(fortio_src); fortio_fclose(fortio_target); set_free( kw_set ); } }
// Inspired by the ecl_pack application in the ERT library void concatenateRestart(const std::string& basename) { std::string inputPath, inputBase; splitBasename(basename, inputPath, inputBase); stringlist_type* inputFiles = stringlist_alloc_new(); const int numFiles = ecl_util_select_filelist(inputPath.c_str(), inputBase.c_str(), ECL_RESTART_FILE, false, inputFiles); const char* target_file_name = ecl_util_alloc_filename(inputPath.c_str(), inputBase.c_str(), ECL_UNIFIED_RESTART_FILE, false, -1); fortio_type* target = fortio_open_writer(target_file_name, false, ECL_ENDIAN_FLIP); int dummy; ecl_kw_type* seqnum_kw = ecl_kw_alloc_new("SEQNUM", 1, ECL_INT, &dummy); int reportStep = 0; for (int i = 0; i < numFiles; ++i) { ecl_util_get_file_type(stringlist_iget(inputFiles, i), nullptr, &reportStep); ecl_file_type* src_file = ecl_file_open(stringlist_iget(inputFiles, i), 0); ecl_kw_iset_int(seqnum_kw, 0, reportStep); ecl_kw_fwrite(seqnum_kw, target); ecl_file_fwrite_fortio(src_file, target, 0); ecl_file_close(src_file); } fortio_fclose(target); stringlist_free(inputFiles); }
int main(int argc, char ** argv) { int num_files = argc - 1; if (num_files >= 1) { /* File type and formatted / unformatted is determined from the first argument on the command line. */ char * ecl_base; char * path; ecl_file_enum file_type , target_type; bool fmt_file; /** Look at the first command line argument to determine type and formatted/unformatted status. */ file_type = ecl_util_get_file_type( argv[1] , &fmt_file , NULL); if (file_type == ECL_SUMMARY_FILE) target_type = ECL_UNIFIED_SUMMARY_FILE; else if (file_type == ECL_RESTART_FILE) target_type = ECL_UNIFIED_RESTART_FILE; else { util_exit("The ecl_pack program can only be used with ECLIPSE restart files or summary files.\n"); target_type = -1; } util_alloc_file_components( argv[1] , &path , &ecl_base , NULL); /** Will pack to cwd, even though the source files might be somewhere else. To unpack to the same directory as the source files, just send in @path as first argument when creating the target_file. */ { msg_type * msg; int i , report_step , prev_report_step; char * target_file_name = ecl_util_alloc_filename( NULL , ecl_base , target_type , fmt_file , -1); stringlist_type * filelist = stringlist_alloc_argv_copy( (const char **) &argv[1] , num_files ); ecl_kw_type * seqnum_kw = NULL; fortio_type * target = fortio_open_writer( target_file_name , fmt_file , ECL_ENDIAN_FLIP); if (target_type == ECL_UNIFIED_RESTART_FILE) { int dummy; seqnum_kw = ecl_kw_alloc_new("SEQNUM" , 1 , ECL_INT_TYPE , &dummy); } { char * msg_format = util_alloc_sprintf("Packing %s <= " , target_file_name); msg = msg_alloc( msg_format , false); free( msg_format ); } msg_show( msg ); stringlist_sort( filelist , ecl_util_fname_report_cmp); prev_report_step = -1; for (i=0; i < num_files; i++) { ecl_file_enum this_file_type; this_file_type = ecl_util_get_file_type( stringlist_iget(filelist , i) , NULL , &report_step); if (this_file_type == file_type) { if (report_step == prev_report_step) util_exit("Tried to write same report step twice: %s / %s \n", stringlist_iget(filelist , i-1) , stringlist_iget(filelist , i)); prev_report_step = report_step; msg_update(msg , stringlist_iget( filelist , i)); { ecl_file_type * src_file = ecl_file_open( stringlist_iget( filelist , i) , 0 ); if (target_type == ECL_UNIFIED_RESTART_FILE) { /* Must insert the SEQNUM keyword first. */ ecl_kw_iset_int(seqnum_kw , 0 , report_step); ecl_kw_fwrite( seqnum_kw , target ); } ecl_file_fwrite_fortio( src_file , target , 0); ecl_file_close( src_file ); } } /* Else skipping file of incorrect type. */ } msg_free(msg , false); fortio_fclose( target ); free(target_file_name); stringlist_free( filelist ); if (seqnum_kw != NULL) ecl_kw_free(seqnum_kw); } free(ecl_base); util_safe_free(path); } }
void unpack_file(const char * filename) { ecl_file_enum target_type = ECL_OTHER_FILE; ecl_file_enum file_type; bool fmt_file; file_type = ecl_util_get_file_type(filename , &fmt_file , NULL); if (file_type == ECL_UNIFIED_SUMMARY_FILE) target_type = ECL_SUMMARY_FILE; else if (file_type == ECL_UNIFIED_RESTART_FILE) target_type = ECL_RESTART_FILE; else util_exit("Can only unpack unified ECLIPSE summary and restart files\n"); if (target_type == ECL_SUMMARY_FILE) { printf("** Warning: when unpacking unified summary files it as ambigous - starting with 0001 -> \n"); } { ecl_file_type * src_file = ecl_file_open( filename , 0 ); int size; int offset; int report_step = 0; int block_index = 0; char * path; char * base; msg_type * msg; util_alloc_file_components( filename , &path , &base , NULL); { char * label = util_alloc_sprintf("Unpacking %s => ", filename); msg = msg_alloc( label , false); free( label ); } msg_show(msg); if (target_type == ECL_SUMMARY_FILE) size = ecl_file_get_num_named_kw( src_file , "SEQHDR" ); else size = ecl_file_get_num_named_kw( src_file , "SEQNUM" ); while (true) { if (block_index == size) break; if (target_type == ECL_SUMMARY_FILE) { ecl_file_select_block( src_file , SEQHDR_KW , block_index ); report_step += 1; offset = 0; } else { ecl_kw_type * seqnum_kw; ecl_file_select_block( src_file , SEQNUM_KW , block_index ); seqnum_kw = ecl_file_iget_named_kw( src_file , SEQNUM_KW , 0); report_step = ecl_kw_iget_int( seqnum_kw , 0); offset = 1; } /** Will unpack to cwd, even though the source files might be somewhere else. To unpack to the same directory as the source files, just send in @path as first argument when creating the target_file. */ { char * target_file = ecl_util_alloc_filename( NULL , base , target_type , fmt_file , report_step); fortio_type * fortio_target = fortio_open_writer( target_file , fmt_file , ECL_ENDIAN_FLIP ); msg_update(msg , target_file); ecl_file_fwrite_fortio( src_file , fortio_target , offset); fortio_fclose(fortio_target); free(target_file); } block_index++; } ecl_file_close( src_file ); util_safe_free(path); free(base); msg_free(msg , true); } }
int main( int argc , char ** argv) { int num_kw = 1000; // Total file size should roughly exceed 2GB int kw_size = 600000; ecl_kw_type * kw = ecl_kw_alloc("KW" , kw_size , ECL_INT_TYPE ); rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); int i; offset_type file_size; for (i=0; i < kw_size; i++) ecl_kw_iset_int( kw , i , rng_get_int( rng , 912732 )); { fortio_type * fortio = fortio_open_writer( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw; i++) { printf("Writing keyword %d/%d to file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fwrite( kw , fortio ); } fortio_fclose( fortio ); } /*{ fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw - 1; i++) { printf("SKipping keyword %d/%d from file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fskip( fortio ); } { ecl_kw_type * file_kw = ecl_kw_fread_alloc( fortio ); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_kw_free( file_kw ); } fortio_fclose( fortio ); } */ file_size = util_file_size( "LARGE_FILE.UNRST" ); printf("File size: %lld \n",file_size); { fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); printf("Seeking to file end: "); fortio_fseek( fortio , file_size , SEEK_SET); fortio_fclose( fortio ); printf("Seek OK \n"); } printf("Doing ecl_file_open(..)\n"); { ecl_file_type * file = ecl_file_open( "LARGE_FILE.UNRST" , 0); ecl_kw_type * file_kw = ecl_file_iget_named_kw( file , "KW" , num_kw - 1); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_file_close( file ); } remove( "LARGE_FILE.UNRST" ); exit(0); }