3030import numpy as np
3131
3232from iris .coords import AuxCoord
33+ from iris .fileformats .cf import CFVariable
3334from iris .fileformats ._pyke_rules .compiled_krb .fc_rules_cf_fc import \
3435 build_auxiliary_coordinate
3536from iris .tests import mock
@@ -40,15 +41,15 @@ def setUp(self):
4041 # Create coordinate cf variables and pyke engine.
4142 points = np .arange (6 ).reshape (2 , 3 )
4243 self .cf_coord_var = mock .Mock (
44+ spec = CFVariable ,
4345 dimensions = ('foo' , 'bar' ),
4446 cf_name = 'wibble' ,
47+ cf_data = mock .Mock (),
4548 standard_name = None ,
4649 long_name = 'wibble' ,
4750 units = 'm' ,
4851 shape = points .shape ,
4952 dtype = points .dtype ,
50- scale_factor = 1 ,
51- add_offset = 0 ,
5253 __getitem__ = lambda self , key : points [key ])
5354
5455 self .engine = mock .Mock (
@@ -75,12 +76,12 @@ def test_slowest_varying_vertex_dim(self):
7576 # Create the bounds cf variable.
7677 bounds = np .arange (24 ).reshape (4 , 2 , 3 )
7778 self .cf_bounds_var = mock .Mock (
79+ spec = CFVariable ,
7880 dimensions = ('nv' , 'foo' , 'bar' ),
7981 cf_name = 'wibble_bnds' ,
82+ cf_data = mock .Mock (),
8083 shape = bounds .shape ,
8184 dtype = bounds .dtype ,
82- scale_factor = 1 ,
83- add_offset = 0 ,
8485 __getitem__ = lambda self , key : bounds [key ])
8586
8687 # Expected bounds on the resulting coordinate should be rolled so that
@@ -116,12 +117,12 @@ def test_slowest_varying_vertex_dim(self):
116117 def test_fastest_varying_vertex_dim (self ):
117118 bounds = np .arange (24 ).reshape (2 , 3 , 4 )
118119 self .cf_bounds_var = mock .Mock (
120+ spec = CFVariable ,
119121 dimensions = ('foo' , 'bar' , 'nv' ),
120122 cf_name = 'wibble_bnds' ,
123+ cf_data = mock .Mock (),
121124 shape = bounds .shape ,
122125 dtype = bounds .dtype ,
123- scale_factor = 1 ,
124- add_offset = 0 ,
125126 __getitem__ = lambda self , key : bounds [key ])
126127
127128 expected_coord = AuxCoord (
@@ -155,12 +156,12 @@ def test_fastest_with_different_dim_names(self):
155156 # this should still work because the vertex dim is the fastest varying.
156157 bounds = np .arange (24 ).reshape (2 , 3 , 4 )
157158 self .cf_bounds_var = mock .Mock (
159+ spec = CFVariable ,
158160 dimensions = ('x' , 'y' , 'nv' ),
159161 cf_name = 'wibble_bnds' ,
162+ cf_data = mock .Mock (),
160163 shape = bounds .shape ,
161164 dtype = bounds .dtype ,
162- scale_factor = 1 ,
163- add_offset = 0 ,
164165 __getitem__ = lambda self , key : bounds [key ])
165166
166167 expected_coord = AuxCoord (
@@ -189,5 +190,65 @@ def test_fastest_with_different_dim_names(self):
189190 expected_list )
190191
191192
193+ class TestDtype (tests .IrisTest ):
194+ def setUp (self ):
195+ # Create coordinate cf variables and pyke engine.
196+ points = np .arange (6 ).reshape (2 , 3 )
197+ self .cf_coord_var = mock .Mock (
198+ spec = CFVariable ,
199+ dimensions = ('foo' , 'bar' ),
200+ cf_name = 'wibble' ,
201+ cf_data = mock .Mock (),
202+ standard_name = None ,
203+ long_name = 'wibble' ,
204+ units = 'm' ,
205+ shape = points .shape ,
206+ dtype = points .dtype ,
207+ __getitem__ = lambda self , key : points [key ])
208+
209+ self .engine = mock .Mock (
210+ cube = mock .Mock (),
211+ cf_var = mock .Mock (dimensions = ('foo' , 'bar' )),
212+ filename = 'DUMMY' ,
213+ provides = dict (coordinates = []))
214+
215+ def patched__getitem__ (proxy_self , keys ):
216+ if proxy_self .variable_name == self .cf_coord_var .cf_name :
217+ return self .cf_coord_var [keys ]
218+ raise RuntimeError ()
219+
220+ self .deferred_load_patch = mock .patch (
221+ 'iris.fileformats.netcdf.NetCDFDataProxy.__getitem__' ,
222+ new = patched__getitem__ )
223+
224+ def test_scale_factor_add_offset_int (self ):
225+ self .cf_coord_var .scale_factor = 3
226+ self .cf_coord_var .add_offset = 5
227+
228+ with self .deferred_load_patch :
229+ build_auxiliary_coordinate (self .engine , self .cf_coord_var )
230+
231+ coord , _ = self .engine .provides ['coordinates' ][0 ]
232+ self .assertEqual (coord .dtype .kind , 'i' )
233+
234+ def test_scale_factor_float (self ):
235+ self .cf_coord_var .scale_factor = 3.
236+
237+ with self .deferred_load_patch :
238+ build_auxiliary_coordinate (self .engine , self .cf_coord_var )
239+
240+ coord , _ = self .engine .provides ['coordinates' ][0 ]
241+ self .assertEqual (coord .dtype .kind , 'f' )
242+
243+ def test_add_offset_float (self ):
244+ self .cf_coord_var .add_offset = 5.
245+
246+ with self .deferred_load_patch :
247+ build_auxiliary_coordinate (self .engine , self .cf_coord_var )
248+
249+ coord , _ = self .engine .provides ['coordinates' ][0 ]
250+ self .assertEqual (coord .dtype .kind , 'f' )
251+
252+
192253if __name__ == '__main__' :
193254 tests .main ()
0 commit comments