-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_wofost_phenology.py
423 lines (374 loc) · 15.3 KB
/
run_wofost_phenology.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""
Run WOFOST phenology for selected locations, years and genotypes to
predict the date of anthesis. The sowing date is estimated using the
approach proposed by Holzkaemper et al., 2015 using the earliest and
latest sowing dates for each location and year.
To speed up the simulations, the phenology is run in parallel for each
year. The number of parallel processes is defined by the number of
available CPUs minus 1. Thus, if you have 4 CPUs, 3 processes (i.e., 3 years)
will be run in parallel.
MIT License
Copyright (c) 2023 Lukas Valentin Graf, EOA-team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import geopandas as gpd
import logging
import multiprocessing as mp
import numpy as np
import pandas as pd
import pcse
from datetime import date, datetime
from pathlib import Path
from pcse.models import Wofost72_Phenology
from pcse.base import ParameterProvider
from pcse.exceptions import WeatherDataProviderError
from utils import get_agromanager, WeatherDataProvider_from_WeatherStation
# parameterization of the algorithm for sowing date estimation based
# on Holzkaemper et al., 2015
TMAX_SOWING = 12 # mean air temperature in deg C
PRECIP_THRESHOLDS = [20, 16, 12, 8, 4] # daily rainfall in mm
# setup logging
logger = logging.getLogger(__name__)
def estimate_sowing_date(
tmean: pd.Series,
daily_precip: pd.Series,
choice: int
) -> date:
"""
Winter Wheat sowing date estimation based on Holzkaemper et al., 2015
(https://doi.org/10.1007/s10113-014-0627-7) for a single point location.
:param tmean:
mean air temperature in deg Celsius
:param daily_precip:
daily precipitation in mm
:param choice:
in case there are multiple time windows fulfilling the criteria specify
which window to select as sowing date.
:returns:
estimated winter wheat sowing date index for the location
"""
df = pd.DataFrame({'tmean': tmean, 'daily_precip': daily_precip})
# restrict time window between October 1 (earliest sowing date October 7th)
# and November 7th
df = df.iloc[:37].copy()
# find period where tmean < 12 deg C for at least 6 consecutive days
temp_reduced = np.zeros(df.shape[0], dtype=bool)
for idx in range(6, df.shape[0]):
if df.tmean.iloc[idx - 6:idx].le(TMAX_SOWING).all():
temp_reduced[idx] = True
# and the amount of daily rainfall was <20, <16, <12, <8, <4
# for 5 consecutive days
precip_reduced = np.zeros(df.shape[0], dtype=bool)
for idx in range(5, df.shape[0]):
if (
df.daily_precip.iloc[idx - 5] < PRECIP_THRESHOLDS[0] and
df.daily_precip.iloc[idx - 4] < PRECIP_THRESHOLDS[1] and
df.daily_precip.iloc[idx - 3] < PRECIP_THRESHOLDS[2] and
df.daily_precip.iloc[idx - 2] < PRECIP_THRESHOLDS[3] and
df.daily_precip.iloc[idx - 1] < PRECIP_THRESHOLDS[4]
):
precip_reduced[idx] = True
matching_idx = np.where(temp_reduced & precip_reduced)[0]
# take the first matching index and use that date as the sowing date
dates = df.index
if len(matching_idx) == 0:
raise ValueError('No sowing date found')
if choice > 0:
# decrease positive choice numbers to be conform with Python
choice -= 1
if choice >= len(matching_idx):
# if there is no choice, set the sowing date index
# to -1 to indicate that there is no choice left
return -1
sowing_date_idx = dates[matching_idx[choice]]
return sowing_date_idx
def process_year(
year: int,
meteo: dict,
units: gpd.GeoDataFrame,
output_dir: Path,
genotypes: list[str],
fpath_tsum1_opt_dir: Path,
fpath_wheat_calendar: Path
) -> None:
"""
For each year and spatial unit, estimate the sowing date and run the
WOFOST phenology model using optimized TSUM1 parameters for the Swiss
winter wheat varieties Arina and CH_Claro.
:param year: year to process
:param meteo: dictionary with weather data for the year
:param units: GeoDataFrame with spatial units
:param output_dir: directory to store the results
:param genotypes: list of genotypes to run
:param fpath_tsum1_opt_dir: directory with optimized TSUM1 parameters
:param fpath_wheat_calendar: path to the wheat calendar file
"""
logger.info(f'Working on growing season {year} - {year+1}')
fpath_out = output_dir.joinpath(f'results_ww_gs_{year}-{year+1}.gpkg')
if fpath_out.exists():
logger.info('Results exist already -> skipping')
return
date_start = date(year, 10, 1)
date_end = date(year+1, 9, 30)
dates = pd.date_range(date_start, date_end)
# prepare weather data of the year
meteo_year = {}
for variable in meteo.keys():
meteo_year[variable] = meteo[variable][
(meteo[variable].date >= date_start) &
(meteo[variable].date <= date_end)
].copy()
# estimate the sowing date per spatial unit
yearly_results_list = []
for _, unit in units.iterrows():
meteo_year_unit = {}
try:
for variable in meteo_year.keys():
meteo_year_unit[variable] = \
meteo_year[variable][str(unit.eodal_id)].copy()
except Exception as e:
logger.error(f'{unit.NAME}: {e}')
continue
# for the sowing date, the daily mean air temperature is required
tmean = (
meteo_year_unit['TmaxD'].values +
meteo_year_unit['TminD'].values) * 0.5
tmean = pd.Series(tmean)
daily_precip = meteo_year_unit['RhiresD'].values
daily_precip = pd.Series(daily_precip)
# first (earliest) possible sowing date
try:
sowing_date_idx_early = estimate_sowing_date(
tmean=tmean,
daily_precip=daily_precip,
choice=1
)
sowing_date_early = dates[sowing_date_idx_early]
except ValueError:
logger.info(
f'{unit.eodal_id} {year}: No sowing date found. ' +
'Using Default.')
sowing_date_early = datetime(year, 10, 7)
# last (latest) possible sowing date
try:
sowing_date_idx_late = estimate_sowing_date(
tmean=tmean,
daily_precip=daily_precip,
choice=-1
)
sowing_date_late = dates[sowing_date_idx_late]
except ValueError:
logger.info(
f'{unit.eodal_id} {year}: No sowing date found. ' +
'Using Default.')
# use default late sowing date (7th ofNovember)
sowing_date_late = datetime(year, 11, 7)
# prepare weather data for WOFOST
weather = pd.DataFrame({
'date': list(dates),
'T_min': meteo_year_unit['TminD'].values,
'T_max': meteo_year_unit['TmaxD'].values
})
# get longitude and latitude
unit_gdf = gpd.GeoDataFrame(
[unit], crs=units.crs)
unit_gdf.geometry = unit_gdf.geometry.centroid
unit_gdf.to_crs(epsg=4326, inplace=True)
longitude = unit_gdf.geometry.x.values[0]
latitude = unit_gdf.geometry.y.values[0]
# use the true elevation if available
if 'Elevation' in unit_gdf.columns:
elevation = unit_gdf.Elevation.values[0]
# else set elevation to dummy value
else:
elevation = 450
# setup agrar manager and weather data provider for WOFOST
agromanager_early = get_agromanager(
sowing_date=sowing_date_early,
fpath_wheat_calender=fpath_wheat_calendar
)['AgroManagement']
agromanager_late = get_agromanager(
sowing_date=sowing_date_late,
fpath_wheat_calender=fpath_wheat_calendar
)['AgroManagement']
wdp = WeatherDataProvider_from_WeatherStation(
weather_data=weather,
elevation=elevation,
lon=longitude,
lat=latitude
)
# loop over genotypes and calculate the date of anthesis
for genotype in genotypes:
with open(
fpath_tsum1_opt_dir.joinpath(f'{genotype.lower()}.txt')
) as src:
tsum1_opt = float(src.read())
cropd = pcse.fileinput.YAMLCropDataProvider()
cropd.set_active_crop('wheat', 'Winter_wheat_105')
cropdata = cropd.copy()
cropdata['TSUM1'] = tsum1_opt
# set the parameters
parameters = ParameterProvider(
cropdata=cropdata
)
# WOFOST72 Phenology model early sowing date
wofsim_early = Wofost72_Phenology(
parameters, wdp, agromanager_early)
# run till terminate
try:
wofsim_early.run_till_terminate()
except WeatherDataProviderError as e:
logger.error(f'{unit.NAME}: {e}')
continue
summary_early = wofsim_early.get_summary_output()
# late sowing date
wofsim_late = Wofost72_Phenology(
parameters, wdp, agromanager_late)
# run till terminate
try:
wofsim_late.run_till_terminate()
except WeatherDataProviderError as e:
logger.error(f'{unit.NAME}: {e}')
continue
summary_late = wofsim_late.get_summary_output()
# save results in compact format
res_early_sowing = {
'id': unit.eodal_id,
'harvest_year': year + 1,
'sowing_date': sowing_date_early.date().strftime(
'%Y-%m-%d'),
'sowing_date_type': 'earliest',
'genotype': genotype,
'emergence_date': summary_early[0]['DOE'].strftime(
'%Y-%m-%d'),
'anthesis_date': summary_early[0]['DOA'].strftime(
'%Y-%m-%d'),
'crop_land_area_km2': unit.crop_land_km2,
'crop_land_area_perc': unit.crop_land_perc,
'geometry': unit.geometry
}
res_late_sowing = {
'id': unit.eodal_id,
'harvest_year': year + 1,
'sowing_date': sowing_date_late.date().strftime(
'%Y-%m-%d'),
'sowing_date_type': 'latest',
'genotype': genotype,
'emergence_date': summary_late[0]['DOE'].strftime(
'%Y-%m-%d'),
'anthesis_date': summary_late[0]['DOA'].strftime(
'%Y-%m-%d'),
'crop_land_area_km2': unit.crop_land_km2,
'crop_land_area_perc': unit.crop_land_perc,
'geometry': unit.geometry
}
yearly_results_list.append(res_early_sowing)
yearly_results_list.append(res_late_sowing)
# concatenate yearly results
yearly_results_df = pd.DataFrame(yearly_results_list)
yearly_results_gdf = gpd.GeoDataFrame(
yearly_results_df,
geometry=yearly_results_df.geometry,
crs=units.crs
)
yearly_results_gdf.to_file(fpath_out, driver='GPKG')
logger.info(f'Finished growing season {year} - {year+1}')
def run(
spatial_units: Path | gpd.GeoDataFrame,
input_data_dir: Path,
output_dir: Path,
years: list[int],
genotypes: list[str],
fpath_wheat_calendar: Path,
fpath_tsum1_opt_dir: Path
) -> None:
"""
Run the WOFOST phenology model for the selected locations and
years.
:param spatial_units:
GeoDataFrame with the selected spatial units (locations)
:param input_data_dir:
directory with the input meteorological data
:param output_dir:
directory where the results should be stored
:param years:
list of years to run the model for.
:param genotypes:
list of genotypes to run the model for.
:param fpath_wheat_calendar:
path to the wheat calendar file
:param fpath_tsum1_opt_dir:
path to the directory with the optimized TSUM1 parameters
per genotype.
"""
# read unitality data
if isinstance(spatial_units, Path):
units = gpd.read_file(spatial_units)
elif isinstance(spatial_units, gpd.GeoDataFrame):
units = spatial_units.copy()
else:
raise TypeError(
'spatial units must be either a Path or a GeoDataFrame')
# read the weather data
meteo = {}
for fpath_meteo in input_data_dir.glob('*.csv'):
variable = fpath_meteo.name.split('_')[0]
meteo_df = pd.read_csv(fpath_meteo, index_col=0)
meteo_df['date'] = pd.to_datetime(meteo_df.date).dt.date
meteo[variable] = meteo_df
# loop through the years using a multiprocessing pool
with mp.Pool(processes=mp.cpu_count()-1) as pool:
pool.starmap(
process_year,
zip(years,
[meteo] * len(years),
[units] * len(years),
[output_dir] * len(years),
[genotypes] * len(years),
[fpath_tsum1_opt_dir] * len(years),
[fpath_wheat_calendar] * len(years)))
if __name__ == '__main__':
# fix working directory for all IDEs
cwd = Path(__file__).absolute().parent
# set paths
# default winter wheat calendar required by WOFOST
fpath_wheat_calendar = cwd.joinpath('wheat_calendar.txt')
# directory with the optimized TSUM1 parameters
fpath_tsum1_opt_dir = cwd.joinpath('genotypes_Tsum1_opt')
# input data directory with meteorological data.
# You will need git's large file storage (lfs) to download the data
# from the repository (on git clone)
input_data_dir = cwd.parent.joinpath('meteo_data')
# grid cells to run the model for (GeoPackage file)
spatial_units = cwd.joinpath('spatial_units.gpkg')
# set years to run the model for
years = list(range(1971, 2020))
# set genotypes
genotypes = ['Arina', 'CH_Claro']
# set output directory. The results will be stored in a
# GeoPackage file per year.
output_dir = cwd.parent.joinpath('results')
# run the model
run(
spatial_units=spatial_units,
input_data_dir=input_data_dir,
output_dir=output_dir,
years=years,
genotypes=genotypes,
fpath_wheat_calendar=fpath_wheat_calendar,
fpath_tsum1_opt_dir=fpath_tsum1_opt_dir
)