How to use the cfunits.Units.conform 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
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
github atmos-python / atmos / atmos / solve.py View on Github external
else:  # no solution caching for this class
            funcs, func_args, extra_values = \
                _get_shortest_solution(args, tuple(self.vars.keys()), (),
                                       self.methods)
        # Above method completed successfully if no ValueError has been raised
        # Calculate each quantity we need to calculate in order
        for i, func in enumerate(funcs):
            # Compute this quantity
            value = func(*[self.vars[varname] for varname in func_args[i]])
            # Add it to our dictionary of quantities for successive functions
            self.vars[extra_values[i]] = value
        return_list = []
        for arg in args:
            # do corrections for non-standard units
            if arg in self.units and self.units[arg] != self._ref_units[arg]:
                self.vars[arg] = cfunits.Units.conform(
                    self.vars[arg], self._ref_units[arg], self.units[arg])
            return_list.append(self.vars[arg])
        if self._debug:
            # We should return a list of funcs as the last item returned
            if len(return_list) == 1:
                return _check_scalar(return_list[0]), funcs
            else:
                return ([_check_scalar(val) for val in return_list] +
                        [funcs, ])
        else:
            # no function debugging, just return the quantities
            if len(args) == 1:
                return _check_scalar(return_list[0])
            else:
                return [_check_scalar(val) for val in return_list]

cfunits

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

MIT
Latest version published 8 months ago

Package Health Score

52 / 100
Full package analysis

Similar packages