Skip to content

Commit 4bc0830

Browse files
authoredJul 29, 2022
Add Gziped fits file support and AstroImages.jl library (#363)
1 parent 0ea2829 commit 4bc0830

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed
 

‎src/registry.jl

+36-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ add_format(format"JLD", (unsafe_wrap(Vector{UInt8}, "Julia data file (HDF5), ver
2424
unsafe_wrap(Vector{UInt8}, "Julia data file (HDF5), version 0.1")), ".jld", [:JLD => UUID("4138dd39-2aa7-5051-a626-17a0bb65d9c8")])
2525
add_format(format"JLD2", (unsafe_wrap(Vector{UInt8},"Julia data file (HDF5), version 0.2"),
2626
unsafe_wrap(Vector{UInt8}, "HDF5-based Julia Data Format, version ")), ".jld2", [:JLD2 => UUID("033835bb-8acc-5ee8-8aae-3f567f8a3819")])
27-
add_format(format"GZIP", [0x1f, 0x8b], ".gz", [:Libz => UUID("2ec943e9-cfe8-584d-b93d-64dcb6d567b7")])
2827
add_format(format"BSON",(),".bson", [:BSON => UUID("fbb218c0-5317-5bc6-957e-2ee96dd4b1f0")])
2928
add_format(format"JLSO", (), ".jlso", [:JLSO => UUID("9da8a3cd-07a3-59c0-a743-3fdc52c30d11")])
3029
add_format(format"NPY", "\x93NUMPY", ".npy", [idNPZ])
@@ -60,6 +59,9 @@ end
6059

6160
detect_compressed(io, len=getlength(io); kwargs...) = detect_compressor(io, len; kwargs...) !== nothing
6261

62+
const compressed_fits_exten = r"\.(fit|fits|fts|FIT|FITS|FTS)\.(gz|GZ)\>"
63+
name_matches_compressed_fits(io) = (:name propertynames(io)) && endswith(io.name, compressed_fits_exten)
64+
6365
# test for RD?n magic sequence at the beginning of R data input stream
6466
function detect_rdata(io)
6567
seekstart(io)
@@ -81,7 +83,7 @@ function detect_rdata(io)
8183
return true
8284
end
8385
checked_match(io) && return true
84-
return detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"])
86+
return detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"]) && !name_matches_compressed_fits(io)
8587
end
8688

8789
add_format(format"RData", detect_rdata, [".rda", ".RData", ".rdata"], [idRData, LOAD])
@@ -102,7 +104,7 @@ function detect_rdata_single(io)
102104

103105
res = checked_match(io)
104106
if !res
105-
res = detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"])
107+
res = detect_compressed(io; formats=["GZIP", "BZIP2", "XZ"]) && !name_matches_compressed_fits(io)
106108
end
107109
seekstart(io)
108110
return res
@@ -463,11 +465,39 @@ end
463465
add_format(format"STL_ASCII", detect_stlascii, [".stl", ".STL"], [idMeshIO])
464466
add_format(format"STL_BINARY", detect_stlbinary, [".stl", ".STL"], [idMeshIO])
465467

468+
# GZip has two simple magic bytes [0x1f, 0x8b] but we don't want to dispatch to Libz
469+
# for file extensions like .fits.gz
470+
function detect_gzip(io)
471+
if name_matches_compressed_fits(io)
472+
return false
473+
end
474+
getlength(io) >= 2 || return false
475+
magic = read!(io, Vector{UInt8}(undef, 2))
476+
return magic == [0x1f, 0x8b]
477+
end
478+
add_format(format"GZIP", detect_gzip, ".gz", [:Libz => UUID("2ec943e9-cfe8-584d-b93d-64dcb6d567b7")])
479+
480+
466481
# Astro Data
482+
# FITS files are often gziped and given the extension ".fits.gz". We want to load those directly and not dispatch to Libz
483+
function detect_fits(io)
484+
# FITS files can have
485+
if name_matches_compressed_fits(io)
486+
return true
487+
end
488+
getlength(io) >= 30 || return false
489+
magic = read!(io, Vector{UInt8}(undef, 30))
490+
return magic == [0x53,0x49,0x4d,0x50,0x4c,0x45,0x20,0x20,0x3d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x54]
491+
end
467492
add_format(format"FITS",
468-
# See https://www.loc.gov/preservation/digital/formats/fdd/fdd000317.shtml#sign
469-
[0x53,0x49,0x4d,0x50,0x4c,0x45,0x20,0x20,0x3d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x54],
470-
[".fit", ".fits", ".fts", ".FIT", ".FITS", ".FTS"], [:FITSIO => UUID("525bcba6-941b-5504-bd06-fd0dc1a4d2eb")])
493+
# See https://www.loc.gov/preservation/digital/formats/fdd/fdd000317.shtml#sign
494+
# [0x53,0x49,0x4d,0x50,0x4c,0x45,0x20,0x20,0x3d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x54],
495+
detect_fits,
496+
[".fit", ".fits", ".fts", ".FIT", ".FITS", ".FTS", ".fit",],
497+
[:FITSIO => UUID("525bcba6-941b-5504-bd06-fd0dc1a4d2eb")],
498+
[:AstroImages => UUID("fe3fc30c-9b16-11e9-1c73-17dabf39f4ad")])
499+
500+
471501

472502
function detect_gadget2(io)
473503
pos = position(io)

‎test/query.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ let file_dir = joinpath(@__DIR__, "files"), file_path = Path(file_dir)
540540

541541
io = open(iris)
542542
q = query(io)
543-
@test typeof(q) <: Stream{format"GZIP"} # FIXME: should be RData
543+
@test typeof(q) <: Stream{format"RData"}
544544
@test FileIO.detect_rdata(io)
545545

546546
# issue #345: it errors here

0 commit comments

Comments
 (0)
Please sign in to comment.