Skip to content

Commit

Permalink
Add a read_only flag to enkf_main
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad authored and oyvindeide committed Aug 8, 2022
1 parent c82e157 commit 5d87e17
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 219 deletions.
2 changes: 1 addition & 1 deletion ert_shared/dark_storage/enkf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def init_facade() -> None:

_config = ResConfig(configfile)
os.chdir(_config.config_path)
_ert = EnKFMain(_config, strict=True)
_ert = EnKFMain(_config, strict=True, read_only=True)
_libres_facade = LibresFacade(_ert)


Expand Down
56 changes: 32 additions & 24 deletions ert_shared/libres_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def get_current_case_name(self):
)

def get_active_realizations(self, case_name):
fs = self._enkf_main.getEnkfFsManager().getFileSystem(case_name)
realizations = SummaryCollector.createActiveList(self._enkf_main, fs)
fs = self._enkf_main.getEnkfFsManager().getFileSystem(case_name, read_only=True)
realizations = SummaryCollector.createActiveList(fs)

return realizations

Expand Down Expand Up @@ -151,18 +151,29 @@ def observation_keys(self, key):
else:
return []

def gather_gen_kw_data(self, case, key, realization_index=None):
""":rtype: pandas.DataFrame"""
def gather_gen_kw_data(
self,
case: str,
key: int,
realization_index: int = None,
) -> DataFrame:
data = GenKwCollector.loadAllGenKwData(
self._enkf_main, case, [key], realization_index=realization_index
self._enkf_main,
case,
[key],
realization_index=realization_index,
)
if key in data:
return data[key].to_frame().dropna()
else:
return DataFrame()

def gather_summary_data(self, case, key, realization_index=None):
""":rtype: pandas.DataFrame"""
def gather_summary_data(
self,
case: str,
key: str,
realization_index: int = None,
) -> DataFrame:
data = SummaryCollector.loadAllSummaryData(
self._enkf_main, case, [key], realization_index
)
Expand All @@ -178,7 +189,7 @@ def gather_summary_data(self, case, key, realization_index=None):
data = data.unstack(level="Realization").droplevel(0, axis=1)
return data

def refcase_data(self, key):
def refcase_data(self, key) -> DataFrame:
refcase = self._enkf_main.eclConfig().getRefcase()

if refcase is None or key not in refcase:
Expand All @@ -192,7 +203,7 @@ def refcase_data(self, key):

return data.iloc[1:]

def history_data(self, key, case=None):
def history_data(self, key, case=None) -> DataFrame:
if not self.is_summary_key(key):
return DataFrame()

Expand All @@ -205,8 +216,7 @@ def history_data(self, key, case=None):

return data

def gather_gen_data_data(self, case, key, realization_index=None):
""":rtype: pandas.DataFrame"""
def gather_gen_data_data(self, case, key, realization_index=None) -> DataFrame:
key_parts = key.split("@")
key = key_parts[0]
if len(key_parts) > 1:
Expand All @@ -216,35 +226,33 @@ def gather_gen_data_data(self, case, key, realization_index=None):

try:
data = GenDataCollector.loadGenData(
self._enkf_main, case, key, report_step, realization_index
self._enkf_main,
case,
key,
report_step,
realization_index,
)
except (ValueError, KeyError):
data = DataFrame()

return data.dropna() # removes all rows that has a NaN

def is_summary_key(self, key):
""":rtype: bool"""
def is_summary_key(self, key) -> bool:
return key in self._enkf_main.getKeyManager().summaryKeys()

def get_summary_keys(self):
""":rtype: list of str"""
def get_summary_keys(self) -> List[str]:
return self._enkf_main.getKeyManager().summaryKeys()

def is_gen_kw_key(self, key):
""":rtype: bool"""
def is_gen_kw_key(self, key) -> bool:
return key in self._enkf_main.getKeyManager().genKwKeys()

def gen_kw_keys(self):
""":rtype: list of str"""
def gen_kw_keys(self) -> List[str]:
return self._enkf_main.getKeyManager().genKwKeys()

def is_gen_data_key(self, key):
""":rtype: bool"""
def is_gen_data_key(self, key) -> bool:
return key in self._enkf_main.getKeyManager().genDataKeys()

def get_gen_data_keys(self):
""":rtype: list of str"""
def get_gen_data_keys(self) -> List[str]:
return self._enkf_main.getKeyManager().genDataKeys()

def gen_kw_priors(self):
Expand Down
20 changes: 11 additions & 9 deletions libres/lib/enkf/enkf_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ enkf_fs_type *enkf_fs_get_ref(enkf_fs_type *fs) {
return fs;
}

enkf_fs_type *enkf_fs_alloc_empty(const char *mount_point) {
enkf_fs_type *enkf_fs_alloc_empty(const char *mount_point, bool read_only) {
enkf_fs_type *fs = new enkf_fs_type;
UTIL_TYPE_ID_INIT(fs, ENKF_FS_TYPE_ID);
fs->time_map = time_map_alloc();
Expand All @@ -226,10 +226,11 @@ enkf_fs_type *enkf_fs_alloc_empty(const char *mount_point) {
if (util_try_lockf(fs->lock_file, S_IWUSR + S_IWGRP, &fs->lock_fd)) {
fs->read_only = false;
} else {
fprintf(stderr, " Another program has already opened filesystem "
"read-write - this instance will be UNSYNCRONIZED "
"read-only. Cross your fingers ....\n");
fs->read_only = true;
if (!read_only) {
util_abort("%s: Another program has already opened filesystem "
"read-write \n",
__func__);
}
}
return fs;
}
Expand Down Expand Up @@ -274,8 +275,9 @@ static void enkf_fs_assign_driver(enkf_fs_type *fs,
}

static enkf_fs_type *enkf_fs_mount_block_fs(FILE *fstab_stream,
const char *mount_point) {
enkf_fs_type *fs = enkf_fs_alloc_empty(mount_point);
const char *mount_point,
bool read_only) {
enkf_fs_type *fs = enkf_fs_alloc_empty(mount_point, read_only);

{
while (true) {
Expand Down Expand Up @@ -394,7 +396,7 @@ void enkf_fs_fwrite_misfit(enkf_fs_type *fs) {
}
}

enkf_fs_type *enkf_fs_mount(const char *mount_point) {
enkf_fs_type *enkf_fs_mount(const char *mount_point, bool read_only) {
FILE *stream = fs_driver_open_fstab(mount_point, false);

if (!stream)
Expand All @@ -408,7 +410,7 @@ enkf_fs_type *enkf_fs_mount(const char *mount_point) {

switch (driver_id) {
case (BLOCK_FS_DRIVER_ID):
fs = enkf_fs_mount_block_fs(stream, mount_point);
fs = enkf_fs_mount_block_fs(stream, mount_point, read_only);
logger->debug("Mounting (block_fs) point {}.", mount_point);
break;
default:
Expand Down
8 changes: 5 additions & 3 deletions libres/lib/enkf/enkf_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ struct enkf_main_struct {

void enkf_main_init_internalization(enkf_main_type *);
static void enkf_main_close_fs(enkf_main_type *enkf_main);
static void enkf_main_user_select_initial_fs(enkf_main_type *enkf_main);
static void enkf_main_user_select_initial_fs(enkf_main_type *enkf_main,
bool read_only);
static void enkf_main_free_ensemble(enkf_main_type *enkf_main);

UTIL_SAFE_CAST_FUNCTION(enkf_main, ENKF_MAIN_ID)
Expand Down Expand Up @@ -297,12 +298,13 @@ static void enkf_main_add_ensemble_members(enkf_main_type *enkf_main) {
case only the site config file will be parsed. The purpose of this
is mainly to be able to test that the site config file is valid.
*/
enkf_main_type *enkf_main_alloc(const res_config_type *res_config) {
enkf_main_type *enkf_main_alloc(const res_config_type *res_config,
bool read_only) {
enkf_main_type *enkf_main = enkf_main_alloc_empty();
enkf_main->res_config = res_config;

enkf_main_rng_init(enkf_main);
enkf_main_user_select_initial_fs(enkf_main);
enkf_main_user_select_initial_fs(enkf_main, read_only);
enkf_main_init_obs(enkf_main);
enkf_main_add_ensemble_members(enkf_main);

Expand Down
21 changes: 12 additions & 9 deletions libres/lib/enkf/enkf_main_manage_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ const char *enkf_main_get_current_fs(const enkf_main_type *enkf_main) {
count has reached zero.
*/
enkf_fs_type *enkf_main_mount_alt_fs(const enkf_main_type *enkf_main,
const char *case_path, bool create) {
const char *case_path, bool create,
bool read_only) {
if (enkf_main_case_is_current(enkf_main, case_path)) {
// Fast path - we just return a reference to the currently selected case;
// with increased refcount.
Expand All @@ -346,7 +347,7 @@ enkf_fs_type *enkf_main_mount_alt_fs(const enkf_main_type *enkf_main,
enkf_main_create_fs(enkf_main, case_path);
}

new_fs = enkf_fs_mount(new_mount_point);
new_fs = enkf_fs_mount(new_mount_point, read_only);
if (new_fs) {
const model_config_type *model_config =
enkf_main_get_model_config(enkf_main);
Expand Down Expand Up @@ -428,12 +429,13 @@ void enkf_main_set_fs(enkf_main_type *enkf_main, enkf_fs_type *fs,
}
}

void enkf_main_select_fs(enkf_main_type *enkf_main, const char *case_path) {
void enkf_main_select_fs(enkf_main_type *enkf_main, const char *case_path,
bool read_only) {
if (enkf_main_case_is_current(enkf_main, case_path))
return; /* We have tried to select the currently selected case - just return. */
else {
enkf_fs_type *new_fs =
enkf_main_mount_alt_fs(enkf_main, case_path, true);
enkf_main_mount_alt_fs(enkf_main, case_path, true, read_only);
if (enkf_main->dbase == new_fs)
util_abort("%s : return reference to current FS in situation where "
"that should not happen.\n",
Expand All @@ -451,29 +453,30 @@ void enkf_main_select_fs(enkf_main_type *enkf_main, const char *case_path) {
}
}

static void enkf_main_user_select_initial_fs(enkf_main_type *enkf_main) {
static void enkf_main_user_select_initial_fs(enkf_main_type *enkf_main,
bool read_only) {
const char *ens_path =
model_config_get_enspath(enkf_main_get_model_config(enkf_main));
char *current_mount_point =
util_alloc_filename(ens_path, CURRENT_CASE, NULL);

if (enkf_main_current_case_file_exists(enkf_main)) {
char *current_case = enkf_main_read_alloc_current_case_name(enkf_main);
enkf_main_select_fs(enkf_main, current_case);
enkf_main_select_fs(enkf_main, current_case, read_only);
free(current_case);
} else if (enkf_fs_exists(current_mount_point) &&
util_is_link(current_mount_point)) {
/*If the current_case file does not exists, but the 'current' symlink does we use readlink to
get hold of the actual target before calling the enkf_main_select_fs() function. We then
write the current_case file and delete the symlink.*/
char *target_case = util_alloc_atlink_target(ens_path, CURRENT_CASE);
enkf_main_select_fs(enkf_main, target_case);
enkf_main_select_fs(enkf_main, target_case, read_only);
unlink(current_mount_point);
enkf_main_write_current_case_file(enkf_main, target_case);
free(target_case);
} else
enkf_main_select_fs(enkf_main,
DEFAULT_CASE); // Selecting (a new) default case
// Selecting (a new) default case
enkf_main_select_fs(enkf_main, DEFAULT_CASE, read_only);

free(current_mount_point);
}
Expand Down
2 changes: 1 addition & 1 deletion libres/lib/enkf/ert_test_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ert_test_context_alloc_internal(test_work_area_type *work_area,
setenv("ERT_UI_MODE", ui_mode, 1);
test_context->res_config = res_config;
test_context->work_area = work_area;
test_context->enkf_main = enkf_main_alloc(test_context->res_config);
test_context->enkf_main = enkf_main_alloc(test_context->res_config, false);
test_context->rng = rng_alloc(MZRAN, INIT_DEV_URANDOM);
return test_context;
}
Expand Down
3 changes: 2 additions & 1 deletion libres/lib/include/ert/enkf/enkf_fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enkf_fs_type *enkf_fs_get_ref(enkf_fs_type *fs);
extern "C" int enkf_fs_decref(enkf_fs_type *fs);
extern "C" int enkf_fs_incref(enkf_fs_type *fs);
extern "C" int enkf_fs_get_refcount(const enkf_fs_type *fs);
extern "C" enkf_fs_type *enkf_fs_mount(const char *path);
extern "C" enkf_fs_type *enkf_fs_mount(const char *path,
bool read_only = false);
void enkf_fs_fwrite_node(enkf_fs_type *enkf_fs, buffer_type *buffer,
const char *node_key, enkf_var_type var_type,
int report_step, int iens);
Expand Down
9 changes: 6 additions & 3 deletions libres/lib/include/ert/enkf/enkf_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ typedef struct enkf_main_struct enkf_main_type;
extern "C" void enkf_main_free(enkf_main_type *);
void enkf_main_exit(enkf_main_type *enkf_main);

extern "C" enkf_main_type *enkf_main_alloc(const res_config_type *);
extern "C" enkf_main_type *enkf_main_alloc(const res_config_type *,
bool read_only = false);

extern "C" enkf_state_type *enkf_main_iget_state(const enkf_main_type *, int);

Expand Down Expand Up @@ -151,10 +152,12 @@ enkf_fs_type *enkf_main_job_get_fs(const enkf_main_type *);
extern "C" enkf_fs_type *enkf_main_get_fs_ref(const enkf_main_type *enkf_main);
const char *enkf_main_get_current_fs(const enkf_main_type *enkf_main);
enkf_fs_type *enkf_main_mount_alt_fs(const enkf_main_type *enkf_main,
const char *case_path, bool create);
const char *case_path, bool create,
bool read_only = false);
extern "C" void enkf_main_set_fs(enkf_main_type *enkf_main, enkf_fs_type *fs,
const char *case_path);
void enkf_main_select_fs(enkf_main_type *enkf_main, const char *case_path);
void enkf_main_select_fs(enkf_main_type *enkf_main, const char *case_path,
bool read_only = false);
bool enkf_main_fs_exists(const enkf_main_type *enkf_main,
const char *input_case);
extern "C" const char *
Expand Down
Loading

0 comments on commit 5d87e17

Please sign in to comment.