def read_run_results(gdir, window=36, filesuffix=None): """Reads the output diagnostics of a simulation and puts the data in a pandas dataframe. Parameters ---------- gdir : oggm.GlacierDirectory the glacier directory defined by oggm window : int size of the moving window: number of observations used to smooth the glacier length timeseries filesuffix : str the file identifier Returns ------- odf : pd.DataFrame dataframe containing glacier length, volume and water storage """ # read model output with xr.open_dataset( gdir.get_filepath('model_diagnostics', filesuffix=filesuffix)) as ds: ds = ds.load() # length needs filtering ts = ds.length_m.to_series() ts = ts.rolling(window).min() ts.iloc[0:window] = ts.iloc[window] # volume change delta_vol = np.append(ds.volume_m3.data[1:] - ds.volume_m3.data[0:-1], [0]) if ds.calendar_month[0] == 10 and gdir.cenlat < 0: # this is to cover up a bug in OGGM _, m = utils.hydrodate_to_calendardate(ds.hydro_year.data, ds.hydro_month.data, start_month=4) ds.calendar_month[:] = m odf = pd.DataFrame() odf['length_m'] = ts odf['volume_m3'] = ds.volume_m3 odf['delta_water_m3'] = delta_vol * 0.9 odf['month'] = ds.calendar_month return odf
def read_run_results(gdir, filesuffix=None): """Reads the output diagnostics of a simulation and puts the data in a pandas dataframe. Parameters ---------- gdir : the glacier directory filesuffix : the file identifier Returns ------- a pandas Dataframe with monthly temp and precip """ with xr.open_dataset( gdir.get_filepath('model_diagnostics', filesuffix=filesuffix)) as ds: ds = ds.load() # Lemgth needs filtering ts = ds.length_m.to_series() ts = ts.rolling(12 * 3).min() ts.iloc[0:12 * 3] = ts.iloc[12 * 3] # Volume change delta_vol = np.append(ds.volume_m3.data[1:] - ds.volume_m3.data[0:-1], [0]) if ds.calendar_month[0] == 10 and gdir.cenlat < 0: # this is to cover up a bug in OGGM _, m = utils.hydrodate_to_calendardate(ds.hydro_year.data, ds.hydro_month.data, start_month=4) ds.calendar_month[:] = m odf = pd.DataFrame() odf['length_m'] = ts odf['volume_m3'] = ds.volume_m3 odf['delta_water_m3'] = delta_vol * 0.9 odf['month'] = ds.calendar_month return odf
def test_hydro_convertion(self): # October y, m = utils.hydrodate_to_calendardate(1, 1) assert (y, m) == (0, 10) y, m = utils.hydrodate_to_calendardate(1, 4) assert (y, m) == (1, 1) y, m = utils.hydrodate_to_calendardate(1, 12) assert (y, m) == (1, 9) y, m = utils.hydrodate_to_calendardate([1, 1, 1], [1, 4, 12]) np.testing.assert_array_equal(y, [0, 1, 1]) np.testing.assert_array_equal(m, [10, 1, 9]) y, m = utils.calendardate_to_hydrodate(1, 1) assert (y, m) == (1, 4) y, m = utils.calendardate_to_hydrodate(1, 9) assert (y, m) == (1, 12) y, m = utils.calendardate_to_hydrodate(1, 10) assert (y, m) == (2, 1) y, m = utils.calendardate_to_hydrodate([1, 1, 1], [1, 9, 10]) np.testing.assert_array_equal(y, [1, 1, 2]) np.testing.assert_array_equal(m, [4, 12, 1]) # Roundtrip time = pd.period_range('0001-01', '1000-12', freq='M') y, m = utils.calendardate_to_hydrodate(time.year, time.month) y, m = utils.hydrodate_to_calendardate(y, m) np.testing.assert_array_equal(y, time.year) np.testing.assert_array_equal(m, time.month) # April y, m = utils.hydrodate_to_calendardate(1, 1, start_month=4) assert (y, m) == (0, 4) y, m = utils.hydrodate_to_calendardate(1, 4, start_month=4) assert (y, m) == (0, 7) y, m = utils.hydrodate_to_calendardate(1, 9, start_month=4) assert (y, m) == (0, 12) y, m = utils.hydrodate_to_calendardate(1, 10, start_month=4) assert (y, m) == (1, 1) y, m = utils.hydrodate_to_calendardate(1, 12, start_month=4) assert (y, m) == (1, 3) y, m = utils.hydrodate_to_calendardate([1, 1, 1], [1, 4, 12], start_month=4) np.testing.assert_array_equal(y, [0, 0, 1]) np.testing.assert_array_equal(m, [4, 7, 3]) # Roundtrip time = pd.period_range('0001-01', '1000-12', freq='M') y, m = utils.calendardate_to_hydrodate(time.year, time.month, start_month=4) y, m = utils.hydrodate_to_calendardate(y, m, start_month=4) np.testing.assert_array_equal(y, time.year) np.testing.assert_array_equal(m, time.month)