1- .fluxcalcapp = function (x = NULL , y = NULL , rad2 = NULL , flux = NULL , xcen = NA , ycen = NA , rad_app = NULL , centype = ' mean' ){
2- if (is.null(rad2 )){
3- if (is.na(xcen )){
4- if (centype == ' wt' | centype == ' mean' ){
5- xcen = .meanwt(x , flux )
6- }else if (centype == ' max' ){
7- xcen = x [which.max(flux )]
8- }else {
9- stop(' centype must be max or mean!' )
10- }
11- }
12-
13- if (is.na(ycen )){
14- if (centype == ' wt' | centype == ' mean' ){
15- ycen = .meanwt(y , flux )
16- }else if (centype == ' max' ){
17- ycen = y [which.max(flux )]
18- }else {
19- stop(' centype must be max or mean!' )
20- }
21- }
22-
23- if (xcen == 0 & ycen == 0 ){
24- rad2 = x ^ 2 + y ^ 2
1+ .fluxcalcapp = function (x = NULL , y = NULL , flux = NULL , xcen = NA , ycen = NA , rad_app = NULL , centype = ' mean' , depth = 4 ){
2+ if (is.na(xcen )){
3+ if (centype == ' wt' | centype == ' mean' ){
4+ xcen = .meanwt(x , flux )
5+ }else if (centype == ' max' ){
6+ xcen = x [which.max(flux )]
257 }else {
26- rad2 = ( x - xcen ) ^ 2 + ( y - ycen ) ^ 2
8+ stop( ' centype must be max or mean! ' )
279 }
28- }else {
29- xcen = 0
30- ycen = 0
3110 }
3211
33- if (is.na(rad_app )){
34- # left here for a different mode I tested for applying rad2 selection outside of this function.
35- # that wasn't any faster, so probably remove this if case in the future
36- Nsel = length(rad2 )
37- rad_out = max(rad2 )
38- sel_out = which(rad2 == rad_out )
39-
40- flux_app = sum(flux , na.rm = TRUE )
41- flux_min = mean(flux [sel_out ], na.rm = TRUE )
42- }else {
43- sel = which(rad2 < = rad_app ^ 2 )
44- Nsel = length(sel )
45-
46- if (length(Nsel ) == 0 ){
47- flux_app = NA_real_
48- flux_min = 0
12+ if (is.na(ycen )){
13+ if (centype == ' wt' | centype == ' mean' ){
14+ ycen = .meanwt(y , flux )
15+ }else if (centype == ' max' ){
16+ ycen = y [which.max(flux )]
4917 }else {
50- if (all(is.na(flux [sel ]))){
51- flux_app = NA_real_
52- flux_min = 0
53- }else {
54- rad_out = max(rad2 [sel ])
55- sel_out = which(rad2 == rad_out )
56-
57- flux_app = sum(flux [sel ], na.rm = TRUE )
58- flux_min = mean(flux [sel_out ], na.rm = TRUE )
59- }
18+ stop(' centype must be max or mean!' )
6019 }
6120 }
6221
22+ # if(xcen == 0 & ycen == 0){
23+ # rad2 = x^2 + y^2
24+ # }else{
25+ # rad2 = (x - xcen)^2 + (y - ycen)^2
26+ # }
27+
28+ # if(is.na(rad_app)){
29+ # #left here for a different mode I tested for applying rad2 selection outside of this function.
30+ # #that wasn't any faster, so probably remove this if case in the future
31+ # Nsel = length(rad2)
32+ # rad_out = max(rad2)
33+ # sel_out = which(rad2 == rad_out)
34+ #
35+ # flux_app = sum(flux, na.rm=TRUE)
36+ # #flux_min = mean(flux[sel_out], na.rm=TRUE)
37+ # }else{
38+ # sel = which(rad2 <= rad_app^2)
39+ # Nsel = length(sel)
40+ #
41+ # if(length(Nsel) == 0){
42+ # flux_app = NA_real_
43+ # #flux_min = 0
44+ # }else{
45+ # if(all(is.na(flux[sel]))){
46+ # flux_app = NA_real_
47+ # #flux_min = 0
48+ # }else{
49+ # rad_out = max(rad2[sel])
50+ # sel_out = which(rad2 == rad_out)
51+ #
52+ # flux_app = sum(flux[sel], na.rm=TRUE)
53+ # #flux_min = mean(flux[sel_out], na.rm=TRUE)
54+ # }
55+ # }
56+ # }
57+
58+ aper_frac = profoundAperCover(x , y , xcen , ycen , rad_app , depth = depth )
59+ flux_app = sum(flux * aper_frac , na.rm = TRUE )
60+ N_app = sum((! is.na(aper_frac ))* aper_frac , na.rm = TRUE )
61+
62+ suppressWarnings({
63+ if (depth > 0 ){
64+ flux_min = min(flux [aper_frac > 0 & aper_frac < 1 & flux > 0 ], na.rm = TRUE )
65+ }else {
66+ flux_min = min(flux [aper_frac > 0 & aper_frac < = 1 & flux > 0 ], na.rm = TRUE )
67+ }
68+ })
69+
6370 if (! isTRUE(is.finite(flux_min ))){ # this catches NA, NaN, NULL, Inf events
6471 flux_min = 0 # don't want to penalise when masked or other weird events
6572 }else if (flux_min < 0 ){
6673 flux_min = 0 # don't want to penalise when in the sky noise
6774 }
6875
69- return (list (flux_app = flux_app , flux_min = flux_min , N = Nsel ))
76+ return (list (flux_app = flux_app , N_app = N_app , flux_min = flux_min ))
7077}
7178
7279profoundAperPhot = function (image = NULL , segim = NULL , app_diam = 1 , mask = NULL , keyvalues = NULL , tar = NULL ,
73- pixscale = 1 , magzero = 0 , correction = TRUE , centype = ' mean' , fluxtype = ' Raw ' ,
74- verbose = FALSE ){
80+ pixscale = 1 , magzero = 0 , correction = TRUE , centype = ' mean' ,
81+ fluxtype = ' Raw ' , depth = 4 , verbose = FALSE ){
7582 if (! is.null(image )){
7683 if (inherits(image , ' Rfits_image' )){
7784 keyvalues = image $ keyvalues
@@ -128,7 +135,7 @@ profoundAperPhot = function(image=NULL, segim=NULL, app_diam=1, mask=NULL, keyva
128135 stop(' fluxtype must be Jansky / Microjansky / Raw!' )
129136 }
130137
131- segID = x = y = flux = j = rad2 = NULL
138+ segID = x = y = flux = j = NULL
132139
133140 Rapp = (app_diam / 2 / pixscale ) # in pixels
134141 Aapp = (pi * Rapp ^ 2 ) # in pixels
@@ -207,7 +214,7 @@ profoundAperPhot = function(image=NULL, segim=NULL, app_diam=1, mask=NULL, keyva
207214 match_segID = match(tempDT $ segID , tar $ segID )
208215 tempDT [, x : = x - tar [match_segID , ' xcen' ]]
209216 tempDT [, y : = y - tar [match_segID , ' ycen' ]]
210- tempDT [, rad2 : = x ^ 2 + y ^ 2 ]
217+ # tempDT[, rad2:= x^2 + y^2]
211218
212219 # setkey(tempDT, segID, rad2)
213220
@@ -218,7 +225,7 @@ profoundAperPhot = function(image=NULL, segim=NULL, app_diam=1, mask=NULL, keyva
218225 if (verbose ){message(' Aperture: ' , app_diam [j ], ' [asec]' )}
219226 # the top one can completely remove segments in some weird edge cases, and doesn't appear to be faster. Use the second!
220227 # temp_app = tempDT[rad2 <= Rapp[j]^2, .fluxcalcapp(x=x, y=y, rad2=rad2, flux=flux, xcen=0, ycen=0, rad_app=NA), by=segID]
221- temp_app = tempDT [, .fluxcalcapp(rad2 = rad2 , flux = flux , xcen = 0 , ycen = 0 , rad_app = Rapp [j ]), by = segID ]
228+ temp_app = tempDT [, .fluxcalcapp(x = x , y = y , flux = flux , xcen = 0 , ycen = 0 , rad_app = Rapp [j ], depth = depth ), by = segID ]
222229
223230 if (correction ){
224231 temp_app $ flux_app = temp_app $ flux_app - (temp_app $ N - Aapp [j ])* temp_app $ flux_min
@@ -228,9 +235,10 @@ profoundAperPhot = function(image=NULL, segim=NULL, app_diam=1, mask=NULL, keyva
228235 return (data.frame (flux_app = temp_app $ flux_app * fluxscale ,
229236 mag_app = mag_app ,
230237 SB_app = mag_app + 2.5 * log10(Aapp [j ]) + 5 * log10(pixscale ),
231- N_app = temp_app $ N ,
232- frac_app = temp_app $ N / Aapp [j ],
233- flux_min = temp_app $ flux_min * fluxscale )
238+ N_app = temp_app $ N_app ,
239+ frac_app = temp_app $ N_app / Aapp [j ],
240+ flux_min = temp_app $ flux_min * fluxscale
241+ )
234242 )
235243 }
236244
@@ -242,7 +250,8 @@ profoundAperPhot = function(image=NULL, segim=NULL, app_diam=1, mask=NULL, keyva
242250}
243251
244252profoundAperRan = function (image = NULL , segim = NULL , app_diam = 1 , mask = NULL , Nran = 100 , keyvalues = NULL ,
245- pixscale = 1 , magzero = 0 , correction = TRUE , fluxtype = ' Raw' , verbose = FALSE ){
253+ pixscale = 1 , magzero = 0 , correction = TRUE , fluxtype = ' Raw' , depth = 4 ,
254+ verbose = FALSE ){
246255 if (! is.null(image )){
247256 if (inherits(image , ' Rfits_image' )){
248257 keyvalues = image $ keyvalues
@@ -318,8 +327,8 @@ profoundAperRan = function(image=NULL, segim=NULL, app_diam=1, mask=NULL, Nran=1
318327 segim_ran [segim > 0L ] = 0L
319328
320329 output = profoundAperPhot(image = image , segim = segim_ran , app_diam = app_diam , keyvalues = keyvalues ,
321- tar = tar , pixscale = pixscale , magzero = magzero , correction = correction ,
322- fluxtype = fluxtype , verbose = verbose )
330+ tar = tar , pixscale = pixscale , magzero = magzero , correction = correction ,
331+ fluxtype = fluxtype , depth = depth , verbose = verbose )
323332
324333 i = NULL
325334
0 commit comments