forked from sPaMFouR/RedPipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombineFrames_2.py
210 lines (174 loc) · 8.87 KB
/
CombineFrames_2.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
# /usr/bin/env python
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx #
# xxxxxxxxxxxxxxxxxxxxxx----------------------------COMBINE IMAGES----------------------------xxxxxxxxxxxxxxxxxxxxxxx #
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx #
# ------------------------------------------------------------------------------------------------------------------- #
# Import Required Libraries
# ------------------------------------------------------------------------------------------------------------------- #
import os
import re
import glob
from pyraf import iraf
from astropy.io import fits
import dateutil.parser as dparser
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Telescope CCD Specifications & Image Header Keywords
# ------------------------------------------------------------------------------------------------------------------- #
read_noise = 4.87
ccd_gain = 1.22
EXPTIME_keyword = 'EXPTIME'
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Name Of The Object
# ------------------------------------------------------------------------------------------------------------------- #
OBJECT_name = '2018hna'
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Load Required IRAF Packages
# ------------------------------------------------------------------------------------------------------------------- #
iraf.noao(_doprint=0)
iraf.images(_doprint=0)
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Functions For File Handling
# ------------------------------------------------------------------------------------------------------------------- #
def remove_file(file_name):
"""
Removes the file "file_name" in the constituent directory.
Args:
file_name : Name of the file to be removed from the current directory
Returns:
None
"""
try:
os.remove(file_name)
except OSError:
pass
def group_similar_files(text_list, common_text, exceptions=''):
"""
Groups similar files based on the string 'common_text'. Writes the similar files
onto the list 'text_list' (only if this string is not empty) and appends the similar
files to a list 'python_list'.
Args:
text_list : Name of the output text file with names grouped based on the 'common_text'
common_text : String containing partial name of the files to be grouped
exceptions : String containing the partial name of the files that need to be excluded
Returns:
list_files : Python list containing the names of the grouped files
"""
list_files = glob.glob(common_text)
if exceptions != '':
list_exception = exceptions.split(',')
for file_name in glob.glob(common_text):
for text in list_exception:
test = re.search(text, file_name)
if test:
try:
list_files.remove(file_name)
except ValueError:
pass
list_files.sort()
if len(text_list) != 0:
with open(text_list, 'w') as f:
for file_name in list_files:
f.write(file_name + '\n')
return list_files
def text_list_to_python_list(text_list):
"""
Returns data in the file 'text_list' as a python_list.
Args:
text_list : Input file containing filenames
Returns:
python_list : List of all the elements in the file 'text_list'
Raises:
Error : File 'text_list 'Not Found
"""
if os.path.isfile(text_list):
with open(text_list, "r+") as f:
python_list = f.read().split()
return python_list
else:
print ("Error : File '{0}' Not Found".format(text_list))
def calculate_exptime(textlist_images):
"""
Calculates total exposure for the images in the list 'list_images'.
Args:
textlist_images : Text list of subject images which needs to be combined
Returns:
total_exptime : Total exposure time of the combined image
"""
list_images = text_list_to_python_list(textlist_images)
total_exptime = 0
if len(list_images) != 0:
for image in list_images:
file_header = fits.getheader(image)
exptime = file_header[EXPTIME_keyword]
total_exptime += int(exptime)
else:
print ("No Images In The Text List {0}".format(textlist_images))
return total_exptime
def edit_exptime(comb_image, total_exptime):
"""
Calculates total exposure for the images in the list 'list_images'.
Args:
comb_image : Image whose header has to be edited
total_exptime : Total exposure time of the combined image
Returns:
None
"""
hdulist = fits.open(comb_image, mode='update')
hdulist[0].header[EXPTIME_keyword] = int(total_exptime)
hdulist.close()
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Functions For Tasks In IRAF
# ------------------------------------------------------------------------------------------------------------------- #
def imcombine(textlist_images, type_combine='sum'):
"""
Combines images in the text list 'text_list_images' using algorithms based on the combine
type mentioned in the variable 'type_combine'.
Args:
textlist_images : Text list of subject images which needs to be combined
type_combine : Type of combining operation to be performed on pixels
Returns:
None
"""
task = iraf.images.immatch.imcombine
task.unlearn()
task.combine = type_combine # Type Of Combining Operation Performed On Pixels
task.reject = 'none' # Type Of Rejection Operation Performed On Pixels
task.project = 'no' # Combine Across The Highest Dimension Of The Image?
task.rdnoise = float(read_noise) # CCD Readout Noise (In e-)
task.gain = float(ccd_gain) # CCD Gain (In e-/ADU)
task(input='@' + textlist_images, output=textlist_images[5:] + ".fits")
total_exptime = calculate_exptime(textlist_images)
edit_exptime(textlist_images[5:] + '.fits', total_exptime)
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Groups Images To Be Combined - [ca_jul30_fbs_object-v1.fits]
# ------------------------------------------------------------------------------------------------------------------- #
list_dates = []
for file_name in group_similar_files('', common_text='*.fits'):
temp_name = file_name.split(OBJECT_name)[0]
date = dparser.parse(temp_name, fuzzy=True)
date = date.strftime('%Y-%m-%d')
list_dates.append(date)
list_dates = set(list_dates)
list_filters = ['U', 'B', 'V', 'R', 'I']
list_patterns = ['ca_' + date + '_cfbs_' + OBJECT_name + '-' + band.lower() for date in list_dates for band in list_filters]
list_list_comb = []
for pattern in list_patterns:
if len(group_similar_files('', pattern + '*')) != 0:
group_similar_files('list_' + pattern, common_text=pattern + '*')
list_list_comb.append('list_' + pattern)
print list_list_comb
# ------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------------------------------------------------------------------------- #
# Combines Images In The List 'list_comb'
# ------------------------------------------------------------------------------------------------------------------- #
for list_comb in list_list_comb:
imcombine(textlist_images=list_comb, type_combine='sum')
# for file_name in group_similar_files('', 'list_ca*'):
# remove_file(file_name)
# ------------------------------------------------------------------------------------------------------------------- #