From 7a065702f63ed97341d9a86cfd205b70c83acc0b Mon Sep 17 00:00:00 2001 From: Fabien Maussion Date: Sun, 4 Feb 2024 13:22:50 +0000 Subject: [PATCH] Fix various small bugs (#1686) * Fix various small bugs * Fix the fix --- oggm/core/centerlines.py | 2 ++ oggm/core/flowline.py | 2 ++ oggm/tests/test_models.py | 11 +++++++++-- oggm/tests/test_utils.py | 2 ++ oggm/utils/_workflow.py | 2 +- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/oggm/core/centerlines.py b/oggm/core/centerlines.py index b33567906..917db6f36 100644 --- a/oggm/core/centerlines.py +++ b/oggm/core/centerlines.py @@ -2430,6 +2430,8 @@ def fixed_dx_elevation_band_flowline(gdir, bin_variables=None, warnings.filterwarnings("ignore", category=RuntimeWarning) interp *= in_total / out_total odf[var] = interp + # Match how OGGM computes distance along flowline + odf.index = np.arange(nx) * dx_meter odf.to_csv(gdir.get_filepath('elevation_band_flowline', filesuffix='_fixed_dx')) diff --git a/oggm/core/flowline.py b/oggm/core/flowline.py index b79b9a334..d3d47bc9c 100644 --- a/oggm/core/flowline.py +++ b/oggm/core/flowline.py @@ -4170,6 +4170,8 @@ def run_with_hydro(gdir, run_task=None, store_monthly_hydro=False, ods[varname] = ('time', data) for k, v in d.items(): ods[varname].attrs[k] = v + if store_monthly_hydro and (varname + '_monthly') in ods: + ods[varname + '_monthly'].attrs[k] = v # Append the output to the existing diagnostics fpath = gdir.get_filepath('model_diagnostics', filesuffix=suffix) diff --git a/oggm/tests/test_models.py b/oggm/tests/test_models.py index 9465cf990..dc783910c 100644 --- a/oggm/tests/test_models.py +++ b/oggm/tests/test_models.py @@ -198,9 +198,16 @@ def test_init_present_time_glacier_obs_thick(self, hef_elev_gdir, # test that if w0<0 it is converted to rectangular # set some thickness to very large values to force it df_fixed_dx = pd.read_csv(gdir.get_filepath('elevation_band_flowline', - filesuffix='_fixed_dx')) + filesuffix='_fixed_dx'), + index_col=0) + + # Check consistency between csv and flowline object + fl_inv = gdir.read_pickle('inversion_flowlines')[0] + assert fl_inv.nx == len(df_fixed_dx) + assert fl_inv.dx_meter == (df_fixed_dx.index[1] - df_fixed_dx.index[0]) + np.testing.assert_allclose(fl_inv.dx_meter * np.arange(fl_inv.nx), df_fixed_dx.index) new_thick = df_fixed_dx['consensus_ice_thickness'] - new_thick[-10:] = new_thick[-10:] + 1000 + new_thick.iloc[-10:] = new_thick.iloc[-10:] + 1000 df_fixed_dx['consensus_ice_thickness'] = new_thick ref_vol_rect = np.sum(df_fixed_dx['area_m2'] * new_thick) df_fixed_dx.to_csv(gdir.get_filepath('elevation_band_flowline', diff --git a/oggm/tests/test_utils.py b/oggm/tests/test_utils.py index 2a6b9f4c7..1ec8920d1 100644 --- a/oggm/tests/test_utils.py +++ b/oggm/tests/test_utils.py @@ -549,6 +549,8 @@ def check_result(ds): assert 'area_m2_min_h' in ds.data_vars assert 'melt_on_glacier' in ds.data_vars assert 'melt_on_glacier_monthly' in ds.data_vars + assert ds_1['melt_on_glacier'].unit == 'kg yr-1' + assert ds_1['melt_on_glacier_monthly'].unit == 'kg yr-1' assert np.all(np.isnan( ds.loc[{'rgi_id': gdirs[0].rgi_id}]['area_m2_min_h'].values)) assert np.all(np.isnan( diff --git a/oggm/utils/_workflow.py b/oggm/utils/_workflow.py index 88f49366a..3ab4bec9e 100644 --- a/oggm/utils/_workflow.py +++ b/oggm/utils/_workflow.py @@ -510,7 +510,7 @@ def _entity_task(gdir, *, reset=None, print_log=True, raise if self.fallback is not None: - self.fallback(gdir) + out = self.fallback(gdir) if return_value: return out