Skip to content

Commit d34f8f9

Browse files
Merge pull request #42 from EcoExtreML/refactor_save_to_netcdf
Refactor save to netcdf: change config.model to config_file
2 parents 321e64d + b637b16 commit d34f8f9

4 files changed

Lines changed: 30 additions & 29 deletions

File tree

PyStemmusScope/save.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import numpy as np
2525
import pandas as pd
2626
import xarray as xr
27+
from PyStemmusScope import config_io
2728
from PyStemmusScope import forcing_io
2829
from . import variable_conversion as vc
2930

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

252253

253-
def to_netcdf(config: Dict, cf_filename: str) -> str:
254+
def to_netcdf(config_file: str, cf_filename: str) -> str:
254255
"""Save csv files generated by Stemmus_Scope model to a netcdf file using
255256
information provided by ALMA conventions.
256257
257258
Args:
258-
config(Dict): PyStemmusScope configuration dictionary.
259+
config_file(str): Path to the config file.
259260
cf_filename(str): Path to a csv file for ALMA conventions.
260261
261262
Returns:
262263
str: path to a csv file under the output directory.
263264
"""
264265

266+
# read config file
267+
config = config_io.read_config(config_file)
268+
265269
# list of required forcing variables, Alma_short_name: forcing_io_name, # model_name
266270
var_names = {
267271
"RH": "rh", # RH
@@ -274,16 +278,13 @@ def to_netcdf(config: Dict, cf_filename: str) -> str:
274278
"Precip": "precip_conv", # Pre
275279
}
276280

277-
# Number of time steps from configuration file
278-
time_steps = config["NumberOfTimeSteps"]
279-
280281
# read forcing file into a dict
281282
forcing_dict = forcing_io.read_forcing_data(
282283
Path(config["ForcingPath"]) / config["ForcingFileName"]
283284
)
284285

285286
# get time info
286-
time = _shorten_data_array(forcing_dict["time"], time_steps)
287+
time = _shorten_data_array(forcing_dict["time"], config["NumberOfTimeSteps"])
287288

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

302303
# create data array
303304
elif alma_name in {"SoilTemp", "SoilMoist"}:

notebooks/run_model_in_notebook.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
"source": [
218218
"# save output in netcdf format\n",
219219
"required_netcdf_variables = \"./required_netcdf_variables.csv\"\n",
220-
"nc_file_name = save.to_netcdf(model.config, required_netcdf_variables)\n",
220+
"nc_file_name = save.to_netcdf(config_path, required_netcdf_variables)\n",
221221
"print(nc_file_name)"
222222
]
223223
},
@@ -268,7 +268,7 @@
268268
" print(result)\n",
269269
" \n",
270270
" # save results in a netcdf file\n",
271-
" nc_file_name = save.to_netcdf(model.config, required_netcdf_variables)\n",
271+
" nc_file_name = save.to_netcdf(config_path, required_netcdf_variables)\n",
272272
" print(nc_file_name)\n",
273273
" "
274274
]

notebooks/run_model_in_notebook_dev.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
],
236236
"source": [
237237
"required_netcdf_variables = path_to_model / \"utils\" / \"csv_to_nc\" / \"required_netcdf_variables.csv\"\n",
238-
"nc_file_name = save.to_netcdf(model.config, required_netcdf_variables)\n",
238+
"nc_file_name = save.to_netcdf(config_path, required_netcdf_variables)\n",
239239
"print(nc_file_name)"
240240
]
241241
},

tests/test_save.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ def model_with_setup(self, tmp_path):
4545
with patch("time.strftime") as mocked_time:
4646
mocked_time.return_value = "2022-08-01-1200"
4747

48-
_ = model.setup(
48+
config_path = model.setup(
4949
WorkDir = str(tmp_path),
5050
ForcingFileName = "dummy_forcing_file.nc",
5151
NumberOfTimeSteps = "3", # less than forcing temporal range
5252
)
53-
return model
53+
return model, config_path
5454

5555
def test_save_to_netcdf(self, cf_convention, model_with_setup):
56-
model = model_with_setup
57-
saved_nc_file = save.to_netcdf(model.config, cf_convention)
56+
model, config_path = model_with_setup
57+
saved_nc_file = save.to_netcdf(config_path, cf_convention)
5858

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

113-
_ = model.setup(
113+
config_path = model.setup(
114114
WorkDir = str(tmp_path),
115115
ForcingFileName = "dummy_forcing_file.nc",
116116
NumberOfTimeSteps = "NA", # use temporal range of forcing data
117117
)
118-
return model
118+
return model, config_path
119119

120120
@pytest.fixture(name="_make_csv_file")
121121
def fixture_make_csv_file(self, model_with_setup):
122-
model = model_with_setup
122+
model, _ = model_with_setup
123123
data = [
124124
"simulation_number,year,DoY,Netlong",
125125
",,,W m-2",
@@ -134,8 +134,8 @@ def fixture_make_csv_file(self, model_with_setup):
134134
write_csv(data, csv_file)
135135

136136
def test_save_to_netcdf(self, cf_convention, _make_csv_file, model_with_setup):
137-
model = model_with_setup
138-
saved_nc_file = save.to_netcdf(model.config, cf_convention)
137+
model, config_path = model_with_setup
138+
saved_nc_file = save.to_netcdf(config_path, cf_convention)
139139

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

196-
_ = model.setup(
196+
config_path = model.setup(
197197
WorkDir = str(tmp_path),
198198
ForcingFileName = "dummy_forcing_file.nc",
199199
NumberOfTimeSteps = "5",
200200
)
201-
return model
201+
return model, config_path
202202

203203
@pytest.fixture(name="_make_csv_file")
204204
def fixture_make_csv_file(self, model_with_setup):
205-
model = model_with_setup
205+
model, _ = model_with_setup
206206
data = [
207207
"1,2,3,5",
208208
"1,1,1,2",
@@ -218,8 +218,8 @@ def fixture_make_csv_file(self, model_with_setup):
218218
write_csv(data, csv_file)
219219

220220
def test_save_to_netcdf(self, cf_convention, _make_csv_file, model_with_setup):
221-
model = model_with_setup
222-
saved_nc_file = save.to_netcdf(model.config, cf_convention)
221+
model, config_path = model_with_setup
222+
saved_nc_file = save.to_netcdf(config_path, cf_convention)
223223

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

285-
_ = model.setup(
285+
config_path = model.setup(
286286
WorkDir = str(tmp_path),
287287
ForcingFileName = "dummy_forcing_file.nc",
288288
NumberOfTimeSteps = "NA",
289289
)
290-
return model
290+
return model, config_path
291291

292292
@pytest.fixture(name="_make_csv_file")
293293
def fixture_make_csv_file(self, model_with_setup):
294-
model = model_with_setup
294+
model, _ = model_with_setup
295295
data = [
296296
"1,2,3,5",
297297
"1,1,1,2",
@@ -318,8 +318,8 @@ def fixture_make_csv_file(self, model_with_setup):
318318
write_csv(data, csv_file)
319319

320320
def test_save_to_netcdf(self, cf_convention, _make_csv_file, model_with_setup):
321-
model = model_with_setup
322-
saved_nc_file = save.to_netcdf(model.config, cf_convention)
321+
model, config_path = model_with_setup
322+
saved_nc_file = save.to_netcdf(config_path, cf_convention)
323323

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

0 commit comments

Comments
 (0)