From 66020991d028f801497f57819dd5a489dfcb3e64 Mon Sep 17 00:00:00 2001 From: "Oddvar Lia (ST MSU GEO)" Date: Fri, 20 Oct 2023 10:38:42 +0200 Subject: [PATCH] update sim_field.py --- .../example_case/scripts/sim_fields.py | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/tests/jobs/localisation/example_case/scripts/sim_fields.py b/tests/jobs/localisation/example_case/scripts/sim_fields.py index defcde729..12920e6a5 100755 --- a/tests/jobs/localisation/example_case/scripts/sim_fields.py +++ b/tests/jobs/localisation/example_case/scripts/sim_fields.py @@ -47,7 +47,6 @@ # to use exactly the same. - # pylint: disable=missing-function-docstring, too-many-locals, invalid-name # pylint: disable=bare-except, raise-missing-from # pylint: disable= redefined-outer-name, too-many-nested-blocks @@ -362,10 +361,15 @@ def write_gen_obs(upscaled_values, observation_settings): cell_indx_list = observation_settings["selected_grid_cells"] rel_err = observation_settings["rel_error"] min_err = observation_settings["min_abs_error"] + if not os.path.exists(observation_dir): + print(f"Create directory: {observation_dir} ") + os.makedirs(observation_dir) + data_dir = observation_dir + "/" + obs_data_dir + if not os.path.exists(data_dir): + print(f"Create directory: {data_dir} ") + os.makedirs(data_dir) + print(f"Write observation file: {obs_file_name} ") - if not os.path.exists(obs_data_dir): - print(f"Create directory: {obs_data_dir} ") - os.makedirs(obs_data_dir) filename = observation_dir + "/" + obs_file_name with open(filename, "w", encoding="utf8") as obs_file: number = 0 @@ -752,17 +756,33 @@ def main( # Observations extracted for a 3D parameter from the coarse grid. extract_and_write_obs = False - try: - iteration = int(os.environ.get("_ERT_ITERATION_NUMBER")) + iter_env_name = "_ERT_ITERATION_NUMBER" + if iter_env_name in os.environ: + iteration = int(os.environ.get(iter_env_name)) extract_and_write_obs = False - except: # noqa: E722 - iteration = int(sys.argv[1]) + else: + if len(sys.argv) < 3: + raise IOError("Missing command line arguments ") + arg1 = sys.argv[1] + if arg1 is None: + raise IOError( + "Missing iteration number (argv[1]) when running this script manually" + ) + iteration = int(arg1) print(f"ERT iteration: {iteration}") - try: - real_number = int(os.environ.get("_ERT_REALIZATION_NUMBER")) - except: # noqa: E722 - real_number = int(sys.argv[2]) + real_env_name = "_ERT_REALIZATION_NUMBER" + if real_env_name in os.environ: + real_number = int(os.environ.get(real_env_name)) + else: + if len(sys.argv) < 3: + raise IOError("Missing command line arguments ") + arg2 = sys.argv[2] + if arg2 is None: + raise IOError( + "Missing real_number (argv[2]) when running this script manually" + ) + real_number = int(arg2) print(f"ERT realization: {real_number}") if make_seed_file: