def test_NumpyArray__array__(self): a = numpy.array([1, 2, 3, 4]) x = cfdm.NumpyArray(a) b = numpy.array(x) self.assertTrue((b == a).all())
def test_NumpyArray_copy(self): a = numpy.array([1, 2, 3, 4]) x = cfdm.NumpyArray(a) y = copy.deepcopy(x) self.assertTrue((x.array == a).all()) self.assertTrue((x.array == y.array).all())
def test_GATHERING_create(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # Define the gathered values gathered_array = numpy.array([[280, 282.5, 281], [279, 278, 277.5]], dtype='float32') # Define the list array values list_array = [1, 4, 5] # Initialise the list variable list_variable = cfdm.List(data=cfdm.Data(list_array)) # Initialise the gathered array object array = cfdm.GatheredArray( compressed_array=cfdm.NumpyArray(gathered_array), compressed_dimension=1, shape=(2, 3, 2), size=12, ndim=3, list_variable=list_variable) # Create the field construct with the domain axes and the gathered # array tas = cfdm.Field(properties={ 'standard_name': 'air_temperature', 'units': 'K' }) # Create the domain axis constructs for the uncompressed array T = tas.set_construct(cfdm.DomainAxis(2)) Y = tas.set_construct(cfdm.DomainAxis(3)) X = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[T, Y, X]) self.assertTrue( (tas.data.array == numpy.ma.masked_array(data=[[[1, 280.0], [1, 1], [282.5, 281.0]], [[1, 279.0], [1, 1], [278.0, 277.5]]], mask=[[[True, False], [True, True], [False, False]], [[True, False], [True, True], [False, False]]], fill_value=1e+20, dtype='float32')).all()) self.assertTrue(tas.data.get_compression_type() == 'gathered') self.assertTrue((tas.data.compressed_array == numpy.array( [[280., 282.5, 281.], [279., 278., 277.5]], dtype='float32')).all()) self.assertTrue( (tas.data.get_list().data.array == numpy.array([1, 4, 5])).all())
def test_NumpyArray__array__(self): """Test the NumPy array conversion of NumpyArray.""" a = numpy.array([1, 2, 3, 4]) x = cfdm.NumpyArray(a) b = numpy.array(x) self.assertTrue((b == a).all())
def test_NumpyArray_copy(self): """Test the copy module copying behaviour of NumpyArray.""" a = numpy.array([1, 2, 3, 4]) x = cfdm.NumpyArray(a) y = copy.deepcopy(x) self.assertTrue((x.array == a).all()) self.assertTrue((x.array == y.array).all())
def test_DSG_contiguous(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return f = self.c.copy() self.assertEqual(len(f), 2) # Select the specific humidity field q = [g for g in f if g.get_property('standard_name') == 'specific_humidity'][0] self.assertTrue(q._equals(self.a, q.data.array)) cfdm.write(f, tempfile) g = cfdm.read(tempfile) self.assertEqual(len(g), len(f)) for i in range(len(f)): self.assertTrue(g[i].equals(f[i], verbose=3)) # ------------------------------------------------------------ # Test creation # ------------------------------------------------------------ # Define the ragged array values ragged_array = numpy.array([280, 282.5, 281, 279, 278, 279.5], dtype='float32') # Define the count array values count_array = [2, 4] # Create the count variable count_variable = cfdm.Count(data=cfdm.Data(count_array)) count_variable.set_property('long_name', 'number of obs for this timeseries') # Create the contiguous ragged array object array = cfdm.RaggedContiguousArray( compressed_array=cfdm.NumpyArray(ragged_array), shape=(2, 4), size=8, ndim=2, count_variable=count_variable) # Create the field construct with the domain axes and the ragged # array tas = cfdm.Field() tas.set_properties({'standard_name': 'air_temperature', 'units': 'K', 'featureType': 'timeSeries'}) # Create the domain axis constructs for the uncompressed array X = tas.set_construct(cfdm.DomainAxis(4)) Y = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[Y, X]) cfdm.write(tas, tempfile)
def test_DSG_create_contiguous(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return # Define the ragged array values ragged_array = numpy.array([1, 3, 4, 3, 6], dtype='float32') # Define the count array values count_array = [2, 3] # Initialise the count variable count_variable = cfdm.Count(data=cfdm.Data(count_array)) count_variable.set_property('long_name', 'number of obs for this timeseries') # Initialise the contiguous ragged array object array = cfdm.RaggedContiguousArray( compressed_array=cfdm.NumpyArray(ragged_array), shape=(2, 3), size=6, ndim=2, count_variable=count_variable) # Initialize the auxiliary coordinate construct with the ragged # array and set some properties z = cfdm.AuxiliaryCoordinate( data=cfdm.Data(array), properties={'standard_name': 'height', 'units': 'km', 'positive': 'up'}) self.assertTrue((z.data.array == numpy.ma.masked_array( data=[[1.0, 3.0, 99], [4.0, 3.0, 6.0]], mask=[[False, False, True], [False, False, False]], fill_value=1e+20, dtype='float32')).all()) self.assertTrue(z.data.get_compression_type() == 'ragged contiguous') self.assertTrue((z.data.compressed_array == numpy.array( [1., 3., 4., 3., 6.], dtype='float32')).all()) self.assertTrue((z.data.get_count().data.array == numpy.array( [2, 3])).all())
def test_DSG_contiguous(self): """TODO DOCS.""" f = self.c.copy() self.assertEqual(len(f), 2) # Select the specific humidity field q = [ g for g in f if g.get_property("standard_name") == "specific_humidity" ][0] self.assertTrue(q._equals(self.a, q.data.array)) cfdm.write(f, tempfile) g = cfdm.read(tempfile) self.assertEqual(len(g), len(f)) for i in range(len(f)): self.assertTrue(g[i].equals(f[i], verbose=3)) # ------------------------------------------------------------ # Test creation # ------------------------------------------------------------ # Define the ragged array values ragged_array = numpy.array([280, 282.5, 281, 279, 278, 279.5], dtype="float32") # Define the count array values count_array = [2, 4] # Create the count variable count_variable = cfdm.Count(data=cfdm.Data(count_array)) count_variable.set_property("long_name", "number of obs for this timeseries") # Create the contiguous ragged array object array = cfdm.RaggedContiguousArray( compressed_array=cfdm.NumpyArray(ragged_array), shape=(2, 4), size=8, ndim=2, count_variable=count_variable, ) # Create the field construct with the domain axes and the ragged # array tas = cfdm.Field() tas.set_properties({ "standard_name": "air_temperature", "units": "K", "featureType": "timeSeries", }) # Create the domain axis constructs for the uncompressed array X = tas.set_construct(cfdm.DomainAxis(4)) Y = tas.set_construct(cfdm.DomainAxis(2)) # Set the data for the field tas.set_data(cfdm.Data(array), axes=[Y, X]) cfdm.write(tas, tempfile)