示例#1
0
    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 = cf.List(data=cf.Data(list_array))

        # Initialise the gathered array object
        array = cf.GatheredArray(compressed_array=cf.Data(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 = cf.Field(properties={
            'standard_name': 'air_temperature',
            'units': 'K'
        })

        # Create the domain axis constructs for the uncompressed array
        T = tas.set_construct(cf.DomainAxis(2))
        Y = tas.set_construct(cf.DomainAxis(3))
        X = tas.set_construct(cf.DomainAxis(2))

        uncompressed_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')

        for chunksize in (1000000, ):
            cf.chunksize(chunksize)
            message = 'chunksize=' + str(chunksize)

            # Set the data for the field
            tas.set_data(cf.Data(array), axes=[T, Y, X])

            self.assertTrue((tas.data.array == uncompressed_array).all(),
                            message)

            self.assertEqual(tas.data.get_compression_type(), 'gathered',
                             message)

            self.assertTrue(
                (tas.data.compressed_array == numpy.array(
                    [[280., 282.5, 281.], [279., 278., 277.5]],
                    dtype='float32')).all(), message)

            self.assertTrue(
                (tas.data.get_list().data.array == numpy.array([1, 4,
                                                                5])).all(),
                message)
示例#2
0
import cf

# Define the gathered values
gathered_array = cf.Data([[2, 1, 3], [4, 0, 5]])

# Define the list array values
list_array = [1, 4, 5]

# Create the list variable
list_variable = cf.List(data=cf.Data(list_array))

# Create the gathered array object, specifying the uncompressed
# shape
array = cf.GatheredArray(compressed_array=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
P = cf.Field(properties={
    'standard_name': 'precipitation_flux',
    'units': 'kg m-2 s-1'
})

# Create the domain axis constructs for the uncompressed array
T = P.set_construct(cf.DomainAxis(2))
Y = P.set_construct(cf.DomainAxis(3))
X = P.set_construct(cf.DomainAxis(2))
    def test_GATHERING_create(self):
        # 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 = cf.List(data=cf.Data(list_array))

        # Initialise the gathered array object
        array = cf.GatheredArray(
            compressed_array=cf.Data(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 = cf.Field(
            properties={"standard_name": "air_temperature", "units": "K"}
        )

        # Create the domain axis constructs for the uncompressed array
        T = tas.set_construct(cf.DomainAxis(2))
        Y = tas.set_construct(cf.DomainAxis(3))
        X = tas.set_construct(cf.DomainAxis(2))

        uncompressed_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=1e20,
            dtype="float32",
        )

        for chunksize in (1000000,):
            cf.chunksize(chunksize)
            message = "chunksize=" + str(chunksize)

            # Set the data for the field
            tas.set_data(cf.Data(array), axes=[T, Y, X])

            self.assertTrue(
                (tas.data.array == uncompressed_array).all(), message
            )

            self.assertEqual(
                tas.data.get_compression_type(), "gathered", message
            )

            self.assertTrue(
                (
                    tas.data.compressed_array
                    == numpy.array(
                        [[280.0, 282.5, 281.0], [279.0, 278.0, 277.5]],
                        dtype="float32",
                    )
                ).all(),
                message,
            )

            self.assertTrue(
                (
                    tas.data.get_list().data.array == numpy.array([1, 4, 5])
                ).all(),
                message,
            )