Пример #1
0
def set_up_wofost(crop_start_date,
                  crop_end_date,
                  meteo,
                  crop,
                  variety,
                  soil,
                  wav=100,
                  co2=400,
                  rdmsol=100.):
    cropdata = YAMLCropDataProvider(fpath="./WOFOST_crop_parameters")
    cropdata.set_active_crop(crop, variety)
    soildata = CABOFileReader(soil)
    soildata['RDMSOL'] = rdmsol
    sitedata = WOFOST71SiteDataProvider(WAV=wav, CO2=co2)
    parameters = ParameterProvider(cropdata=cropdata,
                                   soildata=soildata,
                                   sitedata=sitedata)
    with open("temporal.amgt", 'w') as fp:
        fp.write(
            agromanagement_contents.format(year=crop_start_date.year,
                                           crop=crop,
                                           variety=variety,
                                           crop_start_date=crop_start_date,
                                           crop_end_date=crop_end_date))
    agromanagement = YAMLAgroManagementReader("temporal.amgt")

    wdp = CABOWeatherDataProvider(meteo, fpath=f"./data/meteo/{meteo}/")
    return parameters, agromanagement, wdp
Пример #2
0
 def __init__(self, crop_path, argo_path, soil_path, meteo_path, meteo_name,
              wav, co2, region, crop_name):
     self.crop_name = crop_name
     self.crop_path = crop_path
     self.argo_path = argo_path
     self.soil_path = soil_path
     self.meteo_path = meteo_path
     # load component
     self.soil = CABOFileReader(self.soil_path)
     self.crop = CABOFileReader(self.crop_path)
     print('path', self.argo_path)
     self.argo = YAMLAgroManagementReader(self.argo_path)
     self.weather = CABOWeatherDataProvider(fname=meteo_name,
                                            fpath=self.meteo_path)
     self.site = WOFOST71SiteDataProvider(WAV=wav, CO2=co2)
     self.output_name = f'./OutPut/WofostOutPut/csv/{crop_name}-{region}.csv'
Пример #3
0
def read_agromanagement(conf, aez, crop_rotation_type, _cache={}):
    """Reads the proper agromanagement file for given EAZ and crop rotation type

    :param conf: the configuration file specifying path locations
    :param aez: the AEZ number
    :param crop_rotation_type: the crop rotation type number
    :return: a AgroManagement definition in YAML
    """
    if (aez, crop_rotation_type) not in _cache:
        agro_location = Path(conf.agromanagement_definitions.location)
        agro_definition_fname = agro_location / ("AEZ_%03i" % aez) / (
            "rotation_type_%02i.yaml" % crop_rotation_type)
        agro_definition = YAMLAgroManagementReader(agro_definition_fname)
        _cache[(aez, crop_rotation_type)] = agro_definition
    else:
        agro_definition = _cache[(aez, crop_rotation_type)]

    return agro_definition
def set_wofost_up(crop="maize",
                  variety="Maize_VanHeemst_1988",
                  meteo="Upper_East",
                  soil="ec4.new",
                  wav=60,
                  co2=400,
                  rdmsol=100):
    cropdata = YAMLCropDataProvider(fpath="./WOFOST_crop_parameters")
    cropdata.set_active_crop(crop, variety)
    soildata = CABOFileReader(soil)
    soildata["RDMSOL"] = rdmsol
    sitedata = WOFOST71SiteDataProvider(WAV=wav, CO2=co2)
    parameters = ParameterProvider(cropdata=cropdata,
                                   soildata=soildata,
                                   sitedata=sitedata)

    agromanagement = YAMLAgroManagementReader("ghana_maize.amgt")

    wdp = CABOWeatherDataProvider(meteo, fpath=f"./data/meteo/{meteo}/")
    return parameters, agromanagement, wdp
Пример #5
0
def run(crop: str, soil: str, agro: str, day: int, weather_filename: str,
        saved_name="output"):
    # load argo from directory
    agromanagement = YAMLAgroManagementReader(f"{base_dir}/{agro}")
    sitedata = WOFOST71SiteDataProvider(WAV=100, CO2=360)
    # load soil from directory
    soildata = CABOFileReader(f"{base_dir}/{soil}")
    # load crop from directory
    cropdata = CABOFileReader(f"{base_dir}/{crop}")
    # load weather data from directory
    wdp = CABOWeatherDataProvider(fname=weather_filename, fpath=base_dir)
    # packaing parameters
    parameters = ParameterProvider(cropdata=cropdata, soildata=soildata,
                                   sitedata=sitedata)
    # create model
    wofost = Wofost71_WLP_FD(parameters, wdp, agromanagement)
    # run till [day]
    wofost.run(day)

    # save output az a csv in OUT directory
    model_out_put = wofost.get_output()
    df = pd.DataFrame(model_out_put)
    df.to_csv(f"{out_dir}/{saved_name}.csv")
cropdata = CABOFileReader(os.path.join(para_dir, 'WWH101.CAB'))
soildata = CABOFileReader(os.path.join(para_dir, 'EC3.NEW'))
sitedata = {
    'SSMAX': 0.,
    'IFUNRN': 0,
    'NOTINF': 0,
    'SSI': 0,
    'WAV': 20,
    'SMLIM': 0.03,
    'CO2': 360,
    'RDMSOL': 120
}
parameters = ParameterProvider(cropdata=cropdata,
                               soildata=soildata,
                               sitedata=sitedata)
agromanagement = YAMLAgroManagementReader(
    os.path.join(para_dir, 'wheat_none.agro'))
#  创建文档储存模型数值结果
with open(os.path.join(para_dir, '总结果.txt'), 'w') as fp3:
    fp3.writelines(['2', '\n', 'TSOW', '\n', 'TAGP', '\n', 'time = no', '\n'])
    with open(os.path.join(para_dir, '按生育期结果.txt'), 'w') as fp2:
        fp2.writelines(['1', '\n', 'LAI', '\n', 'time = yes', '\n'])
        #  打开simlab输出的文档
        with open(os.path.join(para_dir, 'PARA.sam'), 'r') as fp:
            fp.readline()  # 第一行
            number = fp.readline()  # 第二行为生成参数个数
            fp.readline()  # 变量个数
            fp.readline()  # 0  此后开始读参数
            fp2.write(str(number))
            fp3.write(str(number))
            for i in progressbar.ProgressBar()(range(int(number))):
                sim_paraments = list(map(float,
Пример #7
0
def agromanagementLoader(name):
    agromanagement_file = os.path.join(argo_dir, name)
    print("loading agromanagement...")
    agromanagement = YAMLAgroManagementReader(agromanagement_file)
    return agromanagement
#site parameters
from pcse.util import WOFOST71SiteDataProvider
sitedata = WOFOST71SiteDataProvider(WAV=100, CO2=360)
print(sitedata)

#pack the different sets of parameters
from pcse.base_classes import ParameterProvider
parameters = ParameterProvider(cropdata=cropdata,
                               soildata=soildata,
                               sitedata=sitedata)  #cropdata = cropd

#agromanagement control the start date and end date
#there are two files, one is defalut, the other one is for excel weather data
from pcse.fileinput import YAMLAgroManagementReader
agromanagement = YAMLAgroManagementReader("sugarbeet_c.agro")
print(agromanagement)

#using modeul to read weather data
from pcse.db import NASAPowerWeatherDataProvider
wdp = NASAPowerWeatherDataProvider(latitude=52, longitude=5)
print(wdp)

#from pcse.fileinput import ExcelWeatherDataProvider
#wdp = ExcelWeatherDataProvider ("weather04.xlsx")
#print (wdp)

#simulate the crop
from pcse.models import Wofost71_WLP_FD
wofsim = Wofost71_WLP_FD(parameters, wdp, agromanagement)
wofsim.run_till_terminate()
Пример #9
0
def wofost_parameter_sweep_func(crop_start_date=dt.date(2011, 7, 1),
                                crop_end_date=dt.datetime(2011, 11, 1),
                                span=40.0,
                                tdwi=20.,
                                tsum1=750.,
                                tsum2=859.,
                                tsumem=70,
                                rgrlai=0.05,
                                cvo=0.05,
                                cvl=0.05,
                                meteo="Upper_East",
                                crop="maize",
                                variety="Maize_VanHeemst_1988",
                                soil="ec4.new",
                                wav=100,
                                co2=400,
                                rdmsol=100.,
                                potential=False):
    cropdata = YAMLCropDataProvider(fpath="./WOFOST_crop_parameters")
    cropdata.set_active_crop(crop, variety)
    soildata = CABOFileReader(soil)
    soildata["RDMSOL"] = rdmsol
    sitedata = WOFOST71SiteDataProvider(WAV=wav, CO2=co2)
    parameters = ParameterProvider(cropdata=cropdata,
                                   soildata=soildata,
                                   sitedata=sitedata)
    for p, v in zip(
        ["SPAN", "TSUM1", "TSUM2", "TSUMEM", "TDWI", "RGRLAI", "CVO", "CVL"],
        [span, tsum1, tsum2, tsumem, tdwi, rgrlai, cvo, cvl]):
        parameters.set_override(p, v, check=True)
    with open("temporal.amgt", 'w') as fp:
        fp.write(
            agromanagement_contents.format(year=crop_start_date.year,
                                           crop=crop,
                                           variety=variety,
                                           crop_start_date=crop_start_date,
                                           crop_end_date=crop_end_date))
    agromanagement = YAMLAgroManagementReader("temporal.amgt")

    wdp = CABOWeatherDataProvider(meteo, fpath=f"./data/meteo/{meteo}/")
    df_results, simulator = run_wofost(parameters,
                                       agromanagement,
                                       wdp,
                                       potential=potential)
    fig, axs = plt.subplots(nrows=5,
                            ncols=2,
                            sharex=True,
                            squeeze=True,
                            figsize=(16, 16))
    axs = axs.flatten()
    for j, p in enumerate(WOFOST_PARAMETERS):
        axs[j].plot_date(df_results.index, df_results[p], '-')
        axs[j].set_ylabel(WOFOST_LABELS[p], fontsize=8)

    plt.gcf().autofmt_xdate()
    plt.gca().fmt_xdata = matplotlib.dates.DateFormatter('%Y-%m-%d')
    axs[8].set_xlabel("Time [d]")
    axs[9].set_xlabel("Time [d]")

    key = f"span_{span}-tdwi_{tdwi}-tsum1_{tsum1}-tsum2_{tsum2}-tsumem_{tsumem}"
    key += f"-rgrlai_{rgrlai}-wav_{wav}-cvo_{cvo}-cvl_{cvl}"

    if potential:
        key += "-POT.csv"
    else:
        key += "-LIM.csv"

    df_results.to_csv(key, encoding="utf-8", index=False)
Пример #10
0
def ensemble_wofost(lon=115.55,
                    lat=38.05,
                    start=dt.date(2008, 10, 12),
                    end=None,
                    en_size=3,
                    prior_file=None,
                    weather_type="NASA",
                    weather_path=None,
                    out_en_file=None,
                    data_dir=None):
    """
    This is a function to generate a emsemble of WOFOST paramters and corresponding output.
    you need to specify Longitude (lon), Latitude (lat), 
    start time of crop (start), end time of crop (end, 270 days duration by default),
    emsemble size (en_size), configuration file for prior distributions of pramaters (prior_file), 
    weather driver dataset type (weather_type), it's set to NASA Power dataset "NASA" by default,
    you could use ERA5 "ERA5" or ECMWF TIGGE "ITGEE" instead or use your own CABO file (%your_cabo_files_name%).)
    """
    if data_dir is None:
        #home = os.path.dirname(os.path.realpath("__file__"))
        home = os.path.split(os.path.realpath(__file__))[
            0]  #os.path.dirname(os.path.abspath(__file__))
        data_dir = home + "/data/"
        #print(data_dir)
    if prior_file is None:
        prior_file = data_dir + "par_prior.csv"
    if out_en_file is None:
        out_en_file = "WOFOST_par_ensemble.npy"

    if lat < -90 or lat > 90:
        msg = "Latitude should be between -90 and 90 degrees."
        raise ValueError(msg)
    if lon < -180 or lon > 180:
        msg = "Longitude should be between -180 and 180 degrees."
        raise ValueError(msg)
    if end == None:
        end = start + dt.timedelta(days=270)
    if start >= end:
        msg = "Start time should be earlier than end time."
        raise ValueError(msg)
    if weather_type == "NASA":
        print("Downloading weather driver from NASA Power...")
        weather = NASAPowerWeatherDataProvider(latitude=lat, longitude=lon)
    elif weather_type[:3] == "ERA" or weather_type[:3] == "era":
        print("ERA5 reanalysis dataset used.")
        if weather_path is None or not os.path.isdir(weather_path):
            msg = "Please provide a valid path for weahter driver data."
            raise ValueError(msg)
        gen_era_cabo(lat,
                     lon,
                     start.year,
                     end.year,
                     inputfile=weather_path,
                     data_dir=data_dir)
        size = 0.25
        weather_name = "ERA_%5.2f_%5.2f" % (int(
            (lon + size / 2.) / size) * size, int(
                (lat + size / 2.) / size) * size)
        weather = CABOWeatherDataProvider(weather_name, fpath=weather_path)
    elif weather_type[:5].upper() == "TIGGE":
        print("TIGGE forecast from ECMWF used.")
        if weather_path is None or not os.path.isdir(weather_path):
            msg = "Please provide a valid path for weahter driver data."
            raise ValueError(msg)
        gen_tigge_cabo(lat,
                       lon,
                       start.year,
                       end.year,
                       inputfile=weather_path,
                       data_dir=data_dir)
        size = 0.25
        weather_name = "TIGGE_%5.2f_%5.2f" % (int(
            (lon + size / 2.) / size) * size, int(
                (lat + size / 2.) / size) * size)
        weather = CABOWeatherDataProvider(weather_name, fpath=weather_path)
    else:
        if weather_path == None:
            raise ValueError("Please provide your weather driver path!")
        weather = CABOWeatherDataProvider(weather_type, fpath=weather_path)

    sdoy = retrieve_pixel_value([lon, lat], data_dir +
                                "mean_wheat_sdoy_china_kriging_int.tif")
    tsum1 = retrieve_pixel_value([lon, lat], data_dir +
                                 "mean_wheat_TSUM1_china_kriging.tif")
    tsum2 = retrieve_pixel_value([lon, lat],
                                 data_dir + "TSUM2_aver_0.1deg.tif")

    varnames = ["day", "TAGP", "LAI", "TWSO", "DVS"]
    tmp = {}

    cropfile = os.path.join(data_dir, 'WWH108.CAB')
    crop = CABOFileReader(cropfile)
    soilfile = os.path.join(data_dir, 'Hengshui.soil')
    soil = CABOFileReader(soilfile)
    site = WOFOST71SiteDataProvider(WAV=100, CO2=360)
    parameters = ParameterProvider(soildata=soil, cropdata=crop, sitedata=site)
    agromanagement_file = os.path.join(data_dir, 'shenzhou_wheat.amgt')
    agromanagement = YAMLAgroManagementReader(agromanagement_file)
    (key, value), = agromanagement[0].items()
    agromanagement[0][dt.datetime.strptime(
        "%d%03d" % (start.year, sdoy),
        "%Y%j").date()] = agromanagement[0].pop(key)
    value['CropCalendar']['crop_start_date'] = dt.datetime.strptime(
        "%d%03d" % (start.year, sdoy), "%Y%j").date()
    print("Crop is sowed at %s" % dt.datetime.strftime(
        value['CropCalendar']['crop_start_date'], "%Y-%m-%d"))
    value['CropCalendar']['crop_end_date'] = end

    prior_dist, prior_list, param_xvalue, param_type = define_prior_distributions(
        chunk=prior_file, tsum1=tsum1, tsum2=tsum2)
    z_start = np.empty((len(prior_list), en_size))
    for i, param in enumerate(prior_list):
        z_start[i, :] = prior_dist[param].rvs(en_size)
    outdata = []

    for i in range(en_size):
        theta_dict = dict(zip(prior_list, z_start[:, i]))
        cropdata = copy.deepcopy(crop)
        tb_x = {}
        tb_y = {}
        tb_t = {}
        tmp_dict = {}
        for par in theta_dict.keys():
            try:
                if param_type[par] != 'S':
                    tb_index = par.find("TB")
                    if tb_index < 0:
                        print(param_xvparam_typealue[par])
                        raise Exception("Are you sure %s is a table value?" %
                                        par)
                    tb_name = par[:tb_index + 2]
                    tmp_list = [param_xvalue[par], theta_dict[par]]
                    if not tb_name in tb_x:
                        tb_x[tb_name] = np.array([param_xvalue[par]])
                        tb_y[tb_name] = np.array([theta_dict[par]])
                        tb_t[tb_name] = param_type[par]
                    else:
                        tb_x[tb_name] = np.append(tb_x[tb_name],
                                                  param_xvalue[par])
                        tb_y[tb_name] = np.append(tb_y[tb_name],
                                                  theta_dict[par])
            except KeyError:
                raise Exception(
                    "There's something wrong with %s, please check it." % par)
        tmp_dict = {}
        for par in tb_x.keys():  # Table parameters
            s_i = np.argsort(tb_x[par])
            s_x = tb_x[par][s_i]
            s_v = tb_y[par][s_i]
            par_tb = []
            #         print(par,tb_t[par],cropdata[par],s_x,s_v)
            if tb_t[par][1] == 'P':
                for i in range(len(tb_x[par])):
                    if tb_t[par][0] == 'Y':  # Partly change table Y values
                        if s_x[i] in cropdata[par][::2]:  # change old value
                            c_i = cropdata[par][::2].index(s_x[i])
                            cropdata[par][c_i * 2] = s_v[i]
                        else:  # insert new value
                            array_X = cropdata[par][::2]
                            array_Y = cropdata[par][1:][::2]
                            ins_i = bisect(array_X, s_x[i])
                            cropdata[par].insert(ins_i * 2, s_x[i])
                            cropdata[par].insert(ins_i * 2 + 1, s_v[i])
                        #print(cropdata[par])
                    else:  # Partly change table X values
                        if s_x[i] in cropdata[par][
                                1:][::2]:  # change old value
                            c_i = cropdata[par][1:][::2].index(s_x[i])
                            cropdata[par][c_i * 2] = s_v[i]
                        else:  # insert new value
                            array_X = cropdata[par][::2]
                            array_Y = cropdata[par][1:][::2]
                            ins_i = bisect(array_X, s_x[i])
                            cropdata[par].insert(ins_i * 2, s_x[i])
                            cropdata[par].insert(ins_i * 2 + 1, s_v[i])
                        #print(cropdata[par])
            elif tb_t[par][1] == 'A':
                if tb_t[par][0] == 'Y':  # Totally change table Y values
                    for i in range(len(tb_x[par])):
                        par_tb.append(s_x[i])
                        par_tb.append(s_v[i])
                else:  # Totally change table X values
                    for i in range(len(tb_x[par])):
                        par_tb.append(s_v[i])
                        par_tb.append(s_x[i])
                tmp_dict[par] = par_tb
                #print(tmp_dict[par])
                theta_dict.update(tmp_dict)
            else:
                raise Exception(
                    "There's something wrong with %s, please check it." % par)
        ##########################################################################
        cropdata.update(theta_dict)

        parameters = ParameterProvider(cropdata=cropdata,
                                       soildata=soil,
                                       sitedata=site)
        wofwof = Wofost71_PP(parameters, weather, agromanagement)
        wofwof.run_till_terminate()
        output = wofwof.get_output()
        summary_output = wofwof.get_summary_output()
        msg = "Reached maturity at {DOM} with max LAI of {LAIMAX} "\
    "and a yield of {TWSO} kg/ha."
        print(msg.format(**summary_output[0]))
        for var in varnames:
            tmp[var] = [t[var] for t in output]
        theta_dict["LAI"] = tmp["LAI"][-181:]
        theta_dict["day"] = tmp["day"][-181:]
        theta_dict["Yield"] = tmp["TWSO"][-1]
        outdata.append(theta_dict)
    np.save(out_en_file, outdata)
Пример #11
0
    def Generate_With_Dists_From_Scratch(self,
                                         distribution_file,
                                         crop_file,
                                         soil_file,
                                         weather_point,
                                         timer_file,
                                         central_value='absolute'):
        """
        Generate ensembles using strings pointing to the wofost files from a parameter distribution file.
        
            - distribution_file - string of the location where the parameter distribution is.
            - crop_file - the parameter file location string.
            - soil_file - the soil parameter file location string.
            - weather_point - the unix wildcard search which identifies the weather data.
            - timer_file - the timer file location string.
        """

        if central_value not in ['absolute', 'relative']:
            raise ValueError('central_value must be absolute or relative.\
                             \nAbsolute using the exact distributions from the distribution file.\
                             \nRelative creates distribuitions around the input crop file.'
                             )

        manager = multiprocessing.Manager()
        self.repo = manager.list()
        self.param_files = []
        self.generated_agromanagers = []
        self.distribution_file = distribution_file
        self.central_value = central_value

        try:
            self.params = pa.read_csv(distribution_file)
        except:
            raise NameError('Cant open the distribution file %s' %
                            distribution_file)

        self.new_param_vals = {}

        for n, i in enumerate(self.params['Param'].iloc[:]):
            if np.isnan(self.params['Function_Value'].iloc[n]) == True:
                self.new_param_vals[i] = []
            else:
                if i not in list(self.new_param_vals.keys()):
                    self.new_param_vals[i] = {}
                    self.new_param_vals[i][
                        self.params['Function_Value'].iloc[n]] = []
                else:
                    self.new_param_vals[i][
                        self.params['Function_Value'].iloc[n]] = []

        # Read in the parameter files:
        crop = CABOFileReader(crop_file)
        soil = CABOFileReader(soil_file)

        # # the site parameters cover extra stuff not covered by the parameter files
        # # wav is the initial soil moisture content.
        site = WOFOST71SiteDataProvider(WAV=100, CO2=360)

        # # Read in the weather file
        weather = CABOWeatherDataProvider(weather_point)

        # get the agromanager
        agromanagement_object = YAMLAgroManagementReader(timer_file)

        # define a function that is multiprocessable
        def multiproc_wofost(input_wofost_object):

            input_wofost_object.run_till_terminate()

            self.repo.append(input_wofost_object.get_output())

        # setup somewhere to put the processes
        active_processes = []

        run_on = multiprocessing.cpu_count() - 1

        process_counter = 0

        while process_counter < self.en_number:

            if len(active_processes) < run_on:

                # get a clean version of the parameters
                new = copy.deepcopy(crop)

                # loop through the parameters in the file
                for j in range(len(self.params)):

                    name, mu, min_val, max_val, sigma, func = self.params.iloc[
                        j]
                    if name == 'PDATE':
                        continue

                    if self.central_value is 'relative':
                        if type(crop_object[name]) in [int, float]:
                            mu = crop_object[name]
                            # min and max are 3 sigma away from the mean
                            min_val = mu - (self.rel_rng * sigma)
                            max_val = mu + (self.rel_rng * sigma)

                        else:
                            if func in new[name]:
                                loc = np.where(
                                    np.array(new[name]) == func)[0][0]
                                mu = new[name][loc + 1]
                                min_val = mu - (self.rel_rng * sigma)
                                max_val = mu + (self.rel_rng * sigma)

                            else:
                                # WARNING:
                                # if we have gone down this route, it means there
                                # is no current function value for this parameter.
                                # this could lead to potentially weird results.
                                # blind_obedience means to put it in anyway.
                                blind_obedience = True
                                if blind_obedience == True:
                                    pass
                                else:
                                    continue

                    # get the distributions
                    dist = scipy.stats.truncnorm((min_val - mu) / sigma,
                                                 (max_val - mu) / sigma,
                                                 loc=mu,
                                                 scale=sigma)

                    # get a new value
                    new_val = dist.rvs(1)[0]

                    # first, reasign the simple single parameters
                    if np.isnan(func) == True:
                        new[name] = new_val
                        self.new_param_vals[name].append(new_val)

                    else:
                        # first check if there already is a function value in place already
                        prs_keys = np.array(new[name])[::2]
                        prs_vals = np.array(new[name])[1::2]

                        # quickly add the val to the new _param_values
                        self.new_param_vals[name][func].append(new_val)

                        # reasign the values if the function value is there
                        if func in prs_keys:
                            prs_vals[np.where(
                                prs_keys == func)[0][0]] = new_val
                            new[name] = np.hstack(zip(prs_keys, prs_vals))

                        # or put a new one in if it is not there already
                        else:
                            new_keys = np.concatenate(
                                [prs_keys, np.array([func])])
                            new_vals = np.concatenate(
                                [prs_vals, np.array([new_val])])

                            sort_index = np.argsort(new_keys)

                            new_keys = new_keys[sort_index]
                            new_vals = new_vals[sort_index]

                            new[name] = np.hstack(zip(new_keys, new_vals))

                # reassign the planting date based off the normal distribution:
                # grab the row in the param file that is the planting date
                if 'PDATE' in self.params['Param'].values:
                    pdate_row = np.where(
                        self.params['Param'].values == 'PDATE')[0][0]

                    # get the aspects to make the normal distribution
                    pdate_min = self.params['Min'].values[pdate_row]
                    pdate_max = self.params['Max'].values[pdate_row]
                    pdate_mu = self.params['Mean'].values[pdate_row]
                    pdate_sigma = self.params['StdDev'].values[pdate_row]

                    # generate the distributions
                    pdate_dist = scipy.stats.truncnorm(
                        (pdate_min - pdate_mu) / pdate_sigma,
                        (pdate_max - pdate_mu) / pdate_sigma,
                        loc=pdate_mu,
                        scale=pdate_sigma)

                    # pull out the key for the agromanager
                    campaign_start = list(agromanagement_object[0].keys())[0]

                    # create a new planting date
                    new_pdate = agromanagement_object[0][campaign_start]['CropCalendar']['crop_start_date'] + \
                    dt.timedelta(days=pdate_dist.rvs(1)[0])

                    # make all ensembles have the same campaign length so everything fits
                    new_campdate = campaign_start - dt.timedelta(
                        days=abs(pdate_min) - 1)

                    # create the new agromanager with the new planting date
                    new_agromanager = copy.deepcopy(
                        agromanagement_object)[0][campaign_start]
                    new_agromanager['CropCalendar'][
                        'crop_start_date'] = new_pdate

                    new_agro_obj = [{new_campdate: new_agromanager}]

                    # add it to a repo so we have a record of it
                    self.generated_agromanagers.append(new_agro_obj)

                else:
                    new_agro_obj = agromanagement_object

                self.param_files.append(new)
                new_parameter_object = ParameterProvider(new, soil, site)

                # instantiate the new version of wofost
                iter_wof = self.runner(new_parameter_object, weather,
                                       new_agro_obj)

                # and process it using multiprocessing
                p = multiprocessing.Process(target=multiproc_wofost,
                                            args=(iter_wof, ))
                p.daemon = True
                p.name = str(process_counter)

                p.start()

                active_processes.append(p)

                process_counter += 1

            else:

                for pr in active_processes:

                    if pr.is_alive() == False:

                        active_processes.remove(pr)
Пример #12
0
"""

from pcse.base import ParameterProvider
parameters = ParameterProvider(cropdata=cropdata, soildata=soildata, sitedata=sitedata)

"""## Agromanagement
The agromanagement inputs provide the start date of the agricultural campaign, the start_date/start_type of the crop simulation, the end_date/end_type of the crop simulation and the maximum duration of the crop simulation. The latter is included to avoid unrealistically long simulations for example as a results of a too high temperature sum requirement.

The agromanagement inputs are defined with a special syntax called [YAML](http://yaml.org/) which allows to easily create more complex structures which is needed for defining the agromanagement. The agromanagement file for sugar beet in Wageningen `sugarbeet_calendar.agro` can be read with the `YAMLAgroManagementReader`:
"""

from pcse.fileinput import YAMLAgroManagementReader
#crop rotation for Moscow region
#agromanagement_file = os.path.join(data_dir, 'agro', 'sugarbeet_calendar_Moscow_short.agro')
agromanagement_file = os.path.join(data_dir, 'agro', 'sugarbeet_calendar.agro')
agromanagement = YAMLAgroManagementReader(agromanagement_file)
print(agromanagement)

"""We can create a crop rotation in the model"""

K_kg = 60
P_kg = 60
N_kg = 120
year_date=2019
yaml_agro = f"""
- {year_date}-06-01:
    CropCalendar:
        crop_name: 'sugar-beet'
        variety_name: 'sugar-beet-601'
        crop_start_date: {year_date}-06-02
        crop_start_type: emergence
soildata = CABOFileReader(os.path.join(data_dir, 'ec3.soil'))
sitedata = {
    'SSMAX': 0.,
    'IFUNRN': 0,
    'NOTINF': 0,
    'SSI': 0,
    'WAV': 100,
    'SMLIM': 0.03,
    'CO2': 360
}
parameters = ParameterProvider(cropdata=cropdata,
                               soildata=soildata,
                               sitedata=sitedata)

# Read agromanagement
agromanagement = YAMLAgroManagementReader(
    os.path.join(data_dir, 'sugarbeet_calendar.amgt'))

# Start WOFOST
wf = Wofost71_WLP_FD(parameters, wdp, agromanagement)
wf.run_till_terminate()

# Get time-series output from WOFOST and take the selected variables
output = wf.get_output()
varnames = ["day", "DVS", "TAGP", "LAI", "SM"]
tmp = {}
for var in varnames:
    tmp[var] = [t[var] for t in output]
day = tmp.pop("day")

# make a figure with 2x2 subplots
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))
Пример #14
0
                                            weather_data_to_write,
                                            filename=filename)

#=======================================
#======= Running the crop model ========

# Set up input paramter files describing soil and crop.
# You will always use the same files here
soil = CABOFileReader('Hengshui.soil')
site = WOFOST71SiteDataProvider(WAV=100, CO2=360)
crop = YAMLCropDataProvider('.')  # directory containing crop file
crop.set_active_crop('maize', 'Grain_maize_204')
parameters = ParameterProvider(crop, soil, site)

# Set up the parameters that describe sowing, harvest and crop management.
agromanagement = YAMLAgroManagementReader('timer_china_maize.amgt')
# Update agromanagement to the year we are interested in. This needs to
# match the year of the weather data you are using
new_agromanagement = change_year(agromanagement, year_of_interest)

# Set up the weather file with the weather data we wrote above
weather = CABOWeatherDataProvider(cabo_weather_file)

# Initialise the model
wofost = Wofost71_PP(parameters, weather, new_agromanagement)
# Run the crop model
wofost.run_till_terminate()
# Get the out put
output = wofost.get_output()

# Final yield, This is what we are interested in.