-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
338 lines (294 loc) · 12.1 KB
/
function.py
File metadata and controls
338 lines (294 loc) · 12.1 KB
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
import os, datetime, ntpath, alipy, glob
from pyraf import iraf
import numpy as np
# from pyraf.iraf import noao, imred, ccdred, darkcombine, flatcombine, ccdproc ,astutil, setjd, setairmass, asthedit, digiphot, apphot, phot, ptools, txdump, artdata, imgeom
from astropy.time import Time
from time import gmtime, strftime
try:
import cosmics
except:
print("Can not load cosmics")
raise SystemExit
from astropy.table import Table
from astropy import table
def readResultFile(self, filename, starID, ifilter, apIndex):
try:
result_file = Table.read(filename,
format='ascii.commented_header',
header_start=-1,
fill_values=[('INDEF', np.nan)])
result_unique_by_keys = table.unique(result_file, keys='FILTER')
results = result_file[(result_file['FILTER'] == ifilter) & (result_file['id'] == int(starID))]
# ifilter_index = result_unique_by_keys.colnames.index('FILTER') + 1
print "Result file is succesfuly read."
return results
except:
print "Result file did not read"
return False
def headerRead(filename, field):
res = iraf.hedit(filename, field, ".", Stdout=1)
if res != []:
res = res[0]
return res.split("=")[1].replace(" ","")
else:
return ""
def headerWrite(filename, field, text):
try:
hd= iraf.images.imutil.hedit
hd (filename, field, text, add="yes", verify="no", show="no", update="yes")
return True
print("Hedit succeed")
except:
return False
print("Hedit failed")
def headerDel(filename, field):
try:
hd= iraf.images.imutil.hedit
hd (filename, field, delete="yes", verify="no", show="no")
return True
print("Hedit succeed")
except:
return False
print("Hedit failed")
def zeroCombine(fileList, out, com="median", rej="none", cty=""):
zc = iraf.noao.imred.ccdred.zerocombine
zc.input = "@%s" %(fileList)
zc.output = out
zc.combine = com
zc.reject = rej
zc.ccdtype = cty
zc.process = "no"
try:
zc._runCode()
print("zerocombine succeed.")
return True
except:
print("zerocombine failed.")
return False
def darkCombine(fileList, out, com="median", rej="none", cty="", scl="exposure"):
dc = iraf.noao.imred.ccdred.darkcombine
dc.input = "@%s" %(fileList)
dc.output = out
dc.combine = com
dc.reject = rej
dc.ccdtype = cty
dc.scale = scl
dc.process = "no"
try:
dc._runCode()
print("darkcombine succeed.")
return True
except:
print("darkcombine failed.")
return False
def flatCombine(fileList, out, com="median", rej="none", cty="", sub="yes"):
fc = iraf.noao.imred.ccdred.flatcombine
fc.input = "@%s" %(fileList)
fc.output = "%s/flat_" %(out)
fc.process = "no"
fc.combine = com
fc.reject = rej
fc.ccdtype = cty
fc.subsets = sub
try:
fc ._runCode()
print("flatcombine succeed.")
return True
except:
print("flatcombine failed.")
return False
def calibration(image, bias, dark, flat, odir, cty="", sub="yes"):
if sub =="no":
headerWrite(image, "tmp", headerRead(image, "SUBSET"))
headerWrite(image, "SUBSET", "MYRAF")
headerWrite(flat, "tmp", headerRead(flat, "SUBSET"))
headerWrite(flat, "SUBSET", "MYRAF")
bia, dar, fla = "yes", "yes", "yes"
if bias == "": bia = "no"
if dark == "": dar = "no"
if flat == "": fla = "no"
bia2="%s(%s)" %(bia, bias)
dar2="%s(%s)" %(dar, dark)
fla2="%s(%s)" %(fla, flat)
print("Starting Calibration:Image=%s Bias=%s, Dark=%s, Flat=%s" %(ntpath.basename(str(image)), bia2, dar2, fla2))
cp = iraf.noao.imred.ccdred.ccdproc
print str(odir)
cp.images = str(image)
cp.output = "%s/%s" %(str(odir), ntpath.basename(str(image)))
cp.ccdtype = cty
cp.fixpix = "no"
cp.overscan = "no"
cp.trim = "no"
cp.zerocor = bia
cp.darkcor = dar
cp.flatcor = fla
cp.zero = bias
cp.dark = dark
cp.flat = flat
try:
cp(images = str(image))
headerWrite("%s/%s" %(str(odir), ntpath.basename(str(image))), "MYRafCAL", "Calibrated Via MYRaf V2.0 Beta @ %s" %(datetime.datetime.utcnow()))
print("Calibration succeed.")
if sub =="no":
print "Geri gel"
headerWrite(image, "SUBSET", headerRead(image, "tmp"))
headerDel(image, "tmp")
headerWrite("%s/%s" %(odir, ntpath.basename(str(image))), "SUBSET", headerRead("%s/%s" %(odir, ntpath.basename(str(image))), "tmp"))
headerDel("%s/%s" %(odir, ntpath.basename(str(image))), "tmp")
headerWrite(flat, "SUBSET", headerRead(flat, "tmp"))
headerDel(flat, "tmp")
return True
except:
if sub =="no":
print "Geri gel2"
headerWrite(image, "SUBSET", headerRead(image, "tmp"))
headerDel(image, "tmp")
headerWrite("%s/%s" %(odir, ntpath.basename(str(image))), "SUBSET", headerRead("%s/%s" %(odir, ntpath.basename(str(image))), "tmp"))
headerDel("%s/%s" %(odir, ntpath.basename(str(image))), "tmp")
headerWrite(flat, "SUBSET", headerRead(flat, "tmp"))
headerDel(flat, "tmp")
print("Calibration failed.")
return False
def autoAlign(self, inFile, refImage, outFile, visu=False):
print("image = %s, ref = %s " %(inFile, refImage))
mkh=iraf.artdata.mkheader
directory = os.path.dirname("%s/alipy_out/" %(self.HOME))
if not os.path.exists(directory):
os.popen("rm -rf %s/alipy_out/" %(self.HOME))
try:
images_to_align = sorted(glob.glob(inFile))
ref_image = refImage
identifications = alipy.ident.run(ref_image, images_to_align, visu=visu)
outputshape = alipy.align.shape(ref_image)
for id in identifications:
if id.ok == True:
print "%20s : %20s, flux ratio %.2f" % (id.ukn.name, id.trans, id.medfluxratio)
alipy.align.affineremap(id.ukn.filepath, id.trans, shape=outputshape)
alipy_out = "%s/alipy_out/%s_affineremap.fits" %(self.HOME, ntpath.basename(str(inFile)).split(".")[0])
mkh(alipy_out, inFile)
headerWrite(alipy_out, "MYRafALI", "Aligned Via MYRaf V2.0 Beta using Alipy @ %s" %(datetime.datetime.utcnow()))
os.popen("mv -f %s %s/%s" %(alipy_out, outFile, ntpath.basename(str(inFile))))
print("alipy succeed.")
return True
except Exception as e:
print(e)
return False
def manAlign(inFile, x, y, outFile):
imsh = iraf.images.imgeom.imshift
try:
print("%s image will align via x:%s y:%s" %(ntpath.basename(str(inFile)), x, y))
imsh (inFile, outFile, int(x), int(y))
return True
except:
return False
def JD(inFile, OBS, date="mydt-obs", time = "mytm-obs", r="ra", d = "dec", epo="epoch", expTime="exptime"):
print("JD calculation for %s image" %(ntpath.basename(str(inFile))))
print("JD F(%s) OBSERVAT(%s) DATE(my-date) TIME(time-obs) Ra(%s) Dec(%s) Epo(%s) Exp(%s)" %(inFile, OBS, r, d, epo, expTime))
try:
sd = iraf.noao.astutil.setjd
sd (inFile, observatory = str(OBS), date=date, time = time, ra=r, dec=d, epoch=epo, exposur=expTime, jd="jd", hjd="hjd", ljd="ljd")
return True
print("JD succeed.")
except:
return False
print("JD failed.")
def epoch(inFile, date, time):
try:
t = Time(strftime('%s %s' %(headerRead(inFile, "mydt-obs"), headerRead(inFile, "mytm-obs"))), format='iso', scale='utc')
return t.byear
except:
return False
def sideReal(self, inFile, OBS):
print("sidereal time calculation for %s image" %(ntpath.basename(str(inFile))))
try:
os.system('echo astcalc > ./tmp/st.cl')
os.system("echo \" observatory=\\\"%s\\\"\" >> %s/tmp/st.cl" %(OBS, self.HOME))
os.system("echo \" st=mst(@'mydt-obs',@'mytm-obs',obsdb(observatory,\\\"longitude\\\"))\" >> %s/tmp/st.cl" %(self.HOME))
os.system("echo quit >> %s/tmp/st.cl" %(self.HOME))
at = iraf.noao.astutil.asthedit
at(inFile, commands = "%s/tmp/st.cl" %(self.HOME), update = "yes", verbose = "yes")
os.popen("rm -rf %s/tmp/st.cl" %(self.HOME))
print("Sidereal calculator succeed.")
return True
except:
print("Sidereal calculator failed.")
return False
def airmass(inFile, OBS="observat", r="ra", d="dec", equ="epoch", s="st", u="mytm-obs", dat="mydt-obs", exp="exptime"):
print("Setairmass for %s image" %(ntpath.basename(str(inFile))))
try:
sam = iraf.setairmass
sam(inFile, observatory=OBS ,intype ="middle" ,outtype="effective", ra=r, dec=d, equinox=equ, st=s, ut=u, date=dat, exposur=exp)
print("Setairmass succeed.")
return True
except:
print("Setairmass failed.")
return False
def phot(self, inFile, outPath, cooFile, expTime = "exptime", Filter = "subset", centerBOX = "10.0", annulus = "25.0", dannulus = "5.0", apertur = "10,15,20,25,30", zmag = "25", airmass = "airmass", otime = "hjd", gain=""):
print("setParam started via: \n\timg=%s \n\tOutfile=%s \n\tcooFile=%s \n\texpTime=%s \n\tFilter=%s \n\tcenterBOX=%s \n\tannulus=%s \n\tdannulus=%s \n\tapertur=%s \n\tzmag=%s \n\tairmass=%s \n\tobstime=%s" %(inFile, outPath, cooFile, expTime, Filter, centerBOX, annulus, dannulus, apertur, zmag, airmass, otime))
try:
iraf.datapars.setParam("exposur", expTime)
iraf.datapars.setParam("filter", Filter)
iraf.datapars.setParam("airmass", airmass)
iraf.datapars.setParam("obstime", otime)
iraf.datapars.setParam("gain", gain)
iraf.datapars.saveParList(filename="%s/uparm/aptdataps.par" %(self.HOME))
iraf.centerpars.setParam("cbox", centerBOX)
iraf.centerpars.saveParList(filename="%s/uparm/aptcentes.par" %(self.HOME))
iraf.fitskypars.setParam("annulus", annulus)
iraf.fitskypars.setParam("dannulus", dannulus)
iraf.fitskypars.saveParList(filename="%s/uparm/aptfitsks.par" %(self.HOME))
iraf.photpars.setParam("apertur", apertur)
iraf.photpars.setParam("zmag", zmag)
iraf.photpars.saveParList(filename="%s/uparm/aptphot.par" %(self.HOME))
pt = iraf.phot
pt(inFile, coords = cooFile, output = outPath , interac = "no", verify = "no", Stdout=1)
return True
print("setParam succeed.")
except:
return False
print("setParam failed.")
def txDump(inFile, outFile, fields="id, otime, mag , merr, xairmass"):
print("txDump started.")
print inFile
try:
tx=iraf.txdump
tx (inFile, fields, "yes", Stdout= outFile)
return True
print("txDump succeed.")
except:
return False
print("txDump failed.")
def cosmicsClean(inFile, outFile, mask = "yes", Gain=2.2, Readnoise=10.0, Sigclip = 5.0, Sigfrac = 0.3, Objlim = 5.0):
try:
print ("MASK=%s, GAIN=%s, READNOISE=%s, SIGCLIP=%s, SIGFRAG=%s, OBJLIM=%s" %(mask, Gain, Readnoise, Sigclip, Sigfrac, Objlim))
fp, fn = os.path.split(outFile)
fn = fn.split(".")[0]
# Thanks for cosmics.py to Malte Tewes (mtewes (at) astro.uni-bonn.de), http://obswww.unige.ch/people/malte.tewes/cosmics_dot_py/
# Read the FITS :
array, header = cosmics.fromfits(inFile)
# array is a 2D numpy array
# Build the object :
c = cosmics.cosmicsimage(array, gain=Gain, readnoise=Readnoise, sigclip = Sigclip, sigfrac = Sigfrac, objlim = Objlim)
# There are other options, check the manual...
# Run the full artillery :
c.run(maxiter = 4)
# Write the cleaned image into a new FITS file, conserving the original header :
cosmics.tofits(outFile, c.cleanarray, header)
# If you want the mask, here it is :
if mask == "yes":
print("%s/%s_mask.fit" %(fp, fn))
cosmics.tofits("%s/%s_mask.fit" %(fp, fn), c.mask, header)
# (c.mask is a boolean numpy array, that gets converted here to an integer array)
print("cosmicsClean succeed.")
return True
except:
print("cosmicsClean failed.")
return False
def fit2gif(inFile, outFile):
try:
iraf.export(str(inFile), outFile, "gif", outband="zscale(i1)")
print "fit2gif succeed"
return True
except:
print "fit2gif failed"
return False