Skip to content

Commit

Permalink
Generated test data is written to tmp directory, added subscript doc …
Browse files Browse the repository at this point in the history
…minimum version
  • Loading branch information
oddvarlia committed Sep 26, 2024
1 parent 627e13d commit 1132829
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 106 deletions.
8 changes: 8 additions & 0 deletions docs/scripts/field_statistics.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FIELD_STATISTICS
=================

.. argparse::
:module: subscript.field_statistics.field_statistics
:func: get_parser
:prog: field_statistics

100 changes: 69 additions & 31 deletions src/subscript/field_statistics/field_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import sys
from pathlib import Path

import ert
import fmu.config.utilities as utils
import numpy as np
import xtgeo
import yaml
from ert.config import ErtScript

import subscript

Expand Down Expand Up @@ -80,7 +82,7 @@
# Example config file for wf_field_param_statistics
field_stat:
field_stat:
# Number of realizations for specified ensemble
# Required.
nreal: 100
Expand Down Expand Up @@ -139,7 +141,8 @@
"Volon": ["phit"]
# Size of ertbox grid for (nx, ny, nz)
# Required.
# Required if the ERTBOX grid is not found as a file
# under rms/output/aps/ERTBOX.EGRID
ertbox_size: [92, 146, 66]
# Standard deviation estimator.
Expand All @@ -163,16 +166,21 @@
LOAD_WORKFLOW ../../bin/workflows/wf_field_param_statistics
-- The workflow file to be located under ert/bin/workflows:
WF_FIELD_PARAM_STATISTICS <FIELD_STAT_CONFIG_FILE> <CONFIG_PATH> <SCRATCH>/<USER>/<CASE_DIR>
-- The workflow job file to be located under ert/bin/jobs:
-- Workflow job for ERT to calculate
-- - mean and stdev of ensemble of continuous 3D parameters with name <name> saved for geogrid
-- under <ensemble_path>/realization-*/iter-*/share/results/grids/geogrid--<name>.roff
-- - estimate facies probabilities of discrete 3D parameters with name <name> saved for geogrid
-- under <ensemble_path>/realization-*/iter-*/share/results/grids/geogrid--<name>.roff
FIELD_STATISTICS -c <FIELD_STAT_CONFIG_FILE>
-p <CONFIG_PATH>
-e <SCRATCH>/<USER>/<CASE_DIR>
-r <RESULT_PATH>
-- Workflow job for ERT to calculate:
-- Mean and standard deviatons of specified continuous 3D parameters.
-- Estimate of facies probabilities from discrete 3D parameter for facies.
-- The input realizations are found under:
-- <ensemble_path>/realization-*/iter-*/share/results/grids/geogrid--<name>.roff
-- The output mean and standard deviations and facies probability estimates are saved
-- under a directory specified by the user.
-- The first three command line arguments are required, the last one (<RESULT_PATH>)
-- has default 'share/grid_statistics' under <ensemble_path>.
INTERNAL False
EXECUTABLE ../scripts/wf_field_param_statistics.py
EXECUTABLE ../scripts/field_statistics.py
MIN_ARG 6
ARG_TYPE 0 STRING
Expand All @@ -187,15 +195,20 @@
""" # noqa
DEFAULT_RELATIVE_RESULT_PATH = "share/grid_statistics"
GLOBAL_VARIABLES_FILE = "../../fmuconfig/output/global_variables.yml"
ERTBOX_GRID_PATH = "../../rms/output/aps/ERTBOX.EGRID"


def main():
"""Invocated from the command line, parsing command line arguments"""
parser = get_parser()
args = parser.parse_args()

logger.setLevel(logging.INFO)
field_stat(args)


def field_stat(args):
# parse the config file for this script
if not Path(args.configfile).exists():
sys.exit("No such file:" + args.configfile)
Expand All @@ -207,23 +220,20 @@ def main():
# Path to FMU project models ert/model directory (ordinary CONFIG PATH in ERT)
if not Path(args.ertconfigpath).exists():
sys.exit("No such file:" + args.ertconfigpath)
ert_config_path = args.ertconfigpath
ert_config_path = Path(args.ertconfigpath)

# Path to ensemble on SCRATCH disk
if not Path(args.ensemblepath).exists():
sys.exit("No such file:" + args.ensemblepath)
ens_path = args.ensemblepath
ens_path = Path(args.ensemblepath)

# Relative path for result of ensemble statistics calculations
# relative to ensemble path on scratch disk
# Path for result of ensemble statistics calculations
# Default path is defined.
result_path = "share/grid_statistics"
if Path(args.resultpath).exists():
result_path = args.resultpath

glob_var_config_path = (
ert_config_path + "/../../fmuconfig/output/global_variables.yml"
)
relative_result_path = DEFAULT_RELATIVE_RESULT_PATH
if args.resultpath:
relative_result_path = Path(args.resultpath)
result_path = ens_path / relative_result_path
glob_var_config_path = ert_config_path / Path(GLOBAL_VARIABLES_FILE)
cfg_global = utils.yaml_load(glob_var_config_path)["global"]
keyword = "FACIES_ZONE"
if keyword in cfg_global:
Expand All @@ -232,11 +242,11 @@ def main():
raise KeyError(f"Missing keyword: {keyword} in {glob_var_config_path}")

# The ERTBOX grid file location in FMU
ertbox_path = ert_config_path + "/../../rms/output/aps/ERTBOX.EGRID"
ertbox_path = ert_config_path / ERTBOX_GRID_PATH
ertbox_size = get_ertbox_size(ertbox_path)
logger.info(f"Config path to FMU project: {ert_config_path}")
logger.info(f"Ensemble path on scratch disk: {ens_path}")
logger.info(f"Result relative path on scratch disk: {result_path}")
logger.info(f"Result path on scratch disk: {result_path}")
logger.info(f"ERTBOX size: {ertbox_size}")

calc_stats(
Expand Down Expand Up @@ -374,13 +384,11 @@ def get_values_in_ertbox(
)
if conformity.upper() in ["PROPORTIONAL", "TOP_CONFORM"]:
ertbox_prop_values[:, :, :nz_zone] = prop_values[:, :, start_layer:end_layer]
# print(f"Top conform or proportional zone: {zone_name}")
elif conformity.upper() == "BASE_CONFORM":
start_layer_ertbox = ertbox_size[2] - nz_zone
ertbox_prop_values[:, :, start_layer_ertbox:] = prop_values[
:, :, start_layer:end_layer
]
# print(f"Base conform zone: {zone_name}")

return ertbox_prop_values

Expand All @@ -405,7 +413,6 @@ def set_subgrid_names(grid, zone_code_names=None, new_subgrids=None):


def write_mean_stdev_nactive(
ensemble_path,
iter_number,
zone_name,
param_name,
Expand All @@ -414,7 +421,7 @@ def write_mean_stdev_nactive(
ncount_active_values,
result_path,
):
output_path = ensemble_path / Path(result_path)
output_path = result_path
if not output_path.exists():
# Create the directory
output_path.mkdir()
Expand Down Expand Up @@ -466,7 +473,7 @@ def write_fraction_nactive(
result_path,
ncount_active_values=None,
):
output_path = ensemble_path / Path(result_path)
output_path = result_path
if not output_path.exists():
# Create the directory
output_path.mkdir()
Expand Down Expand Up @@ -753,7 +760,6 @@ def calc_stats(
# Write mean, stdev
if calc_mean and calc_stdev:
write_mean_stdev_nactive(
ensemble_path,
iter_number,
zone_name,
param_name,
Expand Down Expand Up @@ -881,5 +887,37 @@ def calc_stats(
logger.info(txt)


class FieldStatistics(ErtScript):
"""This class defines the ERT workflow hook.
It is constructed to work identical to the command line except
* field_statistics is upper-cased to FIELD_STATISTICS
* All option names with double-dash must be enclosed in "" to avoid
interference with the ERT comment characters "--".
"""

# pylint: disable=too-few-public-methods
def run(self, *args):
# pylint: disable=no-self-use
"""Pass the ERT workflow arguments on to the same parser as the command
line."""
parser = get_parser()
parsed_args = parser.parse_args(args)
field_stat(parsed_args)


@ert.plugin(name="subscript")
def legacy_ertscript_workflow(config):
"""A hook for usage of this script in an ERT workflow,
using the legacy hook format."""

workflow = config.add_workflow(FieldStatistics, "FIELD_STATISTICS")
workflow.parser = get_parser
workflow.description = DESCRIPTION
workflow.examples = EXAMPLES
workflow.category = CATEGORY


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion src/subscript/field_statistics/wf_field_param_statistics

This file was deleted.

2 changes: 2 additions & 0 deletions src/subscript/field_statistics/wf_field_statistics
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FIELD_STATISTICS -c <FIELD_STAT_CONFIG_FILE> -p <CONFIG_PATH> -e <SCRATCH>/<USER>/<CASE_DIR> -r <RESULT_PATH>

Loading

0 comments on commit 1132829

Please sign in to comment.