Commit 28f15496 authored by Bruce Flynn's avatar Bruce Flynn

fix: make symbols optional

parent dc87803d
......@@ -5,28 +5,26 @@ from tempfile import mkstemp
from formencode import Schema, Invalid
from formencode.validators import Regex, Int, OneOf
from pyramid.view import view_config
from pyramid.response import Response, FileIter
from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
from pyramid.response import Response, FileIter
from pyramid.view import view_config
from amrc_aws import nc, util, db
from amrc_aws import nc, db
LOG = logging.getLogger(__name__)
# valid station names
station_names = []
# valid symbol names
symbol_names = ('air_temp', 'vtempdiff', 'pressure', 'rh',
'wind_spd', 'wind_dir', 'wind_dir_east', 'wind_dir_north')
symbol_names = ('air_temp', 'vtempdiff', 'pressure', 'rh', 'wind_spd', 'wind_dir')
def get_form(schema, request):
try:
return schema().to_python(request.params)
except Invalid as err:
raise HTTPBadRequest('Invalid request parameters: {}'
.format(err))
raise HTTPBadRequest('Invalid request parameters: {}'.format(err))
class DataForm(Schema):
......@@ -35,7 +33,7 @@ class DataForm(Schema):
type = OneOf(['rdr', 'q1h'], if_missing='q1h', if_empty='q1h')
stations = Regex(r'^[-a-zA-Z_0-9() |\.]+$', not_empty=True)
symbols = Regex(r'^[a-zA-Z_0-9|\.]+$', not_empty=True)
symbols = Regex(r'^[a-zA-Z_0-9|\.]+$', if_empty=None, if_missing=None)
start = Regex(r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$', not_empty=True)
end = Regex(r'^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$', not_empty=True)
avg = Int(min=10, max=86400, if_missing=None, if_empty=None)
......@@ -69,6 +67,8 @@ def validate_symbols(form):
Raise HTTPBadRequest is the symbols contain an unknown symbol, otherwise
split them into a list.
"""
if form['symbols'] is None:
return symbol_names
symbols = form['symbols'].split('|')
for name in symbols:
if name not in symbol_names:
......@@ -168,7 +168,7 @@ def data_netcdf_view(request):
})
nc.write_slice_to_netcdf(stations, symbols, data, dest, attrs=attrs)
response = Response(app_iter=RemovingFileIter(open(dest)))
response = Response(app_iter=RemovingFileIter(open(dest, 'rb')))
response.content_type = 'application/x-netcdf'
response.content_disposition = 'attachment; filename="amrc_aws.nc"'
return response
......
......@@ -218,8 +218,7 @@ def _get_slice(type_, stations, symbols, start, end, avg):
dat = np.concatenate((stamps, dat[:, :4], wind_dir, wind_spd), axis=1)
# return only the columns for the requested sensors
all_sensors = ['air_temp', 'vtempdiff', 'rh', 'pressure', 'wind_dir',
'wind_spd']
all_sensors = ['air_temp', 'vtempdiff', 'rh', 'pressure', 'wind_dir', 'wind_spd']
colidxs = [0] + [all_sensors.index(s) + 1 for s in symbols]
return dat[:, colidxs]
......
......@@ -79,15 +79,26 @@ class Test(object):
pyramid_app.get('/data.nc', status=400)
for type_ in ('', 'rdr', 'q1h'):
res = pyramid_app.get((
'/data.nc?'
'type={}&'
'stations=STATION1&'
'symbols={}&'
'start=1970-01-01T00:00:00Z&'
'end=1970-01-01T00:00:00Z&').format(type_, '|'.join(app.symbol_names)),
status=404
)
res = pyramid_app.get(
('/data.nc?'
'type={}&'
'stations=STATION1&'
'symbols={}&'
'start=1970-01-01T00:00:00Z&'
'end=1970-01-01T00:00:00Z&').format(type_, '|'.join(app.symbol_names)),
status=404)
def test_nc_default_symbols(self, pyramid_app):
pyramid_app.get('/data.nc', status=400)
for type_ in ('', 'rdr', 'q1h'):
res = pyramid_app.get(
('/data.nc?'
'type={}&'
'stations=STATION1&'
'start=1970-01-01T00:00:00Z&'
'end=1970-01-01T00:00:00Z&').format(type_),
status=404)
def test_get_stations(self, pyramid_app):
pyramid_app.get('/stations.json', status=200)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment