3 changed files with 68 additions and 2 deletions
@ -1 +1,2 @@
|
||||
from galsim.resources.planet import Planet |
||||
from galsim.resources.ring import Ring |
||||
|
@ -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) |
Loading…
Reference in new issue