From fe3d6eafe372f499d0c2fcd11ed0f1e3880f9b0b Mon Sep 17 00:00:00 2001 From: Fabien Maussion Date: Tue, 21 Nov 2023 08:52:08 +0100 Subject: [PATCH] Start from L1 in prepro gdirs --- oggm/cli/prepro_levels.py | 32 ++++++++++++++++++++++++++++++++ oggm/shop/rgitopo.py | 14 ++++---------- oggm/tests/test_utils.py | 3 +++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/oggm/cli/prepro_levels.py b/oggm/cli/prepro_levels.py index 24b0538c0..c16e5a4ee 100644 --- a/oggm/cli/prepro_levels.py +++ b/oggm/cli/prepro_levels.py @@ -81,6 +81,7 @@ def run_prepro_levels(rgi_version=None, rgi_reg=None, border=None, elev_bands=False, centerlines=False, override_params=None, mb_calibration_strategy='informed_threestep', + select_source_from_dir=None, keep_dem_folders=False, add_consensus_thickness=False, add_itslive_velocity=False, add_millan_thickness=False, add_millan_velocity=False, add_hugonnet_dhdt=False, @@ -133,6 +134,14 @@ def run_prepro_levels(rgi_version=None, rgi_reg=None, border=None, - 'informed_threestep' (default) - 'melt_temp' - 'temp_melt' + select_source_from_dir : str + if starting from a level 1 "ALL" or "STANDARD" DEM sources directory, + select the chosen DEM source here. If you set it to "BY_RES" here, + COPDEM will be used and its resolution chosen based on the gdir's + map resolution (COPDEM30 for dx < 60 m, COPDEM90 elsewhere). + keep_dem_folders : bool + if `select_source_from_dir` is used, wether to keep the original + DEM folders in or not. add_consensus_thickness : bool adds (reprojects) the consensus estimates thickness to the glacier directories. With elev_bands=True, the data will also be binned. @@ -427,6 +436,16 @@ def _time_log(): 'N elev bands type: {}.' ''.format(len(gdirs_cent), len(gdirs_band))) + # If we are coming from a multi-dem setup, let's select it from there + if select_source_from_dir is not None: + from oggm.shop.rgitopo import select_dem_from_dir + workflow.execute_entity_task(select_dem_from_dir, gdirs_band, + dem_source=select_source_from_dir, + keep_dem_folders=keep_dem_folders) + workflow.execute_entity_task(select_dem_from_dir, gdirs_cent, + dem_source=select_source_from_dir, + keep_dem_folders=keep_dem_folders) + # HH2015 method workflow.execute_entity_task(tasks.simple_glacier_masks, gdirs_band) @@ -814,6 +833,17 @@ def parse_args(args): 'compatible with level 1 folders, after which ' 'the processing will stop. The default is to use ' 'the default OGGM DEM.') + parser.add_argument('--select-source-from-dir', type=str, + default=None, + help='if starting from a level 1 "ALL" or "STANDARD" DEM ' + 'sources directory, select the chosen DEM source here. ' + 'If you set it to "BY_RES" here, COPDEM will be used and ' + 'its resolution chosen based on the gdirs map resolution ' + '(COPDEM30 for dx < 60 m, COPDEM90 elsewhere).') + parser.add_argument('--keep-dem-folders', type=bool, + default=False, + help='if `select_source_from_dir` is used, wether to keep ' + 'the original DEM folders in or not.') parser.add_argument('--add-consensus-thickness', nargs='?', const=True, default=False, help='adds (reprojects) the consensus thickness ' 'estimates to the glacier directories. ' @@ -919,6 +949,8 @@ def parse_args(args): logging_level=args.logging_level, elev_bands=args.elev_bands, centerlines=args.centerlines, + select_source_from_dir=args.select_source_from_dir, + keep_dem_folders=args.keep_dem_folders, add_consensus_thickness=args.add_consensus_thickness, add_millan_thickness=args.add_millan_thickness, add_itslive_velocity=args.add_itslive_velocity, diff --git a/oggm/shop/rgitopo.py b/oggm/shop/rgitopo.py index 61568f047..26f30c1b0 100644 --- a/oggm/shop/rgitopo.py +++ b/oggm/shop/rgitopo.py @@ -91,23 +91,17 @@ def select_dem_from_dir(gdir, dem_source=None, keep_dem_folders=False): the glacier directory dem_source : str the source to pick from. If 'RGI', we assume that there is a - `dem_source` attribute in the RGI file. + `dem_source` attribute in the RGI file. If 'BY_RES', we use + COPDEM30 for all gdirs with resolution smaller than 60m keep_dem_folders : bool the default is to delete the other DEM directories to save space. Set this to True to prevent that (e.g. for sensitivity tests) """ - # Start by deleting noise - for fn in os.listdir(gdir.dir): - if fn in ['glacier_mask.tif', 'glacier_grid.json', - 'outlines.tar.gz', 'intersects.tar.gz']: - continue - fn = os.path.join(gdir.dir, fn) - if os.path.isfile(fn): - os.remove(fn) - if dem_source == 'RGI': dem_source = gdir.rgi_dem_source + if dem_source == 'BY_RES': + dem_source = 'COPDEM30' if gdir.grid.dx < 60 else 'COPDEM90' sources = [f.name for f in os.scandir(gdir.dir) if f.is_dir() and not f.name.startswith('.')] diff --git a/oggm/tests/test_utils.py b/oggm/tests/test_utils.py index 10f3cf494..9c5564926 100644 --- a/oggm/tests/test_utils.py +++ b/oggm/tests/test_utils.py @@ -965,6 +965,7 @@ def test_parse_args(self): '--map-border', '160', '--output', 'local/out', '--working-dir', 'local/work', + '--select-source-from-dir', 'BY_RES', '--dem-source', 'ALL', '--add-consensus-thickness', '--add-millan-thickness', @@ -984,6 +985,8 @@ def test_parse_args(self): assert not kwargs['is_test'] assert not kwargs['elev_bands'] assert not kwargs['centerlines'] + assert kwargs['select_source_from_dir'] == 'BY_RES' + assert kwargs['keep_dem_folders'] is False assert kwargs['add_consensus_thickness'] assert kwargs['add_millan_thickness'] assert kwargs['add_millan_velocity']