python-awips/awips/dataaccess/DataAccessLayer.py

218 lines
7.1 KiB
Python
Raw Permalink Normal View History

2015-06-12 11:57:06 -06:00
# #
# #
#
2016-03-11 15:05:01 -07:00
# Published interface for awips.dataaccess package
2016-03-16 16:32:17 -05:00
#
#
2015-06-12 11:57:06 -06:00
# SOFTWARE HISTORY
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 12/10/12 njensen Initial Creation.
# Feb 14, 2013 1614 bsteffen refactor data access framework
# to use single request.
2017-01-06 12:00:14 -07:00
# 04/10/13 1871 mnash move getLatLonCoords to JGridData and add default args
# 05/29/13 2023 dgilling Hook up ThriftClientRouter.
# 03/03/14 2673 bsteffen Add ability to query only ref times.
# 07/22/14 3185 njensen Added optional/default args to newDataRequest
# 07/30/14 3185 njensen Renamed valid identifiers to optional
# Apr 26, 2015 4259 njensen Updated for new JEP API
# Apr 13, 2016 5379 tgurney Add getIdentifierValues()
# Jun 01, 2016 5587 tgurney Add new signatures for
# getRequiredIdentifiers() and
# getOptionalIdentifiers()
# 10/07/16 ---- mjames@ucar Added getForecastRun
2016-03-16 16:32:17 -05:00
#
#
2015-06-12 11:57:06 -06:00
#
import sys
import subprocess
2017-01-06 12:00:14 -07:00
import warnings
2015-06-12 11:57:06 -06:00
THRIFT_HOST = "edex"
2015-06-12 11:57:06 -06:00
USING_NATIVE_THRIFT = False
if 'jep' in sys.modules:
2015-06-12 11:57:06 -06:00
# intentionally do not catch if this fails to import, we want it to
# be obvious that something is configured wrong when running from within
# Java instead of allowing false confidence and fallback behavior
import JepRouter
router = JepRouter
else:
2016-03-11 15:05:01 -07:00
from awips.dataaccess import ThriftClientRouter
2015-06-12 11:57:06 -06:00
router = ThriftClientRouter.ThriftClientRouter(THRIFT_HOST)
USING_NATIVE_THRIFT = True
2016-03-16 16:32:17 -05:00
def getForecastRun(cycle, times):
"""
:param cycle: Forecast cycle reference time
:param times: All available times/cycles
:return: DataTime array for a single forecast run
"""
fcstRun = []
for t in times:
if str(t)[:19] == str(cycle):
fcstRun.append(t)
return fcstRun
2015-06-12 11:57:06 -06:00
def getAvailableTimes(request, refTimeOnly=False):
"""
Get the times of available data to the request.
2016-03-16 16:32:17 -05:00
:param request: the IDataRequest to get data for
:param refTimeOnly: optional, use True if only unique refTimes should be returned (without a forecastHr)
2016-03-16 16:32:17 -05:00
:returns: a list of DataTimes
2015-06-12 11:57:06 -06:00
"""
return router.getAvailableTimes(request, refTimeOnly)
2017-01-06 12:00:14 -07:00
2015-06-12 11:57:06 -06:00
def getGridData(request, times=[]):
"""
Gets the grid data that matches the request at the specified times. Each
combination of parameter, level, and dataTime will be returned as a
separate IGridData.
2016-03-16 16:32:17 -05:00
:param request: the IDataRequest to get data for
:param times: a list of DataTimes, a TimeRange, or None if the data is time agnostic
2016-03-16 16:32:17 -05:00
:returns: a list of IGridData
2015-06-12 11:57:06 -06:00
"""
return router.getGridData(request, times)
2017-01-06 12:00:14 -07:00
2015-06-12 11:57:06 -06:00
def getGeometryData(request, times=[]):
"""
Gets the geometry data that matches the request at the specified times.
2016-03-16 16:32:17 -05:00
Each combination of geometry, level, and dataTime will be returned as a
2015-06-12 11:57:06 -06:00
separate IGeometryData.
2016-03-16 16:32:17 -05:00
:param request: the IDataRequest to get data for
:param times: a list of DataTimes, a TimeRange, or None if the data is time agnostic
2016-03-16 16:32:17 -05:00
:returns: a list of IGeometryData
2015-06-12 11:57:06 -06:00
"""
return router.getGeometryData(request, times)
2017-01-06 12:00:14 -07:00
2015-06-12 11:57:06 -06:00
def getAvailableLocationNames(request):
"""
Gets the available location names that match the request without actually
requesting the data.
2016-03-16 16:32:17 -05:00
:param request: the request to find matching location names for
2016-03-16 16:32:17 -05:00
:returns: a list of strings of available location names.
2015-06-12 11:57:06 -06:00
"""
return router.getAvailableLocationNames(request)
2017-01-06 12:00:14 -07:00
2015-06-12 11:57:06 -06:00
def getAvailableParameters(request):
"""
Gets the available parameters names that match the request without actually
requesting the data.
2016-03-16 16:32:17 -05:00
:param request: the request to find matching parameter names for
2016-03-16 16:32:17 -05:00
:returns: a list of strings of available parameter names.
2015-06-12 11:57:06 -06:00
"""
return router.getAvailableParameters(request)
2017-01-06 12:00:14 -07:00
2015-06-12 11:57:06 -06:00
def getAvailableLevels(request):
"""
Gets the available levels that match the request without actually
requesting the data.
2016-03-16 16:32:17 -05:00
:param request: the request to find matching levels for
2016-03-16 16:32:17 -05:00
:returns: a list of strings of available levels.
2015-06-12 11:57:06 -06:00
"""
return router.getAvailableLevels(request)
2017-01-06 12:00:14 -07:00
def getRequiredIdentifiers(request):
2015-06-12 11:57:06 -06:00
"""
2017-01-06 12:00:14 -07:00
Gets the required identifiers for this request. These identifiers
2015-06-12 11:57:06 -06:00
must be set on a request for the request of this datatype to succeed.
2016-03-16 16:32:17 -05:00
2017-02-01 21:09:01 -07:00
:param request: the request to find required identifiers
2016-03-16 16:32:17 -05:00
:returns: a list of strings of required identifiers
2015-06-12 11:57:06 -06:00
"""
2017-01-06 12:00:14 -07:00
if str(request) == request:
warnings.warn("Use getRequiredIdentifiers(IDataRequest) instead",
DeprecationWarning)
return router.getRequiredIdentifiers(request)
2015-06-12 11:57:06 -06:00
2017-01-06 12:00:14 -07:00
def getOptionalIdentifiers(request):
2015-06-12 11:57:06 -06:00
"""
2017-01-06 12:00:14 -07:00
Gets the optional identifiers for this request.
2016-03-16 16:32:17 -05:00
2017-02-01 21:09:01 -07:00
:param request: the reuqest to find optional identifiers for
2016-03-16 16:32:17 -05:00
:returns: a list of strings of optional identifiers
2015-06-12 11:57:06 -06:00
"""
2017-01-06 12:00:14 -07:00
if str(request) == request:
warnings.warn("Use getOptionalIdentifiers(IDataRequest) instead",
DeprecationWarning)
return router.getOptionalIdentifiers(request)
def getIdentifierValues(request, identifierKey):
"""
Gets the allowed values for a particular identifier on this datatype.
2017-02-01 21:09:01 -07:00
:param request: the request to find identifier values for
:praram identifierKey: the identifier to find values for
2017-01-06 12:00:14 -07:00
2017-02-01 21:09:01 -07:00
:returns: a list of strings of allowed values for the specified identifier
2017-01-06 12:00:14 -07:00
"""
return router.getIdentifierValues(request, identifierKey)
2015-06-12 11:57:06 -06:00
def newDataRequest(datatype=None, **kwargs):
"""
2015-06-12 11:57:06 -06:00
Creates a new instance of IDataRequest suitable for the runtime environment.
All args are optional and exist solely for convenience.
2016-03-16 16:32:17 -05:00
:param datatype: the datatype to create a request for
:param parameters: a list of parameters to set on the request
:param levels: a list of levels to set on the request
:param locationNames: a list of locationNames to set on the request
:param envelope: an envelope to limit the request
:param kwargs: any leftover kwargs will be set as identifiers
2016-03-16 16:32:17 -05:00
:returns: a new IDataRequest
2016-03-16 16:32:17 -05:00
"""
2015-06-12 11:57:06 -06:00
return router.newDataRequest(datatype, **kwargs)
def getSupportedDatatypes():
"""
Gets the datatypes that are supported by the framework
2016-03-16 16:32:17 -05:00
:returns: a list of strings of supported datatypes
2015-06-12 11:57:06 -06:00
"""
return router.getSupportedDatatypes()
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def changeEDEXHost(newHostName):
"""
Changes the EDEX host the Data Access Framework is communicating with. Only
works if using the native Python client implementation, otherwise, this
method will throw a TypeError.
2016-03-16 16:32:17 -05:00
:param newHostHame: the EDEX host to connect to
2015-06-12 11:57:06 -06:00
"""
if USING_NATIVE_THRIFT:
global THRIFT_HOST
THRIFT_HOST = newHostName
global router
router = ThriftClientRouter.ThriftClientRouter(THRIFT_HOST)
else:
raise TypeError("Cannot call changeEDEXHost when using JepRouter.")