@@ -99,88 +99,18 @@ def test_master_clock_dim(self):
99
99
ds = xr .Dataset ()
100
100
assert ds .xsimlab .master_clock_dim is None
101
101
102
- def test_set_master_clock_dim (self ):
103
- ds = xr .Dataset (coords = {'clock' : [1 , 2 ], 'clock2' : [3 , 4 ]})
104
-
105
- ds .xsimlab ._set_master_clock_dim ('clock' )
106
- assert self ._master_clock_key in ds .clock .attrs
107
-
108
- ds .xsimlab ._set_master_clock_dim ('clock2' )
109
- assert self ._master_clock_key not in ds .clock .attrs
110
- assert self ._master_clock_key in ds .clock2 .attrs
111
-
112
- with pytest .raises (KeyError ):
113
- ds .xsimlab ._set_master_clock_dim ('invalid_clock' )
114
-
115
- def test_set_master_clock (self ):
116
- data = [0 , 2 , 4 , 6 , 8 ]
117
-
118
- valid_kwargs = [
119
- {'data' : data },
120
- # data provided -> ignore other arguments even if invalid
121
- {'data' : data , 'end' : 0 , 'nsteps' : - 1 , 'step' : 3 },
122
- {'nsteps' : 4 , 'end' : 8 , 'step' : 2 },
123
- {'nsteps' : 4 , 'end' : 8 },
124
- {'nsteps' : 4 , 'step' : 2 },
125
- {'step' : 2 , 'end' : 8 }
126
- ]
127
- for kwargs in valid_kwargs :
128
- ds = xr .Dataset ()
129
- ds .xsimlab ._set_master_clock ('clock' , ** kwargs )
130
- np .testing .assert_array_equal (ds .clock .values , data )
131
-
132
- invalid_kwargs = [
133
- {'nsteps' : 4 , 'end' : 8 , 'step' : 3 },
134
- {'start' : 1 , 'nsteps' : 4 , 'end' : 8 , 'step' : 2 },
135
- {'nsteps' : 4 }
136
- ]
137
- for kwargs in invalid_kwargs :
138
- with pytest .raises (ValueError ) as excinfo :
139
- ds = xr .Dataset ()
140
- ds .xsimlab ._set_master_clock ('clock' , ** kwargs )
141
- assert "Invalid combination" in str (excinfo .value )
102
+ # def test_set_master_clock_dim(self):
103
+ # ds = xr.Dataset(coords={'clock': [1, 2], 'clock2': [3, 4]})
142
104
143
- ds = xr .Dataset ()
144
- ds .xsimlab ._set_master_clock ('clock' , data = data ,
145
- units = 'years since 1-1-1 0:0:0' ,
146
- calendar = '365_day' )
147
- assert self ._master_clock_key in ds .clock .attrs
148
- assert ds .clock .attrs ['units' ] == 'years since 1-1-1 0:0:0'
149
- assert ds .clock .attrs ['calendar' ] == '365_day'
150
-
151
- with pytest .raises (ValueError ) as excinfo :
152
- ds .xsimlab ._set_master_clock ('clock' , data = data )
153
- assert "already exists" in str (excinfo .value )
154
-
155
- ds = xr .Dataset ()
156
- da = xr .DataArray (data , dims = 'other_dim' )
157
- with pytest .raises (ValueError ) as excinfo :
158
- ds .xsimlab ._set_master_clock ('clock' , data = da )
159
- assert "expected dimension" in str (excinfo .value )
105
+ # ds.xsimlab._set_master_clock_dim('clock')
106
+ # assert self._master_clock_key in ds.clock.attrs
160
107
161
- def test_set_snapshot_clock (self ):
162
- with pytest .raises (ValueError ) as excinfo :
163
- ds = xr .Dataset ()
164
- ds .xsimlab ._set_snapshot_clock ('snap_clock' , data = [1 , 2 ])
165
- assert "no master clock" in str (excinfo .value )
108
+ # ds.xsimlab._set_master_clock_dim('clock2')
109
+ # assert self._master_clock_key not in ds.clock.attrs
110
+ # assert self._master_clock_key in ds.clock2.attrs
166
111
167
- ds = xr .Dataset ()
168
- ds .xsimlab ._set_master_clock ('clock' , data = [0 , 2 , 4 , 6 , 8 ],
169
- units = 'years since 1-1-1 0:0:0' ,
170
- calendar = '365_day' )
171
-
172
- ds .xsimlab ._set_snapshot_clock ('snap_clock' , end = 8 , step = 4 )
173
- np .testing .assert_array_equal (ds ['snap_clock' ], [0 , 4 , 8 ])
174
- assert self ._clock_key in ds ['snap_clock' ].attrs
175
- assert 'units' in ds ['snap_clock' ].attrs
176
- assert 'calendar' in ds ['snap_clock' ].attrs
177
-
178
- ds .xsimlab ._set_snapshot_clock ('snap_clock' , data = [0 , 3 , 8 ])
179
- np .testing .assert_array_equal (ds ['snap_clock' ], [0 , 4 , 8 ])
180
-
181
- with pytest .raises (KeyError ):
182
- ds .xsimlab ._set_snapshot_clock ('snap_clock' , data = [0 , 3 , 8 ],
183
- auto_adjust = False )
112
+ # with pytest.raises(KeyError):
113
+ # ds.xsimlab._set_master_clock_dim('invalid_clock')
184
114
185
115
def test_set_input_vars (self , model , in_dataset ):
186
116
in_vars = {('init_profile' , 'n_points' ): 5 ,
@@ -206,46 +136,78 @@ def test_update_clocks(self, model):
206
136
ds = xr .Dataset ()
207
137
with pytest .raises (ValueError ) as excinfo :
208
138
ds .xsimlab .update_clocks (model = model , clocks = {})
209
- assert "cannot determine which clock" in str (excinfo .value )
139
+ assert "Cannot determine which clock" in str (excinfo .value )
210
140
211
141
ds = xr .Dataset ()
212
142
with pytest .raises (ValueError ) as excinfo :
213
143
ds .xsimlab .update_clocks (
214
144
model = model ,
215
- clocks = {'clock' : { 'data' : [0 , 1 , 2 ]} ,
216
- 'out' : { 'data' : [0 , 2 ]} }
145
+ clocks = {'clock' : [0 , 1 , 2 ],
146
+ 'out' : [0 , 2 ]}
217
147
)
218
- assert "cannot determine which clock" in str (excinfo .value )
148
+ assert "Cannot determine which clock" in str (excinfo .value )
219
149
220
150
ds = xr .Dataset ()
221
151
with pytest .raises (KeyError ) as excinfo :
222
152
ds .xsimlab .update_clocks (
223
153
model = model ,
224
- clocks = {'clock' : { 'data' : [0 , 1 , 2 ]} },
154
+ clocks = {'clock' : [0 , 1 , 2 ]},
225
155
master_clock = 'non_existing_clock_dim'
226
156
)
227
- assert "master clock dimension name" in str (excinfo .value )
157
+ assert "Master clock dimension name" in str (excinfo .value )
158
+
159
+ ds = xr .Dataset ()
160
+ with pytest .raises (ValueError ) as excinfo :
161
+ ds .xsimlab .update_clocks (
162
+ model = model ,
163
+ clocks = {'clock' : ('x' , [0 , 1 , 2 ])},
164
+ )
165
+ assert "Invalid dimension" in str (excinfo .value )
166
+
167
+ ds = xr .Dataset ()
168
+ with pytest .raises (ValueError ) as excinfo :
169
+ ds .xsimlab .update_clocks (
170
+ model = model ,
171
+ clocks = {'clock' : [0 , 1 , 2 ],
172
+ 'out' : [0 , 0.5 , 2 ]},
173
+ master_clock = 'clock'
174
+ )
175
+ assert "not synchronized" in str (excinfo .value )
228
176
229
177
ds = xr .Dataset ()
230
178
ds = ds .xsimlab .update_clocks (
231
179
model = model ,
232
- clocks = {'clock' : { 'data' : [0 , 1 , 2 ]} }
180
+ clocks = {'clock' : [0 , 1 , 2 ]}
233
181
)
234
182
assert ds .xsimlab .master_clock_dim == 'clock'
235
183
236
184
ds .clock .attrs [self ._output_vars_key ] = 'profile__u'
237
185
238
186
ds = ds .xsimlab .update_clocks (
239
187
model = model ,
240
- clocks = {'clock' : { 'data' : [0 , 1 , 2 ]} },
188
+ clocks = {'clock' : [0 , 1 , 2 ]},
241
189
master_clock = {'dim' : 'clock' ,
242
190
'units' : 'days since 1-1-1 0:0:0' ,
243
191
'calendar' : '365_days' }
244
192
)
193
+ np .testing .assert_array_equal (ds .clock .values , [0 , 1 , 2 ])
245
194
assert 'units' in ds .clock .attrs
246
195
assert 'calendar' in ds .clock .attrs
247
196
assert ds .clock .attrs [self ._output_vars_key ] == 'profile__u'
248
197
198
+ new_ds = ds .xsimlab .update_clocks (
199
+ model = model ,
200
+ clocks = {'clock2' : [0 , 0.5 , 1 , 1.5 , 2 ]},
201
+ master_clock = 'clock2'
202
+ )
203
+ assert new_ds .xsimlab .master_clock_dim == 'clock2'
204
+
205
+ new_ds = ds .xsimlab .update_clocks (
206
+ model = model ,
207
+ clocks = {'out2' : [0 , 2 ]}
208
+ )
209
+ assert new_ds .xsimlab .master_clock_dim == 'clock'
210
+
249
211
def test_update_vars (self , model , in_dataset ):
250
212
ds = in_dataset .xsimlab .update_vars (
251
213
model = model ,
@@ -337,8 +299,8 @@ def test_create_setup(model, in_dataset):
337
299
'add__offset' : ('clock' , [1 , 2 , 3 , 4 , 5 ])
338
300
},
339
301
clocks = {
340
- 'clock' : { 'data' : [0 , 2 , 4 , 6 , 8 ]} ,
341
- 'out' : { 'data' : [0 , 4 , 8 ]} ,
302
+ 'clock' : [0 , 2 , 4 , 6 , 8 ],
303
+ 'out' : [0 , 4 , 8 ],
342
304
},
343
305
master_clock = 'clock' ,
344
306
output_vars = {
0 commit comments