How to use the cfunits.Units function in cfunits

To help you get started, we’ve selected a few cfunits examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github atmos-python / atmos / atmos / solve.py View on Github external
def __new__(cls, name, parents, dct):
        if dct['_equation_module'] is not None:
            # Update the class docstring
            if '__doc__' in dct.keys():
                dct['__doc__'] = _fill_doc(
                    dct['__doc__'], dct['_equation_module'],
                    dct['default_assumptions'])
            dct['_ref_units'] = {}
            for quantity in dct['_equation_module'].quantities.keys():
                dct['_ref_units'][quantity] = \
                    cfunits.Units(dct['_equation_module'].quantities[
                        quantity]['units'])
            assumptions = set([])
            for f in inspect.getmembers(equations):
                try:
                    assumptions.update(f[1].assumptions)
                except AttributeError:
                    pass
            dct['all_assumptions'] = tuple(assumptions)

        # we need to call type.__new__ to complete the initialization
        instance = super(SolverMeta, cls).__new__(cls, name, parents, dct)
        return instance
github NCPP / ocgis / src / ocgis / api / request / nc.py View on Github external
def conform_units_to(self,value):
        if value is not None:
            ## import the cfunits package and attempt to construct a units object.
            ## if this is okay, save the units string
            from cfunits import Units
            dest = Units(value)
            ## the units of the source data need to be specified or available
            ## in the metadata.
            try:
                src = self.units or self._get_units_from_metadata_()
                src = Units(src)
            except KeyError:
                exc = NoUnitsError(message='Units could not be read from source metadata. The "units" keyword argument may be needed.')
                ocgis_lh(exc=exc)
            ## units must be equivalent.
            try:
                assert(src.equivalent(dest))
            except AssertionError:
                ocgis_lh(exc=ValueError('The units specified in "conform_units_to" ("{0}") are not equivalent to the source units "{1}".'.format(dest.format(names=True),src.format(names=True))))
            self._conform_units_to = value
        else:
            self._conform_units_to = None
github NCPP / ocgis / src / ocgis / api / request / nc.py View on Github external
def conform_units_to(self,value):
        if value is not None:
            ## import the cfunits package and attempt to construct a units object.
            ## if this is okay, save the units string
            from cfunits import Units
            dest = Units(value)
            ## the units of the source data need to be specified or available
            ## in the metadata.
            try:
                src = self.units or self._get_units_from_metadata_()
                src = Units(src)
            except KeyError:
                exc = NoUnitsError(message='Units could not be read from source metadata. The "units" keyword argument may be needed.')
                ocgis_lh(exc=exc)
            ## units must be equivalent.
            try:
                assert(src.equivalent(dest))
            except AssertionError:
                ocgis_lh(exc=ValueError('The units specified in "conform_units_to" ("{0}") are not equivalent to the source units "{1}".'.format(dest.format(names=True),src.format(names=True))))
            self._conform_units_to = value
        else:
            self._conform_units_to = None
github atmos-python / atmos / atmos / solve.py View on Github external
self.units = {}
        remove_kwargs = []
        for kwarg in kwargs:
            m = _unit_kwarg_prog.match(kwarg)
            if m is not None:
                # select whichever group is not None
                var = m.group(1) or m.group(2)
                self._ensure_quantities(var)
                if var in self.units:
                    raise ValueError(
                        'units for {} specified multiple times'.format(var))
                unit_str = kwargs[kwarg]
                remove_kwargs.append(kwarg)
                if not isinstance(unit_str, string_types):
                    raise TypeError('units must be strings')
                self.units[var] = cfunits.Units(unit_str)
        for kwarg in remove_kwargs:
            kwargs.pop(kwarg)
        # make sure the remaining variables are quantities
        self._ensure_quantities(*kwargs.keys())
        # convert quantities to reference units
        for kwarg in kwargs:
            if (kwarg in self.units and
                    self.units[kwarg] != self._ref_units[kwarg]):
                # special unit defined
                # convert to reference unit for calculations
                kwargs[kwarg] = cfunits.Units.conform(
                    kwargs[kwarg], self.units[kwarg], self._ref_units[kwarg])
        # also store the quantities
        self.vars = kwargs

cfunits

A python interface to UNIDATA's UDUNITS-2 package with CF extensions

MIT
Latest version published 7 months ago

Package Health Score

55 / 100
Full package analysis

Similar packages