-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits_fitsio.jl
4200 lines (3339 loc) · 106 KB
/
fits_fitsio.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Base.Iterators: flatten
using Dates;
using DistributedArrays;
using Downloads;
using FITSIO;
using JSON;
using CodecLz4;
using Mmap;
using Serialization;
using Statistics;
using Images, ImageTransformations, Interpolations;
using ZfpCompression;
using PhysicalConstants.CODATA2018: c_0;
using ThreadsX;
using WCS;
const MADV_WILLNEED = 3
include("classifier.jl")
# include("IPP.jl")
include("ISPC.jl")
const NBINS = 1024
@enum Quality low medium high
@everywhere @enum Intensity MEAN INTEGRATED
@everywhere @enum Beam CIRCLE SQUARE # "square" is a reserved Julia function
const JOB_CHUNK = 16
struct ImageToneMapping
flux::String
pmin::Float32
pmax::Float32
med::Float32
sensitivity::Float32
ratio_sensitivity::Float32
white::Float32
black::Float32
end
@everywhere struct VideoToneMapping
flux::String
dmin::Float32
dmax::Float32
median::Float32
sensitivity::Float32
slope::Float32
white::Float32
black::Float32
end
finale(x) = @async println("Finalizing $(x.datasetid).")
mutable struct FITSDataSet
# metadata
datasetid::String
filesize::Integer
filepath::String
header::Any
headerStr::String
width::Integer
height::Integer
depth::Integer
is_optical::Bool
is_xray::Bool
has_frequency::Bool
has_velocity::Bool
frame_multiplier::Float32
_cdelt3::Float32
datamin::Float32
datamax::Float32
flux::String
ignrval::Float32
# pixels, spectrum
pixels::Any
mask::Any
compressed_pixels::Any
indices::Any
frame_min::Any
frame_max::Any
frame_median::Any
mean_spectrum::Any
integrated_spectrum::Any
# all-data statistics (needed by video streams)
dmin::Float32
dmax::Float32
data_median::Float32
data_mad::Float32
data_mad₊::Float32
data_mad₋::Float32
video_ready::Threads.Atomic{Bool}
# house-keeping
has_header::Threads.Atomic{Bool}
has_data::Threads.Atomic{Bool}
has_error::Threads.Atomic{Bool}
created::Threads.Atomic{Float64}
last_accessed::Threads.Atomic{Float64}
progress::Threads.Atomic{Int}
total::Threads.Atomic{Int}
elapsed::Threads.Atomic{Float64}
mutex::Any
function FITSDataSet()
new(
"",
0,
"",
Nothing,
"NULL",
0,
0,
0,
false,
false,
false,
false,
0.0,
0.0,
0.0,
0.0,
"",
0.0,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
-prevfloat(typemax(Float32)),
prevfloat(typemax(Float32)),
NaN32,
NaN32,
NaN32,
NaN32,
Threads.Atomic{Bool}(false),
Threads.Atomic{Bool}(false),
Threads.Atomic{Bool}(false),
Threads.Atomic{Bool}(false),
Threads.Atomic{Float64}(0.0),
Threads.Atomic{Float64}(0.0),
Threads.Atomic{Int}(0),
Threads.Atomic{Int}(0),
Threads.Atomic{Float64}(0.0),
ReentrantLock(),
)
end
function FITSDataSet(datasetid)
new(
datasetid,
0,
"",
Nothing,
"NULL",
0,
0,
0,
true,
false,
false,
false,
1.0,
1.0,
-prevfloat(typemax(Float32)),
prevfloat(typemax(Float32)),
"",
-prevfloat(typemax(Float32)),
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
Nothing,
-prevfloat(typemax(Float32)),
prevfloat(typemax(Float32)),
NaN32,
NaN32,
NaN32,
NaN32,
Threads.Atomic{Bool}(false),
Threads.Atomic{Bool}(false),
Threads.Atomic{Bool}(false),
Threads.Atomic{Bool}(false),
Threads.Atomic{Float64}(datetime2unix(now())),
Threads.Atomic{Float64}(datetime2unix(now())),
Threads.Atomic{Int}(0),
Threads.Atomic{Int}(0),
Threads.Atomic{Float64}(0.0),
ReentrantLock(),
)
end
end
function update_timestamp(fits::FITSDataSet)
fits.last_accessed[] = datetime2unix(now())
end
function update_progress(fits::FITSDataSet, total::Integer)
fits.elapsed[] = datetime2unix(now()) - fits.created[]
Threads.atomic_add!(fits.progress, 1)
fits.total[] = total
update_timestamp(fits)
end
function serialize_fits(fits::FITSDataSet)
global FITS_CACHE
n = length(workers())
try
lock(fits.mutex)
filename =
FITS_CACHE *
Base.Filesystem.path_separator *
fits.datasetid *
Base.Filesystem.path_separator *
"state.jls"
io = open(filename, "w+")
serialize(io, n)
serialize(io, fits.datasetid)
serialize(io, fits.filesize)
serialize(io, fits.filepath)
serialize(io, fits.header)
serialize(io, fits.headerStr)
serialize(io, fits.width)
serialize(io, fits.height)
serialize(io, fits.depth)
serialize(io, fits.is_optical)
serialize(io, fits.is_xray)
serialize(io, fits.has_frequency)
serialize(io, fits.has_velocity)
serialize(io, fits.frame_multiplier)
serialize(io, fits._cdelt3)
serialize(io, fits.datamin)
serialize(io, fits.datamax)
serialize(io, fits.flux)
serialize(io, fits.ignrval)
if fits.depth == 1
serialize(io, fits.pixels)
serialize(io, fits.mask)
end
# depth > 1
# skipping fits.pixels (DArray does not serialize well)
# skipping fits.mask (DArray does not serialize well)
# skipping fits.compressed_pixels (Futures do not serialize)
serialize(io, fits.indices)
serialize(io, fits.frame_min)
serialize(io, fits.frame_max)
serialize(io, fits.frame_median)
serialize(io, fits.mean_spectrum)
serialize(io, fits.integrated_spectrum)
serialize(io, fits.dmin)
serialize(io, fits.dmax)
serialize(io, fits.data_median)
serialize(io, fits.data_mad)
serialize(io, fits.data_mad₊)
serialize(io, fits.data_mad₋)
serialize(io, fits.video_ready)
serialize(io, fits.has_header)
serialize(io, fits.has_data)
serialize(io, fits.has_error)
# skipping fits.created
# skipping fits.last_accessed
if fits.depth == 1
serialize(io, fits.progress)
end
serialize(io, fits.total)
# skipping fits.elapsed
# skipping fits.mutex
close(io)
catch e
println("error serialising the FITS object::$e")
finally
unlock(fits.mutex)
end
end
function deserialize_fits(datasetid)
global FITS_CACHE
fits = FITSDataSet(datasetid)
filename =
FITS_CACHE *
Base.Filesystem.path_separator *
fits.datasetid *
Base.Filesystem.path_separator *
"state.jls"
io = open(filename)
n = length(workers())
if deserialize(io) != n
println("The number of parallel processes does not match. Invalidating the cache.")
close(io)
dirname = FITS_CACHE * Base.Filesystem.path_separator * fits.datasetid
# rm(dirname, recursive = true)
# use a distributed function to empty the cache directory
ras = [@spawnat w remove_fits_cache(dirname) for w in workers()]
wait.(ras)
error("The number of parallel processes does not match. Invalidating the cache.")
end
fits.datasetid = deserialize(io)
fits.filesize = deserialize(io)
fits.filepath = deserialize(io)
if !isfile(fits.filepath)
error("$(fits.filepath) cannot be accessed. Invalidating the cache.")
end
fits.header = deserialize(io)
fits.headerStr = deserialize(io)
fits.width = deserialize(io)
fits.height = deserialize(io)
fits.depth = deserialize(io)
fits.is_optical = deserialize(io)
fits.is_xray = deserialize(io)
fits.has_frequency = deserialize(io)
fits.has_velocity = deserialize(io)
fits.frame_multiplier = deserialize(io)
fits._cdelt3 = deserialize(io)
fits.datamin = deserialize(io)
fits.datamax = deserialize(io)
fits.flux = deserialize(io)
fits.ignrval = deserialize(io)
if fits.depth == 1
fits.pixels = deserialize(io)
fits.mask = deserialize(io)
end
# depth > 1
# skipping fits.pixels (DArray does not serialize well)
# skipping fits.mask (DArray does not serialize well)
# skipping fits.compressed_pixels
fits.indices = deserialize(io)
fits.frame_min = deserialize(io)
fits.frame_max = deserialize(io)
fits.frame_median = deserialize(io)
fits.mean_spectrum = deserialize(io)
fits.integrated_spectrum = deserialize(io)
fits.dmin = deserialize(io)
fits.dmax = deserialize(io)
fits.data_median = deserialize(io)
fits.data_mad = deserialize(io)
fits.data_mad₊ = deserialize(io)
fits.data_mad₋ = deserialize(io)
fits.video_ready = deserialize(io)
fits.has_header = deserialize(io)
fits.has_data = deserialize(io)
fits.has_error = deserialize(io)
# skipping fits.created
# skipping fits.last_accessed
if fits.depth == 1
fits.progress = deserialize(io)
end
fits.total = deserialize(io)
# skipping fits.elapsed
# skipping fits.mutex
close(io)
return fits
end
function serialize_to_file(fits::FITSDataSet)
global FITS_CACHE
try
filename =
FITS_CACHE * Base.Filesystem.path_separator * fits.datasetid * "/state.jls"
serialize(filename, fits)
catch e
println("error serialising the FITS object::$e")
end
end
function deserialize_from_file(datasetid)
global FITS_CACHE
filename = FITS_CACHE * Base.Filesystem.path_separator * datasetid * "/state.jls"
return deserialize(filename)
end
function get_progress(fits::FITSDataSet)
progress = 0.0
if fits.total[] > 0
progress = 100.0 * Float64(fits.progress[]) / Float64(fits.total[])
end
return progress, fits.elapsed[]
end
function has_header(fits::FITSDataSet)::Bool
return fits.has_header[]
#=
has_header = false
lock(fits.mutex)
has_header = fits.has_header
unlock(fits.mutex)
return has_header
=#
end
function has_data(fits::FITSDataSet)::Bool
return fits.has_data[]
end
function has_error(fits::FITSDataSet)::Bool
return fits.has_error[]
end
function has_video(fits::FITSDataSet)::Bool
return fits.video_ready[]
end
function dataset_exists(datasetid::String, fits_objects, fits_lock)::Bool
key_exists = false
lock(fits_lock)
try
key_exists = haskey(fits_objects, datasetid)
finally
unlock(fits_lock)
end
return key_exists
end
function insert_dataset(dataset::FITSDataSet, fits_objects, fits_lock)
lock(fits_lock)
try
datasetid = dataset.datasetid
fits_objects[datasetid] = dataset
catch e
println("Failed to insert a dataset: $e")
finally
unlock(fits_lock)
end
end
function get_dataset(datasetid::String, fits_objects, fits_lock)::FITSDataSet
local dataset::FITSDataSet
lock(fits_lock)
try
dataset = fits_objects[datasetid]
catch e
dataset = FITSDataSet()
println("Failed to retrieve a dataset: $e")
finally
unlock(fits_lock)
end
return dataset
end
function get_frequency_range(fits::FITSDataSet)
local crval3, cdelt3, crpix3
header = fits.header
# any errors will be propagated back and handled higher up
crval3 = header["CRVAL3"]
cdelt3 = header["CDELT3"]
crpix3 = header["CRPIX3"]
c = c_0.val # [m/s]
f1 = f2 = NaN
if fits.has_velocity
# we need the rest frequency too
restfrq = NaN # default value
try
restfrq = header["RESTFRQ"]
catch e
end
try
restfrq = header["RESTFREQ"]
catch e
end
if !isfinite(restfrq)
error("Could not obtain the rest frequency.")
end
v1 =
crval3 * fits.frame_multiplier + cdelt3 * fits.frame_multiplier * (1.0 - crpix3)
v2 =
crval3 * fits.frame_multiplier +
cdelt3 * fits.frame_multiplier * (fits.depth - crpix3)
f1 = restfrq * sqrt((1.0 - v1 / c) / (1.0 + v1 / c))
f2 = restfrq * sqrt((1.0 - v2 / c) / (1.0 + v2 / c))
end
if fits.has_frequency
f1 =
crval3 * fits.frame_multiplier + cdelt3 * fits.frame_multiplier * (1.0 - crpix3)
f2 =
crval3 * fits.frame_multiplier +
cdelt3 * fits.frame_multiplier * (fits.depth - crpix3)
end
if !isfinite(f1) || !isfinite(f2)
error("Could not obtain {f1,f2}.")
end
freq_start = min(f1, f2) / 1.0E9 # [Hz -> GHz]
freq_end = max(f1, f2) / 1.0E9 # [Hz -> GHz]
return (freq_start, freq_end)
end
function process_header(fits::FITSDataSet)
println("FITS header #records: $(length(fits.header))")
for i = 1:length(fits.header)
record = fits.header[i]
# comments evaluate to 'nothing'
if !isnothing(record)
if typeof(record) != String
continue
end
if occursin("ASTRO-F", record)
fits.is_optical = true
fits.flux = "logistic"
end
if occursin("HSCPIPE", record)
fits.is_optical = true
fits.flux = "ratio"
end
record = lowercase(record)
if occursin("suzaku", record) ||
occursin("hitomi", record) ||
occursin("x-ray", record)
fits.is_optical = false
fits.is_xray = true
fits.flux = "legacy"
fits.ignrval = -1.0
end
end
end
# further examine the header
try
record = fits.header["FRAMEID"]
if occursin("SUPM", record) || occursin("MCSM", record)
fits.is_optical = true
fits.flux = "ratio"
end
catch e
end
try
fits.ignrval = Float32(fits.header["IGNRVAL"])
catch e
end
try
fits.datamin = Float32(fits.header["DATAMIN"])
catch e
end
try
fits.datamax = Float32(fits.header["DATAMAX"])
catch e
end
try
record = lowercase(fits.header["TELESCOP"])
if occursin("alma", record) || occursin("vla", record) || occursin("ska", record)
fits.is_optical = false
end
if occursin("nro45", record)
fits.is_optical = false
fits.flux = "ratio"
end
if occursin("chandra", record)
fits.is_optical = false
fits.is_xray = true
end
if occursin("kiso", record)
fits.is_optical = true
fits.flux = "ratio"
end
catch e
end
try
ctype3 = lowercase(fits.header["CTYPE3"])
if occursin("f", ctype3)
fits.has_frequency = true
end
if occursin("v", ctype3)
fits.has_velocity = true
end
catch e
end
try
cunit3 = lowercase(fits.header["CUNIT3"])
if occursin("hz", cunit3)
fits.has_frequency = true
fits.frame_multiplier = 1.0E0
end
if occursin("khz", cunit3)
fits.has_frequency = true
fits.frame_multiplier = 1.0E3
end
if occursin("mhz", cunit3)
fits.has_frequency = true
fits.frame_multiplier = 1.0E6
end
if occursin("ghz", cunit3)
fits.has_frequency = true
fits.frame_multiplier = 1.0E9
end
if occursin("thz", cunit3)
fits.has_frequency = true
fits.frame_multiplier = 1.0E12
end
if occursin("m/s", cunit3)
fits.has_velocity = true
fits.frame_multiplier = 1.0E0
end
if occursin("km/s", cunit3)
fits.has_velocity = true
fits.frame_multiplier = 1.0E3
end
catch e
end
try
cdelt3 = Float32(fits.header["CDELT3"])
if fits.has_velocity
fits._cdelt3 = cdelt3 * fits.frame_multiplier / 1000.0
else
fits._cdelt3 = 1.0
end
catch e
end
end
# callable in the main thread (invisible to workers...)
function invalidate(x, datamin, datamax, ignrval)::Bool
val = Float32(x)
!isfinite(val) || (val < datamin) || (val > datamax) || (val <= ignrval)
end
# only visible to workers...
@everywhere function invalidate_pixel(x, datamin, datamax, ignrval)::Bool
val = Float32(x)
!isfinite(val) || (val < datamin) || (val > datamax) || (val <= ignrval)
end
@everywhere function load_fits_frames(
datasetid,
jobs,
progress,
path,
width,
height,
datamin,
datamax,
ignrval,
cdelt3,
hdu_id,
global_pixels::DArray,
global_mask::DArray,
)
global FITS_CACHE
local frame, frame_pixels, frame_mask
local valid_pixels, valid_mask
local frame_min, frame_max, frame_median
local mean_spectrum, integrated_spectrum
pixels = zeros(Float32, width, height)
mask = map(!isnan, pixels)
compressed_frames = Dict{Int32,Matrix{Float16}}()
# compressed_pixels = zeros(Float16, width, height)
try
fits_file = FITS(path)
hdu = fits_file[hdu_id]
naxes = ndims(hdu)
while true
frame_start, frame_end = take!(jobs)
# process a chunk of frames
for frame = frame_start:frame_end
# check #naxes, only read (:, :, frame) if and when necessary
if naxes >= 4
frame_pixels = reshape(read(hdu, :, :, frame, 1), (width, height))
else
frame_pixels = reshape(read(hdu, :, :, frame), (width, height))
end
frame_mask = invalidate_pixel.(frame_pixels, datamin, datamax, ignrval)
# replace NaNs with 0.0
frame_pixels[frame_mask] .= 0
# @async or Threads.@spawn ..., or nothing
zfp_compress_pixels(datasetid, frame, Float32.(frame_pixels), frame_mask)
pixels .+= frame_pixels
mask .&= frame_mask
# pick out the valid values only
valid_mask = .!frame_mask
valid_pixels = @view frame_pixels[valid_mask]
pixel_sum = sum(valid_pixels)
pixel_count = length(valid_pixels)
if pixel_count > 0
frame_min, frame_max = ThreadsX.extrema(valid_pixels)
frame_median = median(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))
frame_median = NaN32
mean_spectrum = 0.0
integrated_spectrum = 0.0
end
# convert to half-float (Float16)
compressed_pixels = map(x -> Float16(x), frame_pixels)
# insert back NaNs
compressed_pixels[frame_mask] .= NaN16
# store the data
compressed_frames[frame] = compressed_pixels
#=
# save the half-float (Float16) data
cache_dir =
FITS_CACHE * Base.Filesystem.path_separator * datasetid
filename =
cache_dir *
Base.Filesystem.path_separator *
string(frame) *
".f16"
io = open(filename, "w+")
write(io, compressed_pixels)
# serialize(io, compressed_pixels)
close(io)
=#
# send back the reduced values
put!(
progress,
(
frame,
frame_min,
frame_max,
frame_median,
mean_spectrum,
integrated_spectrum,
myid(),
),
)
# println("processing frame #$frame")
GC.safepoint()
end
end
catch e
# println("task $(myid())/$frame::error: $e")
finally
# copy (pixels,mask) into the distributed arrays (global_pixels,global_mask)
try
# obtain worker-local references
local_pixels = localpart(global_pixels)
local_mask = localpart(global_mask)
# negate the mask so that <true> indicates valid pixels
mask = .!mask
local_pixels[:, :] = pixels
local_mask[:, :] = mask
cache_dir = FITS_CACHE * Base.Filesystem.path_separator * datasetid
filename =
cache_dir * Base.Filesystem.path_separator * string(myid()) * ".pixels"
serialize(filename, pixels)
filename = cache_dir * Base.Filesystem.path_separator * string(myid()) * ".mask"
serialize(filename, mask)
catch e
println("DArray::$e")
end
println("loading FITS cube finished")
end
# do not wait, trigger garbage collection *NOW*
GC.gc()
return compressed_frames
end
function download_progress(dl_total, dl_now)
print(" $dl_now / $dl_total bytes\r")
end
@everywhere function make_fits_cache(cache_dir::String)
try
if !isdir(cache_dir)
mkdir(cache_dir)
end
catch _
end
end
@everywhere function remove_fits_cache(cache_dir::String)
try
if isdir(cache_dir)
rm(cache_dir, recursive = true)
end
catch _
end
end
function loadFITS(fits::FITSDataSet, filepath::String, url::Union{Missing,String} = missing)
global FITS_CACHE
if fits.datasetid == ""
fits.has_error[] = true
return
end
if Sys.iswindows()
filepath = replace(filepath, "/" => "\\")
end
if !ismissing(url)
try
# download a FITS file from <uri>, save it under <filepath>
Downloads.download(url, filepath, progress = download_progress, verbose = true)
catch err
println(err)
fits.has_error[] = true
return
end
end
println("loading $filepath::$(fits.datasetid)")
local f, width::Integer, height::Integer, depth::Integer
try
fits.filesize = filesize(filepath)
f = FITS(filepath)
println(f)
catch e
println(e)
fits.has_error[] = true
return
end
# it is safe to set the filepath, the FITS file has been opened without an error
fits.filepath = filepath
cache_dir = FITS_CACHE * Base.Filesystem.path_separator * fits.datasetid
try
for w in workers()
@spawnat w make_fits_cache(cache_dir)
end
catch err
println(err)
fits.has_error[] = true
return
end
width = 0
height = 0
depth = 1
hdu_id = 0
for hdu in f
hdu_id = hdu_id + 1
println(typeof(hdu))
naxes = ndims(hdu)
if naxes < 2
# continue searching for the "right" HDU
continue
end
# we have at least two dimensions
try
width = size(hdu, 1)
height = size(hdu, 2)
depth = size(hdu, 3)
catch _
end
println(