-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test effect of different RDMSOL values
- Loading branch information
1 parent
2b142a1
commit df2af34
Showing
7 changed files
with
187 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
Analyse a PCSE ensemble generated by rdmsol_generate.py. | ||
Example: | ||
%run experiments/rdmsol_analyse.py outputs/rdmsol -v -c barley | ||
""" | ||
from pathlib import Path | ||
|
||
from matplotlib import pyplot as plt | ||
from tqdm import tqdm | ||
|
||
import fpcup | ||
|
||
# Parse command line arguments | ||
import argparse | ||
parser = argparse.ArgumentParser(description="Run a PCSE ensemble for multiple location with one sowing date.") | ||
parser.add_argument("output_dir", help="folder to load PCSE outputs from", type=Path) | ||
parser.add_argument("-c", "--crop", help="crop to run simulations on", default="barley", choices=("barley", "maize", "sorghum", "soy", "wheat"), type=str.lower) | ||
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") | ||
args = parser.parse_args() | ||
|
||
# Load the ensemble summary | ||
summary = fpcup.io.load_ensemble_summary_from_folder(args.output_dir) | ||
|
||
# Get the associated RDMSOLs | ||
rdmsol_from_run_id = lambda run_id: int(run_id.split("RDMSOL-")[1].split("_")[0]) | ||
summary["RDMSOL"] = summary.index.to_series().apply(rdmsol_from_run_id) | ||
summary.sort_values("RDMSOL", inplace=True) | ||
|
||
# Plot summary outputs as a function of RDMSOL for each soiltype and site | ||
summary_outputs = ("RD", "DOM", "LAIMAX", "TWSO") | ||
|
||
fig, axs = plt.subplots(nrows=len(summary_outputs), ncols=3, sharex=True, sharey="row", figsize=(10, 10), squeeze=False) | ||
|
||
# Loop over the columns (soil types) first | ||
for ax_col, (soiltype, summary_by_soiltype) in zip(axs.T, summary.groupby("soiltype")): | ||
ax_col[0].set_title(f"Soil type: {soiltype}") | ||
|
||
# Loop over the rows (summary outputs) next | ||
for ax, key in zip(ax_col, summary_outputs): | ||
# Plot a line for each site | ||
summary_by_soiltype.groupby("geometry").plot.line("RDMSOL", key, ax=ax, alpha=0.5, legend=False) | ||
|
||
axs[0, 0].set_xlim(0, 150) | ||
for ax, key in zip(axs[:, 0], summary_outputs): | ||
ax.set_ylabel(key) | ||
|
||
fig.align_xlabels() | ||
fig.align_ylabels() | ||
|
||
plt.show() | ||
plt.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
""" | ||
Run a PCSE ensemble for different values of RDMSOL (maximum rooting depth) to determine its effect. | ||
All available soil types are tested. | ||
A single site, roughly central to the Netherlands, is used. | ||
Example: | ||
%run experiments/rdmsol_generate.py -v -c barley -n 9 | ||
""" | ||
from pathlib import Path | ||
|
||
from tqdm import tqdm | ||
|
||
import fpcup | ||
|
||
# Parse command line arguments | ||
import argparse | ||
parser = argparse.ArgumentParser(description="Run a PCSE ensemble for different values of RDMSOL (maximum rooting depth) to determine its effect.") | ||
parser.add_argument("-d", "--data_dir", help="folder to load PCSE data from", type=Path, default=fpcup.settings.DEFAULT_DATA) | ||
parser.add_argument("-o", "--output_dir", help="folder to save PCSE outputs to", type=Path, default=fpcup.settings.DEFAULT_OUTPUT / "rdmsol") | ||
parser.add_argument("-c", "--crop", help="crop to run simulations on", default="barley", choices=("barley", "maize", "sorghum", "soy", "wheat"), type=str.lower) | ||
parser.add_argument("-n", "--number", help="number of locations; result may be lower due to rounding", type=int, default=25) | ||
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") | ||
args = parser.parse_args() | ||
|
||
# Constants | ||
YEAR = 2022 | ||
RDMSOLs = list(range(10, 150, 1)) | ||
|
||
# Generate the coordinates | ||
crs = fpcup.constants.WGS84 | ||
coords = fpcup.site.generate_sites_space(latitude=(51, 53), longitude=(4, 6), n=args.number) | ||
if args.verbose: | ||
print(f"Generated {len(coords)} coordinates") | ||
|
||
### Generate constants | ||
# Crop data | ||
cropdata = fpcup.crop.default | ||
if args.verbose: | ||
print("Loaded crop data") | ||
|
||
# Agromanagement calendars | ||
sowdate = fpcup.agro.sowdate_range(args.crop, YEAR)[0] | ||
agromanagement = fpcup.agro.load_agrotemplate(args.crop, sowdate=sowdate) | ||
if args.verbose: | ||
print("Loaded agro management data:") | ||
print(agromanagement) | ||
|
||
### Loop over sites (coordinates) | ||
failed_runs = [] | ||
for c in tqdm(coords, desc="Sites", unit="site", leave=args.verbose): | ||
### Generate site-specific data | ||
# Get site data | ||
sitedata = fpcup.site.example(c) | ||
|
||
# Get weather data | ||
weatherdata = fpcup.weather.load_weather_data_NASAPower(c) | ||
|
||
### Loop over soil data and depths | ||
for soildata in tqdm(fpcup.soil.soil_types.values(), desc="Soil types", unit="type", leave=False, disable=fpcup.RUNNING_IN_IPYTHON): | ||
for depth in tqdm(RDMSOLs, desc="Running models", unit="RDMSOL", leave=False, disable=fpcup.RUNNING_IN_IPYTHON): | ||
soildata["RDMSOL"] = depth | ||
run_id = f"{args.crop}_{soildata.name}_RDMSOL-{depth:d}_dos{sowdate:%Y%j}_lat{c[0]:.7f}-lon{c[1]:.7f}" | ||
|
||
# Combine input data | ||
run = fpcup.model.RunData(sitedata=sitedata, soildata=soildata, cropdata=cropdata, weatherdata=weatherdata, agromanagement=agromanagement, geometry=c, crs=crs, run_id=run_id) | ||
|
||
# Run model | ||
output = fpcup.model.run_pcse_single(run) | ||
|
||
# Save the results to file | ||
try: | ||
output.to_file(args.output_dir) | ||
# If the run failed, saving to file will also fail, so we instead note that this run failed | ||
except AttributeError: | ||
failed_runs.append(run_id) | ||
|
||
# Feedback on failed runs: if any failed, let the user know. If none failed, only let the user know in verbose mode. | ||
print() | ||
if len(failed_runs) > 0: | ||
print(f"Number of failed runs: {len(failed_runs)}/{len(coords)}") | ||
else: | ||
if args.verbose: | ||
print("No runs failed.") | ||
|
||
# Save an ensemble summary | ||
fpcup.io.save_ensemble_summary(args.output_dir, verbose=args.verbose) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import aggregate, constants, geo, io, plotting, settings | ||
from . import agro, crop, site, soil, weather | ||
from .model import run_pcse_single, run_pcse_ensemble | ||
from .tools import RUNNING_IN_IPYTHON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters