Skip to content

Commit

Permalink
Fix uncareful assumption that one ANALOG:SCALE contains all channels
Browse files Browse the repository at this point in the history
Large number of channels may overflow into an ANALOG:SCALE2, etc
  • Loading branch information
halleysfifthinc committed Aug 12, 2024
1 parent 62bc7da commit 8949232
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
22 changes: 19 additions & 3 deletions src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,25 @@ function readdata(
SCALE = groups[:ANALOG][Float32, :GEN_SCALE] * groups[:ANALOG][Float32, :SCALE]
analog .= (analog .- ANALOG_OFFSET) .* SCALE
else
VECANALOG_OFFSET = groups[:ANALOG][Vector{Int}, :OFFSET][1:numchannels]
VECSCALE = groups[:ANALOG][Float32, :GEN_SCALE] .*
groups[:ANALOG][Vector{Float32}, :SCALE][1:numchannels]
off_labels = get_multipled_parameter_names(groups, :ANALOG, :OFFSET)
if length(off_labels) > 1
VECANALOG_OFFSET = convert(Vector{Float32}, reduce(vcat,
groups[:ANALOG][Vector{Int}, offset]
for offset in off_labels))[1:numchannels]
else
VECANALOG_OFFSET = convert(Vector{Float32},
groups[:ANALOG][Vector{Int}, :OFFSET][1:numchannels])
end

scale_labels = get_multipled_parameter_names(groups, :ANALOG, :SCALE)
if length(scale_labels) > 1
VECSCALE = convert(Vector{Float32}, reduce(vcat,
groups[:ANALOG][Vector{Int}, scale]
for scale in scale_labels))[1:numchannels]
else
VECSCALE = groups[:ANALOG][Vector{Float32}, :SCALE][1:numchannels]
end
VECSCALE .*= groups[:ANALOG][Float32, :GEN_SCALE]

analog .= (analog .- VECANALOG_OFFSET) .* VECSCALE
end
Expand Down
4 changes: 2 additions & 2 deletions src/validate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ function validatec3d(header, groups)

# Pad scale and offset if shorter than :USED
l = length(groups[:ANALOG][Vector{Float32}, :SCALE])
if l < ANALOG_USED
if l < ANALOG_USED && !haskey(groups[:ANALOG], :SCALE2)
append!(groups[:ANALOG][Vector{Float32}, :SCALE],
fill(one(Float32), ANALOG_USED - l))
end

l = length(groups[:ANALOG][Vector{Int16}, :OFFSET])
if l < ANALOG_USED
if l < ANALOG_USED && !haskey(groups[:ANALOG], :OFFSET2)
append!(groups[:ANALOG][Vector{Int16}, :OFFSET],
fill(one(Float32), ANALOG_USED - l))
end
Expand Down
22 changes: 19 additions & 3 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,30 @@ function assemble_analogdata(h::Header{END}, f::C3DFile{END}) where {END<:Abstra
ANALOG_SCALE = f.groups[:ANALOG][Float32, :GEN_SCALE] *
f.groups[:ANALOG][Float32, :SCALE]
elseif numchannels > 1
ANALOG_OFFSET = convert(Vector{Float32}, f.groups[:ANALOG][Vector{Int}, :OFFSET][1:numchannels])'
off_labels = get_multipled_parameter_names(f.groups, :ANALOG, :OFFSET)
if length(off_labels) > 1
ANALOG_OFFSET = convert(Vector{Float32}, reduce(vcat,
f.groups[:ANALOG][Vector{Int}, offset]
for offset in off_labels))[1:numchannels]'
else
ANALOG_OFFSET = convert(Vector{Float32},
f.groups[:ANALOG][Vector{Int}, :OFFSET][1:numchannels])'
end

# addition of positive zero changes sign (to positive), negative zero addition
# leaves sign as-is
ANALOG_OFFSET[iszero.(ANALOG_OFFSET)] .= -0.0f0

ANALOG_SCALE = (f.groups[:ANALOG][Vector{Float32}, :SCALE][1:numchannels] .*
f.groups[:ANALOG][Float32, :GEN_SCALE])'

scale_labels = get_multipled_parameter_names(f.groups, :ANALOG, :SCALE)
if length(scale_labels) > 1
ANALOG_SCALE = convert(Vector{Float32}, reduce(vcat,
f.groups[:ANALOG][Vector{Int}, scale]
for scale in scale_labels))[1:numchannels]'
else
ANALOG_SCALE = f.groups[:ANALOG][Vector{Float32}, :SCALE][1:numchannels]'
end
ANALOG_SCALE .*= f.groups[:ANALOG][Float32, :GEN_SCALE]

# Dividing by zero causes NaNs; dividing by 1 does nothing
ANALOG_SCALE[iszero.(ANALOG_SCALE)] .= 1.0f0
Expand Down

0 comments on commit 8949232

Please sign in to comment.