-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastrocat.py
executable file
·283 lines (203 loc) · 7.33 KB
/
astrocat.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!env/bin/python
import json
import time
from bson import ObjectId
from config import *
from flask import Flask, jsonify, url_for, abort, request
from jsonschema import validate
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient(mongodb_url)
astronomydb = client.AstronomyCatalogs
messier_collection = astronomydb.Messier
def message_template():
"""
Return a message template for consistency throughout
"""
message = {
'responded': time.time(),
}
return message
def sanitize(message):
"""
Sanitize message, use JSONEncoder
:param message:
:return:
"""
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)
message = JSONEncoder().encode(message)
message = json.loads(message)
return message
@app.route('/astronomy/messier/', methods=['GET'])
def list_messier_objects():
"""
Reply with a list of Messier objects
"""
messier_catalog = []
messier_list = messier_collection.find()
# make a list of objects
for messier_object in messier_list:
messier_object['url'] = url_for('get_messier_object_by_mid', mid=messier_object['mid'], _external=False)
messier_catalog.append(messier_object)
# get a message template
message = message_template()
# add the list of Messier objects
message['messierobjects'] = messier_catalog
message = sanitize(message)
# return message
return jsonify(message)
@app.route('/astronomy/messier/<string:mid>', methods=['GET'])
def get_messier_object_by_mid(mid):
"""
Reply with the Messier object based on mid
:param mid:
:return:
"""
# perform a case-insensitive search
messier_object = messier_collection.find_one({'mid': {'$regex': mid, '$options': '-i'}})
if not messier_object:
abort(404)
messier_object['url'] = url_for('get_messier_object_by_mid', mid=messier_object['mid'], _external=False)
message = message_template()
message['object'] = messier_object
message = sanitize(message)
return jsonify(message)
@app.route('/astronomy/messier/', methods=['POST'])
def create_messier_object_by_mid():
"""
Create a Messier object
"""
if type(request.json) is not dict:
abort(400)
messier_object_schema = {
"type": "object",
"properties": {
"loc": {"type": "object"},
"mid": {"type": "string"},
"ngcid": {"type": "string"},
"remarks": {"type": "string"},
"constellation": {"type": "string"}
}
}
# verify POSTed Messier object schema
try:
validate(request.json, messier_object_schema)
new_messier_object = request.json
except:
raise
messier_collection.insert(new_messier_object)
new_messier_object['url'] = url_for('get_messier_object_by_mid', mid=new_messier_object['mid'], _external=False)
message = message_template()
message['object'] = new_messier_object
message = sanitize(message)
return jsonify(message)
@app.route('/astronomy/messier/<string:mid>', methods=['PUT'])
def update_messier_object_by_mid(mid):
"""
Update the Messier object in the database and return the new object as a reply.
It will expect a PUT body in the following form:
{'key': value, 'key': value} where key may be an already existing key in the Messier object
of mid
:param mid:
:return:
"""
mid = mid.upper()
messier_object = messier_collection.find_one({'mid': mid})
if not messier_object:
abort(404)
if type(request.json) is not dict:
abort(400)
updates = request.json
for key in updates:
messier_object[key] = updates[key]
# update the db
messier_collection.update_one({'_id': messier_object['_id']}, {"$set": messier_object})
message = message_template()
message['object'] = messier_object
message = sanitize(message)
return jsonify(message)
@app.route('/astronomy/messier/<string:mid>', methods=['DELETE'])
def delete_messier_object_by_mid(mid):
"""
Delete Messier object by mid
:param mid:
:return:
"""
mid = mid.upper()
messier_object = messier_collection.find_one({'mid': mid})
if not messier_object:
abort(404)
result = messier_collection.delete_one({'_id': messier_object['_id']})
if result.deleted_count == 0:
abort(501)
messier_object['status'] = "deleted"
message = message_template()
message['object'] = messier_object
message = sanitize(message)
return jsonify(message)
@app.route('/astronomy/messier/<string:mid>/nearby/<int:degrees>', methods=['GET'])
def get_nearby_objects_by_mid(mid, degrees):
"""
Based on a given Messier object and angular distance, find nearby objects
:param mid:
:param degrees:
:return:
"""
# find center object
mid = mid.upper()
center_object = messier_collection.find_one({'mid': mid})
if not center_object or not degrees:
abort(404)
center_object['url'] = url_for('get_messier_object_by_mid', mid=center_object['mid'], _external=False)
# calculate degrees from radians
radians = degrees * (3.14 / 180)
# find objects near
nearby_objects = []
nearby_objects_cursor = messier_collection.find({'loc': {'$geoWithin':
{'$centerSphere':
[[center_object['loc']['coordinates'][0],
center_object['loc']['coordinates'][1]],
radians]}
}})
# loop through the result
if nearby_objects_cursor:
for nearby_object in nearby_objects_cursor:
nearby_object['url'] = url_for('get_messier_object_by_mid', mid=nearby_object['mid'], _external=False)
if nearby_object['mid'] != center_object['mid']:
nearby_objects.append(nearby_object)
message = message_template()
message['centerobject'] = center_object
message['nearbyobjects'] = nearby_objects
message = sanitize(message)
return jsonify(message)
@app.route('/astronomy/messier/nearby/<float:ra>/<float:dec>/<int:degrees>', methods=['GET'])
def get_nearby_objects_by_coord(ra, dec, degrees):
"""
:param ra:
:param dec:
:param degrees:
:return:
"""
if not ra or not dec or not degrees:
abort(400)
# calculate radians from degrees
radians = degrees * (3.14 / 180)
# find objects near
nearby_objects = []
nearby_objects_cursor = messier_collection.find({'loc': {'$geoWithin': {'$centerSphere':[[ra, dec], radians]}}})
# loop through the result
if nearby_objects_cursor:
for nearby_object in nearby_objects_cursor:
nearby_object['url'] = url_for('get_messier_object_by_mid', mid=nearby_object['mid'], _external=False)
nearby_objects.append(nearby_object)
message = message_template()
message['target'] = {'coordinates': (ra, dec), 'degrees': degrees}
message['nearbyobjects'] = nearby_objects
message = sanitize(message)
return jsonify(message)
if __name__ == '__main__':
app.run(debug=True)