-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumcounts.R
executable file
·90 lines (73 loc) · 4.22 KB
/
numcounts.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/Rscript --no-init-file
# setup
require("astro", quietly=TRUE)
# definitions
infiles = dir("../calexp"); infiles = paste0("../calexp/", grep(".dat", infiles, v=T))
imstats = read.csv("../sourcecats/imstats.csv", stringsAsFactors=FALSE)
bw = 0.5 # magnitude bin width
# dev
pdf(file="numcounts.pdf", width=8, height=4)
# par
layout(cbind(1,2))
par("mar"=c(0.5,0.5,0.5,0.5))
par("oma"=c(2.5,3.5,1,1.5))
# loop
for(i in 1:length(infiles)){
# plot
palette(c("#000000", "#e66101", "#5e3c99", "#fdb863", "#b2abd2", "#edf8b1", "#7fcdbb", "#2c7fb8"))
aplot(NA, xlim=c(14.5,30.5), ylim=c(4*10^1,2*10^7), log="y", yformat="p", las=1, type="n", xlab="", ylab="", xnmin=1, axes=FALSE)
mtext(side=3, line=0.25, text=c("low density region : 8283-38", "high-density region : 9592-20")[i])
# raw data
dat = read.table(infiles[i], stringsAsFactors=FALSE)
colnames(dat) = c("NUMBER", "FLUX_AUTO", "MAG_AUTO", "KRON_RADIUS", "PETRO_RADIUS", "BACKGROUND", "THRESHOLD", "X_IMAGE", "Y_IMAGE", "A_IMAGE", "B_IMAGE", "THETA_IMAGE", "ELLIPTICITY", "CLASS_STAR", "FLUX_RADIUS")
mags = dat[,"MAG_AUTO"]; if(any(mags == 99)){mags[mags==99]=NA}; mags = mags + 27
area = imstats[grep(strsplit(basename(infiles[i]), ".dat")[[1]][1], imstats[,"FILE"]),"AREA"]
# binned data
breaks = c(seq(0,50,by=bw) - (bw/2), 50 + (bw/2))
h = hist(mags, breaks=breaks, plot=FALSE)
binmag = h$mids
bindat = (h$counts / area) / bw
bindatlog = log10(bindat); if(any(bindatlog==-Inf)){bindatlog[bindatlog==-Inf] = 0}
# power law line fit
magbright = 22
magfaint = 25
fn = function(x, a){return(a + 0.4*x)}
good = which(binmag >= magbright & binmag <= magfaint)
fitdat = chisq.fit(data=bindatlog[good], par=list(a=1), fn=fn, arg=list(x=binmag[good]))
binexplog = fn(x=binmag, a=fitdat$par$a)
binexp = 10^binexplog
binfaint = binexp - bindat
binfaintlog = suppressWarnings(log10(binfaint))
# plot
objlim = (1/area)/bw
maglower = binmag[which(binexp >= objlim)[1]-1]
magupper = 30
bars(x=binmag[binmag>=maglower & binmag<=magbright], y=binexp[binmag>=maglower & binmag<=magbright], width=bw, col="grey50", joined=TRUE)
bars(x=binmag[binmag>=magfaint & binmag<=magupper], y=binfaint[binmag>=magfaint & binmag<=magupper], width=bw, col="grey75", joined=TRUE)
bars(x=binmag, y=bindat, width=bw, col=NA, border=i+1, joined=TRUE, lwd=2, ljoin=1)
bars(x=binmag[binmag>magbright], y=bindat[binmag>magbright], col=i+1, density=25, angle=-45, joined=TRUE)
abline(a=fitdat$par$a, b=0.4, col=i+3, lwd=1.5)
abline(v=c(magbright,magfaint), lty=2, lend=3, lwd=1.5)
abline(h=objlim, lty=3, lend=3)
# sim-relevant data
simmag = binmag
simden = rep(0, length(simmag))
simden[simmag >= maglower & simmag <= magbright] = binexp[simmag >= maglower & simmag <= magbright]
simden[simmag >= magfaint & simmag <= magupper] = binfaint[simmag >= magfaint & simmag <= magupper]
simcat = cbind(MAG=simmag, DEN=simden, NUM=simden*bw*area)
write.csv(simcat, row.names=FALSE, quote=FALSE, file=paste0(strsplit(basename(infiles[i]), ".image.dat")[[1]][1], ".extra.csv"))
# numbers
ncex = 0.75
nreal = sum(bindat[binmag > magbright] * bw * area)
nextra.bright = sum(ceiling(simden[simmag<=magbright] * bw * area))
nextra.faint = sum(ceiling(binfaint[binmag>=magfaint & binmag<=magupper] * bw * area))
# finish up
aaxes(las=1, yformat="p", labels=list(c(1,2),c(1,4))[[i]], xnmin=9, mgp=c(2,0.25,0))
abox()
mtext(side=c(2,NA)[i], line=2, text=bquote(paste(N[obj], " ", deg^{-2}, " ", mag^{-1})))
alegend("topleft", legend=c(paste0("detected (",nrow(dat),")"), bquote("log"[10]*N["obj"]*"="*.(formatC(fitdat$par$a,format="f",digits=2))*"+0.4"*m["r"]), paste0("bright mocks (",nextra.bright,")"), paste0("detected & used (",nreal,")"), paste0("faint missing (",nextra.faint,")")), type=list(l=list(col=i+1,lwd=2,lend=1), l=list(col=i+3,lwd=1.5,lend=1), f=list(col="grey50",border=NA), f=list(col=i+1,density=25,angle=-45,border=NA), f=list(col="grey75",border=NA)), cex=0.65, seg.len=1.25, seg.gap=0.35, inset=0.45)
}
# finish up
layout(1)
mtext(side=1, line=1.75, text=bquote(paste("apparent magnitude : ", m[r])))
graphics.off()