Skip to content

Commit

Permalink
Fix a bug related to dataio output with diffdates
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrivenaes committed Oct 12, 2023
1 parent 7dfbee6 commit 8b7dcc3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/xtgeoapp_grd3dmaps/avghc/_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import yaml
from xtgeo.common import XTGeoDialog

from xtgeoapp_grd3dmaps.avghc._loader import ConstructorError, YamlXLoader

xtg = XTGeoDialog()
Expand Down Expand Up @@ -79,7 +80,6 @@ def parse_args(args, appname, appdescr):
)

if appname == "grid3d_hc_thickness":

parser.add_argument(
"-d",
"--dates",
Expand Down Expand Up @@ -149,12 +149,12 @@ def prepare_metadata(config):
look like this:
"metadata": {
"SWAT--19991201": { # identifier name in this package
"SWAT--19991201-20010101": { # identifier name in this package
"name": "SWAT", # generic property name
"nameinfo" # e.g. "oilthickness"
"source": "$eclroot.UNRST", # info
"t1": "20010101", # timedata entry 1
"t2": "19991201", # timedata entry 2
"t1": "19991201", # timedata entry 1
"t2": "20010101", # timedata entry 2
"content": "saturation", # content info for sumo
"unit": "fraction", # unit info
"globaltag": "avg2c" # a global tag from output.tag
Expand Down Expand Up @@ -274,7 +274,6 @@ def propformatting(config):
newdates.append(entry.strftime("%Y%m%d"))
else:
newdates.append(entry)
fetched_metadata.update({"t1": entry})

if "diffdates" in prop:
for entry in prop["diffdates"]:
Expand All @@ -285,20 +284,26 @@ def propformatting(config):
if isinstance(dd2, datetime.date):
dd2 = dd2.strftime("%Y%m%d")
newdates.append(dd1 + "-" + dd2)
fetched_metadata.update({"t1": dd1})
fetched_metadata.update({"t2": dd2})

# get the addional metadata privded by user input under properties:
if "metadata" in prop:
fetched_metadata.update(prop["metadata"])

namekey = prop["name"]
namekeys = []
datekeys = {}
if newdates:
for mydate in newdates:
namekey = prop["name"] + "--" + mydate
newconfig["input"][namekey] = prop["source"]
namekeys.append(namekey)
if len(mydate) == 8:
datekeys[namekey] = {"t1": mydate}
else:
dd1 = mydate[:8]
dd2 = mydate[9:]
datekeys[namekey] = {"t1": dd1, "t2": dd2}

else:
newconfig["input"][namekey] = prop["source"]
namekeys.append(namekey)
Expand All @@ -307,7 +312,9 @@ def propformatting(config):
fetched_metadata.update({"globaltag": config["output"]["tag"]})

for nkey in namekeys:
newconfig["metadata"][nkey] = fetched_metadata
newconfig["metadata"][nkey] = copy.deepcopy(fetched_metadata)
if nkey in datekeys:
newconfig["metadata"][nkey].update(datekeys[nkey])

del newconfig["input"]["properties"]

Expand Down Expand Up @@ -363,7 +370,6 @@ def yconfig_override(config, args, appname):
newconfig["output"]["legacydateformat"] = args.legacydateformat

if appname == "grid3d_hc_thickness":

if args.dates:
newconfig["input"]["dates"] = args.dates

Expand Down Expand Up @@ -440,7 +446,6 @@ def yconfig_set_defaults(config, appname):
newconfig["computesettings"]["tuning"]["coarsen"] = 1

if appname == "grid3d_hc_thickness":

if "dates" not in newconfig["input"]:
if newconfig["computesettings"]["mode"] in "rock":
xtg.say('No date give, probably OK since "rock" mode)')
Expand Down Expand Up @@ -500,7 +505,6 @@ def yconfig_addons(config, appname):
newconfig = copy.deepcopy(config)

if config["zonation"]["yamlfile"] is not None:

# re-use yconfig:
zconfig = yconfig(config["zonation"]["yamlfile"])
if "zranges" in zconfig:
Expand Down Expand Up @@ -531,3 +535,4 @@ def yconfig_metadata_hc(config):
newconfig["metadata"]["globaltag"] = config["output"].get("tag", "")

return newconfig
return newconfig
1 change: 1 addition & 0 deletions src/xtgeoapp_grd3dmaps/avghc/_export_via_fmudataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def export_avg_map_dataio(surf, nametuple, config):

metadata = config["metadata"]
if nameid not in metadata:
logger.info("Dataio: Nameid missing %s", nametuple)
raise ValueError(
f"Seems that 'metadata' for {nameid} is missing! Cf. documentation"
)
Expand Down
1 change: 1 addition & 0 deletions src/xtgeoapp_grd3dmaps/avghc/grid3d_average_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def do_parse_args(args):

def yamlconfig(inputfile, args):
"""Read from YAML file and modify/override"""

config = _configparser.yconfig(inputfile)
config = _configparser.prepare_metadata(config)
config = _configparser.propformatting(config)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_scripts/test_grid3d_average_map_dataio1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def test_average_map_dataio1a(datatree, avgdataio1aconfig):
)
assert surf.values.mean() == pytest.approx(0.035489, rel=0.01)

surf2 = xtgeo.surface_from_file(
res / "myzone1--avgdataio1a_average_swat--20030101_20010101.gri"
)
assert surf2.values.mean() == pytest.approx(0.113534, rel=0.01)

# read metadatafile
with open(
res / ".myzone1--avgdataio1a_average_swat--20010101_19991201.gri.yml",
Expand Down
3 changes: 3 additions & 0 deletions tests/yaml/global_config3a.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ global:
DATES: &mydates
- 1999-12-01
- 2001-01-01
- 2003-01-01

DIFFDATES: &mydiffdates
- [2001-01-01, 1999-12-01]
- [2003-01-01, 2001-01-01]
- [2003-01-01, 1999-12-01]

0 comments on commit 8b7dcc3

Please sign in to comment.