Skip to content

Commit

Permalink
Change API for PCRGlobWB model class (#85)
Browse files Browse the repository at this point in the history
* Add PCRGlobWBParameterSet

* Change constructor and setup methods

* Select container based on version argument

* Update notebook and make it work

* Fix absolute paths for additional input data

* update notebook text cells

* Remove container locations from config

* Formatting etc.

* Add (optional) forcing class to constructor

* Add warnings to conda env

* Revert "Add warnings to conda env"

This reverts commit a9cc9d3.

* Switch to using new forcing module

* Docstring for forcing

* Make versions a tuple

* Use docker:// for singularity image path

* Forcing files relative to forcing dir

Co-authored-by: Stefan Verhoeven <[email protected]>

* Update forcing paths in setup of default config and document

* Reduce number of configurable parameters

* Add __str__ methods on the forcing and parametersets

* Catch timeout on singularity spawner

* Remove additional_input_dirs option (and add black formattting)

* Catch all futuretimeouts for starting containers

Co-authored-by: Stefan Verhoeven <[email protected]>
  • Loading branch information
Peter9192 and sverhoeven authored Jun 21, 2021
1 parent a7c8d3a commit 937311d
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 411 deletions.
2 changes: 0 additions & 2 deletions ewatercycle/config/ewatercycle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ grdc_location: null
container_engine: null
singularity_dir: null
output_dir: null
pcrglobwb.singularity_image: null
pcrglobwb.docker_image: null
92 changes: 59 additions & 33 deletions ewatercycle/forcing/_pcrglobwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@

class PCRGlobWBForcing(DefaultForcing):
"""Container for pcrglobwb forcing data."""

def __init__(
self,
start_time: str,
end_time: str,
directory: str,
shape: Optional[str] = None,
precipitationNC: Optional[str] = 'precipitation.nc',
temperatureNC: Optional[str] = 'temperature.nc',
precipitationNC: Optional[str] = "precipitation.nc",
temperatureNC: Optional[str] = "temperature.nc",
):
"""
precipitationNC (str): Input file for precipitation data.
temperatureNC (str): Input file for temperature data.
precipitationNC (str): Input file for precipitation data.
temperatureNC (str): Input file for temperature data.
"""
super().__init__(start_time, end_time, directory, shape)
self.precipitationNC = precipitationNC
Expand All @@ -37,71 +38,96 @@ def generate( # type: ignore
end_time: str,
shape: str,
start_time_climatology: str, # TODO make optional, default to start_time
end_time_climatology:
str, # TODO make optional, defaults to start_time + 1 year
end_time_climatology: str, # TODO make optional, defaults to start_time + 1 year
extract_region: dict = None,
) -> 'PCRGlobWBForcing':
) -> "PCRGlobWBForcing":
"""
start_time_climatology (str): Start time for the climatology data
end_time_climatology (str): End time for the climatology data
extract_region (dict): Region specification, dictionary must
contain `start_longitude`, `end_longitude`, `start_latitude`,
`end_latitude`
start_time_climatology (str): Start time for the climatology data
end_time_climatology (str): End time for the climatology data
extract_region (dict): Region specification, dictionary must
contain `start_longitude`, `end_longitude`, `start_latitude`,
`end_latitude`
"""
# load the ESMValTool recipe
recipe_name = "hydrology/recipe_pcrglobwb.yml"
recipe = get_recipe(recipe_name)

# model-specific updates to the recipe
preproc_names = ('crop_basin', 'preproc_pr', 'preproc_tas',
'preproc_pr_clim', 'preproc_tas_clim')
preproc_names = (
"crop_basin",
"preproc_pr",
"preproc_tas",
"preproc_pr_clim",
"preproc_tas_clim",
)

if dataset is not None:
recipe.data['diagnostics']['diagnostic_daily'][
'additional_datasets'] = [DATASETS[dataset]]
recipe.data["diagnostics"]["diagnostic_daily"][
"additional_datasets"
] = [DATASETS[dataset]]

basin = Path(shape).stem
recipe.data['diagnostics']['diagnostic_daily']['scripts']['script'][
'basin'] = basin
recipe.data["diagnostics"]["diagnostic_daily"]["scripts"]["script"][
"basin"
] = basin

if extract_region is None:
extract_region = get_extents(shape)
for preproc_name in preproc_names:
recipe.data['preprocessors'][preproc_name][
'extract_region'] = extract_region
recipe.data["preprocessors"][preproc_name][
"extract_region"
] = extract_region

variables = recipe.data['diagnostics']['diagnostic_daily']['variables']
var_names = 'tas', 'pr'
variables = recipe.data["diagnostics"]["diagnostic_daily"]["variables"]
var_names = "tas", "pr"

startyear = get_time(start_time).year
for var_name in var_names:
variables[var_name]['start_year'] = startyear
variables[var_name]["start_year"] = startyear

endyear = get_time(end_time).year
for var_name in var_names:
variables[var_name]['end_year'] = endyear
variables[var_name]["end_year"] = endyear

var_names_climatology = 'pr_climatology', 'tas_climatology'
var_names_climatology = "pr_climatology", "tas_climatology"

startyear_climatology = get_time(start_time_climatology)
for var_name in var_names_climatology:
variables[var_name]['start_year'] = startyear_climatology
variables[var_name]["start_year"] = startyear_climatology

endyear_climatology = get_time(end_time_climatology)
for var_name in var_names_climatology:
variables[var_name]['end_year'] = endyear_climatology
variables[var_name]["end_year"] = endyear_climatology

# generate forcing data and retrieve useful information
recipe_output = recipe.run()
# TODO dont open recipe output files, but use standard name from ESMValTool diagnostic
directory, forcing_files = data_files_from_recipe_output(recipe_output)

# instantiate forcing object based on generated data
return PCRGlobWBForcing(directory=directory,
start_time=start_time,
end_time=end_time,
precipitationNC=forcing_files['pr'],
temperatureNC=forcing_files['tas'])
return PCRGlobWBForcing(
directory=directory,
start_time=start_time,
end_time=end_time,
precipitationNC=forcing_files["pr"],
temperatureNC=forcing_files["tas"],
)

def plot(self):
raise NotImplementedError('Dont know how to plot')
raise NotImplementedError("Dont know how to plot")

def __str__(self):
"""Nice formatting of the forcing data object."""
return "\n".join(
[
"Forcing data for PCRGlobWB",
"--------------------------",
f"Directory: {self.directory}",
f"Start time: {self.start_time}",
f"End time: {self.end_time}",
f"Shapefile: {self.shape}",
f"Additional information for model config:",
f" - temperatureNC: {self.temperatureNC}",
f" - precipitationNC: {self.precipitationNC}",
]
)
Loading

0 comments on commit 937311d

Please sign in to comment.