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
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
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
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
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 if __name__ == '__main__': save = False params, doy = get_params_doy_irr(mode) outdir = ksl_env.shared_drives(r"Z2003_SLMACC\pasture_growth_modelling\irrigation_tuning") if not os.path.exists(outdir): os.makedirs(outdir) out, paddock_values = create_irrigation_abandomnet_data('b', params, reseed_trig=0.691, reseed_basal=0.722, site='eyrewell') # run some stats outkeys = ['BASAL', 'f_rest', 'IRRIG', 'per_PAW', 'pg'] for k, v in out.items(): v.loc[:, 'month'] = v.index.month rest = get_restriction_record() rest = rest.groupby(['year', 'month']).sum() idxs = pd.MultiIndex.from_product([pd.unique(out['b_paddock_rest'].loc[:, 'year']), pd.unique(out['b_paddock_rest'].loc[:, 'month'])], names=['year', 'month'])
def _gen_input(storyline, nsims, mode, site, chunks, current_c, nperc, simlen, swg_dir, fix_leap): """ :param storyline: loaded storyline :param SWG_path: path to the directory with contining the files from the SWG :param nsims: number of sims to run :param mode: one of ['irrigated', 'dryland'] :param site: one of ['eyrewell', 'oxford'] :param chunks: the number of chunks :param current_c: the current chunk (from range(chunks) :param nperc: number of simulations that can be run per chunk :return: """ # manage chunks if chunks == 1: num_to_pull = nsims elif chunks > 1: num_to_pull = nperc if current_c + 1 == chunks: # manage last chunk num_to_pull = nsims - (current_c * nperc) else: raise ValueError('shouldnt get here') params, doy_irr = get_params_doy_irr(mode, site) matrix_weathers = [] days_harvests = [] # get restriction data if mode == 'dryland': rest_data = np.repeat([None], num_to_pull) elif mode == 'irrigated': rest_data = get_irr_data(num_to_pull, storyline, simlen) else: raise ValueError('weird arg for mode: {}'.format(mode)) # get weather data weather_data = _get_weather_data(storyline=storyline, nsims=num_to_pull, simlen=simlen, swg_dir=swg_dir, site=site, fix_leap=fix_leap) # make all the other data for rest, weather in zip(rest_data, weather_data): if rest is None: rest_temp = None else: rest_temp = pd.DataFrame(data=rest, index=weather.index, columns=['frest']) matrix_weather = create_matrix_weather(mode, weather_data=weather, restriction_data=rest_temp, rest_key='frest', fix_leap=fix_leap) matrix_weathers.append(matrix_weather) days_harvests.append( create_days_harvest(mode, matrix_weather, site, fix_leap=fix_leap)) return params, doy_irr, matrix_weathers, days_harvests