@@ -68,6 +68,8 @@ class BrightStarStamp(AbstractStamp):
6868 archive_element : Persistable | None = None
6969 annularFlux : float | None = None
7070 minValidAnnulusFraction : float = 0.0
71+ optimalInnerRadius : int | None = None
72+ optimalOuterRadius : int | None = None
7173
7274 @classmethod
7375 def factory (cls , stamp_im , metadata , idx , archive_element = None , minValidAnnulusFraction = 0.0 ):
@@ -267,6 +269,7 @@ def initAndNormalize(
267269 use_archive = False ,
268270 imCenter = None ,
269271 discardNanFluxObjects = True ,
272+ forceFindFlux = False ,
270273 statsControl = StatisticsControl (),
271274 statsFlag = stringToStatisticsProperty ("MEAN" ),
272275 badMaskPlanes = ("BAD" , "SAT" , "NO_DATA" ),
@@ -331,12 +334,13 @@ def initAndNormalize(
331334 stamps, stamps normalized with different annulus definitions, or if
332335 stamps are to be normalized but annular radii were not provided.
333336 """
337+ stampSize = starStamps [0 ].stamp_im .getDimensions ()
334338 if imCenter is None :
335- stampSize = starStamps [0 ].stamp_im .getDimensions ()
336339 imCenter = stampSize [0 ] // 2 , stampSize [1 ] // 2
337340 # Create SpanSet of annulus
338341 outerCircle = SpanSet .fromShape (outerRadius , Stencil .CIRCLE , offset = imCenter )
339342 innerCircle = SpanSet .fromShape (innerRadius , Stencil .CIRCLE , offset = imCenter )
343+ annulusWidth = outerRadius - innerRadius
340344 annulus = outerCircle .intersectNot (innerCircle )
341345 # Initialize (unnormalized) brightStarStamps instance
342346 bss = cls (
@@ -354,6 +358,7 @@ def initAndNormalize(
354358 bss ._innerRadius , bss ._outerRadius = innerRadius , outerRadius
355359 # Create a list to contain rejected stamps.
356360 rejects = []
361+ badStamps = []
357362 # Apply normalization
358363 for stamp in bss ._stamps :
359364 try :
@@ -367,13 +372,60 @@ def initAndNormalize(
367372 # steps needed before bright stars can be subtracted.
368373 if discardNanFluxObjects :
369374 rejects .append (stamp )
375+ elif forceFindFlux :
376+ newInnerRadius = innerRadius
377+ newOuterRadius = outerRadius
378+ while True :
379+ newOuterRadius += annulusWidth
380+ newInnerRadius += annulusWidth
381+ if newOuterRadius > min (imCenter ):
382+ logger .info (f"No flux found for the star with Gaia ID of { stamp .gaiaId } " )
383+ stamp .annularFlux = None
384+ badStamps .append (stamp )
385+ break
386+ newOuterCircle = SpanSet .fromShape (newOuterRadius , Stencil .CIRCLE , offset = imCenter )
387+ newInnerCircle = SpanSet .fromShape (newInnerRadius , Stencil .CIRCLE , offset = imCenter )
388+ newAnnulus = newOuterCircle .intersectNot (newInnerCircle )
389+ try :
390+ stamp .measureAndNormalize (
391+ newAnnulus ,
392+ statsControl = statsControl ,
393+ statsFlag = statsFlag ,
394+ badMaskPlanes = badMaskPlanes ,
395+ )
396+
397+ except RuntimeError as err :
398+ stamp .annularFlux = np .nan
399+ logger .error (
400+ "The annulux flux was not found for radii {} and {}" .format (
401+ newInnerRadius , newOuterRadius
402+ )
403+ )
404+ if stamp .annularFlux and stamp .annularFlux > 0 :
405+ logger .info ("The flux is found within an optimized annulus." )
406+ logger .info (
407+ "The optimized annulus raddi are {} and {} and the flux is {}" .format (
408+ newInnerRadius , newOuterRadius , stamp .annularFlux
409+ )
410+ )
411+ stamp .optimalOuterRadius = newOuterRadius
412+ stamp .optimalInnerRadius = newInnerRadius
413+ break
414+ # elif newInnerRadius > maxInnerRadius:
415+ # logger.info("The star with gaiaId of {} is impossible to normalize!".format(stamp.gaiaId))
416+ # break
370417 else :
371418 stamp .annularFlux = np .nan
372419 # Remove rejected stamps.
420+ bss .normalized = True
373421 if discardNanFluxObjects :
374422 for reject in rejects :
375423 bss ._stamps .remove (reject )
376- bss .normalized = True
424+ elif forceFindFlux :
425+ for badStamp in badStamps :
426+ bss ._stamps .remove (badStamp )
427+ bss ._innerRadius , bss ._outerRadius = None , None
428+ return bss , badStamps
377429 return bss
378430
379431 def _refresh_metadata (self ):
0 commit comments