def test_AuxiliaryCoordinate_bounds(self):
        f = cf.read(self.filename)[0]
        d = f.dimension_coordinates('X').value()
        x = cf.AuxiliaryCoordinate(source=d)

        _ = x.upper_bounds
        _ = x.lower_bounds
Beispiel #2
0
    def test_DSG_create_contiguous(self):
        # 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 = cf.Count(data=cf.Data(count_array))
        count_variable.set_property(
            "long_name", "number of obs for this timeseries"
        )

        # Initialise the contiguous ragged array object
        array = cf.RaggedContiguousArray(
            compressed_array=cf.Data(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 = cf.AuxiliaryCoordinate(
            data=cf.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=1e20,
                    dtype="float32",
                )
            ).all()
        )

        self.assertEqual(z.data.get_compression_type(), "ragged contiguous")

        self.assertTrue(
            (
                z.data.compressed_array
                == numpy.array([1.0, 3.0, 4.0, 3.0, 6.0], dtype="float32")
            ).all()
        )

        self.assertTrue(
            (z.data.get_count().data.array == numpy.array([2, 3])).all()
        )
Beispiel #3
0
    def test_AuxiliaryCoordinate_insert_dimension(self):
        d = self.f.dimension_coordinate("X")
        x = cf.AuxiliaryCoordinate(source=d)

        self.assertEqual(x.shape, (9, ))
        self.assertEqual(x.bounds.shape, (9, 2))

        y = x.insert_dimension(0)
        self.assertEqual(y.shape, (1, 9))
        self.assertEqual(y.bounds.shape, (1, 9, 2), y.bounds.shape)

        x.insert_dimension(-1, inplace=True)
        self.assertEqual(x.shape, (9, 1))
        self.assertEqual(x.bounds.shape, (9, 1, 2), x.bounds.shape)
Beispiel #4
0
    def test_AuxiliaryCoordinate_properties(self):
        x = self.f.auxiliary_coordinate("latitude")

        x.positive = "up"
        self.assertEqual(x.positive, "up")
        del x.positive
        self.assertIsNone(getattr(x, "positive", None))

        x.axis = "Z"
        self.assertEqual(x.axis, "Z")
        del x.axis
        self.assertIsNone(getattr(x, "axis", None))

        d = self.f.dimension_coordinate("X")
        x = cf.AuxiliaryCoordinate(source=d)
    def test_AuxiliaryCoordinate_insert_dimension(self):
        f = cf.read(self.filename)[0]
        d = f.dimension_coordinates('X').value()
        x = cf.AuxiliaryCoordinate(source=d)

        self.assertEqual(x.shape, (9, ))
        self.assertEqual(x.bounds.shape, (9, 2))

        y = x.insert_dimension(0)
        self.assertEqual(y.shape, (1, 9))
        self.assertEqual(y.bounds.shape, (1, 9, 2), y.bounds.shape)

        x.insert_dimension(-1, inplace=True)
        self.assertEqual(x.shape, (9, 1))
        self.assertEqual(x.bounds.shape, (9, 1, 2), x.bounds.shape)
Beispiel #6
0
    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 = cf.Count(data=cf.Data(count_array))
        count_variable.set_property('long_name',
                                    'number of obs for this timeseries')

        # Initialise the contiguous ragged array object
        array = cf.RaggedContiguousArray(
            compressed_array=cf.Data(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 = cf.AuxiliaryCoordinate(data=cf.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.assertEqual(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_AuxiliaryCoordinate_properties(self):
        f = cf.read(self.filename)[0]
        x = f.auxiliary_coordinates('latitude').value()

        x.positive = 'up'
        self.assertEqual(x.positive, 'up')
        del x.positive
        self.assertIsNone(getattr(x, 'positive', None))

        x.axis = 'Z'
        self.assertEqual(x.axis, 'Z')
        del x.axis
        self.assertIsNone(getattr(x, 'axis', None))

        d = f.dimension_coordinates('X').value()
        x = cf.AuxiliaryCoordinate(source=d)
Beispiel #8
0
 def setUp(self):
     self.filename = os.path.join(
         os.path.dirname(os.path.abspath(__file__)), 'test_file.nc')
     aux1 = cf.AuxiliaryCoordinate()
     aux1.standard_name = 'latitude'
     a = numpy.array(
         [-30, -23.5, -17.8123, -11.3345, -0.7, -0.2, 0, 0.2, 0.7,
          11.30003, 17.8678678, 23.5, 30]
     )
     aux1.set_data(cf.Data(a, 'degrees_north'))
     bounds = cf.Bounds()
     b = numpy.empty(a.shape + (2,))
     b[:, 0] = a - 0.1
     b[:, 1] = a + 0.1
     bounds.set_data(cf.Data(b))
     aux1.set_bounds(bounds)
     self.aux1 = aux1
Beispiel #9
0
    properties={
        'standard_name': 'grid_longitude',
        'units': 'degrees'
    },
    data=cf.Data(numpy.arange(9.)),
    bounds=cf.Bounds(data=cf.Data(numpy.arange(18).reshape(9, 2))))

dim_T = tas.set_construct(dimension_coordinate_T, axes=axis_T)
dim_Z = tas.set_construct(dimension_coordinate_Z, axes=axis_Z)
dim_Y = tas.set_construct(dimension_coordinate_Y)
dim_X = tas.set_construct(dimension_coordinate_X)

# Create and set the auxiliary coordinate constructs
auxiliary_coordinate_lat = cf.AuxiliaryCoordinate(
    properties={
        'standard_name': 'latitude',
        'units': 'degrees_north'
    },
    data=cf.Data(numpy.arange(90.).reshape(10, 9)))

auxiliary_coordinate_lon = cf.AuxiliaryCoordinate(
    properties={
        'standard_name': 'longitude',
        'units': 'degrees_east'
    },
    data=cf.Data(numpy.arange(90.).reshape(9, 10)))

array = numpy.ma.array(list('abcdefghij'))
array[0] = numpy.ma.masked
auxiliary_coordinate_name = cf.AuxiliaryCoordinate(
    properties={'long_name': 'Grid latitude name'}, data=cf.Data(array))
def _formula_terms(standard_name):
    """Return a field construct with a vertical CRS, its computed non-
    parametric coordinates, and the computed standard name."""
    # field: air_temperature
    field = cf.Field()
    field.set_properties({"standard_name": "air_temperature", "units": "K"})
    data = cf.Data([0, 1, 2], units="K", dtype="f8")

    # domain_axis: Z
    c = cf.DomainAxis()
    c.set_size(3)
    c.nc_set_dimension("z")
    axisZ = field.set_construct(c, key="domainaxis1", copy=False)

    field.set_data(data)

    # coordinate_reference:
    coordref = cf.CoordinateReference()
    coordref.coordinate_conversion.set_parameter(
        "standard_name", standard_name
    )

    aux = cf.AuxiliaryCoordinate()
    aux.long_name = "Computed from parametric {} vertical coordinates".format(
        standard_name
    )

    if standard_name == "atmosphere_ln_pressure_coordinate":
        computed_standard_name = "air_pressure"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([700, 500, 300], "hPa", dtype="f8")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[800, 600], [600, 400], [400, 200]], "hPa", dtype="f8")
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: p0
        p0 = cf.DomainAncillary()
        p0.standard_name = (
            "reference_air_pressure_for_atmosphere_vertical_coordinate"
        )
        data = cf.Data(1000.0, units="hPa", dtype="f8")
        p0.set_data(data)
        p0_key = field.set_construct(p0, axes=(), copy=False)

        # domain_ancillary: Z
        lev = cf.DomainAncillary()
        lev.standard_name = standard_name
        data = -(aux.data / p0.data).log()
        lev.set_data(data)
        bounds = cf.Bounds()
        data = -(aux.bounds.data / p0.data).log()
        bounds.set_data(data)
        lev.set_bounds(bounds)
        lev_key = field.set_construct(lev, axes=axisZ, copy=False)

        # dimension_coordinate: Z
        levc = cf.DimensionCoordinate(source=lev)
        levc_key = field.set_construct(levc, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({levc_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {"p0": p0_key, "lev": lev_key}
        )
        field.set_construct(coordref)

    elif standard_name == "atmosphere_sigma_coordinate":
        computed_standard_name = "air_pressure"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([700, 500, 300], "hPa", dtype="f8")
        aux.set_data(data)
        b = cf.Bounds()
        data = cf.Data([[800, 600], [600, 400], [400, 200]], "hPa", dtype="f8")
        b.set_data(data)
        aux.set_bounds(b)

        # domain_ancillary: ps
        ps = cf.DomainAncillary()
        ps.standard_name = "surface_air_pressure"
        data = cf.Data(1000, units="hPa", dtype="f8")
        ps.set_data(data)
        ps_key = field.set_construct(ps, axes=(), copy=False)

        # domain_ancillary: ptop
        ptop = cf.DomainAncillary()
        ptop.standard_name = "air_pressure_at_top_of_atmosphere_model"
        data = cf.Data(10, units="hPa", dtype="f8")
        ptop.set_data(data)
        ptop_key = field.set_construct(ptop, axes=(), copy=False)

        # domain_ancillary: sigma
        sigma = cf.DomainAncillary()
        sigma.standard_name = standard_name
        data = cf.Data([0.6969697, 0.49494949, 0.29292929])
        sigma.set_data(data)
        b = cf.Bounds()
        data = cf.Data(
            [
                [0.7979798, 0.5959596],
                [0.5959596, 0.39393939],
                [0.39393939, 0.19191919],
            ]
        )
        b.set_data(data)
        sigma.set_bounds(b)
        sigma_key = field.set_construct(sigma, axes=axisZ, copy=False)

        # dimension_coordinate: sigma
        sigmac = cf.DimensionCoordinate(source=sigma)
        sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sigmac_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {"ptop": ptop_key, "ps": ps_key, "sigma": sigma_key}
        )
        field.set_construct(coordref)

    elif standard_name == "atmosphere_hybrid_sigma_pressure_coordinate":
        computed_standard_name = "air_pressure"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([700, 500, 300], "hPa", dtype="f8")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[800, 600], [600, 400], [400, 200]], "hPa", dtype="f8")
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: ps
        ps = cf.DomainAncillary()
        ps.standard_name = "surface_air_pressure"
        data = cf.Data(1000, units="hPa", dtype="f8")
        ps.set_data(data)
        ps_key = field.set_construct(ps, axes=(), copy=False)

        # domain_ancillary: p0
        p0 = cf.DomainAncillary()
        data = cf.Data(1000, units="hPa", dtype="f8")
        p0.set_data(data)
        p0_key = field.set_construct(p0, axes=(), copy=False)

        # domain_ancillary: a
        a = cf.DomainAncillary()
        data = cf.Data([0.6, 0.3, 0], dtype="f8")
        a.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.75, 0.45], [0.45, 0.15], [0.15, 0]])
        bounds.set_data(data)
        a.set_bounds(bounds)
        a_key = field.set_construct(a, axes=axisZ, copy=False)

        # domain_ancillary: b
        b = cf.DomainAncillary()
        data = cf.Data([0.1, 0.2, 0.3], dtype="f8")
        b.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.05, 0.15], [0.15, 0.25], [0.25, 0.2]])
        bounds.set_data(data)
        b.set_bounds(bounds)
        b_key = field.set_construct(b, axes=axisZ, copy=False)

        # dimension_coordinate: sigma
        sigma = cf.DimensionCoordinate()
        sigma.standard_name = standard_name
        data = cf.Data([0.6969697, 0.49494949, 0.29292929])
        sigma.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [
                [0.7979798, 0.5959596],
                [0.5959596, 0.39393939],
                [0.39393939, 0.19191919],
            ]
        )
        bounds.set_data(data)
        sigma.set_bounds(bounds)
        sigma_key = field.set_construct(sigma, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sigma_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {"p0": p0_key, "a": a_key, "b": b_key, "ps": ps_key}
        )
        field.set_construct(coordref)

    elif standard_name == "atmosphere_sleve_coordinate":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([100, 200, 300], "m", dtype="f8")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[50, 150], [150, 250], [250, 350]], "m", dtype="f8")
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: ztop
        ztop = cf.DomainAncillary()
        ztop.standard_name = "altitude_at_top_of_atmosphere_model"
        data = cf.Data(1000, units="m", dtype="f8")
        ztop.set_data(data)
        ztop_key = field.set_construct(ztop, axes=(), copy=False)

        # domain_ancillary: zsurf1
        zsurf1 = cf.DomainAncillary()
        data = cf.Data(90, units="m", dtype="f8")
        zsurf1.set_data(data)
        zsurf1_key = field.set_construct(zsurf1, axes=(), copy=False)

        # domain_ancillary: zsurf2
        zsurf2 = cf.DomainAncillary()
        data = cf.Data(0.1, units="m", dtype="f8")
        zsurf2.set_data(data)
        zsurf2_key = field.set_construct(zsurf2, axes=(), copy=False)

        # domain_ancillary: b1
        b1 = cf.DomainAncillary()
        data = cf.Data([0.05, 0.04, 0.03], dtype="f8")
        b1.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.055, 0.045], [0.045, 0.035], [0.035, 0.025]])
        bounds.set_data(data)
        b1.set_bounds(bounds)
        b1_key = field.set_construct(b1, axes=axisZ, copy=False)

        # domain_ancillary: b2
        b2 = cf.DomainAncillary()
        data = cf.Data([0.5, 0.4, 0.3])
        b2.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.55, 0.45], [0.45, 0.35], [0.35, 0.25]])
        bounds.set_data(data)
        b2.set_bounds(bounds)
        b2_key = field.set_construct(b2, axes=axisZ, copy=False)

        # domain_ancillary: a
        a = cf.DomainAncillary()
        data = cf.Data([0.09545, 0.19636, 0.29727])
        a.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [[0.044995, 0.145905], [0.145905, 0.246815], [0.246815, 0.347725]]
        )
        bounds.set_data(data)
        a.set_bounds(bounds)
        a_key = field.set_construct(a, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.coordinate_conversion.set_domain_ancillaries(
            {
                "zsurf1": zsurf1_key,
                "a": a_key,
                "b1": b1_key,
                "b2": b2_key,
                "zsurf2": zsurf2_key,
                "ztop": ztop_key,
            }
        )
        field.set_construct(coordref)

    elif standard_name == "ocean_sigma_coordinate":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([10, 20, 30], "m", dtype="f8")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[5, 15], [15, 25], [25, 35]], "m", dtype="f8")
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: depth
        depth = cf.DomainAncillary()
        depth.standard_name = "sea_floor_depth_below_geoid"
        data = cf.Data(-1000.0, units="m")
        depth.set_data(data)
        depth_key = field.set_construct(depth, axes=(), copy=False)

        # domain_ancillary: eta
        eta = cf.DomainAncillary()
        eta.standard_name = "sea_surface_height_above_geoid"
        data = cf.Data(100.0, units="m")
        eta.set_data(data)
        eta_key = field.set_construct(eta, axes=(), copy=False)

        # domain_ancillary: sigma
        sigma = cf.DomainAncillary()
        sigma.standard_name = standard_name
        data = cf.Data([0.1, 0.08888888888888889, 0.07777777777777778])
        sigma.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [
                [0.10555556, 0.09444444],
                [0.09444444, 0.08333333],
                [0.08333333, 0.07222222],
            ]
        )
        bounds.set_data(data)
        sigma.set_bounds(bounds)
        sigma_key = field.set_construct(sigma, axes=axisZ, copy=False)

        # dimension_coordinate: sigma
        sigmac = cf.DimensionCoordinate(source=sigma)
        sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sigmac_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {"depth": depth_key, "eta": eta_key, "sigma": sigma_key}
        )
        field.set_construct(coordref)

    elif standard_name == "ocean_s_coordinate":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([15.01701191, 31.86034296, 40.31150319], units="m")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [
                [15.01701191, 23.42877638],
                [23.42877638, 31.86034296],
                [31.86034296, 40.31150319],
            ],
            units="m",
        )
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: depth
        depth = cf.DomainAncillary()
        depth.standard_name = "sea_floor_depth_below_geoid"
        data = cf.Data(-1000.0, units="m")
        depth.set_data(data)
        depth_key = field.set_construct(depth, axes=(), copy=False)

        # domain_ancillary: eta
        eta = cf.DomainAncillary()
        eta.standard_name = "sea_surface_height_above_geoid"
        data = cf.Data(100.0, units="m")
        eta.set_data(data)
        eta_key = field.set_construct(eta, axes=(), copy=False)

        # domain_ancillary: depth_c
        depth_c = cf.DomainAncillary()
        data = cf.Data(10.0, units="m")
        depth_c.set_data(data)
        depth_c_key = field.set_construct(depth_c, axes=(), copy=False)

        # domain_ancillary: a
        a = cf.DomainAncillary()
        data = cf.Data(0.5)
        a.set_data(data)
        a_key = field.set_construct(a, axes=(), copy=False)

        # domain_ancillary: b
        b = cf.DomainAncillary()
        data = cf.Data(0.75)
        b.set_data(data)
        b_key = field.set_construct(b, axes=(), copy=False)

        # domain_ancillary: s
        s = cf.DomainAncillary()
        s.standard_name = standard_name
        data = cf.Data([0.1, 0.08, 0.07])
        s.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]])
        bounds.set_data(data)
        s.set_bounds(bounds)
        s_key = field.set_construct(s, axes=axisZ, copy=False)

        # dimension_coordinate: s
        sc = cf.DimensionCoordinate(source=s)
        sc_key = field.set_construct(sc, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sc_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {
                "depth": depth_key,
                "eta": eta_key,
                "depth_c": depth_c_key,
                "a": a_key,
                "b": b_key,
                "s": s_key,
            }
        )
        field.set_construct(coordref)

    elif standard_name == "ocean_s_coordinate_g1":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([555.4, 464.32, 373.33], units="m")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [[600.85, 509.86], [509.86, 418.87], [418.87, 327.88]], units="m"
        )
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: depth
        depth = cf.DomainAncillary()
        depth.standard_name = "sea_floor_depth_below_geoid"
        data = cf.Data(-1000.0, units="m")
        depth.set_data(data)
        depth_key = field.set_construct(depth, axes=(), copy=False)

        # domain_ancillary: eta
        eta = cf.DomainAncillary()
        eta.standard_name = "sea_surface_height_above_geoid"
        data = cf.Data(100.0, units="m")
        eta.set_data(data)
        eta_key = field.set_construct(eta, axes=(), copy=False)

        # domain_ancillary: depth_c
        depth_c = cf.DomainAncillary()
        data = cf.Data(10.0, units="m")
        depth_c.set_data(data)
        depth_c_key = field.set_construct(depth_c, axes=(), copy=False)

        # domain_ancillary: C
        C = cf.DomainAncillary()
        data = cf.Data([-0.5, -0.4, -0.3])
        C.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[-0.55, -0.45], [-0.45, -0.35], [-0.35, -0.25]])
        bounds.set_data(data)
        C.set_bounds(bounds)
        C_key = field.set_construct(C, axes=axisZ, copy=False)

        # domain_ancillary: s
        s = cf.DomainAncillary()
        s.standard_name = standard_name
        data = cf.Data([0.1, 0.08, 0.07])
        s.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]])
        bounds.set_data(data)
        s.set_bounds(bounds)
        s_key = field.set_construct(s, axes=axisZ, copy=False)

        # dimension_coordinate: s
        sc = cf.DimensionCoordinate(source=s)
        sc_key = field.set_construct(sc, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sc_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {
                "depth": depth_key,
                "eta": eta_key,
                "depth_c": depth_c_key,
                "C": C_key,
                "s": s_key,
            }
        )
        field.set_construct(coordref)

    elif standard_name == "ocean_s_coordinate_g2":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([555.45454545, 464.36363636, 373.36363636], units="m")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [
                [600.90909091, 509.90909091],
                [509.90909091, 418.90909091],
                [418.90909091, 327.90909091],
            ],
            units="m",
        )
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: depth
        depth = cf.DomainAncillary()
        depth.standard_name = "sea_floor_depth_below_geoid"
        data = cf.Data(-1000.0, units="m")
        depth.set_data(data)
        depth_key = field.set_construct(depth, axes=(), copy=False)

        # domain_ancillary: eta
        eta = cf.DomainAncillary()
        eta.standard_name = "sea_surface_height_above_geoid"
        data = cf.Data(100.0, units="m")
        eta.set_data(data)
        eta_key = field.set_construct(eta, axes=(), copy=False)

        # domain_ancillary: depth_c
        depth_c = cf.DomainAncillary()
        data = cf.Data(10.0, units="m")
        depth_c.set_data(data)
        depth_c_key = field.set_construct(depth_c, axes=(), copy=False)

        # domain_ancillary: C
        C = cf.DomainAncillary()
        data = cf.Data([-0.5, -0.4, -0.3])
        C.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[-0.55, -0.45], [-0.45, -0.35], [-0.35, -0.25]])
        bounds.set_data(data)
        C.set_bounds(bounds)
        C_key = field.set_construct(C, axes=axisZ, copy=False)

        # domain_ancillary: s
        s = cf.DomainAncillary()
        s.standard_name = standard_name
        data = cf.Data([0.1, 0.08, 0.07])
        s.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]])
        bounds.set_data(data)
        s.set_bounds(bounds)
        s_key = field.set_construct(s, axes=axisZ, copy=False)

        # dimension_coordinate: s
        sc = cf.DimensionCoordinate(source=s)
        sc_key = field.set_construct(sc, axes=axisZ, copy=False)

        # coordinat
        # coordinate_reference:
        coordref.set_coordinates({sc_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {
                "depth": depth_key,
                "eta": eta_key,
                "depth_c": depth_c_key,
                "C": C_key,
                "s": s_key,
            }
        )
        field.set_construct(coordref)

    elif standard_name == "ocean_sigma_z_coordinate":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data([10.0, 30.0, 40.0], "m", dtype="f8")
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [[10.0, 19.0], [25.0, 35.0], [35.0, 45.0]], "m", dtype="f8"
        )
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: depth
        depth = cf.DomainAncillary()
        depth.standard_name = "sea_floor_depth_below_geoid"
        data = cf.Data(-1000.0, units="m")
        depth.set_data(data)
        depth_key = field.set_construct(depth, axes=(), copy=False)

        # domain_ancillary: eta
        eta = cf.DomainAncillary()
        eta.standard_name = "sea_surface_height_above_geoid"
        data = cf.Data(100.0, units="m")
        eta.set_data(data)
        eta_key = field.set_construct(eta, axes=(), copy=False)

        # domain_ancillary: depth_c
        depth_c = cf.DomainAncillary()
        data = cf.Data(10.0, units="m")
        depth_c.set_data(data)
        depth_c_key = field.set_construct(depth_c, axes=(), copy=False)

        # domain_ancillary: nsigma
        nsigma = cf.DomainAncillary()
        data = cf.Data(1)
        nsigma.set_data(data)
        nsigma_key = field.set_construct(nsigma, axes=(), copy=False)

        # domain_ancillary: zlev
        zlev = cf.DomainAncillary()
        zlev.standard_name = "altitude"
        data = cf.Data([20, 30, 40], units="m", dtype="f8")
        zlev.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[15, 25], [25, 35], [35, 45]], units="m", dtype="f8")
        bounds.set_data(data)
        zlev.set_bounds(bounds)
        zlev_key = field.set_construct(zlev, axes=axisZ, copy=False)

        # domain_ancillary: sigma
        sigma = cf.DomainAncillary()
        sigma.standard_name = standard_name
        data = cf.Data([0.1, 0.08, 0.07])
        sigma.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]])
        bounds.set_data(data)
        sigma.set_bounds(bounds)
        sigma_key = field.set_construct(sigma, axes=axisZ, copy=False)

        # dimension_coordinate: sigma
        sigmac = cf.DimensionCoordinate(source=sigma)
        sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sigmac_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {
                "depth": depth_key,
                "eta": eta_key,
                "depth_c": depth_c_key,
                "nsigma": nsigma_key,
                "zlev": zlev_key,
                "sigma": sigma_key,
            }
        )
        field.set_construct(coordref)

    elif standard_name == "ocean_double_sigma_coordinate":
        computed_standard_name = "altitude"

        # Computed vertical corodinates
        aux.standard_name = computed_standard_name
        data = cf.Data(
            [0.15000000000000002, 0.12, 932.895], units="m", dtype="f8"
        )
        aux.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data(
            [
                [1.50000e-01, 1.35000e-01],
                [1.35000e-01, 1.20000e-01],
                [9.22880e02, 9.32895e02],
            ],
            units="m",
            dtype="f8",
        )
        bounds.set_data(data)
        aux.set_bounds(bounds)

        # domain_ancillary: depth
        depth = cf.DomainAncillary()
        depth.standard_name = "sea_floor_depth_below_geoid"
        data = cf.Data(-1000.0, units="m")
        depth.set_data(data)
        depth_key = field.set_construct(depth, axes=(), copy=False)

        # domain_ancillary: z1
        z1 = cf.DomainAncillary()
        data = cf.Data(2, units="m")
        z1.set_data(data)
        z1_key = field.set_construct(z1, axes=(), copy=False)

        # domain_ancillary: z2
        z2 = cf.DomainAncillary()
        data = cf.Data(1.5, units="m")
        z2.set_data(data)
        z2_key = field.set_construct(z2, axes=(), copy=False)

        # domain_ancillary: a
        a = cf.DomainAncillary()
        data = cf.Data(2.5, units="m")
        a.set_data(data)
        a_key = field.set_construct(a, axes=(), copy=False)

        # domain_ancillary: href
        href = cf.DomainAncillary()
        data = cf.Data(10.5, units="m")
        href.set_data(data)
        href_key = field.set_construct(href, axes=(), copy=False)

        # domain_ancillary: k_c
        k_c = cf.DomainAncillary()
        data = cf.Data(1)
        k_c.set_data(data)
        k_c_key = field.set_construct(k_c, axes=(), copy=False)

        # dimension_coordinate: sigma
        sigma = cf.DomainAncillary()
        sigma.standard_name = standard_name
        data = cf.Data([0.1, 0.08, 0.07])
        sigma.set_data(data)
        bounds = cf.Bounds()
        data = cf.Data([[0.10, 0.09], [0.09, 0.08], [0.08, 0.07]])
        bounds.set_data(data)
        sigma.set_bounds(bounds)
        sigma_key = field.set_construct(sigma, axes=axisZ, copy=False)

        # dimension_coordinate: sigma
        sigmac = cf.DimensionCoordinate(source=sigma)
        sigmac_key = field.set_construct(sigmac, axes=axisZ, copy=False)

        # coordinate_reference:
        coordref.set_coordinates({sigmac_key})
        coordref.coordinate_conversion.set_domain_ancillaries(
            {
                "depth": depth_key,
                "a": a_key,
                "k_c": k_c_key,
                "z1": z1_key,
                "z2": z2_key,
                "href": href_key,
                "sigma": sigma_key,
            }
        )
        field.set_construct(coordref)

    else:
        raise ValueError(
            "Bad standard name: {}, "
            "not an element of FormulaTerms.standard_names".format(
                standard_name
            )
        )

    return (field, aux, computed_standard_name)
Beispiel #11
0
    def test_AuxiliaryCoordinate_bounds(self):
        d = self.f.dimension_coordinate("X")
        x = cf.AuxiliaryCoordinate(source=d)

        x.upper_bounds
        x.lower_bounds
Beispiel #12
0
class AuxiliaryCoordinateTest(unittest.TestCase):
    f = cf.example_field(1)

    aux1 = cf.AuxiliaryCoordinate()
    aux1.standard_name = "latitude"
    a = numpy.array([
        -30,
        -23.5,
        -17.8123,
        -11.3345,
        -0.7,
        -0.2,
        0,
        0.2,
        0.7,
        11.30003,
        17.8678678,
        23.5,
        30,
    ])
    aux1.set_data(cf.Data(a, "degrees_north"))
    bounds = cf.Bounds()
    b = numpy.empty(a.shape + (2, ))
    b[:, 0] = a - 0.1
    b[:, 1] = a + 0.1
    bounds.set_data(cf.Data(b))
    aux1.set_bounds(bounds)

    def test_AuxiliaryCoordinate_mask_invalid(self):
        a = self.aux1.copy()

        a.mask_invalid()
        self.assertIsNone(a.mask_invalid(inplace=True))

        a.del_bounds()
        a.mask_invalid()
        self.assertIsNone(a.mask_invalid(inplace=True))

    def test_AuxiliaryCoordinate_chunk(self):
        a = self.aux1.copy()
        a.chunk()

    def test_AuxiliaryCoordinate__repr__str__dump(self):
        x = self.f.auxiliary_coordinate("latitude")
        repr(x)
        str(x)
        x.dump(display=False)

    def test_AuxiliaryCoordinate_bounds(self):
        d = self.f.dimension_coordinate("X")
        x = cf.AuxiliaryCoordinate(source=d)

        x.upper_bounds
        x.lower_bounds

    def test_AuxiliaryCoordinate_properties(self):
        x = self.f.auxiliary_coordinate("latitude")

        x.positive = "up"
        self.assertEqual(x.positive, "up")
        del x.positive
        self.assertIsNone(getattr(x, "positive", None))

        x.axis = "Z"
        self.assertEqual(x.axis, "Z")
        del x.axis
        self.assertIsNone(getattr(x, "axis", None))

        d = self.f.dimension_coordinate("X")
        x = cf.AuxiliaryCoordinate(source=d)

    def test_AuxiliaryCoordinate_insert_dimension(self):
        d = self.f.dimension_coordinate("X")
        x = cf.AuxiliaryCoordinate(source=d)

        self.assertEqual(x.shape, (9, ))
        self.assertEqual(x.bounds.shape, (9, 2))

        y = x.insert_dimension(0)
        self.assertEqual(y.shape, (1, 9))
        self.assertEqual(y.bounds.shape, (1, 9, 2), y.bounds.shape)

        x.insert_dimension(-1, inplace=True)
        self.assertEqual(x.shape, (9, 1))
        self.assertEqual(x.bounds.shape, (9, 1, 2), x.bounds.shape)

    def test_AuxiliaryCoordinate_transpose(self):
        x = self.f.auxiliary_coordinate("longitude").copy()

        bounds = cf.Bounds(
            data=cf.Data(numpy.arange(9 * 10 * 4).reshape(9, 10, 4)))
        x.set_bounds(bounds)

        self.assertEqual(x.shape, (9, 10))
        self.assertEqual(x.bounds.shape, (9, 10, 4))

        y = x.transpose()
        self.assertEqual(y.shape, (10, 9))
        self.assertEqual(y.bounds.shape, (10, 9, 4), y.bounds.shape)

        x.transpose([1, 0], inplace=True)
        self.assertEqual(x.shape, (10, 9))
        self.assertEqual(x.bounds.shape, (10, 9, 4), x.bounds.shape)

    def test_AuxiliaryCoordinate_squeeze(self):
        x = self.f.auxiliary_coordinate("longitude").copy()

        bounds = cf.Bounds(
            data=cf.Data(numpy.arange(9 * 10 * 4).reshape(9, 10, 4)))
        x.set_bounds(bounds)
        x.insert_dimension(1, inplace=True)
        x.insert_dimension(0, inplace=True)

        self.assertEqual(x.shape, (1, 9, 1, 10))
        self.assertEqual(x.bounds.shape, (1, 9, 1, 10, 4))

        y = x.squeeze()
        self.assertEqual(y.shape, (9, 10))
        self.assertEqual(y.bounds.shape, (9, 10, 4), y.bounds.shape)

        x.squeeze(2, inplace=True)
        self.assertEqual(x.shape, (1, 9, 10))
        self.assertEqual(x.bounds.shape, (1, 9, 10, 4), x.bounds.shape)

    def test_AuxiliaryCoordinate_floor(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.floor().array == numpy.floor(a)).all())
        self.assertTrue((aux.floor().bounds.array == numpy.floor(b)).all())
        self.assertTrue(
            (aux.floor(bounds=False).array == numpy.floor(a)).all())
        self.assertTrue((aux.floor(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.floor().array == numpy.floor(a)).all())
        self.assertTrue(
            (aux.floor(bounds=False).array == numpy.floor(a)).all())

        self.assertIsNone(aux.floor(inplace=True))
        self.assertTrue((aux.array == numpy.floor(a)).all())

    def test_AuxiliaryCoordinate_ceil(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.ceil().array == numpy.ceil(a)).all())
        self.assertTrue((aux.ceil().bounds.array == numpy.ceil(b)).all())
        self.assertTrue((aux.ceil(bounds=False).array == numpy.ceil(a)).all())
        self.assertTrue((aux.ceil(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.ceil().array == numpy.ceil(a)).all())
        self.assertTrue((aux.ceil(bounds=False).array == numpy.ceil(a)).all())

        self.assertIsNone(aux.ceil(inplace=True))
        self.assertTrue((aux.array == numpy.ceil(a)).all())

    def test_AuxiliaryCoordinate_trunc(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.trunc().array == numpy.trunc(a)).all())
        self.assertTrue((aux.trunc().bounds.array == numpy.trunc(b)).all())
        self.assertTrue(
            (aux.trunc(bounds=False).array == numpy.trunc(a)).all())
        self.assertTrue((aux.trunc(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.trunc().array == numpy.trunc(a)).all())
        self.assertTrue(
            (aux.trunc(bounds=False).array == numpy.trunc(a)).all())

        self.assertIsNone(aux.trunc(inplace=True))
        self.assertTrue((aux.array == numpy.trunc(a)).all())

    def test_AuxiliaryCoordinate_rint(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        x0 = aux.rint()
        x = x0.array

        self.assertTrue((x == numpy.rint(a)).all(), x)
        self.assertTrue((aux.rint().bounds.array == numpy.rint(b)).all())
        self.assertTrue((aux.rint(bounds=False).array == numpy.rint(a)).all())
        self.assertTrue((aux.rint(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.rint().array == numpy.rint(a)).all())
        self.assertTrue((aux.rint(bounds=False).array == numpy.rint(a)).all())

        self.assertIsNone(aux.rint(inplace=True))
        self.assertTrue((aux.array == numpy.rint(a)).all())

    def test_AuxiliaryCoordinate_close(self):
        aux = self.aux1.copy()
        aux.close()

    def test_AuxiliaryCoordinate_sin_cos_tan(self):
        aux = self.aux1.copy()

        aux.cos()
        self.assertIsNone(aux.cos(inplace=True))

        aux.sin()
        self.assertIsNone(aux.sin(inplace=True))

        aux.tan()
        self.assertIsNone(aux.tan(inplace=True))

    def test_AuxiliaryCoordinate_log_exp(self):
        aux = self.aux1.copy()

        aux.exp()
        self.assertIsNone(aux.exp(inplace=True))

        aux.log()
        self.assertIsNone(aux.log(inplace=True))

    def test_AuxiliaryCoordinate_count(self):
        aux = self.aux1.copy()

        aux.count()

        aux.del_data()
        with self.assertRaises(Exception):
            aux.count()

    def test_AuxiliaryCoordinate_cyclic(self):
        aux = self.aux1.copy()

        self.assertEqual(aux.cyclic(), set())
        self.assertEqual(aux.cyclic(0), set())
        self.assertEqual(aux.cyclic(), set([0]))

    def test_AuxiliaryCoordinate_roll(self):
        aux = self.aux1.copy()

        aux.roll(0, 3)
        self.assertIsNone(aux.roll(-1, 4, inplace=True))

    def test_AuxiliaryCoordinate_round(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        for decimals in (0, 1, 2, 3, 4, 5):
            aux = self.aux1.copy()

            self.assertTrue(
                (aux.round(decimals).array == numpy.round(a, decimals)).all())
            self.assertTrue((aux.round(decimals).bounds.array == numpy.round(
                b, decimals)).all())
            self.assertTrue(
                (aux.round(decimals,
                           bounds=False).array == numpy.round(a,
                                                              decimals)).all())
            self.assertTrue((aux.round(decimals,
                                       bounds=False).bounds.array == b).all())

            aux.del_bounds()
            self.assertTrue(
                (aux.round(decimals).array == numpy.round(a, decimals)).all())
            self.assertTrue(
                (aux.round(decimals,
                           bounds=False).array == numpy.round(a,
                                                              decimals)).all())

            self.assertIsNone(aux.round(decimals, inplace=True))
            self.assertTrue((aux.array == numpy.round(a, decimals)).all())

    def test_AuxiliaryCoordinate_clip(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.clip(-15, 25).array == numpy.clip(a, -15,
                                                               25)).all())
        self.assertTrue(
            (aux.clip(-15, 25).bounds.array == numpy.clip(b, -15, 25)).all())
        self.assertTrue(
            (aux.clip(-15, 25, bounds=False).array == numpy.clip(a, -15,
                                                                 25)).all())
        self.assertTrue((aux.clip(-15, 25,
                                  bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.clip(-15, 25).array == numpy.clip(a, -15,
                                                               25)).all())
        self.assertTrue(
            (aux.clip(-15, 25, bounds=False).array == numpy.clip(a, -15,
                                                                 25)).all())

        self.assertIsNone(aux.clip(-15, 25, inplace=True))
Beispiel #13
0
    def test_create_field(self):
        # Dimension coordinates
        dim1 = cf.DimensionCoordinate(
            data=cf.Data(numpy.arange(10.0), "degrees"))
        dim1.standard_name = "grid_latitude"

        dim0 = cf.DimensionCoordinate(
            data=cf.Data(numpy.arange(9.0) + 20, "degrees"))
        dim0.standard_name = "grid_longitude"
        dim0.data[-1] += 5
        bounds = cf.Data(
            numpy.array([dim0.data.array - 0.5,
                         dim0.data.array + 0.5]).transpose((1, 0)))
        bounds[-2, 1] = 30
        bounds[-1, :] = [30, 36]
        dim0.set_bounds(cf.Bounds(data=bounds))

        dim2 = cf.DimensionCoordinate(
            data=cf.Data([1.5]), bounds=cf.Bounds(data=cf.Data([[1, 2.0]])))
        dim2.standard_name = "atmosphere_hybrid_height_coordinate"

        # Auxiliary coordinates
        ak = cf.DomainAncillary(data=cf.Data([10.0], "m"))
        ak.id = "atmosphere_hybrid_height_coordinate_ak"
        bounds = cf.Bounds(data=cf.Data([[5, 15.0]], units=ak.Units))
        ak.set_bounds(bounds)

        bk = cf.DomainAncillary(data=cf.Data([20.0]))
        bk.id = "atmosphere_hybrid_height_coordinate_bk"
        bounds = cf.Bounds(data=cf.Data([[14, 26.0]]))
        bk.set_bounds(bounds)

        aux2 = cf.AuxiliaryCoordinate(data=cf.Data(
            numpy.arange(-45, 45, dtype="int32").reshape(10, 9),
            units="degree_N",
        ))
        aux2.standard_name = "latitude"

        aux3 = cf.AuxiliaryCoordinate(data=cf.Data(
            numpy.arange(60, 150, dtype="int32").reshape(9, 10),
            units="degreesE",
        ))
        aux3.standard_name = "longitude"

        aux4 = cf.AuxiliaryCoordinate(data=cf.Data(
            numpy.array(
                [
                    "alpha",
                    "beta",
                    "gamma",
                    "delta",
                    "epsilon",
                    "zeta",
                    "eta",
                    "theta",
                    "iota",
                    "kappa",
                ],
                dtype="S",
            )))
        aux4.standard_name = "greek_letters"
        aux4[0] = cf.masked

        # Cell measures
        msr0 = cf.CellMeasure(
            data=cf.Data(1 + numpy.arange(90.0).reshape(9, 10) * 1234, "km 2"))
        msr0.measure = "area"

        # Data
        data = cf.Data(numpy.arange(90.0).reshape(10, 9), "m s-1")

        properties = {"standard_name": "eastward_wind"}

        f = cf.Field(properties=properties)

        axisX = f.set_construct(cf.DomainAxis(9))
        axisY = f.set_construct(cf.DomainAxis(10))
        axisZ = f.set_construct(cf.DomainAxis(1))

        f.set_data(data)

        x = f.set_construct(dim0)
        y = f.set_construct(dim1, axes=[axisY])
        z = f.set_construct(dim2, axes=[axisZ])

        lat = f.set_construct(aux2)
        lon = f.set_construct(aux3, axes=["X", axisY])
        f.set_construct(aux4, axes=["Y"])

        ak = f.set_construct(ak, axes=["Z"])
        bk = f.set_construct(bk, axes=[axisZ])

        # Coordinate references
        coordinate_conversion = cf.CoordinateConversion(
            parameters={
                "grid_mapping_name": "rotated_latitude_longitude",
                "grid_north_pole_latitude": 38.0,
                "grid_north_pole_longitude": 190.0,
            })
        ref0 = cf.CoordinateReference(
            coordinate_conversion=coordinate_conversion,
            coordinates=[x, y, lat, lon],
        )

        f.set_construct(msr0, axes=[axisX, "Y"])

        f.set_construct(ref0)

        orog = cf.DomainAncillary()
        orog.standard_name = "surface_altitude"
        orog.set_data(cf.Data(f.array * 2, "m"))
        orog.transpose([1, 0], inplace=True)

        orog_key = f.set_construct(orog, axes=["X", axisY])

        coordinate_conversion = cf.CoordinateConversion(
            parameters={
                "standard_name": "atmosphere_hybrid_height_coordinate"
            },
            domain_ancillaries={
                "orog": orog_key,
                "a": ak,
                "b": bk
            },
        )
        ref1 = cf.CoordinateReference(
            coordinate_conversion=coordinate_conversion, coordinates=[z])

        f.set_construct(ref1)

        # Field ancillary variables
        g = cf.FieldAncillary()
        g.set_data(f.data)
        g.transpose([1, 0], inplace=True)
        g.standard_name = "ancillary0"
        g *= 0.01
        f.set_construct(g)

        g = cf.FieldAncillary()
        g.set_data(f.data)
        g.standard_name = "ancillary1"
        g *= 0.01
        f.set_construct(g)

        g = cf.FieldAncillary()
        g.set_data(f[0, :].data)
        g.squeeze(inplace=True)
        g.standard_name = "ancillary2"
        g *= 0.001
        f.set_construct(g)

        g = cf.FieldAncillary()
        g.set_data(f[:, 0].data)
        g.squeeze(inplace=True)
        g.standard_name = "ancillary3"
        g *= 0.001
        f.set_construct(g)

        f.flag_values = [1, 2, 4]
        f.flag_meanings = ["a", "bb", "ccc"]

        for cm in cf.CellMethod.create(
                "grid_longitude: mean grid_latitude: max"):
            f.set_construct(cm)

        # Write the file, and read it in
        cf.write(f, self.filename, verbose=0, string=True)

        g = cf.read(self.filename, squeeze=True, verbose=0)[0]

        self.assertTrue(g.equals(f, verbose=0),
                        "Field not equal to itself read back in")

        x = g.dump(display=False)
        x = f.dump(display=False)
class AuxiliaryCoordinateTest(unittest.TestCase):
    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            'test_file.nc')
    #    f = cf.read(filename)[0]

    aux1 = cf.AuxiliaryCoordinate()
    aux1.standard_name = 'latitude'
    a = numpy.array([
        -30, -23.5, -17.8123, -11.3345, -0.7, -0.2, 0, 0.2, 0.7, 11.30003,
        17.8678678, 23.5, 30
    ])
    aux1.set_data(cf.Data(a, 'degrees_north'))
    bounds = cf.Bounds()
    b = numpy.empty(a.shape + (2, ))
    b[:, 0] = a - 0.1
    b[:, 1] = a + 0.1
    bounds.set_data(cf.Data(b))
    aux1.set_bounds(bounds)

    def test_AuxiliaryCoordinate_mask_invalid(self):
        a = self.aux1.copy()

        _ = a.mask_invalid()
        self.assertIsNone(a.mask_invalid(inplace=True))

        a.del_bounds()
        _ = a.mask_invalid()
        self.assertIsNone(a.mask_invalid(inplace=True))

    def test_AuxiliaryCoordinate_chunk(self):
        a = self.aux1.copy()

        a.chunk()

    def test_AuxiliaryCoordinate__repr__str__dump(self):
        f = cf.read(self.filename)[0]
        x = f.auxiliary_coordinates('latitude').value()

        _ = repr(x)
        _ = str(x)
        _ = x.dump(display=False)

    def test_AuxiliaryCoordinate_bounds(self):
        f = cf.read(self.filename)[0]
        d = f.dimension_coordinates('X').value()
        x = cf.AuxiliaryCoordinate(source=d)

        _ = x.upper_bounds
        _ = x.lower_bounds

    def test_AuxiliaryCoordinate_properties(self):
        f = cf.read(self.filename)[0]
        x = f.auxiliary_coordinates('latitude').value()

        x.positive = 'up'
        self.assertEqual(x.positive, 'up')
        del x.positive
        self.assertIsNone(getattr(x, 'positive', None))

        x.axis = 'Z'
        self.assertEqual(x.axis, 'Z')
        del x.axis
        self.assertIsNone(getattr(x, 'axis', None))

        d = f.dimension_coordinates('X').value()
        x = cf.AuxiliaryCoordinate(source=d)

    def test_AuxiliaryCoordinate_insert_dimension(self):
        f = cf.read(self.filename)[0]
        d = f.dimension_coordinates('X').value()
        x = cf.AuxiliaryCoordinate(source=d)

        self.assertEqual(x.shape, (9, ))
        self.assertEqual(x.bounds.shape, (9, 2))

        y = x.insert_dimension(0)
        self.assertEqual(y.shape, (1, 9))
        self.assertEqual(y.bounds.shape, (1, 9, 2), y.bounds.shape)

        x.insert_dimension(-1, inplace=True)
        self.assertEqual(x.shape, (9, 1))
        self.assertEqual(x.bounds.shape, (9, 1, 2), x.bounds.shape)

    def test_AuxiliaryCoordinate_transpose(self):
        f = cf.read(self.filename)[0]
        x = f.auxiliary_coordinates('longitude').value()

        bounds = cf.Bounds(
            data=cf.Data(numpy.arange(9 * 10 * 4).reshape(9, 10, 4)))
        x.set_bounds(bounds)

        self.assertEqual(x.shape, (9, 10))
        self.assertEqual(x.bounds.shape, (9, 10, 4))

        y = x.transpose()
        self.assertEqual(y.shape, (10, 9))
        self.assertEqual(y.bounds.shape, (10, 9, 4), y.bounds.shape)

        x.transpose([1, 0], inplace=True)
        self.assertEqual(x.shape, (10, 9))
        self.assertEqual(x.bounds.shape, (10, 9, 4), x.bounds.shape)

    def test_AuxiliaryCoordinate_squeeze(self):
        f = cf.read(self.filename)[0]
        x = f.auxiliary_coordinates('longitude').value()

        bounds = cf.Bounds(
            data=cf.Data(numpy.arange(9 * 10 * 4).reshape(9, 10, 4)))
        x.set_bounds(bounds)
        x.insert_dimension(1, inplace=True)
        x.insert_dimension(0, inplace=True)

        self.assertEqual(x.shape, (1, 9, 1, 10))
        self.assertEqual(x.bounds.shape, (1, 9, 1, 10, 4))

        y = x.squeeze()
        self.assertEqual(y.shape, (9, 10))
        self.assertEqual(y.bounds.shape, (9, 10, 4), y.bounds.shape)

        x.squeeze(2, inplace=True)
        self.assertEqual(x.shape, (1, 9, 10))
        self.assertEqual(x.bounds.shape, (1, 9, 10, 4), x.bounds.shape)

    def test_AuxiliaryCoordinate_floor(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.floor().array == numpy.floor(a)).all())
        self.assertTrue((aux.floor().bounds.array == numpy.floor(b)).all())
        self.assertTrue(
            (aux.floor(bounds=False).array == numpy.floor(a)).all())
        self.assertTrue((aux.floor(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.floor().array == numpy.floor(a)).all())
        self.assertTrue(
            (aux.floor(bounds=False).array == numpy.floor(a)).all())

        self.assertIsNone(aux.floor(inplace=True))
        self.assertTrue((aux.array == numpy.floor(a)).all())

    def test_AuxiliaryCoordinate_ceil(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.ceil().array == numpy.ceil(a)).all())
        self.assertTrue((aux.ceil().bounds.array == numpy.ceil(b)).all())
        self.assertTrue((aux.ceil(bounds=False).array == numpy.ceil(a)).all())
        self.assertTrue((aux.ceil(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.ceil().array == numpy.ceil(a)).all())
        self.assertTrue((aux.ceil(bounds=False).array == numpy.ceil(a)).all())

        self.assertIsNone(aux.ceil(inplace=True))
        self.assertTrue((aux.array == numpy.ceil(a)).all())

    def test_AuxiliaryCoordinate_trunc(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.trunc().array == numpy.trunc(a)).all())
        self.assertTrue((aux.trunc().bounds.array == numpy.trunc(b)).all())
        self.assertTrue(
            (aux.trunc(bounds=False).array == numpy.trunc(a)).all())
        self.assertTrue((aux.trunc(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.trunc().array == numpy.trunc(a)).all())
        self.assertTrue(
            (aux.trunc(bounds=False).array == numpy.trunc(a)).all())

        self.assertIsNone(aux.trunc(inplace=True))
        self.assertTrue((aux.array == numpy.trunc(a)).all())

    def test_AuxiliaryCoordinate_rint(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        x0 = aux.rint()
        x = x0.array

        self.assertTrue((x == numpy.rint(a)).all(), x)
        self.assertTrue((aux.rint().bounds.array == numpy.rint(b)).all())
        self.assertTrue((aux.rint(bounds=False).array == numpy.rint(a)).all())
        self.assertTrue((aux.rint(bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.rint().array == numpy.rint(a)).all())
        self.assertTrue((aux.rint(bounds=False).array == numpy.rint(a)).all())

        self.assertIsNone(aux.rint(inplace=True))
        self.assertTrue((aux.array == numpy.rint(a)).all())

    def test_AuxiliaryCoordinate_close(self):
        aux = self.aux1.copy()

        aux.close()

    def test_AuxiliaryCoordinate_sin_cos_tan(self):
        aux = self.aux1.copy()

        _ = aux.cos()
        self.assertIsNone(aux.cos(inplace=True))

        _ = aux.sin()
        self.assertIsNone(aux.sin(inplace=True))

        _ = aux.tan()
        self.assertIsNone(aux.tan(inplace=True))

    def test_AuxiliaryCoordinate_log_exp(self):
        aux = self.aux1.copy()

        _ = aux.exp()
        self.assertIsNone(aux.exp(inplace=True))

        _ = aux.log()
        self.assertIsNone(aux.log(inplace=True))

    def test_AuxiliaryCoordinate_count(self):
        aux = self.aux1.copy()

        _ = aux.count()

        aux.del_data()
        with self.assertRaises(Exception):
            aux.count()

    def test_AuxiliaryCoordinate_cyclic(self):
        aux = self.aux1.copy()

        self.assertEqual(aux.cyclic(), set())
        self.assertEqual(aux.cyclic(0), set())
        self.assertEqual(aux.cyclic(), set([0]))

    def test_AuxiliaryCoordinate_roll(self):
        aux = self.aux1.copy()

        _ = aux.roll(0, 3)
        self.assertIsNone(aux.roll(-1, 4, inplace=True))

    def test_AuxiliaryCoordinate_round(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        for decimals in (0, 1, 2, 3, 4, 5):
            aux = self.aux1.copy()

            self.assertTrue(
                (aux.round(decimals).array == numpy.round(a, decimals)).all())
            self.assertTrue((aux.round(decimals).bounds.array == numpy.round(
                b, decimals)).all())
            self.assertTrue(
                (aux.round(decimals,
                           bounds=False).array == numpy.round(a,
                                                              decimals)).all())
            self.assertTrue((aux.round(decimals,
                                       bounds=False).bounds.array == b).all())

            aux.del_bounds()
            self.assertTrue(
                (aux.round(decimals).array == numpy.round(a, decimals)).all())
            self.assertTrue(
                (aux.round(decimals,
                           bounds=False).array == numpy.round(a,
                                                              decimals)).all())

            self.assertIsNone(aux.round(decimals, inplace=True))
            self.assertTrue((aux.array == numpy.round(a, decimals)).all())

    def test_AuxiliaryCoordinate_clip(self):
        aux = self.aux1.copy()

        a = aux.array
        b = aux.bounds.array

        self.assertTrue((aux.clip(-15, 25).array == numpy.clip(a, -15,
                                                               25)).all())
        self.assertTrue(
            (aux.clip(-15, 25).bounds.array == numpy.clip(b, -15, 25)).all())
        self.assertTrue(
            (aux.clip(-15, 25, bounds=False).array == numpy.clip(a, -15,
                                                                 25)).all())
        self.assertTrue((aux.clip(-15, 25,
                                  bounds=False).bounds.array == b).all())

        aux.del_bounds()
        self.assertTrue((aux.clip(-15, 25).array == numpy.clip(a, -15,
                                                               25)).all())
        self.assertTrue(
            (aux.clip(-15, 25, bounds=False).array == numpy.clip(a, -15,
                                                                 25)).all())

        self.assertIsNone(aux.clip(-15, 25, inplace=True))
    def test_create_field(self):
        # Dimension coordinates
        dim1 = cf.DimensionCoordinate(
            data=cf.Data(numpy.arange(10.), 'degrees'))
        dim1.standard_name = 'grid_latitude'

        dim0 = cf.DimensionCoordinate(
            data=cf.Data(numpy.arange(9.) + 20, 'degrees'))
        dim0.standard_name = 'grid_longitude'
        dim0.data[-1] += 5
        bounds = cf.Data(numpy.array(
            [dim0.data.array-0.5, dim0.data.array+0.5]).transpose((1, 0)))
        bounds[-2, 1] = 30
        bounds[-1, :] = [30, 36]
        dim0.set_bounds(cf.Bounds(data=bounds))

        dim2 = cf.DimensionCoordinate(
            data=cf.Data([1.5]),
            bounds=cf.Bounds(data=cf.Data([[1, 2.]]))
        )
        dim2.standard_name = 'atmosphere_hybrid_height_coordinate'

        # Auxiliary coordinates
        ak = cf.DomainAncillary(data=cf.Data([10.], 'm'))
        ak.id = 'atmosphere_hybrid_height_coordinate_ak'
        bounds = cf.Bounds(data=cf.Data([[5, 15.]], units=ak.Units))
        ak.set_bounds(bounds)

        bk = cf.DomainAncillary(data=cf.Data([20.]))
        bk.id = 'atmosphere_hybrid_height_coordinate_bk'
        bounds = cf.Bounds(data=cf.Data([[14, 26.]]))
        bk.set_bounds(bounds)

        aux2 = cf.AuxiliaryCoordinate(
            data=cf.Data(numpy.arange(-45, 45, dtype='int32').reshape(10, 9),
                         units='degree_N'))
        aux2.standard_name = 'latitude'

        aux3 = cf.AuxiliaryCoordinate(
            data=cf.Data(numpy.arange(60, 150, dtype='int32').reshape(9, 10),
                         units='degreesE'))
        aux3.standard_name = 'longitude'

        aux4 = cf.AuxiliaryCoordinate(
            data=cf.Data(numpy.array(
                ['alpha', 'beta', 'gamma', 'delta', 'epsilon',
                 'zeta', 'eta', 'theta', 'iota', 'kappa'],
                dtype='S'
            ))
        )
        aux4.standard_name = 'greek_letters'
        aux4[0] = cf.masked

        # Cell measures
        msr0 = cf.CellMeasure(
            data=cf.Data(1+numpy.arange(90.).reshape(9, 10)*1234, 'km 2'))
        msr0.measure = 'area'

        # Data
        data = cf.Data(numpy.arange(90.).reshape(10, 9), 'm s-1')

        properties = {'standard_name': 'eastward_wind'}

        f = cf.Field(properties=properties)

        axisX = f.set_construct(cf.DomainAxis(9))
        axisY = f.set_construct(cf.DomainAxis(10))
        axisZ = f.set_construct(cf.DomainAxis(1))

        f.set_data(data)

        x = f.set_construct(dim0)
        y = f.set_construct(dim1, axes=[axisY])
        z = f.set_construct(dim2, axes=[axisZ])

        lat = f.set_construct(aux2)
        lon = f.set_construct(aux3, axes=['X', axisY])
        f.set_construct(aux4, axes=['Y'])

        ak = f.set_construct(ak, axes=['Z'])
        bk = f.set_construct(bk, axes=[axisZ])

        # Coordinate references
        coordinate_conversion = cf.CoordinateConversion(
            parameters={'grid_mapping_name': 'rotated_latitude_longitude',
                        'grid_north_pole_latitude': 38.0,
                        'grid_north_pole_longitude': 190.0})
        ref0 = cf.CoordinateReference(
            coordinate_conversion=coordinate_conversion,
            coordinates=[x, y, lat, lon]
        )

        f.set_construct(msr0, axes=[axisX, 'Y'])

        f.set_construct(ref0)

        orog = cf.DomainAncillary()
        orog.standard_name = 'surface_altitude'
        orog.set_data(cf.Data(f.array*2, 'm'))
        orog.transpose([1, 0], inplace=True)

        orog_key = f.set_construct(orog, axes=['X', axisY])

        coordinate_conversion = cf.CoordinateConversion(
            parameters={
                'standard_name': 'atmosphere_hybrid_height_coordinate'
            },
            domain_ancillaries={
                'orog': orog_key,
                'a': ak,
                'b': bk
            }
        )
        ref1 = cf.CoordinateReference(
            coordinate_conversion=coordinate_conversion, coordinates=[z])

        f.set_construct(ref1)

        # Field ancillary variables
        g = cf.FieldAncillary()
        g.set_data(f.data)
        g.transpose([1, 0], inplace=True)
        g.standard_name = 'ancillary0'
        g *= 0.01
        f.set_construct(g)

        g = cf.FieldAncillary()
        g.set_data(f.data)
        g.standard_name = 'ancillary1'
        g *= 0.01
        f.set_construct(g)

        g = cf.FieldAncillary()
        g.set_data(f[0, :].data)
        g.squeeze(inplace=True)
        g.standard_name = 'ancillary2'
        g *= 0.001
        f.set_construct(g)

        g = cf.FieldAncillary()
        g.set_data(f[:, 0].data)
        g.squeeze(inplace=True)
        g.standard_name = 'ancillary3'
        g *= 0.001
        f.set_construct(g)

        f.flag_values = [1, 2, 4]
        f.flag_meanings = ['a', 'bb', 'ccc']

        for cm in cf.CellMethod.create(
                'grid_longitude: mean grid_latitude: max'):
            f.set_construct(cm)

        # Write the file, and read it in
        cf.write(f, self.filename, verbose=0, string=True)

        g = cf.read(self.filename, squeeze=True, verbose=0)[0]

        self.assertTrue(g.equals(f, verbose=0),
                        "Field not equal to itself read back in")

        x = g.dump(display=False)
        x = f.dump(display=False)
Beispiel #16
0
    def save_datasets(self, datasets, filename, **kwargs):
        """Save all datasets to one or more files."""
        LOG.info('Saving datasets to NetCDF4/CF.')
        fields = []
        shapes = {}
        for dataset in datasets:
            if dataset.shape in shapes:
                domain = shapes[dataset.shape]
            else:
                lines, pixels = dataset.shape

                area = dataset.info.get('area')
                add_time = False
                try:
                    # Create a longitude auxiliary coordinate
                    lat = cf.AuxiliaryCoordinate(
                        data=cf.Data(area.lats, 'degrees_north'))
                    lat.standard_name = 'latitude'

                    # Create a latitude auxiliary coordinate
                    lon = cf.AuxiliaryCoordinate(
                        data=cf.Data(area.lons, 'degrees_east'))
                    lon.standard_name = 'longitude'
                    aux = [lat, lon]
                    add_time = True
                except AttributeError:
                    LOG.info('No longitude and latitude data to save.')
                    aux = None

                try:
                    grid_mapping = create_grid_mapping(area)
                    units = area.proj_dict.get('units', 'm')

                    line_coord = cf.DimensionCoordinate(
                        data=cf.Data(area.proj_y_coords, units))
                    line_coord.standard_name = "projection_y_coordinate"
                    pixel_coord = cf.DimensionCoordinate(
                        data=cf.Data(area.proj_x_coords, units))
                    pixel_coord.standard_name = "projection_x_coordinate"
                    add_time = True

                except (AttributeError, NotImplementedError):
                    LOG.info('No grid mapping to save.')
                    grid_mapping = None
                    line_coord = cf.DimensionCoordinate(
                        data=cf.Data(np.arange(lines), '1'))
                    line_coord.standard_name = "line"
                    pixel_coord = cf.DimensionCoordinate(
                        data=cf.Data(np.arange(pixels), '1'))
                    pixel_coord.standard_name = "pixel"

                start_time = cf.dt(dataset.info['start_time'])
                end_time = cf.dt(dataset.info['end_time'])
                middle_time = cf.dt((dataset.info['end_time'] -
                                     dataset.info['start_time']) / 2 +
                                    dataset.info['start_time'])
                # import ipdb
                # ipdb.set_trace()
                if add_time:
                    info = dataset.info
                    dataset = dataset[np.newaxis, :, :]
                    dataset.info = info

                    bounds = cf.CoordinateBounds(
                        data=cf.Data([start_time, end_time],
                                     cf.Units('days since 1970-1-1')))
                    time_coord = cf.DimensionCoordinate(
                        properties=dict(standard_name='time'),
                        data=cf.Data(middle_time,
                                     cf.Units('days since 1970-1-1')),
                        bounds=bounds)
                    coords = [time_coord, line_coord, pixel_coord]
                else:
                    coords = [line_coord, pixel_coord]

                domain = cf.Domain(dim=coords, aux=aux, ref=grid_mapping)
                shapes[dataset.shape] = domain
            data = cf.Data(dataset, dataset.info.get('units', 'm'))

            # import ipdb
            # ipdb.set_trace()

            wanted_keys = ['standard_name', 'long_name']
            properties = {
                k: dataset.info[k]
                for k in set(wanted_keys) & set(dataset.info.keys())
            }
            new_field = cf.Field(properties=properties,
                                 data=data,
                                 domain=domain)

            new_field._FillValue = dataset.fill_value
            try:
                new_field.valid_range = dataset.info['valid_range']
            except KeyError:
                new_field.valid_range = new_field.min(), new_field.max()
            new_field.Conventions = 'CF-1.7'
            fields.append(new_field)

        fields[0].history = ("Created by pytroll/satpy on " +
                             str(datetime.utcnow()))

        flist = cf.FieldList(fields)

        cf.write(flist, filename, fmt='NETCDF4', compress=6)