Esempio n. 1
0
    def collect(self):
        """
        Collects the metadata supplied in the constructor
        :rtype: GlobalExtraMetadata
        """
        global_metadata = {}
        for key, value in self.extra_metadata_info.global_attributes.items():
            # if value is empty (e.g: metadata "time_of_coverage": "") then should not evaluate this value
            # output of extra metadata should be string in any cases
            if str(value) != "":
                global_metadata[key] = str(self.evaluator.evaluate(value, self.metadata_entry.evalutor_slice))
            else:
                global_metadata[key] = str(value)

        global_metadata = escape_metadata_nested_dicts(global_metadata)

        # NOTE: this bands's metadata is added to gmlcov:metadata not swe:field
        bands_metadata = {}
        # band_attributes is a dict of keys, values
        if self.extra_metadata_info.bands_attributes is not None:
            for band, band_attributes in self.extra_metadata_info.bands_attributes.items():
                bands_metadata[band] = {}
                for key, value in band_attributes.items():
                    # if value is empty (e.g: metadata "time_of_coverage": "") then should not evaluate this value
                    # output of extra metadata should be string in any cases
                    if str(value) != "":
                        bands_metadata[band][key] = str(self.evaluator.evaluate(value, self.metadata_entry.evalutor_slice))
                    else:
                        bands_metadata[band][key] = str(value)

            bands_metadata = escape_metadata_nested_dicts(bands_metadata)

        # Axes metadata (dimension's metadata)
        axes_metadata = {}
        if self.extra_metadata_info.axes_attributes is not None:
            # axes_attributes is a dict of keys, values
            for axis, axis_attributes in self.extra_metadata_info.axes_attributes.items():
                axes_metadata[axis] = {}
                if type(axis_attributes) is dict:
                    for key, value in axis_attributes.items():
                        # if value is empty (e.g: metadata "time_of_coverage": "") then should not evaluate this value
                        # output of extra metadata should be string in any cases
                        if str(value) != "":
                            axes_metadata[axis][key] = str(self.evaluator.evaluate(value, self.metadata_entry.evalutor_slice))
                        else:
                            axes_metadata[axis][key] = str(value)
                else:
                    # It should be a string (e.g: ${netcdf:variable:lat:metadata}) and need to be evaluated
                    axes_metadata[axis] = self.evaluator.evaluate(axis_attributes, self.metadata_entry.evalutor_slice)

            axes_metadata = escape_metadata_nested_dicts(axes_metadata)

        return GlobalExtraMetadata(global_metadata, bands_metadata, axes_metadata)
Esempio n. 2
0
    def _netcdf_bands_metadata_fields(self):
        """
        Returns the bands metadata for netCDF file
        + If "bands" is specified in ingredient file under "metadata" with: "bands": { "band1": "auto", "band2": { "key": "value" }
        then "band1" will get metadata automatically from file (same as "band1" is not specified), "band2" will get metadata
        from the specified keys, values.
        + If "bands" is not specified in ingredient file or specified with "bands": "auto", then all coverage's bands' metadata
        will be extracted automatically from file.
        :rtype: dict
        """
        # a list of user defined bands
        user_bands = self._read_bands()

        if "metadata" in self.options['coverage']:
            for file in self.session.get_files():
                try:
                    file_path = file.filepath
                    # Just fetch all metadata for user specified bands
                    bands_metadata = NetcdfToCoverageConverter.parse_netcdf_bands_metadata(file_path, user_bands)

                    if "bands" in self.options['coverage']['metadata']:
                        # a dictionary of user defined bands' metadata
                        bands_metadata_configured = self.options['coverage']['metadata']['bands']

                        if bands_metadata_configured != "auto":

                            # validate if band's name does exist in list of user defined bands
                            for band, band_attributes in bands_metadata_configured.items():
                                exist = False
                                for user_band in user_bands:

                                    if str(band) == str(user_band.name):
                                        # Metadata for 1 band is configured manually in ingredient file
                                        if band_attributes != "auto":
                                            bands_metadata[band] = band_attributes

                                        exist = True
                                        break

                                if exist is False:
                                    raise RuntimeException(
                                        "Band's metadata with name: '" + band + "' does not exist in user defined bands.")

                    return escape_metadata_nested_dicts(bands_metadata)
                except Exception as e:
                    if ConfigManager.skip == True:
                        pass
                    else:
                        raise e

        return {}
Esempio n. 3
0
    def collect(self):
        """
        Collects the metadata supplied in the constructor
        :rtype: LocalExtraMetadata
        """
        local_metadata = {}

        # if we have local metadata go ahead
        if len(self.extra_metadata_info.local_attributes.items()) > 0:

            for meta_key, sentence in self.extra_metadata_info.local_attributes.items():
                # output of extra metadata should be string in any cases
                local_metadata[meta_key] = str(self.evaluator.evaluate(sentence, self.metadata_entry.evalutor_slice))

        local_metadata = escape_metadata_nested_dicts(local_metadata)

        return LocalExtraMetadata(local_metadata, self.metadata_entry.slice_subsets)
Esempio n. 4
0
    def _netcdf_axes_metadata_fields(self):
        """
        Returns the axes metadata for netCDF file.

        NOTE: There is no "auto" for fetching axes' metadata because the axes' labels from netCDF don't match with
        axes's labels from CRS (e.g: in netCDF axis is 'lon', but in CRS it is 'Long').

        Example: a coverage has 3 axes, ff "axes" is specified in ingredient file
        under "metadata" with: "axes": { "axis1": { "key": "value" }, "axis2": "lon" }
        then metadata for axis1 is specified from user, metadata for axis2 is fetched from axis named 'lon' and
        metadata for axis3 is empty because not specified.
        :rtype: dict
        """
        # a list of user defined bands
        crs = self._resolve_crs(self.options['coverage']['crs'])
        user_axes = self._read_axes(crs)

        if "metadata" in self.options['coverage']:
            if "axes" in self.options['coverage']['metadata']:
                # a dictionary of user defined bands' metadata
                axes_metadata = self.options['coverage']['metadata']['axes']

                if type(axes_metadata) is dict:
                    # validate if axis's name does exist in list of user defined axes
                    for axis, axis_attributes in axes_metadata.items():
                        exists = False
                        for user_axis in user_axes:
                            if str(axis) == str(user_axis.name):
                                exists = True
                                break
                        if exists is False:
                            raise RuntimeException(
                                "Metadata of axis with name '" + axis + "' does not exist in user defined bands.")
                else:
                    raise RuntimeException("Bands metadata is not valid, given type '" + type(axes_metadata) + "'.")
            else:
                # no "axes" in metadata specified, just empty object
                axes_metadata = {}
            return escape_metadata_nested_dicts(axes_metadata)

        return {}
Esempio n. 5
0
    def _netcdf_bands_metadata_fields(self):
        """
        Returns the bands metadata for netCDF file
        + If "bands" is specified in ingredient file under "metadata" with: "bands": { "band1": "auto", "band2": { "key": "value" }
        then "band1" will get metadata automatically from file (same as "band1" is not specified), "band2" will get metadata
        from the specified keys, values.
        + If "bands" is not specified in ingredient file or specified with "bands": "auto", then all coverage's bands' metadata
        will be extracted automatically from file.
        :rtype: dict
        """
        # a list of user defined bands
        user_bands = self._read_bands()

        if "metadata" in self.options['coverage']:
            if "bands" in self.options['coverage']['metadata']:
                # a dictionary of user defined bands' metadata
                bands_metadata = self.options['coverage']['metadata']['bands']

                if bands_metadata == "auto":
                    # Just fetch all metadata for user specified bands
                    bands_metadata = self.__parse_netcdf_bands_metadata(user_bands)
                elif type(bands_metadata) is dict:
                    # validate if band's name does exist in list of user defined bands
                    for band, band_attributes in bands_metadata.items():
                        exist = False
                        for user_band in user_bands:
                            if str(band) == str(user_band.name):
                                exist = True
                                break
                        if exist is False:
                            raise RuntimeException(
                                "Band's metadata with name: '" + band + "' does not exist in user defined bands.")
                else:
                    raise RuntimeException("Bands metadata is not valid, given type '" + type(bands_metadata) + "'.")
            else:
                # "bands" is missing from ingredient file, it is as same as "bands": "auto"
                bands_metadata = self.__parse_netcdf_bands_metadata(user_bands)

            return escape_metadata_nested_dicts(bands_metadata)

        return {}
Esempio n. 6
0
    def _netcdf_axes_metadata_fields(self):
        """
        Returns the axes metadata for netCDF file.

         + If "axes" is specified in ingredient file under "metadata" as "axes":"auto"
         or if "axes" is not specified in ingredient file. All axes's metadata are collected from the first file
         for coverage's global metadata.

         + If "axes" is specified in ingredient file under "metadata" with manual configuration, e.g:
         "axes": {
                "Long": "auto",
                "Lat": { "key1": "value1", "key2": "value2",... }
                 }

         then, axis "Long" will get metadata automatically from file (same as if "Long" is not specified),
         "Lat" will get metadata from the specified "keys": "values".

         NOTE: The axis variable names (e.g: lon, lat,...) in input file are the ones configured by "slicer"/"axes"
         in ingredient file by parsing axis's "min" or "max" configuration (e.g: Long's "min": "${netcdf:variable:lon:min}"
         which means CRS axis 'Long' refers to axis variable 'lon' in netCDF file).

        :rtype: dict
        """
        crs = self._resolve_crs(self.options['coverage']['crs'])
        user_axes = self._read_axes(crs)

        for file in self.session.get_files():
            try:
                file_path = file.filepath
                crs_axes_configured_dict = self.options['coverage']['slicer']['axes']

                # Just fetch all metadata for user specified axes
                axes_metadata = NetcdfToCoverageConverter.parse_netcdf_axes_metadata(file_path, crs_axes_configured_dict)

                if "metadata" in self.options['coverage']:
                    if "axes" in self.options['coverage']['metadata']:
                        # a dictionary of user defined axes' metadata
                        axes_metadata_configured = self.options['coverage']['metadata']['axes']

                        if axes_metadata_configured != "auto":

                            # validate if axis's name does exist in list of user defined axes
                            for axis, axis_attributes in axes_metadata_configured.items():
                                exist = False
                                for user_axis in user_axes:

                                    if str(axis) == str(user_axis.name):
                                        # Metadata for 1 axis is configured manually in ingredient file
                                        if axis_attributes != "auto":
                                            axes_metadata[axis] = axis_attributes

                                        exist = True
                                        break

                                if exist is False:
                                    raise RuntimeException(
                                        "Metadata of axis with name '" + axis + "' does not exist in user defined bands.")

                    return escape_metadata_nested_dicts(axes_metadata)
            except Exception as e:
                if ConfigManager.skip == True:
                    pass
                else:
                    raise e

        return {}