void model_config_set_case_table( model_config_type * model_config , int ens_size , const char * case_table_file ) { if (model_config->case_table_file != NULL) { /* Clear the current selection */ free( model_config->case_table_file ); stringlist_free( model_config->case_names ); model_config->case_table_file = NULL; model_config->case_names = NULL; } if (case_table_file != NULL) { bool atEOF = false; char casename[128]; int case_size = 0; FILE * stream = util_fopen( case_table_file , "r"); model_config->case_names = stringlist_alloc_new(); while (!atEOF) { if (fscanf( stream , "%s" , casename) == 1) { stringlist_append_copy( model_config->case_names , casename ); case_size++; } else atEOF = true; } fclose( stream ); if (case_size < ens_size) { for (int i = case_size; i < ens_size; i++) stringlist_append_owned_ref( model_config->case_names , util_alloc_sprintf("case_%04d" , i)); fprintf(stderr, "** Warning: mismatch between NUM_REALIZATIONS:%d and size of CASE_TABLE:%d - using \'case_nnnn\' for the last cases %d.\n", ens_size , case_size , ens_size - case_size); } else if (case_size > ens_size) fprintf(stderr, "** Warning: mismatch between NUM_REALIZATIONS:%d and CASE_TABLE:%d - only the %d realizations will be used.\n", ens_size , case_size , ens_size); } }
int stringlist_select_matching_files(stringlist_type * names , const char * path , const char * file_pattern) { #ifdef ERT_HAVE_GLOB char * pattern = util_alloc_filename( path , file_pattern , NULL ); int match_count = stringlist_select_matching( names , pattern ); free( pattern ); return match_count; #else { WIN32_FIND_DATA file_data; HANDLE file_handle; char * pattern = util_alloc_filename( path , file_pattern , NULL ); stringlist_clear( names ); file_handle = FindFirstFile( pattern , &file_data ); if (file_handle != INVALID_HANDLE_VALUE) { do { char * full_path = util_alloc_filename( path , file_data.cFileName , NULL); stringlist_append_owned_ref( names , full_path ); } while (FindNextFile( file_handle , &file_data) != 0); } FindClose( file_handle ); free( pattern ); return stringlist_get_size( names ); } #endif }
void stringlist_buffer_fread( stringlist_type * s , buffer_type * buffer ) { int size = buffer_fread_int( buffer ); int i; stringlist_clear(s); for (i=0; i < size; i++) stringlist_append_owned_ref( s , buffer_fread_alloc_string( buffer )); }
/* When a stringlist is loaded from file the current content of the stringlist is discarded; and the stringlist becomes the owner of all the data read in. */ void stringlist_fread(stringlist_type * s, FILE * stream) { int size = util_fread_int(stream); int i; stringlist_clear(s); for (i=0; i < size; i++) stringlist_append_owned_ref( s , util_fread_alloc_string( stream )); }
void * lsb_dlopen( lsb_type * lsb , const char * lib_name) { void * lib_handle = dlopen( lib_name , RTLD_NOW | RTLD_GLOBAL); if (!lib_handle) { lsb->ready = false; stringlist_append_owned_ref( lsb->error_list , util_alloc_sprintf("dlopen(%s) - failed:%s \n" , lib_name , dlerror())); } return lib_handle; }
stringlist_type * stringlist_alloc_argv_owned_ref(const char ** argv , int argc) { int iarg; stringlist_type * stringlist = stringlist_alloc_empty( true ); for (iarg = 0; iarg < argc; iarg++) stringlist_append_owned_ref( stringlist , argv[iarg]); return stringlist; }
static void * lsb_dlsym( lsb_type * lsb , const char * function_name ) { void * function = dlsym( lsb->lib_handle , function_name ); if (!function) { lsb->ready = false; stringlist_append_owned_ref( lsb->error_list , util_alloc_sprintf( "Failed to locate symbol:%s dlerror:%s" , function_name , dlerror())); } return function; }
stringlist_type * hash_alloc_stringlist(hash_type * hash) { stringlist_type * stringlist = stringlist_alloc_new(); char ** keylist = hash_alloc_keylist__(hash , true); int i; for (i = 0; i < hash_get_size( hash ); i++) stringlist_append_owned_ref( stringlist , keylist[i] ); free( keylist ); return stringlist; }
void ecl_config_add_static_kw(ecl_config_type * ecl_config , const char * _kw) { if (strcmp(_kw , DEFAULT_ALL_STATIC_KW) == 0) ecl_config->include_all_static_kw = true; else { char * kw = util_alloc_string_copy(_kw); ecl_util_escape_kw(kw); if (!stringlist_contains( ecl_config->user_static_kw , kw )) stringlist_append_owned_ref( ecl_config->user_static_kw , kw ); } }
stringlist_type * ert_run_context_alloc_runpath_list(const bool_vector_type * iactive , path_fmt_type * runpath_fmt , subst_list_type * subst_list , int iter) { stringlist_type * runpath_list = stringlist_alloc_new(); for (int iens = 0; iens < bool_vector_size( iactive ); iens++) { if (bool_vector_iget( iactive , iens )) stringlist_append_owned_ref( runpath_list , ert_run_context_alloc_runpath(iens , runpath_fmt , subst_list , iter)); else stringlist_append_ref( runpath_list , NULL ); } return runpath_list; }
obs_vector_type * obs_vector_alloc_from_BLOCK_OBSERVATION(const conf_instance_type * conf_instance , const ecl_grid_type * grid , const ecl_sum_type * refcase , const history_type * history, ensemble_config_type * ensemble_config) { if(!conf_instance_is_of_class(conf_instance, "BLOCK_OBSERVATION")) util_abort("%s: internal error. expected \"BLOCK_OBSERVATION\" instance, got \"%s\".\n", __func__, conf_instance_get_class_name_ref(conf_instance) ); block_obs_source_type source_type = SOURCE_SUMMARY; const char * obs_label = conf_instance_get_name_ref(conf_instance); const char * source_string = conf_instance_get_item_value_ref(conf_instance , "SOURCE"); const char * field_name = conf_instance_get_item_value_ref(conf_instance , "FIELD"); const char * sum_kw = NULL; bool OK = true; if (strcmp(source_string , "FIELD") == 0) { source_type = SOURCE_FIELD; if (!ensemble_config_has_key( ensemble_config , field_name)) { OK = false; fprintf(stderr,"** Warning the ensemble key:%s does not exist - observation:%s not added \n", field_name , obs_label); } } else if (strcmp( source_string , "SUMMARY") == 0) { source_type = SOURCE_SUMMARY; sum_kw = __summary_kw( field_name ); } else util_abort("%s: internal error \n",__func__); if (OK) { obs_vector_type * obs_vector = NULL; int size = history_get_last_restart( history ); int obs_restart_nr ; stringlist_type * summary_keys = stringlist_alloc_new(); stringlist_type * obs_pt_keys = conf_instance_alloc_list_of_sub_instances_of_class_by_name(conf_instance, "OBS"); int num_obs_pts = stringlist_get_size(obs_pt_keys); double * obs_value = util_calloc(num_obs_pts , sizeof * obs_value); double * obs_std = util_calloc(num_obs_pts , sizeof * obs_std ); int * obs_i = util_calloc(num_obs_pts , sizeof * obs_i ); int * obs_j = util_calloc(num_obs_pts , sizeof * obs_j ); int * obs_k = util_calloc(num_obs_pts , sizeof * obs_k ); obs_restart_nr = __conf_instance_get_restart_nr(conf_instance , obs_label , history , size); /** Build the observation. */ for(int obs_pt_nr = 0; obs_pt_nr < num_obs_pts; obs_pt_nr++) { const char * obs_key = stringlist_iget(obs_pt_keys, obs_pt_nr); const conf_instance_type * obs_instance = conf_instance_get_sub_instance_ref(conf_instance, obs_key); const char * error_mode = conf_instance_get_item_value_ref(obs_instance, "ERROR_MODE"); double error = conf_instance_get_item_value_double(obs_instance, "ERROR"); double value = conf_instance_get_item_value_double(obs_instance, "VALUE"); double min_error = conf_instance_get_item_value_double(obs_instance, "ERROR_MIN"); if (strcmp( error_mode , "REL") == 0) error *= value; else if (strcmp( error_mode , "RELMIN") == 0) error = util_double_max( error * value , min_error ); obs_value[obs_pt_nr] = value; obs_std [obs_pt_nr] = error; /** The input values i,j,k come from the user, and are offset 1. They are immediately shifted with -1 to become C-based offset zero. */ obs_i[obs_pt_nr] = conf_instance_get_item_value_int( obs_instance, "I") - 1; obs_j[obs_pt_nr] = conf_instance_get_item_value_int( obs_instance, "J") - 1; obs_k[obs_pt_nr] = conf_instance_get_item_value_int( obs_instance, "K") - 1; if (source_type == SOURCE_SUMMARY) { char * summary_key = smspec_alloc_block_ijk_key( SUMMARY_KEY_JOIN_STRING , sum_kw , obs_i[obs_pt_nr] + 1 , obs_j[obs_pt_nr] + 1 , obs_k[obs_pt_nr] + 1 ); stringlist_append_owned_ref( summary_keys , summary_key ); } } if (source_type == SOURCE_FIELD) { const enkf_config_node_type * config_node = ensemble_config_get_node( ensemble_config , field_name); const field_config_type * field_config = enkf_config_node_get_ref( config_node ); block_obs_type * block_obs = block_obs_alloc_complete(obs_label, source_type , NULL , field_config , grid , num_obs_pts, obs_i, obs_j, obs_k, obs_value, obs_std); if (block_obs != NULL) { obs_vector = obs_vector_alloc( BLOCK_OBS , obs_label , ensemble_config_get_node(ensemble_config , field_name), size ); obs_vector_install_node( obs_vector , obs_restart_nr , block_obs); } } else if (source_type == SOURCE_SUMMARY) { OK = true; if (refcase != NULL) { for (int i=0; i < stringlist_get_size( summary_keys ); i++) { const char * sum_key = stringlist_iget( summary_keys , i ); if (!ecl_sum_has_key(refcase , sum_key)) { /* If the */ fprintf(stderr,"** Warning missing summary %s for cell: (%d,%d,%d) in refcase - make sure that \"BPR %d %d %d\" is included in ECLIPSE summary specification \n" , sum_key , obs_i[i]+1 , obs_j[i]+1 , obs_k[i]+1 , obs_i[i]+1 , obs_j[i]+1 , obs_k[i]+1 ); //OK = false; } } } if (OK) { // We can create the container node and add the summary nodes. enkf_config_node_type * container_config = ensemble_config_add_container( ensemble_config , NULL ); for (int i=0; i < stringlist_get_size( summary_keys ); i++) { const char * sum_key = stringlist_iget( summary_keys , i ); enkf_config_node_type * child_node = ensemble_config_add_summary( ensemble_config , sum_key , LOAD_FAIL_WARN ); enkf_config_node_update_container( container_config , child_node ); } { block_obs_type * block_obs = block_obs_alloc_complete(obs_label, source_type , summary_keys , enkf_config_node_get_ref(container_config) , grid , num_obs_pts, obs_i, obs_j, obs_k, obs_value, obs_std); if (block_obs != NULL) { obs_vector = obs_vector_alloc( BLOCK_OBS , obs_label , container_config, size ); obs_vector_install_node( obs_vector , obs_restart_nr , block_obs); } } } } else util_abort("%s: invalid source value \n",__func__); free(obs_value); free(obs_std); free(obs_i); free(obs_j); free(obs_k); stringlist_free(obs_pt_keys); stringlist_free(summary_keys); return obs_vector; } else { fprintf(stderr,"** Warning the ensemble key:%s does not exist - observation:%s not added \n", field_name , obs_label); return NULL; } }
/** Allocates a new stringlist. */ stringlist_type * parser_tokenize_buffer( const parser_type * parser, const char * buffer, bool strip_quote_marks) { int position = 0; int buffer_size = strlen(buffer); int splitters_length = 0; int comment_length = 0; int delete_length = 0; stringlist_type * tokens = stringlist_alloc_new(); while( position < buffer_size ) { /** Skip initial splitters. */ splitters_length = length_of_initial_splitters( &buffer[position], parser ); if(splitters_length > 0) { position += splitters_length; continue; } /** Skip comments. */ comment_length = length_of_comment( &buffer[position], parser); if(comment_length > 0) { position += comment_length; continue; } /** Skip characters which are just deleted. */ delete_length = length_of_delete( &buffer[position] , parser ); if (delete_length > 0) { position += delete_length; continue; } /** Copy the character if it is in the special set, */ if( is_special( buffer[position], parser ) ) { char key[2]; key[0] = buffer[position]; key[1] = '\0'; stringlist_append_copy( tokens, key ); position += 1; continue; } /** If the character is a quotation start, we copy the whole quotation. */ if( is_in_quoters( buffer[position], parser ) ) { int length = length_of_quotation( &buffer[position] ); char * token = alloc_quoted_token( &buffer[position], length, strip_quote_marks ); stringlist_append_owned_ref( tokens, token ); position += length; continue; } /** If we are here, we are guaranteed that that buffer[position] is not: 1. Whitespace. 2. The start of a comment. 3. A special character. 4. The start of a quotation. 5. Something to delete. In other words, it is the start of plain non-splitters. Now we need to find the length of the non-splitters until: 1. Whitespace starts. 2. A comment starts. 3. A special character occur. 4. A quotation starts. */ { int length = length_of_normal_non_splitters( &buffer[position], parser ); char * token = util_calloc( (length + 1) , sizeof * token); int token_length; if (parser->delete_set == NULL) { token_length = length; memcpy( token , &buffer[position] , length * sizeof * token ); } else { int i; token_length = 0; for (i = 0; i < length; i++) { char c = buffer[position + i]; if ( !is_in_delete_set( c , parser)) { token[token_length] = c; token_length++; } } } if (token_length > 0) { /* We do not insert empty tokens. */ token[token_length] = '\0'; stringlist_append_owned_ref( tokens, token ); } else free( token ); /* The whole thing is discarded. */ position += length; continue; } } return tokens; }
void path_stack_push_cwd( path_stack_type * path_stack ) { char * cwd = util_alloc_cwd(); stringlist_append_owned_ref( path_stack->storage , cwd); stringlist_append_ref( path_stack->stack , cwd ); }