Skip to content

Commit

Permalink
added possibility to merge monthly distributed thicknesses
Browse files Browse the repository at this point in the history
  • Loading branch information
pat-schmitt committed Apr 19, 2024
1 parent df14273 commit 7f8f2f1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
30 changes: 23 additions & 7 deletions oggm/sandbox/distribute_2d.py
Expand Up @@ -460,8 +460,12 @@ def merge_simulated_thickness(gdirs,
Default is gridded_simulation_merged{simulation_filesuffix}.
simulation_filesuffix : str
the filesuffix of the gridded_simulation file
years_to_merge : list | None
If not None, only the years in this list are merged. Default is None.
years_to_merge : list | xr.DataArray | None
If not None, and list of integers only the years at month 1 are
merged. You can also provide the timesteps as xarray.DataArray
containing calendar_year and calendar_month as it is the standard
output format for gridded nc files of OGGM.
Default is None.
keep_dem_file
interp
add_topography
Expand Down Expand Up @@ -523,21 +527,33 @@ def _calc_bedrock_topo(fp):

# then the simulated thickness files
if years_to_merge is None:
# open first file to get the years
# open first file to get all available timesteps
with xr.open_dataset(
gdirs[0].get_filepath('gridded_simulation',
filesuffix=simulation_filesuffix)
) as ds:
years_to_merge = ds.time.values
years_to_merge = ds.time

for timestep in years_to_merge:
if isinstance(timestep, int):
year = timestep
month = '1'
elif isinstance(timestep, xr.DataArray):
year = int(timestep.calendar_year)
month = int(timestep.calendar_month)
else:
raise NotImplementedError('Wrong type for years_to_merge! '
'Should be list of int or'
'xarray.DataArray.')

for yr in years_to_merge:
workflow.merge_gridded_data(
gdirs,
output_folder=output_folder,
output_filename=f"{output_filename}_{yr}",
output_filename=f"{output_filename}_{year}_{month:02d}",
input_file='gridded_simulation',
input_filesuffix=simulation_filesuffix,
included_variables=[('simulated_thickness', {'time': [yr]})],
included_variables=[('simulated_thickness',
{'time': [timestep]})],
preserve_totals=preserve_totals,
smooth_radius=smooth_radius,
use_glacier_mask=use_glacier_mask,
Expand Down
24 changes: 24 additions & 0 deletions oggm/tests/test_models.py
Expand Up @@ -5858,3 +5858,27 @@ def test_merge_simulated_thickness(self):
ds_merged_selected_years = ds
assert_allclose(ds_merged_selected_years.simulated_thickness.values,
ds_merged.loc[{'time': years_to_test}].simulated_thickness.values)

# test merging of monthly distributed data
workflow.execute_entity_task(run_random_climate, gdirs, y0=2014,
halfsize=5, nyears=2, seed=0,
output_filesuffix='_random_short',
store_fl_diagnostics=True)
workflow.execute_entity_task(
distribute_2d.distribute_thickness_from_simulation,
gdirs,
input_filesuffix='_random_short',
add_monthly=True,
)
distribute_2d.merge_simulated_thickness(
gdirs,
simulation_filesuffix='_random_short',
reset=True)
files_to_open_monthly = glob.glob(
os.path.join(cfg.PATHS['working_dir'],
'gridded_simulation_merged_random_short*'))
assert len(files_to_open_monthly) == 26
with xr.open_mfdataset(files_to_open_monthly) as ds:
ds_merged_monthly = ds

assert len(ds_merged_monthly.time) == 25

0 comments on commit 7f8f2f1

Please sign in to comment.