Skip to content

Commit c50329a

Browse files
committed
rename Variables_will_be_in_NetCDF_file, rename Variable name in STEMMUS-SCOPE and File name, define dataset attrs as a global var
1 parent c936fa3 commit c50329a

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

PyStemmusScope/save.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
the output directory.
77
88
The file
9-
`Variables_will_be_in_NetCDF_file.csv <https://github.com/EcoExtreML/STEMMUS_SCOPE/blob/main/utils/csv_to_nc/Variables_will_be_in_NetCDF_file.csv>`_
9+
`required_netcf_variables.csv <https://github.com/EcoExtreML/STEMMUS_SCOPE/blob/main/utils/csv_to_nc/required_netcf_variables.csv>`_
1010
lists required variable names and their attributes based on `ALMA+CF
1111
convention table <https://docs.google.com/spreadsheets/d/1CA3aTvI9piXqRqO-3MGrsH1vW-Sd87D8iZXHGrqK42o/edit#gid=2085475627>`_.
1212
@@ -18,7 +18,7 @@
1818
>>>model = StemmusScope(config_file, exe_file)
1919
>>>model.setup()
2020
>>>model.run()
21-
>>>save.to_netcdf(model.config, "Variables_will_be_in_NetCDF_file.csv")
21+
>>>save.to_netcdf(model.config, "required_netcf_variables.csv")
2222
2323
"""
2424

@@ -36,6 +36,18 @@
3636

3737
logger = logging.getLogger(__name__)
3838

39+
DATASET_ATTRS = {
40+
'model': 'STEMMUS_SCOPE',
41+
'institution': 'University of Twente; Northwest A&F University',
42+
'contact': (
43+
'Zhongbo Su, z.su@utwente.nl; '
44+
'Yijian Zeng, y.zeng@utwente.nl; '
45+
'Yunfei Wang, y.wang-3@utwente.nl'
46+
),
47+
'license_type': 'CC BY 4.0',
48+
'license_url': 'https://creativecommons.org/licenses/by/4.0/',
49+
}
50+
3951

4052
def _select_forcing_variables(forcing_dict: Dict, forcing_var: str, alma_var: str) -> xr.DataArray:
4153
"""Select the variable needed by ALMA convention.
@@ -218,19 +230,9 @@ def _update_dataset_attrs_dims(dataset: xr.Dataset, forcing_dict: Dict) -> xr.Da
218230
# additional metadata
219231
lat = forcing_dict["latitude"]
220232
lon = forcing_dict["longitude"]
221-
dataset_reordered.attrs = {
222-
'model': 'STEMMUS_SCOPE',
223-
'institution': 'University of Twente; Northwest A&F University',
224-
'contact': (
225-
'Zhongbo Su, z.su@utwente.nl; '
226-
'Yijian Zeng, y.zeng@utwente.nl; '
227-
'Yunfei Wang, y.wang-3@utwente.nl'
228-
),
229-
'license_type': 'CC BY 4.0',
230-
'license_url': 'https://creativecommons.org/licenses/by/4.0/',
231-
'latitude': lat,
232-
'longitude': lon,
233-
}
233+
dataset_reordered.attrs = DATASET_ATTRS
234+
dataset_reordered.attrs['latitude'] = lat
235+
dataset_reordered.attrs['longitude'] = lon
234236

235237
# update values of x and y coords
236238
dataset = dataset_reordered.assign_coords(
@@ -298,7 +300,7 @@ def to_netcdf(config: Dict, cf_filename: str) -> str:
298300
data_list = []
299301
for alma_name in alma_short_names:
300302
df = conventions.loc[alma_short_names == alma_name].iloc[0]
301-
file_name = Path(config["OutputPath"]) / df["File name"]
303+
file_name = Path(config["OutputPath"]) / df["file_name_STEMMUS-SCOPE"]
302304

303305
if alma_name in var_names:
304306
# select data
@@ -310,15 +312,15 @@ def to_netcdf(config: Dict, cf_filename: str) -> str:
310312
data_array = _prepare_soil_data(file_name, alma_name, time.values)
311313
else:
312314
data_array = _prepare_simulated_data(
313-
file_name, df["Variable name in STEMMUS-SCOPE"], alma_name, time.values
315+
file_name, df["short_name_STEMMUS-SCOPE"], alma_name, time.values
314316
)
315317

316318
# update attributes of array
317319
data_array.attrs = {
318320
"units": df["unit"],
319321
"long_name": df["long_name"],
320322
"standard_name": df["standard_name"],
321-
"STEMMUS-SCOPE_name": df["Variable name in STEMMUS-SCOPE"],
323+
"STEMMUS-SCOPE_name": df["short_name_STEMMUS-SCOPE"],
322324
"definition": df["definition"],
323325
}
324326

tests/test_save.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TestSaveForcingData:
2828
@pytest.fixture
2929
def cf_convention(self, tmp_path):
3030
convention = [
31-
"short_name_alma,standard_name,long_name,definition,unit,File name,Variable name in STEMMUS-SCOPE" ,
31+
"short_name_alma,standard_name,long_name,definition,unit,file_name_STEMMUS-SCOPE,short_name_STEMMUS-SCOPE" ,
3232
"LWdown_ec,surface_downwelling_longwave_flux_in_air,Downward long-wave radiation,,W/m2,ECdata.csv,Rli"
3333
]
3434

@@ -89,7 +89,7 @@ class TestSaveSimulatedData:
8989
@pytest.fixture
9090
def cf_convention(self, tmp_path):
9191
convention = [
92-
'short_name_alma,standard_name,long_name,definition,unit,File name,Variable name in STEMMUS-SCOPE',
92+
'short_name_alma,standard_name,long_name,definition,unit,file_name_STEMMUS-SCOPE,short_name_STEMMUS-SCOPE',
9393
(
9494
'LWnet,surface_net_downward_longwave_flux,Net longwave radiation,'
9595
'"Incident longwave radiation less the simulated outgoing longwave radiation, '
@@ -171,7 +171,7 @@ class TestSoilData:
171171
@pytest.fixture
172172
def cf_convention(self, tmp_path):
173173
convention = [
174-
'short_name_alma,standard_name,long_name,definition,unit,File name,Variable name in STEMMUS-SCOPE',
174+
'short_name_alma,standard_name,long_name,definition,unit,file_name_STEMMUS-SCOPE,short_name_STEMMUS-SCOPE',
175175
(
176176
'SoilMoist,moisture_content_of_soil_layer,Average layer soil moisture,'
177177
'"Soil water content in each user-defined soil layer (3D variable). '
@@ -254,7 +254,7 @@ class TestSaveToNetcdf:
254254
@pytest.fixture
255255
def cf_convention(self, tmp_path):
256256
convention = [
257-
'short_name_alma,standard_name,long_name,definition,unit,File name,Variable name in STEMMUS-SCOPE',
257+
'short_name_alma,standard_name,long_name,definition,unit,file_name_STEMMUS-SCOPE,short_name_STEMMUS-SCOPE',
258258
(
259259
'LWnet,surface_net_downward_longwave_flux,Net longwave radiation,'
260260
'"Incident longwave radiation less the simulated outgoing longwave radiation, '

0 commit comments

Comments
 (0)