Esempio n. 1
0
bool ecl_util_fmt_file(const char *filename , bool * __fmt_file) {
  /*const int min_size = 32768;*/
  const int min_size = 256; /* Veeeery small */
  
  int report_nr;
  ecl_file_enum file_type;
  bool status = true;
  bool fmt_file;
  
  if (util_file_exists(filename)) {
    file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr);
    if (file_type == ECL_OTHER_FILE) {
      if (util_file_size(filename) > min_size)
        fmt_file = util_fmt_bit8(filename);
      else 
        status = false; // Do not know ??
    }
  } else {
    file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr);
    if (file_type == ECL_OTHER_FILE) 
      status = false; // Do not know ??
  }

  *__fmt_file = fmt_file;
  return status;
}
Esempio n. 2
0
static void ecl_sum_data_fread__( ecl_sum_data_type * data , time_t load_end , const stringlist_type * filelist) {
  ecl_file_enum file_type;

  if (stringlist_get_size( filelist ) == 0)
    util_abort("%s: internal error - function called with empty list of data files.\n",__func__);
  
  file_type = ecl_util_get_file_type( stringlist_iget( filelist , 0 ) , NULL , NULL);
  if ((stringlist_get_size( filelist ) > 1) && (file_type != ECL_SUMMARY_FILE))
    util_abort("%s: internal error - when calling with more than one file - you can not supply a unified file - come on?! \n",__func__);
  {
    int filenr;
    if (file_type == ECL_SUMMARY_FILE) {
      
      /* Not unified. */
      for (filenr = 0; filenr < stringlist_get_size( filelist ); filenr++) {
        const char * data_file = stringlist_iget( filelist , filenr);
        ecl_file_enum file_type;
        int report_step;
        file_type = ecl_util_get_file_type( data_file , NULL , &report_step);
        /** 
            ECLIPSE starts a report step by writing an empty summary
            file, therefor we must verify that the ecl_file instance
            returned by ecl_file_fread_alloc() is different from NULL
            before adding it to the ecl_sum_data instance.
        */
        if (file_type != ECL_SUMMARY_FILE)
          util_abort("%s: file:%s has wrong type \n",__func__ , data_file);
        {
          ecl_file_type * ecl_file = ecl_file_open( data_file );
          ecl_sum_data_add_ecl_file( data , load_end , report_step , ecl_file , data->smspec);
          ecl_file_close( ecl_file );
        }
      }
    } else if (file_type == ECL_UNIFIED_SUMMARY_FILE) {
      ecl_file_type * ecl_file = ecl_file_open( stringlist_iget(filelist ,0 ));
      int report_step = 1;   /* <- ECLIPSE numbering - starting at 1. */
      while (true) {
        /*
          Observe that there is a number discrepancy between ECLIPSE
          and the ecl_file_select_smryblock() function. ECLIPSE
          starts counting report steps at 1; whereas the first
          SEQHDR block in the unified summary file is block zero (in
          ert counting). 
        */
        if (ecl_file_select_smryblock( ecl_file , report_step - 1)) {
          ecl_sum_data_add_ecl_file( data , load_end , report_step , ecl_file , data->smspec); 
          report_step++; 
        } else break; 
      }
      ecl_file_close( ecl_file ); 
    } else 
      util_abort("%s: invalid file type:%s \n",__func__ , ecl_util_file_type_name(file_type )); 
  } 
  ecl_sum_data_build_index( data );
}
char * ecl_rft_file_alloc_case_filename(const char * case_input ) {
  ecl_file_enum    file_type;
  bool             fmt_file;
  file_type = ecl_util_get_file_type( case_input , &fmt_file ,  NULL);
  if (file_type == ECL_RFT_FILE)
    return util_alloc_string_copy (case_input );
  else {
    char * return_file = NULL;
    char * path;
    char * basename;
    util_alloc_file_components( case_input , &path , &basename , NULL);
    if ((file_type == ECL_OTHER_FILE) || (file_type == ECL_DATA_FILE)) {      /* Impossible to infer formatted/unformatted from the case_input */
      char * RFT_file  = ecl_util_alloc_filename( path , basename , ECL_RFT_FILE , false , -1 );
      char * FRFT_file = ecl_util_alloc_filename( path , basename , ECL_RFT_FILE , true  , -1 );

      if (util_file_exists( RFT_file ))
        return_file = util_alloc_string_copy( RFT_file );
      else if (util_file_exists( FRFT_file ))
        return_file = util_alloc_string_copy( FRFT_file );

      free( RFT_file );
      free( FRFT_file );
    } else {
      char * RFT_file  = ecl_util_alloc_filename( path , basename , ECL_RFT_FILE , fmt_file , -1 );

      if (util_file_exists( RFT_file ))
        return_file = util_alloc_string_copy( RFT_file );

      free( RFT_file );
    }
    return return_file;
  }
}
Esempio n. 4
0
void test_file( const char * filename , int occurence , bool exists , const ecl_rsthead_type * true_header) {
  int report_step = ecl_util_filename_report_nr( filename );
  ecl_file_type * rst_file = ecl_file_open( filename , 0);
  ecl_file_enum file_type = ecl_util_get_file_type( filename , NULL , NULL );
  ecl_file_view_type * rst_view;
  ecl_rsthead_type * rst_head;

  if (file_type == ECL_RESTART_FILE)
    rst_view = ecl_file_get_global_view( rst_file );
  else
    rst_view = ecl_file_get_restart_view( rst_file , occurence , -1 , -1 , -1 );

  if (exists) {
    test_assert_not_NULL( rst_view );
    rst_head = ecl_rsthead_alloc( rst_view , report_step);
    test_assert_not_NULL( rst_head );

    if (occurence == 0) {
      ecl_rsthead_type * rst_head0 = ecl_rsthead_alloc( rst_view , report_step );

      test_assert_true( ecl_rsthead_equal( rst_head , rst_head0 ));
      ecl_rsthead_free( rst_head0 );
    }
    test_assert_true( ecl_rsthead_equal( rst_head , true_header ));

    ecl_rsthead_free( rst_head );
  } else
    test_assert_NULL( rst_view );

}
Esempio n. 5
0
bool ecl_util_unified_file(const char *filename) {
  int report_nr;
  ecl_file_enum file_type;
  bool fmt_file;
  file_type = ecl_util_get_file_type(filename , &fmt_file , &report_nr);
  
  if ((file_type == ECL_UNIFIED_RESTART_FILE) || (file_type == ECL_UNIFIED_SUMMARY_FILE))
    return true;
  else
    return false;
}
Esempio n. 6
0
File: ecl_sum.c Progetto: edbru/ert
static void ecl_sum_fread(ecl_sum_type * ecl_sum , const char *header_file , const stringlist_type *data_files , bool include_restart) {

  ecl_sum->smspec = ecl_smspec_fread_alloc( header_file , ecl_sum->key_join_string , include_restart);
  {
    bool fmt_file;
    ecl_util_get_file_type( header_file , &fmt_file , NULL);
    ecl_sum_set_fmt_case( ecl_sum , fmt_file );
  }
  ecl_sum_fread_data( ecl_sum , data_files , include_restart );

  {
    ecl_file_enum file_type = ecl_util_get_file_type( stringlist_iget( data_files , 0 ) , NULL , NULL);

    if (file_type == ECL_SUMMARY_FILE)
      ecl_sum_set_unified( ecl_sum , false );
    else if (file_type == ECL_UNIFIED_SUMMARY_FILE)
      ecl_sum_set_unified( ecl_sum , true);
    else
      util_abort("%s: what the f**k? \n",__func__);
  }
}
Esempio n. 7
0
void ecl_file_fwrite(const ecl_file_type * ecl_file , const char * filename, bool fmt_file) {
  bool __fmt_file;
  ecl_file_enum file_type;
  
  file_type = ecl_util_get_file_type( filename , &__fmt_file , NULL);
  if (file_type == ECL_OTHER_FILE)
    __fmt_file = fmt_file;
  
  {
    fortio_type * target = fortio_open_writer( filename , __fmt_file , ECL_ENDIAN_FLIP);
    ecl_file_fwrite_fortio( ecl_file , target , 0);
    fortio_fclose( target );
  }
}
Esempio n. 8
0
ecl_grid_dims_type * ecl_grid_dims_alloc( const char * grid_file , const char * data_file) {
  ecl_grid_dims_type * grid_dims = NULL;
  bool grid_fmt_file;
  ecl_file_enum grid_file_type = ecl_util_get_file_type( grid_file , &grid_fmt_file , NULL );
  
  
  if ((grid_file_type == ECL_GRID_FILE) || (grid_file_type == ECL_EGRID_FILE)) {
    fortio_type * grid_fortio = fortio_open_reader( grid_file , grid_fmt_file , ECL_ENDIAN_FLIP );
    if (grid_fortio) {
      grid_dims = util_malloc( sizeof * grid_dims );
      grid_dims->dims_list = vector_alloc_new( );
      
      {
        fortio_type * data_fortio = NULL;
        bool data_fmt_file;
        
        if (data_file) {
          ecl_util_get_file_type( data_file , &data_fmt_file , NULL );
          data_fortio = fortio_open_reader( data_file , data_fmt_file , ECL_ENDIAN_FLIP );
        }
      
      
        if (grid_file_type == ECL_EGRID_FILE)
          ecl_grid_dims_read_EGRID( grid_dims , grid_fortio , data_fortio );
        else
          ecl_grid_dims_read_GRID( grid_dims , grid_fortio , data_fortio );
      
        if (data_fortio)
          fortio_fclose( data_fortio );
        
      }
      fortio_fclose( grid_fortio );
    }
  }
  
  return grid_dims;
}
//--------------------------------------------------------------------------------------------------
/// Get first occurrence of file of given type in given list of filenames, as filename or nullptr if not found
//--------------------------------------------------------------------------------------------------
QString RifEclipseOutputFileTools::firstFileNameOfType(const QStringList& fileSet, ecl_file_enum fileType)
{
    int i;
    for (i = 0; i < fileSet.count(); i++)
    {
        bool formatted = false;
        int reportNumber = -1;
        if (ecl_util_get_file_type(RiaStringEncodingTools::toNativeEncoded(fileSet.at(i)).data(), &formatted, &reportNumber) == fileType)
        {
            return fileSet.at(i);
        }
    }

    return QString::null;
}
Esempio n. 10
0
void well_info_load_rst_eclfile( well_info_type * well_info , ecl_file_type * ecl_file, bool load_segment_information) {
  int report_nr;
  const char* filename = ecl_file_get_src_file(ecl_file);
  ecl_file_enum file_type = ecl_util_get_file_type( filename , NULL , &report_nr);
  if ((file_type == ECL_RESTART_FILE) || (file_type == ECL_UNIFIED_RESTART_FILE))
  {
    if (file_type == ECL_RESTART_FILE)
      well_info_add_wells( well_info , ecl_file , report_nr , load_segment_information );
    else
      well_info_add_UNRST_wells( well_info , ecl_file , load_segment_information );

  } else
    util_abort("%s: invalid file type: %s - must be a restart file\n", __func__ , filename);

}
Esempio n. 11
0
ui_return_type * ecl_config_validate_grid( const ecl_config_type * ecl_config , const char * grid_file ) {
  ui_return_type * ui_return;
  if (util_file_exists( grid_file )) {
    ecl_file_enum file_type = ecl_util_get_file_type( grid_file , NULL , NULL );
    if ((file_type == ECL_EGRID_FILE) || (file_type == ECL_GRID_FILE))
      ui_return =  ui_return_alloc( UI_RETURN_OK );
    else {
      ui_return =  ui_return_alloc( UI_RETURN_FAIL );
      ui_return_add_error( ui_return , "Input argument is not a GRID/EGRID file");
    }
  } else {
    ui_return =  ui_return_alloc( UI_RETURN_FAIL );
    ui_return_add_error( ui_return , "Input argument does not exist.");
  }
  return ui_return;
}
//--------------------------------------------------------------------------------------------------
/// Get all files of the given type from the provided list of filenames
//--------------------------------------------------------------------------------------------------
QStringList RifEclipseOutputFileTools::filterFileNamesOfType(const QStringList& fileSet, ecl_file_enum fileType)
{
    QStringList fileNames;

    int i;
    for (i = 0; i < fileSet.count(); i++)
    {
        bool formatted = false;
        int reportNumber = -1;
        if (ecl_util_get_file_type(fileSet.at(i).toAscii().data(), &formatted, &reportNumber) == fileType)
        {
            fileNames.append(fileSet.at(i));
        }
    }

    return fileNames;
}
Esempio n. 13
0
// 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);
}
Esempio n. 14
0
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);
  }
}
Esempio n. 15
0
void unpack_file(const char * filename) {
  ecl_file_enum target_type = ECL_OTHER_FILE;
  ecl_file_enum file_type;
  bool fmt_file;
  file_type = ecl_util_get_file_type(filename , &fmt_file , NULL);
  if (file_type == ECL_UNIFIED_SUMMARY_FILE)
    target_type = ECL_SUMMARY_FILE;
  else if (file_type == ECL_UNIFIED_RESTART_FILE)
    target_type = ECL_RESTART_FILE;
  else 
    util_exit("Can only unpack unified ECLIPSE summary and restart files\n");
  
  if (target_type == ECL_SUMMARY_FILE) {
    printf("** Warning: when unpacking unified summary files it as ambigous - starting with 0001  -> \n");
  }
  {
    ecl_file_type * src_file = ecl_file_open( filename , 0 );
    int    size;
    int    offset;
    int    report_step = 0;
    int    block_index = 0;
    char * path; 
    char * base;
    msg_type * msg;
    util_alloc_file_components( filename , &path , &base , NULL);
    {
      char * label  = util_alloc_sprintf("Unpacking %s => ", filename);
      msg = msg_alloc( label , false);
      free( label );
    }
    msg_show(msg);

    if (target_type == ECL_SUMMARY_FILE) 
      size = ecl_file_get_num_named_kw( src_file , "SEQHDR" );
    else
      size = ecl_file_get_num_named_kw( src_file , "SEQNUM" );
    
    
    while (true) {
      if (block_index == size)
        break;

      if (target_type == ECL_SUMMARY_FILE) {
        ecl_file_select_block( src_file , SEQHDR_KW , block_index );
        report_step += 1;
        offset = 0;
      } else {
        ecl_kw_type * seqnum_kw;
        ecl_file_select_block( src_file , SEQNUM_KW , block_index );
        seqnum_kw = ecl_file_iget_named_kw( src_file , SEQNUM_KW , 0);
        report_step = ecl_kw_iget_int( seqnum_kw , 0);
        offset = 1;
      }

      /**
         Will unpack 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.
      */
      
      {
        char * target_file = ecl_util_alloc_filename( NULL , base , target_type , fmt_file , report_step);
        fortio_type * fortio_target = fortio_open_writer( target_file , fmt_file , ECL_ENDIAN_FLIP );
        msg_update(msg , target_file);
        ecl_file_fwrite_fortio( src_file , fortio_target , offset);
        
        fortio_fclose(fortio_target);
        free(target_file);
      }
      block_index++;
    } 
    ecl_file_close( src_file );
    util_safe_free(path);
    free(base);
    msg_free(msg , true);
  }
}
Esempio n. 16
0
bool ecl_util_alloc_summary_files(const char * path , const char * _base , const char * ext , char ** _header_file , stringlist_type * filelist) {
  bool    fmt_input      = false;
  bool    fmt_set        = false;
  bool    fmt_file       = true; 
  bool    unif_input     = false;
  bool    unif_set       = false;

  
  char  * header_file    = NULL;
  char  * base;

  *_header_file = NULL;

  /* 1: We start by inspecting the input extension and see if we can
     learn anything about formatted/unformatted and
     unified/non-unified from this. The input extension can be NULL,
     in which case we learn nothing.
  */

  if (_base == NULL)
    base = ecl_util_alloc_base_guess(path);
  else
    base = (char *) _base;
  
  if (ext != NULL) {
    ecl_file_enum input_type;

    {
      char * test_name = util_alloc_filename( NULL , base, ext );
      input_type = ecl_util_get_file_type( test_name , &fmt_input , NULL);
      free( test_name );
    }

    if ((input_type != ECL_OTHER_FILE) && (input_type != ECL_DATA_FILE)) {
      /* 
         The file has been recognized as a file type from which we can
         at least infer formatted/unformatted inforamtion.
      */
      fmt_set = true;
      switch (input_type) {
      case(ECL_SUMMARY_FILE):
      case(ECL_RESTART_FILE):
        unif_input = false;
        unif_set   = true;
        break;
      case(ECL_UNIFIED_SUMMARY_FILE):
      case(ECL_UNIFIED_RESTART_FILE):
        unif_input = true;
        unif_set   = true;
        break;
      default:  /* Nothing wrong with this */
        break;
      }
    } 
  }
  
  
  /*
    2: We continue by looking for header files. 
  */
  
  {
    char * fsmspec_file = ecl_util_alloc_exfilename(path , base , ECL_SUMMARY_HEADER_FILE , true  , -1);
    char *  smspec_file = ecl_util_alloc_exfilename(path , base , ECL_SUMMARY_HEADER_FILE , false , -1);
    
    if ((fsmspec_file == NULL) && (smspec_file == NULL))   /* Neither file exists */
      return false;

    
    if (fmt_set)  /* The question of formatted|unformatted has already been settled based on the input filename. */
      fmt_file = fmt_input;
    else {
      if ((fsmspec_file != NULL) && (smspec_file != NULL)) {   /* Both fsmspec and smspec exist - we take the newest. */
        if (util_file_difftime(fsmspec_file , smspec_file) < 0) 
          fmt_file = true;
        else 
          fmt_file = false;
      } else {                                                /* Only one of fsmspec / smspec exists */
        if (fsmspec_file != NULL)
          fmt_file = true;
        else
          fmt_file = false;
      }
    }
    
    if (fmt_file) {
      header_file = fsmspec_file;
      util_safe_free( smspec_file );
    } else {
      header_file = smspec_file;
      util_safe_free( fsmspec_file );
    }

    if (header_file == NULL)
      return false;                                           /* If you insist on e.g. unformatted and only fsmspec exists - no results for you. */
  }
  
  

  /* 
     3: OK - we have found a SMSPEC / FMSPEC file - continue to look for
     XXX.Snnnn / XXX.UNSMRY files.
  */

  if (unif_set) { /* Based on the input file we have inferred whether to look for unified or
                     non-unified input files. */

    if ( unif_input ) {
      char  * unif_data_file = ecl_util_alloc_exfilename(path , base , ECL_UNIFIED_SUMMARY_FILE , fmt_file , -1);
      if (unif_data_file != NULL) {
        stringlist_append_copy( filelist , unif_data_file );
        free( unif_data_file );
      }
    } else 
      ecl_util_select_filelist( path , base , ECL_SUMMARY_FILE , fmt_file , filelist);
  } else          
    ecl_util_alloc_summary_data_files( path , base , fmt_file , filelist );
    
  if (_base == NULL)
    free(base);

  *_header_file    = header_file;
    
  return (stringlist_get_size(filelist) > 0) ? true : false;
}