Skip to content

Commit 075d498

Browse files
committed
Allow utilities.yaml_load to read extended YAML
Address issue #49
1 parent f53b8ba commit 075d498

25 files changed

Lines changed: 2268 additions & 19 deletions

src/fmu/config/_configparserfmu_ipl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ def _freeform_handle_entry(
393393

394394
# inner function
395395
def _fixtheentry(variable, myval, subtype, count=None, template=False):
396-
397396
logger.info("Fix freeform entry %s (subtype %s)", variable, subtype)
398397
tmpvalue = str(myval)
399398
if "~" in tmpvalue:

src/fmu/config/configparserfmu.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,11 @@ def _cleanify_doubleunderscores(self):
388388
newcfg = deepcopy(self._config)
389389

390390
for key, val in self._config.items():
391-
392391
if isinstance(val, dict):
393392
subkeyorder = []
394393
_tmps = {}
395394

396395
for subkey, subval in val.items():
397-
398396
if subkey.startswith("__"):
399397
if isinstance(subval, dict):
400398
for subsubkey, subsubval in subval.items():
@@ -507,7 +505,6 @@ def _get_sysinfo(commentmarker="#"):
507505

508506
@staticmethod
509507
def _force_create_folders(folderlist):
510-
511508
for folder in folderlist:
512509
if folder is None:
513510
continue
@@ -519,7 +516,6 @@ def _force_create_folders(folderlist):
519516

520517
@staticmethod
521518
def _check_folders(folderlist):
522-
523519
for folder in folderlist:
524520
if folder is None:
525521
continue
@@ -594,7 +590,6 @@ def _get_required_form(stream, template=False, ipl=False):
594590
if isinstance(stream, list):
595591
pass
596592
elif isinstance(stream, str):
597-
598593
if "~" in stream:
599594
value, tvalue = stream.split("~")
600595
value = value.strip()

src/fmu/config/etc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class Interaction(object):
6464
"""
6565

6666
def __init__(self):
67-
6867
self._callclass = None
6968
self._caller = None
7069
self._lformat = None
@@ -311,7 +310,6 @@ def _get_class_from_frame(frame):
311310
return outer[0]
312311

313312
def _output(self, idx, level, string):
314-
315313
# pylint: disable=too-many-branches
316314

317315
prefix = ""

src/fmu/config/fmuconfigrunner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222

2323
def _do_parse_args(args):
24-
2524
if args is None:
2625
args = sys.argv[1:]
2726

src/fmu/config/utilities.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
11
"""Module with some simple functions, e.g. for parsing for YAML into RMS
22
"""
33

4+
from yaml.loader import Loader
5+
46
# for ordered dicts!
57
from fmu.config import oyaml as yaml
8+
from fmu.config._loader import ConstructorError, FmuLoader
69

710

8-
def yaml_load(filename, safe=True, tool=None):
11+
def yaml_load(filename, safe=True, tool=None, loader="standard"):
912
"""Load as YAML file, return a dictionary of type OrderedDict which is the config.
1013
1114
Returning an ordered dictionary is a main feature of this loader. It makes it much
12-
easier to compare the dictionaries returned.
15+
easier to compare the dictionaries returned. In addition, it allows for reading the
16+
input (extended) YAML format, if key ``allow_extended`` is True.
1317
1418
Args:
1519
filename (str): Name of file (YAML formatted)
16-
safe (bool): If True (default), then use `safe_load`
20+
safe (bool): If True (default), then use `safe_load` when allow_extended is
21+
set to False. Not applied if loader is "fmu".
1722
tool (str): Refers to a particular main section in the config.
18-
Default is None, which measn 'all'.
23+
Default is None, which means 'all'.
24+
loader (str): If "fmu", the in-house FMU extended YAML loader that allows
25+
use of e.g. `!include` is applied; otherwise the default is "standard" YAML.
1926
2027
Example::
2128
>>> import fmu.config.utilities as utils
2229
>>> cfg = utils.yaml_load('somefile.yml')
2330
2431
"""
2532

33+
useloader = FmuLoader if loader.lower() == "fmu" else Loader
34+
2635
with open(filename, "r", encoding="utf-8") as stream:
27-
if safe:
28-
cfg = yaml.safe_load(stream)
29-
else:
30-
cfg = yaml.load(stream)
36+
try:
37+
if safe and loader.lower() != "fmu":
38+
cfg = yaml.safe_load(stream)
39+
else:
40+
cfg = yaml.load(stream, Loader=useloader)
41+
except ConstructorError as cerr:
42+
if "!include" in str(cerr):
43+
print(
44+
"\n*** Consider setting loader='fmu' to read fmu.config "
45+
"input style ***\n"
46+
)
47+
raise
3148

3249
if tool is not None:
3350
try:
3451
newcfg = cfg[tool]
3552
cfg = newcfg
3653
except Exception as exc: # pylint: disable=broad-except
37-
print("Cannot import: {}".format(exc))
54+
print(f"Cannot import: {exc}")
3855
return None
3956

4057
return cfg
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
asset:
2+
name: Drogon
3+
ssdl:
4+
access_level: internal
5+
rep_include: true
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# This is auxiliary metadata that contains helper setup for mostly for SUMO
2+
# * aliases
3+
# * display settings
4+
5+
# The key names are the modelling names used in RMS project (e.g. TopVolantis) while the "name"
6+
# shall be the official SMDA name
7+
8+
MSL:
9+
stratigraphic: False
10+
name: MSL
11+
display_name: MSL
12+
13+
Seabase:
14+
stratigraphic: False
15+
name: Seabase
16+
display_name: Seabase
17+
18+
TopVolantis:
19+
stratigraphic: True
20+
name: VOLANTIS GP. Top # this is the official DB name
21+
display_name: Top Volantis
22+
# additional names, given for demo purpose:
23+
alias:
24+
- TopVOLANTIS
25+
- TOP_VOLANTIS
26+
straigraphic_alias:
27+
- TopValysar
28+
- Valysar Fm. Top
29+
30+
TopTherys:
31+
stratigraphic: True
32+
name: Therys Fm. Top
33+
display_name: Top Therys
34+
35+
TopVolon:
36+
stratigraphic: True
37+
name: Volon Fm. Top
38+
display_name: Top Volon
39+
40+
BaseVolon:
41+
stratigraphic: True
42+
name: Volon Fm. Base
43+
display_name: Base Volon
44+
45+
BaseVolantis:
46+
stratigraphic: True
47+
name: VOLANTIS GP. Base
48+
display_name: Base Volantis
49+
50+
Mantle:
51+
stratigraphic: False
52+
name: Mantle
53+
54+
#=========================================================================================
55+
# zones
56+
Above:
57+
stratigraphic: False
58+
name: Above
59+
display_name: Above Reservoir
60+
61+
Valysar:
62+
stratigraphic: True
63+
name: Valysar Fm.
64+
display_name: Valysar
65+
66+
Therys:
67+
stratigraphic: True
68+
name: Therys Fm.
69+
display_name: Therys
70+
71+
Volon:
72+
stratigraphic: True
73+
name: Volon Fm.
74+
display_name: Volon
75+
76+
Below:
77+
stratigraphic: False
78+
name: Below
79+
display_name: Below Reservoir
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SEISMIC_HIST_DATES: &4d_dates
2+
- 2018-01-01
3+
- 2018-07-01
4+
- 2019-07-01
5+
- 2020-07-01
6+
7+
SEISMIC_HIST_DIFFDATES: &4d_diffdates
8+
- [2018-07-01, 2018-01-01]
9+
- [2019-07-01, 2018-01-01]
10+
- [2020-07-01, 2018-01-01]
11+
- [2019-07-01, 2018-07-01]
12+
- [2020-07-01, 2019-07-01]
13+
14+
ECLIPSE_INIT_DATE: &ecl_init_date 2018-01-01 #init date
15+
16+
# eclipse dates should be aligned with rmsevents IORESTART keyword (restart reporting)
17+
ECLIPSE_HIST_DATES: *4d_dates # same dates as in SEISMIC_HIST_DATES
18+
19+
ECLIPSE_HIST_DIFFDATES: *4d_diffdates # same dates as in SEISMIC_HIST_DIFFDATES
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#seismic pred dates should include diff dates and relevant single dates
2+
SEISMIC_PRED_DATES: &4d_pred_dates
3+
- 2018-01-01
4+
- 2020-07-01
5+
- 2025-01-01
6+
7+
SEISMIC_PRED_DIFFDATES: &4d_pred_diffdates
8+
- [2025-01-01, 2018-01-01]
9+
- [2025-01-01, 2020-07-01]
10+
11+
#ecl_dates should be aligned with rmsevents IORESTART keyword (restart reporting)
12+
ECLIPSE_INIT_DATES: &ecl_init_dates
13+
- 2018-01-01
14+
15+
ECLIPSE_PRED_DATES: &ecl_pred_dates
16+
- 2025-01-01
17+
18+
ECLIPSE_PRED_DIFFDATES: &ecl_pred_diffdates
19+
- [2025-01-01, 2018-01-01]
20+
- [2025-01-01, 2020-07-01]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# These data are currently *not* in use ; they are here for demo purpose!
2+
# This is primarely an example on how to read 2D data from YAML
3+
# in the RMS project it will be a demo_*.py script to show how
4+
# it can be read by python
5+
6+
# ZONE1 VALYSAR ZONE2 THERYS ZONE3 ZONE3 VOLON ZONE3
7+
- [1660 ~ <FWL_VALYSAR_REG1>, 1660 ~ <FWL_THERYS_REG1>, 1660 ~ <FWL_VOLON_REG1>] # region 1 WestLowland
8+
- [1667 ~ <FWL_VALYSAR_REG2>, 1667 ~ <FWL_THERYS_REG2>, 1680 ~ <FWL_VOLON_REG2>] # region 2 CentralSouth
9+
- [1668 ~ <FWL_VALYSAR_REG3>, 1667 ~ <FWL_THERYS_REG3>, 1680 ~ <FWL_VOLON_REG3>] # region 3 CentralNorth
10+
- [1661 ~ <FWL_VALYSAR_REG4>, 1667 ~ <FWL_THERYS_REG4>, 1680 ~ <FWL_VOLON_REG4>] # region 4 NorthHorst
11+
- [1655 ~ <FWL_VALYSAR_REG5>, 1664 ~ <FWL_THERYS_REG5>, 1680 ~ <FWL_VOLON_REG5>] # region 5 CentralRamp
12+
- [1632 ~ <FWL_VALYSAR_REG6>, 1667 ~ <FWL_THERYS_REG6>, 1680 ~ <FWL_VOLON_REG6>] # region 6 CentralHorst
13+
- [1688 ~ <FWL_VALYSAR_REG7>, 1667 ~ <FWL_THERYS_REG7>, 1690 ~ <FWL_VOLON_REG7>] # region 7 EastLowland

0 commit comments

Comments
 (0)