-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data_test.py
90 lines (70 loc) · 2.72 KB
/
get_data_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import flask
import json
from hdf_read import Hdf5Spectra
# -------------------------------
# ----------- FLASK -------------
# -------------------------------
app = flask.Flask(__name__)
hdf_read = Hdf5Spectra('galah_dr53_20190516_none_full.hdf5', raw=False)
@app.route('/', methods=['GET'])
def home():
return "It works - we are ready to serve you some data."
@app.route('/galah_dr53', methods=['GET'])
def get_data():
in_args = flask.request.args
# the only mandatory input is the sobject_id all others are optional
if 'sobject_id' in in_args:
# did user request a specific extension?
ext = 4
if 'ext' in in_args:
ext = int(in_args['ext'])
# did user request a specific wvl range?
ranges = None
if 'range' in in_args:
ranges = [r.split(':') for r in in_args['range'].split(',')]
# multiple spectra are merged by default
merge = 'median'
if 'merge' in in_args:
merge = str(in_args['merge']).lower()
if merge in ['false', 'none']:
merge = None
# do we have to return spectra for only a few ccds?
ccds = [1, 2, 3, 4]
if 'ccd' in in_args:
ccds = in_args['ccd'].split(',')
try:
# get requested data
json_response = hdf_read.get_h5_data(in_args['sobject_id'].split(','),
ccds,
wvl_ranges=ranges,
extension=ext,
merge=merge)
# return them to the user in a json format
return flask.json.jsonify(json_response)
except:
# return empty response to the user
return flask.json.jsonify(json.dumps({}))
else:
return 'Spectrum not selected'
@app.route('/galah_wvl', methods=['GET'])
def get_wvl():
in_args = flask.request.args
if 'ccd' in in_args:
# did user request a specific wvl range?
ranges = None
if 'range' in in_args:
ranges = [r.split(':') for r in in_args['range'].split(',')]
try:
# get requested data
json_response = hdf_read.get_h5_wvl(in_args['ccd'].split(','),
wvl_ranges=ranges)
# return them to the user in a json format
return flask.json.jsonify(json_response)
except:
# return empty response to the user
return flask.json.jsonify(json.dumps({}))
else:
return 'CCD not selected'
app.run(host='193.2.67.179', port=8080, debug=True)
# closing is not needed any more
# hdf_read.close()