Skip to content

Commit

Permalink
faster process_cmip_data by allowing start/end year selection
Browse files Browse the repository at this point in the history
  • Loading branch information
lilianschuster committed Apr 12, 2023
1 parent e3bcccb commit b7e72d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
19 changes: 18 additions & 1 deletion oggm/shop/gcm_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ def process_cesm_data(gdir, filesuffix='', fpath_temp=None, fpath_precc=None,

@entity_task(log, writes=['gcm_data'])
def process_cmip_data(gdir, filesuffix='', fpath_temp=None,
fpath_precip=None, **kwargs):
fpath_precip=None, y0=None, y1=None,
**kwargs):
"""Read, process and store the CMIP5 and CMIP6 climate data for this glacier.
It stores the data in a format that can be used by the OGGM mass balance
Expand All @@ -413,17 +414,33 @@ def process_cmip_data(gdir, filesuffix='', fpath_temp=None,
path to the temp file
fpath_precip : str
path to the precip file
y0 : int
start year of the CMIP data processing.
Default is None which processes the entire timeseries.
Set this to the beginning of your bias correction/
projection period to make process_cmip_data faster.
y1 : int
end year of the CMIP data processing.
Default is None, same as y0.
**kwargs: any kwarg to be passed to ref:`process_gcm_data`
"""

# Glacier location
glon = gdir.cenlon
glat = gdir.cenlat

if y0 is not None:
y0 = str(y0)
if y1 is not None:
y1 = str(y1)
# Read the GCM files
with xr.open_dataset(fpath_temp, use_cftime=True) as tempds, \
xr.open_dataset(fpath_precip, use_cftime=True) as precipds:

# only process and save the gcm data selected --> saves some time!
if (y0 is not None) or (y1 is not None):
tempds = tempds.sel(time=slice(y0, y1))
precipds = precipds.sel(time=slice(y0, y1))
# Check longitude conventions
if tempds.lon.min() >= 0 and glon <= 0:
glon += 360
Expand Down
14 changes: 13 additions & 1 deletion oggm/tests/test_prepro.py
Original file line number Diff line number Diff line change
Expand Up @@ -2791,10 +2791,18 @@ def test_process_cmip5(self):
fpath_temp=fpath_temp,
fpath_precip=fpath_precip,
filesuffix='_CCSM4')
gcm_climate.process_cmip_data(gdir,
fpath_temp=fpath_temp,
fpath_precip=fpath_precip,
filesuffix='_CCSM4_y0_y1', y0=1960,
y1=2095)

fh = gdir.get_filepath('climate_historical')
fcmip = gdir.get_filepath('gcm_data', filesuffix='_CCSM4')
with xr.open_dataset(fh) as cru, xr.open_dataset(fcmip) as cmip:
fcmip_y0_y1 = gdir.get_filepath('gcm_data', filesuffix='_CCSM4_y0_y1')
with xr.open_dataset(fh) as cru,\
xr.open_dataset(fcmip) as cmip,\
xr.open_dataset(fcmip_y0_y1) as cmip_y0_y1:

# Let's do some basic checks
scru = cru.sel(time=slice('1961', '1990'))
Expand All @@ -2807,6 +2815,10 @@ def test_process_cmip5(self):
np.testing.assert_allclose(scru.prcp.mean(),
scesm.prcp.mean(),
rtol=1e-3)
# just check if the right time period is chosen
scesm_y0_y1 = cmip_y0_y1.load()
assert scesm_y0_y1['time.year'].min() == 1960
assert scesm_y0_y1['time.year'].max() == 2095

# Here also std dev! But its not perfect because std_dev
# is preserved over 31 years
Expand Down

0 comments on commit b7e72d3

Please sign in to comment.