Added some comments + error checking

This commit is contained in:
Joakim Hove 2012-10-23 13:02:48 +02:00
parent cab0872048
commit 0c0ee4b4a8

View File

@ -15,6 +15,40 @@
#include <fortio.h>
#endif
/*
Small utility to read through an ECLIPSE input deck and replace
occurences of (large) numerical fields like COORD and ZCORN with
IMPORT statements of a binary versions of the relevant keywords. The
program will follow INCLUDE statements.
Usage: import_rewrite eclipse_case.data
*/
/*
The numerical keywords in the ECLIPSE datafile like e.g. PORO and
COORD are not annoted with type information; however when read and
written in binary form they are of type float. If the updated
datafile should be used with ECLIPSE these float values must be
exported as float; this is achieved by setting the outputFloatType
variable to ECL_FLOAT_TYPE.
In OPM all numerical fields are treated as double, hence if the OPM
EclipseParser meets an import of a float keyword it will be
converted to double. If the output from this little utility will
only be used from OPM the output can be saved as double directly by
setting the outputFloatType to ECL_DOUBLE_TYPE.
*/
const ecl_type_enum outputFloatType = ECL_DOUBLE_TYPE;
/*
Only keywords which have more >= minImportSize elements are
converted to binary form. This is to avoid convertion short keywords
like MAPAXES and TABDIMS.
*/
const int minImportSize = 10;
using namespace Opm;
@ -22,7 +56,6 @@ using namespace Opm;
static void skipKeyword( std::ifstream& is) {
std::string keyword;
EclipseGridParser::readKeyword( is , keyword );
std::cout << "Skipping: " << keyword << "Pos: " << is.tellg() << std::endl;
while (true) {
std::ios::pos_type pos = is.tellg();
@ -62,10 +95,22 @@ static void copyKeyword( std::ifstream& is , std::ofstream& os) {
static ecl_kw_type * loadFromcstdio( const std::string& filename , std::ios::pos_type& offset , ecl_type_enum ecl_type) {
ecl_kw_type * ecl_kw;
FILE * cstream = util_fopen( filename.c_str() , "r");
fseek( cstream , offset , SEEK_SET);
ecl_kw = ecl_kw_fscanf_alloc_current_grdecl( cstream , ecl_type );
offset = ftell( cstream );
fclose( cstream );
return ecl_kw;
}
static void convertKeyword( const std::string& inputFile , const std::string& outputPath , std::ifstream& is , FieldType fieldType , std::ofstream& os ) {
const ecl_type_enum outputFloatType = ECL_DOUBLE_TYPE;
static bool convertKeyword( const std::string& inputFile , const std::string& outputPath , std::ifstream& is , FieldType fieldType , std::ofstream& os ) {
bool convert = true;
ecl_type_enum ecl_type;
ecl_kw_type * ecl_kw;
@ -74,36 +119,41 @@ static void convertKeyword( const std::string& inputFile , const std::string& ou
else
ecl_type = outputFloatType;
std::cout << "InputPos: " << is.tellg() << std::endl;
{
FILE * cstream = util_fopen( inputFile.c_str() , "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 );
std::ios::pos_type inputPos = is.tellg();
ecl_kw_type * ecl_kw = loadFromcstdio( inputFile , inputPos , ecl_type );
if (ecl_kw_get_size( ecl_kw ) >= minImportSize) {
{
std::string outputFile = outputPath + "/" + ecl_kw_get_header( ecl_kw );
fortio_type * fortio = fortio_open_writer( outputFile.c_str() , 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 );
os << "IMPORT" << std::endl << " '" << outputFile << "' /" << std::endl << std::endl;
}
}
std::cout << "Exit: InputPos: " << is.tellg() << std::endl;
is.seekg( inputPos );
} else {
copyKeyword( is , os );
convert = false;
}
bool parseFile(const std::string& inputFile, std::string& outputFile) {
ecl_kw_free( ecl_kw );
}
return convert;
}
bool parseFile(const std::string& inputFile, std::string& outputFile, const std::string& indent = "") {
bool updateFile = false;
std::cout << "Reading file " << inputFile << "\n";
std::cout << indent << "Parsing " << inputFile << "\n";
{
std::ifstream is(inputFile.c_str());
if (is) {
std::ofstream os;
std::string keyword;
std::string path;
@ -129,7 +179,6 @@ bool parseFile(const std::string& inputFile, std::string& outputFile) {
outputFile = basename + "_import" + extension;
}
os.open( outputFile.c_str() );
std::cout << "Writing to file: " << outputFile << "\n";
while(is.good()) {
is >> ignoreWhitespace;
@ -137,24 +186,15 @@ bool parseFile(const std::string& inputFile, std::string& outputFile) {
std::ios::pos_type start_pos = is.tellg();
if (EclipseGridParser::readKeyword( is , keyword )) {
FieldType fieldType = EclipseGridParser::classifyKeyword( keyword );
std::cout << std::endl;
std::cout << "Have read keyword: " << keyword << " Type : " << fieldType << "\n";
switch (fieldType) {
case(Integer):
{
is.seekg( start_pos );
std::cout << "Converting keyword: " << keyword << std::endl;
convertKeyword( inputFile , path , is , fieldType , os );
updateFile = true;
break;
}
case(FloatingPoint):
{
is.seekg( start_pos );
std::cout << "Converting keyword: " << keyword << std::endl;
convertKeyword( inputFile , path , is , fieldType , os );
if (convertKeyword( inputFile , path , is , fieldType , os )) {
std::cout << indent + " " << "Writing binary file: " << path << "/" << keyword << std::endl;
updateFile = true;
}
break;
}
case(Include):
@ -163,13 +203,13 @@ bool parseFile(const std::string& inputFile, std::string& outputFile) {
if (!path.empty()) {
includeFile = path + '/' + includeFile;
}
std::cout << "Continue to includeFile: " << includeFile << std::endl;
{
bool updateInclude = parseFile( includeFile , outputFile );
std::string __outputFile;
bool updateInclude = parseFile( includeFile , __outputFile , indent + " ");
if (updateInclude) {
is.seekg( start_pos );
skipKeyword( is );
os << "INCLUDE" << std::endl << " '" << outputFile << "' /" << std::endl << std::endl;
os << "INCLUDE" << std::endl << " '" << __outputFile << "' /" << std::endl << std::endl;
}
updateFile |= updateInclude;
}
@ -177,10 +217,8 @@ bool parseFile(const std::string& inputFile, std::string& outputFile) {
}
default:
{
std::cout << "Calling copy: " << keyword << " fieldType:" << fieldType << std::endl;
is.seekg( start_pos );
copyKeyword( is , os);
std::cout << "After loop pos: " << is.tellg() << std::endl;
break;
}
}
@ -191,8 +229,12 @@ bool parseFile(const std::string& inputFile, std::string& outputFile) {
os.close();
is.close();
if (!updateFile)
if (updateFile)
std::cout << indent << "Written updated include file: " << outputFile << std::endl;
else
remove( outputFile.c_str() );
} else
std::cerr << indent << "** WARNING: Failed to open include file: " << inputFile << " for reading **" << std::endl;
}
return updateFile;
}
@ -206,7 +248,6 @@ main(int argc, char** argv)
THROW("Need the name of ECLIPSE file on command line");
{
std::string outputFile;
if (parseFile(argv[1] , outputFile))
std::cout << "Have written: " << outputFile << std::endl;
parseFile(argv[1] , outputFile);
}
}