88
99# iop4lib imports
1010from ..enums import *
11+ from iop4lib .utils import get_column_values
1112
1213# other imports
1314import os
1819from astropy .coordinates import Angle , SkyCoord
1920import astropy .units as u
2021import math
22+ import numpy as np
2123
2224# logging
2325import logging
@@ -228,20 +230,29 @@ def last_reducedfit(self):
228230 def last_night_mag_R (self ):
229231 """Returns the average magnitude and error of the last night in the R band."""
230232
231- last_night = self .photopolresults .filter (band = BANDS .R ).earliest ('-epoch__night' ).epoch .night
232- r_avg = self .photopolresults .filter (band = BANDS .R , epoch__night = last_night ).aggregate (mag_avg = Avg ('mag' ), mag_err_avg = Avg ('mag_err' ))
233+ qs = self .photopolresults .filter (band = BANDS .R ).exclude (mag = None ).exclude (mag_err = None )
233234
234- mag_r_avg = r_avg .get ('mag_avg' , None )
235- mag_r_err_avg = r_avg .get ('mag_err_avg' , None )
235+ if not qs :
236+ return None , None , None
237+
238+ last_night = qs .earliest ('-epoch__night' ).epoch .night
239+
240+ qs = qs .filter (epoch__night = last_night )
241+
242+ vals = get_column_values (qs , ['mag' , 'mag_err' ])
243+ mag_r , mag_r_err = vals ['mag' ], vals ['mag_err' ]
244+
245+ mag_r_avg = np .average (mag_r , weights = 1 / mag_r_err ** 2 )
246+ mag_r_avg_err = 1 / np .sqrt (np .sum (1 / mag_r_err ** 2 ))
236247
237- return mag_r_avg , mag_r_err_avg
248+ return mag_r_avg , mag_r_avg_err , last_night
238249
239250 @property
240251 def texp_andor90 (self ):
241- """Recommneded exposure time for Andor90, based on the last R magnitude and for a SNR of 150."""
252+ """Recommended exposure time for Andor90, based on the last R magnitude and for a SNR of 150."""
242253
243254 snr = 150
244- last_night_mag_R , _ = self .last_night_mag_R
255+ last_night_mag_R = self .last_night_mag_R [ 0 ]
245256
246257 if last_night_mag_R is None :
247258 return None
@@ -263,10 +274,10 @@ def texp_andor90(self):
263274
264275 @property
265276 def texp_andor150 (self ):
266- """Recommneded exposure time for Andor150, based on the last night R magnitude and for a SNR of 150."""
277+ """Recommended exposure time for Andor150, based on the last night R magnitude and for a SNR of 150."""
267278
268279 snr = 150
269- last_night_mag_R , _ = self .last_night_mag_R
280+ last_night_mag_R = self .last_night_mag_R [ 0 ]
270281
271282 if last_night_mag_R is None :
272283 return None
@@ -286,10 +297,10 @@ def texp_andor150(self):
286297
287298 @property
288299 def texp_dipol (self ):
289- """Recommneded exposure time for DIPOL, based on the last night R magnitude and for a SNR of 150."""
300+ """Recommended exposure time for DIPOL, based on the last night R magnitude and for a SNR of 150."""
290301
291302 snr = 150
292- last_night_mag_R , _ = self .last_night_mag_R
303+ last_night_mag_R = self .last_night_mag_R [ 0 ]
293304
294305 if last_night_mag_R is None :
295306 return None
@@ -305,10 +316,10 @@ def texp_dipol(self):
305316
306317 @property
307318 def nreps_dipol (self ):
308- """Recommneded number of repetitions for DIPOL, based on the last night R magnitude and for a SNR of 150."""
319+ """Recommended number of repetitions for DIPOL, based on the last night R magnitude and for a SNR of 150."""
309320
310321 snr = 150
311- last_night_mag_R , _ = self .last_night_mag_R
322+ last_night_mag_R = self .last_night_mag_R [ 0 ]
312323
313324 if last_night_mag_R is None :
314325 return None
0 commit comments