Skip to content

Commit 8379477

Browse files
authored
Merge pull request #354 from astro-informatics/cg_h5_fits_converter
Example fits conversion to HDF5 format
2 parents 450e49c + f31bea8 commit 8379477

File tree

2 files changed

+86
-68
lines changed

2 files changed

+86
-68
lines changed

data/atca/0332-391.h5

11.3 MB
Binary file not shown.

data/atca/readuvfits.py

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,86 @@
1-
import astropy.io.fits as fits
2-
import numpy as np
3-
4-
speed_of_light = 299792458. #m/s
5-
6-
def readData(filename, vis_name, pol1, pol2, filter):
7-
hdu = fits.open(filename)
8-
data = np.asfortranarray(hdu[0].data['DATA'])
9-
#reading in uvdata
10-
print("Header...")
11-
# print hdu[0].header
12-
print(hdu[0].data['DATA'].shape)
13-
no_chan = data.shape[3]
14-
no_pol = data.shape[4]
15-
16-
re = np.array([])
17-
im = np.array([])
18-
sigma = np.array([])
19-
u = np.array([])
20-
v = np.array([])
21-
w = np.array([])
22-
for chan in range(no_chan):
23-
freq = hdu[0].header["CRVAL4"] + (chan - no_chan * 0.5)* hdu[0].header["CDELT4"]
24-
25-
print(f"Loading frequency {freq} {chan}")
26-
flags1 = data[:, 0, 0, chan, pol1, 2] #I think this gives the flags....
27-
flags2 = data[:, 0, 0, chan, pol2, 2] #I think this gives the flags....
28-
flags = np.logical_or(np.logical_and((flags1> 0), (flags2 > 0)), not filter)
29-
print("Flagged visibilities: {}".format((~flags).sum()))
30-
#reading in weights and visibilities
31-
sigma = np.concatenate((sigma, np.sqrt((1/data[:, 0, 0, chan, pol1, 2][flags]) /2 + (1/data[:, 0, 0, chan, pol2, 2][flags]) /2)))
32-
re = np.concatenate((re, data[:, 0, 0, chan, pol1, 0][flags]/2 + data[:, 0, 0, chan, pol2, 0][flags]/2))
33-
im = np.concatenate((im, data[:, 0, 0, chan, pol1, 1][flags]/2 + data[:, 0, 0, chan, pol2, 1][flags]/2))
34-
print(data[:, 0, 0, chan, pol2, 2][~flags])
35-
#reading in uv-coordinates
36-
u = np.concatenate((u, hdu[0].data['UU'][flags] * freq))
37-
v = np.concatenate((v, hdu[0].data['VV'][flags] * freq))
38-
w = np.concatenate((w, hdu[0].data['WW'][flags] * freq))
39-
print("Total visibilities... ", u.shape, v.shape, w.shape, re.shape, im.shape, sigma.shape)
40-
41-
if filter:
42-
remove_nan = np.logical_and(~np.isnan(re) , ~np.isnan(complex(0, 1) *im))
43-
u = u[remove_nan]
44-
v = v[remove_nan]
45-
w = w[remove_nan]
46-
re = re[remove_nan]
47-
im = im[remove_nan]
48-
sigma = sigma[remove_nan]
49-
remove_zero = np.abs(re + complex(0, 1) *im) > 0
50-
u = u[remove_zero]
51-
v = v[remove_zero]
52-
w = w[remove_zero]
53-
re = re[remove_zero]
54-
im = im[remove_zero]
55-
sigma = sigma[remove_zero]
56-
57-
table = np.column_stack((u, v, w, re, im, sigma))
58-
print(table[0,:], u[0], v[0], w[0], re[0], im[0], sigma[0])
59-
print("Total visibilities... ", u.shape, v.shape, w.shape, re.shape, im.shape, sigma.shape)
60-
61-
np.savetxt(vis_name, table, delimiter = " ")
62-
63-
names = ["0332-391"]
64-
for name in names:
65-
uv_fits = f"{name}.uvfits"
66-
output_vis = f"{name}.vis"
67-
readData(uv_fits, output_vis, 0, 1, True)
68-
print(f"saved {name}.vis")
1+
import astropy.io.fits as fits
2+
import numpy as np
3+
4+
do_h5 = False
5+
try:
6+
import h5py
7+
do_h5 = True
8+
except ImportError:
9+
do_h5 = False
10+
11+
speed_of_light = 299792458. #m/s
12+
13+
def readData(filename, vis_name, pol1, pol2, filter):
14+
hdu = fits.open(filename)
15+
data = np.asfortranarray(hdu[0].data['DATA'])
16+
#reading in uvdata
17+
print("Header...")
18+
# print hdu[0].header
19+
print(hdu[0].data['DATA'].shape)
20+
no_chan = data.shape[3]
21+
no_pol = data.shape[4]
22+
23+
re = np.array([])
24+
im = np.array([])
25+
sigma = np.array([])
26+
u = np.array([])
27+
v = np.array([])
28+
w = np.array([])
29+
for chan in range(no_chan):
30+
freq = hdu[0].header["CRVAL4"] + (chan - no_chan * 0.5)* hdu[0].header["CDELT4"]
31+
32+
print(f"Loading frequency {freq} {chan}")
33+
flags1 = data[:, 0, 0, chan, pol1, 2] #I think this gives the flags....
34+
flags2 = data[:, 0, 0, chan, pol2, 2] #I think this gives the flags....
35+
flags = np.logical_or(np.logical_and((flags1> 0), (flags2 > 0)), not filter)
36+
print("Flagged visibilities: {}".format((~flags).sum()))
37+
#reading in weights and visibilities
38+
sigma = np.concatenate((sigma, np.sqrt((1/data[:, 0, 0, chan, pol1, 2][flags]) /2 + (1/data[:, 0, 0, chan, pol2, 2][flags]) /2)))
39+
re = np.concatenate((re, data[:, 0, 0, chan, pol1, 0][flags]/2 + data[:, 0, 0, chan, pol2, 0][flags]/2))
40+
im = np.concatenate((im, data[:, 0, 0, chan, pol1, 1][flags]/2 + data[:, 0, 0, chan, pol2, 1][flags]/2))
41+
print(data[:, 0, 0, chan, pol2, 2][~flags])
42+
#reading in uv-coordinates
43+
u = np.concatenate((u, hdu[0].data['UU'][flags] * freq))
44+
v = np.concatenate((v, hdu[0].data['VV'][flags] * freq))
45+
w = np.concatenate((w, hdu[0].data['WW'][flags] * freq))
46+
print("Total visibilities... ", u.shape, v.shape, w.shape, re.shape, im.shape, sigma.shape)
47+
48+
if filter:
49+
remove_nan = np.logical_and(~np.isnan(re) , ~np.isnan(complex(0, 1) *im))
50+
u = u[remove_nan]
51+
v = v[remove_nan]
52+
w = w[remove_nan]
53+
re = re[remove_nan]
54+
im = im[remove_nan]
55+
sigma = sigma[remove_nan]
56+
remove_zero = np.abs(re + complex(0, 1) *im) > 0
57+
u = u[remove_zero]
58+
v = v[remove_zero]
59+
w = w[remove_zero]
60+
re = re[remove_zero]
61+
im = im[remove_zero]
62+
sigma = sigma[remove_zero]
63+
64+
table = np.column_stack((u, v, w, re, im, sigma))
65+
print(table[0,:], u[0], v[0], w[0], re[0], im[0], sigma[0])
66+
print("Total visibilities... ", u.shape, v.shape, w.shape, re.shape, im.shape, sigma.shape)
67+
68+
if do_h5:
69+
h5_name = vis_name[:vis_name.rfind('.')] + '.h5'
70+
f = h5py.File(h5_name, 'w')
71+
f.create_dataset('u', data=u)
72+
f.create_dataset('v', data=v)
73+
f.create_dataset('w', data=w)
74+
f.create_dataset('re', data=re)
75+
f.create_dataset('im', data=im)
76+
f.create_dataset('sigma', data=sigma)
77+
f.close()
78+
print(f"saved {h5_name}")
79+
np.savetxt(vis_name, table, delimiter = " ")
80+
81+
names = ["0332-391"]
82+
for name in names:
83+
uv_fits = f"{name}.uvfits"
84+
output_vis = f"{name}.vis"
85+
readData(uv_fits, output_vis, 0, 1, True)
86+
print(f"saved {name}.vis")

0 commit comments

Comments
 (0)