Start by importing the DataAccessLayer package from python-awips:
from awips.dataaccess import DataAccessLayer
# Unidata's cloud EDEX instance is used in this example
edex_url = "edex-cloud.unidata.ucar.edu"
DataAccessLayer.changeEDEXHost(edex_url)
*DataAccessLayer.getSupportedDatatypes()* returns a list of supported data types offered by the EDEX server defined above. The code below shows how to populate, sort, and print out that list.
dataTypes = DataAccessLayer.getSupportedDatatypes()
dataTypes.sort()
list(dataTypes)
Now create a new data request using *DataAccessLayer.newDataRequest()*, and set the data type using *request.setDatatype()*. Below we create a few different requests with different data types to show some differences with other methods.
For this example we are going to look at the "grid" data type, which is the model data can be found, along with some other datasets (such as MRMS).
# Create a request for data type grid
grid_request = DataAccessLayer.newDataRequest()
grid_request.setDatatype("grid")
Use the *DataAccessLayer.getAvailableLocationNames(request)* method to find out what locations are available for the given dataset. Typically these will be geographic locations or NWS sites, although in some instances it will be something else. Take a look at what's outputted for the grid_request, for example.
# Grid Locations
grid_locations = DataAccessLayer.getAvailableLocationNames(grid_request)
grid_locations.sort()
list(grid_locations)
We're setting the "location" (in this case, what model we are interested in) to specify our request before we look at the available parameters.
Take a look at the available parameters for the data set by using *DataAccessLayer.getAvailableParameters(request)*
# Pick a model and set the location for the grid request -- we'll be using the Global Forecast System (GFS)
grid_request.setLocationNames("GFS")
grid_params = DataAccessLayer.getAvailableParameters(grid_request)
grid_params.sort()
list(grid_params)
Setting the parameters is just an option, you do not need to filter the data if you do not wish to. Also, although we are only setting one parameter in this example, you can set multiple parameters by using an array:
params = ("param1", "param2", "param3"...)
request.setParameters(params)
Set a parameter, from the output above and take a look at what "levels" are available for the data set you're looking at using *DataAccessLayer.getAvailableLevels(request)*.
Note: Not all datasets support levels. If you are trying this with another dataset and run into an exception (error), it's most likely because levels are not supported for that data type.
# For grid data we'll use the temperature parameter ("T")
grid_request.setParameters("T")
grid_levels = DataAccessLayer.getAvailableLevels(grid_request)
for lvl in grid_levels:
print(lvl)
# We'll set the level to surface level
grid_request.setLevels("0.0SFC")
Take a look at what time options are available for the data you're looking at using the *DataAccessLayer.getAvailableTimes()* method:
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS (F:ff)
# Available grid times
grid_cycles = DataAccessLayer.getAvailableTimes(grid_request, True)
grid_times = DataAccessLayer.getAvailableTimes(grid_request)
## Using -1 in an array will access the last element of the array
## (using -2 will access the second last element, and so on)
grid_fcstRun = DataAccessLayer.getForecastRun(grid_cycles[-1], grid_times)
## print out the time instances
times = []
for fcst in grid_fcstRun:
print(fcst.getRefTime(), ':', fcst.getFcstTime(), 'seconds')
Now that we have our request
and DataTime fcstRun
arrays ready, it's time to request the data array from EDEX. Depending on what kind of data we're working with, we'll either use *DataAccessLayer.getGridData()* or *DataAccessLayer.getGeometryData()*
Note: We have more, detailed notebooks about how analyze and visualize the data once you have what you want.
## Grid Data
grid_response = DataAccessLayer.getGridData(grid_request, [grid_fcstRun[-1]])
for grid in grid_response:
grid_data = grid.getRawData()
lons, lats = grid.getLatLonCoords()
print('Time :', grid.getDataTime(), "-", grid.getDataTime().getFcstTime(), 'seconds')
## Take a look at some information in our data
print('Model:', str(grid.getLocationName()))
print('Parm :', str(grid.getParameter()))
print('Unit :', str(grid.getUnit()))
print(grid_data.shape)
Several functions are used throughout this notebook from the DataAccessLayer class in python-awips, to see full documentation for these functions vist here.