예제 #1
0
 def __init__(self, value_str):
     kvp = value_str.split(';')
     for pair in kvp:
         (key, value) = pair.split('=')
         key = key.strip()
         value = value.strip()
         if key == 'name':
             self.name = value
         elif key == 'eastlimit':
             try:
                 self.eastlimit = float(value)
             except Exception as e:
                 msg = "Unable to parse east limit {0}, error: {1}".format(
                     value, str(e))
                 raise GenericResourceMeta.ResourceMetaException(msg)
         elif key == 'northlimit':
             try:
                 self.northlimit = float(value)
             except Exception as e:
                 msg = "Unable to parse north limit {0}, error: {1}".format(
                     value, str(e))
                 raise GenericResourceMeta.ResourceMetaException(msg)
         elif key == 'southlimit':
             try:
                 self.southlimit = float(value)
             except Exception as e:
                 msg = "Unable to parse south limit {0}, error: {1}".format(
                     value, str(e))
                 raise GenericResourceMeta.ResourceMetaException(msg)
         elif key == 'westlimit':
             try:
                 self.westlimit = float(value)
             except Exception as e:
                 msg = "Unable to parse west limit {0}, error: {1}".format(
                     value, str(e))
                 raise GenericResourceMeta.ResourceMetaException(msg)
         elif key == 'units':
             self.units = value
         elif key == 'projection':
             self.projection = value
예제 #2
0
    def _read_resource_metadata(self):
        super(ToolResourceMeta, self)._read_resource_metadata()

        print("--- ToolResource ---")

        hsterms = rdflib.namespace.Namespace('http://hydroshare.org/terms/')

        # Get RequestUrlBase
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.RequestUrlBase, None)):
            # Get value
            value_lit = self._rmeta_graph.value(o, hsterms.value)
            if value_lit is None:
                msg = "RequestUrlBase for ToolResource was not found for resource {0}".\
                    format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.url_base = str(value_lit)

        # Get ResourceType
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ResourceType, None)):
            # Get type
            type_lit = self._rmeta_graph.value(o, hsterms.type)
            if type_lit is not None:
                self.resource_types.append(str(type_lit))

        # Get ToolVersion
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ToolVersion, None)):
            # Get value
            value_lit = self._rmeta_graph.value(o, hsterms.value)
            if value_lit is None:
                msg = "ToolVersion for ToolResource was not found for resource {0}".\
                    format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.version = str(value_lit)

        print("\t\t{0}".format((str(self))))
예제 #3
0
    def _read_resource_metadata(self):
        super(ModelInstanceResourceMeta, self)._read_resource_metadata()

        print("--- ModelInstanceResource ---")

        hsterms = rdflib.namespace.Namespace(
            'https://www.hydroshare.org/terms/')

        # Get ModelOutput
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ModelOutput, None)):
            # Get has_model_output
            has_model_output_lit = self._rmeta_graph.value(
                o, hsterms.includesModelOutput)
            if has_model_output_lit is None:
                msg = "includesModelOutput for ModelOutput was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.has_model_output = str(has_model_output_lit) == 'Yes'
        # Get ExecutedBy
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ExecutedBy, None)):
            # Get modelProgramName
            executed_by_name_lit = self._rmeta_graph.value(
                o, hsterms.modelProgramName)
            if executed_by_name_lit is not None:
                self.executed_by_name = str(executed_by_name_lit)
            # Get modelProgramIdentifier
            executed_by_uri_lit = self._rmeta_graph.value(
                o, hsterms.modelProgramIdentifier)
            if executed_by_uri_lit is not None:
                self.executed_by_uri = str(executed_by_uri_lit)
            if (self.executed_by_name is not None) ^ (self.executed_by_uri
                                                      is not None):
                msg = "Both modelProgramName and modelProgramIdentifier must be supplied if one is supplied."
                raise GenericResourceMeta.ResourceMetaException(msg)
        print("\t\t{0}".format(str(self)))
예제 #4
0
        def __init__(self, extent, crsName, crsRepresentationText, crsRepresentationType):
            # Parse extent into limits and unit
            extent_elem = extent.split(self.EXTENT_ELEM_SEP)
            if len(extent_elem) != len(self.EXTENT_ELEM):
                msg = "Extent {0} does not have the required number of elements ({1}).".format(extent,
                                                                                               len(self.EXTENT_ELEM))
                raise GenericResourceMeta.ResourceMetaException(msg)
            for e in extent_elem:
                kvp = e.strip()
                k, v = kvp.split(self.EXTENT_VAL_SEP)
                if k not in self.EXTENT_ELEM:
                    msg = "Extent element {0} is not valid.  Entire extent was {1}.".format(k,
                                                                                            extent)
                setattr(self, k, v)

            self.crsName = crsName
            self.crsRepresentationText = crsRepresentationText
            self.crsRepresentationType = crsRepresentationType
예제 #5
0
    def write_metadata_to_resource(self, resource):
        """
        Write metadata to resource

        :param resource: ModelInstanceResource instance
        """
        super(ModelInstanceResourceMeta,
              self).write_metadata_to_resource(resource)

        resource.metadata._model_output.update(
            includes_output=self.has_model_output)

        if self.executed_by_uri:
            uri_stripped = self.executed_by_uri.strip('/')
            short_id = os.path.basename(uri_stripped)
            if short_id == '':
                msg = "ExecutedBy URL {0} does not contain a model program resource ID, "
                msg += "for resource {1}"
                msg = msg.format(self.executed_by_uri, self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            # Make sure the resource specified by ExecutedBy exists
            try:
                executed_by_resource = get_resource_by_shortkey(short_id,
                                                                or_404=False)
            except BaseResource.DoesNotExist:
                msg = "ExecutedBy resource {0} does not exist.".format(
                    short_id)
                raise HsDeserializationDependencyException(short_id, msg)
            executed_by = resource.metadata.executed_by
            if not executed_by:
                # Create
                ExecutedBy.create(content_object=resource.metadata,
                                  model_name=short_id)
            else:
                # Update
                ExecutedBy.update(executed_by.element_id, model_name=short_id)
예제 #6
0
    def _read_resource_metadata(self):
        super(RasterResourceMeta, self)._read_resource_metadata()

        print("--- RasterResourceMeta ---")

        # Also parse using SAX so that we can capture certain metadata elements
        # in the same order in which they appear in the RDF+XML serialization.
        SAX_parse_results = RasterResourceSAXHandler()
        xml.sax.parse(self.rmeta_path, SAX_parse_results)

        hsterms = rdflib.namespace.Namespace('http://hydroshare.org/terms/')

        # Get CellInformation
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.CellInformation, None)):
            self.cell_info = RasterResourceMeta.CellInformation()
            # Get name
            name_lit = self._rmeta_graph.value(o, hsterms.name)
            if name_lit is None:
                msg = "Name for CellInformation was not found for resource {0}".\
                    format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.cell_info.name = str(name_lit)
            # Get rows
            rows_lit = self._rmeta_graph.value(o, hsterms.rows)
            if rows_lit is None:
                msg = "Rows attribute was not found for CellInformation for resource {0}".\
                    format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.cell_info.rows = int(str(rows_lit))
            # Get columns
            columns_lit = self._rmeta_graph.value(o, hsterms.columns)
            if columns_lit is None:
                msg = "Columns attribute was not found for CellInformation for resource {0}".\
                    format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.cell_info.columns = int(str(columns_lit))
            # Get cellSizeXValue
            cellX_lit = self._rmeta_graph.value(o, hsterms.cellSizeXValue)
            if cellX_lit is None:
                msg = "cellSizeXValue attribute was not found for CellInformation "
                msg += "for resource {0}"
                msg = msg.format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.cell_info.cellSizeXValue = float(str(cellX_lit))
            # Get cellSizeYValue
            cellY_lit = self._rmeta_graph.value(o, hsterms.cellSizeYValue)
            if cellY_lit is None:
                msg = "cellSizeYValue attribute was not found for CellInformation "
                msg += "for resource {0}"
                msg = msg.format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.cell_info.cellSizeYValue = float(str(cellY_lit))
            # Get cellDataType
            celldt_lit = self._rmeta_graph.value(o, hsterms.cellDataType)
            if celldt_lit is None:
                msg = "cellDataType attribute was not found for CellInformation "
                msg += "for resource {0}"
                msg = msg.format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.cell_info.cellDataType = str(celldt_lit)
            # Get noDateValue
            nodata_lit = self._rmeta_graph.value(o, hsterms.noDataValue)
            if nodata_lit is not None:
                self.cell_info.noDataValue = float(str(nodata_lit))
            print("\t\t{0}".format(self.cell_info))

        # Get BandInformation
        if SAX_parse_results:
            # Use band info from SAX parser
            self.band_info = list(SAX_parse_results.band_info)
        else:
            # Get band info from RDF
            for s, p, o in self._rmeta_graph.triples(
                (None, hsterms.BandInformation, None)):
                band_info = RasterResourceMeta.BandInformation()
                # Get name
                name_lit = self._rmeta_graph.value(o, hsterms.name)
                if name_lit is None:
                    msg = "Name for BandInformation was not found for resource {0}".\
                        format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                band_info.name = str(name_lit)
                # Get variableName
                varname_lit = self._rmeta_graph.value(o, hsterms.variableName)
                if varname_lit is None:
                    msg = "variableName for BandInformation was not found for resource {0}".\
                        format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                band_info.variableName = str(varname_lit)
                # Get variableUnit
                varunit_lit = self._rmeta_graph.value(o, hsterms.variableUnit)
                if varunit_lit is None:
                    msg = "variableUnit for BandInformation was not found for resource {0}".\
                        format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                band_info.variableUnit = str(varunit_lit)
                # Get method
                method_lit = self._rmeta_graph.value(o, hsterms.method)
                if method_lit is not None:
                    band_info.method = str(method_lit)
                # Get comment
                comment_lit = self._rmeta_graph.value(o, hsterms.comment)
                if comment_lit is not None:
                    band_info.comment = str(comment_lit)
                self.band_info.append(band_info)
        for b in self.band_info:
            print("\t\t{0}".format(str(b)))

        # Get spatialReference
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.spatialReference, None)):
            spat_ref_lit = self._rmeta_graph.value(o,
                                                   rdflib.namespace.RDF.value)
            if spat_ref_lit is None:
                msg = "Spatial reference value not found for {0}.".format(o)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.spatial_reference = RasterResourceMeta.SpatialReference(
                str(spat_ref_lit))
            print("\t\t{0}".format(self.spatial_reference))
예제 #7
0
    def _read_resource_metadata(self):
        super(NetcdfResourceMeta, self)._read_resource_metadata()

        print("--- NetcdfResourceMeta ---")

        # Also parse using SAX so that we can capture certain metadata elements
        # in the same order in which they appear in the RDF+XML serialization.
        SAX_parse_results = NetcdfResourceSAXHandler()
        xml.sax.parse(self.rmeta_path, SAX_parse_results)

        hsterms = rdflib.namespace.Namespace('https://www.hydroshare.org/terms/')

        # Get Variable
        if SAX_parse_results:
            # Use variables from SAX parser
            self.variables = list(SAX_parse_results.variables)
        else:
            for s, p, o in self._rmeta_graph.triples((None, hsterms.netcdfVariable, None)):
                var = NetcdfResourceMeta.Variable()
                # Get name
                name_lit = self._rmeta_graph.value(o, hsterms.name)
                if name_lit is None:
                    msg = "Name for Variable was not found for resource {0}".format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                var.name = str(name_lit)
                # Get shape
                shape_lit = self._rmeta_graph.value(o, hsterms.shape)
                if shape_lit is None:
                    msg = "Shape for Variable was not found for resource {0}".format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                var.shape = str(shape_lit)
                # Get type
                type_lit = self._rmeta_graph.value(o, hsterms.type)
                if type_lit is None:
                    msg = "Type for Variable was not found for resource {0}".format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                var.type = str(type_lit)
                # Get unit
                unit_lit = self._rmeta_graph.value(o, hsterms.unit)
                if unit_lit is None:
                    msg = "Unit for Variable was not found for resource {0}".format(self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                var.unit = str(unit_lit)
                # Get longName
                long_name_lit = self._rmeta_graph.value(o, hsterms.longName)
                if long_name_lit:
                    var.longName = str(long_name_lit)
                # Get comment
                comment_lit = self._rmeta_graph.value(o, hsterms.comment)
                if comment_lit:
                    var.comment = str(comment_lit)
                # Get missingValue
                missing_val_lit = self._rmeta_graph.value(o, hsterms.missingValue)
                if missing_val_lit:
                    var.missingValue = str(missing_val_lit)
                self.variables.append(var)
        for v in self.variables:
            print("\t\t{0}".format(str(v)))

        # Get spatialReference
        for s, p, o in self._rmeta_graph.triples((None, hsterms.spatialReference, None)):
            # Get extent
            extent_lit = self._rmeta_graph.value(o, hsterms.extent)
            if extent_lit is None:
                msg = "Extent not found in spatial reference for resource {0}".format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            extent = str(extent_lit)
            # Get crsName
            crs_name_lit = self._rmeta_graph.value(o, hsterms.crsName)
            crs_name = None
            if crs_name_lit is not None:
                crs_name = str(crs_name_lit)
            # Get crsRepresentationText
            crs_repr_text_lit = self._rmeta_graph.value(o, hsterms.crsRepresentationText)
            crs_repr_text = None
            if crs_repr_text_lit is not None:
                crs_repr_text = str(crs_repr_text_lit)
            # Get crsRepresentationType
            crs_repr_type_lit = self._rmeta_graph.value(o, hsterms.crsRepresentationType)
            crs_repr_type = None
            if crs_repr_type_lit is not None:
                crs_repr_type = str(crs_repr_type_lit)
            self.spatial_reference = NetcdfResourceMeta.SpatialReference(extent, crs_name, crs_repr_text,
                                                                         crs_repr_type)
            print("\t\t{0}".format(self.spatial_reference))
예제 #8
0
    def _read_resource_metadata(self):
        super(SWATModelInstanceResourceMeta, self)._read_resource_metadata()

        logger.debug("--- SWATModelInstanceResource ---")

        hsterms = rdflib.namespace.Namespace('http://hydroshare.org/terms/')

        # Get ModelOutput
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ModelOutput, None)):
            # Get IncludesModelOutput
            model_output_lit = self._rmeta_graph.value(
                o, hsterms.includesModelOutput)
            if model_output_lit is None:
                msg = "includesModelOutput for ModelOutput was not found for resource {0}"\
                    .format(self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.model_output = SWATModelInstanceResourceMeta.ModelOutput()
            self.model_output.includes_output = str(model_output_lit) == 'Yes'
            logger.debug("\t\t{0}".format(self.model_output))
        # Get ExecutedBy
        # Should be hsterms.ExecutedBy, BUT THERE IS A TYPO IN THE XML!
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ExecutedBY, None)):
            # Get modelProgramName
            executed_by_name_lit = self._rmeta_graph.value(
                o, hsterms.modelProgramName)
            if executed_by_name_lit is not None:
                self.executed_by_name = str(executed_by_name_lit)
            # Get modelProgramIdentifier
            executed_by_uri_lit = self._rmeta_graph.value(
                o, hsterms.modelProgramIdentifier)
            if executed_by_uri_lit is not None:
                self.executed_by_uri = str(executed_by_uri_lit)
            if (self.executed_by_name is not None) ^ (self.executed_by_uri
                                                      is not None):
                msg = "Both modelProgramName and modelProgramIdentifier must be supplied if one " \
                      "is supplied."
                raise GenericResourceMeta.ResourceMetaException(msg)
        logger.debug("\t\tmodelProgramName {0}".format(
            str(self.executed_by_name)))
        logger.debug("\t\tmodelProgramIdentifier {0}".format(
            str(self.executed_by_uri)))
        # Get modelObjective
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.modelObjective, None)):
            self.model_objective = SWATModelInstanceResourceMeta.ModelObjective(
                str(o))
            logger.debug("\t\t{0}".format(self.model_objective))
        # Get simulationType
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.simulationType, None)):
            self.simulation_type = SWATModelInstanceResourceMeta.SimulationType(
            )
            self.simulation_type.simulation_type_name = str(o)
            logger.debug("\t\t{0}".format(self.simulation_type))
        # Get ModelMethod
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ModelMethod, None)):
            self.model_method = SWATModelInstanceResourceMeta.ModelMethod()
            # Get runoffCalculationMethod
            runoff_method_lit = self._rmeta_graph.value(
                o, hsterms.runoffCalculationMethod)
            if runoff_method_lit is not None:
                self.model_method.runoff_calculation_method = str(
                    runoff_method_lit)
            # Get flowRoutingMethod
            flowrouting_method_lit = self._rmeta_graph.value(
                o, hsterms.flowRoutingMethod)
            if flowrouting_method_lit is not None:
                self.model_method.flow_routing_method = str(
                    flowrouting_method_lit)
            # Get petEstimationMethod
            pet_method_lit = self._rmeta_graph.value(
                o, hsterms.petEstimationMethod)
            if pet_method_lit is not None:
                self.model_method.PET_estimation_method = str(pet_method_lit)
            logger.debug("\t\t{0}".format(self.model_method))
        # Get modelParameter
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.modelParameter, None)):
            self.model_parameter = SWATModelInstanceResourceMeta.ModelParameter(
                str(o))
            logger.debug("\t\t{0}".format(self.model_parameter))
        # Get ModelInput
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.ModelInput, None)):
            self.model_input = SWATModelInstanceResourceMeta.ModelInput()
            # Get warm-upPeriodType
            warmup_period_type_lit = self._rmeta_graph.value(
                o, hsterms['warm-upPeriodType'])
            if warmup_period_type_lit is not None:
                self.model_input.warm_up_period_type = str(
                    warmup_period_type_lit)
            # Get warm-upPeriodValue
            warmup_period_value_lit = self._rmeta_graph.value(
                o, hsterms['warm-upPeriodValue'])
            if warmup_period_value_lit is not None:
                self.model_input.warm_up_period_value = str(
                    warmup_period_value_lit)
            # Get rainfallTimeStepType
            rainfall_time_step_type_lit = self._rmeta_graph.value(
                o, hsterms.rainfallTimeStepType)
            if rainfall_time_step_type_lit is not None:
                self.model_input.rainfall_time_step_type = str(
                    rainfall_time_step_type_lit)
            # Get rainfallTimeStepValue
            rainfall_time_step_value_lit = self._rmeta_graph.value(
                o, hsterms.rainfallTimeStepValue)
            if rainfall_time_step_value_lit is not None:
                self.model_input.rainfall_time_step_value = str(
                    rainfall_time_step_value_lit)
            # Get routingTimeStepType
            routing_time_step_type_lit = self._rmeta_graph.value(
                o, hsterms.routingTimeStepType)
            if routing_time_step_type_lit is not None:
                self.model_input.routing_time_step_type = str(
                    routing_time_step_type_lit)
            # Get routingTimeStepValue
            routing_time_step_value_lit = self._rmeta_graph.value(
                o, hsterms.routingTimeStepValue)
            if routing_time_step_value_lit is not None:
                self.model_input.routing_time_step_value = str(
                    routing_time_step_value_lit)
            # Get simulationTimeStepType
            simulation_time_step_type_lit = self._rmeta_graph.value(
                o, hsterms.simulationTimeStepType)
            if simulation_time_step_type_lit is not None:
                self.model_input.simulation_time_step_type = str(
                    simulation_time_step_type_lit)
            # Get simulationTimeStepValue
            simulation_time_step_value_lit = self._rmeta_graph.value(
                o, hsterms.simulationTimeStepValue)
            if simulation_time_step_value_lit is not None:
                self.model_input.simulation_time_step_value = str(
                    simulation_time_step_value_lit)
            # Get watershedArea
            watershed_area_lit = self._rmeta_graph.value(
                o, hsterms.watershedArea)
            if watershed_area_lit is not None:
                self.model_input.watershed_area = str(watershed_area_lit)
            # Get numberOfSubbasins
            number_of_subbasins_lit = self._rmeta_graph.value(
                o, hsterms.numberOfSubbasins)
            if number_of_subbasins_lit is not None:
                self.model_input.number_of_subbasins = str(
                    number_of_subbasins_lit)
            # Get numberOfHRUs
            number_of_HRUs_lit = self._rmeta_graph.value(
                o, hsterms.numberOfHRUs)
            if number_of_HRUs_lit is not None:
                self.model_input.number_of_HRUs = str(number_of_HRUs_lit)
            # Get demResolution
            DEM_resolution_lit = self._rmeta_graph.value(
                o, hsterms.demResolution)
            if DEM_resolution_lit is not None:
                self.model_input.DEM_resolution = str(DEM_resolution_lit)
            # Get demSourceName
            DEM_source_name_lit = self._rmeta_graph.value(
                o, hsterms.demSourceName)
            if DEM_source_name_lit is not None:
                self.model_input.DEM_source_name = str(DEM_source_name_lit)
            # Get demSourceURL
            DEM_source_URL_lit = self._rmeta_graph.value(
                o, hsterms.demSourceURL)
            if DEM_source_URL_lit is not None:
                self.model_input.DEM_source_URL = str(DEM_source_URL_lit)
            # Get landUseDataSourceName
            landUse_data_source_name_lit = self._rmeta_graph.value(
                o, hsterms.landUseDataSourceName)
            if landUse_data_source_name_lit is not None:
                self.model_input.landUse_data_source_name = str(
                    landUse_data_source_name_lit)
            # Get landUseDataSourceURL
            landUse_data_source_URL_lit = self._rmeta_graph.value(
                o, hsterms.landUseDataSourceURL)
            if landUse_data_source_URL_lit is not None:
                self.model_input.landUse_data_source_URL = str(
                    landUse_data_source_URL_lit)
            # Get soilDataSourceName
            soil_data_source_name_lit = self._rmeta_graph.value(
                o, hsterms.soilDataSourceName)
            if soil_data_source_name_lit is not None:
                self.model_input.soil_data_source_name = str(
                    soil_data_source_name_lit)
            # Get soilDataSourceURL
            soil_data_source_URL_lit = self._rmeta_graph.value(
                o, hsterms.soilDataSourceURL)
            if soil_data_source_URL_lit is not None:
                self.model_input.soil_data_source_URL = str(
                    soil_data_source_URL_lit)
            logger.debug("\t\t{0}".format(self.model_input))
예제 #9
0
    def write_metadata_to_resource(self, resource, **kwargs):
        """
        Write metadata to resource

        :param resource: RasterResource instance
        """
        super(SWATModelInstanceResourceMeta,
              self).write_metadata_to_resource(resource, **kwargs)

        if self.model_output is not None:
            if resource.metadata.model_output:
                resource.metadata.update_element(
                    'modeloutput',
                    resource.metadata.model_output.id,
                    includes_output=self.model_output.includes_output)
            else:
                resource.metadata.create_element(
                    'modeloutput',
                    includes_output=self.model_output.includes_output)
        if self.executed_by_uri is not None:
            if resource.metadata.executed_by:
                # Remove existing
                resource.metadata._executed_by.all().delete()
            if self.executed_by_uri != 'None':
                # Only create ExecutedBy if URI is a URL.
                uri_stripped = self.executed_by_uri.strip('/')
                short_id = os.path.basename(uri_stripped)
                if short_id == '':
                    msg = "ExecutedBy URL {0} does not contain a model program resource ID, "
                    msg += "for resource {1}"
                    msg = msg.format(self.executed_by_uri, self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                try:
                    resource.metadata.create_element('ExecutedBy',
                                                     model_name=short_id)
                except Exception:
                    msg = "Creation of ExecutedBy failed, resource {0} may not exist.".format(
                        short_id)
                    raise HsDeserializationDependencyException(short_id, msg)

        if self.model_objective:
            swat_model_objectives = []
            other_objectives = []
            for obj in self.model_objective.model_objectives:
                if obj in MODEL_OBJECTIVE_CHOICES:
                    swat_model_objectives.append(obj)
                else:
                    other_objectives.append(obj)
            other_objectives_str = None
            if len(other_objectives) > 0:
                other_objectives_str = ",".join(other_objectives)
            model_objective = resource.metadata.model_objective
            if not model_objective:
                ModelObjective.create(
                    content_object=resource.metadata,
                    swat_model_objectives=swat_model_objectives,
                    other_objectives=other_objectives_str)
            else:
                ModelObjective.update(
                    model_objective.id,
                    swat_model_objectives=swat_model_objectives,
                    other_objectives=other_objectives_str)
        if self.simulation_type:
            simulation_type = resource.metadata.simulation_type
            if not simulation_type:
                SimulationType.create(content_object=resource.metadata,
                                      simulation_type_name=self.
                                      simulation_type.simulation_type_name)
            else:
                SimulationType.update(simulation_type.id,
                                      simulation_type_name=self.
                                      simulation_type.simulation_type_name)
        if self.model_method:
            model_method = resource.metadata.model_method
            if not model_method:
                ModelMethod.create(
                    content_object=resource.metadata,
                    runoffCalculationMethod=self.model_method.
                    runoff_calculation_method,
                    flowRoutingMethod=self.model_method.flow_routing_method,
                    petEstimationMethod=self.model_method.PET_estimation_method
                )
            else:
                ModelMethod.update(
                    model_method.id,
                    runoffCalculationMethod=self.model_method.
                    runoff_calculation_method,
                    flowRoutingMethod=self.model_method.flow_routing_method,
                    petEstimationMethod=self.model_method.PET_estimation_method
                )
        if self.model_parameter:
            model_parameters = []
            other_parameters = []
            for param in self.model_parameter.model_parameters:
                if param in MODEL_PARAMETER_CHOICES:
                    model_parameters.append(param)
                else:
                    other_parameters.append(param)
            other_parameters_str = None
            if len(other_parameters) > 0:
                other_parameters_str = ",".join(other_parameters)
            model_parameter = resource.metadata.model_parameter
            if not model_parameter:
                ModelParameter.create(content_object=resource.metadata,
                                      model_parameters=model_parameters,
                                      other_parameters=other_parameters_str)
            else:
                ModelParameter.update(model_parameter.id,
                                      model_parameters=model_parameters,
                                      other_parameters=other_parameters_str)
        if self.model_input:
            model_input = resource.metadata.model_input
            if not model_input:
                ModelInput.create(
                    content_object=resource.metadata,
                    warmupPeriodValue=self.model_input.warm_up_period_value,
                    rainfallTimeStepType=self.model_input.
                    rainfall_time_step_type,
                    rainfallTimeStepValue=self.model_input.
                    rainfall_time_step_value,
                    routingTimeStepType=self.model_input.
                    routing_time_step_type,
                    routingTimeStepValue=self.model_input.
                    routing_time_step_value,
                    simulationTimeStepType=self.model_input.
                    simulation_time_step_type,
                    simulationTimeStepValue=self.model_input.
                    simulation_time_step_value,
                    watershedArea=self.model_input.watershed_area,
                    numberOfSubbasins=self.model_input.number_of_subbasins,
                    numberOfHRUs=self.model_input.number_of_HRUs,
                    demResolution=self.model_input.DEM_resolution,
                    demSourceName=self.model_input.DEM_source_name,
                    demSourceURL=self.model_input.DEM_source_URL,
                    landUseDataSourceName=self.model_input.
                    landUse_data_source_name,
                    landUseDataSourceURL=self.model_input.
                    landUse_data_source_URL,
                    soilDataSourceName=self.model_input.soil_data_source_name,
                    soilDataSourceURL=self.model_input.soil_data_source_URL)
            else:
                ModelInput.update(
                    model_input.id,
                    warmupPeriodValue=self.model_input.warm_up_period_value,
                    rainfallTimeStepType=self.model_input.
                    rainfall_time_step_type,
                    rainfallTimeStepValue=self.model_input.
                    rainfall_time_step_value,
                    routingTimeStepType=self.model_input.
                    routing_time_step_type,
                    routingTimeStepValue=self.model_input.
                    routing_time_step_value,
                    simulationTimeStepType=self.model_input.
                    simulation_time_step_type,
                    simulationTimeStepValue=self.model_input.
                    simulation_time_step_value,
                    watershedArea=self.model_input.watershed_area,
                    numberOfSubbasins=self.model_input.number_of_subbasins,
                    numberOfHRUs=self.model_input.number_of_HRUs,
                    demResolution=self.model_input.DEM_resolution,
                    demSourceName=self.model_input.DEM_source_name,
                    demSourceURL=self.model_input.DEM_source_URL,
                    landUseDataSourceName=self.model_input.
                    landUse_data_source_name,
                    landUseDataSourceURL=self.model_input.
                    landUse_data_source_URL,
                    soilDataSourceName=self.model_input.soil_data_source_name,
                    soilDataSourceURL=self.model_input.soil_data_source_URL)
예제 #10
0
    def _read_resource_metadata(self):
        super(TimeSeriesResourceMeta, self)._read_resource_metadata()

        print("--- TimeSeriesResourceMeta ---")

        hsterms = rdflib.namespace.Namespace('http://hydroshare.org/terms/')

        # Get site
        for s, p, o in self._rmeta_graph.triples((None, hsterms.site, None)):
            self.site = TimeSeriesResourceMeta.Site()
            # Get SiteCode
            site_code_lit = self._rmeta_graph.value(o, hsterms.SiteCode)
            if site_code_lit is None:
                msg = "SiteCode for site was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.site.siteCode = str(site_code_lit)
            # Get SiteName
            site_name_lit = self._rmeta_graph.value(o, hsterms.SiteName)
            if site_name_lit is None:
                msg = "SiteName for site was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.site.siteName = str(site_name_lit)
            # Get Elevation_m
            site_elevation_lit = self._rmeta_graph.value(
                o, hsterms.Elevation_m)
            if site_elevation_lit is not None:
                self.site.elevation_m = float(str(site_elevation_lit))
            # Get ElevationDatum
            site_elevation_datum_lit = self._rmeta_graph.value(
                o, hsterms.ElevationDatum)
            if site_elevation_datum_lit is not None:
                self.site.elevationDatum = str(site_elevation_datum_lit)
            # Get SiteType
            site_type_lit = self._rmeta_graph.value(o, hsterms.SiteType)
            if site_type_lit is not None:
                self.site.siteType = str(site_type_lit)
            print("\t\t{0}".format(self.site))

        # Get variable
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.variable, None)):
            self.variable = TimeSeriesResourceMeta.Variable()
            # Get VariableCode
            var_code_lit = self._rmeta_graph.value(o, hsterms.VariableCode)
            if var_code_lit is None:
                msg = "VariableCode for variable was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.variable.variableCode = str(var_code_lit)
            # Get VariableName
            var_name_lit = self._rmeta_graph.value(o, hsterms.VariableName)
            if var_name_lit is None:
                msg = "VariableName for variable was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.variable.variableName = str(var_name_lit)
            # Get VariableType
            var_type_lit = self._rmeta_graph.value(o, hsterms.VariableType)
            if var_type_lit is None:
                msg = "VariableType for variable was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.variable.variableType = str(var_type_lit)
            # Get NoDataValue
            var_nd_lit = self._rmeta_graph.value(o, hsterms.NoDataValue)
            if var_nd_lit is None:
                msg = "NoDataValue for variable was not found for resource {0}".format(
                    self.root_uri)
                raise GenericResourceMeta.ResourceMetaException(msg)
            self.variable.noDataValue = str(var_nd_lit)
            # Get VariableDefinition
            var_def_lit = self._rmeta_graph.value(o,
                                                  hsterms.VariableDefinition)
            if var_def_lit is not None:
                self.variable.variableDefinition = str(var_def_lit)
            # Get Speciation
            var_spec_lit = self._rmeta_graph.value(o, hsterms.Speciation)
            if var_spec_lit is not None:
                self.variable.speciation = str(var_spec_lit)
            print("\t\t{0}".format(self.variable))

            # Get method
            for s, p, o in self._rmeta_graph.triples(
                (None, hsterms.method, None)):
                self.method = TimeSeriesResourceMeta.Method()
                # Get MethodCode
                method_code_lit = self._rmeta_graph.value(
                    o, hsterms.MethodCode)
                if method_code_lit is None:
                    msg = "MethodCode for method was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.method.methodCode = str(method_code_lit)
                # Get MethodName
                method_name_lit = self._rmeta_graph.value(
                    o, hsterms.MethodName)
                if method_name_lit is None:
                    msg = "MethodName for method was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.method.methodName = str(method_name_lit)
                # Get MethodType
                method_type_lit = self._rmeta_graph.value(
                    o, hsterms.MethodType)
                if method_type_lit is None:
                    msg = "MethodType for method was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.method.methodType = str(method_type_lit)
                # Get MethodDescription
                method_desc_lit = self._rmeta_graph.value(
                    o, hsterms.MethodDescription)
                if method_desc_lit is not None:
                    self.method.methodDescription = str(method_desc_lit)
                # Get MethodLink
                method_link_lit = self._rmeta_graph.value(
                    o, hsterms.MethodLink)
                if method_link_lit is not None:
                    self.method.methodLink = str(method_link_lit)
                print("\t\t{0}".format(self.method))

            # Get processingLevel
            for s, p, o in self._rmeta_graph.triples(
                (None, hsterms.processingLevel, None)):
                self.processing_level = TimeSeriesResourceMeta.ProcessingLevel(
                )
                # Get ProcessingLevelCode
                proc_code_lit = self._rmeta_graph.value(
                    o, hsterms.ProcessingLevelCode)
                if proc_code_lit is None:
                    msg = "ProcessingLevelCode for ProcessingLevel was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.processing_level.processingLevelCode = str(proc_code_lit)
                # Get Definition
                method_def_lit = self._rmeta_graph.value(o, hsterms.Definition)
                if method_def_lit is not None:
                    self.processing_level.definition = str(method_def_lit)
                # Get Explanation
                method_explan_lit = self._rmeta_graph.value(
                    o, hsterms.Explanation)
                if method_explan_lit is not None:
                    self.processing_level.explanation = str(method_explan_lit)
                print("\t\t{0}".format(self.processing_level))

            # Get timeSeriesResult
            for s, p, o in self._rmeta_graph.triples(
                (None, hsterms.timeSeriesResult, None)):
                self.time_series_result = TimeSeriesResourceMeta.TimeSeriesResult(
                )
                # Get units
                for s1, p1, o1 in self._rmeta_graph.triples(
                    (o, hsterms.units, None)):
                    # Get UnitsType
                    unit_type_lit = self._rmeta_graph.value(
                        o1, hsterms.UnitsType)
                    if unit_type_lit is None:
                        msg = "UnitsType for TimeSeriesResult:units was not found for resource {0}".format(
                            self.root_uri)
                        raise GenericResourceMeta.ResourceMetaException(msg)
                    self.time_series_result.unitsType = str(unit_type_lit)
                    # Get UnitsName
                    unit_name_lit = self._rmeta_graph.value(
                        o1, hsterms.UnitsName)
                    if unit_name_lit is None:
                        msg = "UnitsName for TimeSeriesResult:units was not found for resource {0}".format(
                            self.root_uri)
                        raise GenericResourceMeta.ResourceMetaException(msg)
                    self.time_series_result.unitsName = str(unit_name_lit)
                    # Get UnitsAbbreviation
                    unit_abbrev_lit = self._rmeta_graph.value(
                        o1, hsterms.UnitsAbbreviation)
                    if unit_abbrev_lit is None:
                        msg = "UnitsAbbreviation for TimeSeriesResult:units was not found for resource {0}".format(
                            self.root_uri)
                        raise GenericResourceMeta.ResourceMetaException(msg)
                    self.time_series_result.unitsAbbreviation = str(
                        unit_abbrev_lit)
                # Get Status
                res_status_lit = self._rmeta_graph.value(o, hsterms.Status)
                if res_status_lit is None:
                    msg = "Status for TimeSeriesResult was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.time_series_result.status = str(res_status_lit)
                # Get SampleMedium
                res_sampmed_lit = self._rmeta_graph.value(
                    o, hsterms.SampleMedium)
                if res_sampmed_lit is None:
                    msg = "SampleMedium for TimeSeriesResult was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.time_series_result.sampleMedium = str(res_sampmed_lit)
                # Get ValueCount
                res_valcount_lit = self._rmeta_graph.value(
                    o, hsterms.ValueCount)
                if res_valcount_lit is None:
                    msg = "ValueCount for TimeSeriesResult was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.time_series_result.valueCount = int(str(res_valcount_lit))
                # Get AggregationStatistic
                res_aggstat_lit = self._rmeta_graph.value(
                    o, hsterms.AggregationStatistic)
                if res_aggstat_lit is None:
                    msg = "AggregationStatistic for TimeSeriesResult was not found for resource {0}".format(
                        self.root_uri)
                    raise GenericResourceMeta.ResourceMetaException(msg)
                self.time_series_result.aggregationStatistics = str(
                    res_aggstat_lit)
                print("\t\t{0}".format(self.time_series_result))
예제 #11
0
    def _read_resource_metadata(self):
        super(ModelProgramResourceMeta, self)._read_resource_metadata()

        print("--- ModelProgramResource ---")

        hsterms = rdflib.namespace.Namespace('http://hydroshare.org/terms/')

        # Get Model program metadata (MpMetadata)
        for s, p, o in self._rmeta_graph.triples(
            (None, hsterms.MpMetadata, None)):
            # Get software_version
            software_version_lit = self._rmeta_graph.value(
                o, hsterms.software_version)
            if software_version_lit is not None:
                self.software_version = str(software_version_lit).strip()
                if self.software_version == '':
                    self.software_version = None
            # Get modelProgramLanguage
            modelProgramLanguage_lit = self._rmeta_graph.value(
                o, hsterms.modelProgramLanguage)
            if modelProgramLanguage_lit is not None:
                self.modelProgramLanguage = str(
                    modelProgramLanguage_lit).strip()
                if self.modelProgramLanguage == '':
                    self.modelProgramLanguage = None
            # Get modelOperatingSystem
            modelOperatingSystem_lit = self._rmeta_graph.value(
                o, hsterms.modelOperatingSystem)
            if modelOperatingSystem_lit is not None:
                self.modelOperatingSystem = str(
                    modelOperatingSystem_lit).strip()
                if self.modelOperatingSystem == '':
                    self.modelOperatingSystem = None
            # Get modelReleaseDate
            modelReleaseDate_lit = self._rmeta_graph.value(
                o, hsterms.modelReleaseDate)
            if modelReleaseDate_lit is not None:
                modelReleaseDate_str = str(modelReleaseDate_lit).strip()
                if modelReleaseDate_str != '':
                    try:
                        self.modelReleaseDate = hs_date_to_datetime(
                            modelReleaseDate_str)
                    except HsDateException:
                        try:
                            self.modelReleaseDate = hs_date_to_datetime_iso(
                                modelReleaseDate_str)
                        except HsDateException as e:
                            msg = "Unable to parse date {0}, error: {1}".format(
                                str(modelReleaseDate_lit), str(e))
                            raise GenericResourceMeta.ResourceMetaException(
                                msg)
            # Get modelWebsite
            modelWebsite_lit = self._rmeta_graph.value(o, hsterms.modelWebsite)
            if modelWebsite_lit is not None:
                self.modelWebsite = str(modelWebsite_lit).strip()
                if self.modelWebsite == '':
                    self.modelWebsite = None
            # Get modelReleaseNotes
            modelReleaseNotes_lit = self._rmeta_graph.value(
                o, hsterms.modelReleaseNotes)
            if modelReleaseNotes_lit is not None:
                self.modelReleaseNotes = str(modelReleaseNotes_lit).strip()
                if self.modelReleaseNotes == '':
                    self.modelReleaseNotes = None
            # Get modelDocumentation
            modelDocumentation_lit = self._rmeta_graph.value(
                o, hsterms.modelDocumentation)
            if modelDocumentation_lit is not None:
                self.modelDocumentation = str(modelDocumentation_lit).strip()
                if self.modelDocumentation == '':
                    self.modelDocumentation = None
            # Get modelSoftware
            modelSoftware_lit = self._rmeta_graph.value(
                o, hsterms.modelSoftware)
            if modelSoftware_lit is not None:
                self.modelSoftware = str(modelSoftware_lit).strip()
                if self.modelSoftware == '':
                    self.modelSoftware = None
            print("\t\t{0}".format(str(self)))