コード例 #1
0
def run_constant_climate_with_bias(gdir,
                                   temp_bias_ts=None,
                                   prcp_fac_ts=None,
                                   ys=None,
                                   ye=None,
                                   y0=2014,
                                   halfsize=5,
                                   climate_filename='climate_historical',
                                   climate_input_filesuffix='',
                                   output_filesuffix='',
                                   init_model_fls=None,
                                   init_model_filesuffix=None,
                                   init_model_yr=None,
                                   bias=None,
                                   **kwargs):
    """Runs a glacier with temperature and precipitation correction timeseries.

    Parameters
    ----------
    gdir : :py:class:`oggm.GlacierDirectory`
        the glacier directory to process
    temp_bias_ts : pandas DataFrame
        the temperature bias timeseries (in °C) (index: time as years)
    prcp_fac_ts : pandas DataFrame
        the precipitaion bias timeseries (in % change, positive or negative)
        (index: time as years)
    y0 : int
        central year of the constant climate period
    halfsize : int
        half-size of the constant climate period
    climate_filename : str
        name of the climate file, e.g. 'climate_historical' (default) or 'gcm_data'
    climate_input_filesuffix: str
        filesuffix for the input climate file
    output_filesuffix : str
        for the output file
    init_model_filesuffix : str
        if you want to start from a previous model run state. Can be
        combined with `init_model_yr`
    init_model_yr : int
        the year of the initial run you want to start from. The default
        is to take the last year available
    bias : float
        bias of the mb model. Default is to use the calibrated one, which
        is often a better idea. For t* experiments it can be useful to set it
        to zero
    kwargs : dict
        kwargs to pass to the FluxBasedModel instance
    """

    if init_model_filesuffix is not None:
        fp = gdir.get_filepath('model_geometry',
                               filesuffix=init_model_filesuffix)
        fmod = FileModel(fp)
        if init_model_yr is None:
            init_model_yr = fmod.last_yr
        # Avoid issues here
        if init_model_yr > fmod.y0:
            fmod.run_until(init_model_yr)
        else:
            fmod.run_until(fmod.y0)

        init_model_fls = fmod.fls

    # Final crop
    mb = BiasedConstantMassBalance(gdir,
                                   temp_bias_ts=temp_bias_ts,
                                   prcp_fac_ts=prcp_fac_ts,
                                   y0=y0,
                                   bias=bias,
                                   halfsize=halfsize,
                                   filename=climate_filename,
                                   input_filesuffix=climate_input_filesuffix)

    # Decide from climate
    if ye is None:
        ye = mb.ye
    if ys is None:
        ys = mb.ys

    return flowline_model_run(gdir,
                              output_filesuffix=output_filesuffix,
                              mb_model=mb,
                              ys=ys,
                              ye=ye,
                              init_model_fls=init_model_fls,
                              **kwargs)
コード例 #2
0
ファイル: tasks.py プロジェクト: pat-schmitt/oggm
def run_uncertain_random_climate(gdir,
                                 nyears=700,
                                 output_filesuffix='',
                                 sigma_t=None,
                                 sigma_p=None,
                                 sigma_smb=None,
                                 rdn_temp_bias_seed=None,
                                 rdn_prcp_fac_seed=None,
                                 rdn_bias_seed=None,
                                 orig_mb_tbias=0,
                                 orig_mb_pbias=1,
                                 orig_mb_sbias=0,
                                 **kwargs):
    """Test stuff

    Parameters
    ----------
    gdir
    nyears
    output_filesuffix
    rdn_temp_bias_seed
    rdn_prcp_bias_seed
    rdn_bias_seed
    kwargs

    Returns
    -------
    """

    # Find the optimal bias for a balanced MB
    mbref = mbmods.PastMassBalance(gdir)
    h, w = gdir.get_inversion_flowline_hw()
    cyrs = np.unique(mbref.years)

    def to_minimize(x):
        mbref.bias = x
        return np.mean(mbref.get_specific_mb(h, w, cyrs))

    opti_bias = optimization.brentq(to_minimize, -10000, 10000, xtol=0.01)
    mbref.bias = opti_bias
    assert np.allclose(np.mean(mbref.get_specific_mb(h, w, cyrs)),
                       0,
                       atol=0.01)

    rdf = pd.DataFrame(index=cyrs)
    for y in cyrs:
        rdf.loc[y, 'SMB'] = mbref.get_specific_mb(h, w, year=y)
        t, _, p, _ = mbref.get_annual_climate([np.mean(h)], year=y)
        rdf.loc[y, 'TEMP'] = t
        rdf.loc[y, 'PRCP'] = p

    if sigma_smb is None:
        sigma_smb = rdf.std()['SMB'] / 2
    if sigma_t is None:
        sigma_t = rdf.std()['TEMP'] / 2
    if sigma_p is None:
        sigma_p = (rdf.std() / rdf.mean())['PRCP'] / 2

    mb = mbmods.RandomMassBalance(gdir, all_years=True, seed=0)
    mb.mbmod.temp_bias = orig_mb_tbias
    mb.mbmod.prcp_bias = orig_mb_pbias
    mb.mbmod.bias = orig_mb_sbias + opti_bias

    rmb = mbmods.UncertainMassBalance(mb,
                                      rdn_temp_bias_seed=rdn_temp_bias_seed,
                                      rdn_temp_bias_sigma=sigma_t,
                                      rdn_prcp_fac_seed=rdn_prcp_fac_seed,
                                      rdn_prcp_fac_sigma=sigma_p,
                                      rdn_bias_seed=rdn_bias_seed,
                                      rdn_bias_sigma=sigma_smb)

    return flowline_model_run(gdir,
                              output_filesuffix=output_filesuffix,
                              mb_model=rmb,
                              ys=0,
                              ye=nyears,
                              **kwargs)
コード例 #3
0
ファイル: massbalance.py プロジェクト: fmaussion/overshoots
def run_from_magicc_data(gdir,
                         magicc_ts=None,
                         ys=None,
                         ye=None,
                         y0=2014,
                         halfsize=5,
                         use_dt_per_dt=True,
                         use_dp_per_dt=True,
                         climate_filename='climate_historical',
                         climate_input_filesuffix='',
                         output_filesuffix='',
                         init_model_filesuffix=None,
                         init_model_yr=None,
                         bias=None,
                         **kwargs):
    """Runs a glacier with climate input from MAGICC.

    Parameters
    ----------
    gdir : :py:class:`oggm.GlacierDirectory`
        the glacier directory to process
    gdir : :py:class:`oggm.GlacierDirectory`
        the glacier directory to process
    ys : int
        start year of the model run (default: from the glacier geometry
        date if init_model_filesuffix is None, else init_model_yr)
    ye : int
        end year of the model run (default: last year of the magicc file)
    y0 : int
        central year where to apply the MAGICC anomaly method
    halfsize : int
        half-size of the MAGICC anomaly window
    climate_filename : str
        name of the climate file, e.g. 'climate_historical' (default) or
        'gcm_data'
    climate_input_filesuffix: str
        filesuffix for the input climate file
    output_filesuffix : str
        for the output file
    init_model_filesuffix : str
        if you want to start from a previous model run state. Can be
        combined with `init_model_yr`
    init_model_yr : int
        the year of the initial run you want to start from. The default
        is to take the y0 - halfsize
    bias : float
        bias of the mb model. Default is to use the calibrated one, which
        is often a better idea. For t* experiments it can be useful to set it
        to zero
    kwargs : dict
        kwargs to pass to the FluxBasedModel instance
    """

    if init_model_yr is None:
        init_model_yr = y0 - halfsize

    if init_model_filesuffix is not None:
        fp = gdir.get_filepath('model_geometry',
                               filesuffix=init_model_filesuffix)
        fmod = FileModel(fp)
        if init_model_yr is None:
            init_model_yr = fmod.last_yr
        # Avoid issues here
        if init_model_yr > fmod.y0:
            fmod.run_until(init_model_yr)
        else:
            fmod.run_until(fmod.y0)

        init_model_fls = fmod.fls
        if ys is None:
            ys = init_model_yr
    else:
        raise InvalidParamsError('We strongly recommend to start from '
                                 'prepro for this task.')

    # Take from rgi date if not set yet
    if ys is None:
        raise InvalidParamsError('ys should not be guessed at this point')

    dt_per_dt = 1.
    dp_per_dt = 0.
    if use_dt_per_dt:
        dt_per_dt = np.asarray(gdir.get_diagnostics()['magicc_dt_per_dt'])
    if use_dp_per_dt:
        dp_per_dt = np.asarray(gdir.get_diagnostics()['magicc_dp_per_dt'])

    # Final crop
    mb = MagiccMassBalance(gdir,
                           magicc_ts=magicc_ts,
                           y0=y0,
                           halfsize=halfsize,
                           dt_per_dt=dt_per_dt,
                           dp_per_dt=dp_per_dt,
                           filename=climate_filename,
                           bias=bias,
                           input_filesuffix=climate_input_filesuffix)

    if ye is None:
        # Decide from climate
        ye = mb.ye

    return flowline_model_run(gdir,
                              output_filesuffix=output_filesuffix,
                              mb_model=mb,
                              ys=ys,
                              ye=ye,
                              init_model_fls=init_model_fls,
                              **kwargs)