python-awips/awips/dataaccess/ThriftClientRouter.py

197 lines
8.5 KiB
Python
Raw Normal View History

2015-06-12 11:57:06 -06:00
# #
# This software was developed and / or modified by Raytheon Company,
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# U.S. EXPORT CONTROLLED TECHNICAL DATA
# This software product contains export-restricted data whose
# export/transfer/disclosure is restricted by U.S. law. Dissemination
# to non-U.S. persons whether in the United States or abroad requires
# an export license or other authorization.
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# Contractor Name: Raytheon Company
# Contractor Address: 6825 Pine Street, Suite 340
# Mail Stop B8
# Omaha, NE 68106
# 402.291.0100
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
# further licensing information.
# #
#
# Routes requests to the Data Access Framework through Python Thrift.
#
2016-03-16 16:32:17 -05:00
#
#
2017-01-06 12:00:14 -07:00
# SOFTWARE HISTORY
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
2017-01-06 12:00:14 -07:00
# 05/21/13 2023 dgilling Initial Creation.
# 01/06/14 2537 bsteffen Share geometry WKT.
# 03/03/14 2673 bsteffen Add ability to query only ref times.
# 07/22/14 3185 njensen Added optional/default args to newDataRequest
# 07/23/14 3185 njensen Added new methods
# 07/30/14 3185 njensen Renamed valid identifiers to optional
# 06/30/15 4569 nabowle Use hex WKB for geometries.
# 04/13/15 5379 tgurney Add getIdentifierValues()
# 06/01/16 5587 tgurney Add new signatures for
# getRequiredIdentifiers() and
# getOptionalIdentifiers()
# 11/10/16 5900 bsteffen Correct grid shape
2015-06-12 11:57:06 -06:00
#
import numpy
import shapely.wkb
2015-06-12 11:57:06 -06:00
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.impl import DefaultDataRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetAvailableLocationNamesRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetAvailableTimesRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetGeometryDataRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetGridDataRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetAvailableParametersRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetAvailableLevelsRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetRequiredIdentifiersRequest
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetOptionalIdentifiersRequest
2017-01-06 12:00:14 -07:00
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetIdentifierValuesRequest
2015-06-12 11:57:06 -06:00
from dynamicserialize.dstypes.com.raytheon.uf.common.dataaccess.request import GetSupportedDatatypesRequest
2016-03-16 10:28:37 -05:00
from awips import ThriftClient
2016-03-11 15:05:01 -07:00
from awips.dataaccess import PyGeometryData
from awips.dataaccess import PyGridData
2015-06-12 11:57:06 -06:00
class ThriftClientRouter(object):
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def __init__(self, host='localhost'):
self._client = ThriftClient.ThriftClient(host)
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def getAvailableTimes(self, request, refTimeOnly):
timesRequest = GetAvailableTimesRequest()
timesRequest.setRequestParameters(request)
timesRequest.setRefTimeOnly(refTimeOnly)
response = self._client.sendRequest(timesRequest)
return response
2016-03-16 16:32:17 -05:00
def getGridData(self, request, times):
2015-06-12 11:57:06 -06:00
gridDataRequest = GetGridDataRequest()
gridDataRequest.setRequestParameters(request)
# if we have an iterable times instance, then the user must have asked
# for grid data with the List of DataTime objects
# else, we assume it was a single TimeRange that was meant for the
# request
try:
iter(times)
gridDataRequest.setRequestedTimes(times)
except TypeError:
gridDataRequest.setRequestedPeriod(times)
response = self._client.sendRequest(gridDataRequest)
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
locSpecificData = {}
2016-04-16 17:00:50 -06:00
locNames = list(response.getSiteNxValues().keys())
2015-06-12 11:57:06 -06:00
for location in locNames:
nx = response.getSiteNxValues()[location]
ny = response.getSiteNyValues()[location]
2017-01-06 12:00:14 -07:00
latData = numpy.reshape(numpy.array(response.getSiteLatGrids()[location]), (ny, nx))
lonData = numpy.reshape(numpy.array(response.getSiteLonGrids()[location]), (ny, nx))
2015-06-12 11:57:06 -06:00
locSpecificData[location] = (nx, ny, (lonData, latData))
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
retVal = []
for gridDataRecord in response.getGridData():
locationName = gridDataRecord.getLocationName()
locData = locSpecificData[locationName]
retVal.append(PyGridData.PyGridData(gridDataRecord, locData[0], locData[1], locData[2]))
return retVal
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def getGeometryData(self, request, times):
geoDataRequest = GetGeometryDataRequest()
geoDataRequest.setRequestParameters(request)
# if we have an iterable times instance, then the user must have asked
# for geometry data with the List of DataTime objects
# else, we assume it was a single TimeRange that was meant for the
# request
try:
iter(times)
geoDataRequest.setRequestedTimes(times)
except TypeError:
geoDataRequest.setRequestedPeriod(times)
response = self._client.sendRequest(geoDataRequest)
geometries = []
for wkb in response.getGeometryWKBs():
# convert the wkb to a bytearray with only positive values
2016-04-16 17:00:50 -06:00
byteArrWKB = bytearray([x % 256 for x in wkb.tolist()])
# convert the bytearray to a byte string and load it.
geometries.append(shapely.wkb.loads(bytes(byteArrWKB)))
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
retVal = []
for geoDataRecord in response.getGeoData():
geom = geometries[geoDataRecord.getGeometryWKBindex()]
2015-06-12 11:57:06 -06:00
retVal.append(PyGeometryData.PyGeometryData(geoDataRecord, geom))
return retVal
def getAvailableLocationNames(self, request):
locNamesRequest = GetAvailableLocationNamesRequest()
locNamesRequest.setRequestParameters(request)
response = self._client.sendRequest(locNamesRequest)
return response
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def getAvailableParameters(self, request):
paramReq = GetAvailableParametersRequest()
paramReq.setRequestParameters(request)
response = self._client.sendRequest(paramReq)
return response
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def getAvailableLevels(self, request):
levelReq = GetAvailableLevelsRequest()
levelReq.setRequestParameters(request)
response = self._client.sendRequest(levelReq)
return response
2016-03-16 16:32:17 -05:00
2017-01-06 12:00:14 -07:00
def getRequiredIdentifiers(self, request):
if str(request) == request:
# Handle old version getRequiredIdentifiers(str)
request = self.newDataRequest(request)
2015-06-12 11:57:06 -06:00
idReq = GetRequiredIdentifiersRequest()
2017-01-06 12:00:14 -07:00
idReq.setRequest(request)
2015-06-12 11:57:06 -06:00
response = self._client.sendRequest(idReq)
return response
2016-03-16 16:32:17 -05:00
2017-01-06 12:00:14 -07:00
def getOptionalIdentifiers(self, request):
if str(request) == request:
# Handle old version getOptionalIdentifiers(str)
request = self.newDataRequest(request)
2015-06-12 11:57:06 -06:00
idReq = GetOptionalIdentifiersRequest()
2017-01-06 12:00:14 -07:00
idReq.setRequest(request)
2015-06-12 11:57:06 -06:00
response = self._client.sendRequest(idReq)
2016-03-16 16:32:17 -05:00
return response
2015-06-12 11:57:06 -06:00
2017-01-06 12:00:14 -07:00
def getIdentifierValues(self, request, identifierKey):
idValReq = GetIdentifierValuesRequest()
idValReq.setIdentifierKey(identifierKey)
idValReq.setRequestParameters(request)
response = self._client.sendRequest(idValReq)
return response
2015-06-12 11:57:06 -06:00
def newDataRequest(self, datatype, parameters=[], levels=[], locationNames = [], envelope=None, **kwargs):
req = DefaultDataRequest()
if datatype:
req.setDatatype(datatype)
2016-03-16 16:32:17 -05:00
if parameters:
2015-06-12 11:57:06 -06:00
req.setParameters(*parameters)
2016-03-16 16:32:17 -05:00
if levels:
2015-06-12 11:57:06 -06:00
req.setLevels(*levels)
if locationNames:
req.setLocationNames(*locationNames)
if envelope:
2016-03-16 16:32:17 -05:00
req.setEnvelope(envelope)
2015-06-12 11:57:06 -06:00
if kwargs:
# any args leftover are assumed to be identifiers
2016-03-16 16:32:17 -05:00
req.identifiers = kwargs
2015-06-12 11:57:06 -06:00
return req
2016-03-16 16:32:17 -05:00
def getSupportedDatatypes(self):
2015-06-12 11:57:06 -06:00
response = self._client.sendRequest(GetSupportedDatatypesRequest())
return response