From 00dc06f074e7eedff2253a28e58940f9d6314b73 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Mon, 22 Oct 2012 17:17:16 +0200 Subject: [PATCH] Added example/import_rewrite --- examples/Makefile.am | 43 ++++---- examples/import_rewrite.cpp | 205 ++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+), 18 deletions(-) create mode 100644 examples/import_rewrite.cpp diff --git a/examples/Makefile.am b/examples/Makefile.am index 145fbbdd..4d17a2cc 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,13 +1,13 @@ # Build-time flags needed to form example programs ERT_INCLUDE_PATH = $(ERT_ROOT)/include -AM_CPPFLAGS = \ --I$(top_srcdir) \ --I$(ERT_INCLUDE_PATH) \ +AM_CPPFLAGS = \ +-I$(top_srcdir) \ +-I$(ERT_INCLUDE_PATH) \ $(OPM_BOOST_CPPFLAGS) # All targets link to the library -LDADD = \ +LDADD = \ $(top_builddir)/lib/libopmcore.la # Convenience definition for targets that use Boost.Filesystem directly. @@ -19,9 +19,9 @@ $(top_builddir)/lib/libopmcore.la # Additional details at # https://fedoraproject.org/wiki/UnderstandingDSOLinkChange # -LINK_BOOST_FILESYSTEM = \ -$(OPM_BOOST_LDFLAGS) \ -$(BOOST_FILESYSTEM_LIB) \ +LINK_BOOST_FILESYSTEM = \ +$(OPM_BOOST_LDFLAGS) \ +$(BOOST_FILESYSTEM_LIB) \ $(BOOST_SYSTEM_LIB) # ---------------------------------------------------------------------- @@ -29,14 +29,16 @@ $(BOOST_SYSTEM_LIB) # # Please keep the list sorted. -noinst_PROGRAMS = \ -compute_tof \ -refine_wells \ -scaneclipsedeck \ -sim_2p_comp_reorder \ -sim_2p_incomp_reorder \ -sim_wateroil \ -wells_example +noinst_PROGRAMS = \ +compute_tof \ +refine_wells \ +rewrite_import \ +scaneclipsedeck \ +sim_2p_comp_reorder \ +sim_2p_incomp_reorder \ +sim_wateroil \ +wells_example + # ---------------------------------------------------------------------- # Product constituents. Must be specified for every product that's @@ -47,8 +49,13 @@ wells_example compute_tof_SOURCES = compute_tof.cpp compute_tof_LDADD = $(LDADD) $(LINK_BOOST_FILESYSTEM) + refine_wells_SOURCES = refine_wells.cpp +rewrite_import_SOURCES = rewrite_import.cpp +rewrite_import_LDADD = $(LDADD) $(LINK_BOOST_FILESYSTEM) + + sim_2p_comp_reorder_SOURCES = sim_2p_comp_reorder.cpp sim_2p_comp_reorder_LDADD = $(LDADD) $(LINK_BOOST_FILESYSTEM) @@ -67,8 +74,8 @@ if UMFPACK noinst_PROGRAMS += spu_2p spu_2p_SOURCES = spu_2p.cpp -spu_2p_LDADD = \ -$(LDADD) \ -$(LINK_BOOST_FILESYSTEM) \ +spu_2p_LDADD = \ +$(LDADD) \ +$(LINK_BOOST_FILESYSTEM) \ $(LAPACK_LIBS) $(BLAS_LIBS) $(LIBS) endif diff --git a/examples/import_rewrite.cpp b/examples/import_rewrite.cpp new file mode 100644 index 00000000..f414af08 --- /dev/null +++ b/examples/import_rewrite.cpp @@ -0,0 +1,205 @@ +#if HAVE_CONFIG_H +#include "config.h" +#endif // HAVE_CONFIG_H + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +//#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_ERT +#include + +#include + +#include +#include +#include +#include + +#endif + + +using namespace Opm; + + + +static void skipKeyword( std::ifstream& is , std::ofstream& os) { + std::ios::pos_type start_pos = is.tellg(); + std::ios::pos_type end_pos; + size_t length; + { + std::string keyword; + std::cout << "Starting in " << start_pos; + EclipseGridParser::readKeyword( is , keyword ); + while (true) { + + end_pos = is.tellg(); + if (EclipseGridParser::readKeyword( is , keyword )) + break; + else + is >> ignoreLine; + + if (!is.good()) { + is.clear(); + is.seekg( 0 , std::ios::end ); + end_pos = is.tellg(); + break; + } + } + + length = end_pos - start_pos; + } + + { + char * buffer = new char[length]; + { + is.seekg( start_pos ); + is.read( buffer , length ); + } + os.write( buffer , length ); + delete[] buffer; + } + std::cout << "Stopping in " << end_pos << std::endl; +} + + +static void convertKeyword( const char * inputFile , std::ifstream& is , FieldType fieldType , std::ofstream& os ) { + const ecl_type_enum outputFloatType = ECL_DOUBLE_TYPE; + ecl_type_enum ecl_type; + ecl_kw_type * ecl_kw; + + if (fieldType == Integer) + ecl_type = ECL_INT_TYPE; + else + ecl_type = outputFloatType; + + { + FILE * cstream = util_fopen( inputFile , "r"); + fseek( cstream , is.tellg() , SEEK_SET); + ecl_kw = ecl_kw_fscanf_alloc_current_grdecl( cstream , ecl_type ); + { + std::ios::pos_type pos = ftell( cstream ); + is.seekg( pos , std::ios::beg ); + } + util_fclose( cstream ); + + { + fortio_type * fortio = fortio_open_writer( ecl_kw_get_header( ecl_kw ) , false , ECL_ENDIAN_FLIP ); + ecl_kw_fwrite( ecl_kw , fortio ); + std::cout << "Writing binary file: " << ecl_kw_get_header( ecl_kw ) << std::endl; + fortio_fclose( fortio ); + } + } +} + + + + +int +main(int argc, char** argv) +{ + if (argc != 2) + THROW("Need the name of ECLIPSE file on command line"); + + std::cout << "Reading file " << argv[1] << "\n"; + { + std::ifstream is(argv[1]); + std::ofstream os; + std::string outputFile(argv[1]); + std::string keyword; + + + { + std::string basename; + std::string extension; + + size_t ext_pos = outputFile.rfind("."); + if (ext_pos == std::string::npos) { + basename = outputFile.substr(); + extension = ""; + } else { + basename = outputFile.substr(0,ext_pos); + extension = outputFile.substr(ext_pos); + } + + outputFile = basename + "_import" + extension; + } + os.open( outputFile.c_str() ); + std::cout << "Writing to file: " << outputFile << "\n"; + + while(is.good()) { + is >> ignoreWhitespace; + { + std::ios::pos_type start_pos = is.tellg(); + if (EclipseGridParser::readKeyword( is , keyword )) { + FieldType fieldType = EclipseGridParser::classifyKeyword( keyword ); + std::cout << "Have read keyword: " << keyword << " Type : " << fieldType << "\n"; + + is.seekg( start_pos ); + if (fieldType == Integer || fieldType == FloatingPoint) + convertKeyword( argv[1] , is , fieldType , os ); + else + skipKeyword( is , os ); + } else + is >> ignoreLine; + } + + } + + os.close(); + is.close(); + } +}