-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits_upsampleCube.jl
137 lines (105 loc) · 4.08 KB
/
fits_upsampleCube.jl
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
using CFITSIO
using WCS
using ImageTransformations
println("CFITSIO version: ", libcfitsio_version())
bpx = [8, 16, 32, 64, -32, -64, -64]
suffix = ["uint8", "int16", "int32", "int64", "float32", "float64", "float64_num_overflow"]
for (idx, sfx) in zip(bpx, suffix)
println("processing BITPIX = ", idx, " (", sfx, ")")
src = homedir() * "/NAO/FITS/cube_" * sfx * ".fits"
dst = homedir() * "/NAO/FITS/cube_" * sfx * "+.fits"
f = fits_open_diskfile(src)
num = fits_get_num_hdus(f)
println("Number of HDUs in the file: ", num)
for i = 1:num
hdu_type = fits_movabs_hdu(f, i)
println(i, ") hdu_type = ", hdu_type)
end
bitpix, = fits_read_keyword(f, "BITPIX")
println("BITPIX = ", bitpix)
bitpix = parse(Int64, bitpix)
if bitpix != idx
println("bitpix must be == $idx")
close(f)
continue
end
width, = fits_read_keyword(f, "NAXIS1")
height, = fits_read_keyword(f, "NAXIS2")
depth, = fits_read_keyword(f, "NAXIS3")
width = parse(Int64, width)
height = parse(Int64, height)
depth = parse(Int64, depth)
println("width: $(width), height: $(height), depth: $(depth)")
keysexist, morekeys = fits_get_hdrspace(f)
out = fits_clobber_file(dst)
for i = 1:keysexist
rec = fits_read_record(f, i)
name, value, comment = fits_read_keyn(f, i)
# println(rec)
# println(name, "|", value, "|", comment)
fits_write_record(out, rec)
end
# add the WCS keywords
wcs = WCSTransform(3;
cdelt=[-2.777777777778E-05, 2.777777777778E-05, -2.929537207031E+04],
ctype=["RA---SIN", "DEC--SIN", "FREQ"],
crpix=[1.510000000000E+02, 1.510000000000E+02, 1.0],
crval=[2.690886656602E+02, -2.195623208439E+01, 2.195706138430E+11])
# convert a WCSTransform to a FITS header
header = WCS.to_header(wcs)
println(header)
println("header length: ", length(header))
# divide the header into 80-character strings
nkeywords = Int(length(header) / 80)
for i = 1:nkeywords
rec = SubString(header, (i - 1) * 80 + 1, i * 80)
if !isempty(strip(rec))
println(rec, length(rec))
fits_write_record(out, String(rec))
end
end
fits_write_key(out, "RESTFRQ", 2.195632900000E+11, "Rest Frequency (Hz)")
fits_write_key(out, "SPECSYS", "LSRK", "Spectral reference frame")
fits_write_key(out, "BUNIT", "JY/BEAM", "Brightness (pixel) unit")
fits_write_key(out, "OBSRA", "2.690886656602E+02", "Right Ascension of observation")
fits_write_key(out, "OBSDEC", "-2.195623208439E+01", "Declination of observation")
fits_write_key(out, "OBJECT", "NUMBERS", "Name of observed object")
fits_write_key(out, "TELESCOP", "ALMA", "Telescope")
# the extra one is the <END> keyword
rec = fits_read_record(f, keysexist + 1)
fits_write_record(out, rec)
println("FITS image dimensions: ", fits_get_img_size(f))
# switch data array type based on BITPIX
if bitpix == 8
data = Array{UInt8}(undef, width, height)
elseif bitpix == 16
data = Array{Int16}(undef, width, height)
elseif bitpix == 32
data = Array{Int32}(undef, width, height)
elseif bitpix == 64
data = Array{Int64}(undef, width, height)
elseif bitpix == -32
data = Array{Float32}(undef, width, height)
elseif bitpix == -64
data = Array{Float64}(undef, width, height)
else
println("BITPIX must be one of [8, 16, 32, 64, -32, -64]")
close(f)
close(out)
continue
end
nelements = width * height
for frame = 1:depth
fpixel = [1, 1, frame, 1]
fits_read_pix(f, fpixel, nelements, data)
# calculate the mean and sum of data
integrated_intensity = sum(data)
mean_intensity = integrated_intensity / nelements
println("HDU $(frame): ", size(data))
println("intensity: int.: $(integrated_intensity), mean: $(mean_intensity)")
fits_write_pix(out, fpixel, width * height, data)
println("")
end
fits_close_file(f)
fits_close_file(out)
end