forked from bjanesh/odi-tools
/
full_phot.py
226 lines (191 loc) · 8.6 KB
/
full_phot.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import sys, os, glob, string
import numpy as np
import astropy as ast
import matplotlib.pyplot as plt
from pyraf import iraf
import odi_config as odi
import pandas as pd
from astropy.coordinates import SkyCoord
from astropy import units as u
from collections import OrderedDict
def trim_img(img):
x1,x2 = 2508,15798
y1,y2 = 2216,15506
input = img[:-5]+'['+repr(x1)+':'+repr(x2)+','+repr(y1)+':'+repr(y2)+']'
output = img[:-5]+'.trim.fits'
if not os.path.isfile(output):
print 'Trimming image: ' ,img
iraf.unlearn(iraf.imcopy)
iraf.imcopy(input = input,output = output,verbose='no',mode='h')
def find_sources_full(img,fwhm,bg_std,threshold=4.0):
output = img[:-5]+'_sources.coo'
if not os.path.isfile(output):
print 'Locating sources on ',img
print 'Will output to ',output
iraf.unlearn(iraf.apphot.daofind)
iraf.datapars.setParam('fwhmpsf',fwhm,check=1)
iraf.datapars.setParam('datamin',-900,check=1)
iraf.datapars.setParam('datamax',60000,check=1)
iraf.datapars.setParam('sigma',bg_std,check=1)
iraf.findpars.setParam('threshold',threshold)
iraf.apphot.daofind.setParam('output',output)
iraf.apphot.daofind(image=img, verbose="no", verify='no')
def phot_sources_full(img,fwhm,airmass,apfactor):
iraf.ptools(_doprint=0)
coords = img[:-5]+'_sources.coo'
output = img[:-5]+'.phot.1'
phot_tbl = img[:-5]+'.srcphot'
if not os.path.isfile(phot_tbl) :
print 'phot-ing ', img, ' from daofind'
iraf.unlearn(iraf.apphot.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars)
iraf.apphot.phot.setParam('interactive',"no")
iraf.apphot.phot.setParam('verify',"no")
iraf.datapars.setParam('datamax',50000.)
iraf.datapars.setParam('gain',"gain")
iraf.datapars.setParam('ccdread','rdnoise')
iraf.datapars.setParam('exposure',"exptime")
iraf.datapars.setParam('xairmass',airmass)
iraf.datapars.setParam('filter',"filter")
iraf.datapars.setParam('obstime',"time-obs")
iraf.datapars.setParam('sigma',"INDEF")
iraf.photpars.setParam('zmag',0.)
iraf.centerpars.setParam('cbox',9.)
iraf.centerpars.setParam('maxshift',3.)
iraf.fitskypars.setParam('salgorithm',"median")
iraf.fitskypars.setParam('dannulus',10.)
iraf.datapars.setParam('fwhmpsf',fwhm)
iraf.photpars.setParam('apertures',apfactor*fwhm)
iraf.fitskypars.setParam('annulus',6.5*fwhm)
iraf.apphot.phot(image=img, coords=coords, output=output)
with open(phot_tbl,'w+') as txdump_out:
iraf.ptools.txdump(textfiles=output, fields="id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image",expr='yes', headers='no', Stdout=txdump_out)
outputfile_clean = open(phot_tbl.replace('.srcphot','_clean.srcphot'),"w")
for line in open(phot_tbl,"r"):
if not 'INDEF' in line:
outputfile_clean.write(line)
if 'INDEF' in line:
outputfile_clean.write(line.replace('INDEF','999'))
outputfile_clean.close()
os.rename(phot_tbl.replace('.srcphot','_clean.srcphot'),phot_tbl)
def phot_sources_xy2sky(img,inst):
phot_tbl = img[0:-5]+'.srcphot'
outputradec = img[0:-5]+'.srcphotrd'
hdulist= odi.fits.open(img)
#if inst == 'podi':
# header = hdulist[0].header
# print header['CTYPE1']
# print header['CTYPE2']
# header['CTYPE1'] = 'RA---TPV'
# header['CTYPE2'] = 'DEC--TPV'
# print header['CTYPE1']
# print header['CTYPE2']
# w = odi.WCS(header)
#w.wcs.ctype = ["RA---TPV", "DEC--TPV"]
# print w
#pvlist = hdulist[0].header['PV*']
#for pv in pvlist:
#tpv = 'T'+pv
#hdulist[0].header.rename_keyword(pv, tpv, force=False)
w = odi.WCS(hdulist[0].header)
MAG, MERR, SKY, SERR, RAPERT, XPOS, YPOS = np.loadtxt(phot_tbl, usecols=(1,2,3,4,5,6,7), dtype=float, unpack=True)
with open(outputradec, 'w+') as fxy:
for i,c in enumerate(XPOS):
coords2 = [[XPOS[i],YPOS[i]]]
pixcrd2 = w.wcs_pix2world(coords2, 1)
print >> fxy, pixcrd2[0][0], pixcrd2[0][1],XPOS[i],YPOS[i],MAG[i], MERR[i],SKY[i],SERR[i],RAPERT[i]
hdulist.close()
def match_phot_srcs(img1,img2):
img1_srcs =img1[:-5]+'.srcphotrd'
img1_srsc_match = img1[:-5]+'.match.srscrd'
img2_srcs =img2[:-5]+'.srcphotrd'
img2_srsc_match = img2[:-5]+'.match.srscrd'
ra_1, dec_1,x_1,y_1,mag_1,merr_1,sky_1,serr_1,rapert_1 = np.loadtxt(img1_srcs,usecols=(0,1,2,3,4,5,6,7,8),unpack=True)
ra_2, dec_2,x_2,y_2,mag_2,merr_2,sky_2,serr_2,rapert_2 = np.loadtxt(img2_srcs,usecols=(0,1,2,3,4,5,6,7,8),unpack=True)
img1_catalog = SkyCoord(ra = ra_1*u.degree, dec= dec_1*u.degree)
img2_catalog = SkyCoord(ra = ra_2*u.degree, dec= dec_2*u.degree)
id_img1, id_img2, d2d, d3d = img2_catalog.search_around_sky(img1_catalog,0.00005*u.deg)
print len(id_img1),len(id_img2),len(x_1),len(x_2)
ra_1 = ra_1[id_img1]
dec_1 = dec_1[id_img1]
x_1 = x_1[id_img1]
y_1 = y_1[id_img1]
mag_1 = mag_1[id_img1]
merr_1 = merr_1[id_img1]
sky_1 = sky_1[id_img1]
serr_1 = serr_1[id_img1]
rapert_1 = rapert_1[id_img1]
ra_2 = ra_2[id_img2]
dec_2 = dec_2[id_img2]
x_2 = x_2[id_img2]
y_2 = y_2[id_img2]
mag_2 = mag_2[id_img2]
merr_2 = merr_2[id_img2]
sky_2 = sky_2[id_img2]
serr_2 = serr_2[id_img2]
rapert_2 = rapert_2[id_img2]
with open(img1_srsc_match,'w+') as m1:
with open(img2_srsc_match,'w+') as m2:
with open('calibration.dat','w+') as cal:
for i,s in enumerate(ra_1):
print >> m1,ra_1[i], dec_1[i],x_1[i],y_1[i],mag_1[i],merr_1[i],sky_1[i],serr_1[i],rapert_1[i]
print >> m2,ra_2[i], dec_2[i],x_2[i],y_2[i],mag_2[i],merr_2[i],sky_2[i],serr_2[i],rapert_2[i]
print >> cal, i, x_1[i], y_1[i], rapert_1[i], mag_1[i], merr_1[i], x_2[i], y_2[i], rapert_2[i], mag_2[i], merr_2[i]
m1.close()
m2.close()
junk = open('match.reg',"w")
for i in range(len(ra_1)):
print >> junk, x_2[i], y_2[i]
junk.close()
def calc_calibrated_mags(apcor_g, cal_A_g, apcor_r, cal_A_r):
from matplotlib import gridspec
kg = 0.200
kr = 0.12
ki = 0.058
rdnoise = 6.5
# get the photometric calibration coefficients from Steven's help file <--
# or from the image header/fits table/ whatever
photcalFile = open('GCPair-F2_odi_g-no_help.txt')
photcal = photcalFile.read()
photcalLines = photcal.splitlines()
mu_gr = float(photcalLines[28].split()[5])
zp_gr = float(photcalLines[30].split()[4])
eps_gr = float(photcalLines[34].split()[5])
zp_r = float(photcalLines[36].split()[4])
amg = float(photcalLines[25].split()[5])
amr = float(photcalLines[26].split()[5])
photcalFile.close()
nid,gx,gy,g_i,g_ierr,rx,ry,r_i,r_ierr = np.loadtxt('calibration.dat',usecols=(0,1,2,4,5,6,7,9,10),unpack=True)
print apcor_g,apcor_r
g0 = g_i - (kg*amg) + apcor_g
r0 = r_i - (kr*amr) + apcor_r
gmr = mu_gr*(g0-r0) + zp_gr
r_mag = r0 + eps_gr*gmr + zp_r
g_mag = gmr + r_mag - cal_A_g
r_mag = r_mag - cal_A_r
gmr = g_mag - r_mag
print 'Median (g-r) :: g - r = {0:7.4f}'.format(np.median(gmr))
print 'Final number of phot-ed stars :: g = {0:5d} : r = {1:5d}'.format(len(g_mag),len(r_mag))
g_mag_lims = [g_mag[i] for i in range(len(g_mag)) if (g_ierr[i] >= 0.2)]
r_mag_lims = [r_mag[i] for i in range(len(r_mag)) if (r_ierr[i] >= 0.2)]
with open('calibrated_magstest.dat', 'w+') as f3:
for i in range(len(rx)) :
print >> f3, '{0:8.2f} {1:8.2f} {2:12.3f} {3:12.3f} {4:8.2f} {5:8.2f} {6:12.3f} {7:12.3f} {8:12.3f} '.format(gx[i],gy[i],g_mag[i],g_ierr[i],rx[i],ry[i],r_mag[i],r_ierr[i],gmr[i])
plt.clf()
fig = plt.figure(figsize=(10, 8))
fig.subplots_adjust(hspace=0)
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax0.scatter(gmr, r_mag, s=2, color='black', marker='o', edgecolors='none')
ax0.set_ylabel('$r$')
ax0.set_xlabel('$(g-r)$')
ax0.set_ylim(24,10)
ax0.set_xlim(-1,2)
ax1 = plt.subplot(gs[1])
ax1.scatter(r_ierr, r_mag, s=2, color='black', marker='o', edgecolors='none')
# ax1.set_ylabel('$r$')
ax1.set_xlabel('inst $r$ err.')
ax1.set_ylim(24,10)
ax1.set_xlim(-0.002,0.05)
plt.setp(ax1.get_yticklabels(), visible=False)
plt.tight_layout()
plt.savefig("GCPF2_CMD.pdf")