From 95702c3fb2964f387ad5cabbabb7a192519515cd Mon Sep 17 00:00:00 2001 From: tyrin Date: Tue, 17 Sep 2024 15:04:04 +0300 Subject: [PATCH] first prototype API implementation --- models.py | 3 +++ serializers.py | 2 +- views.py | 54 +++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/models.py b/models.py index 98edc4e..8751a36 100644 --- a/models.py +++ b/models.py @@ -75,6 +75,9 @@ class GaiaSource(models.Model): healpix_nested_index = models.BigIntegerField(null=True, blank=True) + #healpix_nside = models.IntegerField(null=True, blank=True) + #additional field storing nside value + diff --git a/serializers.py b/serializers.py index efd6031..6787ef9 100644 --- a/serializers.py +++ b/serializers.py @@ -4,4 +4,4 @@ from .models import GaiaSource class GaiaSourceSerializer(serializers.ModelSerializer): class Meta: model = GaiaSource - fields = '__all__' \ No newline at end of file + fields = ['uuid', 'ra', 'dec'] \ No newline at end of file diff --git a/views.py b/views.py index 7c4ec88..fc31c79 100644 --- a/views.py +++ b/views.py @@ -1,14 +1,58 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status + from .models import GaiaSource from .serializers import GaiaSourceSerializer +import numpy as np +import healpy as hp +from astropy.coordinates import SkyCoord +from astropy import units as u + + class ConeSearchView(APIView): + def get(self, request, ra, dec, radius): radius = float(radius) - ra = radians(float(ra)) - dec = radians(float(dec)) - objects = GaiaSource.objects.all() - result = [] - + ra = float(ra) + dec = float(dec) + nside = 2048 + + 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) + #fetch all objects from those healpixes + objects = GaiaSource.objects.filter(healpix_ring_index__in=healpix_indices) + + results = [] + + for obj in objects: + + source_coordinates = SkyCoord( + ra=obj.ra*u.deg, + dec=obj.dec*u.deg, + frame='icrs' + ) + + separation = center.separation(source_coordinates) + + if separation <= radius: + + results.append(obj) + + serializer = GaiaSourceSerializer(results, many=True) + + 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 + +