Source code for cf_units

# (C) British Crown Copyright 2015 - 2018, Met Office
#
# This file is part of cf_units.
#
# cf_units is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cf_units is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with cf_units.  If not, see <http://www.gnu.org/licenses/>.
"""
Units of measure.

Provision of a wrapper class to support Unidata/UCAR UDUNITS-2, and the
cftime calendar functionality.

See also: `UDUNITS-2
<http://www.unidata.ucar.edu/software/udunits>`_.

"""

from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip)  # noqa

import six

from contextlib import contextmanager
import copy
import os.path
import sys
import warnings

import cftime
import numpy as np

from . import config
from .util import _OrderedHashable, approx_equal

from cf_units import _udunits2 as _ud
from cf_units._udunits2 import (UT_ASCII, UT_ISO_8859_1, UT_LATIN1, UT_UTF8,
                                UT_NAMES, UT_DEFINITION)


# Define __version__ based on versioneer's interpretation.
from ._version import get_versions
__version__ = get_versions()['version']
del get_versions

__all__ = ['CALENDAR_STANDARD',
           'CALENDAR_GREGORIAN',
           'CALENDAR_PROLEPTIC_GREGORIAN',
           'CALENDAR_NO_LEAP',
           'CALENDAR_JULIAN',
           'CALENDAR_ALL_LEAP',
           'CALENDAR_365_DAY',
           'CALENDAR_366_DAY',
           'CALENDAR_360_DAY',
           'CALENDARS',
           'CALENDAR_ALIASES',
           'UT_NAMES',
           'UT_DEFINITION',
           'UT_ASCII',
           'FLOAT32',
           'FLOAT64',
           'julian_day2date',
           'date2julian_day',
           'is_time',
           'is_vertical',
           'Unit',
           'date2num',
           'decode_time',
           'encode_clock',
           'encode_date',
           'encode_time',
           'num2date',
           'suppress_errors']


########################################################################
#
# module level constants
#
########################################################################

#
# default constants
#
EPOCH = '1970-01-01 00:00:00'
_UNKNOWN_UNIT_STRING = 'unknown'
_UNKNOWN_UNIT_SYMBOL = '?'
_UNKNOWN_UNIT = [_UNKNOWN_UNIT_STRING, _UNKNOWN_UNIT_SYMBOL, '???', '']
_NO_UNIT_STRING = 'no_unit'
_NO_UNIT_SYMBOL = '-'
_NO_UNIT = [_NO_UNIT_STRING, _NO_UNIT_SYMBOL, 'no unit', 'no-unit', 'nounit']
_UNIT_DIMENSIONLESS = '1'
_OP_SINCE = ' since '
_CATEGORY_UNKNOWN, _CATEGORY_NO_UNIT, _CATEGORY_UDUNIT = range(3)


#
# libudunits2 constants
#
UT_FORMATS = [UT_ASCII, UT_ISO_8859_1, UT_LATIN1, UT_UTF8, UT_NAMES,
              UT_DEFINITION]

#
# cftime constants
#
CALENDAR_STANDARD = 'standard'
CALENDAR_GREGORIAN = 'gregorian'
CALENDAR_PROLEPTIC_GREGORIAN = 'proleptic_gregorian'
CALENDAR_NO_LEAP = 'noleap'
CALENDAR_JULIAN = 'julian'
CALENDAR_ALL_LEAP = 'all_leap'
CALENDAR_365_DAY = '365_day'
CALENDAR_366_DAY = '366_day'
CALENDAR_360_DAY = '360_day'

#: The calendars recognised by cf_units.
#: These are accessible as strings, or as constants in the form
#: ``cf_units.CALENDAR_{ calendar_name.upper() }``. For example,
#: ``cf_units.CALENDAR_NO_LEAP`` and ``cf_units.CALENDAR_366_DAY``.
CALENDARS = [CALENDAR_STANDARD, CALENDAR_GREGORIAN,
             CALENDAR_PROLEPTIC_GREGORIAN, CALENDAR_NO_LEAP, CALENDAR_JULIAN,
             CALENDAR_ALL_LEAP, CALENDAR_365_DAY, CALENDAR_366_DAY,
             CALENDAR_360_DAY]

#: Where calendars have multiple names, we map the alias to the
#: definitive form.
CALENDAR_ALIASES = {
    CALENDAR_STANDARD: CALENDAR_GREGORIAN,
    CALENDAR_NO_LEAP: CALENDAR_365_DAY,
    CALENDAR_ALL_LEAP: CALENDAR_366_DAY}


#
# floating point types
#
FLOAT32 = np.float32
FLOAT64 = np.float64

########################################################################
#
# module level statements
#
########################################################################

# Convenience dictionary for the Unit convert method.
_cv_convert_scalar = {FLOAT32: _ud.convert_float,
                      FLOAT64: _ud.convert_double}
_cv_convert_array = {FLOAT32: _ud.convert_floats,
                     FLOAT64: _ud.convert_doubles}

# Map of ut_encodings to encoding strings
_encoding_lookup = {UT_ASCII: 'ascii',
                    UT_ISO_8859_1: 'iso_8859_1',
                    UT_LATIN1: 'latin1',
                    UT_UTF8: 'utf-8'}


@contextmanager
def suppress_errors():
    """
    Suppresses all error messages from UDUNITS-2.

    """
    _default_handler = _ud.set_error_message_handler(_ud.ignore)
    try:
        yield
    finally:
        _ud.set_error_message_handler(_default_handler)


#
# load the UDUNITS-2 xml-formatted unit-database
#:
# Ignore standard noisy UDUNITS-2 start-up.
with suppress_errors():
    # Load the unit-database from the default location (modified via
    # the UDUNITS2_XML_PATH environment variable) and if that fails look
    # relative to sys.prefix to support environments such as conda.
    try:
        _ud_system = _ud.read_xml()
    except _ud.UdunitsError:
        _alt_xml_path = config.get_option(
            'System', 'udunits2_xml_path',
            default=os.path.join(sys.prefix, 'share', 'udunits',
                                 'udunits2.xml'))
        try:
            _ud_system = _ud.read_xml(_alt_xml_path.encode())
        except _ud.UdunitsError as e:
            error_msg = ': "%s"' % e.error_msg() if e.errnum else ''
            raise OSError(
                '[%s] Failed to open UDUNITS-2 XML unit database%s'
                % (e.status_msg(), error_msg))


########################################################################
#
# module level function definitions
#
########################################################################

[docs]def encode_time(year, month, day, hour, minute, second): """ Return date/clock time encoded as a double precision value. Encoding performed using UDUNITS-2 hybrid Gregorian/Julian calendar. Dates on or after 1582-10-15 are assumed to be Gregorian dates; dates before that are assumed to be Julian dates. In particular, the year 1 BCE is immediately followed by the year 1 CE. Args: * year (int): Year value to be encoded. * month (int): Month value to be encoded. * day (int): Day value to be encoded. * hour (int): Hour value to be encoded. * minute (int): Minute value to be encoded. * second (int): Second value to be encoded. Returns: float. For example: >>> import cf_units >>> cf_units.encode_time(1970, 1, 1, 0, 0, 0) -978307200.0 """
return _ud.encode_time(year, month, day, hour, minute, second)
[docs]def encode_date(year, month, day): """ Return date encoded as a double precision value. Encoding performed using UDUNITS-2 hybrid Gergorian/Julian calendar. Dates on or after 1582-10-15 are assumed to be Gregorian dates; dates before that are assumed to be Julian dates. In particular, the year 1 BCE is immediately followed by the year 1 CE. Args: * year (int): Year value to be encoded. * month (int): Month value to be encoded. * day (int): Day value to be encoded. Returns: float. For example: >>> import cf_units >>> cf_units.encode_date(1970, 1, 1) -978307200.0 """
return _ud.encode_date(year, month, day)
[docs]def encode_clock(hour, minute, second): """ Return clock time encoded as a double precision value. Args: * hour (int): Hour value to be encoded. * minute (int): Minute value to be encoded. * second (int): Second value to be encoded. Returns: float. For example: >>> import cf_units >>> cf_units.encode_clock(0, 0, 0) 0.0 """
return _ud.encode_clock(hour, minute, second)
[docs]def decode_time(time): """ Decode a double precision date/clock time value into its component parts and return as tuple. Decode time into it's year, month, day, hour, minute, second, and resolution component parts. Where resolution is the uncertainty of the time in seconds. Args: * time (float): Date/clock time encoded as a double precision value. Returns: tuple of (year, month, day, hour, minute, second, resolution). For example: >>> import cf_units >>> cf_units.decode_time(cf_units.encode_time(1970, 1, 1, 0, 0, 0)) (1970, 1, 1, 0, 0, 0.0, 1.086139178596568e-07) """
return _ud.decode_time(time)
[docs]def julian_day2date(julian_day, calendar): """ Return a cftime datetime-like object representing the Julian day. If calendar is 'standard' or 'gregorian', Julian day follows Julian calendar on and before 1582-10-5, Gregorian calendar after 1582-10-15. If calendar is 'proleptic_gregorian', Julian Day follows Gregorian calendar. If calendar is 'julian', Julian Day follows Julian calendar. The datetime object is a 'real' datetime object if the date falls in the Gregorian calendar (i.e. calendar is 'proleptic_gregorian', or calendar is 'standard'/'gregorian' and the date is after 1582-10-15). Otherwise, it's a 'phony' datetime object which is actually an instance of cftime.datetime. Algorithm: Meeus, Jean (1998) Astronomical Algorithms (2nd Edition). Willmann-Bell, Virginia. p. 63. Args: * julian_day (float): Julian day with a resolution of 1 second. * calendar (string): Name of the calendar, see cf_units.CALENDARS. Returns: datetime or cftime.datetime. For example: >>> import cf_units >>> import datetime >>> cf_units.julian_day2date( ... cf_units.date2julian_day(datetime.datetime(1970, 1, 1, 0, 0, 0), ... cf_units.CALENDAR_STANDARD), ... cf_units.CALENDAR_STANDARD) datetime.datetime(1970, 1, 1, 0, 0) """
return cftime.DateFromJulianDay(julian_day, calendar)
[docs]def date2julian_day(date, calendar): """ Return the Julian day (resolution of 1 second) from a cftime datetime-like object. If calendar is 'standard' or 'gregorian', Julian day follows Julian calendar on and before 1582-10-5, Gregorian calendar after 1582-10-15. If calendar is 'proleptic_gregorian', Julian day follows Gregorian calendar. If calendar is 'julian', Julian day follows Julian calendar. Algorithm: Meeus, Jean (1998) Astronomical Algorithms (2nd Edition). Willmann-Bell, Virginia. p. 63. Args: * date (cftime.date): Date and time representation. * calendar (string): Name of the calendar, see cf_units.CALENDARS. Returns: float. For example: >>> import cf_units >>> import datetime >>> cf_units.date2julian_day(datetime.datetime(1970, 1, 1, 0, 0, 0), ... cf_units.CALENDAR_STANDARD) 2440587.5... """
return cftime.JulianDayFromDate(date, calendar)
[docs]def date2num(date, unit, calendar): """ Return numeric time value (resolution of 1 second) encoding of datetime object. The units of the numeric time values are described by the unit and calendar arguments. The datetime objects must be in UTC with no time-zone offset. If there is a time-zone offset in unit, it will be applied to the returned numeric values. Like the :func:`matplotlib.dates.date2num` function, except that it allows for different units and calendars. Behaves the same as if unit = 'days since 0001-01-01 00:00:00' and calendar = 'proleptic_gregorian'. Args: * date (datetime): A datetime object or a sequence of datetime objects. The datetime objects should not include a time-zone offset. * unit (string): A string of the form '<time-unit> since <time-origin>' describing the time units. The <time-unit> can be days, hours, minutes or seconds. The <time-origin> is a date/time reference point. A valid choice would be unit='hours since 1800-01-01 00:00:00 -6:00'. * calendar (string): Name of the calendar, see cf_units.CALENDARS. Returns: float, or numpy.ndarray of float. For example: >>> import cf_units >>> import datetime >>> dt1 = datetime.datetime(1970, 1, 1, 6, 0, 0) >>> dt2 = datetime.datetime(1970, 1, 1, 7, 0, 0) >>> cf_units.date2num(dt1, 'hours since 1970-01-01 00:00:00', ... cf_units.CALENDAR_STANDARD) 6.0 >>> cf_units.date2num([dt1, dt2], 'hours since 1970-01-01 00:00:00', ... cf_units.CALENDAR_STANDARD) array([6., 7.]) """ # # ensure to strip out any 'UTC' postfix which is generated by # UDUNITS-2 formatted output and causes the cftime parser # to choke # unit_string = unit.rstrip(" UTC") if unit_string.endswith(" since epoch"): unit_string = unit_string.replace("epoch", EPOCH) unit_inst = Unit(unit_string, calendar=calendar)
return unit_inst.date2num(date) def _discard_microsecond(date): """ Return a date with the microsecond componenet discarded. Works for scalars, sequences and numpy arrays. Returns a scalar if input is a scalar, else returns a numpy array. Args: * date (datetime.datetime or cftime.datetime): Date value/s Returns: datetime, or numpy.ndarray of datetime object. """ dates = np.asarray(date) shape = dates.shape dates = dates.ravel() # Create date objects of the same type returned by utime.num2date() # (either datetime.datetime or cftime.datetime), discarding the # microseconds dates = np.array([d and d.__class__(d.year, d.month, d.day, d.hour, d.minute, d.second) for d in dates]) result = dates[0] if shape is () else dates.reshape(shape) return result
[docs]def num2date(time_value, unit, calendar): """ Return datetime encoding of numeric time value (resolution of 1 second). The units of the numeric time value are described by the unit and calendar arguments. The returned datetime object represent UTC with no time-zone offset, even if the specified unit contain a time-zone offset. Like the :func:`matplotlib.dates.num2date` function, except that it allows for different units and calendars. Behaves the same if unit = 'days since 001-01-01 00:00:00'} calendar = 'proleptic_gregorian'. The datetime instances returned are 'real' python datetime objects if the date falls in the Gregorian calendar (i.e. calendar='proleptic_gregorian', or calendar = 'standard' or 'gregorian' and the date is after 1582-10-15). Otherwise, they are 'phony' datetime objects which support some but not all the methods of 'real' python datetime objects. This is because the python datetime module cannot use the 'proleptic_gregorian' calendar, even before the switch occured from the Julian calendar in 1582. The datetime instances do not contain a time-zone offset, even if the specified unit contains one. Works for scalars, sequences and numpy arrays. Returns a scalar if input is a scalar, else returns a numpy array. Args: * time_value (float): Numeric time value/s. Maximum resolution is 1 second. * unit (sting): A string of the form '<time-unit> since <time-origin>' describing the time units. The <time-unit> can be days, hours, minutes or seconds. The <time-origin> is the date/time reference point. A valid choice would be unit='hours since 1800-01-01 00:00:00 -6:00'. * calendar (string): Name of the calendar, see cf_units.CALENDARS. Returns: datetime, or numpy.ndarray of datetime object. For example: >>> import cf_units >>> import datetime >>> cf_units.num2date(6, 'hours since 1970-01-01 00:00:00', ... cf_units.CALENDAR_STANDARD) datetime.datetime(1970, 1, 1, 6, 0) >>> cf_units.num2date([6, 7], 'hours since 1970-01-01 00:00:00', ... cf_units.CALENDAR_STANDARD) array([datetime.datetime(1970, 1, 1, 6, 0), datetime.datetime(1970, 1, 1, 7, 0)], dtype=object) """ # # ensure to strip out any 'UTC' postfix which is generated by # UDUNITS-2 formatted output and causes the cftime parser # to choke # unit_string = unit.rstrip(" UTC") if unit_string.endswith(" since epoch"): unit_string = unit_string.replace("epoch", EPOCH) unit_inst = Unit(unit_string, calendar=calendar)
return unit_inst.num2date(time_value) def _num2date_to_nearest_second(time_value, utime): """ Return datetime encoding of numeric time value with respect to the given time reference units, with a resolution of 1 second. * time_value (float): Numeric time value/s. * utime (cftime.utime): netcdf.utime object with which to perform the conversion/s. Returns: datetime, or numpy.ndarray of datetime object. """ time_values = np.asanyarray(time_value) shape = time_values.shape time_values = time_values.ravel() # We account for the edge case where the time is in seconds and has a # half second: utime.num2date() may produce a date that would round # down. # # Note that this behaviour is different to the num2date function in version # 1.1 and earlier of cftime that didn't have microsecond precision. In # those versions, a half-second value would be rounded up or down # arbitrarily. It is probably not possible to replicate that behaviour with # later versions, if one wished to do so for the sake of consistency. has_half_seconds = np.logical_and(utime.units == 'seconds', time_values % 1. == 0.5) dates = utime.num2date(time_values) try: # We can assume all or none of the dates have a microsecond attribute microseconds = np.array([d.microsecond if d else 0 for d in dates]) except AttributeError: microseconds = 0 round_mask = np.logical_or(has_half_seconds, microseconds != 0) ceil_mask = np.logical_or(has_half_seconds, microseconds >= 500000) if time_values[ceil_mask].size > 0: useconds = Unit('second') second_frac = useconds.convert(0.75, utime.units) dates[ceil_mask] = utime.num2date(time_values[ceil_mask] + second_frac) dates[round_mask] = _discard_microsecond(dates[round_mask]) result = dates[0] if shape is () else dates.reshape(shape) return result ######################################################################## # # unit wrapper class for unidata/ucar UDUNITS-2 # ######################################################################## def _Unit(category, ut_unit, calendar=None, origin=None): unit = _OrderedHashable.__new__(Unit) unit._init(category, ut_unit, calendar, origin) return unit _CACHE = {} def as_unit(unit): """ Returns a Unit corresponding to the given unit. .. note:: If the given unit is already a Unit it will be returned unchanged. """ if isinstance(unit, Unit): result = unit else: result = None use_cache = isinstance(unit, six.string_types) or unit is None if use_cache: result = _CACHE.get(unit) if result is None: # Typically unit is a string, however we cater for other types of # 'unit' (e.g. iris.unit.Unit). result = Unit(unit, calendar=getattr(unit, 'calendar', None)) if use_cache: _CACHE[unit] = result return result def is_time(unit): """ Determine whether the unit is a related SI Unit of time. Args: * unit (string/Unit): Unit to be compared. Returns: Boolean. For example: >>> import cf_units >>> cf_units.is_time('hours') True >>> cf_units.is_time('meters') False """ return as_unit(unit).is_time() def is_vertical(unit): """ Determine whether the unit is a related SI Unit of pressure or distance. Args: * unit (string/Unit): Unit to be compared. Returns: Boolean. For example: >>> import cf_units >>> cf_units.is_vertical('millibar') True >>> cf_units.is_vertical('km') True """ return as_unit(unit).is_vertical()
[docs]class Unit(_OrderedHashable): """ A class to represent S.I. units and support common operations to manipulate such units in a consistent manner as per UDUNITS-2. These operations include scaling the unit, offsetting the unit by a constant or time, inverting the unit, raising the unit by a power, taking a root of the unit, taking a log of the unit, multiplying the unit by a constant or another unit, dividing the unit by a constant or another unit, comparing units, copying units and converting unit data to single precision or double precision floating point numbers. This class also supports time and calendar defintion and manipulation. """ def _init_from_tuple(self, values): for name, value in zip(self._names, values): object.__setattr__(self, name, value) def _as_tuple(self): return tuple(getattr(self, name) for name in self._names) # Provide hash semantics def _identity(self): return self._as_tuple() def __hash__(self): return hash(self._identity()) # Provide default ordering semantics def __lt__(self, other): return self._identity() < other._identity() # Prevent attribute updates def __setattr__(self, name, value): raise AttributeError('Instances of %s are immutable' % type(self).__name__) def __delattr__(self, name): raise AttributeError('Instances of %s are immutable' % type(self).__name__) # Declare the attribute names relevant to the ordered and hashable # behaviour. _names = ('category', 'ut_unit', 'calendar', 'origin') category = None 'Is this an unknown unit, a no-unit, or a UDUNITS-2 unit.' ut_unit = None 'Reference to the quantity defining the UDUNITS-2 unit.' calendar = None 'Represents the unit calendar name, see cf_units.CALENDARS' origin = None 'The original string used to create this unit.' __slots__ = () def __init__(self, unit, calendar=None): """ Create a wrapper instance for UDUNITS-2. An optional calendar may be provided for a unit which defines a time reference of the form '<time-unit> since <time-origin>' i.e. unit='days since 1970-01-01 00:00:00'. For a unit that is a time reference, the default calendar is 'standard'. Accepted calendars are as follows, * 'standard' or 'gregorian' - Mixed Gregorian/Julian calendar as defined by udunits. * 'proleptic_gregorian' - A Gregorian calendar extended to dates before 1582-10-15. A year is a leap year if either, 1. It is divisible by 4 but not by 100, or 2. It is divisible by 400. * 'noleap' or '365_day' - A Gregorian calendar without leap years i.e. all years are 365 days long. * 'all_leap' or '366_day' - A Gregorian calendar with every year being a leap year i.e. all years are 366 days long. * '360_day' - All years are 360 days divided into 30 day months. * 'julian' - Proleptic Julian calendar, extended to dates after 1582-10-5. A year is a leap year if it is divisible by 4. Args: * unit: Specify the unit as defined by UDUNITS-2. * calendar (string): Describes the calendar used in time calculations. The default is 'standard' or 'gregorian' for a time reference unit. Returns: Unit object. Units should be set to "no_unit" for values which are strings. Units can also be set to "unknown" (or None). For example: >>> from cf_units import Unit >>> volts = Unit('volts') >>> no_unit = Unit('no_unit') >>> unknown = Unit('unknown') >>> unknown = Unit(None) """ ut_unit = _ud.NULL_UNIT calendar_ = None if unit is None: unit = '' else: unit = str(unit).strip() if unit.lower().endswith(' utc'): unit = unit[:unit.lower().rfind(' utc')] if unit.endswith(" since epoch"): unit = unit.replace("epoch", EPOCH) if "#" in unit: unit = unit.replace("#", "1") if unit.lower() in _UNKNOWN_UNIT: # TODO - removing the option of an unknown unit. Currently # the auto generated MOSIG rules are missing units on a # number of phenomena which would lead to errors. # Will be addressed by work on metadata translation. category = _CATEGORY_UNKNOWN unit = _UNKNOWN_UNIT_STRING elif unit.lower() in _NO_UNIT: category = _CATEGORY_NO_UNIT unit = _NO_UNIT_STRING else: category = _CATEGORY_UDUNIT try: ut_unit = _ud.parse(_ud_system, unit.encode('ascii'), UT_ASCII) except _ud.UdunitsError as e: self._propogate_error('Failed to parse unit "%s"' % unit, e) if _OP_SINCE in unit.lower(): if calendar is None: calendar_ = CALENDAR_GREGORIAN elif isinstance(calendar, six.string_types): calendar_ = calendar.lower() if calendar_ in CALENDAR_ALIASES: calendar_ = CALENDAR_ALIASES[calendar_] if calendar_ not in CALENDARS: msg = '{!r} is an unsupported calendar.' raise ValueError(msg.format(calendar)) else: msg = 'Expected string-like calendar argument, got {!r}.' raise TypeError(msg.format(type(calendar))) self._init_from_tuple((category, ut_unit, calendar_, unit,)) def _propogate_error(self, msg, ud_err): """ Retrieve the UDUNITS-2 ut_status, the implementation-defined string corresponding to UDUNITS-2 errno from the UdunitsError and raise generic exception. """ error_msg = ': "%s"' % ud_err.error_msg() if ud_err.errnum != 0 else '' raise ValueError('[%s] %s%s' % (ud_err.status_msg(), msg, error_msg)) # NOTE: # "__getstate__" and "__setstate__" functions are defined here to # provide a custom interface for Pickle # : Pickle "normal" behaviour is just to save/reinstate the object # dictionary # : that won't work here, because the "ut_unit" attribute is an # object handle # - the corresponding udunits object only exists in the original # invocation def __getstate__(self): # state capture method for Pickle.dump() # - return the instance data needed to reconstruct a Unit value return {'unit_text': self.origin, 'calendar': self.calendar} def __setstate__(self, state): # object reconstruction method for Pickle.load() # intercept the Pickle.load() operation and call own __init__ again # - this is to ensure a valid ut_unit attribute (as these # handles aren't persistent) self.__init__(state['unit_text'], calendar=state['calendar']) def __copy__(self): return self def __deepcopy__(self, memo): return self
[docs] def is_time(self): """ Determine whether this unit is a related SI Unit of time. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('hours') >>> u.is_time() True >>> v = cf_units.Unit('meter') >>> v.is_time() False """ if self.is_unknown() or self.is_no_unit(): result = False else: day = _ud.get_unit_by_name(_ud_system, b'day') result = _ud.are_convertible(self.ut_unit, day)
return result
[docs] def is_vertical(self): """ Determine whether the unit is a related SI Unit of pressure or distance. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('millibar') >>> u.is_vertical() True >>> v = cf_units.Unit('km') >>> v.is_vertical() True """ if self.is_unknown() or self.is_no_unit(): result = False else: bar = _ud.get_unit_by_name(_ud_system, b'bar') result = _ud.are_convertible(self.ut_unit, bar) if not result: meter = _ud.get_unit_by_name(_ud_system, b'meter') result = _ud.are_convertible(self.ut_unit, meter)
return result
[docs] def is_udunits(self): """Return whether the unit is a vaild unit of UDUNITS."""
return self.ut_unit is not _ud.NULL_UNIT
[docs] def is_time_reference(self): """ Return whether the unit is a time reference unit of the form '<time-unit> since <time-origin>' i.e. unit='days since 1970-01-01 00:00:00' Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('days since epoch') >>> u.is_time_reference() True """
return self.calendar is not None
[docs] def is_long_time_interval(self): """ Defines whether this unit describes a time unit with a long time interval ("months" or "years"). These long time intervals *are* supported by `UDUNITS2` but are not supported by `cftime`. This discrepancy means we cannot run self.num2date() on a time unit with a long time interval. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('days since epoch') >>> u.is_long_time_interval() False >>> u = cf_units.Unit('years since epoch') >>> u.is_long_time_interval() True """ result = False long_time_intervals = ['year', 'month'] if self.is_time_reference(): result = any(interval in self.origin for interval in long_time_intervals)
return result
[docs] def title(self, value): """ Return the unit value as a title string. Args: * value (float): Unit value to be incorporated into title string. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('hours since epoch', ... calendar=cf_units.CALENDAR_STANDARD) >>> u.title(10) '1970-01-01 10:00:00' """ if self.is_time_reference(): dt = self.num2date(value) result = dt.strftime('%Y-%m-%d %H:%M:%S') else: result = '%s %s' % (str(value), self)
return result @property def modulus(self): """ *(read-only)* Return the modulus value of the unit. Convenience method that returns the unit modulus value as follows, * 'radians' - pi*2 * 'degrees' - 360.0 * Otherwise None. Returns: float. For example: >>> import cf_units >>> u = cf_units.Unit('degrees') >>> u.modulus 360.0 """ if self == 'radians': result = np.pi * 2 elif self == 'degrees': result = 360.0 else: result = None return result
[docs] def is_convertible(self, other): """ Return whether two units are convertible. Args: * other (Unit): Unit to be compared. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> v = cf_units.Unit('kilometers') >>> u.is_convertible(v) True """ other = as_unit(other) if self.is_unknown() or self.is_no_unit() or other.is_unknown() or \ other.is_no_unit(): result = False else: result = (self.calendar == other.calendar and _ud.are_convertible(self.ut_unit, other.ut_unit))
return result
[docs] def is_dimensionless(self): """ Return whether the unit is dimensionless. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> u.is_dimensionless() False >>> u = cf_units.Unit('1') >>> u.is_dimensionless() True """ return (self.category == _CATEGORY_UDUNIT and
bool(_ud.is_dimensionless(self.ut_unit)))
[docs] def is_unknown(self): """ Return whether the unit is defined to be an *unknown* unit. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('unknown') >>> u.is_unknown() True >>> u = cf_units.Unit('meters') >>> u.is_unknown() False """
return self.category == _CATEGORY_UNKNOWN
[docs] def is_no_unit(self): """ Return whether the unit is defined to be a *no_unit* unit. Typically, a quantity such as a string, will have no associated unit to describe it. Such a class of quantity may be defined using the *no_unit* unit. Returns: Boolean. For example: >>> import cf_units >>> u = cf_units.Unit('no unit') >>> u.is_no_unit() True >>> u = cf_units.Unit('meters') >>> u.is_no_unit() False """
return self.category == _CATEGORY_NO_UNIT
[docs] def format(self, option=None): """ Return a formatted string representation of the binary unit. Args: * option (cf_units.UT_FORMATS): Set the option of the formatted string representation. Valid encoding options may be at most one of the following enumerations: * Unit.UT_ASCII * Unit.UT_ISO_8859_1 * Unit.UT_LATIN1 * Unit.UT_UTF8 Any combination of the following may also be used: * Unit.UT_NAMES * Unit.UT_DEFINITION Multiple options may be combined within a list. The default option is cf_units.UT_ASCII. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> u.format() 'm' >>> u.format(cf_units.UT_NAMES) 'meter' >>> u.format(cf_units.UT_DEFINITION) 'm' """ if self.is_unknown(): return _UNKNOWN_UNIT_STRING elif self.is_no_unit(): return _NO_UNIT_STRING else: bitmask = UT_ASCII if option is not None: if not isinstance(option, list): option = [option] for i in option: bitmask |= i encoding = bitmask & \ (UT_ASCII | UT_ISO_8859_1 | UT_LATIN1 | UT_UTF8) endocing_str = _encoding_lookup[encoding] try: result = _ud.format(self.ut_unit, bitmask) except _ud.UdunitsError as e: self._propogate_error('Failed to format %r' % self, e)
return str(result.decode(endocing_str)) @property def name(self): """ *(read-only)* The full name of the unit. Formats the binary unit into a string representation using method :func:`cf_units.Unit.format` with keyword argument option=cf_units.UT_NAMES. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('watts') >>> u.name 'watt' """ return self.format(UT_NAMES) @property def symbol(self): """ *(read-only)* The symbolic representation of the unit. Formats the binary unit into a string representation using method :func:`cf_units.Unit.format`. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('watts') >>> u.symbol 'W' """ if self.is_unknown(): result = _UNKNOWN_UNIT_SYMBOL elif self.is_no_unit(): result = _NO_UNIT_SYMBOL else: result = self.format() return result @property def definition(self): """ *(read-only)* The symbolic decomposition of the unit. Formats the binary unit into a string representation using method :func:`cf_units.Unit.format` with keyword argument option=cf_units.UT_DEFINITION. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('watts') >>> u.definition 'm2.kg.s-3' """ if self.is_unknown(): result = _UNKNOWN_UNIT_SYMBOL elif self.is_no_unit(): result = _NO_UNIT_SYMBOL else: result = self.format(UT_DEFINITION) return result
[docs] def offset_by_time(self, origin): """ Returns the time unit offset with respect to the time origin. Args: * origin (float): Time origin as returned by the :func:`cf_units.encode_time` method. Returns: None. For example: >>> import cf_units >>> u = cf_units.Unit('hours') >>> u.offset_by_time(cf_units.encode_time(1970, 1, 1, 0, 0, 0)) Unit('h @ 19700101T000000.0000000 UTC') """ if not isinstance(origin, (float, six.integer_types)): raise TypeError('a numeric type for the origin argument is' ' required') try: ut_unit = _ud.offset_by_time(self.ut_unit, origin) except _ud.UdunitsError as e: self._propogate_error('Failed to offset %r' % self, e) calendar = None
return _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
[docs] def invert(self): """ Invert the unit i.e. find the reciprocal of the unit, and return the Unit result. Returns: Unit. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> u.invert() Unit('m-1') """ if self.is_unknown(): result = self elif self.is_no_unit(): raise ValueError("Cannot invert a 'no-unit'.") else: try: ut_unit = _ud.invert(self.ut_unit) except _ud.UdunitsError as e: self._propogate_error('Failed to invert %r' % self, e) calendar = None result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
return result
[docs] def root(self, root): """ Returns the given root of the unit. Args: * root (int): Value by which the unit root is taken. Returns: None. For example: >>> import cf_units >>> u = cf_units.Unit('meters^2') >>> u.root(2) Unit('m') .. note:: Taking a fractional root of a unit is not supported. """ if round(root) != root: raise TypeError('An integer for the root argument is required') if self.is_unknown(): result = self elif self.is_no_unit(): raise ValueError("Cannot take the root of a 'no-unit'.") else: # only update the unit if it is not scalar if self == Unit('1'): result = self else: try: ut_unit = _ud.root(self.ut_unit, root) except _ud.UdunitsError as e: self._propogate_error('Failed to take the root of %r' % self, e) calendar = None result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
return result
[docs] def log(self, base): """ Returns the logorithmic unit corresponding to the given logorithmic base. Args: * base (int/float): Value of the logorithmic base. Returns: None. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> u.log(2) Unit('lb(re 1 m)') """ if self.is_unknown(): result = self elif self.is_no_unit(): raise ValueError("Cannot take the logarithm of a 'no-unit'.") else: try: ut_unit = _ud.log(base, self.ut_unit) except TypeError: raise TypeError('A numeric type for the base argument is ' ' required') except _ud.UdUnitsError as e: msg = 'Failed to calculate logorithmic base of %r' % self self._propogate_error(msg, e) calendar = None result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar)
return result def __str__(self): """ Returns a simple string representation of the unit. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('miles/hour') >>> str(u) 'miles/hour' """ return self.origin or self.symbol def __repr__(self): """ Returns a string representation of the unit object. Returns: string. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> repr(u) "Unit('meters')" """ if self.calendar is None: result = "%s('%s')" % (self.__class__.__name__, self) else: result = "%s('%s', calendar='%s')" % (self.__class__.__name__, self, self.calendar) return result def _offset_common(self, offset): if self.is_unknown(): result = self elif self.is_no_unit(): raise ValueError("Cannot offset a 'no-unit'.") else: try: ut_unit = _ud.offset(self.ut_unit, offset) except TypeError: result = NotImplemented except _ud.UdunitsError as e: self._propogate_error('Failed to offset %r' % self, e) else: calendar = None result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar) return result def __add__(self, other): return self._offset_common(other) def __sub__(self, other): try: other = -other except TypeError: result = NotImplemented else: result = self._offset_common(-other) return result def _op_common(self, other, op_func): # Convienience method to create a new unit from an operation between # the units 'self' and 'other'. op_label = op_func.__name__ other = as_unit(other) if self.is_no_unit() or other.is_no_unit(): raise ValueError("Cannot %s a 'no-unit'." % op_label) if self.is_unknown() or other.is_unknown(): result = _Unit(_CATEGORY_UNKNOWN, None) else: try: ut_unit = op_func(self.ut_unit, other.ut_unit) except _ud.UdunitsError as e: msg = 'Failed to %s %r by %r' % (op_label, self, other) self._propogate_error(msg, e) calendar = None result = _Unit(_CATEGORY_UDUNIT, ut_unit, calendar) return result def __rmul__(self, other): # NB. Because we've subclassed a tuple, we need to define this to # prevent the default tuple-repetition behaviour. # ie. 2 * ('a', 'b') -> ('a', 'b', 'a', 'b') return self * other def __mul__(self, other): """ Multiply the self unit by the other scale factor or unit and return the Unit result. Note that, multiplication involving an 'unknown' unit will always result in an 'unknown' unit. Args: * other (int/float/string/Unit): Multiplication scale factor or unit. Returns: Unit. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> v = cf_units.Unit('hertz') >>> u*v Unit('m.s-1') """ return self._op_common(other, _ud.multiply) def __div__(self, other): """ Divide the self unit by the other scale factor or unit and return the Unit result. Note that, division involving an 'unknown' unit will always result in an 'unknown' unit. Args: * other (int/float/string/Unit): Division scale factor or unit. Returns: Unit. For example: >>> import cf_units >>> u = cf_units.Unit('m.s-1') >>> v = cf_units.Unit('hertz') >>> u / v Unit('m') """ return self._op_common(other, _ud.divide) def __truediv__(self, other): """ Divide the self unit by the other scale factor or unit and return the Unit result. Note that, division involving an 'unknown' unit will always result in an 'unknown' unit. Args: * other (int/float/string/Unit): Division scale factor or unit. Returns: Unit. For example: >>> import cf_units >>> u = cf_units.Unit('m.s-1') >>> v = cf_units.Unit('hertz') >>> u / v Unit('m') """ return self.__div__(other) def __pow__(self, power): """ Raise the unit by the given power and return the Unit result. Note that, UDUNITS-2 does not support raising a non-dimensionless unit by a fractional power. Approximate floating point power behaviour has been implemented specifically for cf_units. Args: * power (int/float): Value by which the unit power is raised. Returns: Unit. For example: >>> import cf_units >>> u = cf_units.Unit('meters') >>> u**2 Unit('m2') """ try: power = float(power) except ValueError: raise TypeError('A numeric value is required for the power' ' argument.') if self.is_unknown(): result = self elif self.is_no_unit(): raise ValueError("Cannot raise the power of a 'no-unit'.") elif self == Unit('1'): # 1 ** N -> 1 result = self else: # UDUNITS-2 does not support floating point raise/root. # But if the power is of the form 1/N, where N is an integer # (within a certain acceptable accuracy) then we can find the Nth # root. if not approx_equal(power, 0.0) and abs(power) < 1: if not approx_equal(1 / power, round(1 / power)): raise ValueError('Cannot raise a unit by a decimal.') root = int(round(1 / power)) result = self.root(root) else: # Failing that, check for powers which are (very nearly) simple # integer values. if not approx_equal(power, round(power)): msg = 'Cannot raise a unit by a decimal (got %s).' % power raise ValueError(msg) power = int(round(power)) try: ut_unit = _ud.raise_(self.ut_unit, power) except _ud.UdunitsError as e: self._propogate_error('Failed to raise the power of %r' % self, e) result = _Unit(_CATEGORY_UDUNIT, ut_unit) return result def _identity(self): # Redefine the comparison/hash/ordering identity as used by # _OrderedHashable. return (self.name, self.calendar) def __eq__(self, other): """ Compare the two units for equality and return the boolean result. Args: * other (string/Unit): Unit to be compared. Returns: Boolean. For example: >>> from cf_units import Unit >>> Unit('meters') == Unit('millimeters') False >>> Unit('meters') == 'm' True """ other = as_unit(other) # Compare category (i.e. unknown, no_unit, etc.). if self.category != other.category: return False # Compare calendar as UDUNITS cannot handle calendars. if self.calendar != other.calendar: return False # Compare UDUNITS. res = _ud.compare(self.ut_unit, other.ut_unit) return res == 0 def __ne__(self, other): """ Compare the two units for inequality and return the boolean result. Args: * other (string/Unit): Unit to be compared. Returns: Boolean. For example: >>> from cf_units import Unit >>> Unit('meters') != Unit('millimeters') True >>> Unit('meters') != 'm' False """ return not self == other
[docs] def convert(self, value, other, ctype=FLOAT64, inplace=False): """ Converts a single value or NumPy array of values from the current unit to the other target unit. If the units are not convertible, then no conversion will take place. Args: * value (int/float/numpy.ndarray): Value/s to be converted. * other (string/Unit): Target unit to convert to. * ctype (cf_units.FLOAT32/cf_units.FLOAT64): Floating point 32-bit single-precision (cf_units.FLOAT32) or 64-bit double-precision (cf_units.FLOAT64) used for conversion when `value` is not a NumPy array or is a NumPy array composed of NumPy integers. The default is 64-bit double-precision conversion. * inplace (bool): If ``False``, return a deep copy of the value array. If ``True``, convert the values in-place. A new array will be created if ``value`` is an integer NumPy array. Returns: float or numpy.ndarray of appropriate float type. For example: >>> import cf_units >>> import numpy as np >>> c = cf_units.Unit('deg_c') >>> f = cf_units.Unit('deg_f') >>> c.convert(0, f) 31.999999999999886 >>> c.convert(0, f, cf_units.FLOAT32) 32.0 >>> a64 = np.arange(3, dtype=np.float64) >>> c.convert(a64, f) array([32. , 33.8, 35.6]) >>> a32 = np.arange(3, dtype=np.float32) >>> c.convert(a32, f) array([32. , 33.8, 35.6], dtype=float32) .. note:: Conversion between unit calendars is not permitted unless the calendars are aliases, see :attr:`cf_units.CALENDAR_ALIASES`. >>> from cf_units import Unit >>> a = Unit('days since 1850-1-1', calendar='gregorian') >>> b = Unit('days since 1851-1-1', calendar='standard') >>> a.convert(365.75, b) 0.75 """ other = as_unit(other) if self == other: return value if self.is_convertible(other): if inplace: result = value else: result = copy.deepcopy(value) # Use utime for converting reference times that are not using a # gregorian calendar as it handles these and udunits does not. if self.is_time_reference() \ and self.calendar != CALENDAR_GREGORIAN: ut1 = self.utime() ut2 = other.utime() result = ut2.date2num(ut1.num2date(result)) # Preserve the datatype of the input array if it was float32. if (isinstance(value, np.ndarray) and value.dtype == np.float32): result = result.astype(np.float32) else: try: ut_converter = _ud.get_converter(self.ut_unit, other.ut_unit) except _ud.UdunitsError as e: self._propogate_error('Failed to convert %r to %r' % (self, other), e) if isinstance(result, np.ndarray): # Can only handle array of np.float32 or np.float64 so # cast array of ints to array of floats of requested # precision. if issubclass(result.dtype.type, np.integer): result = result.astype(ctype) # Convert arrays with explicit endianness to native # endianness: udunits seems to be tripped up by arrays # with endianness other than native. if result.dtype.byteorder != '=': if inplace: raise ValueError( 'Unable to convert non-native byte ordered ' 'array in-place. Consider byte-swapping ' 'first.') else: result = result.astype(result.dtype.type) # Strict type check of numpy array. if result.dtype.type not in (np.float32, np.float64): raise TypeError( "Expect a numpy array of '%s' or '%s'" % np.float32, np.float64) ctype = result.dtype.type # Utilise global convenience dictionary # _cv_convert_array to convert our array in 1d form result_tmp = result.ravel(order='A') # Do the actual conversion. _cv_convert_array[ctype]( ut_converter, result_tmp, result_tmp) # If result_tmp was a copy, not a view (i.e. not C # contiguous), copy the data back to the original. if not np.shares_memory(result, result_tmp): result_tmp = result_tmp.reshape( result.shape, order='A') if isinstance(result, np.ma.MaskedArray): result.data[...] = result_tmp else: result[...] = result_tmp else: if ctype not in _cv_convert_scalar: raise ValueError('Invalid target type. Can only ' 'convert to float or double.') # Utilise global convenience dictionary # _cv_convert_scalar result = _cv_convert_scalar[ctype](ut_converter, result) return result else: raise ValueError("Unable to convert from '%r' to '%r'." %
(self, other))
[docs] def utime(self): """ Returns a cftime.utime object which performs conversions of numeric time values to/from datetime objects given the current calendar and unit time reference. The current unit time reference must be of the form: '<time-unit> since <time-origin>' i.e. 'hours since 1970-01-01 00:00:00' Returns: cftime.utime. For example: >>> import cf_units >>> u = cf_units.Unit('hours since 1970-01-01 00:00:00', ... calendar=cf_units.CALENDAR_STANDARD) >>> ut = u.utime() >>> ut.num2date(2) datetime.datetime(1970, 1, 1, 2, 0, 0, 6) """ if self.calendar is None: raise ValueError('Unit has undefined calendar') # `cftime` cannot parse long time intervals ("months" or "years"). if self.is_long_time_interval(): interval = self.origin.split(' ')[0] emsg = ('Time units with interval of "months", "years" ' '(or singular of these) cannot be processed, got {!r}.') raise ValueError(emsg.format(interval)) # # ensure to strip out non-parsable 'UTC' postfix, which # is generated by UDUNITS-2 formatted output #
return cftime.utime(str(self).rstrip(" UTC"), self.calendar)
[docs] def date2num(self, date): """ Returns the numeric time value calculated from the datetime object using the current calendar and unit time reference. The current unit time reference must be of the form: '<time-unit> since <time-origin>' i.e. 'hours since 1970-01-01 00:00:00' Works for scalars, sequences and numpy arrays. Returns a scalar if input is a scalar, else returns a numpy array. Args: * date (datetime): A datetime object or a sequence of datetime objects. The datetime objects should not include a time-zone offset. Returns: float or numpy.ndarray of float. For example: >>> import cf_units >>> import datetime >>> u = cf_units.Unit('hours since 1970-01-01 00:00:00', ... calendar=cf_units.CALENDAR_STANDARD) >>> u.date2num(datetime.datetime(1970, 1, 1, 5)) 5.00000000372529 >>> u.date2num([datetime.datetime(1970, 1, 1, 5), ... datetime.datetime(1970, 1, 1, 6)]) array([5., 6.]) """ cdf_utime = self.utime() date = _discard_microsecond(date)
return cdf_utime.date2num(date)
[docs] def num2date(self, time_value): """ Returns a datetime-like object calculated from the numeric time value using the current calendar and the unit time reference. The current unit time reference must be of the form: '<time-unit> since <time-origin>' i.e. 'hours since 1970-01-01 00:00:00' The datetime objects returned are 'real' Python datetime objects if the date falls in the Gregorian calendar (i.e. the calendar is 'standard', 'gregorian', or 'proleptic_gregorian' and the date is after 1582-10-15). Otherwise a 'phoney' datetime-like object (cftime.datetime) is returned which can handle dates that don't exist in the Proleptic Gregorian calendar. Works for scalars, sequences and numpy arrays. Returns a scalar if input is a scalar, else returns a numpy array. Args: * time_value (float): Numeric time value/s. Maximum resolution is 1 second. Returns: datetime, or numpy.ndarray of datetime object. For example: >>> import cf_units >>> u = cf_units.Unit('hours since 1970-01-01 00:00:00', ... calendar=cf_units.CALENDAR_STANDARD) >>> u.num2date(6) datetime.datetime(1970, 1, 1, 6, 0) >>> u.num2date([6, 7]) array([datetime.datetime(1970, 1, 1, 6, 0), datetime.datetime(1970, 1, 1, 7, 0)], dtype=object) """ cdf_utime = self.utime()
return _num2date_to_nearest_second(time_value, cdf_utime)