def create_get_site_response(self, siteArg=None): if siteArg is None or siteArg == '': siteResultArr = self.dao.get_all_sites() else: siteCodesArr = siteArg.split(',') siteCodesArr = [self.get_site_code(s) for s in siteCodesArr] siteResultArr = self.dao.get_sites_by_codes(siteCodesArr) # if len(siteResultArr) == 0: # return None siteInfoResponse = WaterML.SiteInfoResponseType() queryInfo = WaterML.QueryInfoType() # TODO: check on how this should be done for multiple sites. criteria = WaterML.criteria(locationParam=siteArg) queryInfo.set_criteria(criteria) queryInfoNote = WaterML.NoteType() queryInfo.add_note(queryInfoNote) queryInfo.set_extension('') siteInfoResponse.set_queryInfo(queryInfo) if siteResultArr: for siteResult in siteResultArr: s = self.create_site_element(siteResult) siteInfoResponse.add_site(s) else: # site = WaterML.site() # siteInfoResponse.add_site(site) raise Exception("Site,'%s', Not Found" % siteArg) return siteInfoResponse
def create_site_element(self, siteResult, seriesResultArr=None): site = WaterML.site() siteInfo = self.create_site_info_element(siteResult) site.set_siteInfo(siteInfo) # need at least one note element to meet WaterML 1.0 schema validation if (not siteResult.County and not siteResult.State and not siteResult.Comments): siteInfo.add_note(WaterML.NoteType()) else: if siteResult.County: countyNote = WaterML.NoteType(title="County", valueOf_=siteResult.County) siteInfo.add_note(countyNote) if siteResult.State: stateNote = WaterML.NoteType(title="State", valueOf_=siteResult.State) siteInfo.add_note(stateNote) if siteResult.Comments: commentsNote = WaterML.NoteType(title="Site Comments", valueOf_=escape( siteResult.Comments)) siteInfo.add_note(commentsNote) seriesCatalog = WaterML.seriesCatalogType() if seriesResultArr is not None: seriesCatalog.menuGroupName = self.menu_group_name # TODO: Make sure this is set properly in config filename. seriesCatalog.serviceWsdl = self.service_wsdl for seriesResult in seriesResultArr: series = self.create_series_element(seriesResult) seriesCatalog.add_series(series) site.add_seriesCatalog(seriesCatalog) # need at least one extension element to meet WaterML 1.0 # schema validation site.set_extension('') return site
def create_get_site_info_response(self, siteArg, varArg=None): if siteArg is None: raise Exception("Site Not Found") siteCode = self.get_site_code(siteArg) siteResult = self.dao.get_site_by_code(siteCode) if (varArg is None or varArg == ''): seriesResultArr = self.dao.get_series_by_sitecode(siteCode) else: varCode = self.get_variable_code(varArg) seriesResultArr = self.dao.get_series_by_sitecode_and_varcode( siteCode, varCode) if seriesResultArr is None or len(seriesResultArr) == 0: raise Exception("Site,'%s', Not Found" % siteArg) siteInfoResponse = WaterML.SiteInfoResponseType() queryInfo = WaterML.QueryInfoType() criteria = WaterML.criteria(locationParam=siteArg, variableParam=varArg) queryInfo.set_criteria(criteria) queryInfoNote = WaterML.NoteType() queryInfo.add_note(queryInfoNote) queryInfo.set_extension('') siteInfoResponse.set_queryInfo(queryInfo) if siteResult: s = self.create_site_element(siteResult, seriesResultArr) siteInfoResponse.add_site(s) else: # site = WaterML.site() # siteInfoResponse.add_site(site) raise Exception("Site,'%s', Not Found" % siteArg) return siteInfoResponse
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