Esempio n. 1
0
def get_first_guess_surface_h(data_logger):
    """TODO: Function to conduct spinup for first guess surface_h"""
    fl = data_logger.flowline_init

    if 'surface_h' in list(data_logger.spinup_options.keys()):
        mb_options = data_logger.spinup_options['surface_h']['mb_model']
        if mb_options['type'] == 'constant':
            yr_start_run = mb_options['years'][0]
            yr_end_run = mb_options['years'][1]
            halfsize = (yr_end_run - yr_start_run) / 2
            mb_spinup = MultipleFlowlineMassBalance(
                data_logger.gdir,
                fls=[fl],
                mb_model_class=ConstantMassBalance,
                filename='climate_historical',
                input_filesuffix='',
                y0=yr_start_run + halfsize,
                halfsize=halfsize)
            mb_spinup.temp_bias = mb_options['t_bias']

            model = FluxBasedModel(copy.deepcopy([fl]),
                                   mb_spinup,
                                   y0=yr_start_run)
            model.run_until(yr_end_run)

            return model.fls[0].surface_h
        else:
            raise NotImplementedError(
                f'mb type {mb_options["type"]} not implemented!')

    else:
        raise NotImplementedError(
            'The provided spinup option is not implemented!')
def create_spinup_glacier(gdir, rgi_id_to_name, yr_start_run, yr_end_run,
                          yr_spinup, used_mb_models):
    """TODO
    """
    # now creat spinup glacier with consensus flowline starting from ice free,
    # try to match length at rgi_date for 'realistic' experiment setting
    fl_consensus = gdir.read_pickle('model_flowlines',
                                    filesuffix='_consensus')[0]
    length_m_ref = fl_consensus.length_m

    fls_spinup = copy.deepcopy([fl_consensus])
    fls_spinup[0].thick = np.zeros(len(fls_spinup[0].thick))
    halfsize = (yr_start_run - yr_spinup) / 2
    yr_rgi = gdir.rgi_date

    mb_spinup = MultipleFlowlineMassBalance(gdir,
                                            fls=fls_spinup,
                                            mb_model_class=ConstantMassBalance,
                                            filename='climate_historical',
                                            input_filesuffix='',
                                            y0=yr_spinup + halfsize,
                                            halfsize=halfsize)

    mb_historical = MultipleFlowlineMassBalance(gdir,
                                                fls=fls_spinup,
                                                mb_model_class=PastMassBalance,
                                                filename='climate_historical',
                                                input_filesuffix='')

    def spinup_run(t_bias):
        # with t_bias the glacier state after spinup is changed between iterations
        mb_spinup.temp_bias = t_bias
        # run the spinup
        model_spinup = FluxBasedModel(copy.deepcopy(fls_spinup),
                                      mb_spinup,
                                      y0=0)
        model_spinup.run_until_equilibrium(max_ite=1000)

        # Now conduct the actual model run to the rgi date
        model_historical = FluxBasedModel(model_spinup.fls,
                                          mb_historical,
                                          y0=yr_spinup)
        model_historical.run_until(yr_rgi)

        cost = (model_historical.length_m - length_m_ref) / length_m_ref * 100

        return cost

    glacier_name = rgi_id_to_name[gdir.rgi_id]
    if experiment_glaciers[glacier_name]['t_bias_spinup_limits'] is None:
        print(
            'returned spinup_run function for searching for the t_bias_limits!'
        )
        return gdir, spinup_run
    else:
        t_bias_spinup_limits = \
            experiment_glaciers[glacier_name]['t_bias_spinup_limits']

    if experiment_glaciers[glacier_name]['t_bias_spinup'] is None:
        # search for it by giving back
        t_bias_spinup = brentq(spinup_run,
                               t_bias_spinup_limits[0],
                               t_bias_spinup_limits[1],
                               maxiter=20,
                               disp=False)
    else:
        t_bias_spinup = experiment_glaciers[glacier_name]['t_bias_spinup']

    print(
        f'{gdir.name} spinup tbias: {t_bias_spinup} with L mismatch at rgi_date:'
        f' {spinup_run(t_bias_spinup)} m')

    mb_spinup.temp_bias = t_bias_spinup
    model_spinup = FluxBasedModel(copy.deepcopy(fls_spinup), mb_spinup, y0=0)
    model_spinup.run_until_equilibrium(max_ite=1000)

    gdir.write_pickle(model_spinup.fls,
                      'model_flowlines',
                      filesuffix='_spinup')