Skip to content

Commit aa94c08

Browse files
committed
add option to run arbitrary module -- let's see
1 parent 0fcd0aa commit aa94c08

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

oggm/cli/prepro_levels.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import time
1313
import logging
1414
import json
15+
import importlib
1516
import pandas as pd
1617
import numpy as np
1718
import geopandas as gpd
@@ -86,6 +87,8 @@ def run_prepro_levels(rgi_version=None, rgi_reg=None, border=None,
8687
add_millan_thickness=False, add_millan_velocity=False,
8788
add_hugonnet_dhdt=False, add_bedmachine=False,
8889
add_glathida=False,
90+
custom_climate_task=None,
91+
custom_climate_task_kwargs=None,
8992
start_level=None, start_base_url=None, max_level=5,
9093
logging_level='WORKFLOW',
9194
dynamic_spinup=False, err_dmdtda_scaling_factor=0.2,
@@ -169,6 +172,12 @@ def run_prepro_levels(rgi_version=None, rgi_reg=None, border=None,
169172
add_glathida : bool
170173
adds (reprojects) the glathida thickness data to the glacier
171174
directories. Data points are stored as csv files.
175+
custom_climate_task : str
176+
optional import path to a custom climate task in the form
177+
"module_path:function_name". If provided, it will be called instead of
178+
the default process_climate_data.
179+
custom_climate_task_kwargs : dict
180+
optional kwargs passed to the custom climate task when it is executed.
172181
start_level : int
173182
the pre-processed level to start from (default is to start from
174183
scratch). If set, you'll need to indicate start_base_url as well.
@@ -629,7 +638,23 @@ def _time_log():
629638
utils.mkdir(sum_dir)
630639

631640
# Climate
632-
workflow.execute_entity_task(tasks.process_climate_data, gdirs)
641+
climate_kwargs = custom_climate_task_kwargs or {}
642+
if custom_climate_task:
643+
try:
644+
mod_path, func_name = custom_climate_task.rsplit(':', 1)
645+
except ValueError:
646+
raise InvalidParamsError('custom_climate_task must be of the form "module:function"')
647+
try:
648+
mod = importlib.import_module(mod_path)
649+
except ModuleNotFoundError as err:
650+
raise InvalidParamsError(f'Cannot import module {mod_path}') from err
651+
try:
652+
custom_task_func = getattr(mod, func_name)
653+
except AttributeError as err:
654+
raise InvalidParamsError(f'Module {mod_path} has no attribute {func_name}') from err
655+
workflow.execute_entity_task(custom_task_func, gdirs, **climate_kwargs)
656+
else:
657+
workflow.execute_entity_task(tasks.process_climate_data, gdirs)
633658

634659
# Small optim to avoid concurrency
635660
utils.get_geodetic_mb_dataframe()
@@ -946,6 +971,11 @@ def parse_args(args):
946971
help='adds (reprojects) the glathida point thickness '
947972
'observations to the glacier directories. '
948973
'The data points are stored as csv.')
974+
parser.add_argument('--custom-climate-task', type=str, default=None,
975+
help='Custom climate task import path in the form module:function. '
976+
'If provided, it replaces the default process_climate_data.')
977+
parser.add_argument('--custom-climate-task-kwargs', type=json.loads, default=None,
978+
help='JSON dict of kwargs passed to the custom climate task.')
949979
parser.add_argument('--demo', nargs='?', const=True, default=False,
950980
help='if you want to run the prepro for the '
951981
'list of demo glaciers.')
@@ -1036,6 +1066,8 @@ def parse_args(args):
10361066
add_hugonnet_dhdt=args.add_hugonnet_dhdt,
10371067
add_bedmachine=args.add_bedmachine,
10381068
add_glathida=args.add_glathida,
1069+
custom_climate_task=args.custom_climate_task,
1070+
custom_climate_task_kwargs=args.custom_climate_task_kwargs,
10391071
dynamic_spinup=dynamic_spinup,
10401072
err_dmdtda_scaling_factor=args.err_dmdtda_scaling_factor,
10411073
dynamic_spinup_start_year=args.dynamic_spinup_start_year,

oggm/tests/test_prepro.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,15 +1433,9 @@ def test_mb_calibration_from_scalar_mb(self):
14331433
centerlines.fixed_dx_elevation_band_flowline(gdir)
14341434
climate.process_custom_climate_data(gdir)
14351435

1436-
from oggm.ignore.lmr_forcing import process_mira_data
1437-
process_mira_data(gdir, filesuffix='mira')
1438-
14391436
with xr.open_dataset(gdir.get_filepath('climate_historical')) as dsc:
14401437
dsc = dsc.load()
14411438

1442-
with xr.open_dataset(gdir.get_filepath('climate_historical', filesuffix='mira')) as dse:
1443-
dse = dse.load()
1444-
14451439
mbdf = gdir.get_ref_mb_data()
14461440
mbdf['ref_mb'] = mbdf['ANNUAL_BALANCE']
14471441
ref_mb = mbdf.ANNUAL_BALANCE.mean()

0 commit comments

Comments
 (0)