first prototype API implementation
This commit is contained in:
		@@ -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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,4 +4,4 @@ from .models import GaiaSource
 | 
			
		||||
class GaiaSourceSerializer(serializers.ModelSerializer):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = GaiaSource
 | 
			
		||||
        fields = '__all__'
 | 
			
		||||
        fields = ['uuid', 'ra', 'dec']
 | 
			
		||||
							
								
								
									
										52
									
								
								views.py
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user