Skip to content

Commit d98d0ed

Browse files
authored
Merge pull request #17 from ChevronETC/typestableprop
Type-stable version of prop.
2 parents 0f26b20 + 673695e commit d98d0ed

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/teaseisio.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ Compute the fold of a frame where io is JSeis corresponding to the dataset, and
935935
For example: `io=jsopen("file.js"); fold(io, readframehdrs(io,1))`
936936
"""
937937
function fold(io::JSeis, hdrs::Array{UInt8,2})
938-
trctyp = prop(io, "TRC_TYPE")
938+
trctyp = prop(io, stockprop[:TRC_TYPE])
939939
mapreduce(i->get(trctyp, hdrs, i) == tracetype[:live] ? 1 : 0, +, 1:size(hdrs,2))
940940
end
941941

@@ -1114,12 +1114,13 @@ end
11141114
prop(io, propertyname)
11151115
11161116
Get a trace property from `io::JSeis` where `propertyname` is either `String` or `TracePropertyDef`.
1117+
Note that if `propertyname` is a String, then this method produces a type-unstable result.
11171118
For example:
11181119
11191120
```julia
11201121
io = jsopen("data.js")
1121-
p = prop(io, "REC_X") # using an `String`
1122-
p = prop(io, stockprop[:REC_X]) # using a `TracePropertyDef`
1122+
p = prop(io, "REC_X") # using a `String`, output type of prop is not inferred
1123+
p = prop(io, stockprop[:REC_X]) # using a `TracePropertyDef`, output type of prop is inferred
11231124
```
11241125
"""
11251126
function prop(io::JSeis, _property::String)
@@ -1130,7 +1131,14 @@ function prop(io::JSeis, _property::String)
11301131
end
11311132
error("unable to find trace property with label $(_property)\n")
11321133
end
1133-
prop(io::JSeis, _property::TracePropertyDef) = prop(io, _property.label)
1134+
function prop(io::JSeis, _property::TracePropertyDef{T}) where {T}
1135+
for property in io.properties
1136+
if _property.label == property.def.label
1137+
return property::TraceProperty{T}
1138+
end
1139+
end
1140+
error("unable to find trace property with label $(_property)\n")
1141+
end
11341142

11351143
"""
11361144
copy!(ioout, hdrsout, ioin, hdrsin)
@@ -1871,11 +1879,14 @@ function writeframe(io::JSeis, trcs::AbstractArray{Float32, 2}, idx::Int...)
18711879
@assert length(idx) == ndims(io)-2
18721880
hdrs = allocframehdrs(io)
18731881

1874-
map(i->set!(prop(io, io.axis_propdefs[2]), hdrs, i, Int(io.axis_lstarts[2] + (i-1)*io.axis_lincs[2])), 1:io.axis_lengths[2])
1882+
props = [prop(io, io.axis_propdefs[idim]) for idim = 1:ndims(io)]
1883+
1884+
map(i->set!(props[2], hdrs, i, Int(io.axis_lstarts[2] + (i-1)*io.axis_lincs[2])), 1:io.axis_lengths[2])
18751885
for idim = 3:ndims(io)
1876-
map(i->set!(prop(io, io.axis_propdefs[idim]), hdrs, i, idx[idim-2]), 1:io.axis_lengths[2])
1886+
map(i->set!(props[idim], hdrs, i, idx[idim-2]), 1:io.axis_lengths[2])
18771887
end
1878-
map(i->set!(prop(io, stockprop[:TRC_TYPE]), hdrs, i, tracetype[:live]), 1:io.axis_lengths[2])
1888+
proptt = prop(io, stockprop[:TRC_TYPE])
1889+
map(i->set!(proptt, hdrs, i, tracetype[:live]), 1:io.axis_lengths[2])
18791890
writeframe_impl(io, trcs, hdrs, Int(io.axis_lengths[2]), sub2ind(io, idx))
18801891
end
18811892
writeframe(io::JSeis, trcs::AbstractArray{Float64, 2}, idx::Int...) = writeframe(io, convert(Array{Float32, 2}, trcs), idx...)
@@ -2008,9 +2019,10 @@ function sub2ind(io::JSeis, idx::NTuple)
20082019
end
20092020
function sub2ind(io::JSeis, hdrs::AbstractArray{UInt8,2})
20102021
ptrctype = prop(io, stockprop[:TRC_TYPE])
2022+
props = [prop(io, io.axis_propdefs[i]) for i=3:ndims(io)]
20112023
for itrc = 1:size(hdrs,2)
20122024
if get(ptrctype, hdrs, itrc) == tracetype[:live]
2013-
idx = ntuple(i->get(prop(io, io.axis_propdefs[2+i]), hdrs, itrc), length(io.axis_lengths) - 2)
2025+
idx = ntuple(i->get(props[i], hdrs, itrc), length(io.axis_lengths) - 2)
20142026
return sub2ind(io, idx)
20152027
end
20162028
end
@@ -2046,7 +2058,7 @@ function leftjustify!(io::JSeis, trcs::Array{Float32, 2}, hdrs::Array{UInt8, 2})
20462058
if fold(io, hdrs) == io.axis_lengths[2]
20472059
return
20482060
end
2049-
proptyp = prop(io, "TRC_TYPE")
2061+
proptyp = prop(io, stockprop[:TRC_TYPE])
20502062
j, ntrcs, nsamp, nhead = 1, size(trcs, 2), size(trcs,1), size(hdrs,1)
20512063
tmp_trc, tmp_hdr = Array{Float32}(undef, size(io,1)), Array{UInt8}(undef, headerlength(io))
20522064
for i = 1:ntrcs

0 commit comments

Comments
 (0)