def setUpClass(cls): # Create a temp directory for temp files. cls.temp_dir = tempfile.mkdtemp() cls.path_ref_cdl = path_join(cls.temp_dir, 'standard.cdl') cls.path_ref_nc = path_join(cls.temp_dir, 'standard.nc') # Create reference CDL file. with open(cls.path_ref_cdl, 'w') as f_out: f_out.write(cls._simple_cdl_string()) # Create reference netCDF file from reference CDL. command = 'ncgen -o {} {}'.format(cls.path_ref_nc, cls.path_ref_cdl) check_call(command, shell=True) cls.path_temp_nc = path_join(cls.temp_dir, 'tmp.nc') # Create reference cube. cls.cube_ref = stock.climatology_3d()
def _check_bounds_setting(self, climatological=False): # Generic test that can run with or without a climatological coord. cube = stock.climatology_3d() coord = cube.coord("time").copy() # Over-write original value from stock.climatology_3d with test value. coord.climatological = climatological # Set up expected strings. if climatological: property_name = "climatology" varname_extra = "climatology" else: property_name = "bounds" varname_extra = "bnds" boundsvar_name = "time_" + varname_extra # Set up arguments for testing _create_cf_bounds. saver = mock.MagicMock(spec=Saver) # NOTE: 'saver' must have spec=Saver to fake isinstance(save, Saver), # so it can pass as 'self' in the call to _create_cf_cbounds. # Mock a '_dataset' property; not automatic because 'spec=Saver'. saver._dataset = mock.MagicMock() # Mock the '_ensure_valid_dtype' method to return an object with a # suitable 'shape' and 'dtype'. saver._ensure_valid_dtype.return_value = mock.Mock( shape=coord.bounds.shape, dtype=coord.bounds.dtype ) var = mock.MagicMock(spec=nc.Variable) # Make the main call. Saver._create_cf_bounds(saver, coord, var, "time") # Test the call of _setncattr in _create_cf_bounds. setncattr_call = mock.call( property_name, boundsvar_name.encode(encoding="ascii") ) self.assertEqual(setncattr_call, var.setncattr.call_args) # Test the call of createVariable in _create_cf_bounds. dataset = saver._dataset expected_dimensions = var.dimensions + ("bnds",) create_var_call = mock.call( boundsvar_name, coord.bounds.dtype, expected_dimensions ) self.assertEqual(create_var_call, dataset.createVariable.call_args)
def test_with_climatology(self): cube = stock.climatology_3d() with self.temp_filename('.nc') as nc_path: with Saver(nc_path, 'NETCDF4') as saver: saver.write(cube) self.assertCDL(nc_path)
def climatology_3d(): return stock.climatology_3d()