Skip to content

Commit 6daf452

Browse files
Implement File writing
1 parent fc25de2 commit 6daf452

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

src/gti.jl

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ function get_gti_from_hdu(gtihdu::TableHDU)
2424
gtistart = FITSIO.read(gtihdu,startstr)
2525
gtistop = FITSIO.read(gtihdu,stopstr)
2626

27-
return mapreduce(permutedims, vcat,
28-
[[a, b] for (a,b) in zip(gtistart, gtistop)])
27+
final_gti = [[a, b] for (a,b) in zip(gtistart, gtistop)]
28+
29+
if isempty(final_gti)
30+
return reshape(Float64[],0,2)
31+
else
32+
return mapreduce(permutedims, vcat, final_gti)
33+
end
2934
end
3035

3136
function check_gtis(gti::AbstractMatrix)
@@ -96,7 +101,13 @@ function create_gti_mask(times::AbstractVector{<:Real},gtis::AbstractMatrix{<:Re
96101
end
97102
end
98103

99-
return mask, mapreduce(permutedims, vcat, keepat!(new_gtis,new_gti_mask))
104+
keepat!(new_gtis,new_gti_mask)
105+
106+
if isempty(new_gtis)
107+
return mask, reshape(Float64[],0,2)
108+
else
109+
return mask, mapreduce(permutedims, vcat, new_gtis)
110+
end
100111
end
101112

102113
function create_gti_from_condition(time::AbstractVector{<:Real}, condition::AbstractVector{Bool};
@@ -124,7 +135,11 @@ function create_gti_from_condition(time::AbstractVector{<:Real}, condition::Abst
124135
end
125136
push!(gtis,[t0, t1])
126137
end
127-
return mapreduce(permutedims, vcat, gtis)
138+
if isempty(gtis)
139+
return reshape(Float64[],0,2)
140+
else
141+
return mapreduce(permutedims, vcat, gtis)
142+
end
128143
end
129144

130145
function operations_on_gtis(gti_list::AbstractVector{<:AbstractMatrix{T}},
@@ -189,7 +204,11 @@ function get_btis(gtis::AbstractMatrix{T}, start_time, stop_time) where {T<:Real
189204
push!(btis, [first(interval), last(interval)])
190205
end
191206

192-
return mapreduce(permutedims, vcat, btis)
207+
if isempty(btis)
208+
return reshape(Float64[],0,2)
209+
else
210+
return mapreduce(permutedims, vcat, btis)
211+
end
193212
end
194213

195214
function time_intervals_from_gtis(gtis::AbstractMatrix{<:Real}, segment_size::Real;

src/io.jl

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,49 @@ function load_events_from_fits(filename::String)
1919
end
2020
ev.gti = load_gtis(filename)
2121
header = FITSIO.read_header(eventHDU)
22-
if "MJDREFI" in header.keys
22+
if haskey(header,"MJDREFI")
2323
ev.mjdref += header["MJDREFI"]
2424
end
25-
if "MJDREFF" in header.keys
25+
if haskey(header,"MJDREFF")
2626
ev.mjdref += header["MJDREFF"]
2727
end
28-
if "INSTRUME" in header.keys
28+
if haskey(header,"INSTRUME")
2929
ev.instr = header["INSTRUME"]
3030
end
31-
if "TIMEREF" in header.keys
31+
if haskey(header,"TIMEREF")
3232
ev.timeref = header["TIMEREF"]
3333
end
34-
if "TIMESYS" in header.keys
34+
if haskey(header,"TIMESYS")
3535
ev.timesys = header["TIMESYS"]
3636
end
37-
if "PLEPHEM" in header.keys
37+
if haskey(header,"PLEPHEM")
3838
ev.ephem = header["PLEPHEM"]
3939
end
4040
ev
4141
end
4242
return a
4343
end
4444

45-
# TODO
4645
function write_events_to_fits(filename::String, ev::EventList)
47-
# FITS(filename,"w") do event
48-
# FITSIO.write(event, ev)
49-
# end
46+
FITS(filename,"w") do hduList
47+
48+
tkeys = String[]
49+
values = []
50+
51+
for field in fieldnames(EventList)
52+
fval = getfield(ev, field)
53+
if !(fval isa AbstractVecOrMat)
54+
push!(tkeys, String(field))
55+
push!(values, fval)
56+
end
57+
end
58+
59+
header = FITSHeader(tkeys, values,fill("",length(tkeys)))
60+
61+
eventData = Dict("TIME"=>ev.time, "ENERGY"=>ev.energy, "PI"=>ev.PI);
62+
FITSIO.write(hduList, eventData, header = header, name = "EVENTS")
63+
64+
gtiData = Dict("START"=>ev.gti[:,1], "STOP"=>ev.gti[:,2])
65+
FITSIO.write(hduList, gtiData, name = "GTI")
66+
end
5067
end

src/lightcurve.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function make_lightcurves(toa::AbstractVector{<:Real}, dt::Float64; tseg::Real=0
1919
sort(toa)
2020
if isnothing(tstart)
2121
# if tstart is not set, assume light curve starts with first photon
22-
tstart = toa[0]
22+
tstart = toa[begin]
2323
end
2424

2525
# compute the number of bins in the light curve
@@ -28,7 +28,7 @@ function make_lightcurves(toa::AbstractVector{<:Real}, dt::Float64; tseg::Real=0
2828
# are not throwing away good events.
2929

3030
if isnothing(tseg)
31-
tseg = toa[-1] - tstart
31+
tseg = toa[end-1] - tstart
3232
end
3333

3434
timebin = tseg ÷ dt
@@ -38,15 +38,16 @@ function make_lightcurves(toa::AbstractVector{<:Real}, dt::Float64; tseg::Real=0
3838
end
3939

4040
tend = tstart + timebin * dt
41-
good = (tstart <= toa) & (toa < tend)
41+
good = tstart <= toa < tend
4242
if !use_hist
43-
binned_toas = (toa[good] - tstart) ÷ dt
43+
binned_toas = (keepat!(toa,good) .- tstart) .÷ dt
4444
counts = bincount(binned_toas, minlength=timebin)
4545
time = tstart + range(0.5, 0.5 + len(counts)) * dt
4646
else
4747
histbins = range(tstart, tend + dt, dt)
48-
counts, histbins = histogram(toa[good], bins=histbins)
49-
time = histbins[begin:end] + 0.5 * dt
48+
counts, histbins = histogram(keepat!(toa,good), bins=histbins)
49+
time = @view(histbins[begin:end-1]) + 0.5 * dt
50+
end
5051

5152
return Lightcurve(time, counts; gti=gti, mjdref=mjdref, dt=dt,
5253
skip_checks=True, err_dist="poisson")

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ using Stingray
22
using Test
33
using FFTW, Distributions, Statistics, StatsBase, Metadata, HDF5
44

5-
# include("test_fourier.jl")
6-
# include("test_gti.jl")
5+
include("test_fourier.jl")
6+
include("test_gti.jl")
77
include("test_events.jl")

test/test_events.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@testset "test_events" begin
2-
times = [0.5, 1.5, 2.5, 3.5]
2+
time = [0.5, 1.5, 2.5, 3.5]
33
counts = [3000, 2000, 2200, 3600]
44
counts_flat = [3000, 3000, 3000, 3000]
55
spectrum = [[1, 2, 3, 4, 5, 6], [1000, 2040, 1000, 3000, 4020, 2070]]
@@ -96,6 +96,14 @@
9696
ev = Stingray.read(fname, "fits")
9797
@test ev.mjdref == 55197.00076601852
9898
end
99+
100+
@testset "test_io_with_fits" begin
101+
ev = EventList(time=time, mjdref=54000)
102+
Stingray.write(ev, "ev.fits", "fits")
103+
new_ev = Stingray.read("ev.fits", "fits")
104+
@test new_ev.time == time
105+
rm("ev.fits")
106+
end
99107
end
100108
end
101109

0 commit comments

Comments
 (0)