diff --git a/urls.py b/urls.py index 357a23b..1f54539 100644 --- a/urls.py +++ b/urls.py @@ -3,7 +3,7 @@ from .views import ConeSearchView urlpatterns = [ path( - 'cone_search///', + 'cone_search///', ConeSearchView.as_view(), name = 'cone_search' ), diff --git a/views.py b/views.py index 224a45f..373d55c 100644 --- a/views.py +++ b/views.py @@ -13,6 +13,19 @@ from astropy import units as u class ConeSearchView(APIView): + @staticmethod + def get_healpix_indices(ra, dec, radius, nside): + + # Convert the input coordinates to a SkyCoord object + center = SkyCoord(ra=ra*u.deg, dec=dec*u.deg, frame='icrs') + + # Convert the center coordinates to HEALPix vector + vec = hp.ang2vec(center.ra.deg, center.dec.deg, lonlat=True) + + # Find the pixels within the given radius + indices = hp.query_disc(nside, vec, np.radians(radius)) + return indices + def get(self, request, ra, dec, radius): radius = float(radius) ra = float(ra) @@ -23,7 +36,7 @@ class ConeSearchView(APIView): center = SkyCoord(ra=ra*u.deg, dec=dec*u.deg, frame='icrs') #fetch healpix indices in the specified disc - healpix_indices = get_healpix_indices(ra, dec, radius, nside) + healpix_indices = self.get_healpix_indices(ra, dec, radius, nside) #fetch all objects from those healpixes objects = GaiaSource.objects.filter(healpix_ring_index__in=healpix_indices) @@ -39,7 +52,7 @@ class ConeSearchView(APIView): separation = center.separation(source_coordinates) - if separation.degrees <= radius: + if separation.degree <= radius: results.append(obj) @@ -47,16 +60,6 @@ class ConeSearchView(APIView): return Response(serializer.data, status=status.HTTP_200_OK) - def get_healpix_indices(ra, dec, radius, nside): - - # Convert the input coordinates to a SkyCoord object - center = SkyCoord(ra=ra*u.deg, dec=dec*u.deg, frame='icrs') - - # Convert the center coordinates to HEALPix vector - vec = hp.ang2vec(center.ra.deg, center.dec.deg, lonlat=True) - - # Find the pixels within the given radius - indices = hp.query_disc(nside, vec, np.radians(radius)) - return indices +