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,125 +119,122 @@ 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 );
{
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 );
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 );
fortio_fclose( fortio );
os << "IMPORT" << std::endl << " '" << outputFile << "' /" << std::endl << std::endl;
os << "IMPORT" << std::endl << " '" << outputFile << "' /" << std::endl << std::endl;
}
is.seekg( inputPos );
} else {
copyKeyword( is , os );
convert = false;
}
ecl_kw_free( ecl_kw );
}
std::cout << "Exit: InputPos: " << is.tellg() << std::endl;
return convert;
}
bool parseFile(const std::string& inputFile, std::string& outputFile) {
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());
std::ofstream os;
std::string keyword;
std::string path;
{
boost::filesystem::path inputPath(inputFile);
path = inputPath.parent_path().string();
}
{
std::string basename;
std::string extension;
outputFile = inputFile;
size_t ext_pos = inputFile.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;
if (is) {
std::ofstream os;
std::string keyword;
std::string path;
{
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 );
updateFile = true;
break;
}
case(Include):
{
std::string includeFile = readString(is);
if (!path.empty()) {
includeFile = path + '/' + includeFile;
}
std::cout << "Continue to includeFile: " << includeFile << std::endl;
{
bool updateInclude = parseFile( includeFile , outputFile );
if (updateInclude) {
is.seekg( start_pos );
skipKeyword( is );
os << "INCLUDE" << std::endl << " '" << outputFile << "' /" << std::endl << std::endl;
}
updateFile |= updateInclude;
}
break;
}
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;
}
}
} else
is >> ignoreLine; // Not at a valid keyword
boost::filesystem::path inputPath(inputFile);
path = inputPath.parent_path().string();
}
}
os.close();
is.close();
if (!updateFile)
remove( outputFile.c_str() );
{
std::string basename;
std::string extension;
outputFile = inputFile;
size_t ext_pos = inputFile.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() );
while(is.good()) {
is >> ignoreWhitespace;
{
std::ios::pos_type start_pos = is.tellg();
if (EclipseGridParser::readKeyword( is , keyword )) {
FieldType fieldType = EclipseGridParser::classifyKeyword( keyword );
switch (fieldType) {
case(Integer):
case(FloatingPoint):
{
is.seekg( start_pos );
if (convertKeyword( inputFile , path , is , fieldType , os )) {
std::cout << indent + " " << "Writing binary file: " << path << "/" << keyword << std::endl;
updateFile = true;
}
break;
}
case(Include):
{
std::string includeFile = readString(is);
if (!path.empty()) {
includeFile = path + '/' + includeFile;
}
{
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;
}
updateFile |= updateInclude;
}
break;
}
default:
{
is.seekg( start_pos );
copyKeyword( is , os);
break;
}
}
} else
is >> ignoreLine; // Not at a valid keyword
}
}
os.close();
is.close();
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);
}
}