예제 #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
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
    def load_model(self):
        """
        Function to load input soil, site and crop parameters data from yaml files
        
        """

        crop = YAMLCropDataProvider()
        #         soil = CABOFileReader(os.path.join(self.data_dir, "ec3.soil"))

        soil = CABOFileReader(os.path.join(self.data_dir, "wofost_npk.soil"))
        site = CABOFileReader(os.path.join(self.data_dir, "wofost_npk.site"))
        site['CO2'] = 360.0
        #         site = WOFOST71SiteDataProvider(WAV=100,CO2=360)

        #parameters for model
        self.parameterprovider = ParameterProvider(soildata=soil,
                                                   cropdata=crop,
                                                   sitedata=site)
            crop_start_date: 2002-10-10
            crop_start_type: sowing
            crop_end_date: 2003-03-31
            crop_end_type: harvest
            max_duration: 200
        TimedEvents: null
        StateEvents: null
        """
    agro = yaml.load(agro_yaml)

    #crop parameters have two ways:
    #using YAML cropdataprovider to find the target crop
    #https://github.com/ajwdewit/WOFOST_crop_parameters (introduction of these parameters)
    from pcse.fileinput import YAMLCropDataProvider
    force_reload = True
    cropd = YAMLCropDataProvider()
    #cropd.print_crops_varieties ()
    #read soybena crop
    cropd.set_active_crop('soybean', 'Soybean_902')

    # read soil file
    from pcse.fileinput import CABOFileReader
    soildata = CABOFileReader("soy.soil")

    #site parameters
    from pcse.util import WOFOST71SiteDataProvider
    sitedata = WOFOST71SiteDataProvider(WAV=100, CO2=360)

    #pack the different sets of parameters
    from pcse.base_classes import ParameterProvider
    parameters = ParameterProvider(cropdata=cropd,
예제 #5
0
        crop_start_date: 2002-10-15
        crop_start_type: sowing
        crop_end_date: 2003-03-31
        crop_end_type: harvest
        max_duration: 200
    TimedEvents: null
    StateEvents: null
"""
agro = yaml.load(agro_yaml)

#crop parameters have two ways: (1) using YAMLcropdataprovider, (2) uisng CABOfilereader
#using YAML cropdataprovider to find the target crop
#https://github.com/ajwdewit/WOFOST_crop_parameters (introduction of these parameters)
from pcse.fileinput import YAMLCropDataProvider
force_reload = True
cropd = YAMLCropDataProvider(
    "/Users/Darren/Desktop/DIS_tools/Q3_wofost/WOFOST_crop_parameters-master")
cropd.print_crops_varieties()
#read soybena crop
cropd.set_active_crop('soybean', 'Soybean_906')
print(cropd)

##using CABO filereader to read crop parameters
#from pcse.fileinput import CABOFileReader
#cropdata = CABOFileReader ("sug0601.crop")
#print (cropdata)

# read soil file
from pcse.fileinput import CABOFileReader
soildata = CABOFileReader("soy.soil")

#site parameters
예제 #6
0
    def initialize(self, config_file="gridded_wofost.yaml"):
        t1 = time.time()
        self.config = read_config_file(config_file)

        # Layer inputs for AgroManagement
        with rasterio.open(self.config.maps.AEZ_map.location) as ds:
            aez_map = ds.read(1)
            aez_map[aez_map < 0] = np.NaN
            check_grid_size(self.config, aez_map)
        with rasterio.open(self.config.maps.crop_rotation_map.location) as ds:
            crop_rotation_map = ds.read(1)
            crop_rotation_map[crop_rotation_map < 0] = np.NaN
            check_grid_size(self.config, crop_rotation_map)

        # Crop, site parameters and soil grid
        crop_parameters = YAMLCropDataProvider(
            fpath=self.config.crop_parameters.location)
        site_parameters = WOFOST71SiteDataProvider(WAV=10, CO2=360)
        with rasterio.open(self.config.maps.rooting_depth.location) as ds:
            rooting_depth = ds.read(1)
            rooting_depth[rooting_depth < 0] = np.NaN
            check_grid_size(self.config, rooting_depth)

        # Weather from WFLOW NetCDF files
        self.WFLOWWeatherDataProvider = WFLOWWeatherDataProvider(self.config)

        # initialize object grid for storing WOFOST results
        self.WOFOSTgrid = np.ndarray(shape=aez_map.shape, dtype=np.object)

        # WOFOST configuration
        p = Path(__file__)
        wofost_config = str(p.parent / "conf" / "Wofost71_PP.conf")

        nrows, ncols = aez_map.shape
        p_row = None
        print("Initializing: .", end="")
        for (row, col) in product(range(nrows), range(ncols)):
            if row != p_row:
                p_row = row
                if row % 10 == 0:
                    print(f"{row/float(nrows)*100:.1f}%..", end="")
            if row == 15:
                break
            crop_rotation_type = crop_rotation_map[row, col]
            aez = aez_map[row, col]
            if crop_rotation_type not in self.config.maps.crop_rotation_map.relevant_crop_rotations:
                continue
            if aez not in self.config.maps.AEZ_map.relevant_AEZ:
                continue
            agro = read_agromanagement(self.config, aez, crop_rotation_type)
            soil_parameters = {
                "RDMSOL": rooting_depth[row, col],
                "SM0": 0.4,
                "SMFCF": 0.25,
                "SMW": 0.1,
                "CRAIRC": 0.04
            }
            params = ParameterProvider(sitedata=site_parameters,
                                       cropdata=crop_parameters,
                                       soildata=soil_parameters)
            wofsim = GridAwareEngine(
                row=row,
                col=col,
                parameterprovider=params,
                weatherdataprovider=self.WFLOWWeatherDataProvider,
                agromanagement=agro,
                config=wofost_config)
            self._check_start_end_date(wofsim, row, col, aez,
                                       crop_rotation_type)
            self.WOFOSTgrid[row, col] = wofsim
        print(f"\nInitializing took {time.time() - t1} seconds")
예제 #7
0
                crop_start_date: 2002-10-10
                crop_start_type: sowing
                crop_end_date: 2003-03-31
                crop_end_type: harvest
                max_duration: 200
            TimedEvents: null
            StateEvents: null
            """
        agro = yaml.load(agro_yaml)

        #crop parameters have two ways:
        #using YAML cropdataprovider to find the target crop
        #https://github.com/ajwdewit/WOFOST_crop_parameters (introduction of these parameters)
        from pcse.fileinput import YAMLCropDataProvider
        force_reload = True
        cropd = YAMLCropDataProvider()
        cropd.print_crops_varieties()
        #read soybena crop
        cropd.set_active_crop('soybean', 'Soybean_902')
        print(cropd)

        # read soil file
        from pcse.fileinput import CABOFileReader
        soildata = CABOFileReader("soy.soil")

        #site parameters
        from pcse.util import WOFOST71SiteDataProvider
        sitedata = WOFOST71SiteDataProvider(WAV=100, CO2=360)
        print(sitedata)

        #pack the different sets of parameters
예제 #8
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)
예제 #9
0
# Write the weather data to a cabo text file. cabo_file is a string to locate the file (Will be the same as filename)
# Don't add an extension to 'filename' write_cabo_weather_file will automatically add it.
cabo_weather_file = write_cabo_weather_file(year_of_interest,
                                            lat,
                                            lon,
                                            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