Exemple #1
0
def up_to_inversion(reset=False):
    """Run the tasks you want."""

    gdirs = up_to_climate(reset=reset)

    with open(CLI_LOGF, 'rb') as f:
        clilog = pickle.load(f)

    if clilog != 'histalp':
        reset = True
    else:
        try:
            tasks.prepare_for_inversion(gdirs[0])
        except Exception:
            reset = True

    if reset:
        # Use histalp for the actual inversion test
        cfg.PARAMS['temp_use_local_gradient'] = True
        cfg.PARAMS['baseline_climate'] = 'HISTALP'
        cru_dir = get_demo_file('HISTALP_precipitation_all_abs_1801-2014.nc')
        cfg.PATHS['cru_dir'] = os.path.dirname(cru_dir)
        workflow.climate_tasks(gdirs)
        with open(CLI_LOGF, 'wb') as f:
            pickle.dump('histalp', f)

        # Inversion
        workflow.inversion_tasks(gdirs)

    return gdirs
Exemple #2
0
def up_to_inversion(reset=False):
    """Run the tasks you want."""

    gdirs = up_to_climate(reset=reset)

    with open(CLI_LOGF, 'rb') as f:
        clilog = pickle.load(f)

    if clilog != 'histalp':
        reset = True
    else:
        try:
            tasks.prepare_for_inversion(gdirs[0])
        except Exception:
            reset = True

    if reset:
        # Use histalp for the actual inversion test
        cfg.PARAMS['temp_use_local_gradient'] = True
        cfg.PARAMS['baseline_climate'] = 'HISTALP'
        workflow.climate_tasks(gdirs)
        with open(CLI_LOGF, 'wb') as f:
            pickle.dump('histalp', f)

        # Inversion
        workflow.inversion_tasks(gdirs)

    return gdirs
    def setup_cache(self):

        utils.mkdir(self.testdir, reset=True)
        self.cfg_init()

        hef_file = get_demo_file('Hintereisferner_RGI5.shp')
        entity = gpd.read_file(hef_file).iloc[0]

        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)

        tasks.define_glacier_region(gdir, entity=entity)
        tasks.glacier_masks(gdir)
        tasks.compute_centerlines(gdir)
        tasks.initialize_flowlines(gdir)
        tasks.compute_downstream_line(gdir)
        tasks.compute_downstream_bedshape(gdir)
        tasks.catchment_area(gdir)
        tasks.catchment_intersections(gdir)
        tasks.catchment_width_geom(gdir)
        tasks.catchment_width_correction(gdir)
        tasks.process_custom_climate_data(gdir)
        tasks.mu_candidates(gdir)
        mbdf = gdir.get_ref_mb_data()['ANNUAL_BALANCE']
        res = climate.t_star_from_refmb(gdir, mbdf)
        tasks.local_mustar(gdir, tstar=res['t_star'], bias=res['bias'])
        tasks.apparent_mb(gdir)

        tasks.prepare_for_inversion(gdir)
        tasks.mass_conservation_inversion(gdir)

        return gdir
Exemple #4
0
def up_to_inversion(reset=False):
    """Run the tasks you want."""

    gdirs = up_to_climate(reset=reset)

    with open(CLI_LOGF, 'rb') as f:
        clilog = pickle.load(f)

    if clilog != 'histalp':
        reset = True
    else:
        try:
            tasks.prepare_for_inversion(gdirs[0])
        except Exception:
            reset = True

    if reset:
        # Use histalp for the actual inversion test
        cfg.PARAMS['temp_use_local_gradient'] = True
        cfg.PARAMS['baseline_climate'] = 'HISTALP'
        cru_dir = get_demo_file('HISTALP_precipitation_all_abs_1801-2014.nc')
        cfg.PATHS['cru_dir'] = os.path.dirname(cru_dir)
        workflow.climate_tasks(gdirs)
        with open(CLI_LOGF, 'wb') as f:
            pickle.dump('histalp', f)

        # Inversion
        workflow.inversion_tasks(gdirs)

    return gdirs
Exemple #5
0
    def test_run(self):

        entity = gpd.read_file(self.rgi_file).iloc[0]

        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
        gis.define_glacier_region(gdir, entity=entity)
        gis.glacier_masks(gdir)
        centerlines.compute_centerlines(gdir)
        centerlines.initialize_flowlines(gdir)
        centerlines.compute_downstream_line(gdir)
        centerlines.compute_downstream_bedshape(gdir)
        centerlines.catchment_area(gdir)
        centerlines.catchment_intersections(gdir)
        centerlines.catchment_width_geom(gdir)
        centerlines.catchment_width_correction(gdir)

        # Climate tasks -- only data IO and tstar interpolation!
        tasks.process_dummy_cru_file(gdir, seed=0)
        tasks.local_t_star(gdir)
        tasks.mu_star_calibration(gdir)

        # Inversion tasks
        tasks.prepare_for_inversion(gdir)
        # We use the default parameters for this run
        tasks.mass_conservation_inversion(gdir)
        tasks.filter_inversion_output(gdir)

        # Final preparation for the run
        tasks.init_present_time_glacier(gdir)

        # check that calving happens in the real context as well
        tasks.run_constant_climate(gdir, bias=0, nyears=100)
        with xr.open_dataset(gdir.get_filepath('model_diagnostics')) as ds:
            assert ds.calving_m3.max() > 10
                    w_depth = thick - t_altitude
            else:
                w_depth = thick - t_altitude

            fl = gdir.read_pickle('inversion_flowlines')[-1]
            width = fl.widths[-1] * gdir.grid.dx

            # Lets compute the theoretical calving out of it
            pre_calving = k * thick * w_depth * width / 1e9
            gdir.inversion_calving_rate = pre_calving
            print('pre_calving', pre_calving)

            # Recompute all with calving
            tasks.distribute_t_stars([gdir], minimum_mustar=0.)
            tasks.apparent_mb(gdir)
            tasks.prepare_for_inversion(gdir, add_debug_var=True)
            tasks.volume_inversion(gdir, glen_a=cfg.A, fs=cfg.FS)
            df = pd.read_csv(gdir.get_filepath('local_mustar')).iloc[0]
            mu_star = df['mu_star']

            while i < 50:
                # First calculates a calving flux from model output

                F_calving, new_depth, new_thick, t_width = calving_from_depth(
                    gdir, k)

                # Stores the data, and we see it
                data_calving += [F_calving]
                w_depth = np.append(w_depth, new_depth)
                thick = np.append(thick, new_thick)
                t_width = t_width
Exemple #7
0
tasks.define_glacier_region(gdir, entity=entity)
tasks.glacier_masks(gdir)
tasks.compute_centerlines(gdir)
tasks.initialize_flowlines(gdir)
tasks.compute_downstream_line(gdir)
tasks.compute_downstream_bedshape(gdir)
tasks.catchment_area(gdir)
tasks.catchment_intersections(gdir)
tasks.catchment_width_geom(gdir)
tasks.catchment_width_correction(gdir)
tasks.process_cru_data(gdir)
tasks.mu_candidates(gdir)
tasks.compute_ref_t_stars([gdir])
tasks.distribute_t_stars([gdir])
tasks.apparent_mb(gdir)
tasks.prepare_for_inversion(gdir)
tasks.volume_inversion(gdir, glen_a=cfg.A, fs=0)
tasks.filter_inversion_output(gdir)
tasks.init_present_time_glacier(gdir)

df = utils.glacier_characteristics([gdir], path=False)

reset = True
seed = 0

tasks.random_glacier_evolution(gdir,
                               nyears=800,
                               seed=0,
                               y0=2000,
                               filesuffix='_2000_def',
                               reset=reset)
Exemple #8
0
    def setup_cache(self):

        setattr(full_workflow.setup_cache, "timeout", 360)

        utils.mkdir(self.testdir, reset=True)
        self.cfg_init()

        entity = gpd.read_file(get_demo_file('01_rgi60_Columbia.shp')).iloc[0]
        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)

        tasks.define_glacier_region(gdir, entity=entity)
        tasks.glacier_masks(gdir)
        tasks.compute_centerlines(gdir)
        tasks.initialize_flowlines(gdir)
        tasks.compute_downstream_line(gdir)
        tasks.compute_downstream_bedshape(gdir)
        tasks.catchment_area(gdir)
        tasks.catchment_intersections(gdir)
        tasks.catchment_width_geom(gdir)
        tasks.catchment_width_correction(gdir)
        climate.process_dummy_cru_file(gdir, seed=0)

        rho = cfg.PARAMS['ice_density']
        i = 0
        calving_flux = []
        mu_star = []
        ite = []
        cfg.PARAMS['clip_mu_star'] = False
        cfg.PARAMS['min_mu_star'] = 0  # default is now 1
        while i < 12:

            # Calculates a calving flux from model output
            if i == 0:
                # First call we set to zero (not very necessary,
                # this first loop could be removed)
                f_calving = 0
            elif i == 1:
                # Second call we set a very small positive calving
                f_calving = utils.calving_flux_from_depth(gdir, water_depth=1)
            elif cfg.PARAMS['clip_mu_star']:
                # If we have to clip mu the calving becomes the real flux
                fl = gdir.read_pickle('inversion_flowlines')[-1]
                f_calving = fl.flux[-1] * (gdir.grid.dx**2) * 1e-9 / rho
            else:
                # Otherwise it is parameterized
                f_calving = utils.calving_flux_from_depth(gdir)

            # Give it back to the inversion and recompute
            gdir.inversion_calving_rate = f_calving

            # At this step we might raise a MassBalanceCalibrationError
            mu_is_zero = False
            try:
                climate.local_t_star(gdir)
                df = gdir.read_json('local_mustar')
            except MassBalanceCalibrationError as e:
                assert 'mu* out of specified bounds' in str(e)
                # When this happens we clip mu* to zero and store the
                # bad value (just for plotting)
                cfg.PARAMS['clip_mu_star'] = True
                df = gdir.read_json('local_mustar')
                df['mu_star_glacierwide'] = float(str(e).split(':')[-1])
                climate.local_t_star(gdir)

            climate.mu_star_calibration(gdir)
            tasks.prepare_for_inversion(gdir, add_debug_var=True)
            v_inv, _ = tasks.mass_conservation_inversion(gdir)

            # Store the data
            calving_flux = np.append(calving_flux, f_calving)
            mu_star = np.append(mu_star, df['mu_star_glacierwide'])
            ite = np.append(ite, i)

            # Do we have to do another_loop?
            if i > 0:
                avg_one = np.mean(calving_flux[-4:])
                avg_two = np.mean(calving_flux[-5:-1])
                difference = abs(avg_two - avg_one)
                conv = (difference < 0.05 * avg_two or calving_flux[-1] == 0
                        or calving_flux[-1] == calving_flux[-2])
                if mu_is_zero or conv:
                    break
            i += 1

        assert i < 8
        assert calving_flux[-1] < np.max(calving_flux)
        assert calving_flux[-1] > 2
        assert mu_star[-1] == 0

        mbmod = massbalance.MultipleFlowlineMassBalance
        mb = mbmod(gdir,
                   use_inversion_flowlines=True,
                   mb_model_class=massbalance.ConstantMassBalance,
                   bias=0)
        flux_mb = (mb.get_specific_mb() * gdir.rgi_area_m2) * 1e-9 / rho
        np.testing.assert_allclose(flux_mb, calving_flux[-1], atol=0.001)

        return calving_flux, mu_star
Exemple #9
0
width = cl['width'][-1]
thick = cl['volume'][-1] / cl['dx'] / width
print('Without calving the width is {} m and the thick is {} m'.format(width, thick))

# Try many C's (they should make sense in comparison to the size of the glacier)
c_candidates = np.linspace(0, 0.1, 10)
for c in c_candidates:

    # First guess calving
    gdir.inversion_calving_rate = c

    # Recompute mu
    tasks.distribute_t_stars([gdir])

    # Inversion
    tasks.prepare_for_inversion(gdir)
    tasks.volume_inversion(gdir)

    # We read the output, last pixel of the inversion
    cl = gdir.read_pickle('inversion_output', div_id=1)[-1]
    width = cl['width'][-1]
    thick = cl['volume'][-1] / cl['dx'] / width
    print('With calving of {} km3 a-1 the width is {} m  and the thick is {} m'.format(gdir.inversion_calving_rate, width, thick))

    # Now compute the calving rate that would come from H and width
    # Compare to the old calving and see if it has to be reduced or increased


# And in the very end, alle these steps should be automatized with
# an optimisation function
tasks.define_glacier_region(gdir, entity=entity)
tasks.glacier_masks(gdir)
tasks.compute_centerlines(gdir)
tasks.initialize_flowlines(gdir)
tasks.catchment_area(gdir)
tasks.catchment_intersections(gdir)
tasks.catchment_width_geom(gdir)
tasks.catchment_width_correction(gdir)
tasks.process_cru_data(gdir)
tasks.mu_candidates(gdir, reset=True)
res = t_star_from_refmb(gdir, gdir.get_ref_mb_data()['ANNUAL_BALANCE'])
local_mustar(gdir, tstar=res['t_star'][-1], bias=res['bias'][-1],
             prcp_fac=res['prcp_fac'], reset=True)
apparent_mb(gdir, reset=True)

tasks.prepare_for_inversion(gdir, reset=True)


f, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharey=True)

facs = np.append((np.arange(9)+1)*0.1, (np.arange(19)+2) * 0.5)
vols1 = facs * 0.
vols2 = facs * 0.
vols3 = facs * 0.

for i, f in enumerate(facs):
    v, _ = mass_conservation_inversion(gdir, glen_a=cfg.A*f)
    vols1[i] = v * 1e-9
    v, _ = mass_conservation_inversion(gdir, glen_a=cfg.A*f,
                                       fs=cfg.FS*0.5)
    vols2[i] = v * 1e-9
Exemple #11
0
                width = cl['width'][-1]
                #print('width',width)

            else:
                # we read the McNabb width
                width = dfwidth[index[0][0]]
                #print('width',width)

            # Lets compute the theoretical calving out of it
            pre_calving = 2 * thick * w_depth * width / 1e9
            gdir.inversion_calving_rate = pre_calving
            print('pre_calving', pre_calving)

            # Recompute all with calving
            tasks.distribute_t_stars([gdir])
            tasks.prepare_for_inversion(gdir, add_debug_var=True)
            tasks.volume_inversion(gdir)

            while i < 50:
                # First calculates a calving flux from model output

                F_calving, new_depth, new_thick, t_width = calving_from_depth(gdir)

                # Stores the data, and we see it
                data_calving += [F_calving]
                w_depth += [new_depth]
                thick += [new_thick]
                t_width = t_width
                #print('Calving rate calculated', F_calving)

                # We put the calving function output into the Model
Exemple #12
0
gdir = oggm.GlacierDirectory(entity, base_dir=base_dir)
cfg.set_intersects_db(get_rgi_intersects_entities(['RGI60-11.00897']))

tasks.define_glacier_region(gdir, entity=entity)
tasks.glacier_masks(gdir)
tasks.compute_centerlines(gdir)
tasks.initialize_flowlines(gdir)
tasks.catchment_area(gdir)
tasks.catchment_intersections(gdir)
tasks.catchment_width_geom(gdir)
tasks.catchment_width_correction(gdir)
tasks.process_cru_data(gdir)
local_t_star(gdir)
mu_star_calibration(gdir)

tasks.prepare_for_inversion(gdir)

facs = np.append((np.arange(9) + 1) * 0.1, (np.arange(19) + 2) * 0.5)
vols1 = facs * 0.
vols2 = facs * 0.
vols3 = facs * 0.
vols4 = facs * 0.
vols5 = facs * 0.
vols6 = facs * 0.
vols7 = facs * 0.
vols8 = facs * 0.

glen_a = cfg.PARAMS['glen_a']
fs = 5.7e-20
for i, f in enumerate(facs):
    v, _ = mass_conservation_inversion(gdir, glen_a=glen_a * f)