-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratchpad.jl
228 lines (179 loc) · 5.03 KB
/
scratchpad.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
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
# reduce (integrate) the image
pixels = @distributed (+) for i = 1:depth
local val, pixels
try
fits_file = FITS(filepath)
pixels = reshape(read(fits_file[hdu_id], :, :, i, 1), (width, height))
val = sum(pixels)
close(fits_file)
catch e
println("i:$i::error: $e")
val = 0.0
# the type should be decided based on <bitpix>
pixels = zeros(Float32, width, height)
end
put!(progress, (i, val))
pixels
end
################################
pixels = zeros(Float32, width, height)
mask = map(isnan, pixels)
image = RemoteChannel(() -> Channel{Tuple}(32))
image_task = @async while true
try
thread_pixels, thread_mask = take!(image)
pixels .+= thread_pixels
mask .&= thread_mask
println("received (pixels,mask)")
catch e
println("image task completed")
break
end
end
# send back the result to the root
put!(results, (pixels, mask))
close(image)
wait(image_task)
fits.pixels = pixels
fits.mask = mask
################################
frame_mask = invalidate_pixel.(frame_pixels, datamin, datamax, ignrval)
# replace NaNs with 0.0
frame_pixels[frame_mask] .= 0.0
pixels .+= frame_pixels
mask .&= frame_mask
# pick out the valid values only
valid_mask = .!frame_mask
valid_pixels = frame_pixels[valid_mask]
pixel_sum = sum(valid_pixels)
pixel_count = length(valid_pixels)
if pixel_count > 0
frame_min, frame_max = extrema(valid_pixels)
mean_spectrum = pixel_sum / pixel_count
integrated_spectrum = pixel_sum * cdelt3
else
# no mistake here, reverse the min/max values
# so that global dmin/dmax can get correct values
# in the face of all-NaN frames
frame_min = prevfloat(typemax(Float32))
frame_max = -prevfloat(typemax(Float32))
mean_spectrum = 0.0
integrated_spectrum = 0.0
end
# insert back NaNs ahead of conversion to half-float (Float16)
frame_pixels[frame_mask] .= NaN32
######################################
# very slow:
pixel_count = 0
pixel_sum = 0.0
frame_min = prevfloat(typemax(Float32))
frame_max = -prevfloat(typemax(Float32))
# a single pass through the data
for idx in eachindex(frame_pixels)
x = frame_pixels[idx]
is_nan = !isfinite(x) || (x < datamin) || (x > datamax) || (x <= ignrval)
if is_nan
x = NaN32
else
pixel_count += 1
pixel_sum += x
pixels[idx] += x
mask[idx] |= true
if x < frame_min
frame_min = x
end
if x > frame_max
frame_max = x
end
end
end
if pixel_count > 0
mean_spectrum = pixel_sum / pixel_count
integrated_spectrum = pixel_sum * cdelt3
else
mean_spectrum = 0.0
integrated_spectrum = 0.0
end
######################################
function preloadFITS(fits::FITSDataSet)
if (fits.datasetid == "") || (fits.depth <= 1)
return
end
@everywhere function preload_fits(datasetid, width, height, idx)
for frame in idx
try
cache_dir = ".cache/" * datasetid
filename = cache_dir * "/" * string(frame) * ".bin"
io = open(filename) # default is read-only
compressed_pixels = Mmap.mmap(io, Matrix{Float16}, (width, height))
close(io)
# touch the data
pixel_sum = sum(compressed_pixels)
println("preloaded frame #$frame")
catch e
println(e)
end
end
end
for (w, value) in fits.indices
idx = findall(value)
println("worker $w::", idx, "($(length(idx)))")
@spawnat w preload_fits(fits.datasetid, fits.width, fits.height, idx)
end
end
########################################
using BSON
function serialize_to_bson(fits::FITSDataSet)
try
filename = ".cache/" * fits.datasetid * "/state.bson"
bson(filename, fits)
catch e
println("error serialising the FITS object::$e")
end
end
function deserialize_from_bson(datasetid)::FITSDataSet
filename = ".cache/" * datasetid * "/state.bson"
return BSON.load(filename)
end
###############################################
for (w, value) in fits.indices
idx = findall(value)
println("worker $w::", idx, "($(length(idx)))")
@spawnat w preload_frames(fits.datasetid, fits.width, fits.height, idx)
end
##############################################
using OpenEXR
try
filename = tempname() * ".exr"
image = Float16.(pixels)
channels = OpenEXR.WRITE_Y
OpenEXR.save_exr(filename, image, channels)
catch e
println(e)
end
# fetchVideoFrame
#=
try
# make an element-by-element write-enabled copy
pixels = deepcopy(frame_pixels)
catch e
println("frame_pixels: ", e)
return
end
mask = map(isnan, pixels)
try
# replace NaNs with 0.0
pixels[mask] .= 0.0
catch e
println("pixels: ", e)
return
end
try
# invert the mask
mask = .!mask
catch e
println("mask: ", e)
return
end
=#
#