Ejemplo n.º 1
0
void rml_enkf_log_open( rml_enkf_log_type * rml_log , int iteration_nr ) {
  if (rml_log->log_file) {
    if ( iteration_nr == 0) {

      if (rml_log->clear_log)
        rml_log->log_stream = util_mkdir_fopen( rml_log->log_file , "w");
      else
        rml_log->log_stream = util_mkdir_fopen( rml_log->log_file , "a");

    } else
      rml_log->log_stream = util_fopen( rml_log->log_file , "a");
  }
}
Ejemplo n.º 2
0
void test_min_realizations_percent(const char * num_realizations_str, const char * min_realizations_str, int min_realizations){
  test_work_area_type * work_area = test_work_area_alloc("test_min_realizations");

  {
    FILE * config_file_stream = util_mkdir_fopen("config_file", "w");
    test_assert_not_NULL(config_file_stream);
    fprintf(config_file_stream, num_realizations_str);
    fprintf(config_file_stream, min_realizations_str);
    fclose(config_file_stream);

    config_type * c = config_alloc();
    config_schema_item_type * item = config_add_schema_item(c , NUM_REALIZATIONS_KEY , true );
    config_schema_item_set_default_type(item, CONFIG_INT);
    config_schema_item_set_argc_minmax( item , 1 , 1);
    item = config_add_schema_item(c , MIN_REALIZATIONS_KEY , false );
    config_schema_item_set_argc_minmax( item , 1 , 2);
    test_assert_true(config_parse(c , "config_file" , "--" , NULL , NULL , false , true ));

    analysis_config_type * ac = create_analysis_config( );
    analysis_config_init(ac, c);

    test_assert_int_equal( min_realizations , analysis_config_get_min_realisations( ac ) );

    analysis_config_free( ac );
    config_free( c );
  }

  test_work_area_free(work_area);
}
Ejemplo n.º 3
0
Archivo: log.c Proyecto: jokva/ert
void log_reopen(log_type *logh , const char *filename) {
    if (logh->stream != NULL)  { /* Close the existing file descriptor. */
        size_t file_size;
        fclose( logh->stream );
        file_size = util_file_size( logh->filename );
        if (file_size == 0)
            remove( logh->filename ); /* Unlink the old log file if it had zero size. */
    }

    logh->filename = util_realloc_string_copy( logh->filename , filename );
#ifdef HAVE_PTHREAD
    pthread_mutex_lock( &logh->mutex );
#endif

    if (filename != NULL) {
        logh->stream = util_mkdir_fopen( filename , "a+");
        logh->fd     = fileno( logh->stream );
    } else {
        /* It is ~OK to open a log with NULL filename, but then
                     log_reopen() with a VALID filename must be
                     called before it is actually used. */
        logh->stream = NULL;
        logh->fd     = -1;
    }

#ifdef HAVE_PTHREAD
    pthread_mutex_unlock( &logh->mutex );
#endif
}
Ejemplo n.º 4
0
rng_type * rng_config_init_rng__(const rng_config_type * rng_config, rng_type * rng) {
    const char * seed_load  = rng_config_get_seed_load_file( rng_config );
    const char * seed_store = rng_config_get_seed_store_file( rng_config );

    if (seed_load != NULL) {
        if (util_file_exists( seed_load)) {
            FILE * stream = util_fopen( seed_load , "r");
            rng_fscanf_state( rng , stream );
            fclose( stream );
        } else {
            /*
               In the special case that seed_load == seed_store; we accept a
               seed_load argument pointing to a non-existant file.
            */
            if (seed_store) {
                if (util_string_equal( seed_store , seed_load))
                    rng_init( rng , INIT_DEV_URANDOM );
                else
                    util_abort("%s: tried to load random seed from non-existing file:%s \n",__func__ , seed_load);
            }
        }
    } else
        rng_init( rng , INIT_DEV_URANDOM );


    if (seed_store != NULL) {
        FILE * stream = util_mkdir_fopen( seed_store , "w");
        rng_fprintf_state( rng , stream );
        fclose( stream );
    }

    return rng;
}
Ejemplo n.º 5
0
void misfit_ranking_fprintf( const misfit_ranking_type * misfit_ranking , const char * filename) {
  FILE * stream                       = util_mkdir_fopen( filename , "w");
  const int ens_size                  = misfit_ranking->ens_size;
  const int * permutations            = misfit_ranking->sort_permutation;
  double summed_up = 0.0;
  {
    // All this whitespace is finely tuned and highly significant .... 
    const char * key_fmt       = " %18s ";                                
    const char * value_fmt     = " %10.3f %8.3f";
    const char * start_fmt     = " %2d       %3d     %7.3f %8.3f";  

    hash_type * obs_hash       = vector_iget( misfit_ranking->ensemble , 0);
    stringlist_type * obs_keys = hash_alloc_stringlist( obs_hash );
    int num_obs                = stringlist_get_size( obs_keys );
    int iobs;
    int num_obs_total = num_obs * ens_size;

    stringlist_sort( obs_keys , enkf_util_compare_keys__ );
    fprintf(stream , "                       Overall  ");
    for (iobs =0; iobs < num_obs; iobs++) 
      fprintf(stream , key_fmt , stringlist_iget( obs_keys , iobs ));

    fprintf(stream , "\n");
    fprintf(stream , "  #    Realization  Norm    Total");
    for (iobs =0; iobs < num_obs; iobs++) 
      fprintf(stream , "       Norm    Total");
    
    fprintf(stream , "\n");
    for (int i = 0; i < ens_size; i++) {
      int iens = permutations[i];
      hash_type * obs_hash = vector_iget( misfit_ranking->ensemble , iens );
      double total_value   = double_vector_iget( misfit_ranking->total , iens );
      double normalized_misfit = sqrt(total_value / num_obs_total);
      summed_up = summed_up+total_value;
      fprintf(stream , start_fmt , i , iens , normalized_misfit , total_value);
      for (iobs =0; iobs < num_obs; iobs++){
        double single_value = hash_get_double( obs_hash , stringlist_iget( obs_keys , iobs ));
        double single_value_normalized = sqrt(single_value / (num_obs_total));
        fprintf(stream , value_fmt , single_value_normalized , single_value);
      }
      fprintf(stream , "\n");
    }
    double summed_up_normalized = sqrt(summed_up / (num_obs_total * ens_size));
    fprintf(stream , "           All    %7.3f %8.3f" , summed_up_normalized , summed_up);
    for (iobs = 0; iobs < num_obs; iobs++){
      double single_value_summed_up = 0.0;      
      for (int i = 0; i < ens_size; i++) {  
        single_value_summed_up = single_value_summed_up + hash_get_double( obs_hash , stringlist_iget( obs_keys , iobs ));
      }
      double single_value_summed_up_normalized=sqrt(single_value_summed_up / (num_obs_total * ens_size));
      fprintf(stream , value_fmt , single_value_summed_up_normalized , single_value_summed_up);
    }
    fprintf(stream , "\n");
  }
  fclose( stream );
}
Ejemplo n.º 6
0
Archivo: ecl_sum.c Proyecto: flikka/ert
void ecl_sum_export_csv(const ecl_sum_type * ecl_sum , const char * filename  , const stringlist_type * var_list , const char * date_format , const char * sep) {
  FILE * stream = util_mkdir_fopen(filename , "w");
  char * date_header = util_alloc_sprintf("DAYS%sDATE" , sep);
  bool report_only = false;
  ecl_sum_fmt_type fmt;
  ecl_sum_fmt_init_csv( &fmt , date_format , date_header , sep );
  ecl_sum_fprintf( ecl_sum , stream , var_list , report_only , &fmt );
  fclose( stream );
  free( date_header );
}
Ejemplo n.º 7
0
void state_map_fwrite( state_map_type * map , const char * filename) {
  pthread_rwlock_rdlock( &map->rw_lock );
  {
    FILE * stream = util_mkdir_fopen( filename , "w");
    if (stream) {
      int_vector_fwrite( map->state , stream );
      fclose( stream );
    } else
      util_abort("%s: failed to open:%s for writing \n",__func__ , filename );
  }
  pthread_rwlock_unlock( &map->rw_lock );
}
Ejemplo n.º 8
0
void test_chdir() {
  test_work_area_type * work_area = test_work_area_alloc("test-area");
  const char * cwd = test_work_area_get_cwd( work_area );

  test_assert_false( util_chdir_file( "/file/does/not/exist"));
  test_assert_false( util_chdir_file( cwd ));
  {
    FILE * stream = util_mkdir_fopen("path/FILE","w");
    fclose( stream );
  }
  test_assert_true( util_chdir_file( "path/FILE" ));
  test_assert_string_equal( util_alloc_cwd() , util_alloc_filename( cwd, "path", NULL));
  test_work_area_free( work_area );
}
Ejemplo n.º 9
0
void runpath_list_fprintf(runpath_list_type * list ) {
  pthread_rwlock_rdlock( &list->lock );
  {
    FILE * stream = util_mkdir_fopen( list->export_file , "w");
    const char * line_fmt = runpath_list_get_line_fmt( list );
    int index;
    vector_sort( list->list , runpath_node_cmp );
    for (index =0; index < vector_get_size( list->list ); index++) {
      const runpath_node_type * node = runpath_list_iget_node__( list , index );
      runpath_node_fprintf( node , line_fmt , stream );
    }
    fclose( stream );
  }
  pthread_rwlock_unlock( &list->lock );
}
Ejemplo n.º 10
0
void summary_key_set_fwrite(summary_key_set_type * set, const char * filename) {
    pthread_rwlock_rdlock( &set->rw_lock );
    {
        FILE * stream = util_mkdir_fopen(filename , "w");
        if (stream) {
            stringlist_type * keys = hash_alloc_stringlist(set->key_set);
            stringlist_fwrite(keys, stream);
            stringlist_free(keys);
            fclose( stream );
        } else {
            util_abort("%s: failed to open: %s for writing \n", __func__, filename);
        }
    }
    pthread_rwlock_unlock( &set->rw_lock );
}
Ejemplo n.º 11
0
void output_save_plain__( const output_type * output , ensemble_type * ensemble , const double ** data , bool add_header) {
  FILE * stream = util_mkdir_fopen( output->file , "w");
  const char * key_fmt      = " %18s:%4.2f ";
  const char * time_header  = "--    DAYS      DATE    ";
  const char * time_dash    = "------------------------";
  const char * key_dash     = "-------------------------";
  const char * float_fmt    = "%24.5f ";
  const char * days_fmt     = "%10.2f ";
  const char * date_fmt     = "  %02d/%02d/%04d ";
  const int    data_columns = vector_get_size( output->keys );
  const int    data_rows    = time_t_vector_size( ensemble->interp_time );
  int row_nr,column_nr;

  if (add_header) {
    fprintf( stream ,time_header);
    for (int i=0; i < vector_get_size( output->keys ); i++) {
      const quant_key_type * qkey = vector_iget( output->keys , i );
      fprintf( stream , key_fmt , qkey->sum_key , qkey->quantile );
    }
    fprintf(stream , "\n");

    fprintf( stream , time_dash );
    for (int i=0; i < vector_get_size( output->keys ); i++)
      fprintf(stream , key_dash );
    fprintf(stream , "\n");
  }

  /*4: Writing the actual data. */
  for (row_nr = 0; row_nr < data_rows; row_nr++) {
    time_t interp_time = time_t_vector_iget( ensemble->interp_time , row_nr);
    fprintf(stream , days_fmt , 1.0*(interp_time - ensemble->start_time) / 86400);
    {
      int mday,month,year;
      util_set_datetime_values(interp_time , NULL , NULL , NULL , &mday , &month , &year);
      fprintf(stream , date_fmt , mday , month , year);
    }

    for (column_nr = 0; column_nr < data_columns; column_nr++) {
      fprintf(stream , float_fmt , data[row_nr][column_nr]);
    }
    fprintf( stream , "\n");
  }
}
Ejemplo n.º 12
0
bool ranking_table_fwrite_ranking( const ranking_table_type * ranking_table , const char * ranking_key, const char * filename ) {
  if (hash_has_key( ranking_table->ranking_table , ranking_key)) {
    void * ranking = hash_get( ranking_table->ranking_table , ranking_key );

    FILE * file = util_mkdir_fopen(filename, "w");

    if (data_ranking_is_instance( ranking )) {
      data_ranking_type * data_ranking = data_ranking_safe_cast( ranking );
      data_ranking_display( data_ranking , file );
    } else if (misfit_ranking_is_instance( ranking )) {
      misfit_ranking_type * misfit_ranking = misfit_ranking_safe_cast( ranking );
      misfit_ranking_display( misfit_ranking , file );
    } else
      util_abort("%s: internal error \n",__func__);

    util_fclose(file);

    return true;
  } else
    return false;
}
Ejemplo n.º 13
0
void subst_list_filter_file(const subst_list_type * subst_list , const char * src_file , const char * target_file) {
  char * backup_file   = NULL;
  buffer_type * buffer = buffer_fread_alloc( src_file );
  buffer_fseek( buffer , 0 , SEEK_END );  /* Ensure that the buffer is a \0 terminated string. */
  buffer_fwrite_char( buffer , '\0');
  
  /*****************************************************************/
  /* Backup file ??  */
  if (util_same_file(src_file , target_file)) {
    char * backup_prefix = util_alloc_sprintf("%s-%s" , src_file , __func__);
    backup_file = util_alloc_tmp_file("/tmp" , backup_prefix , false);
    free(backup_prefix);
  }
  
  /* Writing backup file */
  if (backup_file != NULL) {
    FILE * stream = util_fopen(backup_file , "w");
    buffer_stream_fwrite_n( buffer , 0 , -1 , stream );  /* -1: Do not write the trailing \0. */
    fclose(stream);
  }
  
  
  /* Doing the actual update */
  subst_list_update_buffer(subst_list , buffer);
  

  /* Writing updated file */
  {
    FILE * stream = util_mkdir_fopen(target_file , "w");
    buffer_stream_fwrite_n( buffer , 0 , -1 , stream );  /* -1: Do not write the trailing \0. */
    fclose(stream);
  }
  
  /* OK - all went hunka dory - unlink the backup file and leave the building. */
  if (backup_file != NULL) {
    remove( backup_file );
    free( backup_file );
  }
  free(buffer);
}
Ejemplo n.º 14
0
Archivo: enkf_fs.c Proyecto: shulNN/ert
FILE * enkf_fs_open_case_tstep_member_file( const enkf_fs_type * fs , const char * input_name , int tstep , int iens , const char * mode) {
  char * filename = enkf_fs_alloc_case_tstep_member_filename( fs , tstep , iens , input_name );
  FILE * stream   = util_mkdir_fopen( filename , mode );
  free( filename );
  return stream;
}
Ejemplo n.º 15
0
void output_save_S3Graph( const output_type * output, ensemble_type * ensemble , const double ** data ) {
  FILE * stream = util_mkdir_fopen( output->file , "w");
  const char * kw_fmt       = "\t%s";
  const char * unit_fmt     = "\t%s";
  const char * wgname_fmt   = "\t%s";
  const char * num_fmt      = "\t%d";
  const char * float_fmt    = "\t%0.4f";
  const char * days_fmt     = "\t%0.2f";

  const char * empty_fmt    = "\t";
  const char * date_fmt     = "%d/%d/%d";
  const char * time_header  = "DATE\tTIME";
  const char * time_unit    = "\tDAYS";
  const char * time_blank   = "\t";
  const int    data_columns = vector_get_size( output->keys );
  const int    data_rows    = time_t_vector_size( ensemble->interp_time );
  int row_nr,column_nr;

  {
    char       * origin;
    util_alloc_file_components( output->file , NULL ,&origin , NULL);
    fprintf(stream , "ORIGIN %s\n", origin );
    free( origin );
  }

  /* 1: Writing first header line with variables. */
  fprintf(stream , time_header );
  for (column_nr = 0; column_nr < data_columns; column_nr++) {
    const quant_key_type * qkey = vector_iget( output->keys , column_nr );
    print_var( stream , ecl_sum_get_keyword( ensemble->refcase , qkey->sum_key ) , qkey->quantile , kw_fmt);
  }
  fprintf(stream , "\n");

  /* 2: Writing second header line with units. */
  fprintf(stream , time_unit );
  for (column_nr = 0; column_nr < data_columns; column_nr++) {
    const quant_key_type * qkey = vector_iget( output->keys , column_nr );
    fprintf(stream , unit_fmt , ecl_sum_get_unit( ensemble->refcase , qkey->sum_key ) );
  }
  fprintf(stream , "\n");

  /*3: Writing third header line with WGNAMES / NUMS - extra information -
       breaks completely down with LGR information. */
  fprintf(stream , time_blank );
  {
    for (column_nr = 0; column_nr < data_columns; column_nr++) {
      const quant_key_type * qkey  = vector_iget( output->keys , column_nr );
      const char * ecl_key         = qkey->sum_key;
      const char * wgname          = ecl_sum_get_wgname( ensemble->refcase , ecl_key );
      int          num             = ecl_sum_get_num( ensemble->refcase , ecl_key );
      ecl_smspec_var_type var_type = ecl_sum_get_var_type( ensemble->refcase , ecl_key);
      bool need_num                = ecl_smspec_needs_num( var_type );
      bool need_wgname             = ecl_smspec_needs_wgname( var_type );

      if (need_num && need_wgname) {
        /** Do not know how to include both - will just create a
            mangled name as a combination. */
        char * wgname_num = util_alloc_sprintf("%s:%d" , wgname , num);
        fprintf(stream , wgname_fmt , wgname_num);
        free( wgname_num );
      } else if (need_num)
        fprintf(stream , num_fmt , num);
      else if (need_wgname)
        fprintf(stream , wgname_fmt , wgname);
      else
        fprintf(stream , empty_fmt );
    }
    fprintf(stream , "\n");
  }

  /*4: Writing the actual data. */
  for (row_nr = 0; row_nr < data_rows; row_nr++) {
    time_t interp_time = time_t_vector_iget( ensemble->interp_time , row_nr);
    {
      int mday,month,year;
      util_set_datetime_values(interp_time , NULL , NULL , NULL , &mday , &month , &year);
      fprintf(stream , date_fmt , mday , month , year);
    }
    fprintf(stream , days_fmt , 1.0*(interp_time - ensemble->start_time) / 86400);

    for (column_nr = 0; column_nr < data_columns; column_nr++) {
      fprintf(stream , float_fmt , data[row_nr][column_nr]);
    }
    fprintf( stream , "\n");
  }
}
Ejemplo n.º 16
0
void cases_config_fwrite( cases_config_type * config , const char * filename ) {
  FILE * stream = util_mkdir_fopen(filename , "w");
  int iteration_no = cases_config_get_iteration_number(config);
  util_fwrite_int( iteration_no , stream );
  fclose( stream );
}