Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recommended exposure times OSN instruments in the admin #143

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions iop4admin/modeladmins/astrosource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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'<a href="{url}">{redf.epoch.night}</a>')
Expand All @@ -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:
Expand All @@ -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):
Expand Down
109 changes: 108 additions & 1 deletion iop4lib/db/astrosource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand All @@ -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
Expand Down Expand Up @@ -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
morcuended marked this conversation as resolved.
Show resolved Hide resolved
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
Loading