示例#1
0
    def test_annual_climate(self):
        """Test my routine against the corresponding OGGM routine from
        the `PastMassBalance()` model.
        """

        # run all needed prepro tasks
        gdir = self._setup_mb_test()

        # instance the mass balance models
        vas_mbmod = vascaling.VAScalingMassBalance(gdir)
        past_mbmod = massbalance.PastMassBalance(gdir)

        # get relevant glacier surface elevation
        min_hgt, max_hgt = vascaling.get_min_max_elevation(gdir)
        heights = np.array([min_hgt, (min_hgt + max_hgt) / 2, max_hgt])

        # specify an (arbitray) year
        year = 1975
        # get mass balance relevant climate information
        temp_for_melt_vas, prcp_solid_vas = \
            vas_mbmod.get_annual_climate(min_hgt, max_hgt, year)
        _, temp_for_melt_oggm, _, prcp_solid_oggm = \
            past_mbmod.get_annual_climate(heights, year)

        # prepare my (monthly) values for comparison
        temp_for_melt_vas = temp_for_melt_vas.sum()
        prcp_solid_vas = prcp_solid_vas.sum()

        # computed positive terminus melting temperature must be equal for both
        # used methods, i.e. temp_VAS == temp_OGGM
        np.testing.assert_allclose(temp_for_melt_vas,
                                   temp_for_melt_oggm[0],
                                   rtol=1e-3)

        # glacier averaged solid precipitation amount must be greater than (or
        # equal to) solid precipitation amount at glacier terminus elevation
        assert md(prcp_solid_oggm[0], prcp_solid_vas) >= 0
        # glacier averaged solid precipitation amount must be comparable to the
        # solid precipitation amount at average glacier surface elevation
        assert rel_err(prcp_solid_oggm[1], prcp_solid_vas) <= 0.15
        # glacier averaged solid precipitation amount must be less than (or
        # equal to) solid precipitation amount at maximum glacier elevation
        assert md(prcp_solid_oggm[2], prcp_solid_vas) <= 0
示例#2
0
    def test_present_time_glacier_massbalance(self):

        gdir = init_hef(border=DOM_BORDER)
        flowline.init_present_time_glacier(gdir)

        mb_mod = massbalance.HistalpMassBalanceModel(gdir)

        fls = gdir.read_pickle('model_flowlines')
        glacier = flowline.FlowlineModel(fls)

        hef_file = utils.get_demo_file('mbdata_RGI40-11.00897.csv')
        mbdf = pd.read_csv(hef_file).set_index('YEAR')

        hgts = np.array([])
        widths = np.array([])
        for fl in glacier.fls:
            hgts = np.concatenate((hgts, fl.surface_h))
            widths = np.concatenate((widths, fl.widths_m))
        tot_mb = []
        refmb = []
        grads = hgts * 0
        for yr, mb in mbdf.iterrows():
            refmb.append(mb['ANNUAL_BALANCE'])
            mbh = mb_mod.get_mb(hgts, yr) * SEC_IN_YEAR * cfg.RHO
            grads += mbh
            tot_mb.append(np.average(mbh, weights=widths))
        grads /= len(tot_mb)

        # Bias
        self.assertTrue(np.abs(utils.md(tot_mb, refmb)) < 50)

        # Gradient
        dfg = pd.read_csv(utils.get_demo_file('mbgrads_RGI40-11.00897.csv'),
                          index_col='ALTITUDE').mean(axis=1)

        # Take the altitudes below 3100 and fit a line
        dfg = dfg[dfg.index < 3100]
        pok = np.where(hgts < 3100)
        from scipy.stats import linregress
        slope_obs, _, _, _, _ = linregress(dfg.index, dfg.values)
        slope_our, _, _, _, _ = linregress(hgts[pok], grads[pok])
        np.testing.assert_allclose(slope_obs, slope_our, rtol=0.1)
示例#3
0
    def test_present_time_glacier_massbalance(self):

        gdir = init_hef(border=DOM_BORDER)
        flowline.init_present_time_glacier(gdir)

        mb_mod = massbalance.HistalpMassBalanceModel(gdir)

        fls = gdir.read_pickle('model_flowlines')
        glacier = flowline.FlowlineModel(fls)

        hef_file = utils.get_demo_file('mbdata_RGI40-11.00897.csv')
        mbdf = pd.read_csv(hef_file).set_index('YEAR')

        hgts = np.array([])
        widths = np.array([])
        for fl in glacier.fls:
            hgts = np.concatenate((hgts, fl.surface_h))
            widths = np.concatenate((widths, fl.widths_m))
        tot_mb = []
        refmb = []
        grads = hgts * 0
        for yr, mb in mbdf.iterrows():
            refmb.append(mb['ANNUAL_BALANCE'])
            mbh = mb_mod.get_mb(hgts, yr) * SEC_IN_YEAR * cfg.RHO
            grads += mbh
            tot_mb.append(np.average(mbh, weights=widths))
        grads /= len(tot_mb)

        # Bias
        self.assertTrue(np.abs(utils.md(tot_mb, refmb)) < 50)

        # Gradient
        dfg = pd.read_csv(utils.get_demo_file('mbgrads_RGI40-11.00897.csv'),
                          index_col='ALTITUDE').mean(axis=1)

        # Take the altitudes below 3100 and fit a line
        dfg = dfg[dfg.index < 3100]
        pok = np.where(hgts < 3100)
        from scipy.stats import linregress
        slope_obs, _, _, _, _ = linregress(dfg.index, dfg.values)
        slope_our, _, _, _, _ = linregress(hgts[pok], grads[pok])
        np.testing.assert_allclose(slope_obs, slope_our, rtol=0.1)
示例#4
0
    def test_find_tstars(self):

        hef_file = get_demo_file('Hintereisferner.shp')
        rgidf = gpd.GeoDataFrame.from_file(hef_file)

        # loop because for some reason indexing wont work
        gdirs = []
        for index, entity in rgidf.iterrows():
            gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
            gis.define_glacier_region(gdir, entity=entity)
            gis.glacier_masks(gdir)
            centerlines.compute_centerlines(gdir)
            geometry.initialize_flowlines(gdir)
            geometry.catchment_area(gdir)
            geometry.catchment_width_geom(gdir)
            geometry.catchment_width_correction(gdir)
            gdirs.append(gdir)
        climate.distribute_climate_data(gdirs)
        climate.mu_candidates(gdir, div_id=0)

        hef_file = get_demo_file('mbdata_RGI40-11.00897.csv')
        mbdf = pd.read_csv(hef_file).set_index('YEAR')
        t_stars, bias = climate.t_star_from_refmb(gdir, mbdf['ANNUAL_BALANCE'])

        y, t, p = climate.mb_yearly_climate_on_glacier(gdir, div_id=0)

        # which years to look at
        selind = np.searchsorted(y, mbdf.index)
        t = t[selind]
        p = p[selind]

        mu_yr_clim = gdir.read_pickle('mu_candidates', div_id=0)
        for t_s, rmd in zip(t_stars, bias):
            mb_per_mu = p - mu_yr_clim.loc[t_s] * t
            md = utils.md(mbdf['ANNUAL_BALANCE'], mb_per_mu)
            np.testing.assert_allclose(md, rmd)
            self.assertTrue(np.abs(md / np.mean(mbdf['ANNUAL_BALANCE'])) < 0.1)
            r = utils.corrcoef(mbdf['ANNUAL_BALANCE'], mb_per_mu)
            self.assertTrue(r > 0.8)
示例#5
0
    def test_find_tstars(self):

        hef_file = get_demo_file('Hintereisferner.shp')
        rgidf = gpd.GeoDataFrame.from_file(hef_file)

        # loop because for some reason indexing wont work
        gdirs = []
        for index, entity in rgidf.iterrows():
            gdir = cfg.GlacierDir(entity, base_dir=self.testdir)
            gis.define_glacier_region(gdir, entity)
            gis.glacier_masks(gdir)
            centerlines.compute_centerlines(gdir)
            geometry.initialize_flowlines(gdir)
            geometry.catchment_area(gdir)
            geometry.catchment_width_geom(gdir)
            geometry.catchment_width_correction(gdir)
            gdirs.append(gdir)
        climate.distribute_climate_data(gdirs)
        climate.mu_candidates(gdir, div_id=0)

        hef_file = get_demo_file('mbdata_RGI40-11.00897.csv')
        mbdf = pd.read_csv(hef_file).set_index('YEAR')
        t_stars, bias = climate.t_star_from_refmb(gdir, mbdf['ANNUAL_BALANCE'])

        y, t, p = climate.mb_yearly_climate_on_glacier(gdir, div_id=0)

        # which years to look at
        selind = np.searchsorted(y, mbdf.index)
        t = t[selind]
        p = p[selind]

        mu_yr_clim = gdir.read_pickle('mu_candidates', div_id=0)
        for t_s, rmd in zip(t_stars, bias):
            mb_per_mu = p - mu_yr_clim.loc[t_s] * t
            md = utils.md(mbdf['ANNUAL_BALANCE'], mb_per_mu)
            np.testing.assert_allclose(md, rmd)
            self.assertTrue(np.abs(md/np.mean(mbdf['ANNUAL_BALANCE'])) < 0.1)
            r = utils.corrcoef(mbdf['ANNUAL_BALANCE'], mb_per_mu)
            self.assertTrue(r > 0.8)
示例#6
0
    def test_repro_to_glacier(self, class_case_dir, monkeypatch):

        # Init
        cfg.initialize()
        cfg.PATHS['working_dir'] = class_case_dir
        cfg.PARAMS['use_intersects'] = False
        cfg.PATHS['dem_file'] = get_demo_file('dem_Columbia.tif')
        cfg.PARAMS['border'] = 10

        entity = gpd.read_file(get_demo_file('RGI60-01.10689.shp')).iloc[0]
        gdir = oggm.GlacierDirectory(entity)
        tasks.define_glacier_region(gdir)
        tasks.glacier_masks(gdir)

        # use our files
        region_files = {
            'ALA': {
                'vx': get_demo_file('crop_ALA_G0120_0000_vx.tif'),
                'vy': get_demo_file('crop_ALA_G0120_0000_vy.tif')
            }
        }
        monkeypatch.setattr(its_live, 'region_files', region_files)
        monkeypatch.setattr(utils, 'file_downloader', lambda x: x)

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=RuntimeWarning)
            its_live.velocity_to_gdir(gdir)

        with xr.open_dataset(gdir.get_filepath('gridded_data')) as ds:
            mask = ds.glacier_mask.data.astype(bool)
            vx = ds.obs_icevel_x.where(mask).data
            vy = ds.obs_icevel_y.where(mask).data

        vel = np.sqrt(vx**2 + vy**2)
        assert np.nanmax(vel) > 2900
        assert np.nanmin(vel) < 2

        # We reproject with rasterio and check no big diff
        cfg.BASENAMES['its_live_vx'] = ('its_live_vx.tif', '')
        cfg.BASENAMES['its_live_vy'] = ('its_live_vy.tif', '')
        gis.rasterio_to_gdir(gdir,
                             region_files['ALA']['vx'],
                             'its_live_vx',
                             resampling='bilinear')
        gis.rasterio_to_gdir(gdir,
                             region_files['ALA']['vy'],
                             'its_live_vy',
                             resampling='bilinear')

        with xr.open_rasterio(gdir.get_filepath('its_live_vx')) as da:
            _vx = da.where(mask).data.squeeze()
        with xr.open_rasterio(gdir.get_filepath('its_live_vy')) as da:
            _vy = da.where(mask).data.squeeze()

        _vel = np.sqrt(_vx**2 + _vy**2)
        np.testing.assert_allclose(utils.rmsd(vel[mask], _vel[mask]),
                                   0,
                                   atol=40)
        np.testing.assert_allclose(utils.md(vel[mask], _vel[mask]), 0, atol=8)

        if DO_PLOT:
            import matplotlib.pyplot as plt

            smap = salem.Map(gdir.grid.center_grid, countries=False)
            smap.set_shapefile(gdir.read_shapefile('outlines'))

            with warnings.catch_warnings():
                warnings.filterwarnings('ignore', category=RuntimeWarning)
                smap.set_topography(gdir.get_filepath('dem'))

            vel = np.sqrt(vx**2 + vy**2)
            smap.set_data(vel)
            smap.set_plot_params(cmap='Blues', vmin=None, vmax=None)

            xx, yy = gdir.grid.center_grid.xy_coordinates
            xx, yy = smap.grid.transform(xx, yy, crs=gdir.grid.proj)

            yy = yy[2::5, 2::5]
            xx = xx[2::5, 2::5]
            vx = vx[2::5, 2::5]
            vy = vy[2::5, 2::5]

            f, ax = plt.subplots()
            smap.visualize(ax=ax,
                           title='ITS_LIVE velocity',
                           cbar_title='m yr-1')
            ax.quiver(xx, yy, vx, vy)
            plt.show()
示例#7
0
#Uncomment for paper information
# ids_negative = df_racmo_negative.RGIId.values
# keep_ids_negative = [(i in ids_negative) for i in rgidf.RGIId]
# rgidf_racmo_negative = rgidf.iloc[keep_ids_negative]
#
# area_racmo_negative = rgidf_racmo_negative.Area.sum()
#
# print('Area % coverage where racmo data is negative',
#       area_racmo_negative/ study_area * 100)
#
# negative_percet = np.round(area_racmo_negative/ study_area * 100)

RMSD = utils.rmsd(df_vel_result.u_obs, df_vel_result.u_surf)
print('RMSD between observations and oggm', RMSD)
mean_dev = utils.md(df_vel_result.u_obs, df_vel_result.u_surf)
print('mean difference between observations and oggm', mean_dev)

slope, intercept, r_value, p_value, std_err = stats.linregress(
    df_vel_result.u_obs, df_vel_result.u_surf)

Num = area_with_cal_result / study_area * 100

print('N = ', Num)
print('bo = ', slope)
print(intercept)
print(r_value)
print(p_value)
print(mean_dev)
print(RMSD)
示例#8
0
    def test_yearly_mb_temp_prcp(self):
        """Test the routine which returns the yearly mass balance relevant
        climate parameters, i.e. positive melting temperature and solid
        precipitation. The testing target is the output of the corresponding
        OGGM routine `get_yearly_mb_climate_on_glacier(gdir)`.
        """

        # read the Hintereisferner DEM
        hef_file = get_demo_file('Hintereisferner_RGI5.shp')
        entity = gpd.read_file(hef_file).iloc[0]

        # initialize the GlacierDirectory
        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
        # define the local grid and glacier mask
        gis.define_glacier_region(gdir, entity=entity)
        gis.glacier_masks(gdir)
        # run centerline prepro tasks
        centerlines.compute_centerlines(gdir)
        centerlines.initialize_flowlines(gdir)
        centerlines.catchment_area(gdir)
        centerlines.catchment_intersections(gdir)
        centerlines.catchment_width_geom(gdir)
        centerlines.catchment_width_correction(gdir)
        # process the given climate file
        climate.process_custom_climate_data(gdir)

        # get yearly sums of terminus temperature and solid precipitation
        years, temp, prcp = vascaling.get_yearly_mb_temp_prcp(gdir)

        # use the OGGM methode to get the mass balance
        # relevant climate parameters
        years_oggm, temp_oggm, prcp_oggm = \
            climate.mb_yearly_climate_on_glacier(gdir)

        # the energy input at the glacier terminus must be greater than (or
        # equal to) the glacier wide average, since the air temperature drops
        # with elevation, i.e. the mean deviation must be positive, using the
        # OGGM data as reference
        assert md(temp_oggm, temp) >= 0
        # consequentially, the average mass input must be less than (or equal
        # to) the mass input integrated over the whole glacier surface, i.e.
        # the mean deviation must be negative, using the OGGM data as reference
        # TODO: does it actually?! And if so, why?! @ASK
        assert md(prcp_oggm, prcp) <= 0

        # correlation must be higher than set threshold
        assert corrcoef(temp, temp_oggm) >= 0.94
        assert corrcoef(prcp, prcp_oggm) >= 0.98

        # get terminus temperature using the OGGM routine
        fpath = gdir.get_filepath('gridded_data')
        with ncDataset(fpath) as nc:
            mask = nc.variables['glacier_mask'][:]
            topo = nc.variables['topo'][:]
        heights = np.array([np.min(topo[np.where(mask == 1)])])
        years_height, temp_height, _ = \
            climate.mb_yearly_climate_on_height(gdir, heights, flatten=False)
        temp_height = temp_height[0]
        # both time series must be equal
        np.testing.assert_array_equal(temp, temp_height)

        # get solid precipitation averaged over the glacier
        # (not weighted with widths)
        fls = gdir.read_pickle('inversion_flowlines')
        heights = np.array([])
        for fl in fls:
            heights = np.append(heights, fl.surface_h)
        years_height, _, prcp_height = \
            climate.mb_yearly_climate_on_height(gdir, heights, flatten=True)
        # correlation must be higher than set threshold
        assert corrcoef(prcp, prcp_height) >= 0.99

        # TODO: assert absolute values (or differences) of precipitation @ASK

        # test exception handling of out of bounds time/year range
        with self.assertRaises(climate.MassBalanceCalibrationError):
            # start year out of bounds
            year_range = [1500, 1980]
            _, _, _ = vascaling.get_yearly_mb_temp_prcp(gdir,
                                                        year_range=year_range)
        with self.assertRaises(climate.MassBalanceCalibrationError):
            # end year oud of bounds
            year_range = [1980, 3000]
            _, _, _ = vascaling.get_yearly_mb_temp_prcp(gdir,
                                                        year_range=year_range)
        with self.assertRaises(ValueError):
            # get not N full years
            t0 = datetime.datetime(1980, 1, 1)
            t1 = datetime.datetime(1980, 3, 1)
            time_range = [t0, t1]
            _, _, _ = vascaling.get_yearly_mb_temp_prcp(gdir,
                                                        time_range=time_range)

        # TODO: assert gradient in climate file?!

        pass
示例#9
0
文件: test_prepro.py 项目: OGGM/oggm
    def test_find_tstars_prcp_fac(self):

        hef_file = get_demo_file("Hintereisferner.shp")
        entity = gpd.GeoDataFrame.from_file(hef_file).iloc[0]

        mb_file = get_demo_file("RGI_WGMS_oetztal.csv")
        mb_file = os.path.join(os.path.dirname(mb_file), "mbdata", "mbdata_RGI40-11.00897.csv")

        cfg.PARAMS["prcp_auto_scaling_factor"] = True

        gdirs = []
        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
        gis.define_glacier_region(gdir, entity=entity)
        gis.glacier_masks(gdir)
        centerlines.compute_centerlines(gdir)
        geometry.initialize_flowlines(gdir)
        geometry.catchment_area(gdir)
        geometry.catchment_width_geom(gdir)
        geometry.catchment_width_correction(gdir)
        gdirs.append(gdir)
        climate.process_histalp_nonparallel(gdirs)
        climate.mu_candidates(gdir, div_id=0)

        mbdf = pd.read_csv(mb_file).set_index("YEAR")["ANNUAL_BALANCE"]
        t_stars, bias, prcp_fac = climate.t_star_from_refmb(gdir, mbdf)

        y, t, p = climate.mb_yearly_climate_on_glacier(gdir, prcp_fac, div_id=0)

        # which years to look at
        selind = np.searchsorted(y, mbdf.index)
        t = t[selind]
        p = p[selind]

        dffac = gdir.read_pickle("prcp_fac_optim").loc[prcp_fac]
        np.testing.assert_allclose(dffac["avg_bias"], np.mean(bias))
        mu_yr_clim = gdir.read_pickle("mu_candidates", div_id=0)[prcp_fac]
        std_bias = []
        for t_s, rmd in zip(t_stars, bias):
            mb_per_mu = p - mu_yr_clim.loc[t_s] * t
            md = utils.md(mbdf, mb_per_mu)
            np.testing.assert_allclose(md, rmd, rtol=1e-4)
            self.assertTrue(np.abs(md / np.mean(mbdf)) < 0.1)
            r = utils.corrcoef(mbdf, mb_per_mu)
            self.assertTrue(r > 0.8)
            std_bias.append(np.std(mb_per_mu) - np.std(mbdf))

        np.testing.assert_allclose(dffac["avg_std_bias"], np.mean(std_bias), rtol=1e-4)

        # test crop years
        cfg.PARAMS["tstar_search_window"] = [1902, 0]
        climate.mu_candidates(gdir, div_id=0)
        t_stars, bias, prcp_fac = climate.t_star_from_refmb(gdir, mbdf)
        mu_yr_clim = gdir.read_pickle("mu_candidates", div_id=0)[prcp_fac]
        y, t, p = climate.mb_yearly_climate_on_glacier(gdir, prcp_fac, div_id=0)
        selind = np.searchsorted(y, mbdf.index)
        t = t[selind]
        p = p[selind]
        for t_s, rmd in zip(t_stars, bias):
            mb_per_mu = p - mu_yr_clim.loc[t_s] * t
            md = utils.md(mbdf, mb_per_mu)
            np.testing.assert_allclose(md, rmd, rtol=1e-4)
            self.assertTrue(np.abs(md / np.mean(mbdf)) < 0.1)
            r = utils.corrcoef(mbdf, mb_per_mu)
            self.assertTrue(r > 0.8)
            self.assertTrue(t_s >= 1902)

        # test distribute
        cfg.PATHS["wgms_rgi_links"] = get_demo_file("RGI_WGMS_oetztal.csv")
        climate.compute_ref_t_stars(gdirs)
        climate.distribute_t_stars(gdirs)
        cfg.PARAMS["tstar_search_window"] = [0, 0]

        df = pd.read_csv(gdir.get_filepath("local_mustar"))
        np.testing.assert_allclose(df["t_star"], t_s)
        np.testing.assert_allclose(df["bias"], rmd)
        np.testing.assert_allclose(df["prcp_fac"], prcp_fac)

        cfg.PARAMS["prcp_auto_scaling_factor"] = False
示例#10
0
def test_N(gdir):
    # tests whether modelled mb_daily massbalances of different values of N
    # is similar to observed mass balances

    # this could be optimised and included in the above tests
    climate = 'ERA5dr'
    mb_type = 'mb_daily'
    cfg.PARAMS['baseline_climate'] = 'ERA5dr'
    oggm.shop.ecmwf.process_ecmwf_data(gdir, dataset="ERA5dr")
    for grad_type in ['cte', 'var_an_cycle']:
        if grad_type == 'var_an_cycle':
            fail_err_4 = (mb_type == 'mb_monthly') and (climate == 'CRU')
            mu_star_opt = mu_star_opt_var
        else:
            fail_err_4 = False
            mu_star_opt = mu_star_opt_cte

        if fail_err_4:
            with pytest.raises(InvalidParamsError):
                mb_mod = mb_modules(gdir,
                                    mu_star_opt[mb_type],
                                    mb_type=mb_type,
                                    prcp_fac=pf,
                                    t_solid=0,
                                    t_liq=2,
                                    t_melt=0,
                                    default_grad=-0.0065,
                                    bias=0,
                                    grad_type=grad_type)
        else:
            mbdf = gdir.get_ref_mb_data()
            hgts, widths = gdir.get_inversion_flowline_hw()

            tot_mb_N = {}
            for N in [10000, 5000, 1000, 500, 100]:
                mb_mod = mb_modules(gdir,
                                    mu_star_opt[mb_type],
                                    mb_type=mb_type,
                                    prcp_fac=pf,
                                    N=N,
                                    t_solid=0,
                                    t_liq=2,
                                    t_melt=0,
                                    default_grad=-0.0065,
                                    bias=0,
                                    grad_type=grad_type)

                tot_mb_N[N] = mb_mod.get_specific_mb(heights=hgts,
                                                     widths=widths,
                                                     year=mbdf.index.values)

                assert np.abs(utils.md(tot_mb_N[N],
                                       mbdf['ANNUAL_BALANCE'])) < 10


# %%
# something like that could also be included later on
# bbut only if I somehow get the data from the climate files ....
# or can I use these things with the ELA... without the climate files...
# in the moment oggm.core.climate works only with default OGGM mass balance

# def test_mb_modules(self, hef_gdir):

#     rho = cfg.PARAMS['ice_density']

#     F = SEC_IN_YEAR * rho

#     gdir = hef_gdir
#     init_present_time_glacier(gdir)

#     df = gdir.read_json('local_mustar')
#     mu_star = df['mu_star_glacierwide']
#     bias = df['bias']

#     # Climate period
#     yrp = [1851, 2000]

#     # Flowlines height
#     h, w = gdir.get_inversion_flowline_hw()

#     mb_mod = massbalance.PastMassBalance(gdir, bias=0)
#     for i, yr in enumerate(np.arange(yrp[0], yrp[1]+1)):
#         ref_mb_on_h = p[:, i] - mu_star * t[:, i]
#         my_mb_on_h = mb_mod.get_annual_mb(h, yr) * F
#         np.testing.assert_allclose(ref_mb_on_h, my_mb_on_h,
#                                    atol=1e-2)
#         ela_z = mb_mod.get_ela(year=yr)
#         totest = mb_mod.get_annual_mb([ela_z], year=yr) * F
#         assert_allclose(totest[0], 0, atol=1)

#     mb_mod = massbalance.PastMassBalance(gdir)
#     for i, yr in enumerate(np.arange(yrp[0], yrp[1]+1)):
#         ref_mb_on_h = p[:, i] - mu_star * t[:, i]
#         my_mb_on_h = mb_mod.get_annual_mb(h, yr) * F
#         np.testing.assert_allclose(ref_mb_on_h, my_mb_on_h + bias,
#                                    atol=1e-2)
#         ela_z = mb_mod.get_ela(year=yr)
#         totest = mb_mod.get_annual_mb([ela_z], year=yr) * F
#         assert_allclose(totest[0], 0, atol=1)

#     for i, yr in enumerate(np.arange(yrp[0], yrp[1]+1)):

#         ref_mb_on_h = p[:, i] - mu_star * t[:, i]
#         my_mb_on_h = ref_mb_on_h*0.
#         for m in np.arange(12):
#             yrm = utils.date_to_floatyear(yr, m + 1)
#             tmp = mb_mod.get_monthly_mb(h, yrm) * SEC_IN_MONTH * rho
#             my_mb_on_h += tmp

#         np.testing.assert_allclose(ref_mb_on_h,
#                                    my_mb_on_h + bias,
#                                    atol=1e-2)

#     # real data
#     h, w = gdir.get_inversion_flowline_hw()
#     mbdf = gdir.get_ref_mb_data()
#     mbdf.loc[yr, 'MY_MB'] = np.NaN
#     mb_mod = massbalance.PastMassBalance(gdir)
#     for yr in mbdf.index.values:
#         my_mb_on_h = mb_mod.get_annual_mb(h, yr) * SEC_IN_YEAR * rho
#         mbdf.loc[yr, 'MY_MB'] = np.average(my_mb_on_h, weights=w)

#     np.testing.assert_allclose(mbdf['ANNUAL_BALANCE'].mean(),
#                                mbdf['MY_MB'].mean(),
#                                atol=1e-2)
#     mbdf['MY_ELA'] = mb_mod.get_ela(year=mbdf.index.values)
#     assert mbdf[['MY_ELA', 'MY_MB']].corr().values[0, 1] < -0.9
#     assert mbdf[['MY_ELA', 'ANNUAL_BALANCE']].corr().values[0, 1] < -0.7

#     mb_mod = massbalance.PastMassBalance(gdir, bias=0)
#     for yr in mbdf.index.values:
#         my_mb_on_h = mb_mod.get_annual_mb(h, yr) * SEC_IN_YEAR * rho
#         mbdf.loc[yr, 'MY_MB'] = np.average(my_mb_on_h, weights=w)

#     np.testing.assert_allclose(mbdf['ANNUAL_BALANCE'].mean() + bias,
#                                mbdf['MY_MB'].mean(),
#                                atol=1e-2)

#     mb_mod = massbalance.PastMassBalance(gdir)
#     for yr in mbdf.index.values:
#         my_mb_on_h = mb_mod.get_annual_mb(h, yr) * SEC_IN_YEAR * rho
#         mbdf.loc[yr, 'MY_MB'] = np.average(my_mb_on_h, weights=w)
#         mb_mod.temp_bias = 1
#         my_mb_on_h = mb_mod.get_annual_mb(h, yr) * SEC_IN_YEAR * rho
#         mbdf.loc[yr, 'BIASED_MB'] = np.average(my_mb_on_h, weights=w)
#         mb_mod.temp_bias = 0

#     np.testing.assert_allclose(mbdf['ANNUAL_BALANCE'].mean(),
#                                mbdf['MY_MB'].mean(),
#                                atol=1e-2)
#     assert mbdf.ANNUAL_BALANCE.mean() > mbdf.BIASED_MB.mean()

#     # Repeat
#     mb_mod = massbalance.PastMassBalance(gdir, repeat=True,
#                                          ys=1901, ye=1950)
#     yrs = np.arange(100) + 1901
#     mb = mb_mod.get_specific_mb(h, w, year=yrs)
#     assert_allclose(mb[50], mb[-50])
示例#11
0
def test_present_time_glacier_massbalance(gdir):

    # check if area of  HUSS flowlines corresponds to the rgi area
    h, w = gdir.get_inversion_flowline_hw()
    fls = gdir.read_pickle('inversion_flowlines')
    assert_allclose(gdir.rgi_area_m2, np.sum(w * gdir.grid.dx * fls[0].dx))

    # do this for all model types
    # ONLY TEST it for ERA5dr or ERA5_daily!!!
    for climate in ['ERA5dr', 'ERA5_daily']:
        for mb_type in ['mb_monthly', 'mb_daily', 'mb_real_daily']:
            for grad_type in ['cte', 'var_an_cycle']:
                if grad_type == 'var_an_cycle':
                    fail_err_4 = ((mb_type == 'mb_monthly')
                                  and (climate == 'CRU'))
                    mu_star_opt = mu_star_opt_var
                else:
                    fail_err_4 = False
                    mu_star_opt = mu_star_opt_cte
                if climate == 'ERA5dr':
                    cfg.PARAMS['baseline_climate'] = 'ERA5dr'
                    oggm.shop.ecmwf.process_ecmwf_data(gdir, dataset="ERA5dr")
                elif climate == 'ERA5_daily':
                    cfg.PARAMS['baseline_climate'] = 'ERA5_daily'
                    process_era5_daily_data(gdir)
                else:
                    tasks.process_climate_data(gdir)
                    pass
                fail_err_1 = (mb_type == 'mb_daily') and (climate != 'ERA5dr')
                fail_err_2 = ((mb_type == 'mb_monthly')
                              and (climate == 'ERA5_daily'))
                fail_err_3 = ((mb_type == 'mb_real_daily')
                              and (climate != 'ERA5_daily'))

                if fail_err_1 or fail_err_2 or fail_err_3 or fail_err_4:
                    with pytest.raises(InvalidParamsError):
                        mb_mod = mb_modules(gdir,
                                            mu_star_opt[mb_type],
                                            mb_type=mb_type,
                                            prcp_fac=pf,
                                            t_solid=0,
                                            t_liq=2,
                                            t_melt=0,
                                            default_grad=-0.0065,
                                            bias=0,
                                            grad_type=grad_type)
                else:
                    # this is just a test for reproducibility!
                    mb_mod = mb_modules(gdir,
                                        mu_star_opt[mb_type],
                                        mb_type=mb_type,
                                        prcp_fac=pf,
                                        t_solid=0,
                                        t_liq=2,
                                        t_melt=0,
                                        default_grad=-0.0065,
                                        bias=0,
                                        grad_type=grad_type)

                    mbdf = gdir.get_ref_mb_data()
                    hgts, widths = gdir.get_inversion_flowline_hw()

                    tot_mb = []
                    refmb = []
                    grads = hgts * 0
                    for yr, mb in mbdf.iterrows():
                        refmb.append(mb['ANNUAL_BALANCE'])
                        mbh = (mb_mod.get_annual_mb(hgts, yr) * SEC_IN_YEAR *
                               cfg.PARAMS['ice_density'])
                        grads += mbh
                        tot_mb.append(np.average(mbh, weights=widths))
                    grads /= len(tot_mb)

                    # check if calibrated total mass balance similar
                    # to observe mass balance time series
                    assert np.abs(utils.md(tot_mb, refmb)) < 50