diff --git a/views.py b/views.py index 9d80b97..00f4bfa 100644 --- a/views.py +++ b/views.py @@ -10,6 +10,8 @@ import scipy.special as sp from astropy.coordinates import SkyCoord, Angle from astropy.stats import poisson_conf_interval +from collections import defaultdict + from django.db.models import Sum from django.shortcuts import get_object_or_404 from rest_framework.views import APIView @@ -225,6 +227,7 @@ class UpperLimitView(APIView): # COMPUTE COUNTS, BACKGROUND ESTIMATE, EXPOSURE # ************************************************************** + # summing counts across all surveys N = sum(obj.counts for obj in source_pixels) Nnpix = len(source_pixels) @@ -235,9 +238,21 @@ class UpperLimitView(APIView): B = Bcounts / Bnpix * Nnpix - tsum = sum(obj.exposure for obj in source_pixels) + # create a dict of exposures keyed by survey + t_by_survey = defaultdict(list) - t = tsum / Nnpix + for pixel in source_pixels: + t_by_survey[pixel.survey].append(pixel.exposure) + + # create and populate a list of average exposures per survey + survey_averages = [] + + for survey_id, exposures in t_by_survey.items(): + average_exposure = sum(exposures) / len(exposures) + survey_averages.append(average_exposure) + + # sum them up across surveys + t = sum(survey_averages) # CONSTANTS # **************************************************************