def _compair_pet():
    """just to compaire the pet and peyman results, the are slightly differnt,
    but I think that is due to different methods of calculating PET,"""
    from supporting_functions.plotting import plot_multiple_results
    from basgra_python import run_basgra_nz
    verbose = False
    params, matrix_weather, days_harvest, doy_irr = establish_peyman_input(
        False)
    peyman_out = run_basgra_nz(params,
                               matrix_weather,
                               days_harvest,
                               doy_irr,
                               verbose=verbose,
                               dll_path='default',
                               supply_pet=False)

    params, matrix_weather, days_harvest, doy_irr = establish_peyman_input(
        True)
    pet_out = run_basgra_nz(params,
                            matrix_weather,
                            days_harvest,
                            doy_irr,
                            verbose=verbose,
                            dll_path='default',
                            supply_pet=True)

    from supporting_functions.plotting import plot_multiple_results
    plot_multiple_results({'pet': pet_out, 'peyman': peyman_out})
    return params, matrix_weather, days_harvest, doy_irr
Example #2
0
def run_frequent_harvest(freq, trig, targ):
    params, matrix_weather, days_harvest, doy_irr = establish_org_input(
        'lincoln')

    strs = [
        '{}-{:03d}'.format(e, f)
        for e, f in matrix_weather[['year', 'doy']].itertuples(False, None)
    ]
    days_harvest = pd.DataFrame({
        'year': matrix_weather.loc[:, 'year'],
        'doy': matrix_weather.loc[:, 'doy'],
        'frac_harv': np.ones(len(matrix_weather)),  # set filler values
        'harv_trig':
        np.zeros(len(matrix_weather)) - 1,  # set flag to not harvest
        'harv_targ': np.zeros(len(matrix_weather)),  # set filler values
        'weed_dm_frac': np.zeros(len(matrix_weather)),  # set filler values
    })

    # start harvesting at the same point
    harv_days = pd.date_range(start='2011-09-03',
                              end='2017-04-30',
                              freq='{}D'.format(freq))
    idx = np.in1d(pd.to_datetime(strs, format='%Y-%j'), harv_days)
    days_harvest.loc[idx, 'harv_trig'] = trig
    days_harvest.loc[idx, 'harv_targ'] = targ

    out = run_basgra_nz(params,
                        matrix_weather,
                        days_harvest,
                        doy_irr,
                        verbose=False)
    return out
Example #3
0
def run_nonirr_lincoln_low_basil(IBASAL):
    params, matrix_weather, days_harvest, doy_irr = establish_org_input(
        'lincoln')

    matrix_weather = get_lincoln_broadfield()
    matrix_weather.loc[:, 'max_irr'] = 10
    matrix_weather.loc[:, 'irr_trig'] = 0
    matrix_weather.loc[:, 'irr_targ'] = 1

    matrix_weather = matrix_weather.loc[:, matrix_weather_keys_pet]

    params['IRRIGF'] = 0  # no irrigation
    params['BASALI'] = IBASAL  # start at 20% basal

    days_harvest = _clean_harvest(days_harvest, matrix_weather)

    out = run_basgra_nz(params,
                        matrix_weather,
                        days_harvest,
                        doy_irr,
                        verbose=False)
    out.loc[:, 'per_fc'] = out.loc[:, 'WAL'] / out.loc[:, 'WAFC']
    out.loc[:, 'per_paw'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']

    return out
def run_inital_basgra(basali, weed_dm_frac, harv_targ, harv_trig, freq):
    """
    run an intial test
    :param basali:
    :param weed_dm_frac:
    :return:
    """
    params, matrix_weather, days_harvest, doy_irr = get_input_data(
        basali,
        weed_dm_frac,
        harv_targ=harv_targ,
        harv_trig=harv_trig,
        freq=freq)

    temp = run_basgra_nz(params,
                         matrix_weather,
                         days_harvest,
                         doy_irr,
                         verbose=False)
    out = {'temp': temp}
    temp.to_csv(r"C:\Users\Matt Hanson\Downloads\test_get_time.csv")
    plot_multiple_results(out,
                          out_vars=[
                              'DM', 'YIELD', 'DMH_RYE', 'DM_RYE_RM', 'IRRIG',
                              'PAW', 'DMH', 'BASAL'
                          ])
def run_mod_past_basgra_dryland(return_inputs=False, site='oxford', reseed=True, pg_mode='from_dmh', fun='mean',
                            reseed_trig=0.06, reseed_basal=0.1, basali=0.2, weed_dm_frac=0.05,
                            use_defined_params_except_weed_dm_frac=True):
    if not isinstance(weed_dm_frac, dict) and weed_dm_frac is not None:
        weed_dm_frac = {e: weed_dm_frac for e in range(1, 13)}
    mode = 'dryland'
    print('running: {}, {}, reseed: {}'.format(mode, site, reseed))
    weather = get_vcsn_record(site=site)
    rest = None
    params, doy_irr = get_params_doy_irr(mode)
    matrix_weather = create_matrix_weather(mode, weather, rest, fix_leap=False)
    days_harvest = create_days_harvest(mode, matrix_weather, site, fix_leap=False)
    if not reseed:
        days_harvest.loc[:, 'reseed_trig'] = -1
    else:
        if not use_defined_params_except_weed_dm_frac:
            days_harvest.loc[days_harvest.reseed_trig > 0, 'reseed_trig'] = reseed_trig
            days_harvest.loc[days_harvest.reseed_trig > 0, 'reseed_basal'] = reseed_basal
            params['BASALI'] = basali
    if weed_dm_frac is not None:
        for m in range(1, 13):
            days_harvest.loc[days_harvest.index.month == m, 'weed_dm_frac'] = weed_dm_frac[m]
    out = run_basgra_nz(params, matrix_weather, days_harvest, doy_irr, verbose=False)
    out.loc[:, 'per_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']
    pg = pd.DataFrame(calc_pasture_growth(out, days_harvest, mode=pg_mode, resamp_fun=fun, freq='1d'))
    out.loc[:, 'pg'] = pg.loc[:, 'pg']
    out = calc_pasture_growth_anomaly(out, fun=fun)
    if return_inputs:
        return out, (params, doy_irr, matrix_weather, days_harvest)
    return out
Example #6
0
def run_example_basgra():
    params, matrix_weather, days_harvest, doy_irr = establish_org_input()
    days_harvest = _clean_harvest(days_harvest, matrix_weather)
    out = run_basgra_nz(params,
                        matrix_weather,
                        days_harvest,
                        doy_irr,
                        verbose=False)
Example #7
0
def run_old_basgra():
    params, matrix_weather, days_harvest, doy_irr = establish_org_input(
        'lincoln')

    days_harvest = _clean_harvest(days_harvest, matrix_weather)

    out = run_basgra_nz(params,
                        matrix_weather,
                        days_harvest,
                        doy_irr,
                        verbose=False)
    return out
Example #8
0
def _temp_basgra_run():
    start_mod, dates, params, matrix_weather, days_harvest, doy_irr = _base_restriction_data(
    )
    print(start_mod)
    temp = run_basgra_nz(params,
                         matrix_weather,
                         days_harvest,
                         doy_irr,
                         verbose=False)
    out = {'temp': temp}
    temp.to_csv(r"C:\Users\Matt Hanson\Downloads\test_get_time.csv")
    plot_multiple_results(out,
                          out_vars=[
                              'DM', 'YIELD', 'BASAL', 'DMH_RYE', 'DM_RYE_RM',
                              'IRRIG', 'PAW'
                          ])
def run_past_basgra_dryland(return_inputs=False, site='eyrewell', reseed=True, version='trended'):
    mode = 'dryland'
    print('running: {}, {}, reseed: {}'.format(mode, site, reseed))
    weather = get_vcsn_record(site=site, version=version)
    rest = None
    params, doy_irr = get_params_doy_irr(mode)
    matrix_weather = create_matrix_weather(mode, weather, rest, fix_leap=False)
    days_harvest = create_days_harvest(mode, matrix_weather, site, fix_leap=False)
    if not reseed:
        days_harvest.loc[:, 'reseed_trig'] = -1

    out = run_basgra_nz(params, matrix_weather, days_harvest, doy_irr, verbose=False)
    out.loc[:, 'per_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']
    pg = pd.DataFrame(calc_pasture_growth(out, days_harvest, mode='from_yield', resamp_fun='mean', freq='1d'))
    out.loc[:, 'pg'] = pg.loc[:, 'pg']
    out = calc_pasture_growth_anomaly(out, fun='mean')

    if return_inputs:
        return out, (params, doy_irr, matrix_weather, days_harvest)
    return out
Example #10
0
def support_for_memory_usage():
    params, matrix_weather, days_harvest, doy_irr = establish_org_input()
    start_year = matrix_weather['year'].min()
    start_day = matrix_weather.loc[matrix_weather.year == start_year,
                                   'doy'].min()

    # set up a maximum run time
    idxs = np.random.random_integers(0, len(matrix_weather) - 1, 36600)
    matrix_weather = matrix_weather.iloc[idxs]
    expected_days = pd.Series(
        pd.date_range(start=pd.to_datetime('{}-{}'.format(
            start_year, start_day),
                                           format='%Y-%j'),
                      periods=36600))
    matrix_weather.loc[:, 'year'] = expected_days.dt.year.values
    matrix_weather.loc[:, 'doy'] = expected_days.dt.dayofyear.values

    days_harvest = _clean_harvest(days_harvest, matrix_weather)
    out = run_basgra_nz(params,
                        matrix_weather,
                        days_harvest,
                        doy_irr,
                        verbose=False)
Example #11
0
def calc_past_mean(fun, pg_mode, return_norm=True, freq='month'):
    site = 'oxford'
    mode = 'dryland'
    weather = get_vcsn_record(site=site)
    rest = None
    params, doy_irr = get_params_doy_irr(mode)
    matrix_weather = create_matrix_weather(mode, weather, rest, fix_leap=False)
    days_harvest = create_days_harvest(mode,
                                       matrix_weather,
                                       site,
                                       fix_leap=False)
    out = run_basgra_nz(params,
                        matrix_weather,
                        days_harvest,
                        doy_irr,
                        verbose=False)
    out.loc[:, 'per_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']

    pg = pd.DataFrame(
        calc_pasture_growth(out,
                            days_harvest,
                            mode=pg_mode,
                            resamp_fun=fun,
                            freq=freq))

    out.loc[:, 'pg'] = pg.loc[:, 'pg']
    out.loc[:, 'month'] = pd.Series(out.index).dt.month.values

    out_norm = out.groupby('month').agg({'pg': fun})
    strs = ['2011-{:02d}-15'.format(e) for e in out_norm.index]
    out_norm.loc[:, 'date'] = pd.to_datetime(strs)
    out_norm.set_index('date', inplace=True)

    if return_norm:
        return out_norm
    else:
        return out, out_norm
Example #12
0
days_harvest.loc[:, 'reseed_trig'] = -1
days_harvest.loc[:, 'reseed_basal'] = 1
days_harvest.drop(columns=['percent_harvest'], inplace=True)

#--->	4.0:
#		days of irrigation
doy_no_irr = [0]
doy_irr = [i for i in range(1, 120)] + [i for i in range(280, 366)]
#doy_irr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

#--->	5.0:
#		run BASGRA
outputs_no_irrig = run_basgra_nz(params,
                                 matrix_weather,
                                 days_harvest,
                                 doy_no_irr,
                                 verbose=False,
                                 dll_path='default',
                                 supply_pet=True)

params_irrig = copy.deepcopy(params)
params_irrig['irr_frm_paw'] = 1.0
params_irrig['IRRIGF'] = .90
outputs_irrig = run_basgra_nz(params_irrig,
                              matrix_weather,
                              days_harvest,
                              doy_irr,
                              verbose=False,
                              dll_path='default',
                              supply_pet=True)
def create_irrigation_abandomnet_data(base_name, params, reseed_trig=-1, reseed_basal=1, site='eyrewell'):
    out = {}
    weather = get_vcsn_record(site=site)
    rest = get_restriction_record()
    p, doy_irr = get_params_doy_irr(mode)
    matrix_weather = create_matrix_weather(mode, weather, rest, fix_leap=False)
    restrict = 1 - matrix_weather.loc[:, 'max_irr'] / 5

    matrix_weather.loc[:, 'max_irr'] = 5
    days_harvest = create_days_harvest(mode, matrix_weather, site, fix_leap=False)

    # set reseed days harvest
    idx = days_harvest.doy == 152
    days_harvest.loc[idx, 'reseed_trig'] = reseed_trig
    days_harvest.loc[idx, 'reseed_basal'] = reseed_basal

    # run models

    matrix_weather_new = matrix_weather.copy(deep=True)
    matrix_weather_new.loc[restrict >= 0.9999, 'max_irr'] = 0
    temp = run_basgra_nz(params, matrix_weather_new, days_harvest, doy_irr, verbose=False)
    temp.loc[:, 'f_rest'] = 1 - matrix_weather_new.loc[:, 'max_irr'] / 5
    temp.loc[:, 'pg'] = calc_pasture_growth(temp, days_harvest, 'from_yield', '1D', resamp_fun='mean')

    out['{}_no_rest'.format(base_name)] = temp

    matrix_weather_new = matrix_weather.copy(deep=True)
    matrix_weather_new.loc[:, 'max_irr'] *= (1 - restrict)

    temp = run_basgra_nz(params, matrix_weather_new, days_harvest, doy_irr, verbose=False)
    temp.loc[:, 'f_rest'] = 2 - matrix_weather_new.loc[:, 'max_irr'] / 5
    temp.loc[:, 'pg'] = calc_pasture_growth(temp, days_harvest, 'from_yield', '1D', resamp_fun='mean')

    out['{}_partial_rest'.format(base_name)] = temp

    matrix_weather_new = matrix_weather.copy(deep=True)
    matrix_weather_new.loc[:, 'max_irr'] *= (restrict <= 0).astype(int)

    temp = run_basgra_nz(params, matrix_weather_new, days_harvest, doy_irr, verbose=False)
    temp.loc[:, 'f_rest'] = 3 - matrix_weather_new.loc[:, 'max_irr'] / 5
    temp.loc[:, 'pg'] = calc_pasture_growth(temp, days_harvest, 'from_yield', '1D', resamp_fun='mean')
    out['{}_full_rest'.format(base_name)] = temp

    out['{}_mixed_rest'.format(base_name)] = (out['{}_full_rest'.format(base_name)].transpose() *
                                              restrict.values +
                                              out['{}_no_rest'.format(base_name)].transpose() *
                                              (1 - restrict.values)).transpose()

    # paddock level restrictions
    levels = np.arange(0, 125, 25) / 100
    padock_values = {}
    temp_out = []
    for i, (ll, lu) in enumerate(zip(levels[0:-1], levels[1:])):
        matrix_weather_new = matrix_weather.copy(deep=True)

        matrix_weather_new.loc[restrict <= ll, 'max_irr'] = 5
        matrix_weather_new.loc[restrict >= lu, 'max_irr'] = 0
        idx = (restrict > ll) & (restrict < lu)
        matrix_weather_new.loc[idx, 'max_irr'] = 5 * ((restrict.loc[idx] - ll) / 0.25)

        temp = run_basgra_nz(params, matrix_weather_new, days_harvest, doy_irr, verbose=False)
        temp_out.append(temp.values)
        temp.loc[:, 'per_PAW'] = temp.loc[:, 'PAW'] / temp.loc[:, 'MXPAW']
        temp.loc[:, 'pg'] = calc_pasture_growth(temp, days_harvest, 'from_yield', '1D', resamp_fun='mean')
        temp.loc[:, 'f_rest'] = restrict
        temp.loc[:, 'RESEEDED'] += i
        padock_values['l:{} u:{}'.format(ll, lu)] = temp
    temp = np.mean(temp_out, axis=0)

    temp2 = out['{}_mixed_rest'.format(base_name)].copy(deep=True).drop(columns=['f_rest', 'pg'])
    temp2.loc[:, :] = temp
    temp2.loc[:, 'f_rest'] = restrict + 3
    temp2.loc[:, 'pg'] = calc_pasture_growth(temp2, days_harvest, 'from_yield', '1D', resamp_fun='mean')

    out['{}_paddock_rest'.format(base_name)] = temp2

    for k in out:
        out[k].loc[:, 'per_PAW'] = out[k].loc[:, 'PAW'] / out[k].loc[:, 'MXPAW']

    return out, padock_values
Example #14
0
def irrigation_restrictions(duration, restrict, rest, outdir, ttl_str):
    """
    plot up irrigatino restriction handling
    run two irritation restrictions with a rest period
    :param duration: length of restriction
    :param restrict: restiction 0-1
    :param rest: non-restriction time in between
    :param outdir: passed to plotting function
    :param ttl_str: title string passed to plotting function
    :return:
    """

    out = {}
    start_mod1, dates, params, matrix_weather, days_harvest, doy_irr = _base_restriction_data(
    )
    print(start_mod1)

    stop_mod1 = start_mod1 + pd.DateOffset(days=duration)
    start_mod2 = stop_mod1 + pd.DateOffset(days=rest)
    stop_mod2 = start_mod2 + pd.DateOffset(days=duration)
    idx = ((dates < stop_mod1) &
           (dates >= start_mod1)) | ((dates < stop_mod2) &
                                     (dates >= start_mod2))

    out['no_rest'] = run_basgra_nz(params,
                                   matrix_weather,
                                   days_harvest,
                                   doy_irr,
                                   verbose=False)

    matrix_weather_new = matrix_weather.copy(deep=True)
    matrix_weather_new.loc[idx, 'max_irr'] *= (1 - restrict)

    out['partial_rest'] = run_basgra_nz(params,
                                        matrix_weather_new,
                                        days_harvest,
                                        doy_irr,
                                        verbose=False)

    matrix_weather_new = matrix_weather.copy(deep=True)
    matrix_weather_new.loc[idx, 'max_irr'] = 0
    out['full_rest'] = run_basgra_nz(params,
                                     matrix_weather_new,
                                     days_harvest,
                                     doy_irr,
                                     verbose=False)

    out['mixed_rest'] = out['full_rest'] * restrict + out['no_rest'] * (
        1 - restrict)

    for k in out:
        out[k].loc[:,
                   'per_PAW'] = out[k].loc[:, 'PAW'] / out[k].loc[:, 'MXPAW']

    plot_multiple_results(out,
                          out_vars=[
                              'DM', 'YIELD', 'BASAL', 'DMH_RYE', 'DM_RYE_RM',
                              'IRRIG', 'per_PAW'
                          ],
                          outdir=outdir,
                          title_str=ttl_str)
Example #15
0
def compare_oxford_irr_dry(save=False):
    nsims = 100
    storyline = get_baseline_storyline()
    simlen = np.array([month_len[e] for e in storyline.month]).sum()
    params_dry, doy_irr, all_matrix_weathers, all_days_harvests_dry = _gen_input(
        storyline=storyline,
        nsims=nsims,
        mode='dryland',
        site='oxford',
        chunks=1,
        current_c=1,
        nperc=nsims,
        simlen=simlen,
        swg_dir=default_swg_dir,
        fix_leap=True)

    params_irr, doy_irr, all_matrix_weathers, all_days_harvests_irr = _gen_input(
        storyline=storyline,
        nsims=nsims,
        mode='irrigated',
        site='oxford',
        chunks=1,
        current_c=1,
        nperc=nsims,
        simlen=simlen,
        swg_dir=default_swg_dir,
        fix_leap=True)
    params_irr_eyre, doy_irr_eyre, all_matrix_weathers_eyre, all_days_harvests_irr_eyre = _gen_input(
        storyline=storyline,
        nsims=nsims,
        mode='irrigated',
        site='eyrewell',
        chunks=1,
        current_c=1,
        nperc=nsims,
        simlen=simlen,
        swg_dir=default_swg_dir,
        fix_leap=True)

    all_out_dry = np.zeros((len(out_variables), simlen, nsims)) * np.nan
    all_out_dry_monk = np.zeros((len(out_variables), simlen, nsims)) * np.nan
    all_out_irr = np.zeros((len(out_variables), simlen, nsims)) * np.nan
    all_out_irr_eyre = np.zeros((len(out_variables), simlen, nsims)) * np.nan
    for i, (matrix_weather, days_harvest_irr, days_harvest_dry,
            matrix_weather_eyre, days_harvest_irr_eyre) in enumerate(
                zip(all_matrix_weathers, all_days_harvests_irr,
                    all_days_harvests_dry, all_matrix_weathers_eyre,
                    all_days_harvests_irr_eyre)):
        if i % 10 == 0:
            print(i)
        restrict = 1 - matrix_weather.loc[:, 'max_irr'] / abs_max_irr
        out = run_basgra_nz(params_irr,
                            matrix_weather,
                            days_harvest_irr,
                            doy_irr,
                            verbose=False,
                            run_365_calendar=True)
        out.loc[:, 'PER_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']
        pg = pd.DataFrame(
            calc_pasture_growth(out,
                                days_harvest_irr,
                                mode='from_yield',
                                resamp_fun='mean',
                                freq='1d'))
        out.loc[:, 'PGR'] = pg.loc[:, 'pg']
        out.loc[:, 'F_REST'] = restrict
        all_out_irr[:, :, i] = out.loc[:, out_variables].values.transpose()

        # run eyrewell
        pg = pd.DataFrame(
            calc_pasture_growth(out,
                                days_harvest_irr,
                                mode='from_yield',
                                resamp_fun='mean',
                                freq='1d'))
        out.loc[:, 'PGR'] = pg.loc[:, 'pg']
        out.loc[:, 'F_REST'] = restrict
        all_out_irr[:, :, i] = out.loc[:, out_variables].values.transpose()

        restrict = 1 - matrix_weather.loc[:, 'max_irr'] / abs_max_irr
        out = run_basgra_nz(params_irr_eyre,
                            matrix_weather_eyre,
                            days_harvest_irr_eyre,
                            doy_irr_eyre,
                            verbose=False,
                            run_365_calendar=True)
        out.loc[:, 'PER_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']
        pg = pd.DataFrame(
            calc_pasture_growth(out,
                                days_harvest_irr_eyre,
                                mode='from_yield',
                                resamp_fun='mean',
                                freq='1d'))
        out.loc[:, 'PGR'] = pg.loc[:, 'pg']
        out.loc[:, 'F_REST'] = restrict
        all_out_irr_eyre[:, :, i] = out.loc[:,
                                            out_variables].values.transpose()

        # run dryland
        matrix_weather.loc[:, 'max_irr'] = 0
        matrix_weather.loc[:, 'irr_trig'] = 0
        matrix_weather.loc[:, 'irr_targ'] = 0
        restrict = 1 - matrix_weather.loc[:, 'max_irr'] / abs_max_irr
        out = run_basgra_nz(params_dry,
                            matrix_weather,
                            days_harvest_dry,
                            doy_irr,
                            verbose=False,
                            run_365_calendar=True)
        out.loc[:, 'PER_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']

        pg = pd.DataFrame(
            calc_pasture_growth(out,
                                days_harvest_dry,
                                mode='from_yield',
                                resamp_fun='mean',
                                freq='1d'))
        out.loc[:, 'PGR'] = pg.loc[:, 'pg']
        out.loc[:, 'F_REST'] = restrict
        all_out_dry[:, :, i] = out.loc[:, out_variables].values.transpose()

        # run dryland monk
        for m in range(1, 13):
            days_harvest_dry.loc[days_harvest_dry.index.month == m,
                                 'weed_dm_frac'] = weed_dict_2[m]
        matrix_weather.loc[:, 'max_irr'] = 0
        matrix_weather.loc[:, 'irr_trig'] = 0
        matrix_weather.loc[:, 'irr_targ'] = 0
        restrict = 1 - matrix_weather.loc[:, 'max_irr'] / abs_max_irr
        out = run_basgra_nz(params_dry,
                            matrix_weather,
                            days_harvest_dry,
                            doy_irr,
                            verbose=False,
                            run_365_calendar=True)
        out.loc[:, 'PER_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']

        pg = pd.DataFrame(
            calc_pasture_growth(out,
                                days_harvest_dry,
                                mode='from_yield',
                                resamp_fun='mean',
                                freq='1d'))
        out.loc[:, 'PGR'] = pg.loc[:, 'pg']
        out.loc[:, 'F_REST'] = restrict
        all_out_dry_monk[:, :, i] = out.loc[:,
                                            out_variables].values.transpose()

    all_out_dry = pd.DataFrame(np.nanmean(all_out_dry, axis=2).transpose(),
                               columns=out_variables,
                               index=matrix_weather.index)
    all_out_dry_monk = pd.DataFrame(np.nanmean(all_out_dry_monk,
                                               axis=2).transpose(),
                                    columns=out_variables,
                                    index=matrix_weather.index)
    all_out_irr = pd.DataFrame(np.nanmean(all_out_irr, axis=2).transpose(),
                               columns=out_variables,
                               index=matrix_weather.index)
    all_out_irr_eyre = pd.DataFrame(np.nanmean(all_out_irr_eyre,
                                               axis=2).transpose(),
                                    columns=out_variables,
                                    index=matrix_weather.index)
    if save:
        all_out_irr.to_csv(
            "D:\mh_unbacked\SLMACC_2020\one_off_data\irr_baseline.csv")
        all_out_irr_eyre.to_csv(
            "D:\mh_unbacked\SLMACC_2020\one_off_data\dry_baseline.csv")
        all_out_dry.to_csv(
            "D:\mh_unbacked\SLMACC_2020\one_off_data\dry_baseline.csv")
        all_out_dry_monk.to_csv(
            "D:\mh_unbacked\SLMACC_2020\one_off_data\dry_monk_baseline.csv")
    all_out_irr.loc[:, 'pg'] = all_out_irr.loc[:, 'PGR']
    all_out_irr_eyre.loc[:, 'pg'] = all_out_irr_eyre.loc[:, 'PGR']
    all_out_dry.loc[:, 'pg'] = all_out_dry.loc[:, 'PGR']
    all_out_dry_monk.loc[:, 'pg'] = all_out_dry_monk.loc[:, 'PGR']
    data = {
        'dryland_monkeying': all_out_dry_monk,
        'dryland_baseline': all_out_dry,
        'irrigated_oxford': all_out_irr,
        'irrigated_eyrewell': all_out_irr_eyre,
    }
    fun = 'mean'
    data2 = {e: make_mean_comparison(v, fun) for e, v in data.items()}
    data2['horata'] = get_horarata_data_old()
    plot_multiple_monthly_results(data=data2,
                                  out_vars=['pg', 'pgr'],
                                  show=True,
                                  main_kwargs={'marker': 'o'})
def _run_paddock_rest(storyline_key, outdir, storyline, nsims, mode, site,
                      simlen, save_daily, description, storyline_text, swg_dir,
                      verbose, n_parallel, fix_leap):
    """
    run storyline through paddock restrictions...
    :param storyline:
    :param swg_path:
    :param nsims:
    :param mode:
    :param site:
    :return:
    """
    # paddock level restrictions
    levels = np.arange(
        0, 125, 25
    ) / 100  # levels already capture the extra (mean run) as levels has start stop
    number_run = int(psutil.virtual_memory().available //
                     (memory_per_run *
                      (simlen / 365) * len(levels)) / n_parallel)
    chunks = int(-1 * (-nsims // number_run))
    if chunks == 1:
        number_run = nsims

    all_outpaths = []
    for c in range(chunks):
        if verbose:
            print('running paddock pasture growth for chunk {} of {} {}-{}'.
                  format(c + 1, chunks, site, mode))
        params, doy_irr, all_matrix_weathers, all_days_harvests = _gen_input(
            storyline=storyline,
            nsims=nsims,
            mode=mode,
            site=site,
            chunks=chunks,
            current_c=c,
            nperc=number_run,
            simlen=simlen,
            swg_dir=swg_dir,
            fix_leap=fix_leap)

        all_out = np.zeros(
            (len(out_variables), simlen, len(levels) - 1, number_run)) * np.nan
        out_names = []
        out_limits = []
        for j, (matrix_weather, days_harvest) in enumerate(
                zip(all_matrix_weathers, all_days_harvests)):
            restrict = 1 - matrix_weather.loc[:, 'max_irr'] / abs_max_irr
            matrix_weather.loc[:, 'max_irr'] = abs_max_irr
            for i, (ll, lu) in enumerate(zip(levels[0:-1], levels[1:])):
                if j == 0:
                    out_name = 'paddock_{}'.format(i)
                    out_names.append(out_name)
                    out_limits.append('lower_{}_upper_{}'.format(ll, lu))
                matrix_weather_new = matrix_weather.copy(deep=True)

                matrix_weather_new.loc[restrict <= ll, 'max_irr'] = abs_max_irr
                matrix_weather_new.loc[restrict >= lu, 'max_irr'] = 0
                idx = (restrict > ll) & (restrict < lu)
                matrix_weather_new.loc[idx, 'max_irr'] = abs_max_irr * (
                    (restrict.loc[idx] - ll) / 0.25)

                temp = run_basgra_nz(params,
                                     matrix_weather_new,
                                     days_harvest,
                                     doy_irr,
                                     verbose=False,
                                     run_365_calendar=fix_leap)
                temp.loc[:,
                         'PER_PAW'] = temp.loc[:, 'PAW'] / temp.loc[:, 'MXPAW']
                temp.loc[:, 'PGR'] = calc_pasture_growth(temp,
                                                         days_harvest,
                                                         'from_yield',
                                                         '1D',
                                                         resamp_fun='mean')
                temp.loc[:, 'F_REST'] = restrict

                all_out[:, :, i,
                        j] = temp.loc[:, out_variables].values.transpose()
        # one netcdf for each mode,site, (paddock, mean)
        month = temp.index.month.values
        doy = temp.loc[:, 'doy'].values
        year = temp.loc[:, 'year'].values
        outpaths = _output_to_nc_paddock(storyline_key=storyline_key,
                                         storyline_text=storyline_text,
                                         outdir=outdir,
                                         outdata=all_out,
                                         nsims=nsims,
                                         month=month,
                                         doy=doy,
                                         year=year,
                                         chunks=chunks,
                                         current_c=c,
                                         nperchunk=number_run,
                                         out_names=out_names,
                                         out_limits=out_limits,
                                         save_daily=save_daily,
                                         description=description,
                                         site=site,
                                         mode=mode,
                                         verbose=verbose)
        all_outpaths.extend(outpaths)

    for p in np.unique(all_outpaths):
        add_pasture_growth_anaomoly_to_nc(p)
def _run_simple_rest(storyline, nsims, mode, site, simlen, storyline_key,
                     outdir, save_daily, description, storyline_text, swg_dir,
                     verbose, n_parallel, fix_leap):
    number_run = int(psutil.virtual_memory().available // memory_per_run *
                     (simlen / 365) / n_parallel)
    chunks = int(-1 * (-nsims // number_run))
    if chunks == 1:
        number_run = nsims
    all_outpaths = []
    for c in range(chunks):
        if verbose:
            print(
                'starting chunk {} of {} for simple irrigation restrictions {}-{}'
                .format(c + 1, chunks, site, mode))
        params, doy_irr, all_matrix_weathers, all_days_harvests = _gen_input(
            storyline=storyline,
            nsims=nsims,
            mode=mode,
            site=site,
            chunks=chunks,
            current_c=c,
            nperc=number_run,
            simlen=simlen,
            swg_dir=swg_dir,
            fix_leap=fix_leap)

        all_out = np.zeros((len(out_variables), simlen, number_run)) * np.nan
        for i, (matrix_weather, days_harvest) in enumerate(
                zip(all_matrix_weathers, all_days_harvests)):
            restrict = 1 - matrix_weather.loc[:, 'max_irr'] / abs_max_irr
            out = run_basgra_nz(params,
                                matrix_weather,
                                days_harvest,
                                doy_irr,
                                verbose=False,
                                run_365_calendar=fix_leap)
            out.loc[:, 'PER_PAW'] = out.loc[:, 'PAW'] / out.loc[:, 'MXPAW']

            pg = pd.DataFrame(
                calc_pasture_growth(out,
                                    days_harvest,
                                    mode='from_yield',
                                    resamp_fun='mean',
                                    freq='1d'))
            out.loc[:, 'PGR'] = pg.loc[:, 'pg']
            out.loc[:, 'F_REST'] = restrict

            all_out[:, :, i] = out.loc[:, out_variables].values.transpose()

        # one netcdf file for each mode/site
        month = out.index.month.values
        doy = out.loc[:, 'doy'].values
        year = out.loc[:, 'year'].values
        outpath = _output_to_nc(storyline_key=storyline_key,
                                storyline_text=storyline_text,
                                outdir=outdir,
                                outdata=all_out,
                                nsims=nsims,
                                month=month,
                                doy=doy,
                                year=year,
                                chunks=chunks,
                                current_c=c,
                                nperchunk=number_run,
                                save_daily=save_daily,
                                description=description,
                                site=site,
                                mode=mode,
                                verbose=verbose)

    add_pasture_growth_anaomoly_to_nc(outpath)