Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed Jun 6, 2024
1 parent f13236d commit 960c4d2
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- uses: julia-actions/julia-buildpkg@v1
with:
ignore-no-cache: true
localregistry: https://github.com/0h7z/0hjl.git
- uses: julia-actions/julia-runtest@v1
- uses: heptazhou/julia-codecov@v1
with:
Expand Down
7 changes: 6 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
name = "Exts"
uuid = "0b12d779-4123-4875-9d6c-e33c2e29e2c9"
authors = ["Heptazhou <zhou at 0h7z dot com>"]
version = "0.1.4"
version = "0.1.5"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[weakdeps]
CFITSIO = "3b1b4be9-1499-4b22-8d78-7db3344d1961"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
FITSIO = "525bcba6-941b-5504-bd06-fd0dc1a4d2eb"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[extensions]
DataFramesExt = "DataFrames"
FITSIOExt = ["DataFrames", "FITSIO"]
StatisticsExt = "StatsBase"

[compat]
CFITSIO = "≥ 1.4.2"
FITSIO = "≥ 0.16.6"
julia = "≥ 1.9"
52 changes: 52 additions & 0 deletions ext/FITSIOExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2023-2024 Heptazhou <zhou@0h7z.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

module FITSIOExt

using Base.Threads: @spawn
using DataFrames: DataFrame
using FITSIO: FITSIO, FITS, EitherTableHDU

const StrOrSym = Union{AbstractString, Symbol}

const ensure_vector(a::AbstractArray) = eachslice(a, dims = ndims(a))
const ensure_vector(v::AbstractVector) = v

function Base.read(t::EitherTableHDU, ::Type{DataFrame},
colnames::AbstractVector{<:StrOrSym} = FITSIO.Tables.columnnames(t))
fits = t.fitsfile
f, n = FITSIO.fits_file_name(fits), t.ext
if 0 FITSIO.fits_file_mode(fits) # 0 => read-only, 1 => read-write
throw(ArgumentError("FITS file must be opened in read-only mode"))
end
cols = map(map(String, colnames)) do colname
@spawn ensure_vector(FITS(f -> read(f[n], colname), f))
end
DataFrame(map(fetch, cols), colnames)
end

# https://github.com/JuliaAstro/FITSIO.jl/pull/193
@static if !hasmethod(FITSIO.fits_tform_char, (Type{UInt64},))
FITSIO.fits_tform_char(::Type{UInt64}) = 'W'
end
@static if !haskey(FITSIO.CFITSIO_COLTYPE, 080)
function __init__()
FITSIO.CFITSIO_COLTYPE[080] = UInt64
nothing
end
end

end # module

3 changes: 3 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
FITSIO = "525bcba6-941b-5504-bd06-fd0dc1a4d2eb"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
38 changes: 38 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
using InteractiveUtils: subtypes
using Test

@testset "Pkg" begin
using Pkg: Pkg, PlatformEngines
@test getfield.(Pkg.Registry.reachable_registries(), :name) ["General", "0hjl"]
@test any(startswith("7-Zip "), readlines(PlatformEngines.exe7z()))
# https://github.com/ip7z/7zip/blob/main/CPP/7zip/UI/Console/Main.cpp
# https://github.com/mcmilk/7-Zip/blob/master/CPP/7zip/UI/Console/Main.cpp
# https://github.com/p7zip-project/p7zip/blob/master/CPP/7zip/UI/Console/Main.cpp

@test PlatformEngines.p7zip_jll.is_available()
@info PlatformEngines.p7zip_jll.p7zip_path
@info PlatformEngines.find7z()
end

@testset "Core" begin
local v = Type[Real]
map(_ -> unique!(append!(v, mapreduce(subtypes, vcat, v))), 1:3)
Expand Down Expand Up @@ -58,6 +71,31 @@ end
@test df == CSV.read(tmp, DataFrame)
end

@testset "FITSIOExt" begin
using FITSIO: FITSIO, CFITSIO, FITS
@test ccall((:fits_is_reentrant, CFITSIO.libcfitsio), Bool, ())
# https://heasarc.gsfc.nasa.gov/fitsio/c/c_user/node15.html

local A_Nothing = Array{Nothing, 3}(undef, Tuple(rand(0:9, 3)))
local FITSIOExt = Base.get_extension(Exts, :FITSIOExt)
@test FITSIOExt.ensure_vector(A_Nothing) isa
AbstractVector{<:AbstractMatrix{Nothing}}

using HTTP: HTTP
local tmp = HTTP.download(
"https://data.sdss.org/sas/dr18/spectro/sdss/redux/" *
"v5_13_2/spectra/lite/3650/spec-3650-55244-0001.fits",
update_period = Inf,
)
FITS(tmp) do f
@test read(f["SPALL"], DataFrame) isa DataFrame
end
FITS(tmp, "r+") do f
@test_throws ArgumentError read(f["SPALL"], DataFrame)
end
rm(tmp, force = true)
end

@testset "StatisticsExt" begin
using StatsBase: mean, weights
@test mean(1:20, weights(zeros(20))) |> isnan
Expand Down

0 comments on commit 960c4d2

Please sign in to comment.