diff --git a/iop4admin/modeladmins/astrosource.py b/iop4admin/modeladmins/astrosource.py index 6f994013..acc002d0 100644 --- a/iop4admin/modeladmins/astrosource.py +++ b/iop4admin/modeladmins/astrosource.py @@ -25,7 +25,10 @@ class AdminAstroSource(admin.ModelAdmin): model = AstroSource - list_display = ['name', 'other_names', 'ra_hms', 'dec_dms', 'srctype', 'get_last_reducedfit', 'get_last_mag_R', 'get_calibrates', 'get_comment_firstline', 'get_details'] + list_display = ['name', 'other_names', 'ra_hms', 'dec_dms', 'srctype', 'get_last_reducedfit', + 'get_last_mag_R', 'get_calibrates', + 'get_texp_andor90', 'get_texp_andor150', 'get_texp_dipol', + 'get_comment_firstline', 'get_details'] search_fields = ['name', 'other_names', 'ra_hms', 'dec_dms', 'srctype', 'comment'] list_filter = ('srctype',) actions = ['add_field_stars_from_panstarrs', 'remove_field_stars_from_panstarrs'] @@ -45,7 +48,7 @@ def get_comment_firstline(self, obj): @admin.display(description='LAST FILE') def get_last_reducedfit(self, obj): - redf = obj.in_reducedfits.order_by('-epoch__night').first() + redf = obj.last_reducedfit if redf is not None: url = reverse('iop4admin:%s_%s_changelist' % (ReducedFit._meta.app_label, ReducedFit._meta.model_name)) + "?id=%s" % redf.pk return format_html(rf'{redf.epoch.night}') @@ -54,13 +57,7 @@ def get_last_reducedfit(self, obj): @admin.display(description="LAST MAG") def get_last_mag_R(self, obj): - ## get the average of last night - last_night = obj.photopolresults.filter(band=BANDS.R).earliest('-epoch__night').epoch.night - r_avg = obj.photopolresults.filter(band=BANDS.R, epoch__night=last_night).aggregate(mag_avg=Avg('mag'), mag_err_avg=Avg('mag_err')) - - mag_r_avg = r_avg.get('mag_avg', None) - mag_r_err_avg = r_avg.get('mag_err_avg', None) - + mag_r_avg, mag_r_err_avg = obj.last_night_mag_R if mag_r_avg is not None: return f"{mag_r_avg:.2f}" else: @@ -78,6 +75,45 @@ def get_urls(self): ] return my_urls + urls + @admin.display(description='T. Andor90') + def get_texp_andor90(self, obj): + last_night_mag_R, _ = obj.last_night_mag_R + texp = obj.texp_andor90 + + if last_night_mag_R is None: + return None + + if texp is None: + return "X" + + return texp + + @admin.display(description='T. Andor150') + def get_texp_andor150(self, obj): + last_night_mag_R, _ = obj.last_night_mag_R + texp = obj.texp_andor150 + + if last_night_mag_R is None: + return None + + if texp is None: + return "X" + + return texp + + @admin.display(description='T. x N DIPOL') + def get_texp_dipol(self, obj): + last_night_mag_R, _ = obj.last_night_mag_R + texp = obj.texp_dipol + nreps = obj.nreps_dipol + + if last_night_mag_R is None: + return None + + if texp is None: + return "X" + + return f"{texp} x {nreps}" @admin.action(description='Automatically add field stars from PanSTARRS') def add_field_stars_from_panstarrs(self, request, queryset): diff --git a/iop4lib/db/astrosource.py b/iop4lib/db/astrosource.py index 3f1f7516..2b717cdd 100644 --- a/iop4lib/db/astrosource.py +++ b/iop4lib/db/astrosource.py @@ -4,7 +4,7 @@ # django imports from django.db import models from django.db.models import Exists, OuterRef -from django.db.models import Q +from django.db.models import Q, Avg # iop4lib imports from ..enums import * @@ -17,6 +17,7 @@ from astropy.wcs import WCS from astropy.coordinates import Angle, SkyCoord import astropy.units as u +import math # logging import logging @@ -210,3 +211,109 @@ def get_sources_in_field(cls, wcs=None, width=None, height=None, fit=None, qs=No sources_in_field.append(obj) return sources_in_field + + + @property + def last_reducedfit(self): + """Returns the last ReducedFit object associated with the source.""" + return self.in_reducedfits.order_by('-epoch__night').first() + + @property + def last_night_mag_R(self): + """Returns the average magnitude and error of the last night in the R band.""" + + last_night = self.photopolresults.filter(band=BANDS.R).earliest('-epoch__night').epoch.night + r_avg = self.photopolresults.filter(band=BANDS.R, epoch__night=last_night).aggregate(mag_avg=Avg('mag'), mag_err_avg=Avg('mag_err')) + + mag_r_avg = r_avg.get('mag_avg', None) + mag_r_err_avg = r_avg.get('mag_err_avg', None) + + return mag_r_avg, mag_r_err_avg + + @property + def texp_andor90(self): + """Recommneded exposure time for Andor90, based on the last R magnitude and for a SNR of 150.""" + + snr = 150 + last_night_mag_R, _ = self.last_night_mag_R + + if last_night_mag_R is None: + return None + + texp = math.pow(snr,2) * 9.77 * 1e-16 * math.pow(10, 0.8*last_night_mag_R) + + if texp < 30: + return 60 + elif texp <= 100: + return 150 + elif texp <= 250: + return 300 + elif texp <= 400: + return 450 + elif texp <= 800: + return 600 + else: + return None + + @property + def texp_andor150(self): + """Recommneded exposure time for Andor150, based on the last night R magnitude and for a SNR of 150.""" + + snr = 150 + last_night_mag_R, _ = self.last_night_mag_R + + if last_night_mag_R is None: + return None + + texp = 0.36 * math.pow(snr,2) * 9.77 * 1e-16 * math.pow(10, 0.8*last_night_mag_R) + + if texp < 30: + return 60 + elif texp <= 100: + return 150 + elif texp <= 250: + return 300 + elif texp <= 400: + return 450 + else: + return 600 + + @property + def texp_dipol(self): + """Recommneded exposure time for DIPOL, based on the last night R magnitude and for a SNR of 150.""" + + snr = 150 + last_night_mag_R, _ = self.last_night_mag_R + + if last_night_mag_R is None: + return None + + texp = math.pow(snr,2) * 9.77 * 1e-16 * math.pow(10, 0.8*last_night_mag_R) + + if texp <= 300: + return math.ceil(texp / 10) * 10 + 10 + elif texp <= 2000: + return 300 + else: + return None + + @property + def nreps_dipol(self): + """Recommneded number of repetitions for DIPOL, based on the last night R magnitude and for a SNR of 150.""" + + snr = 150 + last_night_mag_R, _ = self.last_night_mag_R + + if last_night_mag_R is None: + return None + + texp = math.pow(snr,2) * 9.77 * 1e-16 * math.pow(10, 0.8*last_night_mag_R) + + if texp <= 20: + return 8 + elif texp <= 40: + return 4 + elif texp <= 80: + return 2 + else: + return 1 \ No newline at end of file