-
Notifications
You must be signed in to change notification settings - Fork 0
/
pybdsf_cataloger.py
89 lines (85 loc) · 4.43 KB
/
pybdsf_cataloger.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
import os,sys,re
import bdsf
def headless(inputfile):
''' Parse the list of inputs given in the specified file. (Modified from evn_funcs.py)'''
INPUTFILE = open(inputfile, "r")
control = {}
# a few useful regular expressions
newline = re.compile(r'\n')
space = re.compile(r'\s')
char = re.compile(r'\w')
comment = re.compile(r'#.*')
# parse the input file assuming '=' is used to separate names from values
for line in INPUTFILE:
if char.match(line):
line = comment.sub(r'', line)
line = line.replace("'", '')
(param, value) = line.split('=')
param = newline.sub(r'', param)
param = param.strip()
param = space.sub(r'', param)
value = newline.sub(r'', value)
value = value.replace(' ','').strip()
valuelist = value.split(',')
if len(valuelist) == 1:
if valuelist[0] == '0' or valuelist[0]=='1' or valuelist[0]=='2':
control[param] = int(valuelist[0])
else:
control[param] = str(valuelist[0])
else:
control[param] = ','.join(valuelist)
return control
inputs = headless('catalog_inputs.txt')
detection_threshold = float(inputs['S_N_ratio'])
postfix = str(inputs['postfix'])
shorthand = inputs['shorthand']
ds9 = inputs['ds9']
split_catalogues = inputs['split_catalogues']
def write_catalog_pybdsf(input_image,detection_threshold,shorthand):
if shorthand == 'True':
name = input_image.split('_MSSC')[0]
else:
name = input_image
img = bdsf.process_image(input_image, advanced_opts=True, group_by_isl=True, spline_rank=4, thresh_pix=detection_threshold,\
thresh='hard', thresh_isl=2.9)
# Write the source list catalog. File is named automatically.
img.write_catalog(format='csv', catalog_type='srl',clobber=True)
if ds9 == 'True':
img.write_catalog(format='ds9', catalog_type='srl',clobber=True)
# Write the residual image. File is name automatically.
img.export_image(outfile=name+'_gaus.residual.fits',img_type='gaus_resid',clobber=True)
# Write the model image. File name is specified.
img.export_image(outfile=name+'_gaus.model.fits',img_type='gaus_model',clobber=True)
img.export_image(outfile=name+'.rms.fits', img_type='rms', clobber=True)
#write_catalog_pybdsf('HDFA0002_MSSC_FG_NA_IM.fits',detection_threshold)
def combine_pybdsf(shorthand,postfix,catalog_list):
os.system('rm catalogue_PYBDSF_%s.csv' % postfix)
if os.path.isfile('catalogue_pybdsf_%s.csv' % postfix) == False:
s = 'Name_{0}, Source_id_{0}, Isl_id_{0}, RA_{0}, E_RA_{0}, DEC_{0}, E_DEC_{0}, Total_flux_{0}, E_Total_flux_{0}, Peak_flux_{0}, E_Peak_flux_{0}, RA_max_{0}, E_RA_max_{0}, DEC_max_{0}, E_DEC_max_{0}, Maj_{0}, E_Maj_{0}, Min_{0}, E_Min_{0}, PA_{0}, E_PA_{0}, Maj_img_plane_{0}, E_Maj_img_plane_{0}, Min_img_plane_{0}, E_Min_img_plane_{0}, PA_img_plane_{0}, E_PA_img_plane_{0}, DC_Maj_{0}, E_DC_Maj_{0}, DC_Min_{0}, E_DC_Min_{0}, DC_PA_{0}, E_DC_PA_{0}, DC_Maj_img_plane_{0}, E_DC_Maj_img_plane_{0}, DC_Min_img_plane_{0}, E_DC_Min_img_plane_{0}, DC_PA_img_plane_{0}, E_DC_PA_img_plane_{0}, Isl_Total_flux_{0}, E_Isl_Total_flux_{0}, Isl_rms_{0}, Isl_mean_{0}, Resid_Isl_rms_{0}, Resid_Isl_mean_{0}, S_Code_{0}\n'.format(postfix)
os.system('touch catalogue_PYBDSF_%s.csv' % postfix)
text_file = open('catalogue_PYBDSF_%s.csv' % postfix,'a')
text_file.write(s)
for file in catalog_list:
if file.endswith('.srl'):
lines = open('%s' % file).readlines()
if shorthand == 'True':
names = file[:8]+','
else:
names = file+','
if len(lines) > 6:
#detections = detections + [file]
#print names+names.join(lines[6:])
text_file.write(names+names.join(lines[6:]))
os.system('rm %s' % file)
catalog_list = []
for i in os.listdir('./'):
if i.endswith('.fits'):
write_catalog_pybdsf(i,detection_threshold,shorthand)
for file in os.listdir('./'):
if file.endswith('.srl'):
catalog_list = catalog_list + [file]
if split_catalogues == 'False':
combine_pybdsf(shorthand=shorthand,postfix=postfix,catalog_list=catalog_list)
else:
for i in catalog_list:
combine_pybdsf(shorthand=shorthand,postfix=i.split('.srl')[0],catalog_list=[i])