-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits_upsample3D.jl
142 lines (104 loc) · 3.81 KB
/
fits_upsample3D.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
138
139
140
141
142
using CFITSIO
using WCS
using ImageTransformations
println("CFITSIO version: ", libcfitsio_version())
src = homedir() * "/Downloads/numbers.fits"
dst = homedir() * "/Downloads/numbers_upsampled.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 != -32
println("bitpix must be == -32")
close(f)
exit()
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)")
# new dimensions
new_width = 10 * width
new_height = 10 * height
println("new width: $new_width, new_height: $new_height")
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)
if name == "NAXIS1"
fits_write_key(out, "NAXIS1", new_width, "")
continue
end
if name == "NAXIS2"
fits_write_key(out, "NAXIS2", new_height, "")
continue
end
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
global rec
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))
data = Array{Float32}(undef, width, height)
new_data = Array{Float32}(undef, new_width, new_height)
nelements = width * height
new_nelements = new_width * new_height
for frame = 1:depth
global new_data
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
new_data = Float32.(imresize(data, (new_width, new_height)))
# calculate the mean and sum of new_data
new_integrated_intensity = sum(new_data)
new_mean_intensity = new_integrated_intensity / new_nelements
println("HDU $(frame): ", size(data), "-->", size(new_data))
println("intensity: int.: $(integrated_intensity), mean: $(mean_intensity)")
println("new intensity: int.: $(new_integrated_intensity), mean: $(new_mean_intensity)")
fits_write_pix(out, fpixel, new_width * new_height, new_data)
println("")
end
println("upsampled size:", size(new_data))
fits_close_file(f)
fits_close_file(out)