Skip to content

Commit fa70b9a

Browse files
Add extra steps to diagnostic to make output of hydrology/recipe_lisflood.yml usable by the LISFLOOD model (ESMValGroup#1737)
* draft of extra steps to make output forcing usable by lisflood * replace draft cube manipulations with iris in diagnostic and process data over global domain in recipe * clean up and turn crop region * loop over coordinates to remove unused coordinates and the bounds of used coordinates * reorder coordinates * change to xarray as sorting variables and coordinates is not possible in iris * fix output file encoding * fix wrong variable name * fix date fetching * Apply suggestions from code review * Automatic formatting Co-authored-by: Peter Kalverla <peter.kalverla@gmx.com>
1 parent 837c092 commit fa70b9a

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

esmvaltool/diag_scripts/hydrology/lisflood.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from pathlib import Path
44

55
import iris
6+
import numpy as np
7+
import xarray as xr
68
from iris.analysis.maths import exp as iris_exp
79

810
from esmvaltool.diag_scripts.shared import (ProvenanceLogger,
@@ -20,6 +22,7 @@ def get_provenance_record(ancestor_files):
2022
'authors': [
2123
'verhoeven_stefan',
2224
'kalverla_peter',
25+
'camphuijsen_jaro',
2326
],
2427
'projects': [
2528
'ewatercycle',
@@ -76,16 +79,15 @@ def compute_vapour_pressure(tdps):
7679
esat.long_name = 'Daily Actual Water Vapour Pressure'
7780
esat.standard_name = 'water_vapor_pressure'
7881
esat.units = 'hPa'
79-
esat.attributes['comment'] = ''.join((
80-
'Actual water vapour pressure of air near the surface calculated',
81-
' from tdps using Tetens formula'
82-
))
82+
esat.attributes['comment'] = ''.join(
83+
('Actual water vapour pressure of air near the surface calculated',
84+
' from tdps using Tetens formula'))
8385
return esat
8486

8587

8688
def compute_windspeed(uas, vas):
8789
"""Compute absolute wind speed from horizontal components."""
88-
sfc_wind = (uas ** 2 + vas ** 2) ** .5
90+
sfc_wind = (uas**2 + vas**2)**.5
8991
sfc_wind.var_name = 'sfcWind'
9092
sfc_wind.long_name = 'Daily-Mean Near-Surface Wind Speed'
9193
sfc_wind.standard_name = 'wind_speed'
@@ -94,11 +96,10 @@ def compute_windspeed(uas, vas):
9496
return sfc_wind
9597

9698

97-
def save(cube, var_name, dataset, cfg):
99+
def save(xrds, var_name, dataset, cfg):
98100
"""Save processed cube to a lisflood-compatible file."""
99-
time_coord = cube.coord('time')
100-
start_year = time_coord.cell(0).point.year
101-
end_year = time_coord.cell(-1).point.year
101+
start_year = int(xrds.time[0].dt.year)
102+
end_year = int(xrds.time[-1].dt.year)
102103
basename = '_'.join([
103104
'lisflood',
104105
dataset,
@@ -108,7 +109,7 @@ def save(cube, var_name, dataset, cfg):
108109
str(end_year),
109110
])
110111
output_file = get_diagnostic_filename(basename, cfg)
111-
iris.save(cube, output_file, fill_value=1.e20)
112+
xrds.to_netcdf(output_file, encoding={var_name: {'_FillValue': 1.e20}})
112113
return output_file
113114

114115

@@ -138,19 +139,23 @@ def main(cfg):
138139
cubes['pr'].units = 'mm d-1'
139140

140141
for var_name, cube in cubes.items():
141-
cube.remove_coord('shape_id')
142142
# Western emisphere longitudes should be negative
143143
points = cube.coord('longitude').points
144144
cube.coord('longitude').points = (points + 180) % 360 - 180
145145
# latitudes decreasing
146146
cube = cube[:, ::-1, ...]
147147

148-
output_file = save(cube, var_name, dataset, cfg)
148+
# convert to xarray dataset (xrds)
149+
# remove coordinate bounds drop extra coordinates and reorder
150+
xrds = xr.DataArray.from_iris(cube).to_dataset()
151+
ordered_coords = ['lon', 'lat', 'time']
152+
extra_coords = np.setdiff1d(xrds.coords, ordered_coords)
153+
xrds = xrds.drop(extra_coords)[ordered_coords + [var_name]]
154+
155+
output_file = save(xrds, var_name, dataset, cfg)
149156

150157
# Store provenance
151-
provenance_record = get_provenance_record(
152-
ancestors[var_name]
153-
)
158+
provenance_record = get_provenance_record(ancestors[var_name])
154159
with ProvenanceLogger(cfg) as provenance_logger:
155160
provenance_logger.log(output_file, provenance_record)
156161

0 commit comments

Comments
 (0)