-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_v8.py
More file actions
463 lines (325 loc) · 12.9 KB
/
stack_v8.py
File metadata and controls
463 lines (325 loc) · 12.9 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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import sys
import time
import numpy as np
from astropy.io import fits
import scipy as sc
import matplotlib.pyplot as plt
from scipy import spatial
from pylab import *
from math import e
hz2ghz = 10.**9
ion()
def uvstack(fits_input, ln = [], freqs = [], width=1, file_out = 'uvs_out.fits', clob=False):
'''
Given a uvfits file, spectral line frequencies/channels and expected line width stacks spectral lines for each visibility
Inputs:
fits_input: string
enter input uvfits file e.g. 'input.fits'
file_out: string
output fits file name e.g. uvsim_out.fits
freqs: float
line frequencies in GHz (default = [])
width: int
width of spectral line in channels.
ln: array[int]
list of channel number where spectral lines are centred
clob: bool
To overwrite existing file with same name as output
'''
#
# data_cube, header_data_cube = fits.getdata(fits_input, 0, header=True)
# file = fits.open('test.fits', mode='append')
file = fits.open(fits_input) # opening uvfits file
nfits = np.shape(file)[0]
flag2 = 0
if (nfits > 1):
print'Not a UVFITS file:',nfits,'HDUs present'
print'Will append them to FITS file'
flag2 = 2
header1 = file[0].header # extracting header for HDU
ln, freqs, flag = chkvals(header1, ln, freqs, width) # function to check if frequencies/channel numbers are consistent with the FITS file
freq0 = header1['CRVAL4'] # extracting the central frequency
n_chan = header1['NAXIS4'] # extracting central channel value
if(flag < 1):
return
uvtable = file[0].data # extracting uvtable
vis = uvtable['DATA'] # extracting visibiities
n_vis = np.shape(vis)[0]
n_lines = np.shape(ln)[0] # number of lines to stack
# line width in channels - make sure its even.
u_cent = uvtable['UU'] # U, V, W values given based on central frequency.
v_cent = uvtable['VV']
w_cent = uvtable['WW']
base = uvtable['BASELINE']
date1 = uvtable['DATE']
date2 = uvtable['_DATE']
new_uvtable = np.zeros((n_vis*n_lines,)) # new empty table in shape of stacked visibilities
new_vis = np.zeros((n_vis*n_lines,1,1,1,width,1,3)) # this is FILE-SPECIFIC - beware.
# empty u,v,w,baseline, date arrays,
uu = []
vv = []
ww = []
base2 = []
date21 = []
date22 = []
for fq in freqs:
uu.extend(np.multiply(freq0/fq, u_cent)) # appending scaled u,v,w values to the empty visi file
vv.extend(np.multiply(freq0/fq, v_cent))
ww.extend(np.multiply(freq0/fq, w_cent))
base2.extend(base)
date22.extend(date2)
date21.extend(date1)
for i in range(n_vis*n_lines): # for all visibilities, copying over select channels
j = i/n_vis
jj = i%n_vis
low, high = ln[j] - width/2, ln[j] + width/2
if (width%2!=0):
high = high + 1 # fixing the shape for odd width
if(flag2 > 0): # file format is not uv
tmp1 = vis[jj,:,:,:,low:high,:,:]
else:
tmp1 = vis[jj,:,:,low:high,:,:]
new_vis[i] = tmp1 # appends to the new visibility
pars = [uu,vv,ww,base2,date21,date22]
pscal = header1['PSCAL*']
pzero = header1['PZERO*']
for i in range(0,5): # shifting anc scaling parameters
pars[i] = np.array([x - pzero[i] for x in pars[i]])
pars[i]/= pscal[i]
# creating new GroupData
pnames=['UU','VV','WW','BASELINE','DATE','DATE'] # note that 2 DATE names will result in one DATE and one _DATE columns.
pdata=pars
tmp2= fits.GroupData(new_vis,bitpix=-32, pardata=pdata, parnames=pnames) # bitpix -64 will not be understood by GILDAS.
hdu = fits.GroupsHDU(tmp2, header1)
# writing data to fits file
hdu_array = file
hdu_array[0] = hdu
tmp3 = fits.HDUList(hdu_array)
tmp3.writeto('temp.fits', clobber=True) # the GroupdHDU must be the first one.
# patch-job to include BSCALE/BZERO values which are not included in copying the header over.
tmp4 = fits.open('temp.fits')
tmp4[0].header['BSCALE'] = header1['BSCALE']
tmp4[0].header['BZERO'] = header1['BZERO']
tmp4.writeto(file_out, clobber=clob)
# c = fits.open('uvs_out.fits')
# print c[0].data
##################
# IMAGE STACKING #
##################
def imstack(fits_input, file_out = 'imgs_out.fits', width=1, ln = [137,267,611,683, 842] ,clob=True, plt=0):
'''
Given an image fits file, spectral line frequencies/channels and expected line width, it stacks spectral lines on the image plane.
Inputs:
fits_input: string
enter input uvfits file e.g. 'input.fits'
file_out: string
output fits file name e.g. 'uvsim_out.fits'
freqs: float
line frequencies in GHz (defaule = [])
width: int
width of spectral line in channels.
ln: array[int]
list of channel number where spectral lines are centred
plt: int [default = 0]
To plot stacked image or not
'''
flag = 2
img_cube, header = fits.getdata(fits_input, 0, header=True)
n_chan = np.shape(img_cube)[1]
xl = np.shape(img_cube)[2]
yl = np.shape(img_cube)[3]
flag = chkvals_img(ln,width, n_chan)
if (flag < 1):
return
img_f = np.zeros((1,width,xl,yl))
lns = np.shape(ln)[0]
for l in ln:
img_f = img_f + img_cube[:,l-width/2.:l+width/2,:,:]
img_f = img_f/lns
if (plt > 0):
implt = plt.imshow(np.mean(img_f, axis=0))
plt.colorbar()
plt.show()
header['NAXIS3'] = (width/2) + (width/2)
fits.writeto(file_out, img_f, header, clobber=clob)
# function to extract spectra line information from uvfits fiel header
def chkvals(header, ln, freqs, width):
'''
Checks consistency between line channels, line frequencies, and values in header.
Inputs:
header: FITS header object
header from PrimaryHDU with visibility data
freqs: float
line frequencies in GHz (defaule = [])
width: int
width of spectral line in channels.
ln: array[int]
list of channel number where spectral lines are centred
'''
flag = 2
freq0 = header['CRVAL4']
n_chan = header['NAXIS4']
ref = header['CRPIX4']
freq_res = header['CDELT4']
freq_low = freq0 - ref*freq_res
wd = freq_res*n_chan
print '\n' , 'Central freq (GHz): ', freq0/hz2ghz, '\n' ,'Nchan:', n_chan, '\n' ,'Ref. channel: ', ref, '\n' ,'Freq. res. (Hz):', freq_res, '\n','Lower freq(Hz): ', freq_low, '\n' ,'Bandwidth (Hz): ', wd ,'\n'
for i in range(np.shape(ln)[0]-1):
if (ln[i] > ln[i+1]):
print('Channels unsorted. Getting out! Channels must be in order. Try again. ')
flag = -1
for i in range(np.shape(ln)[0]-1):
if (ln[i] + width/2 > ln[i+1] - width/2):
print'Warning - overlap between stacks',i,i+1
if (ln==[] and freqs == []):
print '\n' ,'Channels/ frequencies of expected lines not provided - will use defaults'
ln = [137,267,611,683,842]
freqs=np.multiply(hz2ghz, [80.683, 81.190, 82.533, 82.815, 83.436])
print'\n' ,'channels' , ln
print'\n' ,'frequencies (in GHz)', freqs
# will assume the HCN lines if none are given
elif (ln == []):
print"Channels of expected lines not provided - will calculate using frequencies"
ln = [int(round((i - freq_low)*n_chan/wd)) for i in freqs]
print'Found channels', ln, 'from frequencies', freqs
elif (freqs == []):
print"Frequencies of expected lines not provided - calculating channel numbers using frequencies"
freqs = [((1.*i/n_chan)*wd + freq_low) for i in ln]
print'Calculated frequencies (Hz)', freqs, 'from channels' , ln
if(np.max(ln) + width/2 > n_chan or np.min(ln) - width/2 < 0):
print'Invalid channel, width or frequency list given, check input'
print(np.max(ln), n_chan, width)
flag = -1
return ln, freqs, flag
def chkvals_img(ln, width, n_chan):
'''
Checks consistency between line channels and line width for image stacking.
Inputs:
width: int
width of spectral line in channels.
ln: array[int]
list of channel number where spectral lines are centred
'''
flag = 0
for i in range(np.shape(ln)[0]-1):
if (ln[i] > ln[i+1]):
print('Channels unsorted. Getting out! Channels must be in order. Try again. ')
flag = -1
for i in range(np.shape(ln)[0]-1):
if (ln[i] + width/2 > ln[i+1] - width/2):
print'Warning - overlap between stacks',i,i+1
if(np.max(ln) + width/2 > n_chan or np.min(ln) - width/2 < 0):
print'Invalid channel, width or frequency list given, check input'
print(np.max(ln), n_chan, width)
flag = -1
return flag
def uvsim(fits_input, file_out = 'uvsim_out.fits', width=50, ln = [200, 500, 800, 1500], mod = 0.2, centre = 1150, clob=True):
'''
Given a uvfits file, with a spectral line of width = <width>, centred on <centre>, this code insets the spectral linear at other channels, listed in <ln>. The amplitude is modulated by <mod>
Inputs :
fits_input: string
enter input uvfits file
file_out: string
output fits file name, default 'uvsim_out.fits'
width: int
width of spectral line in channels.
ln: array([int])
list of channels where spectral line is to be inserted
mod: float
amplitude scale from original line
centre: int
channel where spectral line is centred
'''
# channels 36 to 60, from 1215, to 1099 in the other one.
# data_cube, header_data_cube = fits.getdata(fits_input, 0, header=True)
# file = fits.open('test.fits', mode='append')
file = fits.open(fits_input) # open uvfits file
header1 = file[0].header # extract header
uvtable = file[0].data # extract data
vis = uvtable['DATA'] # extract visibilities from data
n_chan = np.shape(file[0])[3] # assuming shape of uvfits file to be something like (1, 1, 1, 100, 1, 3, 0)
### beware - the shape may be different for different uvfits files
n_vis = np.shape(vis)[0] # number of visibilities
vis0 = vis
# line width in channels - make sure its even. check what happens when its odd ?
for chan in ln:
if ((chan < 100 + centre) and (chan > centre - 100)):
print" Beware : adding spectral line within 200 channels of the original.."
for i in range(0,n_vis): # for each visibility, stacking the spectral lines.
tmp1 = vis[i][0][0][0][chan-width/2:chan+width/2] # extracted original weights - keeping them the same.
wts = tmp1[:,:,2]
tmp2 = vis[i][0][0][0][centre-width/2:centre +width/2] # Extracting spectral line
re = tmp2[:,:,0]
im = tmp2[:,:,1]
tmp3 = np.zeros((width,1,3))
tmp3[:,:,0] = re
tmp3[:,:,1] = im
tmp3[:,:,2] = wts
#tmp3[:,:,2] = np.zeros((np.shape(re)))
vis[i][0][0][0][chan-width/2:chan+width/2]+= np.multiply(tmp3,mod)
uu = uvtable['UU'] # copying over all the data to create the new fits file
vv = uvtable['VV']
ww = uvtable['WW']
base = uvtable['BASELINE']
date1 = uvtable['DATE']
date2 = uvtable['_DATE']
# uu_given = (uu_orig + pzero)* pscal
# unscaling and unshifting the given u,v,w visibilities.
pars = [uu,vv,ww,base,date1,date2]
pscal = header1['PSCAL*']
pzero = header1['PZERO*']
for i in range(0,5): # shifting and scaling parameters
pars[i] = np.array([x - pzero[i] for x in pars[i]])
pars[i]/= pscal[i]
pnames=['UU','VV','WW','BASELINE','DATE','DATE']
pdata=pars
tmp4= fits.GroupData(vis,bitpix=-32, pardata=pdata, parnames=pnames) # making data structure
hdu = fits.GroupsHDU(tmp4, header1) # attaching header
tmp5 = fits.HDUList([hdu, file[1]]) # creating HDU
tmp5.writeto('temp_sim.fits', clobber=True) # the GroupdHDU must be the first one. # writing out file.
tmp6 = fits.open('temp_sim.fits')
tmp6[0].header['BSCALE'] = header1['BSCALE']
tmp6[0].header['BZERO'] = header1['BZERO']
tmp6.writeto(file_out, clobber=clob)
def img_avg(fits_input, file_out = 'imgs_out.fits', width = -1, ln = [-1, -1], clob = 0):
'''
Given an image fits file, spectral line frequencies/channels and expected line width, it stacks spectral lines on the image plane.
Inputs:
fits_input: string
enter input uvfits file e.g. 'input.fits'
file_out: string
output fits file name e.g. 'uvsim_out.fits'
freqs: float
line frequencies in GHz (defaule = [])
width: int
width of spectral line in channels.
ln: array[int]
list of channel number where spectral lines are centred
'''
img_cube, header = fits.getdata(fits_input, 0, header=True)
nchan = np.shape(img_cube)[1]
xl = np.shape(img_cube)[2]
yl = np.shape(img_cube)[3]
if ((width < 0 or width > nchan) and ln[0]<0 or ln[1]< 0):
print 'Error - no valid range provided'
return
elif (width < 0):
nchanf = 1
img_f = np.mean(img_cube[:,ln[0]:ln[1],:,:],1) # averaging along axis 1
flag = 1
elif(ln == []):
nchanf = nchan/width + 1
img_f = np.zeros((1,nchanf,xl,yl))
for i in range(0, nchanf-1):
img_f[:,i,:,:] = np.mean(img_cube[:,i*width:(i+1)*width,:,:],1) # averaging along axis 1
# img_f[nchanf] = np.mean(img_cube[:,nchanf*width:nchan,:,:],1)
flag = 2
print(np.shape(img_f))
if (plt > 0):
implt = plt.imshow(np.mean(img_f, axis=0))
plt.colorbar()
plt.show()
header['NAXIS3'] = nchanf
fits.writeto(file_out, img_f, header, clobber = clob)
return img_f
# function to extract spectra line information from uvfits fiel header