Example #1
0
    def create_series_element(self, seriesResult):
        series = WaterML.series()

        # Variable
        variable = self.create_variable_element(seriesResult.Variable)
        series.set_variable(variable)

        series.valueCount = WaterML.valueCount(
            valueOf_=str(seriesResult.ValueCount))

        # in WML1_0, these are strings.
        beginDateTime = core._get_datavalues_datetime(
            seriesResult, "BeginDateTime", "BeginDateTimeUTC").isoformat()
        endDateTime = core._get_datavalues_datetime(
            seriesResult, "EndDateTime", "EndDateTimeUTC").isoformat()

        # TimeInterval
        if beginDateTime is None:
            beginDateTime = datetime.datetime.now().isoformat()
        if endDateTime is None:
            endDateTime = datetime.datetime.now().isoformat()
        variableTimeInt = WaterML.TimeIntervalType(beginDateTime=beginDateTime,
                                                   endDateTime=endDateTime)
        series.variableTimeInterval = variableTimeInt

        # Method
        if seriesResult.Method:
            method = self.create_method_element(seriesResult.Method)
            series.Method = method

        # Source
        if seriesResult.Source:
            source = self.create_source_element(seriesResult.Source)
            series.Source = source

        # QualityControlLevel
        qualityControlLevel = WaterML.QualityControlLevelType(
            qualityControlLevelID=seriesResult.QualityControlLevelID,
            valueOf_=seriesResult.QualityControlLevelCode)

        series.QualityControlLevel = qualityControlLevel

        return series
Example #2
0
    def create_get_values_response(self,
                                   siteArg,
                                   varArg,
                                   startDateTime=None,
                                   endDateTime=None):

        # TODO: Tim thinks the DAO should handle network and vocab parsing,
        #       not WOF

        siteCode = self.get_site_code(siteArg)
        varCode = self.get_variable_code(varArg)

        valueResultArr = self.dao.get_datavalues(siteCode, varCode,
                                                 startDateTime, endDateTime)

        timeSeriesResponse = WaterML.TimeSeriesResponseType()

        queryInfo = WaterML.QueryInfoType()
        timeParam = WaterML.timeParam(beginDateTime=startDateTime,
                                      endDateTime=endDateTime)
        criteria = WaterML.criteria(locationParam=siteArg,
                                    variableParam=varArg,
                                    timeParam=timeParam)
        queryInfo.set_criteria(criteria)
        queryInfoNote = WaterML.NoteType()
        queryInfo.add_note(queryInfoNote)
        queryInfo.set_extension('')
        timeSeriesResponse.set_queryInfo(queryInfo)

        # if not valueResultArr:
        #     timeSeries = WaterML.TimeSeriesType()
        #     timeSeriesResponse.set_timeSeries(timeSeries)
        #     return timeSeriesResponse

        if not valueResultArr:
            raise Exception("Values Not Found for %s:%s for dates %s - %s" %
                            (siteCode, varCode, startDateTime, endDateTime))

        if isinstance(valueResultArr, dict):
            for key in valueResultArr.keys():
                valueResultArr = valueResultArr[key]
                break

        timeSeries = WaterML.TimeSeriesType()

        # sourceInfo (which is a siteInfo) element
        siteResult = self.dao.get_site_by_code(siteCode)

        # TODO: Exception?
        if not siteResult:
            pass

        sourceInfo = self.create_site_info_element(siteResult)
        timeSeries.sourceInfo = sourceInfo

        # variable element
        varResult = self.dao.get_variable_by_code(varCode)

        # TODO: Exception?
        if not varResult:
            pass

        variable = self.create_variable_element(varResult)
        timeSeries.variable = variable

        # TODO: fill in some more of the attributes in this element.
        values = WaterML.TsValuesSingleVariableType()

        values.count = len(valueResultArr)

        if varResult.VariableUnits:
            values.unitsAbbreviation = varResult.VariableUnits.UnitsAbbreviation  # noqa
            values.unitsCode = varResult.VariableUnits.UnitsID

        # Need to keep track of unique methodIDs and sourceIDs.
        methodIdSet = set()
        sourceIdSet = set()
        qualifierIdSet = set()
        offsetTypeIdSet = set()
        qualitycontrollevelIdSet = set()

        for valueResult in valueResultArr:
            if valueResult.QualityControlLevelID is not None:
                qualitycontrollevelIdSet.add(
                    valueResult.QualityControlLevelID)  # noqa
                qlevelResult = self.dao.get_qualcontrollvl_by_id(
                    valueResult.QualityControlLevelID)  # noqa
                if hasattr(qlevelResult, 'Definition'):
                    valueResult.QualityControlLevel = qlevelResult.Definition
                # else:
                #     if hasattr(valueResult,'QualityControlLevel'):
                #         valueResult.QualityControlLevel = qlevelResult.QualityControlLevelCode  # noqa
            v = self.create_value_element(valueResult)
            values.add_value(v)

            if valueResult.MethodID is not None:
                methodIdSet.add(valueResult.MethodID)

            if valueResult.SourceID is not None:
                sourceIdSet.add(valueResult.SourceID)

            if valueResult.QualifierID is not None:
                qualifierIdSet.add(valueResult.QualifierID)

            if valueResult.OffsetTypeID is not None:
                offsetTypeIdSet.add(valueResult.OffsetTypeID)

            if valueResult.QualityControlLevelID is not None:
                qualitycontrollevelIdSet.add(valueResult.QualityControlLevelID)

        # Add method elements for each unique methodID
        if methodIdSet:
            methodIdArr = list(methodIdSet)
            methodResultArr = self.dao.get_methods_by_ids(methodIdArr)
            for methodResult in methodResultArr:
                method = self.create_method_element(methodResult)
                values.add_method(method)

        # Add source elements for each unique sourceID
        if sourceIdSet:
            sourceIdArr = list(sourceIdSet)
            sourceResultArr = self.dao.get_sources_by_ids(sourceIdArr)
            for sourceResult in sourceResultArr:
                source = self.create_source_element(sourceResult)
                values.add_source(source)

        # Add qualifier elements
        if qualifierIdSet:
            qualIdArr = list(qualifierIdSet)
            qualResultArr = self.dao.get_qualifiers_by_ids(qualIdArr)
            for qualifierResult in qualResultArr:
                q = WaterML.qualifier(
                    qualifierID=qualifierResult.QualifierID,
                    default=None,
                    network=self.network,
                    vocabulary=self.vocabulary,
                    qualifierCode=qualifierResult.QualifierCode)
                values.add_qualifier(q)

        # Add offset elements
        if offsetTypeIdSet:
            offsetTypeIdArr = list(offsetTypeIdSet)
            offsetTypeResultArr = self.dao.get_offsettypes_by_ids(
                offsetTypeIdArr)
            for offsetTypeResult in offsetTypeResultArr:
                offset = self.create_offset_element(offsetTypeResult)
                values.add_offset(offset)

        # Add qualitycontrollevel elements
        if qualitycontrollevelIdSet:
            qlevelIdIdArr = list(qualitycontrollevelIdSet)

            try:
                qlevelResultArr = self.dao.get_qualcontrollvls_by_ids(
                    qlevelIdIdArr)  # noqa
                for qlevelResult in qlevelResultArr:
                    qlevel = self.create_qlevel_element(qlevelResult)
                    values.add_qualityControlLevel(qlevel)
            except:
                logging.warn(
                    "WofPy: DOA has no get_qualcontrollvls_by_ids method (added for 2.x)"
                )  # noqa
                for qlevelID in qlevelIdIdArr:
                    qlevel = WaterML.QualityControlLevelType(
                        qualityControlLevelID=qlevelID)
                    values.add_qualityControlLevel(qlevel)

        timeSeries.values = values
        timeSeriesResponse.set_timeSeries(timeSeries)
        return timeSeriesResponse