def test_FieldList_select(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cf.FieldList(self.x)

        g = f('not this one')
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 0)

        g = f('air_temperature')
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 1, len(g))

        g = f(re.compile('^air'))
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 1, len(g))

        f *= 9
        f[4] = f[0].copy()
        f[4].standard_name = 'this one'
        f[6] = f[0].copy()
        f[6].standard_name = 'this one'

        g = f(re.compile('^air'))
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 7, len(g))

        g = f('this one')
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 2)

        # select_by_Units
        f[1] = f[1].override_units(cf.Units('watt'))
        f[3] = f[3].override_units(cf.Units('K @ 273.15'))

        self.assertEqual(len(f.select_by_units()), 9)
        self.assertEqual(len(f.select_by_units(cf.Units('K'))), 7)
        self.assertEqual(
            len(f.select_by_units(cf.Units('K'), exact=False)), 8)
        self.assertEqual(len(f.select_by_units('K')), 7)
        self.assertEqual(len(f.select_by_units('K', exact=False)), 8)
        self.assertEqual(len(f.select_by_units(re.compile('^K @|watt'))), 2)

        self.assertEqual(len(f.select_by_units('long_name=qwery:asd')), 0)

        # select_by_ncvar
        for a in f:
            a.nc_set_variable('ta')

        f[1].nc_set_variable('qwerty')
        f[4].nc_set_variable('ta2')

        self.assertEqual(len(f.select_by_ncvar()), 9)
        self.assertEqual(len(f.select_by_ncvar('qwerty')), 1)
        self.assertEqual(len(f.select_by_ncvar('ta')), 7)
        self.assertEqual(len(f.select_by_ncvar('ta2')), 1)
        self.assertEqual(len(f.select_by_ncvar(re.compile('^ta'))), 8)
Esempio n. 2
0
    def test_FieldList_select(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cf.FieldList(self.x)

        g = f("not this one")
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 0)

        g = f("air_temperature")
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 1, len(g))

        g = f(re.compile("^air"))
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 1, len(g))

        f *= 9
        f[4] = f[0].copy()
        f[4].standard_name = "this one"
        f[6] = f[0].copy()
        f[6].standard_name = "this one"

        g = f(re.compile("^air"))
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 7, len(g))

        g = f("this one")
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 2)

        # select_by_Units
        f[1] = f[1].override_units(cf.Units("watt"))
        f[3] = f[3].override_units(cf.Units("K @ 273.15"))

        self.assertEqual(len(f.select_by_units()), 9)
        self.assertEqual(len(f.select_by_units(cf.Units("K"))), 7)
        self.assertEqual(len(f.select_by_units(cf.Units("K"), exact=False)), 8)
        self.assertEqual(len(f.select_by_units("K")), 7)
        self.assertEqual(len(f.select_by_units("K", exact=False)), 8)
        self.assertEqual(len(f.select_by_units(re.compile("^K @|watt"))), 2)

        self.assertEqual(len(f.select_by_units("long_name=qwery:asd")), 0)

        # select_by_ncvar
        for a in f:
            a.nc_set_variable("ta")

        f[1].nc_set_variable("qwerty")
        f[4].nc_set_variable("ta2")

        self.assertEqual(len(f.select_by_ncvar()), 9)
        self.assertEqual(len(f.select_by_ncvar("qwerty")), 1)
        self.assertEqual(len(f.select_by_ncvar("ta")), 7)
        self.assertEqual(len(f.select_by_ncvar("ta2")), 1)
        self.assertEqual(len(f.select_by_ncvar(re.compile("^ta"))), 8)
Esempio n. 3
0
    def test_CoordinateReference_canonical_units(self):
        f = self.f.copy()

        self.assertIsNone(cf.CoordinateReference.canonical_units('qwerty'))
        self.assertEqual(
            cf.CoordinateReference.canonical_units('earth_radius'),
            cf.Units('m'))

        cr = f.construct('standard_name:atmosphere_hybrid_height_coordinate')
        self.assertIsNone(cr.canonical_units('qwerty'))
        self.assertEqual(cr.canonical_units('earth_radius'), cf.Units('m'))
    def test_CoordinateReference_canonical_units(self):
        f = self.f.copy()

        self.assertIsNone(cf.CoordinateReference.canonical_units("qwerty"))
        self.assertEqual(
            cf.CoordinateReference.canonical_units("earth_radius"),
            cf.Units("m"),
        )

        cr = f.construct("standard_name:atmosphere_hybrid_height_coordinate")
        self.assertIsNone(cr.canonical_units("qwerty"))
        self.assertEqual(cr.canonical_units("earth_radius"), cf.Units("m"))
Esempio n. 5
0
    def test_DimensionCoordinate_override_units(self):
        d = self.dim.copy()

        self.assertTrue(d.Units.equals(cf.Units("degrees_north")))
        self.assertTrue(d.bounds.Units.equals(cf.Units("degrees_north")))

        d.override_units("km", inplace=True)
        self.assertTrue(d.Units.equals(cf.Units("km")))
        self.assertTrue(d.bounds.Units.equals(cf.Units("km")))

        c = d.cellsize
        self.assertTrue(c.Units.equals(cf.Units("km")))
Esempio n. 6
0
def _parse_cmip6_properties(cim2_properties, global_attributes, time_coords):
    """Extends cim2 proeprty set with CMIP6 specific properties.


    """
    cim2_properties.update(
        zip(['parent_realization_index',
             'parent_initialization_index',
             'parent_physics_index',
             'parent_forcing_index'],
            map(int, re.findall('\d+', global_attributes.get('parent_variant_label', 'none')))))

    # parent_time_units
    parent_time_units = global_attributes.get('parent_time_units')
    if parent_time_units in (None, 'no parent'):
        # parent_time_units has not been set in file, so they are
        # assumed to be the same as the child time units
        parent_time_units = time_coords.Units
    else:
        # parent_time_units have been set in file
        m = re.match('(.*) *\((.*?)\)', parent_time_units)
        if m:
            parent_time_units = cf.Units(*m.groups())
        else:
            parent_time_units = cf.Units(parent_time_units,
                                         cim2_properties['calendar'])

    # ----------------------------------------------------------------
    # CIM2 branch_time_in_parent
    # ----------------------------------------------------------------
    branch_time_in_parent = global_attributes.get('branch_time_in_parent')
    if branch_time_in_parent is not None:
        if isinstance(branch_time_in_parent, basestring):
            # Fix in case branch_time_in_parent is a string
            # print "WARNING: branch_time_in_parent is a string, converting to float"
            branch_time_in_parent = float(branch_time_in_parent)

        x = cf.Data([branch_time_in_parent], units=parent_time_units).dtarray[0]
        cim2_properties['branch_time_in_parent'] = str(x)

    # ----------------------------------------------------------------
    # CIM2 branch_time_in_child
    # ----------------------------------------------------------------
    branch_time_in_child = global_attributes.get('branch_time_in_child')
    if branch_time_in_child is not None:
        if not isinstance(branch_time_in_child, float):
            # Fix in case branch_time_in_child is a string
            # print "WARNING: branch_time_in_child is a {}, converting to float".format(branch_time_in_child.__class__.__name__)
            branch_time_in_child = float(branch_time_in_child)            

        x = cf.Data([branch_time_in_child], units=time_coords.Units).dtarray[0]
        cim2_properties['branch_time_in_child'] = str(x)
Esempio n. 7
0
    def test_DimensionCoordinate_override_calendar(self):
        d = self.dim.copy()

        self.assertTrue(d.Units.equals(cf.Units("degrees_north")))
        self.assertTrue(d.bounds.Units.equals(cf.Units("degrees_north")))

        d.override_units("days since 2000-01-01", inplace=True)
        self.assertTrue(d.Units.equals(cf.Units("days since 2000-01-01")))
        self.assertTrue(
            d.bounds.Units.equals(cf.Units("days since 2000-01-01")))

        d.override_calendar("360_day", inplace=True)
        self.assertTrue(
            d.Units.equals(
                cf.Units("days since 2000-01-01", calendar="360_day")))
        self.assertTrue(
            d.bounds.Units.equals(
                cf.Units("days since 2000-01-01", calendar="360_day")))

        d.override_calendar("365_day", inplace=True)
        self.assertTrue(
            d.Units.equals(
                cf.Units("days since 2000-01-01", calendar="365_day")))
        self.assertTrue(
            d.bounds.Units.equals(
                cf.Units("days since 2000-01-01", calendar="365_day")))
Esempio n. 8
0
    def test_aggregate_bad_units(self):
        f = cf.read(self.filename, squeeze=True)[0]

        g = cf.FieldList(f[0])
        g.append(f[1:])

        h = cf.aggregate(g)
        self.assertEqual(len(h), 1)

        g[0].override_units(cf.Units("apples!"), inplace=True)
        g[1].override_units(cf.Units("oranges!"), inplace=True)

        h = cf.aggregate(g)
        self.assertEqual(len(h), 2)
Esempio n. 9
0
    def test_write_reference_datetime(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        for reference_datetime in ("1751-2-3", "1492-12-30"):
            for chunksize in self.chunk_sizes:
                cf.chunksize(chunksize)
                f = cf.read(self.filename)[0]
                t = cf.DimensionCoordinate(
                    data=cf.Data([123], "days since 1750-1-1"))

                t.standard_name = "time"
                axisT = f.set_construct(cf.DomainAxis(1))
                f.set_construct(t, axes=[axisT])
                cf.write(
                    f,
                    tmpfile,
                    fmt="NETCDF4",
                    reference_datetime=reference_datetime,
                )
                g = cf.read(tmpfile)[0]
                t = g.dimension_coordinate("T")
                self.assertEqual(
                    t.Units,
                    cf.Units("days since " + reference_datetime),
                    ("Units written were " + repr(t.Units.reftime) + " not " +
                     repr(reference_datetime)),
                )
        # --- End: for
        cf.chunksize(self.original_chunksize)
Esempio n. 10
0
    def test_PP_load_stash2standard_name(self):
        f = cf.read(self.ppfilename)[0]
        self.assertEqual(f.identity(), 'surface_temperature')
        self.assertEqual(f.Units, cf.Units('K'))

        for merge in (True, False):
            cf.load_stash2standard_name(self.new_table, merge=merge)
            f = cf.read(self.ppfilename)[0]
            self.assertEqual(f.identity(), 'NEW_NAME')
            self.assertEqual(f.Units, cf.Units('Pa'))
            cf.load_stash2standard_name()
            f = cf.read(self.ppfilename)[0]
            self.assertEqual(f.identity(), 'surface_temperature')
            self.assertEqual(f.Units, cf.Units('K'))

        cf.load_stash2standard_name()
Esempio n. 11
0
    def test_write_reference_datetime(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        for reference_datetime in ('1751-2-3', '1492-12-30'):
            for chunksize in self.chunk_sizes:
                cf.chunksize(chunksize)
                f = cf.read(self.filename)[0]
                t = cf.DimensionCoordinate(
                    data=cf.Data([123], 'days since 1750-1-1')
                )

                t.standard_name = 'time'
                axisT = f.set_construct(cf.DomainAxis(1))
                f.set_construct(t, axes=[axisT])
                cf.write(f, tmpfile, fmt='NETCDF4',
                         reference_datetime=reference_datetime)
                g = cf.read(tmpfile)[0]
                t = g.dimension_coordinate('T')
                self.assertEqual(
                    t.Units, cf.Units('days since ' + reference_datetime),
                    ('Units written were ' + repr(t.Units.reftime)
                     + ' not ' + repr(reference_datetime)))
        # --- End: for
        cf.chunksize(self.original_chunksize)
Esempio n. 12
0
    def test_load_stash2standard_name(self):
        f = cf.read(self.ppfile)[0]
        self.assertEqual(f.identity(), "eastward_wind")
        self.assertEqual(f.Units, cf.Units("m s-1"))

        for merge in (True, False):
            cf.load_stash2standard_name(self.new_table, merge=merge)
            f = cf.read(self.ppfile)[0]
            self.assertEqual(f.identity(), "NEW_NAME")
            self.assertEqual(f.Units, cf.Units("Pa"))
            cf.load_stash2standard_name()
            f = cf.read(self.ppfile)[0]
            self.assertEqual(f.identity(), "eastward_wind")
            self.assertEqual(f.Units, cf.Units("m s-1"))

        cf.load_stash2standard_name()
Esempio n. 13
0
    def test_DimensionCoordinate_cellsize(self):
        d = self.dim.copy()

        c = d.cellsize
        self.assertTrue(numpy.allclose(c.array, 0.2))

        self.assertTrue(d.Units.equals(cf.Units("degrees_north")))
        self.assertTrue(d.bounds.Units.equals(cf.Units("degrees_north")))

        d.override_units("km", inplace=True)
        self.assertTrue(d.Units.equals(cf.Units("km")))
        self.assertTrue(d.bounds.Units.equals(cf.Units("km")))

        c = d.cellsize
        self.assertTrue(c.Units.equals(cf.Units("km")))

        d.del_bounds()
        c = d.cellsize
        self.assertTrue(numpy.allclose(c.array, 0))
def wrap_netcdf(year_o, yearpart_o, var_o, standard_name_o, units_o):
    var_shape = var_o.shape

    # Define Coordinaties
    start_date = (datetime.datetime(year_o, 1, 1) +
                  datetime.timedelta(yearpart_o)).strftime('%Y-%m-%d')
    if var_shape[0] == 1:
        dim0 = cf.DimensionCoordinate(properties={'standard_name': 'time'},
                                      data=cf.Data(
                                          0.,
                                          cf.Units('days since ' + start_date,
                                                   calendar='standard')))
    elif var_shape[0] == 240:
        nc_time = (86400.0 / count_time / divt) * np.arange(count_time * divt)
        dim0 = cf.DimensionCoordinate(properties={'standard_name': 'time'},
                                      data=cf.Data(
                                          nc_time,
                                          cf.Units('seconds since ' +
                                                   start_date + ' 0:3:0',
                                                   calendar='standard')))
    elif var_shape[0] == 241:
        nc_time = (86400.0 / count_time / divt) * np.arange(count_time * divt +
                                                            1)
        dim0 = cf.DimensionCoordinate(properties={'standard_name': 'time'},
                                      data=cf.Data(
                                          nc_time,
                                          cf.Units('seconds since ' +
                                                   start_date + ' 0:0:0',
                                                   calendar='standard')))
    dim1 = cf.DimensionCoordinate(data=cf.Data(latitude, 'degrees_north'),
                                  properties={'standard_name': 'latitude'})
    dim2 = cf.DimensionCoordinate(data=cf.Data(longitude, 'degrees_east'),
                                  properties={'standard_name': 'longitude'})

    # Define cf.Field then insert variable and coordinates
    f = cf.Field(properties={'standard_name': standard_name_o})
    f.insert_dim(dim0)
    f.insert_dim(dim1)
    f.insert_dim(dim2)
    data = cf.Data(var_o, units_o)
    f.insert_data(data)

    return f
Esempio n. 15
0
def _get_simulation_start_end_dates(dates, calendar):
    """Returns the start and end times of the simulation and return them as ISO8601-like strings.

    """
    if dates:
        date = sorted(set(dates))
        units = cf.Units('days since ' + str(dates[0]), calendar)
        dates = cf.Data(dates, units, dt=True)
        return str(dates.min().dtarray[0]), str(dates.max().dtarray[0])

    return (None, None)
    def test_write_reference_datetime(self):
        for reference_datetime in ("1751-2-3", "1492-12-30"):
            cf.write(self.f0, tmpfile, reference_datetime=reference_datetime)

            g = cf.read(tmpfile)[0]

            t = g.dimension_coordinate("T")
            self.assertEqual(
                t.Units,
                cf.Units("days since " + reference_datetime),
                f"Units written were {t.Units.reftime!r} not "
                f"{reference_datetime!r}",
            )
Esempio n. 17
0
t[:, :, 1:3] = u[2]
print(t[:, :, 1:3].array)

print("\n**Units**\n")

q, t = cf.read('file.nc')
t.units
t.Units
t.units = 'degreesC'
t.units
t.Units
t.Units += 273.15
t.units
t.Units
t.data
t.Units = cf.Units('degreesC')
t.data
t.units = 'Kelvin'
t.data
t.data
t[0, 0, 0] = cf.Data(1)
t.data
t[0, 0, 0] = cf.Data(1, 'degreesC')
t.data
air_temp = cf.read('air_temperature.nc')[0]
time = air_temp.coordinate('time')
time.units
time.calendar
time.Units

print("\n**Filtering metadata constructs**\n")
Esempio n. 18
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)
Esempio n. 19
0
    def test_div_xy(self):
        f = cf.example_field(0)

        # Spherical polar coordinates
        theta = 90 - f.convert("Y", full_domain=True)
        sin_theta = theta.sin()

        radius = 2
        r = f.radius(radius)

        for wrap in (False, True, None):
            for one_sided in (False, True):
                x, y = f.grad_xy(
                    radius=radius,
                    x_wrap=wrap,
                    one_sided_at_boundary=one_sided,
                )

                d = cf.div_xy(
                    x,
                    y,
                    radius=radius,
                    x_wrap=wrap,
                    one_sided_at_boundary=one_sided,
                )

                self.assertTrue(d.Units == cf.Units("m-2 rad-2"), d.Units)

                term1 = x.derivative(
                    "X", wrap=wrap, one_sided_at_boundary=one_sided
                )
                term2 = (y * sin_theta).derivative(
                    "Y", one_sided_at_boundary=one_sided
                )

                d0 = (term1 + term2) / (sin_theta * r)

                # Check the data
                with cf.rtol(1e-10):
                    self.assertTrue((d.data == d0.data).all())

                del d.long_name
                d0.set_data(d.data)
                self.assertTrue(d.equals(d0))

        # Cartesian coordinates
        dim_x = f.dimension_coordinate("X")
        dim_y = f.dimension_coordinate("Y")
        dim_x.override_units("m", inplace=True)
        dim_y.override_units("m", inplace=True)
        dim_x.standard_name = "projection_x_coordinate"
        dim_y.standard_name = "projection_y_coordinate"
        f.cyclic("X", iscyclic=False)

        for wrap in (False, True, None):
            for one_sided in (True, False):
                x, y = f.grad_xy(x_wrap=wrap, one_sided_at_boundary=one_sided)

                d = cf.div_xy(
                    x, y, x_wrap=wrap, one_sided_at_boundary=one_sided
                )

                self.assertTrue(d.Units == cf.Units("m-2"))

                term1 = x.derivative(
                    "X", wrap=wrap, one_sided_at_boundary=one_sided
                )
                term2 = y.derivative("Y", one_sided_at_boundary=one_sided)

                d0 = term1 + term2

                del d.long_name
                del d0.long_name
                self.assertTrue(d.equals(d0, rtol=1e-10))
Esempio n. 20
0
    def test_FieldList_select(self):
        if self.test_only and inspect.stack()[0][3] not in self.test_only:
            return

        f = cf.read(self.filename)

        g = f('not this one')
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 0)

        g = f('eastward_wind')
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 1, len(g))

        g = f(re.compile('^eastw'))
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 1, len(g))

        f *= 9
        f[4] = f[0].copy()
        f[4].standard_name = 'this one'
        f[6] = f[0].copy()
        f[6].standard_name = 'this one'

        g = f(re.compile('^eastw'))
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 7, len(g))

        g = f('this one')
        self.assertIsInstance(g, cf.FieldList)
        self.assertEqual(len(g), 2)

        # select_by_Units
        f[1] = f[1].override_units(cf.Units('watt'))
        f[3] = f[3].override_units(cf.Units('mile hour-1'))

        self.assertEqual(len(f.select_by_units()), 9)
        self.assertEqual(len(f.select_by_units(cf.Units('m s-1'))), 7)
        self.assertEqual(
            len(f.select_by_units(cf.Units('m s-1'), exact=False)), 8)
        self.assertEqual(len(f.select_by_units('m s-1')), 7)
        self.assertEqual(len(f.select_by_units('m s-1', exact=False)), 8)
        self.assertEqual(len(f.select_by_units(re.compile('^mile|watt'))), 2)

        # select_by_Units
        f[1] = f[1].override_units(cf.Units('watt'))
        f[3] = f[3].override_units(cf.Units('mile hour-1'))

        self.assertEqual(len(f.select_by_units()), 9)
        self.assertEqual(len(f.select_by_units(cf.Units('m s-1'))), 7)
        self.assertEqual(
            len(f.select_by_units(cf.Units('m s-1'), exact=False)), 8)
        self.assertEqual(len(f.select_by_units('m s-1')), 7)
        self.assertEqual(len(f.select_by_units('m s-1', exact=False)), 8)
        self.assertEqual(len(f.select_by_units(re.compile('^mile|watt'))), 2)

        self.assertEqual(len(f.select_by_units('long_name=qwery:asd')), 0)

        # select_by_ncvar
        f[1].nc_set_variable('qwerty')
        f[4].nc_set_variable('eastward_wind2')

        self.assertEqual(len(f.select_by_ncvar()), 9)
        self.assertEqual(len(f.select_by_ncvar('qwerty')), 1)
        self.assertEqual(len(f.select_by_ncvar('eastward_wind')), 7)
        self.assertEqual(len(f.select_by_ncvar('eastward_wind2')), 1)
        self.assertEqual(len(f.select_by_ncvar(re.compile('^east'))), 8)
Esempio n. 21
0
    def test_TimeDuration(self):
        self.assertGreater(
            cf.TimeDuration(2, "calendar_years"),
            cf.TimeDuration(1, "calendar_years"),
        )
        self.assertLess(
            cf.TimeDuration(2, "calendar_years"),
            cf.TimeDuration(25, "calendar_months"),
        )
        self.assertLessEqual(
            cf.TimeDuration(2, "hours"), cf.TimeDuration(1, "days")
        )
        self.assertEqual(
            cf.TimeDuration(2, "hours"), cf.TimeDuration(1 / 12.0, "days")
        )
        self.assertEqual(
            cf.TimeDuration(2, "days"), cf.TimeDuration(48, "hours")
        )
        self.assertEqual(cf.TimeDuration(2, "days"), cf.Data(2))
        self.assertEqual(cf.TimeDuration(2, "days"), cf.Data([2.0], "days"))
        self.assertGreater(
            cf.TimeDuration(2, "days"), cf.Data([[60]], "seconds")
        )
        self.assertLessEqual(cf.TimeDuration(2, "hours"), 2)
        self.assertEqual(cf.TimeDuration(0.1, units="seconds"), 0.1)
        self.assertNotEqual(cf.TimeDuration(2, "days"), 30.5)
        self.assertGreater(
            cf.TimeDuration(2, "calendar_years"), numpy.array(1.5)
        )
        self.assertLess(
            cf.TimeDuration(2, "calendar_months"), numpy.array([[12]])
        )

        self.assertGreater(
            cf.TimeDuration(2, "calendar_years"),
            cf.TimeDuration(1, "calendar_years"),
        )
        self.assertLessEqual(
            cf.TimeDuration(1, "calendar_years"),
            cf.TimeDuration(2, "calendar_years"),
        )

        self.assertGreaterEqual(
            cf.TimeDuration(25, "calendar_months"),
            cf.TimeDuration(2, "calendar_years"),
        )
        self.assertLess(
            cf.TimeDuration(2, "calendar_years"),
            cf.TimeDuration(25, "calendar_months"),
        )

        self.assertGreaterEqual(
            cf.TimeDuration(1, "days"), cf.TimeDuration(2, "hours")
        )
        self.assertEqual(
            cf.TimeDuration(2, "hours"), cf.TimeDuration(1 / 12.0, "days")
        )
        self.assertEqual(
            cf.TimeDuration(2, "days"), cf.TimeDuration(48, "hours")
        )
        self.assertEqual(cf.TimeDuration(2, "days"), cf.Data(2))
        self.assertEqual(cf.TimeDuration(2, "days"), cf.Data([2.0], "days"))
        self.assertGreater(
            cf.TimeDuration(2, "days"), cf.Data([[60]], "seconds")
        )
        self.assertEqual(cf.TimeDuration(2, "hours"), 2)
        self.assertNotEqual(cf.TimeDuration(2, "days"), 30.5)
        self.assertGreater(
            cf.TimeDuration(2, "calendar_years"), numpy.array(1.5)
        )
        self.assertLess(
            cf.TimeDuration(2, "calendar_months"), numpy.array([[12]])
        )

        self.assertEqual(cf.TimeDuration(64, "calendar_years") + 2, cf.Y(66))
        self.assertEqual(
            cf.TimeDuration(64, "calendar_years") - 2.5, cf.Y(61.5)
        )
        self.assertEqual(
            cf.M(23) + cf.TimeDuration(64, "calendar_years"), cf.M(791)
        )
        self.assertEqual(
            cf.TimeDuration(64, "calendar_years") + cf.M(24), cf.Y(66)
        )

        self.assertEqual(cf.TimeDuration(36, "calendar_months") / 8, cf.M(4.5))
        self.assertEqual(cf.TimeDuration(36, "calendar_months") // 8, cf.M(4))

        self.assertEqual(
            cf.TimeDuration(36, "calendar_months") / numpy.array(8.0),
            cf.M(36 / 8.0),
        )
        self.assertEqual(
            cf.TimeDuration(12, "calendar_months") * cf.Data([[1.5]]),
            cf.Y(1.5),
        )
        self.assertEqual(
            cf.TimeDuration(36, "calendar_months") // 8.25, cf.M(4.0)
        )
        self.assertEqual(cf.TimeDuration(36, "calendar_months") % 10, cf.M(6))

        self.assertEqual(
            cf.TimeDuration(24, "hours") + cf.TimeDuration(0.5, "days"),
            cf.h(36.0),
        )
        self.assertEqual(
            cf.TimeDuration(0.5, "days") + cf.TimeDuration(24, "hours"),
            cf.D(1.5),
        )

        t = cf.TimeDuration(24, "hours")
        t += 2
        self.assertEqual(t, cf.h(26))
        t -= cf.Data(3, "hours")
        self.assertEqual(t, cf.h(23))
        t = cf.TimeDuration(24.0, "hours")
        t += 2
        self.assertEqual(t, cf.h(26))
        self.assertEqual(t - cf.Data(2.5, "hours"), cf.h(23.5))
        t *= 2
        self.assertEqual(t, cf.h(52.0))
        t -= 1.0
        self.assertEqual(t, cf.h(51))
        t /= 3
        self.assertEqual(t, cf.h(17))
        t += 5.5
        self.assertEqual(t, cf.h(22.5))
        t //= numpy.array(2)
        self.assertEqual(t, cf.h(11.0))
        t *= 10
        self.assertEqual(t, cf.h(110.0))
        t %= 3
        self.assertEqual(t, cf.h(2.0))

        t = cf.TimeDuration(24.5, "hours")
        self.assertEqual(-t, -24.5)
        self.assertEqual(int(t), 24)
        self.assertEqual(t / 0.5, 49)
        self.assertEqual(t // 2, 12)
        self.assertEqual(25 - t, 0.5)
        self.assertEqual(2 * t, 49)
        self.assertEqual(2.0 % t, 2, 2.0 % t)

        self.assertTrue(cf.TimeDuration(24, "hours").isint)
        self.assertTrue(cf.TimeDuration(24.0, "hours").isint)
        self.assertFalse(t.isint)

        t.Units = "days"
        self.assertEqual(t.Units, cf.Units("days"))
        t.Units = "hours"

        self.assertTrue(cf.TimeDuration(12, "hours").is_day_factor())
        self.assertFalse(cf.TimeDuration(13, "hours").is_day_factor())
        self.assertFalse(cf.TimeDuration(2, "days").is_day_factor())

        self.assertEqual(cf.TimeDuration(cf.Data(2, "days")), 2)
        self.assertEqual(cf.TimeDuration(cf.Data(48, "hours")), 48)
        self.assertEqual(
            cf.TimeDuration(cf.Data(48, "hours"), units="days"), 2
        )
        self.assertEqual(cf.TimeDuration(0.1, units="seconds"), 0.1)

        self.assertTrue(t.equals(t, verbose=2))
        self.assertTrue(t.equals(t.copy(), verbose=2))

        self.assertTrue(t.equivalent(t, verbose=2))
        self.assertTrue(t.equivalent(t.copy(), verbose=2))

        with self.assertRaises(Exception):
            t = cf.TimeDuration(48, "m")
        with self.assertRaises(Exception):
            t = cf.TimeDuration(cf.Data(48, "m"))
        with self.assertRaises(Exception):
            t = cf.TimeDuration(cf.Data(48, "days"), units="m")

        t = t.copy()
        t = copy.deepcopy(t)

        repr(t)
        str(t)

        t //= 2
        t %= 2
Esempio n. 22
0
    def test_TimeDuration(self):
        self.assertGreater(cf.TimeDuration(2, 'calendar_years'),
                           cf.TimeDuration(1, 'calendar_years'))
        self.assertLess(cf.TimeDuration(2, 'calendar_years'),
                        cf.TimeDuration(25, 'calendar_months'))
        self.assertLessEqual(cf.TimeDuration(2, 'hours'),
                             cf.TimeDuration(1, 'days'))
        self.assertEqual(cf.TimeDuration(2, 'hours'),
                         cf.TimeDuration(1 / 12.0, 'days'))
        self.assertEqual(cf.TimeDuration(2, 'days'),
                         cf.TimeDuration(48, 'hours'))
        self.assertEqual(cf.TimeDuration(2, 'days'), cf.Data(2))
        self.assertEqual(cf.TimeDuration(2, 'days'), cf.Data([2.], 'days'))
        self.assertGreater(cf.TimeDuration(2, 'days'),
                           cf.Data([[60]], 'seconds'))
        self.assertLessEqual(cf.TimeDuration(2, 'hours'), 2)
        self.assertEqual(cf.TimeDuration(0.1, units='seconds'), 0.1)
        self.assertNotEqual(cf.TimeDuration(2, 'days'), 30.5)
        self.assertGreater(cf.TimeDuration(2, 'calendar_years'),
                           numpy.array(1.5))
        self.assertLess(cf.TimeDuration(2, 'calendar_months'),
                        numpy.array([[12]]))

        self.assertGreater(cf.TimeDuration(2, 'calendar_years'),
                           cf.TimeDuration(1, 'calendar_years'))
        self.assertLessEqual(cf.TimeDuration(1, 'calendar_years'),
                             cf.TimeDuration(2, 'calendar_years'))

        self.assertGreaterEqual(cf.TimeDuration(25, 'calendar_months'),
                                cf.TimeDuration(2, 'calendar_years'))
        self.assertLess(cf.TimeDuration(2, 'calendar_years'),
                        cf.TimeDuration(25, 'calendar_months'))

        self.assertGreaterEqual(cf.TimeDuration(1, 'days'),
                                cf.TimeDuration(2, 'hours'))
        self.assertEqual(cf.TimeDuration(2, 'hours'),
                         cf.TimeDuration(1 / 12.0, 'days'))
        self.assertEqual(cf.TimeDuration(2, 'days'),
                         cf.TimeDuration(48, 'hours'))
        self.assertEqual(cf.TimeDuration(2, 'days'), cf.Data(2))
        self.assertEqual(cf.TimeDuration(2, 'days'), cf.Data([2.], 'days'))
        self.assertGreater(cf.TimeDuration(2, 'days'),
                           cf.Data([[60]], 'seconds'))
        self.assertEqual(cf.TimeDuration(2, 'hours'), 2)
        self.assertNotEqual(cf.TimeDuration(2, 'days'), 30.5)
        self.assertGreater(cf.TimeDuration(2, 'calendar_years'),
                           numpy.array(1.5))
        self.assertLess(cf.TimeDuration(2, 'calendar_months'),
                        numpy.array([[12]]))

        self.assertEqual(cf.TimeDuration(64, 'calendar_years') + 2, cf.Y(66))
        self.assertEqual(
            cf.TimeDuration(64, 'calendar_years') - 2.5, cf.Y(61.5))
        self.assertEqual(
            cf.M(23) + cf.TimeDuration(64, 'calendar_years'), cf.M(791))
        self.assertEqual(
            cf.TimeDuration(64, 'calendar_years') + cf.M(24), cf.Y(66))

        self.assertTrue(
            cf.TimeDuration(36, 'calendar_months') / 8 == cf.M(4.5))
        self.assertTrue(cf.TimeDuration(36, 'calendar_months') // 8 == cf.M(4))

        self.assertTrue(
            cf.TimeDuration(36, 'calendar_months') /
            numpy.array(8.0) == cf.M(36 / 8.0))
        self.assertTrue(
            cf.TimeDuration(12, 'calendar_months') *
            cf.Data([[1.5]]) == cf.Y(1.5))
        self.assertTrue(
            cf.TimeDuration(36, 'calendar_months') // 8.25 == cf.M(4.0))
        self.assertTrue(cf.TimeDuration(36, 'calendar_months') % 10 == cf.M(6))

        self.assertTrue(
            cf.TimeDuration(24, 'hours') +
            cf.TimeDuration(0.5, 'days') == cf.h(36.0))
        self.assertTrue(
            cf.TimeDuration(0.5, 'days') +
            cf.TimeDuration(24, 'hours') == cf.D(1.5))

        t = cf.TimeDuration(24, 'hours')
        t += 2
        self.assertTrue(t == cf.h(26))
        t -= cf.Data(3, 'hours')
        self.assertTrue(t == cf.h(23))
        t = cf.TimeDuration(24.0, 'hours')
        t += 2
        self.assertTrue(t == cf.h(26))
        self.assertTrue(t - cf.Data(2.5, 'hours') == cf.h(23.5))
        t *= 2
        self.assertTrue(t == cf.h(52.0))
        t -= 1.0
        self.assertTrue(t == cf.h(51))
        t /= 3
        self.assertTrue(t == cf.h(17))
        t += 5.5
        self.assertTrue(t == cf.h(22.5))
        t //= numpy.array(2)
        self.assertTrue(t == cf.h(11.0))
        t *= 10
        self.assertTrue(t == cf.h(110.0))
        t %= 3
        self.assertTrue(t == cf.h(2.0))

        t = cf.TimeDuration(24.5, 'hours')
        self.assertTrue(-t == -24.5)
        self.assertTrue(int(t) == 24)
        self.assertTrue(t / 0.5 == 49)
        self.assertTrue(t // 2 == 12)
        self.assertTrue(25 - t == 0.5)
        self.assertTrue(2 * t == 49)
        self.assertTrue(2.0 % t == 2, 2.0 % t)

        self.assertTrue(cf.TimeDuration(24, 'hours').isint)
        self.assertTrue(cf.TimeDuration(24.0, 'hours').isint)
        self.assertFalse(t.isint)

        t.Units = 'days'
        self.assertTrue(t.Units == cf.Units('days'))
        t.Units = 'hours'

        self.assertTrue(cf.TimeDuration(12, 'hours').is_day_factor())
        self.assertFalse(cf.TimeDuration(13, 'hours').is_day_factor())
        self.assertFalse(cf.TimeDuration(2, 'days').is_day_factor())

        self.assertTrue(cf.TimeDuration(cf.Data(2, 'days')) == 2)
        self.assertTrue(cf.TimeDuration(cf.Data(48, 'hours')) == 48)
        self.assertTrue(
            cf.TimeDuration(cf.Data(48, 'hours'), units='days') == 2)
        self.assertTrue(cf.TimeDuration(0.1, units='seconds') == 0.1)

        self.assertTrue(t.equals(t, verbose=2))
        self.assertTrue(t.equals(t.copy(), verbose=2))

        self.assertTrue(t.equivalent(t, verbose=2))
        self.assertTrue(t.equivalent(t.copy(), verbose=2))

        with self.assertRaises(Exception):
            t = cf.TimeDuration(48, 'm')
        with self.assertRaises(Exception):
            t = cf.TimeDuration(cf.Data(48, 'm'))
        with self.assertRaises(Exception):
            t = cf.TimeDuration(cf.Data(48, 'days'), units='m')

        t = t.copy()
        t = copy.deepcopy(t)

        _ = repr(t)
        _ = str(t)

        t //= 2
        t %= 2