Esempio n. 1
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. 2
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. 3
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. 4
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);
  }
}