From 64e85f9247ef589ee44a2d70f9e9edbea243732d Mon Sep 17 00:00:00 2001 From: tmasthay Date: Thu, 23 May 2024 09:04:16 -0500 Subject: [PATCH] REVERT: Accidental changes (rebase later) --- api/julia/m8r.jl | 426 +++++++++++------------------------ api/julia/test/afdm.jl | 34 +-- api/julia/test/clip.jl | 3 +- api/julia/test/runtests.jl | 167 ++++++-------- book/rsf/rsf/afdm/SConstruct | 3 +- 5 files changed, 216 insertions(+), 417 deletions(-) diff --git a/api/julia/m8r.jl b/api/julia/m8r.jl index edcb3200d0..7f331e6f8e 100644 --- a/api/julia/m8r.jl +++ b/api/julia/m8r.jl @@ -30,7 +30,8 @@ julia> sfspike(n1=2) |> x -> sfwindow(x; n1=1) |> x -> rsf_write(x, "spike.rsf") """ module m8r -export rsf_read, rsf_write +export rsf_read, + rsf_write if haskey(ENV, "RSFROOT") RSFROOT = ENV["RSFROOT"] @@ -39,10 +40,10 @@ else end #if Libdl.find_library("libdrsf") == "" -#push!(Libdl.DL_LOAD_PATH, joinpath(RSFROOT, "lib")) -#if Libdl.find_library("libdrsf") == "" -#throw("Cannot find C api. Make sure RSFROOT/lib is in LD_LIBRARY_PATH") -#end + #push!(Libdl.DL_LOAD_PATH, joinpath(RSFROOT, "lib")) + #if Libdl.find_library("libdrsf") == "" + #throw("Cannot find C api. Make sure RSFROOT/lib is in LD_LIBRARY_PATH") + #end #end struct RSFFile @@ -58,76 +59,56 @@ function __init__() if isempty(ARGS) push!(argv, "-") else - append!(argv, ARGS) + append!(argv,ARGS) end - ccall((:sf_init, "libdrsf"), Cvoid, (Int32, Ptr{Ptr{UInt8}}), length(argv), argv) + ccall((:sf_init,"libdrsf"),Cvoid,(Int32,Ptr{Ptr{UInt8}}),length(argv),argv) end -function input(tag::String; temp = false) +function input(tag::String; temp=false) if tag ≠ "in" if !isfile(tag) throw("SystemError: unable to read file $tag") end end - rsf = ccall((:sf_input, "libdrsf"), Ptr{UInt8}, (Ptr{UInt8},), tag) + rsf = ccall((:sf_input,"libdrsf"),Ptr{UInt8},(Ptr{UInt8},),tag) RSFFile(tag, rsf, temp) end function output(tag::String) - rsf = ccall((:sf_output, "libdrsf"), Ptr{UInt8}, (Ptr{UInt8},), tag) + rsf = ccall((:sf_output,"libdrsf"),Ptr{UInt8},(Ptr{UInt8},),tag) RSFFile(tag, rsf) end function gettype(file::RSFFile) - return ccall((:sf_gettype, "libdrsf"), Cuint, (Ptr{UInt8},), file.rsf) + 1 + return ccall((:sf_gettype,"libdrsf"),Cuint,(Ptr{UInt8},),file.rsf) + 1 end function getform(file::RSFFile) - return ccall((:sf_getform, "libdrsf"), Cuint, (Ptr{UInt8},), file.rsf) + 1 + return ccall((:sf_getform,"libdrsf"),Cuint,(Ptr{UInt8},),file.rsf) + 1 end function esize(file::RSFFile) - return ccall((:sf_esize, "libdrsf"), Csize_t, (Ptr{UInt8},), file.rsf) + return ccall((:sf_esize,"libdrsf"),Csize_t,(Ptr{UInt8},),file.rsf) end -function setformat(file::RSFFile, format::String) - ccall((:sf_setformat, "libdrsf"), Cvoid, (Ptr{UInt8}, Ptr{UInt8}), file.rsf, format) +function setformat(file::RSFFile,format::String) + ccall((:sf_setformat,"libdrsf"),Cvoid,(Ptr{UInt8},Ptr{UInt8}),file.rsf,format) end -function histint(file::RSFFile, name::String) +function histint(file::RSFFile,name::String) val = Cint[0] - ccall( - (:sf_histint, "libdrsf"), - Bool, - (Ptr{UInt8}, Ptr{UInt8}, Ref{Cint}), - file.rsf, - name, - val, - ) + ccall((:sf_histint,"libdrsf"),Bool,(Ptr{UInt8},Ptr{UInt8},Ref{Cint}),file.rsf,name,val) return convert(Int, val[]) end -function histfloat(file::RSFFile, name::String) +function histfloat(file::RSFFile,name::String) val = Cfloat[0] - ccall( - (:sf_histfloat, "libdrsf"), - Bool, - (Ptr{UInt8}, Ptr{UInt8}, Ref{Cfloat}), - file.rsf, - name, - val, - ) + ccall((:sf_histfloat,"libdrsf"),Bool,(Ptr{UInt8},Ptr{UInt8},Ref{Cfloat}),file.rsf,name,val) return convert(Float32, val[]) end -function histstring(file::RSFFile, name::String) - val = ccall( - (:sf_histstring, "libdrsf"), - Ptr{Cchar}, - (Ptr{UInt8}, Ptr{UInt8}), - file.rsf, - name, - ) +function histstring(file::RSFFile,name::String) + val = ccall((:sf_histstring,"libdrsf"),Ptr{Cchar},(Ptr{UInt8},Ptr{UInt8}),file.rsf,name) if val == C_NULL return "" end @@ -136,20 +117,20 @@ end function getint(name::String, val::Integer) val = Cint[val] - ccall((:sf_getint, "libdrsf"), Bool, (Ptr{UInt8}, Ref{Cint}), name, val) + ccall((:sf_getint,"libdrsf"),Bool,(Ptr{UInt8},Ref{Cint}),name,val) return convert(Int, val[]) end getint(name::String; val::Integer = 0) = getint(name, val) function getfloat(name::String, val::Real) val = Cfloat[val] - ccall((:sf_getfloat, "libdrsf"), Bool, (Ptr{UInt8}, Ref{Cfloat}), name, val) + ccall((:sf_getfloat,"libdrsf"),Bool,(Ptr{UInt8},Ref{Cfloat}),name,val) return convert(Float32, val[]) end getfloat(name::String; val::Real = 0) = getfloat(name, val) function getstring(name::String, val::String) - v = ccall((:sf_getstring, "libdrsf"), Ptr{Cchar}, (Ptr{UInt8},), name) + v = ccall((:sf_getstring,"libdrsf"),Ptr{Cchar},(Ptr{UInt8},),name) if v == C_NULL return val end @@ -159,197 +140,92 @@ getstring(name::String; val::String = "") = getstring(name, val) function getbool(name::String, val::Bool) val = Bool[val] - ccall((:sf_getbool, "libdrsf"), Bool, (Ptr{UInt8}, Ref{Bool}), name, val) + ccall((:sf_getbool,"libdrsf"),Bool,(Ptr{UInt8},Ref{Bool}),name,val) return val[] end getbool(name::String; val::Bool = true) = getbool(name, val) -function leftsize(file::RSFFile, dim::Integer) +function leftsize(file::RSFFile,dim::Integer) dim::Cint = dim - ccall((:sf_leftsize, "libdrsf"), Culonglong, (Ptr{UInt8}, Cint), file.rsf, dim) + ccall((:sf_leftsize,"libdrsf"),Culonglong,(Ptr{UInt8},Cint),file.rsf,dim) end -function ucharread(arr::Array{UInt8,1}, size::Integer, file::RSFFile) +function ucharread(arr::Array{UInt8,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_ucharread, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_ucharread,"libdrsf"),Cvoid,(Ptr{UInt8},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function charread(arr::Array{UInt8,1}, size::Integer, file::RSFFile) +function charread(arr::Array{UInt8,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_charread, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_charread,"libdrsf"),Cvoid,(Ptr{UInt8},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function intread(arr::Array{Int32,1}, size::Integer, file::RSFFile) +function intread(arr::Array{Int32,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_intread, "libdrsf"), - Cvoid, - (Ptr{Cint}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_intread,"libdrsf"),Cvoid,(Ptr{Cint},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function floatread(arr::Array{Float32,1}, size::Integer, file::RSFFile) +function floatread(arr::Array{Float32,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_floatread, "libdrsf"), - Cvoid, - (Ptr{Cfloat}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_floatread,"libdrsf"),Cvoid,(Ptr{Cfloat},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function complexread(arr::Array{ComplexF32,1}, size::Integer, file::RSFFile) +function complexread(arr::Array{ComplexF32,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_complexread, "libdrsf"), - Cvoid, - (Ptr{ComplexF32}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_complexread,"libdrsf"),Cvoid,(Ptr{ComplexF32},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function shortread(arr::Array{Int16,1}, size::Integer, file::RSFFile) +function shortread(arr::Array{Int16,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_shortread, "libdrsf"), - Cvoid, - (Ptr{Cshort}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_shortread,"libdrsf"),Cvoid,(Ptr{Cshort},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function ucharwrite(arr::Array{UInt8,1}, size::Integer, file::RSFFile) +function ucharwrite(arr::Array{UInt8,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_ucharwrite, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_ucharwrite,"libdrsf"),Cvoid,(Ptr{UInt8},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function charwrite(arr::Array{UInt8,1}, size::Integer, file::RSFFile) +function charwrite(arr::Array{UInt8,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_charwrite, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_charwrite,"libdrsf"),Cvoid,(Ptr{UInt8},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function intwrite(arr::Array{Int32,1}, size::Integer, file::RSFFile) +function intwrite(arr::Array{Int32,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_intwrite, "libdrsf"), - Cvoid, - (Ptr{Cint}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_intwrite,"libdrsf"),Cvoid,(Ptr{Cint},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function floatwrite(arr::Array{Float32,1}, size::Integer, file::RSFFile) +function floatwrite(arr::Array{Float32,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_floatwrite, "libdrsf"), - Cvoid, - (Ptr{Cfloat}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_floatwrite,"libdrsf"),Cvoid,(Ptr{Cfloat},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function complexwrite(arr::Array{ComplexF32,1}, size::Integer, file::RSFFile) +function complexwrite(arr::Array{ComplexF32,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_complexwrite, "libdrsf"), - Cvoid, - (Ptr{ComplexF32}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_complexwrite,"libdrsf"),Cvoid,(Ptr{ComplexF32},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function shortwrite(arr::Array{Int16,1}, size::Integer, file::RSFFile) +function shortwrite(arr::Array{Int16,1},size::Integer,file::RSFFile) size::Csize_t = size - ccall( - (:sf_complexwrite, "libdrsf"), - Cvoid, - (Ptr{Cshort}, Csize_t, Ptr{UInt8}), - arr, - size, - file.rsf, - ) + ccall((:sf_complexwrite,"libdrsf"),Cvoid,(Ptr{Cshort},Csize_t,Ptr{UInt8}),arr,size,file.rsf) end -function putint(file::RSFFile, name::String, val::Integer) +function putint(file::RSFFile,name::String,val::Integer) val::Cint = val - ccall( - (:sf_putint, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Ptr{UInt8}, Cint), - file.rsf, - name, - val, - ) + ccall((:sf_putint,"libdrsf"),Cvoid,(Ptr{UInt8},Ptr{UInt8},Cint),file.rsf,name,val) end -function putfloat(file::RSFFile, name::String, val::Real) +function putfloat(file::RSFFile,name::String,val::Real) val::Cfloat = val - ccall( - (:sf_putfloat, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Ptr{UInt8}, Cfloat), - file.rsf, - name, - val, - ) -end - -function putstring(file::RSFFile, name::String, val::String) - ccall( - (:sf_putstring, "libdrsf"), - Cvoid, - (Ptr{UInt8}, Ptr{UInt8}, Ptr{UInt8}), - file.rsf, - name, - val, - ) + ccall((:sf_putfloat,"libdrsf"),Cvoid,(Ptr{UInt8},Ptr{UInt8},Cfloat),file.rsf,name,val) +end + +function putstring(file::RSFFile,name::String,val::String) + ccall((:sf_putstring,"libdrsf"),Cvoid,(Ptr{UInt8},Ptr{UInt8},Ptr{UInt8}),file.rsf,name,val) end function close(file::RSFFile) - ccall((:sf_fileclose, "libdrsf"), Cvoid, (Ptr{UInt8},), file.rsf) + ccall((:sf_fileclose,"libdrsf"), Cvoid, (Ptr{UInt8},), file.rsf) end """ @@ -406,17 +282,17 @@ julia> rsf_read("spike.rsf") (Float32[1.0 1.0 1.0; 1.0 1.0 1.0], [2, 3], Float32[0.004, 0.1], Float32[0.0, 0.0], String["Time", "Distance"], String["s", "km"]) ``` """ -function rsf_read(file::RSFFile; headers_only::Bool = false) +function rsf_read(file::RSFFile; headers_only::Bool=false) types = [ - UInt8, # SF_UCHAR - UInt8, # SF_CHAR - Int32, # SF_INT - Float32, # SF_FLOAT - ComplexF32, # SF_COMPLEX - Int16, # SF_SHORT - Float64, # SF_DOUBLE - Clong, # SF_LONG (UNIX: Int, Windows: Int32) - ] + UInt8, # SF_UCHAR + UInt8, # SF_CHAR + Int32, # SF_INT + Float32, # SF_FLOAT + ComplexF32, # SF_COMPLEX + Int16, # SF_SHORT + Float64, # SF_DOUBLE + Clong, # SF_LONG (UNIX: Int, Windows: Int32) + ] n = Int[i for i in size(file)] sz = prod(n) @@ -424,11 +300,11 @@ function rsf_read(file::RSFFile; headers_only::Bool = false) o = Float32[] l = String[] u = String[] - for i = 1:length(n) - append!(d, [histfloat(file, "d" * string(i))]) - append!(o, [histfloat(file, "o" * string(i))]) - append!(l, [histstring(file, "label" * string(i))]) - append!(u, [histstring(file, "unit" * string(i))]) + for i in 1:length(n) + append!(d, [histfloat(file, "d"*string(i))]) + append!(o, [histfloat(file, "o"*string(i))]) + append!(l, [histstring(file, "label"*string(i))]) + append!(u, [histstring(file, "unit"*string(i))]) end if headers_only @@ -461,15 +337,15 @@ function rsf_read(file::RSFFile; headers_only::Bool = false) return data, n, d, o, l, u end -rsf_read(name::String; headers_only::Bool = false) = - rsf_read(input(name); headers_only = headers_only) +rsf_read(name::String; headers_only::Bool=false) = + rsf_read(input(name); headers_only=headers_only) -function rsf_read(my_stdin::NTuple{2,Base.PipeEndpoint}; headers_only::Bool = false) +function rsf_read(my_stdin::NTuple{2, Base.PipeEndpoint}; headers_only::Bool=false) rin, win = my_stdin flush(win) old_stdin = stdin redirect_stdin(rin) - data = rsf_read("in"; headers_only = headers_only) + data = rsf_read("in"; headers_only=headers_only) redirect_stdin(old_stdin) return data end @@ -537,29 +413,22 @@ julia> rsf_read(out.tag) (Float32[1.0, 2.0], [2], Float32[1.0], Float32[0.0], String[""], String[""]) ``` """ -function rsf_write( - file::RSFFile, - dat::AbstractArray, - n = nothing, - d = nothing, - o = nothing, - l = nothing, - u = nothing, -) +function rsf_write(file::RSFFile, dat::AbstractArray, n=nothing, d=nothing, + o=nothing, l=nothing, u=nothing) if n == nothing n = Base.size(dat) end dim = length(n) - d = d == nothing ? [1 for i = 1:dim] : d - o = o == nothing ? [0 for i = 1:dim] : o - l = l == nothing ? ["" for i = 1:dim] : l - u = u == nothing ? ["" for i = 1:dim] : u - for i = 1:dim + d = d == nothing ? [1 for i in 1:dim] : d + o = o == nothing ? [0 for i in 1:dim] : o + l = l == nothing ? ["" for i in 1:dim] : l + u = u == nothing ? ["" for i in 1:dim] : u + for i in 1:dim typeof(n[i]) <: Integer || throw("All n must be `Integer`") - typeof(d[i]) <: Real || throw("All d must be `Real`") - typeof(o[i]) <: Real || throw("All o must be `Real`") - typeof(l[i]) <: String || throw("All l must be `String`") - typeof(u[i]) <: String || throw("All u must be `String`") + typeof(d[i]) <: Real || throw("All d must be `Real`") + typeof(o[i]) <: Real || throw("All o must be `Real`") + typeof(l[i]) <: String || throw("All l must be `String`") + typeof(u[i]) <: String || throw("All u must be `String`") putint(file, "n$i", n[i]) putfloat(file, "d$i", d[i]) putfloat(file, "o$i", o[i]) @@ -583,15 +452,8 @@ function rsf_write( close(file) end -function rsf_write( - name::String, - dat::AbstractArray, - n = nothing, - d = nothing, - o = nothing, - l = nothing, - u = nothing, -) +function rsf_write(name::String, dat::AbstractArray, n=nothing, d=nothing, + o=nothing, l=nothing, u=nothing) # Madagascar's output function inherits the type of the previous input. # Therefore, in order to have the correct output type, one must create a # dummy input of the correct type. @@ -610,29 +472,16 @@ function rsf_write( else pipe = `$spike n1=1 out=stdout` end - Base.wait(run(pipeline(pipe, stdout = win), wait = false)) + Base.wait(run(pipeline(pipe, stdout=win), wait=false)) redirect_stdin(old_stdin) rsf_read((rin, win)) rsf_write(output(name), dat, n, d, o, l, u) end -rsf_write( - file::Union{String,RSFFile}, - dat::AbstractArray; - n = nothing, - d = nothing, - o = nothing, - l = nothing, - u = nothing, -) = rsf_write(file, dat, n, d, o, l, u) -function rsf_write( - dat::AbstractArray, - n = nothing, - d = nothing, - o = nothing, - l = nothing, - u = nothing, -) +rsf_write(file::Union{String, RSFFile}, dat::AbstractArray; n=nothing, d=nothing, + o=nothing, l=nothing, u=nothing) = rsf_write(file, dat, n, d, o, l, u) +function rsf_write(dat::AbstractArray, n=nothing, d=nothing, o=nothing, + l=nothing, u=nothing) tag = temporary_rsf() rsf_write(tag, dat, n, d, o, l, u) return tag @@ -648,7 +497,7 @@ function rsf_write(tag::String, file::RSFFile) return rsf_write(file, tag) end -function process_args(; kwargs...) +function process_args(;kwargs...) args = String[] for (key, val) in kwargs if typeof(val) <: Tuple @@ -664,7 +513,7 @@ end function delete_rsf(tag::String) sfrm = joinpath(m8r.RSFROOT, "bin", "sfrm") fname = String[tag] - return run(`$sfrm $fname`, wait = false) + return run(`$sfrm $fname`, wait=false) end function temporary_rsf() @@ -678,73 +527,62 @@ function temporary_rsf() end if RSFROOT ≠ nothing - progs = filter(x -> startswith(x, "sf"), readdir(joinpath(RSFROOT, "bin"))) - for (F, S) in [(Symbol(p), p) for p in progs] + progs = filter(x -> startswith(x, "sf"), + readdir(joinpath(RSFROOT, "bin"))) + for (F, S) = [ (Symbol(p), p) for p in progs ] @eval export $F @eval begin progname = $S - manfile = joinpath(m8r.RSFROOT, "share", "man", "man1", progname * ".1") + manfile = joinpath(m8r.RSFROOT, "share", "man", "man1", + progname*".1") if isfile(manfile) old_stdout = stdout (rout, wout) = redirect_stdout() - run( - pipeline( - `man $manfile`, - stdout = wout, - stdin = devnull, - stderr = devnull, - ), - ) + run(pipeline(`man $manfile`, stdout=wout, stdin=devnull, + stderr=devnull)) Base.close(wout) manpage = String(readavailable(rout)) manpage = replace(manpage, "\n" => "\n\t") - manpage = "\n# RSF Documentation\n" * manpage + manpage = "\n# RSF Documentation\n"*manpage Base.close(rout) redirect_stdout(old_stdout) else manpage = "" end - """ - $progname(input; kwargs...) -> m8r.RSFFile - - Runs RSF program `$progname` on the data provided by `input`. This may be an - `m8r.RSFFile` or an array (and optionally, positional and keyword arguments - n, d, o, l, u). If the program requires no input, it may be absent. - - It is also possible to pass keyword arguments to the `$progname`. See `?m8r` - for examples. - - $manpage""" - function ($F)(; kwargs...) - out_tag = temporary_rsf() - args = process_args(; kwargs...) - progpath = joinpath(RSFROOT, "bin", $S) - pipe = `$progpath $args` - run(pipeline(pipe, stdout = out_tag)) - return input(out_tag; temp = true) - end +""" + $progname(input; kwargs...) -> m8r.RSFFile + +Runs RSF program `$progname` on the data provided by `input`. This may be an +`m8r.RSFFile` or an array (and optionally, positional and keyword arguments +n, d, o, l, u). If the program requires no input, it may be absent. + +It is also possible to pass keyword arguments to the `$progname`. See `?m8r` +for examples. + +$manpage""" + function ($F)(;kwargs...) + out_tag = temporary_rsf() + args = process_args(;kwargs...) + progpath = joinpath(RSFROOT, "bin", $S) + pipe = `$progpath $args` + run(pipeline(pipe, stdout=out_tag)) + return input(out_tag; temp=true) + end end @eval function ($F)(in_file::RSFFile; kwargs...) out_tag = temporary_rsf() - args = process_args(; kwargs...) + args = process_args(;kwargs...) progpath = joinpath(RSFROOT, "bin", $S) pipe = `$progpath $args` - run(pipeline(pipe, stdin = in_file.tag, stdout = out_tag)) + run(pipeline(pipe, stdin=in_file.tag, stdout=out_tag)) if in_file.temp delete_rsf(in_file.tag) end - return input(out_tag; temp = true) + return input(out_tag; temp=true) end - @eval function ($F)( - dat::AbstractArray, - n = nothing, - d = nothing, - o = nothing, - l = nothing, - u = nothing; - kwargs..., - ) + @eval function ($F)(dat::AbstractArray, n=nothing, d=nothing, o=nothing, + l=nothing, u=nothing; kwargs...) return rsf_write(dat, n, d, o, l, u) |> x -> $F(x; kwargs...) end @eval function ($F)(tag::String; kwargs...) diff --git a/api/julia/test/afdm.jl b/api/julia/test/afdm.jl index 964499f169..ea4477db07 100755 --- a/api/julia/test/afdm.jl +++ b/api/julia/test/afdm.jl @@ -1,9 +1,9 @@ #!/usr/bin/env julia import m8r -c0 = -30.0f0 / 12.0f0 -c1 = +16.0f0 / 12.0f0 -c2 = -1.0f0 / 12.0f0 +c0 = -30.f0/12.f0 +c1 = +16.f0/12.f0 +c2 = - 1.f0/12.f0 verb = m8r.getbool("verb", false) # verbosity vel = m8r.getstring("vel") @@ -32,7 +32,7 @@ oout = [oz ox ot] lout = [lz lx lt] uout = [uz ux ut] Fo = m8r.output("out") -for i = 1:length(nout) +for i in 1:length(nout) m8r.putint(Fo, "n$i", nout[i]) m8r.putfloat(Fo, "d$i", dout[i]) m8r.putfloat(Fo, "o$i", oout[i]) @@ -40,9 +40,9 @@ for i = 1:length(nout) m8r.putstring(Fo, "unit$i", uout[i]) end -dt2 = dt * dt -idz = 1.0f0 / (dz * dz) -idx = 1.0f0 / (dx * dx) +dt2 = dt*dt +idz = 1.f0/(dz*dz) +idx = 1.f0/(dx*dx) # allocate temporary arrays function run_afdm() @@ -51,16 +51,15 @@ function run_afdm() up = zero(vv) ud = zero(vv) - for it = 1:nt + for it in 1:nt if verb print(stderr, "\b\b\b\b\b $it") end - @views @. ud[3:end-2, 3:end-2] = - c0 * uo[3:end-2, 3:end-2] * (idx + idz) + - c1 * (uo[3:end-2, 2:end-3] + uo[3:end-2, 4:end-1]) * idx + - c2 * (uo[3:end-2, 1:end-4] + uo[3:end-2, 5:end]) * idx + - c1 * (uo[2:end-3, 3:end-2] + uo[4:end-1, 3:end-2]) * idz + - c2 * (uo[1:end-4, 3:end-2] + uo[5:end, 3:end-2]) * idz + @views @. ud[3:end-2, 3:end-2] = c0 * uo[3:end-2, 3:end-2] * (idx+idz) + + c1*(uo[3:end-2, 2:end-3] + uo[3:end-2, 4:end-1])*idx + + c2*(uo[3:end-2, 1:end-4] + uo[3:end-2, 5:end ])*idx + + c1*(uo[2:end-3, 3:end-2] + uo[4:end-1, 3:end-2])*idz + + c2*(uo[1:end-4, 3:end-2] + uo[5:end, 3:end-2])*idz # inject wavelet ud = @. ud - ww[it] * rr @@ -68,12 +67,12 @@ function run_afdm() # scale by velocity ud = @. ud * vv * vv - # time step - up = @. 2.0f0uo - um + ud * dt2 + # time step + up = @. 2f0uo - um + ud * dt2 um = uo uo = up - m8r.floatwrite(vec(uo), nz * nx, Fo) + m8r.floatwrite(vec(uo), nz*nx, Fo) end end @fastmath @inbounds run_afdm() @@ -81,3 +80,4 @@ end if verb println(stderr, "\n") end + diff --git a/api/julia/test/clip.jl b/api/julia/test/clip.jl index 9c415ac5e7..0421a199de 100755 --- a/api/julia/test/clip.jl +++ b/api/julia/test/clip.jl @@ -12,8 +12,9 @@ clip = m8r.getfloat("clip") trace = Array{Float32}(undef, n1) -for i2 = 1:n2 +for i2 in 1:n2 m8r.floatread(trace, n1, inp) clamp!(trace, -clip, clip) m8r.floatwrite(trace, n1, out) end + diff --git a/api/julia/test/runtests.jl b/api/julia/test/runtests.jl index 5a822086e5..e1e29a1fe0 100755 --- a/api/julia/test/runtests.jl +++ b/api/julia/test/runtests.jl @@ -10,37 +10,33 @@ println("RSFROOT") println("getfloat") @test m8r.getfloat("float1") ≈ 0 -@test m8r.getfloat("float2", 2) ≈ 2 -@test m8r.getfloat("float3", val = 3) ≈ 3 +@test m8r.getfloat("float2", 2) ≈ 2 +@test m8r.getfloat("float3", val=3) ≈ 3 @test m8r.getfloat("float4") ≈ 0 println("getint") @test m8r.getint("int1") == 1 @test m8r.getint("int2", 2) == 2 -@test m8r.getint("int3", val = 3) == 3 +@test m8r.getint("int3", val=3) == 3 @test m8r.getint("int4") == 0 println("getstring") @test m8r.getstring("str1") == "ḉ" @test m8r.getstring("str2", "2") == "2" -@test m8r.getstring("str3", val = "3") == "3" +@test m8r.getstring("str3", val="3") == "3" @test m8r.getstring("str4") == "" println("getbool") @test m8r.getbool("bool1") == false @test m8r.getbool("bool2", false) == false -@test m8r.getbool("bool3", val = false) == false +@test m8r.getbool("bool3", val=false) == false @test m8r.getbool("bool4") == true println("input uchar") -run(pipeline(`echo "a bA?"`, stdout = "test_inp_uchar.txt")) -run( - pipeline( - `echo n1=5 data_format=ascii_uchar in=test_inp_uchar.txt - out=stdout`, - stdout = "test_inp_uchar.rsf", - ), -) +run(pipeline(`echo "a bA?"`, stdout="test_inp_uchar.txt")) +run(pipeline(`echo n1=5 data_format=ascii_uchar in=test_inp_uchar.txt + out=stdout`, + stdout="test_inp_uchar.rsf")) inp = m8r.input("test_inp_uchar.rsf") @test m8r.size(inp) == (5,) @test m8r.gettype(inp) == 1 @@ -85,7 +81,7 @@ m8r.close(out) old_stdout = stdout (rout, wout) = redirect_stdout() -run(pipeline(`sfdisfil`, stdin = "test_out_uchar.rsf", stdout = wout)) +run(pipeline(`sfdisfil`, stdin="test_out_uchar.rsf", stdout=wout)) data = String(readavailable(rout)) close(rout) redirect_stdout(old_stdout) @@ -93,13 +89,9 @@ redirect_stdout(old_stdout) run(`sfrm test_out_uchar.rsf`) println("input char") -run(pipeline(`echo "a bA?"`, stdout = "test_inp_char.txt")) -run( - pipeline( - `echo n1=5 data_format=ascii_char in=test_inp_char.txt out=stdout`, - stdout = "test_inp_char.rsf", - ), -) +run(pipeline(`echo "a bA?"`, stdout="test_inp_char.txt")) +run(pipeline(`echo n1=5 data_format=ascii_char in=test_inp_char.txt out=stdout`, + stdout="test_inp_char.rsf")) inp = m8r.input("test_inp_char.rsf") @test m8r.size(inp) == (5,) @test m8r.gettype(inp) == 2 @@ -143,7 +135,7 @@ m8r.close(out) old_stdout = stdout (rout, wout) = redirect_stdout() -run(pipeline(`sfdisfil`, stdin = "test_out_char.rsf", stdout = wout)) +run(pipeline(`sfdisfil`, stdin="test_out_char.rsf", stdout=wout)) data = String(readavailable(rout)) close(rout) redirect_stdout(old_stdout) @@ -151,17 +143,11 @@ redirect_stdout(old_stdout) run(`sfrm test_out_char.rsf`) println("input int") -run( - pipeline( - pipeline( - `sfspike n1=2 k1=1,2,2 - n2=3 k2=1,2,3 - nsp=3 mag=1,4,2`, - `sfdd type=int`, - ), - stdout = "test_inp_int.rsf", - ), -) +run(pipeline(pipeline(`sfspike n1=2 k1=1,2,2 + n2=3 k2=1,2,3 + nsp=3 mag=1,4,2`, + `sfdd type=int`), + stdout="test_inp_int.rsf")) inp = m8r.input("test_inp_int.rsf") @test m8r.size(inp) == (2, 3) @test m8r.gettype(inp) == 3 @@ -212,7 +198,7 @@ m8r.close(out) old_stdout = stdout (rout, wout) = redirect_stdout() -run(pipeline(`sfdisfil`, stdin = "test_out_int.rsf", stdout = wout)) +run(pipeline(`sfdisfil`, stdin="test_out_int.rsf", stdout=wout)) data = String(readavailable(rout)) close(rout) redirect_stdout(old_stdout) @@ -221,12 +207,10 @@ run(`sfrm test_out_int.rsf`) println("input float") -run(pipeline( - `sfspike n1=2 k1=1,2,2 - n2=3 k2=1,2,3 - nsp=3 mag=1,4,2 out=stdout`, - stdout = "test_inp_float.rsf", -)) +run(pipeline(`sfspike n1=2 k1=1,2,2 + n2=3 k2=1,2,3 + nsp=3 mag=1,4,2 out=stdout`, + stdout="test_inp_float.rsf")) inp = m8r.input("test_inp_float.rsf") @test m8r.size(inp) == (2, 3) @test m8r.gettype(inp) == 4 @@ -274,7 +258,7 @@ m8r.close(out) old_stdout = stdout (rout, wout) = redirect_stdout() -run(pipeline(`sfdisfil`, stdin = "test_out_float.rsf", stdout = wout)) +run(pipeline(`sfdisfil`, stdin="test_out_float.rsf", stdout=wout)) data = String(readavailable(rout)) close(rout) redirect_stdout(old_stdout) @@ -282,18 +266,12 @@ redirect_stdout(old_stdout) run(`sfrm test_out_float.rsf`) println("input complex") -run( - pipeline( - pipeline( - `sfspike n1=2 k1=1,2,2 - n2=3 k2=1,2,3 - nsp=3 mag=1,4,2`, - `sfrtoc`, - `sfmath output='input + I' out=stdout`, - ), - stdout = "test_inp_complex.rsf", - ), -) +run(pipeline(pipeline(`sfspike n1=2 k1=1,2,2 + n2=3 k2=1,2,3 + nsp=3 mag=1,4,2`, + `sfrtoc`, + `sfmath output='input + I' out=stdout`), + stdout="test_inp_complex.rsf")) inp = m8r.input("test_inp_complex.rsf") @test m8r.size(inp) == (2, 3) @test m8r.gettype(inp) == 5 @@ -327,7 +305,7 @@ m8r.putstring(out, "label2", "é") m8r.putstring(out, "unit1", "普通话") m8r.putstring(out, "unit2", "µm") -m8r.complexwrite(ComplexF32[0.5 + im; 2 + im], Int32[m8r.leftsize(out, 0)][], out) +m8r.complexwrite(ComplexF32[0.5+im; 2+im], Int32[m8r.leftsize(out, 0)][], out) @test m8r.histint(out, "n1") == 1 @test m8r.histint(out, "n2") == 2 @@ -343,7 +321,7 @@ m8r.close(out) old_stdout = stdout (rout, wout) = redirect_stdout() -run(pipeline(`sfdisfil`, stdin = "test_out_complex.rsf", stdout = wout)) +run(pipeline(`sfdisfil`, stdin="test_out_complex.rsf", stdout=wout)) data = String(readavailable(rout)) close(rout) redirect_stdout(old_stdout) @@ -351,17 +329,11 @@ redirect_stdout(old_stdout) run(`sfrm test_out_complex.rsf`) println("input short") -run( - pipeline( - pipeline( - `sfspike n1=2 k1=1,2,2 - n2=3 k2=1,2,3 - nsp=3 mag=1,4,2`, - `sfdd type=short out=stdout`, - ), - stdout = "test_inp_short.rsf", - ), -) +run(pipeline(pipeline(`sfspike n1=2 k1=1,2,2 + n2=3 k2=1,2,3 + nsp=3 mag=1,4,2`, + `sfdd type=short out=stdout`), + stdout="test_inp_short.rsf")) inp = m8r.input("test_inp_short.rsf") @test m8r.size(inp) == (2, 3) @test m8r.gettype(inp) == 6 @@ -411,7 +383,7 @@ m8r.close(out) old_stdout = stdout (rout, wout) = redirect_stdout() -run(pipeline(`sfdisfil`, stdin = "test_out_short.rsf", stdout = wout)) +run(pipeline(`sfdisfil`, stdin="test_out_short.rsf", stdout=wout)) data = String(readavailable(rout)) close(rout) redirect_stdout(old_stdout) @@ -420,28 +392,29 @@ run(`sfrm test_out_short.rsf`) println("prog") println(" read") -dat, n, d, o, l, u = - sfspike(n1 = 4, n2 = 2, nsp = 2, k1 = (1, 2), mag = (1, 3)) |> - x -> sfwindow(x; n1 = 1, squeeze = false) |> rsf_read -@test dat ≈ [1.0 1.0] +dat, n, d, o, l, u = sfspike(n1=4, n2=2, nsp=2, k1=(1,2), mag=(1,3)) |> + x -> sfwindow(x; n1=1, squeeze=false) |> + rsf_read +@test dat ≈ [1. 1.] @test n == [1, 2] @test d ≈ [0.004, 0.1] @test o ≈ [0, 0] @test l == ["Time", "Distance"] @test u == ["s", "km"] -dat, n, d, o, l, u = - sfspike(n1 = 4, nsp = 2, k1 = (1, 2), mag = (1, 3)) |> - sfrtoc |> - x -> sfmath(x; output = "input + I") |> x -> sfwindow(x; n1 = 3) |> rsf_read -@test dat ≈ [1.0 + 1.0im, 3.0 + 1.0im, 1.0im] +dat, n, d, o, l, u = sfspike(n1=4, nsp=2, k1=(1,2), mag=(1,3)) |> + sfrtoc |> + x -> sfmath(x; output="input + I") |> + x -> sfwindow(x; n1=3) |> + rsf_read +@test dat ≈ [1.0+1.0im, 3.0+1.0im, 1.0im] @test n == [3] @test d ≈ [0.004] @test o ≈ [0] @test l == ["Time"] @test u == ["s"] -dat, n, d, o, l, u = sfwindow([1 2; 2 3; 4 5]; n1 = 1) |> rsf_read +dat, n, d, o, l, u = sfwindow([1 2; 2 3; 4 5]; n1=1) |> rsf_read @test [1, 2] == dat @test n == [2] @test d ≈ [1.0] @@ -450,15 +423,9 @@ dat, n, d, o, l, u = sfwindow([1 2; 2 3; 4 5]; n1 = 1) |> rsf_read @test u == [""] println(" write") -dat, n, d, o, l, u = - rsf_write( - [1.1; 0; 0.5], - [1 3], - [0.1 0.2 0.3], - [0.4 0.5 0.5], - ["a" "b" "c"], - ["d" "e" "f"], - ) |> rsf_read +dat, n, d, o, l, u = rsf_write([1.1; 0; 0.5], [1 3], [.1 .2 .3], [.4 .5 .5], + ["a" "b" "c"], ["d" "e" "f"]) |> + rsf_read @test dat ≈ [1.1 0 0.5] @test n == [1, 3] @test d ≈ [0.1, 0.2] @@ -466,15 +433,10 @@ dat, n, d, o, l, u = @test l == ["a", "b"] @test u == ["d", "e"] -dat, n, d, o, l, u = - rsf_write( - [im; 0; 0.5], - [1 3], - [0.1 0.2 0.3], - [0.4 0.5 0.5], - ["a" "b" "c"], - ["d" "e" "f"], - ) |> x -> sfadd(x; scale = 2) |> rsf_read +dat, n, d, o, l, u = rsf_write([im; 0; 0.5], [1 3], [.1 .2 .3], [.4 .5 .5], + ["a" "b" "c"], ["d" "e" "f"]) |> + x -> sfadd(x; scale=2) |> + rsf_read @test dat ≈ [2im 0 1] @test n == [1, 3] @test d ≈ [0.1, 0.2] @@ -482,9 +444,9 @@ dat, n, d, o, l, u = @test l == ["a", "b"] @test u == ["d", "e"] -sfspike(; n1 = 1) |> x -> rsf_write("test_write.rsf", x) +sfspike(;n1=1) |> x -> rsf_write("test_write.rsf", x) dat, n, d, o, l, u = rsf_read("test_write.rsf") -@test dat ≈ [1.0] +@test dat ≈ [1.] @test n == [1] @test d ≈ [0.004] @test o ≈ [0] @@ -492,9 +454,9 @@ dat, n, d, o, l, u = rsf_read("test_write.rsf") @test u == ["s"] run(`sfrm test_write.rsf`) -sfspike(; n1 = 1) |> x -> rsf_write(x, "test_write.rsf") +sfspike(;n1=1) |> x -> rsf_write(x, "test_write.rsf") dat, n, d, o, l, u = rsf_read("test_write.rsf") -@test dat ≈ [1.0] +@test dat ≈ [1.] @test n == [1] @test d ≈ [0.004] @test o ≈ [0] @@ -502,13 +464,12 @@ dat, n, d, o, l, u = rsf_read("test_write.rsf") @test u == ["s"] run(`sfrm test_write.rsf`) -dat, n, d, o, l, u = - rsf_write([5 2; 3 4], d = [0.1, 0.2], o = [1, 2], l = ["t", "x"], u = ["s", "m"]) |> - rsf_read +dat, n, d, o, l, u = rsf_write([5 2; 3 4], d=[0.1, 0.2], o=[1,2], l=["t", "x"], + u=["s", "m"]) |> rsf_read @test dat ≈ [5 2; 3 4] @test n == [2, 2] @test d ≈ [0.1, 0.2] -@test o ≈ [1, 2] +@test o ≈ [1,2] @test l == ["t", "x"] @test u == ["s", "m"] diff --git a/book/rsf/rsf/afdm/SConstruct b/book/rsf/rsf/afdm/SConstruct index 6a21a2d0b3..9d611fdb66 100644 --- a/book/rsf/rsf/afdm/SConstruct +++ b/book/rsf/rsf/afdm/SConstruct @@ -4,8 +4,7 @@ from rsf.proj import * prog = {} -# for lang in ('c','c++','f90','python','julia','chapel'): -for lang in ('c++'): +for lang in ('c','c++','f90','python','julia','chapel'): if lang != 'julia' or WhereIs('julia'): SConscript('../../../../api/%s/test/SConstruct' % lang) prog[lang] = '../../../../api/%s/test/afdm.exe' % lang