Browse Source

Add a new end point for enumerating rings

main
Jennie Rose 1 year ago
parent
commit
bb58f71edf
  1. 1
      galsim/resources/__init__.py
  2. 58
      galsim/resources/ring.py
  3. 11
      galsim/server.py

1
galsim/resources/__init__.py

@ -1 +1,2 @@
from galsim.resources.planet import Planet
from galsim.resources.ring import Ring

58
galsim/resources/ring.py

@ -0,0 +1,58 @@
from flask_restful import abort, Resource
from webargs import fields
from webargs.flaskparser import use_kwargs
import astropy.units as u
from galsim.schemas import PlanetSchema
import galsim.planets as galsim
class Ring(Resource):
spacing = 12.0 * u.pc
rings = [
'Andes', 'Rocky-Mountain', 'Kunlun', 'Ural',
'Atlas', 'Himalaya', 'Altai', 'Carpathian',
'Sierra-Madre', 'Karakoram', 'Cascade', 'Annamite'
]
@classmethod
def ring_index(cls, ring_id):
if ring_id.isnumeric():
return int(ring_id) - 1
else:
ring_id = ring_id.title()
if ring_id in self.rings:
return self.rings.index(ring_id)
def __init__(self, **kwargs):
self.tree = kwargs['planet_tree']
@use_kwargs({
'include': fields.DelimitedList(fields.Str()),
'limit': fields.Int(default=50),
'page': fields.Int(default=0)
}, location='query')
def get(self, ring_id, include=None, limit=50, page=0):
idx = self.ring_index(ring_id)
if idx not in range(len(self.rings)):
message = (
f'Invalid Ring ID: [{ring_id}]. '
+ 'Rings can be identified by the following names or by the corresponding numbers: ['
+ ', '.join(f'{name} ({i+1})' for i, name in enumerate(self.rings))
+ ']'
)
abort(404, message=message)
inner = idx * self.spacing
outer = inner + self.spacing
stream = self.tree.iter_within_radius(0, 0, 0, outer, inner)
planets = []
for i, planet in enumerate(stream):
if i < page * limit:
continue
elif i >= (page + 1) * limit:
break
planets.append(planet)
schema = PlanetSchema(only=include, many=True)
return schema.dump(planets)

11
galsim/server.py

@ -1,10 +1,11 @@
from flask import Flask
from flask_restful import Api
from galsim.resources import Planet
from galsim.planets import Catalog, FieldGroup
from galsim.resources import Planet, Ring
from galsim.planets import Catalog, FieldGroup, PlanetTree
catalog = Catalog(160, *FieldGroup)
catalog.fetch()
tree = PlanetTree.from_catalog(catalog)
app = Flask(__name__)
api = Api(app)
@ -15,5 +16,11 @@ api.add_resource(
resource_class_kwargs={'catalog': catalog}
)
api.add_resource(
Ring,
'/ring/<string:ring_id>',
resource_class_kwargs={'planet_tree': tree}
)
if __name__ == '__main__':
app.run(debug=True)

Loading…
Cancel
Save