static ecl_kw_type * __ecl_kw_fscanf_alloc_grdecl__(FILE * stream , const char * header , bool strict , int size , ecl_type_enum ecl_type) { if (! (ecl_type == ECL_FLOAT_TYPE || ecl_type == ECL_INT_TYPE || ecl_type == ECL_DOUBLE_TYPE)) util_abort("%s: sorry only types FLOAT, INT and DOUBLE supported\n",__func__); if (header != NULL) if (!ecl_kw_grdecl_fseek_kw( header , true , stream )) return NULL; /* Could not find it. */ { char file_header[9]; if (fscanf(stream , "%s" , file_header) == 1) { int kw_size; char * data = fscanf_alloc_grdecl_data( file_header , strict , ecl_type , &kw_size , stream ); // Verify size if (size > 0) if (size != kw_size) { util_safe_free( data ); util_abort("%s: size mismatch when loading:%s. File:%d elements. Requested:%d elements \n", __func__ , file_header , kw_size , size); } { ecl_kw_type * ecl_kw = ecl_kw_alloc_new( file_header , kw_size , ecl_type , NULL ); ecl_kw_set_data_ptr( ecl_kw , data ); return ecl_kw; } } else /** No header read - probably at EOF */ return NULL; } }
// 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); } }