我是目录
使用自己的数据运行PCSE/WOFOST模型
Introduction
要使用您自己的数据源运行PCSE/WOFOST(以及一般的PCSE模型),您需要三种不同类型的输入:
1.模型参数,参数化不同模型组件。这些参数通常包括作物参数(或多组作物轮作)、土壤参数和场地参数。后者提供特定位置的辅助参数,例如土壤中的初始湿度。
2.由可从各种来源获得的天气数据表示的驱动变量。
3.农业管理行动,具体说明将在PCSE模拟的田间进行的农场活动。
在本例中,我们将在瓦赫宁根(荷兰)对甜菜进行模拟,我们将从几个不同的来源逐步读取输入数据,而不是使用预先配置的start_wofost()脚本。例如,假设数据文件位于此笔记本所在目录的数据目录中。
Importing the relevant modules
%matplotlib inline
import sys, os
import matplotlib
matplotlib.style.use("ggplot")
import matplotlib.pyplot as plt
import pandas as pd
data_dir = os.path.join(os.getcwd(), "data")
import pcse
print("This notebook was built with:")
print("python version: %s " % sys.version)
print("PCSE version: %s" % pcse.__version__)
This notebook was built with:
python version: 3.7.9 (default, Aug 31 2020, 07:22:35)
[Clang 10.0.0 ]
PCSE version: 5.4.2
Reading model parameters
Crop parameters 作物参数
作物参数由参数名称和相应的参数值组成,需要对作物模拟模型的组成部分进行参数化。这些是关于物候学、同化作用、呼吸作用、生物量分配等的作物特定值。甜菜的参数文件取自 WOFOST 控制中心的作物文件。
由于Wageningen中的许多作物模型都是用FORTRAN编写的,因此Wageningen中许多模型的作物参数通常以CABO格式提供,可通过TTUTIL FORTRAN库读取。这种CABO格式将逐步淘汰,PCSE将转向基于YAML的新格式,请参见此处的示例。但是,PCSE尽量做到向后兼容,并提供CABOFileReader以读取CABO格式的参数文件。CABOFileReader返回一个包含参数名称/值对的字典:
from pcse.fileinput import CABOFileReader
cropfile = os.path.join(data_dir, 'crop', 'SUG0601.crop')
cropd = CABOFileReader(cropfile)
Soil parameters 土壤参数
提供了与土壤类型和土壤物理特性相关的参数名称/值对。参数的数量是可变的,取决于用于模拟的土壤水平衡类型。在这个例子中,我们将使用自由排水土壤的水平衡,并使用中细砂的土壤文件:ec3.soil。
soilfile = os.path.join(data_dir, 'soil', 'ec3.soil')
soild = CABOFileReader(soilfile)
Site parameters 位点参数
场地参数提供与作物或土壤无关的辅助参数。例如,水平衡的初始条件,如初始土壤含水量(土壤初始含水量)(WAV)和初始和最大表面存储(SSI、SSMAX)。还有大气𝐶𝑂二氧化碳浓度,是一个典型的现场参数。目前,我们可以直接在Python命令行上将这些参数定义为一个简单的Python字典。但是,使用WOFOST71SiteDataProvider记录站点参数并提供合理的默认值更方便:
from pcse.util import WOFOST71SiteDataProvider
sited = WOFOST71SiteDataProvider(WAV=10, CO2=360)
print(sited)
Packaging all parameters 包装参数
最后,我们需要使用“ParameterProvider”将不同的参数集打包成一个变量。这是必需的,因为PCSE需要一个包含所有参数值的变量。使用此方法的另一个优点是,在运行多个参数值略有不同的模拟时,可以轻松覆盖参数值:
from pcse.base import ParameterProvider
parameters = ParameterProvider(cropdata=cropd, soildata=soild, sitedata=sited)
Agromanagement
农业管理输入提供了农业活动的开始日期、作物模拟的开始_日期/开始_类型、作物模拟的结束_日期/结束_类型以及作物模拟的最大持续时间。包括后者是为了避免不切实际的长时间模拟,例如由于温度总和要求过高而导致的模拟。
agromanagement输入使用一种称为[YAML]的特殊语法进行定义(http://yaml.org/),这允许轻松创建定义农业管理所需的更复杂的结构。Wageningen“Sugarbete_calendar.agro”中甜菜的农业管理文件可通过“YAMLAgroManagementReader”读取:
from pcse.fileinput import YAMLAgroManagementReader
agromanagement_file = os.path.join(data_dir, 'agro', 'sugarbeet_calendar.agro')
agromanagement = YAMLAgroManagementReader(agromanagement_file)
print(agromanagement)
!!python/object/new:pcse.fileinput.yaml_agro_loader.YAMLAgroManagementReader
listitems:
- 2006-01-01:
CropCalendar:
crop_end_date: 2006-10-20
crop_end_type: harvest
crop_name: sugar-beet
crop_start_date: 2006-04-05
crop_start_type: emergence
max_duration: 300
variety_name: sugar-beet-601
StateEvents: null
TimedEvents: null
Daily weather observations
运行模拟需要每日天气变量。PCSE中有多个用于读取天气数据的数据提供程序,请参阅[天气数据提供程序]部分(http://pcse.readthedocs.io/en/stable/reference_guide.html#weather-数据提供者)以获取概述。
在本例中,我们将使用excel文件中的天气数据,该文件提供了2004年至2008年期间瓦赫宁根的每日天气数据。我们将使用excel WeatherDataProvider从文件中读取数据:
from pcse.fileinput import ExcelWeatherDataProvider
weatherfile = os.path.join(data_dir, 'meteo', 'nl1.xls')
wdp = ExcelWeatherDataProvider(weatherfile)
print(wdp)
Weather data provided by: ExcelWeatherDataProvider
--------Description---------
Weather data for:
Country: Netherlands
Station: Wageningen, Location Haarweg
Description: Observed data from Station Haarweg in Wageningen
Source: Meteorology and Air Quality Group, Wageningen University
Contact: Peter Uithol
----Site characteristics----
Elevation: 7.0
Latitude: 51.970
Longitude: 5.670
Data available for 2004-01-02 - 2008-12-31
Number of missing days: 0
import pcse
from pcse.fileinput import ExcelWeatherDataProvider
wdp = ExcelWeatherDataProvider('/Users/will/Project/PCSE/pcse_notebooks-master/data/meteo/nl1.xls')
此外,我们可以调用带有日期对象的天气数据提供商来检索该日期的天气数据容器:
from datetime import date
day = date(2006,7,3)
wdc = wdp(day)
print(wdc)
Weather data for 2006-07-03 (DAY)
IRRAD: 29290000.00 J/m2/day
TMIN: 17.20 Celsius
TMAX: 29.60 Celsius
VAP: 12.80 hPa
RAIN: 0.00 cm/day
E0: 0.77 cm/day
ES0: 0.69 cm/day
ET0: 0.72 cm/day
WIND: 2.90 m/sec
Latitude (LAT): 51.97 degr.
Longitude (LON): 5.67 degr.
Elevation (ELEV): 7.0 m.
# 访问单个天气元素
print(wdc.TMAX)
29.6
#最后,为了方便起见,也可以使用表示日期的字符串调用WeatherDataProvider。此字符串可以采用YYYYMMDD或YYYYDDD格式:
print (wdp("20060703"))
Weather data for 2006-07-03 (DAY)
IRRAD: 29290000.00 J/m2/day
TMIN: 17.20 Celsius
TMAX: 29.60 Celsius
VAP: 12.80 hPa
RAIN: 0.00 cm/day
E0: 0.77 cm/day
ES0: 0.69 cm/day
ET0: 0.72 cm/day
WIND: 2.90 m/sec
Latitude (LAT): 51.97 degr.
Longitude (LON): 5.67 degr.
Elevation (ELEV): 7.0 m.
# 或格式为YYYYDDD
print (wdp("2006183"))
Weather data for 2006-07-02 (DAY)
IRRAD: 28339000.00 J/m2/day
TMIN: 15.00 Celsius
TMAX: 29.90 Celsius
VAP: 14.20 hPa
RAIN: 0.00 cm/day
E0: 0.79 cm/day
ES0: 0.71 cm/day
ET0: 0.67 cm/day
WIND: 2.90 m/sec
Latitude (LAT): 51.97 degr.
Longitude (LON): 5.67 degr.
Elevation (ELEV): 7.0 m.
Importing, initializing and running a PCSE model
PCSE使用模拟引擎运行作物模拟,此引擎获取一个配置文件,该文件指定了模拟需要使用的作物、土壤和农业管理的组件。因此,任何PCSE模型都可以通过导入引擎并使用给定的配置文件和相应的参数、天气数据和农业管理对其进行初始化来启动。
但是,由于许多PCSE用户只需要特定的配置(例如潜在产量的WOFOST模型),因此“PCSE.models”中提供了预配置的引擎。对于甜菜示例,我们将导入WOFOST模型,用于自由排水土壤条件下的水限制模拟:
from pcse.models import Wofost71_WLP_FD, Wofost71_PP
wofsim = Wofost71_WLP_FD(parameters, wdp, agromanagement)
output = wofsim.get_output()
import pandas as pd
df = pd.DataFrame(output)
df.to_excel("wofsim_results_02.xls")
然后,我们可以在WOFOST对象上运行模拟并使用get_output()方法检索每日模拟输出的时间序列。最后,我们将模拟结果转换为 pandas dataframe:
wofsim.run_till_terminate()
df_results = pd.DataFrame(wofsim.get_output())
df_results = df_results.set_index("day")
df_results.tail()
Visualizing simulation results
最后,我们可以使用MatPlotLib绘图软件包生成WOFOST变量的一些图形,such as the development (DVS), total biomass总生物量 (TAGP), leaf area index叶面积指数 (LAI) and root-zone soil moisture 根区土壤水分(SM) 。
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12,10))
for var, ax in zip(["DVS", "TAGP", "LAI", "SM"], axes.flatten()):
ax.plot_date(df_results.index, df_results[var], 'b-')
ax.set_title(var)
fig.autofmt_xdate()
总结
本期内容主要介绍了PCSE: The Python Crop Simulation Environment 利用自己的数据进行运行。
理论需实践,期待每一次的成长。下期见!
参考文献:
[1] https://pcse.readthedocs.io/en/stable/