@@ -1245,16 +1245,28 @@ def fcn2min(pars):
1245
1245
1246
1246
# Method calculates the flux of the star (uses the skybg_phot method to do background sub)
1247
1247
def aperPhot (data , starIndex , xc , yc , r = 5 , dr = 5 ):
1248
- if dr > 0 and not np .isnan (xc ) and not np .isnan (yc ):
1248
+ # Check for invalid coordinates
1249
+ if np .isnan (xc ) or np .isnan (yc ):
1250
+ return 0 , 0
1251
+
1252
+ # Calculate background if dr > 0
1253
+ if dr > 0 :
1249
1254
bgflux , sigmabg , Nbg = skybg_phot (data , starIndex , xc , yc , r + 2 , dr )
1250
1255
else :
1251
1256
bgflux , sigmabg , Nbg = 0 , 0 , 0
1252
-
1257
+
1258
+ # Create aperture and mask
1253
1259
aperture = CircularAperture (positions = [(xc , yc )], r = r )
1254
1260
mask = aperture .to_mask (method = 'exact' )[0 ]
1255
1261
data_cutout = mask .cutout (data )
1262
+
1263
+ # Check if aperture is valid
1264
+ if data_cutout is None :
1265
+ # Aperture is partially or fully outside the image
1266
+ return 0 , bgflux # Return zero flux but valid background
1267
+
1268
+ # Calculate and return aperture sum
1256
1269
aperture_sum = (mask .data * (data_cutout - bgflux )).sum ()
1257
-
1258
1270
return aperture_sum , bgflux
1259
1271
1260
1272
0 commit comments