Пример #1
0
Файл: nc.py Проект: wk1984/ocgis
def create_dimension_map_entry(src, variables, strict=False, attr_name='axis'):
    """
    Create a dimension map entry dictionary by searching variable metadata using attribute constraints.

    :param src: The source information to use for constructing the entry. If ``src`` is a dictionary, it must have two
     entries. The key ``'value'`` corresponds to the string attribute value. The key ``'axis'`` is the representative
     axis to assign the source value (for example ``'X'`` or ``'Y'``).
    :type src: str | dict
    :param dict variables: The metadata entries for the group's variables.
    :param bool strict: If ``False``, do not use a strict interpretation of metadata. Allow some standard approaches for
     handling metadata exceptions.
    :param str attr_name: Name of the attribute to use for checking the attribute values form ``src``.
    :return: dict
    """
    if isinstance(src, dict):
        axis = src['axis']
        attr_value = src['value']
    else:
        axis = src
        attr_value = src

    axis_vars = []
    for variable in list(variables.values()):
        vattrs = variable.get('attrs', {})
        if vattrs.get(attr_name) == attr_value:
            if len(variable['dimensions']) == 0:
                pass
            else:
                axis_vars.append(variable['name'])

    # Try to find by default names.
    if not strict and len(axis_vars) == 0:
        possible_names = CFName.get_axis_mapping().get(axis, [])
        for pn in possible_names:
            if pn in list(variables.keys()):
                axis_vars.append(variables[pn]['name'])

    if len(axis_vars) == 1:
        var_name = axis_vars[0]
        dims = list(variables[var_name]['dimensions'])

        if not strict:
            # Use default index positions for X/Y dimensions.
            if axis in ('X', 'Y') and len(dims) > 1:
                if axis == 'Y':
                    dims = [dims[0]]
                elif axis == 'X':
                    dims = [dims[1]]

        ret = {'variable': var_name, DimensionMapKey.DIMENSION: dims}
    elif len(axis_vars) > 1:
        msg = 'Multiple axis (axis="{}") possibilities found using variable(s) "{}". Use a dimension map to specify ' \
              'the appropriate coordinate dimensions.'
        w = OcgWarning(msg.format(axis, axis_vars))
        warn(w)
        ret = None
    else:
        ret = None
    return ret
Пример #2
0
def format_attribute_for_dump_report(attr_value):
    if isinstance(attr_value, six.string_types):
        try:
            ret = '"{}"'.format(attr_value)
        except UnicodeEncodeError:
            msg = "UnicodeEncodeError encountered. Skipping attribute value formatting by changing value to the empty " \
                  "string."
            warn(OcgWarning(msg))
            ret = '""'
    else:
        ret = attr_value
    return ret
Пример #3
0
    def __call__(self,
                 msg=None,
                 logger=None,
                 level=logging.INFO,
                 alias=None,
                 ugid=None,
                 exc=None,
                 force=False):
        # attach a default exception to messages to handle warnings if an exception is not provided
        if level == logging.WARN:
            if exc is None:
                exc = OcgWarning(msg)
            if not env.SUPPRESS_WARNINGS or force:
                logging.captureWarnings(False)
                try:
                    warn(exc)
                finally:
                    logging.captureWarnings(env.SUPPRESS_WARNINGS)

        if self.callback is not None and self.callback_level <= level:
            if msg is not None:
                self.callback(msg)
            elif exc is not None:
                callback_msg = '{0}: {1}'.format(exc.__class__.__name__, exc)
                self.callback(callback_msg)

        if self.null:
            if exc is None or level == logging.WARN:
                pass
            else:
                raise exc
        else:
            dest_level = level or self.level
            # get the logger by string name
            if isinstance(logger, six.string_types):
                dest_logger = self.get_logger(logger)
            else:
                dest_logger = logger or self.parent
            if alias is not None:
                msg = self.get_formatted_msg(msg, alias, ugid=ugid)
            if exc is None:
                dest_logger.log(dest_level, msg)
            else:
                if level == logging.WARN:
                    wmsg = '{0}: {1}'.format(exc.__class__.__name__, str(exc))
                    dest_logger.warning(wmsg)
                else:
                    dest_logger.exception(msg)
                    raise exc
Пример #4
0
def get_dimension_map_entry(axis, variables, dimensions, strict=False):
    axis_vars = []
    for variable in list(variables.values()):
        vattrs = variable['attrs']
        if vattrs.get('axis') == axis:
            if len(variable['dimensions']) == 0:
                pass
            else:
                axis_vars.append(variable['name'])

    # Try to find by default names.
    if not strict and len(axis_vars) == 0:
        possible_names = CFName.get_name_mapping().get(axis, [])
        for pn in possible_names:
            if pn in list(variables.keys()):
                axis_vars.append(variables[pn]['name'])

    if len(axis_vars) == 1:
        var_name = axis_vars[0]
        dims = list(variables[var_name]['dimensions'])

        if not strict:
            # Use default index positions for X/Y dimensions.
            if axis in ('X', 'Y') and len(dims) > 1:
                if axis == 'Y':
                    dims = [dims[0]]
                elif axis == 'X':
                    dims = [dims[1]]

        ret = {'variable': var_name, DimensionMapKey.DIMENSION: dims}
    elif len(axis_vars) > 1:
        msg = 'Multiple axis (axis="{}") possibilities found using variable(s) "{}". Use a dimension map to specify ' \
              'the appropriate coordinate dimensions.'
        w = OcgWarning(msg.format(axis, axis_vars))
        warn(w)
        ret = None
    else:
        ret = None
    return ret
Пример #5
0
    def write_attributes_to_netcdf_object(self, target):
        """
        :param target: The attribute write target.
        :type target: :class:`netCDF4.Variable` | :class:`netCDF4.Dataset`
        """

        for k, v in self.attrs.items():
            if k.startswith('_') or v is None:
                # Do not write private/protected attributes used by netCDF or None values.
                continue
            try:
                if isinstance(v, six.string_types):
                    v = str(v)
                if k == 'axis' and isinstance(v, six.string_types):
                    # HACK: Axis writing was causing a strange netCDF failure.
                    target.axis = str(v)
                else:
                    target.setncattr(str(k), v)
            except UnicodeError:
                # Just write the attribute if we encounter a unicode error.
                msg = "UnicodeError encountered when converting the value of attribute with name '{}' to a string. " \
                      "Sending the value to the netCDF API".format(k)
                warn(OcgWarning(msg))
                target.setncattr(k, v)
Пример #6
0
# different location. it is unclear what mechanism causes the import issue. ESMF is not a required package, so a failed
# import is okay (if it is not installed).
try:
    import ESMF
except ImportError:
    pass

# HACK!! the gdal data is often not read correctly by the osgeo installation. remove the necessity for users to set this
# variable when installing.

# Need to check for bad GDAL data directories.
if 'GDAL_DATA' in os.environ:
    if not os.path.exists(os.environ['GDAL_DATA']):
        msg = 'The GDAL_DATA directory set in environment does not exist!: {}. It will be overloaded.'.format(
            os.environ['GDAL_DATA'])
        warn(OcgWarning(msg))
        os.environ.pop('GDAL_DATA')

if 'GDAL_DATA' not in os.environ:
    try:
        if os.name == 'nt':
            # Try setting the value assuming an Anaconda environment on Windows.
            datadir = os.path.join(
                os.path.split(sys.executable)[0], 'share', 'gdal')
            if not os.path.exists(datadir):
                datadir = os.path.join(
                    os.path.split(sys.executable)[0], 'Library', 'share',
                    'gdal')
        else:
            # Try using the gdal-config executable to find the data directory.
            datadir = subprocess.check_output(['gdal-config',