-
Notifications
You must be signed in to change notification settings - Fork 0
/
rungalfit.py
409 lines (349 loc) · 18.8 KB
/
rungalfit.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
'''
REQUIRED MODULES:
pyds9
'''
#import pyds9
import os
from astropy.io import fits
def parse_galfit_results(galfit_outimage,asymflag=0,ncomp=1):
numerical_error_flag=0
if asymflag:
header_keywords=['1_XC','1_YC','1_MAG','1_RE','1_N','1_AR','1_PA','2_SKY','1_F1','1_F1PA','CHI2NU']
else:
header_keywords=['1_XC','1_YC','1_MAG','1_RE','1_N','1_AR','1_PA','2_SKY','CHI2NU']
if ncomp == 2:
header_keywords=['1_XC','1_YC','1_MAG','1_RE','1_N','1_AR','1_PA','2_XC','2_YC','2_MAG','2_RE','2_N','2_AR','2_PA','3_SKY','CHI2NU']
fit_parameters=[]
working_dir=os.getcwd()+'/'
image_header = fits.getheader(galfit_outimage,2)
for hkey in header_keywords:
s=str(image_header[hkey])
#print hkey
if s.find('[') > -1:
s=s.replace('[','')
s=s.replace(']','')
t=s.split('+/-')
values=(float(t[0]),0.)# fit and error
else:
t=s.split('+/-')
try:
values=(float(t[0]),float(t[1]))# fit and error
except ValueError:
# look for * in the string, which indicates numerical problem
if t[0].find('*') > -1:
numerical_error_flag=1
t[0]=t[0].replace('*','')
t[1]=t[1].replace('*','')
values=(float(t[0]),float(t[1]))# fit and error
except IndexError: # for CHI2NU
chi2nu=float(t[0])
continue
fit_parameters.append(values)
fit_parameters.append(numerical_error_flag)
fit_parameters.append(chi2nu)
return fit_parameters
class galfit:
def __init__(self,galname=None,image=None,sigma_image=None,psf_image=None,psf_oversampling=None,mask_image=None,xminfit=None,yminfit=None,xmaxfit=None,ymaxfit=None,convolution_size=None,magzp=None,pscale=None,convflag=1,constraintflag=1,fitallflag=False,ncomp=1,asym=False):
self.galname=galname
self.image=image
self.sigma_image=sigma_image
self.psf_image=psf_image
self.psf_oversampling=psf_oversampling
self.mask_image=mask_image
self.xminfit=xminfit
self.yminfit=yminfit
self.xmaxfit=xmaxfit
self.ymaxfit=ymaxfit
self.convolution_size=convolution_size
self.magzp=magzp
self.pscale=pscale
self.convflag=convflag
self.constraintflag=constraintflag
self.fitallflag=fitallflag
self.ncomp=ncomp
self.asymmetry=asym
if self.sigma_image == None:
self.sigma_image = 'none'
#print(xminfit,xmaxfit,yminfit,ymaxfit,convolution_size)
#print(self.xminfit,self.xmaxfit,self.yminfit,self.ymaxfit,self.convolution_size)
#print('psf_image = ',psf_image)
#print('self.fitall = ',self.fitallflag)
#print('***%%%%%%%%%%%%%%%%%')
def create_output_names(self):
if self.asymmetry:
output_image=str(self.galname)+'-'+ str(self.ncomp) +'Comp-galfit-out-asym.fits'
else:
output_image=str(self.galname)+'-'+ str(self.ncomp) +'Comp-galfit-out.fits'
self.output_image=output_image
# create galfit input file
self.galfile=str(self.galname)+'-galfit.input.'+str(self.ncomp)+'Comp'
def open_galfit_input(self):
self.galfit_input=open(self.galfile,'w')
def write_image_params(self):#,input_image,output_image,sigma_image,psf_image,psf_oversampling,mask_image,xminfit,xmaxfit,yminfit,ymaxfit,convolution_size,magzp,pscale,convflag=1,constraintflag=1,fitallflag=0):
self.galfit_input.write('# IMAGE PARAMETERS\n')
self.galfit_input.write('A) '+self.image+' # Input data image (FITS file)\n')
self.galfit_input.write('B) '+self.output_image+' # Name for the output image\n')
self.galfit_input.write('C) %s # Sigma image name (made from data if blank or "none") \n'%(self.sigma_image))
if self.convflag:
self.galfit_input.write('D) '+self.psf_image+' # Input PSF image and (optional) diffusion kernel\n')
self.galfit_input.write('E) %i # PSF oversampling factor relative to data\n'%(self.psf_oversampling))
if self.fitallflag:
self.galfit_input.write('F) # Pixel mask (ASCII file or FITS file with non-0 values)\n')
else:
self.galfit_input.write('F) '+self.mask_image+' # Pixel mask (ASCII file or FITS file with non-0 values)\n')
self.galfit_input.write('H) '+str(int(round(self.xminfit)))+' '+str(int(round(self.xmaxfit)))+' '+str(int(round(self.yminfit)))+' '+str(int(round(self.ymaxfit)))+' # Image region to fit (xmin xmax ymin ymax)\n')
if self.convflag:
self.galfit_input.write('I) '+str(int(round(self.convolution_size)))+' '+str(int(round(self.convolution_size)))+' # Size of convolution box (x y)\n')
self.galfit_input.write('J) %5.2f # Magnitude photometric zeropoint \n'%(self.magzp))
self.galfit_input.write('K) %6.5f %6.5f # Plate scale (dx dy) [arcsec/pix]\n'%(self.pscale,self.pscale))
self.galfit_input.write('O) regular # Display type (regular, curses, both)\n')
self.galfit_input.write('P) 0 # Create output image only? (1=yes; 0=optimize) \n')
self.galfit_input.write('S) 0 # Modify/create objects interactively?\n')
def set_sersic_params(self,xobj=None,yobj=None,mag=None,rad=None,nsersic=None,BA=None,PA=None,fitmag=1,fitcenter=1,fitrad=1,fitBA=1,fitPA=1,fitn=1,first_time=0):
self.xobj=xobj
self.yobj=yobj
self.mag=mag
self.rad=rad
self.nsersic=nsersic
self.BA=BA
self.PA=PA
self.fitmag=fitmag
self.fitn=fitn
self.fitcenter=fitcenter
self.fitrad=fitrad
self.fitBA=fitBA
self.fitPA=fitPA
print('inside rungalfit, fitBA = ',self.fitBA)
if first_time:
self.xobj0=xobj
self.yobj0=yobj
self.mag0=mag
self.rad0=rad
self.nsersic0=nsersic
self.BA0=BA
self.PA0=PA
self.fitmag0=fitmag
self.fitn0=fitn
self.fitcenter0=fitcenter
self.fitrad0=fitrad
self.fitBA0=fitBA
self.fitPA0=fitPA
self.asymmetry0=self.asymmetry
def set_sersic_params_comp2(self,xobj=None,yobj=None,mag=None,rad=None,nsersic=None,BA=None,PA=None,fitmag=1,fitcenter=1,fitrad=1,fitBA=1,fitPA=1,fitn=1,first_time=0):
self.xobj2=xobj
self.yobj2=yobj
self.mag2=mag
self.rad2=rad
self.nsersic2=nsersic
self.BA2=BA
self.PA2=PA
self.fitmag2=fitmag
self.fitn2=fitn
self.fitcenter2=fitcenter
self.fitrad2=fitrad
self.fitBA2=fitBA
self.fitPA2=fitPA
if first_time:
self.xobj02=xobj
self.yobj02=yobj
self.mag02=mag
self.rad02=rad
self.nsersic02=nsersic
self.BA02=BA
self.PA02=PA
self.fitmag02=fitmag
self.fitn02=fitn
self.fitcenter02=fitcenter
self.fitrad02=fitrad
self.fitBA02=fitBA
self.fitPA02=fitPA
self.asymmetry02=self.asymmetry
def reset_sersic_params(self):
self.xobj=self.xobj0
self.yobj=self.yobj0
self.mag=self.mag0
self.rad=self.rad0
self.nsersic=self.nsersic0
self.BA=self.BA0
self.PA=self.PA0
self.fitmag=self.fitmag0
self.fitn=self.fitn0
self.fitcenter=self.fitcenter0
self.fitrad=self.fitrad0
self.fitBA=self.fitBA0
self.fitPA=self.fitPA0
self.asymmetry=self.asymmetry0
def set_sky(self,sky):
self.sky=sky
def write_sersic(self,objnumber,profile, nsersic=None):
self.galfit_input.write(' \n')
self.galfit_input.write('# Object number: %i \n'%(objnumber))
self.galfit_input.write(' 0) %s # Object type \n'%(profile))
self.galfit_input.write(' 1) %8.1f %8.1f %i %i # position x, y [pixel] \n'%(self.xobj,self.yobj,int(self.fitcenter),int(self.fitcenter)))
self.galfit_input.write(' 3) %5.2f %i # total magnitude \n'%(self.mag,self.fitmag))
self.galfit_input.write(' 4) %8.2f %i # R_e [Pixels] \n'%(self.rad,self.fitrad))
print('sersic n, fitsersicn = ',self.nsersic,self.fitn)
if nsersic == None:
self.galfit_input.write(' 5) %5.2f %i # Sersic exponent (deVauc=4, expdisk=1) \n'%(self.nsersic,int(self.fitn)))
else:
self.galfit_input.write(' 5) %5.2f %i # Sersic exponent (deVauc=4, expdisk=1) \n'%(nsersic,int(self.fitn)))
print('BA, fitBA = ',self.BA,self.fitBA)
self.galfit_input.write(' 9) %5.2f %i # axis ratio (b/a) \n'%(self.BA,int(self.fitBA)))
self.galfit_input.write('10) %5.2f %i # position angle (PA) [Degrees: Up=0, Left=90] \n'%(self.PA,int(self.fitPA)))
if self.asymmetry:
self.galfit_input.write('F1) 0.0001 0.00 1 1 # azim. Fourier mode 1, amplitude & phase angle \n')
self.galfit_input.write(" Z) 0 # Output option (0 = residual, 1 = Don't subtract) \n")
def write_sersic_BD(self):
########################
# write disk profile
########################
self.galfit_input.write(' \n')
self.galfit_input.write('# Object number: 1 \n')
self.galfit_input.write(' 0) sersic # Object type \n')
self.galfit_input.write(' 1) %8.1f %8.1f %i %i # position x, y [pixel] \n'%(self.xobj,self.yobj,int(self.fitcenter),int(self.fitcenter)))
self.galfit_input.write(' 3) %5.2f %i # total magnitude \n'%(self.mag,self.fitmag))
self.galfit_input.write(' 4) %8.2f %i # R_e [Pixels] \n'%(self.rad,self.fitrad))
self.galfit_input.write(' 5) %5.2f %i # Sersic exponent (deVauc=4, expdisk=1) \n'%(self.nsersic,0))
self.galfit_input.write(' 9) %5.2f %i # axis ratio (b/a) \n'%(self.BA,int(self.fitBA)))
self.galfit_input.write('10) %5.2f %i # position angle (PA) [Degrees: Up=0, Left=90] \n'%(self.PA,int(self.fitPA)))
if self.asymmetry:
self.galfit_input.write('F1) 0.0001 0.00 1 1 # azim. Fourier mode 1, amplitude & phase angle \n')
self.galfit_input.write(" Z) 0 # Output option (0 = residual, 1 = Don't subtract) \n")
########################
# write bulge profile
########################
self.galfit_input.write(' \n')
self.galfit_input.write('# Object number: 2 \n')
self.galfit_input.write(' 0) sersic # Object type \n')
self.galfit_input.write(' 1) %8.1f %8.1f %i %i # position x, y [pixel] \n'%(self.xobj2,self.yobj2,int(self.fitcenter),int(self.fitcenter)))
self.galfit_input.write(' 3) %5.2f %i # total magnitude \n'%(self.mag2,self.fitmag))
self.galfit_input.write(' 4) %8.2f %i # R_e [Pixels] \n'%(self.rad2,self.fitrad))
self.galfit_input.write(' 5) %5.2f %i # Sersic exponent (deVauc=4, expdisk=1) \n'%(self.nsersic2,1)) # allow n to vary
self.galfit_input.write(' 9) %5.2f %i # axis ratio (b/a) \n'%(self.BA2,int(self.fitBA)))
self.galfit_input.write('10) %5.2f %i # position angle (PA) [Degrees: Up=0, Left=90] \n'%(self.PA2,int(self.fitPA)))
if self.asymmetry:
self.galfit_input.write('F1) 0.0001 0.00 1 1 # azim. Fourier mode 1, amplitude & phase angle \n')
self.galfit_input.write(" Z) 0 # Output option (0 = residual, 1 = Don't subtract) \n")
def write_sky(self,objnumber):
self.galfit_input.write(' \n')
self.galfit_input.write('# Object number: %i \n'%(objnumber))
self.galfit_input.write(' 0) sky # Object type \n')
self.galfit_input.write(' 1) %8.1f 1 # sky background at center of fitting region [ADUs] \n'%(self.sky))
self.galfit_input.write(' 2) 0 0 # dsky/dx (sky gradient in x) \n')
self.galfit_input.write(' 3) 0 0 # dsky/dy (sky gradient in y) \n')
self.galfit_input.write(" Z) 0 # Output option (0 = residual, 1 = Don't subtract) \n")
def write_input_file(self):
self.create_output_names()
self.open_galfit_input()
print('in rungalfit.run_galfit, self.psf_image = ',self.psf_image)
self.write_image_params()
if (self.ncomp == 1):
self.write_sersic(1,'sersic')
self.write_sky(2)
elif (self.ncomp == 2):
self.write_sersic_BD()
self.write_sky(3)
if (self.fitallflag):
print('%%%%%%%%%%%%%% HEY %%%%%%%%%%%%%')
print('I think fitall is true, just sayin...')
self.fitall()
self.close_input_file()
def run_galfit(self,displayflag=False):
self.write_input_file()
#print 'self.fitall = ',self.fitall
s = 'galfit '+self.galfile
print('run the following: ',s)
errno=os.system(s)
self.galfit_flag=1
image_id=str(self.galname)+'-'
self.galfit_log=image_id+str(self.ncomp)+'Comp-fit.log'
s='cp fit.log '+self.galfit_log
os.system(s)
self.galfit_out=image_id+str(self.ncomp)+'Comp'+'-galfit.01'
s='mv galfit.01 '+self.galfit_out
try:
os.rename('galfit.01',self.galfit_out)
except:
print("appears like galfit did not complete")
#galflag[j]=0
self.galfit_flag=0
return
if displayflag:
self.display_results()
def fitall(self,mindistance=8):
os.system('cp '+homedir+'research/LocalClusters/sextractor/default.param .')
os.system('cp '+homedir+'research/LocalClusters/sextractor/default.nnw .')
s='sex '+self.image+'[1] -c '+homedir+'research/LocalClusters/sextractor/default.sex.24um.galfitsource -WEIGHT_TYPE MAP_RMS -WEIGHT_IMAGE '+self.sigma_image+' -CATALOG_NAME '+self.galname+'test.cat -CATALOG_TYPE ASCII_HEAD'
os.system(s)
# read in SE table to get x,y for sources
#fname=self.galname+'test.fits'
fname=self.galname+'test.cat'
print('FITALL CATALOG NAME = ',fname)
objnumber=2
profile='sersic'
try:
se=atpy.Table(fname,type='ascii')
print('found ',len(se.X_IMAGE),' sources on the field of ',self.galname)
nearbyobjflag=sqrt((se.X_IMAGE-self.xobj)**2+(se.Y_IMAGE-self.yobj)**2) > mindistance
for k in range(len(se.X_IMAGE)):
if nearbyobjflag[k]:
objnumer=objnumber+1
self.add_simple_sersic_object(objnumber,profile,se.X_IMAGE[k],se.Y_IMAGE[k],se.MAG_BEST[k],se.FLUX_RADIUS[k,0],2,se.B_IMAGE[k]/se.A_IMAGE[k],se.THETA_IMAGE[k])
except AttributeError:
print('WARNING: no sources detected in image!')
input=('hit any key to continue \n')
def add_simple_sersic_object(self,objnumber,profile,x,y,mag,rad,nsersic,BA,PA):
self.galfit_input.write(' \n')
self.galfit_input.write('# Object number: %i \n'%(objnumber))
self.galfit_input.write(' 0) %s # Object type \n'%(profile))
self.galfit_input.write(' 1) %8.1f %8.1f 1 1 # position x, y [pixel] \n'%(x,y))
self.galfit_input.write(' 3) %5.2f 1 # total magnitude \n'%(mag))
self.galfit_input.write(' 4) %8.2f 1 # R_e [Pixels] \n'%(rad))
self.galfit_input.write(' 5) %5.2f 1 # Sersic exponent (deVauc=4, expdisk=1) \n'%(nsersic))
self.galfit_input.write(' 9) %5.2f 1 # axis ratio (b/a) \n'%(BA))
self.galfit_input.write('10) %5.2f 1 # position angle (PA) [Degrees: Up=0, Left=90] \n'%(PA))
self.galfit_input.write(" Z) 0 # Output option (0 = residual, 1 = Don't subtract) \n")
def close_input_file(self):
self.galfit_input.close()
def print_params(self):
print('CURRENT INPUTS: \n mag = %5.2f %i \n Re = %5.2f %i \n n = %5.2f %i\n B/A = %5.2f %i \n PA = %5.2f %i \n fitall = %i \n fitcenter = %i \n'%(self.mag,self.fitmag,self.rad,self.fitrad,self.nsersic,self.fitn,self.BA,self.fitBA,self.PA,self.fitPA,self.fitallflag,self.fitcenter))
def print_galfit_results(self,image):
if self.asymmetry:
header_keywords=['1_XC','1_YC','1_MAG','1_RE','1_N','1_AR','1_PA','2_SKY','1_F1','1_F1PA','ERROR','CHI2NU']
else:
header_keywords=['1_XC','1_YC','1_MAG','1_RE','1_N','1_AR','1_PA','2_SKY','ERROR','CHI2NU']
if self.ncomp == 2:
header_keywords=['1_XC','1_YC','1_MAG','1_RE','1_N','1_AR','1_PA','2_XC','2_YC','2_MAG','2_RE','2_N','2_AR','2_PA','3_SKY','ERROR','CHI2NU']
t=parse_galfit_results(image, ncomp = self.ncomp,asymflag=self.asymmetry)
for i in range(len(header_keywords)):
try:
print('%6s : %5.2f +/- %5.2f'%(header_keywords[i],t[i][0],t[i][1]))
except:
print('%6s : %5.2f'%(header_keywords[i],t[i]))
def edit_params_menu(self):
flag=str(input('What is wrong?\n o = nearby object (toggle fitall) \n c = recenter \n f = hold values fixed \n a = toggle asymmetry parameter \n R = reset to original values \n g = go (run galfit) \n x=quit \n '))
return flag
def toggle_fitall(self):
self.fitallflag=not(self.fitallflag)
def toggle_asymmetry(self):
self.asymmetry=not(self.asymmetry)
def print_fix_menu(self):
self.print_params()
flag3=str(input('What do you want to hold fixed/toggle?\n n = fix sersic index \n r = fix Re \n b = fix B/A \n p = PA \n c = center \n f = use constraint file \n R = reset to original values \n g = go (run galfit) \n x=quit \n '))
return flag3
def fix_n(self):
n=float(input('sersic exponent = '))
self.nsersic=n
self.fitn=not(self.fitn)
def fix_rad(self):
self.fitrad=not(self.fitrad)
def fix_BA(self):
self.fitBA=not(self.fitBA)
print(self.fitBA, self.BA)
def fix_PA(self):
self.fitPA=not(self.fitPA)
def fix_center(self):
self.fitcenter=not(self.fitcenter)
def add_constraint_file(self):
self.constraintflag=not(self.constraintflag)