added mask_radius_deg field to nearby source dict

This commit is contained in:
Никита Тырин 2025-05-27 10:40:36 +03:00
parent 42c63547b1
commit e01941f06b
2 changed files with 65 additions and 26 deletions

View File

@ -156,7 +156,7 @@ class Command(BaseCommand):
self.stdout.write("All catalog rows already exist in the database.") self.stdout.write("All catalog rows already exist in the database.")
# hard coded nside and flux-radius mapping # hard coded nside and flux-radius mapping
# maybe change that # maybe change
nside = 4096 nside = 4096
npix = hp.nside2npix(nside) npix = hp.nside2npix(nside)

View File

@ -12,7 +12,18 @@ from astropy.stats import poisson_conf_interval
from collections import defaultdict from collections import defaultdict
from django.db.models import Sum, Max from django.db.models import (
Max,
Sum,
F,
IntegerField,
BooleanField,
ExpressionWrapper,
Case,
When,
Value,
)
from django.db.models.functions import Cast
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
@ -366,25 +377,37 @@ class UpperLimitView(APIView):
nearby_sources = [] nearby_sources = []
radius_map = {0: 0.06, 125: 0.15, 250: 0.5, 2000: 0.9, 20000: 2.5}
sorted_bounds = sorted(radius_map.keys())
# refine belt to circular region using astropy separation # refine belt to circular region using astropy separation
for catsrc in belt_sources: for catsrc in belt_sources:
catsrc_coord = SkyCoord(catsrc.ra_deg, catsrc.dec_deg, unit="deg") catsrc_coord = SkyCoord(catsrc.ra_deg, catsrc.dec_deg, unit="deg")
if center_coord.separation(catsrc_coord).deg <= radius_deg: if center_coord.separation(catsrc_coord).deg > radius_deg:
nearby_sources.append( continue
{ f = catsrc.flux or 0.0
"srcid": catsrc.srcid, for lb in reversed(sorted_bounds):
"name": catsrc.name, if f >= lb:
"ra_deg": catsrc.ra_deg, mask_radius = radius_map[lb]
"dec_deg": catsrc.dec_deg, break
"pos_error": catsrc.pos_error,
"significance": catsrc.significance, nearby_sources.append(
"flux": catsrc.flux, {
"flux_error": catsrc.flux_error, "srcid": catsrc.srcid,
"catalog_name": catsrc.catalog_name, "name": catsrc.name,
"new_xray": catsrc.new_xray, "ra_deg": catsrc.ra_deg,
"source_type": catsrc.source_type, "dec_deg": catsrc.dec_deg,
} "pos_error": catsrc.pos_error,
) "significance": catsrc.significance,
"flux": catsrc.flux,
"flux_error": catsrc.flux_error,
"catalog_name": catsrc.catalog_name,
"new_xray": catsrc.new_xray,
"source_type": catsrc.source_type,
"mask_radius_deg": mask_radius,
}
)
# REGION IMAGE SERVING # REGION IMAGE SERVING
# **************************************************************** # ****************************************************************
@ -407,11 +430,26 @@ class UpperLimitView(APIView):
# fetch those pixels for the requested surveys # fetch those pixels for the requested surveys
# summing counts and sorting by hpid # summing counts and sorting by hpid
# map_pixels_qs = (
# Pixel.objects.filter(hpid__in=map_pixel_list, survey__in=survey_numbers)
# .values("hpid")
# .annotate(counts=Sum("counts"))
# .order_by("hpid")
# )
map_pixels_qs = ( map_pixels_qs = (
Pixel.objects.filter(hpid__in=map_pixel_list, survey__in=survey_numbers) Pixel.objects.filter(hpid__in=map_pixel_list, survey__in=survey_numbers)
.values("hpid") .values("hpid")
.annotate( .annotate(
counts=Sum("counts"), total_counts=Sum("counts"),
max_contaminated_int=Max(Cast("contaminated", IntegerField())),
)
.annotate(
contaminated=Case(
When(max_contaminated_int=1, then=Value(True)),
default=Value(False),
output_field=BooleanField(),
)
) )
.order_by("hpid") .order_by("hpid")
) )
@ -421,15 +459,16 @@ class UpperLimitView(APIView):
# get lists of healpix indices and count values # get lists of healpix indices and count values
map_healpix_list = [d["hpid"] for d in map_pixels_list] map_healpix_list = [d["hpid"] for d in map_pixels_list]
map_counts_list = [d["counts"] for d in map_pixels_list] map_counts_list = [d["total_counts"] for d in map_pixels_list]
map_contaminated_list = [d["contaminated"] for d in map_pixels_list]
cont_dict = dict( # cont_dict = dict(
Pixel.objects.filter(hpid__in=map_healpix_list, survey__in=survey_numbers) # Pixel.objects.filter(hpid__in=map_healpix_list, survey__in=survey_numbers)
.values_list("hpid", "contaminated") # .values_list("hpid", "contaminated")
.distinct() # .distinct()
) # )
map_contaminated_list = [cont_dict[h] for h in map_healpix_list] # map_contaminated_list = [cont_dict[h] for h in map_healpix_list]
# set map nside # set map nside
map_nside = 4096 map_nside = 4096