Added some comments + error checking
This commit is contained in:
parent
cab0872048
commit
0c0ee4b4a8
@ -15,6 +15,40 @@
|
|||||||
#include <fortio.h>
|
#include <fortio.h>
|
||||||
#endif
|
#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;
|
using namespace Opm;
|
||||||
|
|
||||||
@ -22,7 +56,6 @@ using namespace Opm;
|
|||||||
static void skipKeyword( std::ifstream& is) {
|
static void skipKeyword( std::ifstream& is) {
|
||||||
std::string keyword;
|
std::string keyword;
|
||||||
EclipseGridParser::readKeyword( is , keyword );
|
EclipseGridParser::readKeyword( is , keyword );
|
||||||
std::cout << "Skipping: " << keyword << "Pos: " << is.tellg() << std::endl;
|
|
||||||
while (true) {
|
while (true) {
|
||||||
std::ios::pos_type pos = is.tellg();
|
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_type_enum ecl_type;
|
||||||
ecl_kw_type * ecl_kw;
|
ecl_kw_type * ecl_kw;
|
||||||
|
|
||||||
@ -74,125 +119,122 @@ static void convertKeyword( const std::string& inputFile , const std::string& ou
|
|||||||
else
|
else
|
||||||
ecl_type = outputFloatType;
|
ecl_type = outputFloatType;
|
||||||
|
|
||||||
std::cout << "InputPos: " << is.tellg() << std::endl;
|
|
||||||
{
|
{
|
||||||
FILE * cstream = util_fopen( inputFile.c_str() , "r");
|
std::ios::pos_type inputPos = is.tellg();
|
||||||
fseek( cstream , is.tellg() , SEEK_SET);
|
ecl_kw_type * ecl_kw = loadFromcstdio( inputFile , inputPos , ecl_type );
|
||||||
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 );
|
|
||||||
|
|
||||||
{
|
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 );
|
std::string outputFile = outputPath + "/" + ecl_kw_get_header( ecl_kw );
|
||||||
ecl_kw_fwrite( ecl_kw , fortio );
|
fortio_type * fortio = fortio_open_writer( outputFile.c_str() , false , ECL_ENDIAN_FLIP );
|
||||||
std::cout << "Writing binary file: " << ecl_kw_get_header( ecl_kw ) << std::endl;
|
ecl_kw_fwrite( ecl_kw , fortio );
|
||||||
fortio_fclose( 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 updateFile = false;
|
|
||||||
std::cout << "Reading file " << 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;
|
bool parseFile(const std::string& inputFile, std::string& outputFile, const std::string& indent = "") {
|
||||||
|
bool updateFile = false;
|
||||||
outputFile = inputFile;
|
std::cout << indent << "Parsing " << inputFile << "\n";
|
||||||
size_t ext_pos = inputFile.rfind(".");
|
{
|
||||||
if (ext_pos == std::string::npos) {
|
std::ifstream is(inputFile.c_str());
|
||||||
basename = outputFile.substr();
|
if (is) {
|
||||||
extension = "";
|
std::ofstream os;
|
||||||
} else {
|
std::string keyword;
|
||||||
basename = outputFile.substr(0,ext_pos);
|
std::string path;
|
||||||
extension = outputFile.substr(ext_pos);
|
{
|
||||||
|
boost::filesystem::path inputPath(inputFile);
|
||||||
|
path = inputPath.parent_path().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
outputFile = basename + "_import" + extension;
|
{
|
||||||
}
|
std::string basename;
|
||||||
os.open( outputFile.c_str() );
|
std::string extension;
|
||||||
std::cout << "Writing to file: " << outputFile << "\n";
|
|
||||||
|
outputFile = inputFile;
|
||||||
while(is.good()) {
|
size_t ext_pos = inputFile.rfind(".");
|
||||||
is >> ignoreWhitespace;
|
if (ext_pos == std::string::npos) {
|
||||||
{
|
basename = outputFile.substr();
|
||||||
std::ios::pos_type start_pos = is.tellg();
|
extension = "";
|
||||||
if (EclipseGridParser::readKeyword( is , keyword )) {
|
} else {
|
||||||
FieldType fieldType = EclipseGridParser::classifyKeyword( keyword );
|
basename = outputFile.substr(0,ext_pos);
|
||||||
std::cout << std::endl;
|
extension = outputFile.substr(ext_pos);
|
||||||
std::cout << "Have read keyword: " << keyword << " Type : " << fieldType << "\n";
|
}
|
||||||
|
|
||||||
switch (fieldType) {
|
outputFile = basename + "_import" + extension;
|
||||||
case(Integer):
|
}
|
||||||
{
|
os.open( outputFile.c_str() );
|
||||||
is.seekg( start_pos );
|
|
||||||
std::cout << "Converting keyword: " << keyword << std::endl;
|
while(is.good()) {
|
||||||
convertKeyword( inputFile , path , is , fieldType , os );
|
is >> ignoreWhitespace;
|
||||||
updateFile = true;
|
{
|
||||||
break;
|
std::ios::pos_type start_pos = is.tellg();
|
||||||
}
|
if (EclipseGridParser::readKeyword( is , keyword )) {
|
||||||
case(FloatingPoint):
|
FieldType fieldType = EclipseGridParser::classifyKeyword( keyword );
|
||||||
{
|
switch (fieldType) {
|
||||||
is.seekg( start_pos );
|
case(Integer):
|
||||||
std::cout << "Converting keyword: " << keyword << std::endl;
|
case(FloatingPoint):
|
||||||
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 );
|
is.seekg( start_pos );
|
||||||
if (updateInclude) {
|
if (convertKeyword( inputFile , path , is , fieldType , os )) {
|
||||||
is.seekg( start_pos );
|
std::cout << indent + " " << "Writing binary file: " << path << "/" << keyword << std::endl;
|
||||||
skipKeyword( is );
|
updateFile = true;
|
||||||
os << "INCLUDE" << std::endl << " '" << outputFile << "' /" << std::endl << std::endl;
|
|
||||||
}
|
}
|
||||||
updateFile |= updateInclude;
|
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;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
default:
|
} else
|
||||||
{
|
is >> ignoreLine; // Not at a valid keyword
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
os.close();
|
||||||
os.close();
|
is.close();
|
||||||
is.close();
|
if (updateFile)
|
||||||
if (!updateFile)
|
std::cout << indent << "Written updated include file: " << outputFile << std::endl;
|
||||||
remove( outputFile.c_str() );
|
else
|
||||||
|
remove( outputFile.c_str() );
|
||||||
|
} else
|
||||||
|
std::cerr << indent << "** WARNING: Failed to open include file: " << inputFile << " for reading **" << std::endl;
|
||||||
}
|
}
|
||||||
return updateFile;
|
return updateFile;
|
||||||
}
|
}
|
||||||
@ -206,7 +248,6 @@ main(int argc, char** argv)
|
|||||||
THROW("Need the name of ECLIPSE file on command line");
|
THROW("Need the name of ECLIPSE file on command line");
|
||||||
{
|
{
|
||||||
std::string outputFile;
|
std::string outputFile;
|
||||||
if (parseFile(argv[1] , outputFile))
|
parseFile(argv[1] , outputFile);
|
||||||
std::cout << "Have written: " << outputFile << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user