def test_cfunits_conform(self): units_kelvin = get_units_object('kelvin') # Conversion of celsius units to kelvin. attrs = {k: 1 for k in NETCDF_ATTRIBUTES_TO_REMOVE_ON_VALUE_CHANGE} var = Variable(name='tas', units='celsius', value=self.value, attrs=attrs) self.assertEqual(len(var.attrs), 4) var.cfunits_conform(units_kelvin) self.assertNumpyAll(var.value, np.ma.array([278.15] * 3)) self.assertEqual(var.cfunits, units_kelvin) self.assertEqual(var.units, 'kelvin') self.assertEqual(len(var.attrs), 0) # If there are no units associated with a variable, conforming the units should fail. var = Variable(name='tas', units=None, value=self.value) with self.assertRaises(NoUnitsError): var.cfunits_conform(units_kelvin) # Conversion should fail for nonequivalent units. var = Variable(name='tas', units='kelvin', value=self.value) with self.assertRaises(ValueError): var.cfunits_conform(get_units_object('grams')) # The data type should always be updated to match the output from CF units backend. av = Variable(value=np.array([4, 5, 6]), dtype=int) self.assertEqual(av.dtype, np.dtype(int)) with self.assertRaises(NoUnitsError): av.cfunits_conform('K') av.units = 'celsius' av.cfunits_conform('K') self.assertIsNone(av._dtype) self.assertEqual(av.dtype, av.value.dtype)
def test_conform(self): ## conversion of celsius units to kelvin var = Variable(name='tas',units='celsius',value=self.value) var.cfunits_conform(Units('kelvin')) self.assertNumpyAll(var.value,np.ma.array([278.15]*3)) self.assertEqual(var.cfunits,Units('kelvin')) self.assertEqual(var.units,'kelvin')
def test_cfunits_conform(self): # conversion of celsius units to kelvin attrs = {k: 1 for k in NETCDF_ATTRIBUTES_TO_REMOVE_ON_VALUE_CHANGE} var = Variable(name='tas', units='celsius', value=self.value, attrs=attrs) self.assertEqual(len(var.attrs), 2) var.cfunits_conform(Units('kelvin')) self.assertNumpyAll(var.value, np.ma.array([278.15] * 3)) self.assertEqual(var.cfunits, Units('kelvin')) self.assertEqual(var.units, 'kelvin') self.assertEqual(len(var.attrs), 0) # if there are no units associated with a variable, conforming the units should fail var = Variable(name='tas', units=None, value=self.value) with self.assertRaises(NoUnitsError): var.cfunits_conform(Units('kelvin')) # conversion should fail for nonequivalent units var = Variable(name='tas', units='kelvin', value=self.value) with self.assertRaises(ValueError): var.cfunits_conform(Units('grams')) # the data type should always be updated to match the output from cfunits av = FakeAbstractValueVariable(value=np.array([4, 5, 6]), dtype=int) self.assertEqual(av.dtype, np.dtype(int)) with self.assertRaises(NoUnitsError): av.cfunits_conform('K') av.units = 'celsius' av.cfunits_conform('K') self.assertIsNone(av._dtype) self.assertEqual(av.dtype, av.value.dtype) # calendar can be finicky - those need to be stripped from the string conversion conform_units_to = Units('days since 1949-1-1', calendar='standard') units = Units('days since 1900-1-1', calendar='standard') av = FakeAbstractValueVariable(value=np.array([4000, 5000, 6000]), units=units, conform_units_to=conform_units_to) self.assertEqual(av.units, 'days since 1949-1-1')
def test_cfunits_conform_masked_array(self): # assert mask is respected by inplace unit conversion value = np.ma.array(data=[5, 5, 5], mask=[False, True, False]) var = Variable(name='tas', units=Units('celsius'), value=value) var.cfunits_conform(Units('kelvin')) self.assertNumpyAll(np.ma.array([278.15, 278.15, 278.15], mask=[False, True, False]), var.value)
def test_conform_no_units(self): ## if there are no units associate with a variable, conforming the units ## should fail var = Variable(name='tas',units=None,value=self.value) with self.assertRaises(NoUnitsError): var.cfunits_conform(Units('kelvin'))
def test_conform_nonequivalent_units(self): ## conversion should fail for nonequivalent units var = Variable(name='tas',units='kelvin',value=self.value) with self.assertRaises(ValueError): var.cfunits_conform(Units('grams'))