Skip to content

Commit

Permalink
Merge pull request #42 from EcoExtreML/refactor_save_to_netcdf
Browse files Browse the repository at this point in the history
Refactor save to netcdf: change config.model to config_file
  • Loading branch information
SarahAlidoost authored Oct 25, 2022
2 parents 321e64d + b637b16 commit d34f8f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
15 changes: 8 additions & 7 deletions PyStemmusScope/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np
import pandas as pd
import xarray as xr
from PyStemmusScope import config_io
from PyStemmusScope import forcing_io
from . import variable_conversion as vc

Expand Down Expand Up @@ -250,18 +251,21 @@ def _update_dataset_attrs_dims(dataset: xr.Dataset, forcing_dict: Dict) -> xr.Da
return dataset


def to_netcdf(config: Dict, cf_filename: str) -> str:
def to_netcdf(config_file: str, cf_filename: str) -> str:
"""Save csv files generated by Stemmus_Scope model to a netcdf file using
information provided by ALMA conventions.
Args:
config(Dict): PyStemmusScope configuration dictionary.
config_file(str): Path to the config file.
cf_filename(str): Path to a csv file for ALMA conventions.
Returns:
str: path to a csv file under the output directory.
"""

# read config file
config = config_io.read_config(config_file)

# list of required forcing variables, Alma_short_name: forcing_io_name, # model_name
var_names = {
"RH": "rh", # RH
Expand All @@ -274,16 +278,13 @@ def to_netcdf(config: Dict, cf_filename: str) -> str:
"Precip": "precip_conv", # Pre
}

# Number of time steps from configuration file
time_steps = config["NumberOfTimeSteps"]

# read forcing file into a dict
forcing_dict = forcing_io.read_forcing_data(
Path(config["ForcingPath"]) / config["ForcingFileName"]
)

# get time info
time = _shorten_data_array(forcing_dict["time"], time_steps)
time = _shorten_data_array(forcing_dict["time"], config["NumberOfTimeSteps"])

# read convention file
conventions = pd.read_csv(cf_filename)
Expand All @@ -297,7 +298,7 @@ def to_netcdf(config: Dict, cf_filename: str) -> str:
if alma_name in var_names:
# select data
data_array = _select_forcing_variables(forcing_dict, var_names[alma_name], alma_name)
data_array = _shorten_data_array(data_array, time_steps)
data_array = _shorten_data_array(data_array, config["NumberOfTimeSteps"])

# create data array
elif alma_name in {"SoilTemp", "SoilMoist"}:
Expand Down
4 changes: 2 additions & 2 deletions notebooks/run_model_in_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"source": [
"# save output in netcdf format\n",
"required_netcdf_variables = \"./required_netcdf_variables.csv\"\n",
"nc_file_name = save.to_netcdf(model.config, required_netcdf_variables)\n",
"nc_file_name = save.to_netcdf(config_path, required_netcdf_variables)\n",
"print(nc_file_name)"
]
},
Expand Down Expand Up @@ -268,7 +268,7 @@
" print(result)\n",
" \n",
" # save results in a netcdf file\n",
" nc_file_name = save.to_netcdf(model.config, required_netcdf_variables)\n",
" nc_file_name = save.to_netcdf(config_path, required_netcdf_variables)\n",
" print(nc_file_name)\n",
" "
]
Expand Down
2 changes: 1 addition & 1 deletion notebooks/run_model_in_notebook_dev.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
],
"source": [
"required_netcdf_variables = path_to_model / \"utils\" / \"csv_to_nc\" / \"required_netcdf_variables.csv\"\n",
"nc_file_name = save.to_netcdf(model.config, required_netcdf_variables)\n",
"nc_file_name = save.to_netcdf(config_path, required_netcdf_variables)\n",
"print(nc_file_name)"
]
},
Expand Down
38 changes: 19 additions & 19 deletions tests/test_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def model_with_setup(self, tmp_path):
with patch("time.strftime") as mocked_time:
mocked_time.return_value = "2022-08-01-1200"

_ = model.setup(
config_path = model.setup(
WorkDir = str(tmp_path),
ForcingFileName = "dummy_forcing_file.nc",
NumberOfTimeSteps = "3", # less than forcing temporal range
)
return model
return model, config_path

def test_save_to_netcdf(self, cf_convention, model_with_setup):
model = model_with_setup
saved_nc_file = save.to_netcdf(model.config, cf_convention)
model, config_path = model_with_setup
saved_nc_file = save.to_netcdf(config_path, cf_convention)

expected_nc_file = (
"tests/test_data/directories/output/dummy-2022-08-01-1200/dummy-2022-08-01-1200_STEMMUS_SCOPE.nc"
Expand Down Expand Up @@ -110,16 +110,16 @@ def model_with_setup(self, tmp_path):
with patch("time.strftime") as mocked_time:
mocked_time.return_value = "2022-08-01-1200"

_ = model.setup(
config_path = model.setup(
WorkDir = str(tmp_path),
ForcingFileName = "dummy_forcing_file.nc",
NumberOfTimeSteps = "NA", # use temporal range of forcing data
)
return model
return model, config_path

@pytest.fixture(name="_make_csv_file")
def fixture_make_csv_file(self, model_with_setup):
model = model_with_setup
model, _ = model_with_setup
data = [
"simulation_number,year,DoY,Netlong",
",,,W m-2",
Expand All @@ -134,8 +134,8 @@ def fixture_make_csv_file(self, model_with_setup):
write_csv(data, csv_file)

def test_save_to_netcdf(self, cf_convention, _make_csv_file, model_with_setup):
model = model_with_setup
saved_nc_file = save.to_netcdf(model.config, cf_convention)
model, config_path = model_with_setup
saved_nc_file = save.to_netcdf(config_path, cf_convention)

expected_nc_file = (
"tests/test_data/directories/output/dummy-2022-08-01-1200/dummy-2022-08-01-1200_STEMMUS_SCOPE.nc"
Expand Down Expand Up @@ -193,16 +193,16 @@ def model_with_setup(self, tmp_path):
with patch("time.strftime") as mocked_time:
mocked_time.return_value = "2022-08-01-1200"

_ = model.setup(
config_path = model.setup(
WorkDir = str(tmp_path),
ForcingFileName = "dummy_forcing_file.nc",
NumberOfTimeSteps = "5",
)
return model
return model, config_path

@pytest.fixture(name="_make_csv_file")
def fixture_make_csv_file(self, model_with_setup):
model = model_with_setup
model, _ = model_with_setup
data = [
"1,2,3,5",
"1,1,1,2",
Expand All @@ -218,8 +218,8 @@ def fixture_make_csv_file(self, model_with_setup):
write_csv(data, csv_file)

def test_save_to_netcdf(self, cf_convention, _make_csv_file, model_with_setup):
model = model_with_setup
saved_nc_file = save.to_netcdf(model.config, cf_convention)
model, config_path = model_with_setup
saved_nc_file = save.to_netcdf(config_path, cf_convention)

expected_nc_file = (
"tests/test_data/directories/output/dummy-2022-08-01-1200/dummy-2022-08-01-1200_STEMMUS_SCOPE.nc"
Expand Down Expand Up @@ -282,16 +282,16 @@ def model_with_setup(self, tmp_path):
with patch("time.strftime") as mocked_time:
mocked_time.return_value = "2022-08-01-1200"

_ = model.setup(
config_path = model.setup(
WorkDir = str(tmp_path),
ForcingFileName = "dummy_forcing_file.nc",
NumberOfTimeSteps = "NA",
)
return model
return model, config_path

@pytest.fixture(name="_make_csv_file")
def fixture_make_csv_file(self, model_with_setup):
model = model_with_setup
model, _ = model_with_setup
data = [
"1,2,3,5",
"1,1,1,2",
Expand All @@ -318,8 +318,8 @@ def fixture_make_csv_file(self, model_with_setup):
write_csv(data, csv_file)

def test_save_to_netcdf(self, cf_convention, _make_csv_file, model_with_setup):
model = model_with_setup
saved_nc_file = save.to_netcdf(model.config, cf_convention)
model, config_path = model_with_setup
saved_nc_file = save.to_netcdf(config_path, cf_convention)

expected_nc_file = (
"tests/test_data/directories/output/dummy-2022-08-01-1200/dummy-2022-08-01-1200_STEMMUS_SCOPE.nc"
Expand Down

0 comments on commit d34f8f9

Please sign in to comment.