-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitsToBp.py
64 lines (52 loc) · 2.16 KB
/
fitsToBp.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
#!/usr/bin/env python
# coding: utf-8
from astropy.io import fits
import adios2
import numpy as np
import os
import sys
import re
if (len(sys.argv) < 2):
raise Exception("Usage : python fitsToBp.py <filename> ")
filename = sys.argv[1]
os.system("rm -rf casa.bp")
with adios2.open("casa.bp", "w") as fh:
# Convert the FITS into a BP file
with fits.open(filename) as hdul:
for hdu in hdul:
hdr = hdu.header
for key in hdr.keys():
if(not key):
continue
if(re.search("^NAXIS.*$", key)):
continue
if((type(hdr[key]) is int) or (type(hdr[key]) is float)):
fh.write(key, np.array(hdr[key]))
else:
fh.write(key, str(hdr[key]))
data = hdu.data
# Endian conversion
# print(data.dtype.name + "printing data type")
data = np.ascontiguousarray(hdu.data, dtype=data.dtype.name)
# for 3d image cube, convert to 4d cube
if("NAXIS4" not in hdr.keys()):
fh.write("NAXIS", np.array(4))
fh.write("NAXIS1", np.array(hdr["NAXIS1"]))
fh.write("NAXIS2", np.array(hdr["NAXIS2"]))
fh.write("NAXIS3", np.array(hdr["NAXIS3"]))
fh.write("NAXIS4", np.array(1))
data = data.reshape(
1, hdr["NAXIS3"], hdr["NAXIS2"], hdr["NAXIS1"])
# for 4d image, where 3rd and 4th axis needs to be swapped
elif((hdr["NAXIS4"] != 1) and (hdr["NAXIS3"] == 1)):
fh.write("NAXIS", np.array(4))
fh.write("NAXIS1", np.array(hdr["NAXIS1"]))
fh.write("NAXIS2", np.array(hdr["NAXIS2"]))
fh.write("NAXIS3", np.array(hdr["NAXIS4"]))
fh.write("NAXIS4", np.array(1))
data = data.reshape(
1, hdr["NAXIS4"], hdr["NAXIS2"], hdr["NAXIS1"])
size = list(data.shape)
start = list(np.zeros(data.ndim, dtype=np.int32))
# print(data.size)
fh.write("data", data, size, start, size, end_step=True)