Skip to content

Commit bcfeaa1

Browse files
authored
update test for ocean cmoriser (#135)
* update test for ocean cmoriser * fix format
1 parent e5e740d commit bcfeaa1

2 files changed

Lines changed: 154 additions & 1 deletion

File tree

tests/integration/test_cmoriser_integration.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from unittest.mock import patch
22

33
from access_moppy import ACCESS_ESM_CMORiser
4-
from tests.mocks.mock_data import create_mock_atmosphere_dataset
4+
from tests.mocks.mock_data import (
5+
create_mock_2d_ocean_dataset,
6+
create_mock_atmosphere_dataset,
7+
)
58

69

710
class TestCMORiserIntegration:
@@ -94,3 +97,30 @@ def test_multiple_variables_workflow(
9497
# Verify correct variable is being processed
9598
expected_var = compound_name.split(".")[1]
9699
assert cmoriser.cmoriser.cmor_name == expected_var
100+
101+
@patch("access_moppy.base.xr.open_mfdataset")
102+
def test_multiple_ocean_variables_workflow(
103+
self, mock_open_mfdataset, mock_config, temp_dir
104+
):
105+
"""Test workflow with multiple variables in dataset."""
106+
# Create dataset with multiple variables
107+
mock_dataset = create_mock_2d_ocean_dataset()
108+
109+
mock_open_mfdataset.return_value = mock_dataset
110+
111+
# Test different compound names
112+
compound_name = "Omon.tos"
113+
114+
cmoriser = ACCESS_ESM_CMORiser(
115+
input_paths=["mock_file.nc"],
116+
compound_name=compound_name,
117+
output_path=temp_dir,
118+
**mock_config,
119+
)
120+
121+
with patch.object(cmoriser, "write"):
122+
cmoriser.run()
123+
124+
# Verify correct variable is being processed
125+
expected_var = compound_name.split(".")[1]
126+
assert cmoriser.cmoriser.cmor_name == expected_var

tests/mocks/mock_data.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,129 @@ def create_mock_ocean_dataset(
197197
return ds
198198

199199

200+
def create_mock_2d_ocean_dataset(
201+
nt=12, ny=300, nx=360, variables=["surface_temp"], start_date="2000-01-01", freq="M"
202+
):
203+
"""
204+
Create a mock xarray Dataset mimicking ACCESS-ESM ocean surface temperature output.
205+
206+
Returns a dataset with 12 monthly time steps, matching the structure from ncdump.
207+
"""
208+
# Dimensions
209+
nt, ny, nx = 12, 300, 360
210+
211+
# Coordinates
212+
xt_ocean = np.linspace(0.5, 359.5, nx)
213+
yt_ocean = np.linspace(-89.5, 89.5, ny)
214+
nv = np.array([1.0, 2.0])
215+
216+
# Time: 12 months starting from year 1444
217+
days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
218+
base_days = (1444 - 1) * 365
219+
220+
# time = []
221+
time_bnds = np.zeros((12, 2))
222+
cumulative = base_days
223+
224+
for i in range(12):
225+
time_bnds[i, 0] = cumulative
226+
time_bnds[i, 1] = cumulative + days_per_month[i]
227+
cumulative += days_per_month[i]
228+
229+
time = pd.date_range(start=start_date, periods=nt, freq=freq)
230+
average_T1 = time_bnds[:, 0]
231+
average_T2 = time_bnds[:, 1]
232+
average_DT = average_T2 - average_T1
233+
234+
# Surface temperature data (K)
235+
np.random.seed(42)
236+
data_var = np.random.uniform(273.0, 303.0, (nt, ny, nx)).astype(np.float32)
237+
238+
# Create dataset
239+
ds = xr.Dataset(
240+
data_vars={
241+
variables[0]: (
242+
["time", "yt_ocean", "xt_ocean"],
243+
data_var,
244+
{
245+
"long_name": "Conservative temperature",
246+
"units": "K",
247+
"valid_range": np.array([-10.0, 500.0], dtype=np.float32),
248+
"missing_value": np.float32(-1e20),
249+
"_FillValue": np.float32(-1e20),
250+
"cell_methods": "time: mean",
251+
"standard_name": "sea_surface_conservative_temperature",
252+
},
253+
),
254+
"average_T1": (
255+
["time"],
256+
average_T1,
257+
{
258+
"long_name": "Start time for average period",
259+
"units": "days since 0001-01-01 00:00:00",
260+
},
261+
),
262+
"average_T2": (
263+
["time"],
264+
average_T2,
265+
{
266+
"long_name": "End time for average period",
267+
"units": "days since 0001-01-01 00:00:00",
268+
},
269+
),
270+
"average_DT": (
271+
["time"],
272+
average_DT,
273+
{"long_name": "Length of average period", "units": "days"},
274+
),
275+
"time_bnds": (
276+
["time", "nv"],
277+
time_bnds,
278+
{"long_name": "time axis boundaries", "units": "days"},
279+
),
280+
},
281+
coords={
282+
"xt_ocean": (
283+
"xt_ocean",
284+
xt_ocean,
285+
{
286+
"long_name": "tcell longitude",
287+
"units": "degrees_E",
288+
"cartesian_axis": "X",
289+
},
290+
),
291+
"yt_ocean": (
292+
"yt_ocean",
293+
yt_ocean,
294+
{
295+
"long_name": "tcell latitude",
296+
"units": "degrees_N",
297+
"cartesian_axis": "Y",
298+
},
299+
),
300+
"time": (
301+
"time",
302+
time,
303+
{
304+
"long_name": "time",
305+
"units": "days since 0001-01-01 00:00:00",
306+
"calendar": "PROLEPTIC_GREGORIAN",
307+
"bounds": "time_bnds",
308+
},
309+
),
310+
"nv": ("nv", nv, {"long_name": "vertex number"}),
311+
},
312+
attrs={
313+
"filename": "ocean-2d-surface_temp-1monthly-mean-ym_2000_01.nc",
314+
"title": "ACCESS-ESM_CMIP6",
315+
"grid_type": "mosaic",
316+
"grid_tile": "1",
317+
},
318+
)
319+
320+
return ds
321+
322+
200323
def create_chunked_dataset(chunks=None, **kwargs):
201324
"""Create a chunked dataset for testing dask operations."""
202325
if chunks is None:

0 commit comments

Comments
 (0)