text:
# This file is a part of Julia. License is MIT: https://julialang.org/license

baremodule Base

using Core.Intrinsics, Core.IR
ccall(:jl_set_istopmod, Cvoid, (Any, Bool), Base, true)

getproperty(x, f::Symbol) = getfield(x, f)
setproperty!(x, f::Symbol, v) = setfield!(x, f, convert(fieldtype(typeof(x), f), v))

# Try to help prevent users from shooting them-selves in the foot
# with ambiguities by defining a few common and critical operations
# (and these don't need the extra convert code)
getproperty(x::Module, f::Symbol) = getfield(x, f)
setproperty!(x::Module, f::Symbol, v) = setfield!(x, f, v)
getproperty(x::Type, f::Symbol) = getfield(x, f)
setproperty!(x::Type, f::Symbol, v) = setfield!(x, f, v)

function include(mod::Module, path::AbstractString)
    local result
    if INCLUDE_STATE === 1
        result = _include1(mod, path)
    elseif INCLUDE_STATE === 2
        result = _include(mod, path)
    elseif INCLUDE_STATE === 3
        result = include_relative(mod, path)
    end
    result
end
function include(path::AbstractString)
    local result
    if INCLUDE_STATE === 1
        result = _include1(Base, path)
    elseif INCLUDE_STATE === 2
        result = _include(Base, path)
    else
        # to help users avoid error (accidentally evaluating into Base), this is deprecated
        depwarn("Base.include(string) is deprecated, use `include(fname)` or `Base.include(@__MODULE__, fname)` instead.", :include)
        result = include_relative(_current_module(), path)
    end
    result
end
const _included_files = Array{Tuple{Module,String},1}()
function _include1(mod::Module, path)
    Core.Compiler.push!(_included_files, (mod, ccall(:jl_prepend_cwd, Any, (Any,), path)))
    Core.include(mod, path)
end
let SOURCE_PATH = ""
    # simple, race-y TLS, relative include
    global _include
    function _include(mod::Module, path)
        prev = SOURCE_PATH
        path = joinpath(dirname(prev), path)
        push!(_included_files, (mod, abspath(path)))
        SOURCE_PATH = path
        result = Core.include(mod, path)
        SOURCE_PATH = prev
        result
    end
end
INCLUDE_STATE = 1 # include = Core.include

baremodule MainInclude
export include
include(fname::AbstractString) = Main.Base.include(Main, fname)
end

include("coreio.jl")

eval(x) = Core.eval(Base, x)
eval(m, x) = Core.eval(m, x)
VecElement{T}(arg) where {T} = VecElement{T}(convert(T, arg))
convert(::Type{T}, arg)  where {T<:VecElement} = T(arg)
convert(::Type{T}, arg::T) where {T<:VecElement} = arg

# init core docsystem
import Core: @doc, @__doc__, WrappedException
if isdefined(Core, :Compiler)
    import Core.Compiler.CoreDocs
    Core.atdoc!(CoreDocs.docm)
end

include("exports.jl")

if false
    # simple print definitions for debugging. enable these if something
    # goes wrong during bootstrap before printing code is available.
    # otherwise, they just just eventually get (noisily) overwritten later
    global show, print, println
    show(io::IO, x) = Core.show(io, x)
    print(io::IO, a...) = Core.print(io, a...)
    println(io::IO, x...) = Core.println(io, x...)
end

## Load essential files and libraries
include("essentials.jl")
include("ctypes.jl")
include("gcutils.jl")
include("generator.jl")
include("reflection.jl")
include("options.jl")

# core operations & types
include("promotion.jl")
include("tuple.jl")
include("pair.jl")
include("traits.jl")
include("range.jl")
include("expr.jl")
include("error.jl")

# core numeric operations & types
include("bool.jl")
include("number.jl")
include("int.jl")
include("operators.jl")
include("pointer.jl")
include("refpointer.jl")
include("checked.jl")
using .Checked

# vararg Symbol constructor
Symbol(x...) = Symbol(string(x...))

# Define the broadcast function, which is mostly implemented in
# broadcast.jl, so that we can overload broadcast methods for
# specific array types etc.
#  --Here, just define fallback routines for broadcasting with no arguments
broadcast(f) = f()
broadcast!(f, X::AbstractArray) = (@inbounds for I in eachindex(X); X[I] = f(); end; X)

# array structures
include("indices.jl")
include("array.jl")
include("abstractarray.jl")
include("subarray.jl")
include("reinterpretarray.jl")

# ## dims-type-converting Array constructors for convenience
# type and dimensionality specified, accepting dims as series of Integers
Vector{T}(::Uninitialized, m::Integer) where {T} = Vector{T}(uninitialized, Int(m))
Matrix{T}(::Uninitialized, m::Integer, n::Integer) where {T} = Matrix{T}(uninitialized, Int(m), Int(n))
# type but not dimensionality specified, accepting dims as series of Integers
Array{T}(::Uninitialized, m::Integer) where {T} = Array{T,1}(uninitialized, Int(m))
Array{T}(::Uninitialized, m::Integer, n::Integer) where {T} = Array{T,2}(uninitialized, Int(m), Int(n))
Array{T}(::Uninitialized, m::Integer, n::Integer, o::Integer) where {T} = Array{T,3}(uninitialized, Int(m), Int(n), Int(o))
Array{T}(::Uninitialized, d::Integer...) where {T} = Array{T}(uninitialized, convert(Tuple{Vararg{Int}}, d))
# dimensionality but not type specified, accepting dims as series of Integers
Vector(::Uninitialized, m::Integer) = Vector{Any}(uninitialized, Int(m))
Matrix(::Uninitialized, m::Integer, n::Integer) = Matrix{Any}(uninitialized, Int(m), Int(n))
# empty vector constructor
Vector() = Vector{Any}(uninitialized, 0)

# Array constructors for nothing and missing
# type and dimensionality specified
Array{T,N}(::Nothing, d...) where {T,N} = fill!(Array{T,N}(uninitialized, d...), nothing)
Array{T,N}(::Missing, d...) where {T,N} = fill!(Array{T,N}(uninitialized, d...), missing)
# type but not dimensionality specified
Array{T}(::Nothing, d...) where {T} = fill!(Array{T}(uninitialized, d...), nothing)
Array{T}(::Missing, d...) where {T} = fill!(Array{T}(uninitialized, d...), missing)

include("abstractdict.jl")

include("namedtuple.jl")

# numeric operations
include("hashing.jl")
include("rounding.jl")
using .Rounding
include("float.jl")
include("twiceprecision.jl")
include("complex.jl")
include("rational.jl")
include("multinverses.jl")
using .MultiplicativeInverses
include("abstractarraymath.jl")
include("arraymath.jl")

# define MIME"foo/bar" early so that we can overload 3-arg show
struct MIME{mime} end
macro MIME_str(s)
    :(MIME{$(Expr(:quote, Symbol(s)))})
end
# fallback text/plain representation of any type:
show(io::IO, ::MIME"text/plain", x) = show(io, x)

# SIMD loops
include("simdloop.jl")
using .SimdLoop

# map-reduce operators
include("reduce.jl")

## core structures
include("reshapedarray.jl")
include("bitarray.jl")
include("bitset.jl")

if !isdefined(Core, :Compiler)
    include("docs/core.jl")
    Core.atdoc!(CoreDocs.docm)
end

# Some type
include("some.jl")

include("dict.jl")
include("set.jl")
include("iterators.jl")
using .Iterators: zip, enumerate
using .Iterators: Flatten, product  # for generators

include("char.jl")
include("strings/basic.jl")
include("strings/string.jl")

# Definition of StridedArray
StridedReshapedArray{T,N,A<:Union{DenseArray,FastContiguousSubArray}} = ReshapedArray{T,N,A}
StridedReinterpretArray{T,N,A<:Union{DenseArray,FastContiguousSubArray}} = ReinterpretArray{T,N,S,A} where S
StridedArray{T,N,A<:Union{DenseArray,StridedReshapedArray},
    I<:Tuple{Vararg{Union{RangeIndex, AbstractCartesianIndex}}}} =
    Union{DenseArray{T,N}, SubArray{T,N,A,I}, StridedReshapedArray{T,N}, StridedReinterpretArray{T,N,A}}
StridedVector{T,A<:Union{DenseArray,StridedReshapedArray},
    I<:Tuple{Vararg{Union{RangeIndex, AbstractCartesianIndex}}}} =
    Union{DenseArray{T,1}, SubArray{T,1,A,I}, StridedReshapedArray{T,1}, StridedReinterpretArray{T,1,A}}
StridedMatrix{T,A<:Union{DenseArray,StridedReshapedArray},
    I<:Tuple{Vararg{Union{RangeIndex, AbstractCartesianIndex}}}} =
    Union{DenseArray{T,2}, SubArray{T,2,A,I}, StridedReshapedArray{T,2}, StridedReinterpretArray{T,2,A}}
StridedVecOrMat{T} = Union{StridedVector{T}, StridedMatrix{T}}

# For OS specific stuff
include(string((length(Core.ARGS)>=2 ? Core.ARGS[2] : ""), "build_h.jl"))     # include($BUILDROOT/base/build_h.jl)
include(string((length(Core.ARGS)>=2 ? Core.ARGS[2] : ""), "version_git.jl")) # include($BUILDROOT/base/version_git.jl)

include("osutils.jl")
include("c.jl")

# Core I/O
include("io.jl")
include("iostream.jl")
include("iobuffer.jl")

# strings & printing
include("intfuncs.jl")
include("strings/strings.jl")
include("parse.jl")
include("shell.jl")
include("regex.jl")
include("show.jl")
include("arrayshow.jl")

# multidimensional arrays
include("cartesian.jl")
using .Cartesian
include("multidimensional.jl")
include("permuteddimsarray.jl")
using .PermutedDimsArrays

include("broadcast.jl")
using .Broadcast

# define the real ntuple functions
@inline function ntuple(f::F, ::Val{N}) where {F,N}
    N::Int
    (N >= 0) || throw(ArgumentError(string("tuple length should be ≥0, got ", N)))
    if @generated
        quote
            @nexprs $N i -> t_i = f(i)
            @ncall $N tuple t
        end
    else
        Tuple(f(i) for i = 1:N)
    end
end
@inline function fill_to_length(t::Tuple, val, ::Val{N}) where {N}
    M = length(t)
    M > N && throw(ArgumentError("input tuple of length $M, requested $N"))
    if @generated
        quote
            (t..., $(fill(:val, N-length(t.parameters))...))
        end
    else
        (t..., fill(val, N-M)...)
    end
end

# version
include("version.jl")

# system & environment
include("sysinfo.jl")
include("libc.jl")
using .Libc: getpid, gethostname, time

const DL_LOAD_PATH = String[]
if Sys.isapple()
    push!(DL_LOAD_PATH, "@loader_path/julia")
    push!(DL_LOAD_PATH, "@loader_path")
end

include("env.jl")

# Scheduling
include("libuv.jl")
include("event.jl")
include("task.jl")
include("lock.jl")
include("threads.jl")
include("weakkeydict.jl")

# Logging
include("logging.jl")
using .CoreLogging
global_logger(SimpleLogger(Core.STDERR, CoreLogging.Info))

# To limit dependency on rand functionality (implemented in the Random
# module), Crand is used in file.jl, and could be used in error.jl
# (but it breaks a test)
"""
    Crand([T::Type])

Interface to the C `rand()` function. If `T` is provided, generate a value of type `T`
by composing two calls to `Crand()`. `T` can be `UInt32` or `Float64`.
"""
Crand() = ccall(:rand, Cuint, ())
# RAND_MAX at least 2^15-1 in theory, but we assume 2^16-1 (in practice, it's 2^31-1)
Crand(::Type{UInt32}) = ((Crand() % UInt32) << 16) ⊻ (Crand() % UInt32)
Crand(::Type{Float64}) = Crand(UInt32) / 2^32

"""
    Csrand([seed])

Interface the the C `srand(seed)` function.
"""
Csrand(seed=floor(time())) = ccall(:srand, Cvoid, (Cuint,), seed)

# functions defined in Random
function rand end
function randn end

# I/O
include("stream.jl")
include("socket.jl")
include("filesystem.jl")
using .Filesystem
include("process.jl")
include("grisu/grisu.jl")
include("methodshow.jl")

# core math functions
include("floatfuncs.jl")
include("math.jl")
using .Math
import .Math: gamma
const (√)=sqrt
const (∛)=cbrt

INCLUDE_STATE = 2 # include = _include (from lines above)

# reduction along dims
include("reducedim.jl")  # macros in this file relies on string.jl

# basic data structures
include("ordering.jl")
using .Order

# Combinatorics
include("sort.jl")
using .Sort

# Fast math
include("fastmath.jl")
using .FastMath

function deepcopy_internal end

# BigInts and BigFloats
include("gmp.jl")
using .GMP

for T in [Signed, Integer, BigInt, Float32, Float64, Real, Complex, Rational]
    @eval flipsign(x::$T, ::Unsigned) = +x
    @eval copysign(x::$T, ::Unsigned) = +x
end

include("mpfr.jl")
using .MPFR
big(n::Integer) = convert(BigInt,n)
big(x::AbstractFloat) = convert(BigFloat,x)
big(q::Rational) = big(numerator(q))//big(denominator(q))

include("combinatorics.jl")

# more hashing definitions
include("hashing2.jl")

# irrational mathematical constants
include("irrationals.jl")
include("mathconstants.jl")
using .MathConstants: ℯ, π, pi

# (s)printf macros
include("printf.jl")
# import .Printf

# metaprogramming
include("meta.jl")

# enums
include("Enums.jl")
using .Enums

# concurrency and parallelism
include("channels.jl")

# utilities
include("deepcopy.jl")
include("clipboard.jl")
include("download.jl")
include("summarysize.jl")
include("errorshow.jl")

# Stack frames and traces
include("stacktraces.jl")
using .StackTraces

include("initdefs.jl")
include("client.jl")

# misc useful functions & macros
include("util.jl")

# statistics
include("statistics.jl")

# missing values
include("missing.jl")

# worker threads
include("threadcall.jl")

# code loading
include("uuid.jl")
include("loading.jl")

# set up depot & load paths to be able to find stdlib packages
let BINDIR = ccall(:jl_get_julia_bindir, Any, ())
    init_depot_path(BINDIR)
    init_load_path(BINDIR)
end

INCLUDE_STATE = 3 # include = include_relative

import Base64

INCLUDE_STATE = 2

include("asyncmap.jl")

include("multimedia.jl")
using .Multimedia

# deprecated functions
include("deprecated.jl")

# Some basic documentation
include("docs/basedocs.jl")

# Documentation -- should always be included last in sysimg.
include("docs/Docs.jl")
using .Docs
isdefined(Core, :Compiler) && Docs.loaddocs(Core.Compiler.CoreDocs.DOCS)

function __init__()
    # for the few uses of Crand in Base:
    Csrand()
    # Base library init
    reinit_stdio()
    global_logger(root_module(PkgId("Logging")).ConsoleLogger(STDERR))
    Multimedia.reinit_displays() # since Multimedia.displays uses STDOUT as fallback
    early_init()
    init_depot_path()
    init_load_path()
    init_threadcall()
end

INCLUDE_STATE = 3 # include = include_relative

end # baremodule Base

using Base

# Ensure this file is also tracked
pushfirst!(Base._included_files, (@__MODULE__, joinpath(@__DIR__, "sysimg.jl")))

# load some stdlib packages but don't put their names in Main
Base.require(Base, :Base64)
Base.require(Base, :CRC32c)
Base.require(Base, :Dates)
Base.require(Base, :DelimitedFiles)
Base.require(Base, :Serialization)
Base.require(Base, :Distributed)
Base.require(Base, :FileWatching)
Base.require(Base, :Future)
Base.require(Base, :IterativeEigensolvers)
Base.require(Base, :Libdl)
Base.require(Base, :LinearAlgebra)
Base.require(Base, :Logging)
Base.require(Base, :Mmap)
Base.require(Base, :Printf)
Base.require(Base, :Profile)
Base.require(Base, :Random)
Base.require(Base, :SharedArrays)
Base.require(Base, :SparseArrays)
Base.require(Base, :SuiteSparse)
Base.require(Base, :Test)
Base.require(Base, :Unicode)
Base.require(Base, :LibGit2)
Base.require(Base, :Pkg)
Base.require(Base, :REPL)
Base.require(Base, :Markdown)

@eval Base begin
    @deprecate_binding Test root_module(Base, :Test) true ", run `using Test` instead"
    @deprecate_binding Mmap root_module(Base, :Mmap) true ", run `using Mmap` instead"
    @deprecate_binding Profile root_module(Base, :Profile) true ", run `using Profile` instead"
    @deprecate_binding Dates root_module(Base, :Dates) true ", run `using Dates` instead"
    @deprecate_binding Distributed root_module(Base, :Distributed) true ", run `using Distributed` instead"
    @deprecate_binding Random root_module(Base, :Random) true ", run `using Random` instead"
    @deprecate_binding Serializer root_module(Base, :Serialization) true ", run `using Serialization` instead"
    @deprecate_binding Libdl root_module(Base, :Libdl) true ", run `using Libdl` instead"
    @deprecate_binding Markdown root_module(Base, :Markdown) true ", run `using Markdown` instead"

    # PR #25249
    @deprecate_binding SparseArrays root_module(Base, :SparseArrays) true ", run `using SparseArrays` instead"
    @deprecate_binding(AbstractSparseArray, root_module(Base, :SparseArrays).AbstractSparseArray, true,
        ", run `using SparseArrays` to load sparse array functionality")
    @deprecate_binding(AbstractSparseMatrix, root_module(Base, :SparseArrays).AbstractSparseMatrix, true,
        ", run `using SparseArrays` to load sparse array functionality")
    @deprecate_binding(AbstractSparseVector, root_module(Base, :SparseArrays).AbstractSparseVector, true,
        ", run `using SparseArrays` to load sparse array functionality")
    @deprecate_binding(SparseMatrixCSC, root_module(Base, :SparseArrays).SparseMatrixCSC, true,
        ", run `using SparseArrays` to load sparse array functionality")
    @deprecate_binding(SparseVector, root_module(Base, :SparseArrays).SparseVector, true,
        ", run `using SparseArrays` to load sparse array functionality")

    @deprecate_binding(SharedArray, root_module(Base, :SharedArrays).SharedArray, true,
        ", run `using SharedArrays` to load shared array functionality")

    # PR #25571
    @deprecate_binding LinAlg root_module(Base, :LinearAlgebra) true ", run `using LinearAlgebra` instead"
    @deprecate_binding(I, root_module(Base, :LinearAlgebra).I, true,
        ", run `using LinearAlgebra` to load linear algebra functionality.")

    # PR 25544
    @deprecate_binding REPL            root_module(Base, :REPL)                 true ", run `using REPL` instead"
    @deprecate_binding LineEdit        root_module(Base, :REPL).LineEdit        true ", use `REPL.LineEdit` instead"
    @deprecate_binding REPLCompletions root_module(Base, :REPL).REPLCompletions true ", use `REPL.REPLCompletions` instead"
    @deprecate_binding Terminals       root_module(Base, :REPL).Terminals       true ", use `REPL.Terminals` instead"

    @deprecate_binding Pkg root_module(Base, :Pkg) true ", run `using Pkg` instead"
    @deprecate_binding LibGit2 root_module(Base, :LibGit2) true ", run `import LibGit2` instead"

    @eval @deprecate_binding $(Symbol("@doc_str")) getfield(root_module(Base, :Markdown), Symbol("@doc_str")) true ", use `Markdown` instead"

    @deprecate_stdlib readdlm  DelimitedFiles true
    @deprecate_stdlib writedlm DelimitedFiles true
    @deprecate_stdlib readcsv  DelimitedFiles true
    @deprecate_stdlib writecsv DelimitedFiles true

    @eval @deprecate_stdlib $(Symbol("@profile")) Profile true

    @deprecate_stdlib base64encode Base64 true
    @deprecate_stdlib base64decode Base64 true
    @deprecate_stdlib Base64EncodePipe Base64 true
    @deprecate_stdlib Base64DecodePipe Base64 true

    @deprecate_stdlib poll_fd FileWatching true
    @deprecate_stdlib poll_file FileWatching true
    @deprecate_stdlib PollingFileWatcher FileWatching true
    @deprecate_stdlib watch_file FileWatching true
    @deprecate_stdlib FileMonitor FileWatching true

    @eval @deprecate_stdlib $(Symbol("@spawn")) Distributed true
    @eval @deprecate_stdlib $(Symbol("@spawnat")) Distributed true
    @eval @deprecate_stdlib $(Symbol("@fetch")) Distributed true
    @eval @deprecate_stdlib $(Symbol("@fetchfrom")) Distributed true
    @eval @deprecate_stdlib $(Symbol("@everywhere")) Distributed true
    @eval @deprecate_stdlib $(Symbol("@parallel")) Distributed true

    @deprecate_stdlib addprocs Distributed true
    @deprecate_stdlib CachingPool Distributed true
    @deprecate_stdlib clear! Distributed true
    @deprecate_stdlib ClusterManager Distributed true
    @deprecate_stdlib default_worker_pool Distributed true
    @deprecate_stdlib init_worker Distributed true
    @deprecate_stdlib interrupt Distributed true
    @deprecate_stdlib launch Distributed true
    @deprecate_stdlib manage Distributed true
    @deprecate_stdlib myid Distributed true
    @deprecate_stdlib nprocs Distributed true
    @deprecate_stdlib nworkers Distributed true
    @deprecate_stdlib pmap Distributed true
    @deprecate_stdlib procs Distributed true
    @deprecate_stdlib remote Distributed true
    @deprecate_stdlib remotecall Distributed true
    @deprecate_stdlib remotecall_fetch Distributed true
    @deprecate_stdlib remotecall_wait Distributed true
    @deprecate_stdlib remote_do Distributed true
    @deprecate_stdlib rmprocs Distributed true
    @deprecate_stdlib workers Distributed true
    @deprecate_stdlib WorkerPool Distributed true
    @deprecate_stdlib RemoteChannel Distributed true
    @deprecate_stdlib Future Distributed true
    @deprecate_stdlib WorkerConfig Distributed true
    @deprecate_stdlib RemoteException Distributed true
    @deprecate_stdlib ProcessExitedException Distributed true

    @deprecate_stdlib crc32c CRC32c true

    @deprecate_stdlib DateTime Dates true
    @deprecate_stdlib DateFormat Dates true
    @eval @deprecate_stdlib $(Symbol("@dateformat_str")) Dates true
    @deprecate_stdlib now Dates true

    @deprecate_stdlib eigs IterativeEigensolvers true
    @deprecate_stdlib svds IterativeEigensolvers true

    @eval @deprecate_stdlib $(Symbol("@printf")) Printf true
    @eval @deprecate_stdlib $(Symbol("@sprintf")) Printf true

    # PR #24874
    @deprecate_stdlib rand! Random true
    @deprecate_stdlib srand Random true
    @deprecate_stdlib AbstractRNG Random true
    @deprecate_stdlib randcycle  Random true
    @deprecate_stdlib randcycle!  Random true
    @deprecate_stdlib randperm  Random true
    @deprecate_stdlib randperm! Random true
    @deprecate_stdlib shuffle  Random true
    @deprecate_stdlib shuffle! Random true
    @deprecate_stdlib randsubseq Random true
    @deprecate_stdlib randsubseq! Random true
    @deprecate_stdlib randstring Random true
    @deprecate_stdlib MersenneTwister  Random true
    @deprecate_stdlib RandomDevice  Random true
    @deprecate_stdlib randn! Random true
    @deprecate_stdlib randexp Random true
    @deprecate_stdlib randexp! Random true
    @deprecate_stdlib bitrand Random true
    @deprecate_stdlib randjump Random true
    @deprecate_stdlib GLOBAL_RNG Random false

    @deprecate_stdlib serialize Serialization true
    @deprecate_stdlib deserialize Serialization true
    @deprecate_stdlib AbstractSerializer Serialization true
    @deprecate_stdlib SerializationState Serialization true

    # PR #25249: SparseArrays to stdlib
    ## the Base.SparseArrays module itself and exported types are deprecated in base/sysimg.jl
    ## functions that were re-exported from Base
    @deprecate_stdlib nonzeros   SparseArrays true
    @deprecate_stdlib permute    SparseArrays true
    @deprecate_stdlib blkdiag    SparseArrays true
    @deprecate_stdlib dropzeros  SparseArrays true
    @deprecate_stdlib dropzeros! SparseArrays true
    @deprecate_stdlib issparse   SparseArrays true
    @deprecate_stdlib sparse     SparseArrays true
    @deprecate_stdlib sparsevec  SparseArrays true
    @deprecate_stdlib spdiagm    SparseArrays true
    @deprecate_stdlib sprand     SparseArrays true
    @deprecate_stdlib sprandn    SparseArrays true
    @deprecate_stdlib spzeros    SparseArrays true
    @deprecate_stdlib rowvals    SparseArrays true
    @deprecate_stdlib nzrange    SparseArrays true
    @deprecate_stdlib nnz        SparseArrays true
    @deprecate_stdlib findnz     SparseArrays true
    ## functions that were exported from Base.SparseArrays but not from Base
    @deprecate_stdlib droptol!   SparseArrays false
    ## deprecated functions that are moved to stdlib/SparseArrays/src/deprecated.jl
    @deprecate_stdlib spones     SparseArrays true
    @deprecate_stdlib speye      SparseArrays true

    # PR #25571: LinearAlgebra to stdlib
    @deprecate_stdlib BLAS        LinearAlgebra true
    ## functions that were re-exported from Base
    @deprecate_stdlib bkfact!     LinearAlgebra true
    @deprecate_stdlib bkfact      LinearAlgebra true
    @deprecate_stdlib chol        LinearAlgebra true
    @deprecate_stdlib cholfact!   LinearAlgebra true
    @deprecate_stdlib cholfact    LinearAlgebra true
    @deprecate_stdlib cond        LinearAlgebra true
    @deprecate_stdlib condskeel   LinearAlgebra true
    @deprecate_stdlib cross       LinearAlgebra true
    @deprecate_stdlib adjoint!    LinearAlgebra true
    # @deprecate_stdlib adjoint     LinearAlgebra true
    @deprecate_stdlib det         LinearAlgebra true
    @deprecate_stdlib diag        LinearAlgebra true
    @deprecate_stdlib diagind     LinearAlgebra true
    @deprecate_stdlib diagm       LinearAlgebra true
    @deprecate_stdlib diff        LinearAlgebra true
    @deprecate_stdlib dot         LinearAlgebra true
    @deprecate_stdlib eig         LinearAlgebra true
    @deprecate_stdlib eigfact!    LinearAlgebra true
    @deprecate_stdlib eigfact     LinearAlgebra true
    @deprecate_stdlib eigmax      LinearAlgebra true
    @deprecate_stdlib eigmin      LinearAlgebra true
    @deprecate_stdlib eigvals     LinearAlgebra true
    @deprecate_stdlib eigvals!    LinearAlgebra true
    @deprecate_stdlib eigvecs     LinearAlgebra true
    @deprecate_stdlib factorize   LinearAlgebra true
    @deprecate_stdlib givens      LinearAlgebra true
    @deprecate_stdlib hessfact!   LinearAlgebra true
    @deprecate_stdlib hessfact    LinearAlgebra true
    @deprecate_stdlib isdiag      LinearAlgebra true
    @deprecate_stdlib ishermitian LinearAlgebra true
    @deprecate_stdlib isposdef!   LinearAlgebra true
    @deprecate_stdlib isposdef    LinearAlgebra true
    @deprecate_stdlib issymmetric LinearAlgebra true
    @deprecate_stdlib istril      LinearAlgebra true
    @deprecate_stdlib istriu      LinearAlgebra true
    # @deprecate_stdlib kron        LinearAlgebra true
    @deprecate_stdlib ldltfact    LinearAlgebra true
    @deprecate_stdlib ldltfact!   LinearAlgebra true
    @deprecate_stdlib linreg      LinearAlgebra true
    @deprecate_stdlib logabsdet   LinearAlgebra true
    @deprecate_stdlib logdet      LinearAlgebra true
    @deprecate_stdlib lu          LinearAlgebra true
    @deprecate_stdlib lufact!     LinearAlgebra true
    @deprecate_stdlib lufact      LinearAlgebra true
    @deprecate_stdlib lyap        LinearAlgebra true
    @deprecate_stdlib norm        LinearAlgebra true
    @deprecate_stdlib normalize   LinearAlgebra true
    @deprecate_stdlib normalize!  LinearAlgebra true
    @deprecate_stdlib nullspace   LinearAlgebra true
    @deprecate_stdlib ordschur!   LinearAlgebra true
    @deprecate_stdlib ordschur    LinearAlgebra true
    @deprecate_stdlib peakflops   LinearAlgebra true
    @deprecate_stdlib pinv        LinearAlgebra true
    @deprecate_stdlib qr          LinearAlgebra true
    @deprecate_stdlib qrfact!     LinearAlgebra true
    @deprecate_stdlib qrfact      LinearAlgebra true
    @deprecate_stdlib lq          LinearAlgebra true
    @deprecate_stdlib lqfact!     LinearAlgebra true
    @deprecate_stdlib lqfact      LinearAlgebra true
    @deprecate_stdlib rank        LinearAlgebra true
    @deprecate_stdlib scale!      LinearAlgebra true
    @deprecate_stdlib schur       LinearAlgebra true
    @deprecate_stdlib schurfact!  LinearAlgebra true
    @deprecate_stdlib schurfact   LinearAlgebra true
    @deprecate_stdlib svd         LinearAlgebra true
    @deprecate_stdlib svdfact!    LinearAlgebra true
    @deprecate_stdlib svdfact     LinearAlgebra true
    @deprecate_stdlib svdvals!    LinearAlgebra true
    @deprecate_stdlib svdvals     LinearAlgebra true
    @deprecate_stdlib sylvester   LinearAlgebra true
    @deprecate_stdlib trace       LinearAlgebra true
    @deprecate_stdlib transpose!  LinearAlgebra true
    # @deprecate_stdlib transpose   LinearAlgebra true
    @deprecate_stdlib tril!       LinearAlgebra true
    @deprecate_stdlib tril        LinearAlgebra true
    @deprecate_stdlib triu!       LinearAlgebra true
    @deprecate_stdlib triu        LinearAlgebra true
    @deprecate_stdlib vecdot      LinearAlgebra true
    @deprecate_stdlib vecnorm     LinearAlgebra true
    # @deprecate_stdlib ⋅           LinearAlgebra true
    # @deprecate_stdlib ×           LinearAlgebra true

    ## types that were re-exported from Base
    @deprecate_stdlib Diagonal        LinearAlgebra true
    @deprecate_stdlib Bidiagonal      LinearAlgebra true
    @deprecate_stdlib Tridiagonal     LinearAlgebra true
    @deprecate_stdlib SymTridiagonal  LinearAlgebra true
    @deprecate_stdlib UpperTriangular LinearAlgebra true
    @deprecate_stdlib LowerTriangular LinearAlgebra true
    @deprecate_stdlib Symmetric       LinearAlgebra true
    @deprecate_stdlib Hermitian       LinearAlgebra true
    @deprecate_stdlib Factorization   LinearAlgebra true
    @deprecate_stdlib UniformScaling  LinearAlgebra true
    @deprecate_stdlib Adjoint         LinearAlgebra true
    @deprecate_stdlib Transpose       LinearAlgebra true

    ## functions that were exported from Base.LinAlg but not from Base
    @deprecate_stdlib axpy!           LinearAlgebra false
    @deprecate_stdlib axpby!          LinearAlgebra false
    @deprecate_stdlib copy_transpose! LinearAlgebra false
    @deprecate_stdlib issuccess       LinearAlgebra false
    @deprecate_stdlib transpose_type  LinearAlgebra false
    @deprecate_stdlib A_mul_B!        LinearAlgebra false
    @deprecate_stdlib A_mul_Bt!       LinearAlgebra false
    @deprecate_stdlib At_mul_B!       LinearAlgebra false
    @deprecate_stdlib At_mul_Bt!      LinearAlgebra false
    @deprecate_stdlib A_mul_Bc!       LinearAlgebra false
    @deprecate_stdlib Ac_mul_B!       LinearAlgebra false
    @deprecate_stdlib Ac_mul_Bc!      LinearAlgebra false
    @deprecate_stdlib A_ldiv_B!       LinearAlgebra false
    @deprecate_stdlib At_ldiv_B!      LinearAlgebra false
    @deprecate_stdlib Ac_ldiv_B!      LinearAlgebra false

    ## types that were exported from Base.LinAlg but not from Base
    @deprecate_stdlib BunchKaufman     LinearAlgebra false
    @deprecate_stdlib Cholesky         LinearAlgebra false
    @deprecate_stdlib CholeskyPivoted  LinearAlgebra false
    @deprecate_stdlib Eigen            LinearAlgebra false
    @deprecate_stdlib GeneralizedEigen LinearAlgebra false
    @deprecate_stdlib GeneralizedSVD   LinearAlgebra false
    @deprecate_stdlib GeneralizedSchur LinearAlgebra false
    @deprecate_stdlib Hessenberg       LinearAlgebra false
    @deprecate_stdlib LU               LinearAlgebra false
    @deprecate_stdlib LDLt             LinearAlgebra false
    @deprecate_stdlib QR               LinearAlgebra false
    @deprecate_stdlib QRPivoted        LinearAlgebra false
    @deprecate_stdlib LQ               LinearAlgebra false
    @deprecate_stdlib Schur            LinearAlgebra false
    @deprecate_stdlib SVD              LinearAlgebra false

    ## deprecated functions that are moved to stdlib/LinearAlgebra/src/deprecated.jl
    @deprecate_stdlib eye        LinearAlgebra true
    @deprecate_stdlib sqrtm      LinearAlgebra true
    @deprecate_stdlib expm       LinearAlgebra true
    @deprecate_stdlib expm!      LinearAlgebra true
    @deprecate_stdlib logm       LinearAlgebra true
    @deprecate_stdlib gradient   LinearAlgebra true
    @deprecate_stdlib ConjArray  LinearAlgebra true
    @deprecate_stdlib ConjVector LinearAlgebra true
    @deprecate_stdlib ConjMatrix LinearAlgebra true
    @deprecate_stdlib RowVector  LinearAlgebra true

    # PR #25021
    @deprecate_stdlib normalize_string Unicode true
    @deprecate_stdlib graphemes Unicode true
    @deprecate_stdlib is_assigned_char Unicode true

    @deprecate_stdlib whos          InteractiveUtils true
    @deprecate_stdlib subtypes      InteractiveUtils true
    @deprecate_stdlib apropos       InteractiveUtils true
    @deprecate_stdlib edit          InteractiveUtils true
    @deprecate_stdlib less          InteractiveUtils true
    @deprecate_stdlib code_llvm     InteractiveUtils true
    @deprecate_stdlib code_native   InteractiveUtils true
    @deprecate_stdlib code_warntype InteractiveUtils true
    @deprecate_stdlib methodswith   InteractiveUtils true
    @deprecate_stdlib varinfo       InteractiveUtils true
    @deprecate_stdlib versioninfo   InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@which"))         InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@edit"))          InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@less"))          InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@functionloc"))   InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@code_typed"))    InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@code_warntype")) InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@code_lowered"))  InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@code_llvm"))     InteractiveUtils true
    @eval @deprecate_stdlib $(Symbol("@code_native"))   InteractiveUtils true
end

empty!(DEPOT_PATH)
empty!(LOAD_PATH)

Base.isfile("userimg.jl") && Base.include(Main, "userimg.jl")

Base.include(Base, "precompile.jl")

max_stars_repo_name: BenChung/Julia-BTI

max_issues_repo_name: BenChung/Julia-BTI

max_forks_repo_name: BenChung/Julia-BTI


text:
using DataStructures

@testgroup "testcases" begin

@testcase "testcase dependencies" begin

    fx1 = @global_fixture begin
        @produce 1
    end

    fx2 = @global_fixture for x in fx1
        @produce x
    end

    fx3 = @local_fixture for x in fx1
        @produce x
    end

    tcs = Jute.collect_testobjs() do
        @testcase "tc" for x in fx2, y in fx3
            @test 1 == 1
        end
    end
    tc = tcs[1]

    @test Jute.dependencies(tc) == OrderedSet([fx1, fx2])
    @test Jute.parameters(tc) == [fx2, fx3]
end

@testcase "tagging" begin
    tcs = Jute.collect_testobjs() do
        @testcase "no tags" begin
        end

        @testcase tags=[:tag1, :tag2] "some tags" begin
        end
    end

    @test Jute.tags(tcs[1]) == Set([])
    @test Jute.tags(tcs[2]) == Set([:tag1, :tag2])
end

end

ext: jl


text:
##
## Fun with pi!
##
## Tomáš Kalvoda, KAM FIT ČVUT, 2015
##

##
## References: J. Gibbons, Unbounded Spigot Algorithms for the Digits of pi
##
## http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf
##

function nextdigit(state)
    q,r,t,i = state
    u, y = (3*(3*i+1)*(3*i+2), div(q*(27*i-12)+5*r, 5*t))
    return (y, [10*q*i*(2*i-1), 10*u*(q*(5*i-2)+r-y*t), t*u, i+1])
end

function pidigits(n)
    dig = Int[]
    d, state = nextdigit(BigInt[1, 180, 60, 2])
    for j in 1:n
        push!(dig,d)
        d, state = nextdigit(state)
    end
    return dig
end

##
## Progression
##

const ndigits = 2000

info("Computing first $(ndigits) digits of pi...")
mypi = pidigits(ndigits)

info("Counting number occurances of each digit...")
dcount = Dict{Int64,Int64}({i => 0 for i in 0:9})
ddrawn = Dict{Int64,Int64}({i => 0 for i in 0:9})

for d in mypi
    dcount[d] += 1
end

digit_angle = 0.85 * (360. / 10.)
space_angle = 0.15 * (360. / 10.)
delta_angle = Dict{Int64,Float64}({d => digit_angle / (dcount[d] - 1) for d in 0:9})

radius  = 4
opacity = 0.7

# tex head

texout = "\\documentclass{standalone}
\\usepackage[T1]{fontenc}
\\usepackage{tikz}

\\definecolor{d0}{RGB}{253,253,150}
\\definecolor{d1}{RGB}{174,198,207}
\\definecolor{d2}{RGB}{244,154,194}
\\definecolor{d3}{RGB}{255,105,97}
\\definecolor{d4}{RGB}{130,105,83}
\\definecolor{d5}{RGB}{255,179,71}
\\definecolor{d6}{RGB}{3,192,60}
\\definecolor{d7}{RGB}{207,207,196}
\\definecolor{d8}{RGB}{119,158,203}
\\definecolor{d9}{RGB}{203,153,201}

\\begin{document}
\\begin{tikzpicture}\n"

# connections

for i in 1:(length(mypi)-1)
    d1 = mypi[i]
    d2 = mypi[i+1]
    angled1 = d1*(36.) + ddrawn[d1] * delta_angle[d1]
    outd1 = angled1 + 180.
    angled2 = d2*(36.) + ddrawn[d2] * delta_angle[d2]
    outd2 = angled2 + 180.
    texout = string(texout, "\\draw[thin, opacity=$(opacity), d$(d1)] ($(angled1):$(radius)) to[out=$(outd1),in=$(outd2)] ($(angled2):$(radius));\n")
    ddrawn[d1] += 1
end

# legend

for d in 0:9
    angle1 = d*36
    angle2 = (d+1)*36 - space_angle
    texout = string(texout, "\\draw[very thick, d$(d)] ($(angle1):$(radius)) arc ($(angle1):$(angle2):$(radius));\n")
    texout = string(texout, "\\node at ($(angle1 + 18):{1.1*$(radius)}) {$(d)};\n")
end

# tail

texout = string(texout,"\\end{tikzpicture}\n\\end{document}\n")

# output

info("Writing .tex file...")
fileout = open("pi.tex","w")
write(fileout, texout)
close(fileout)

max_stars_repo_name: kalvotom/julia-examples

max_issues_repo_name: kalvotom/julia-examples

max_forks_repo_name: kalvotom/julia-examples


text:
####################################################
#                   CONTAINS ALL THE TEST
# Those test are:
# - Activation functions
# - Neuronal network structure
# - Forward Propagation
# - Our initialization algorithm
####################################################
include("../src/OptimizedNeuralNetwork.jl")
using .OptimizedNeuralNetwork

println("Testing Activation functions...")
t = @elapsed include("activation_functions.test.jl")
println("done (took $t seconds).")

println("Testing Neuronal Network Data type...")
t = @elapsed include("one_layer_neural_network.test.jl")
println("done (took $t seconds).")

println("Testing forward_propagation...")
t = @elapsed include("forward_propagation.test.jl")
println("done (took $t seconds).")

println("Testing our initialization algorithm")
t = @elapsed include("weight-inizializer-algorithm/main.test.jl")
println("done (took $t seconds).")

println("Testing metric estimation")
t = @elapsed include("metric_estimation.test.jl")
println("done (took $t seconds).")

println("Testing backpropagation")
t = @elapsed include("backpropagation.test.jl")
println("done (took $t seconds).")


text:
function mrp(q::Quaternion)
    q̄ = vector(q)
    return q̄[2:4] ./ (q̄[1] + 1.0)
end

function mrp(q::AbstractVector)
    return q[2:4] ./ (q[1] + 1.0)
end

function dmrpdq(q::AbstractVector)
    s = q[1]
    v = q[2:4]

    d1 = 1 / (s + 1)^2
    di = 1 / (s + 1)

    [-v[1] * d1  di 0.0 0.0;
     -v[2] * d1 0.0 di 0.0;
     -v[3] * d1 0.0 0.0 di]
end

function axis(q)
    m = mrp(q)
    mag = norm(m)
    if mag > 0
        # θ = 4.0 * atan(mag)
        n = m ./ mag
    else
        # θ = 0.0
        n = [1.0; 0.0; 0.0]
    end
    return n
end

function daxisdq(q::AbstractVector)
    m = mrp(q)
    dmrpdq(q) ./ norm(m) - m ./ norm(m)^2 * transpose(m ./ norm(m)) * dmrpdq(q)
end

function angle(q)
    m = mrp(q)
    mag = norm(m)
    if mag > 0
        θ = 4.0 * atan(mag)
    else
        θ = 0.0
    end
    return θ
end

function dangledq(q::AbstractVector)
    m = mrp(q)
    4.0 * 1.0 / (1.0 + norm(m)^2.0) * transpose(m ./ norm(m)) * dmrpdq(q)
end

function axis_angle(q)
    return axis(q), angle(q)
end

function rotation_vector(q)
    n, θ = axis_angle(q)
    return θ .* n
end

function drotation_vectordq(q::AbstractVector)
    θ = angle(q)
    if θ != 0.0
        a = axis(q)
        return a * dangledq(q) + θ * daxisdq(q)
    else
        [
            0.0  2.0  0.0  0.0;
            0.0  0.0  2.0  0.0;
            0.0  0.0  0.0  2.0;
        ]
    end
end

drotation_vectordq(q::Quaternion) = drotation_vectordq(vector(q))


text:
struct MOLFiniteDifference{T} <: DiffEqBase.AbstractDiscretization
  dxs::T
  order::Int
end
MOLFiniteDifference(args...;order=2) = MOLFiniteDifference(args,order)

function DiffEqBase.discretize(pdesys::PDESystem,discretization::MOLFiniteDifference)
  tdomain = pdesys.domain[1].domain
  domain = pdesys.domain[2].domain
  @assert domain isa IntervalDomain
  len = domain.upper - domain.lower
  dx = discretization.dxs[1]
  interior = domain.lower+dx:dx:domain.upper-dx
  X = domain.lower:dx:domain.upper
  L = CenteredDifference(2,2,dx,Int(len/dx)-2)
  Q = DirichletBC(0.0,0.0)
  function f(du,u,p,t)
    mul!(du,L,Array(Q*u))
  end
  u0 = @. - interior * (interior - 1) * sin(interior)
  PDEProblem(ODEProblem(f,u0,(tdomain.lower,tdomain.upper),nothing),Q,X)
end


text:
"when the client fail to connect to the server"
struct ConnectionException <: Exception
    message::AbstractString
end

"when the response from the server doesn't conform to RESP"
struct ProtocolException <: Exception
    message::AbstractString
end

"when the server returns an error response [http://redis.io/topics/protocol#resp-errors]()"
struct RedisException <: Exception
    message::AbstractString
end

"when timeout expired"
struct TimeoutException <: Exception
    message::AbstractString
end


text:
using Distributions

export generate_small_dataset, generate_big_dataset

START = 0
STEP_SIZE = 1 / 30
NOISE_MU = 0  # mean of noise

function _generate_toy_data(
    data_samples,
    true_samples,
    f1,
    f2,
    f3;
    observation_noise = 0.05,
    extended_true_period = 0,
    nr_nuked_intervals=0,
    nuked_per_interval=0  # Number of elements to remove from the middle of the data
)
    # Generate True function
    stop = STEP_SIZE * data_samples
    x_true = range(START, stop + extended_true_period, length = true_samples)
    y1_true = f1(x_true)
    y2_true = f2(x_true, y1_true)
    y3_true = f3(x_true, y1_true, y2_true)
    y_true = [y1_true, y2_true, y3_true]

    # Create the noisy normal distribution
    normal_noise = Normal(NOISE_MU, observation_noise^2)
    # Add the noise to the observations
    # TODO: make noise scale with y
    x = range(START, stop, length = data_samples)
    x, removed_elms = nuke(x, nr_nuked_intervals, nuked_per_interval)
    y1 = f1(x) + rand(normal_noise, data_samples - removed_elms)
    y2 = f2(x, y1) + rand(normal_noise, data_samples - removed_elms)
    y3 = f3(x, y1, y2) + rand(normal_noise, data_samples - removed_elms)
    y_obs = [y1, y2, y3]

    return x, y_obs, x_true, y_true
end

function nuke(x, nr_nuked_intervals, nuked_per_interval)
    # Remove two intervals of data
    if nr_nuked_intervals == 0
        return x, 0
    end
    kept_elements = length(x) ÷ (nr_nuked_intervals + 1)
    accumulator = [x[1:kept_elements]]
    for i in 1:nr_nuked_intervals
        append!(accumulator,
            [x[i * kept_elements + 1 + nuked_per_interval: (i + 1)*kept_elements]]
            )
    end
    nuked_x = vcat(accumulator...)
    removed_elms = length(x) - length(nuked_x)
    return nuked_x, removed_elms
end

SMALL_DATA_SAMPLES = 30
SMALL_TRUE_SAMPLES = 1000 # discretised points used to model true function
# Functions used to generate data
f1_small = (x -> -sin.(10 * pi .* (x .+ 1)) ./ (2 .* x .+ 1) - x .^ 4)
f2_small = ((x, y1) -> cos.(y1) .^ 2 + sin.(3 .* x))
f3_small = ((x, y1, y2) -> y2 .* (y1 .^ 2) + 3 * x)

observation_noise_small = 0.05
generate_small_dataset() = _generate_toy_data(
    SMALL_DATA_SAMPLES,
    SMALL_TRUE_SAMPLES,
    f1_small,
    f2_small,
    f3_small;
    observation_noise=observation_noise_small,
)

BIG_DATA_SAMPLES = 10000
BIG_TRUE_SAMPLES = 1000000 # discretised points used to model true function

f1_big = (x -> 3 .+ -sin.(1 / 10 * pi .* (x .+ 1)) - x .^ 0.3)
f2_big = ((x, y1) -> cos.(y1) .^ 2 + sin.(pi / 20 .* x))
f3_big = ((x, y1, y2) -> y2 .* (y1 .^ 2) + 0.1 .* x)

observation_noise_big = 0.8
extended_true_period_big = 50  # plot the true data for up to 50 datapoints after
# Removing subsets of data
nr_nuked_intervals_big = 5
nuked_per_interval_big = 300
generate_big_dataset() = _generate_toy_data(
    BIG_DATA_SAMPLES,
    BIG_TRUE_SAMPLES,
    f1_big,
    f2_big,
    f3_big,
    observation_noise=observation_noise_big,
    extended_true_period=extended_true_period_big,
    nr_nuked_intervals=nr_nuked_intervals_big,
    nuked_per_interval=nuked_per_interval_big
)

max_stars_repo_name: willtebbutt/GPAR-at-scale

max_issues_repo_name: willtebbutt/GPAR-at-scale

max_forks_repo_name: willtebbutt/GPAR-at-scale


text:
using FMMTrees
#using Plots; plotlyjs()

p = rand(40)
p, tp = clustertree(p)

q = rand(40)
q, tq = clustertree(q)

depthfirst(tp) do b,l
    println(l, ": ", b[1].begin_idx:b[1].end_idx-1)
end

depthfirst(tq) do b,l
    println(l, ": ", b[1].begin_idx:b[1].end_idx-1)
end

@assert first(children((tp,tq)))[1] isa FMMTrees.Tree
@assert first(children((tp,tq)))[2] isa FMMTrees.Tree

S = zeros(length(p), length(p))
depthfirst((tp,tp)) do b,l
    I = b[1][1].begin_idx : b[1][1].end_idx-1
    J = b[2][1].begin_idx : b[2][1].end_idx-1
    println(l, ": ", I, "×", J)
    l == 5 && (S[I,J] = rand(1:20))
end

# Visual representation of the block partition at level 3
# heatmap(S)
nmin = 20
η = 1.5
function adm(b)
    I = b[1][1].begin_idx : b[1][1].end_idx-1
    J = b[2][1].begin_idx : b[2][1].end_idx-1
    length(I) < nmin && return true
    length(J) < nmin && return true
    ll1, ur1 = FMMTrees.boundingbox(p[I]); c1 = (ll1+ur1)/2;
    ll2, ur2 = FMMTrees.boundingbox(p[J]); c2 = (ll2+ur2)/2;
    diam1 = norm(ur1-c1)
    diam2 = norm(ur2-c2)
    dist12 = norm(c2-c1)
    return dist12 >= η*max(diam1, diam2)
end

P = admissable_partition((tp,tp), adm)
S = zeros(length(p), length(p))
for b in P
    I = b[1][1].begin_idx : b[1][1].end_idx-1
    J = b[2][1].begin_idx : b[2][1].end_idx-1
    S[I,J] += length(I) + length(J)
end

extrema(S)

# using Plots; plotlyjs();
# heatmap(S)

find(P) do b
    I = b[1][1].begin_idx : b[1][1].end_idx-1
    J = b[2][1].begin_idx : b[2][1].end_idx-1
    length(I) + length(J) == 40
end


text:
@testset "Kabsch algorithm" begin

    @testset "Test I" begin
        a = [1  1 0
             2  1 0
             1+cos(pi/4) 1-sin(pi/4) 0
             1  0 0 ]
        b = [1 1 0
             1 2 0
             1+cos(pi/4) 1+sin(pi/4)  0 ] # mean(b,1) 1.2357  1.56904  0.0
        sa = a[1:3, :]
        ma = mean(sa, dims=1) # mean x, y, z for a[1:3,:]

        # Center b, sa, a
        center!(b)
        sa[:,:] = sa .- ma
        a[:,:]  = a  .- ma # center != 0

        @test isapprox(vec(mean(b, dims=1)), [0.0, 0.0, 0.0], atol=1e-13)
        @test isapprox(vec(mean(sa, dims=1)), [0.0, 0.0, 0.0], atol=1e-13)

        F = kabsch(b, sa) # Reference: b
        R = sa * F
        RR = a * F

        @test R ≈ b
        @test RR[1:3,:] ≈ b

        @test RR[4,:] .- RR[1,:] ≈ [1.0, 0.0, 0.0]
        @test sqrt((RR[1,1] - RR[4,1])^2 + (RR[1,2] - RR[4,2])^2) ≈ 1.0

        @test isapprox(PDB.rmsd(R, b), 0.0, atol=1e-13)
        @test isapprox(PDB.rmsd(RR[1:3,:], b), 0.0, atol=1e-13)
    end

    @testset "BiomolecularStructures' test" begin

        P = [51.65 -1.90 50.07
             50.40 -1.23 50.65
             50.68 -0.04 51.54
             50.22 -0.02 52.85]
        Q = [51.30 -2.99 46.54
             51.09 -1.88 47.58
             52.36 -1.20 48.03
             52.71 -1.18 49.38] # P and Q are from BiomolecularStructures.jl' kabsch tests
        Qdistances = Float64[ sqrt(sum(abs2, Q[j,:] .- Q[i,:])) for i in 1:4, j in 1:4 ]

        center!(P)
        center!(Q)

        @test isapprox(vec(mean(P, dims=1)), zeros(3), atol=1e-13)
        @test isapprox(vec(mean(Q, dims=1)), zeros(3), atol=1e-13)

        rotationmatrix = kabsch(P, Q)
        rotated = Q * rotationmatrix

        @test PDB.rmsd(P, rotated) ≈ 0.0030426652601371583

        # Internal distances mustn't change
        @test Float64[sqrt(sum(abs2, Q[j,:] .- Q[i,:])) for i in 1:4, j in 1:4] ≈ Qdistances # Translated
        @test Float64[sqrt(sum(abs2, rotated[j,:] .- rotated[i,:])) for i in 1:4, j in 1:4] ≈ Qdistances # Translated  and rotated
    end

    @testset "Test II" begin
        P = [-1. 0. 0.
             0. 2. 0.
             0. 1. 0.
             0. 1. 1]
        Q = [0. -1. -1.
             0. -1. 0
             0. 0. 0.
             -1. 0. 0.]

        center!(P)
        center!(Q)
        rotated = Q * kabsch(P, Q)
        @test isapprox(PDB.rmsd(P, rotated), 0.695, atol=0.001)
    end

    @testset "RMSF" begin
        A = [ -1. -1. -1.
              -1.  0.  1. ]
        B = [  1.  1.  1.
              -1.  0.  1. ]
        w = [ .25 , .75 ]

        @test_throws ArgumentError mean_coordinates(Matrix{Float64}[A[1:1,:], B])
        @test_throws ArgumentError mean_coordinates(Matrix{Float64}[A[:,1:2], B])
        @test_throws ArgumentError mean_coordinates(Matrix{Float64}[A])
        @test_throws ArgumentError mean_coordinates(Matrix{Float64}[A,  B'])
        @test_throws ArgumentError mean_coordinates(Matrix{Float64}[A', B'])

        @test mean_coordinates(Matrix{Float64}[A, B]) == [ 0. 0. 0.
                                                          -1. 0. 1. ]

        @test mean_coordinates(Matrix{Float64}[A, B], w) == [ 0.5 0.5 0.5
                                                             -1.  0.  1. ]

        @test mean_coordinates(Matrix{Float64}[A, B], reverse(w)) == [ -0.5 -0.5 -0.5
                                                                       -1.   0.   1. ]

        @test rmsf(Matrix{Float64}[A, B]) == [sqrt(3), 0.]
        @test rmsf(Matrix{Float64}[A, B], w) == [sqrt(0.25*6.75 + 0.75*0.75), 0.]
    end

    @testset "Superimpose PDBs" begin

        hemoglobin = read(joinpath(pwd(),"data","2hhb.pdb.gz"),PDBFile,group="ATOM",model="1")

        α1 = @residues hemoglobin model "1" chain "A" group "ATOM" residue All
        α2 = @residues hemoglobin model "1" chain "C" group "ATOM" residue All
        β1 = @residues hemoglobin model "1" chain "B" group "ATOM" residue All
        β2 = @residues hemoglobin model "1" chain "D" group "ATOM" residue All

        a1, a2, rα = superimpose(α1, α2)

        @test isapprox(rα, 0.230, atol=0.001) # Bio3D RMSD

        _, _, rα2 = superimpose(α1, α2, zip(1:length(α1), 1:length(α2)))
        @test rα2 == rα

        _, _, rα2 = superimpose(α1, α2, zip(1:2, 1:2))
        @test rα2 < rα/10  # aligning only 2 points is accurate

        b1, b2, rβ = superimpose(β1, β2)

        @test isapprox(rβ, 0.251, atol=0.001) # Bio3D & Chimera's MatchMaker RMSD

        @test length(a1) == length(α1)
        @test length(a2) == length(α2)
        @test length(b1) == length(β1)
        @test length(b2) == length(β2)

        @test a1 != α1
        @test a2 != α2

        @test b1 != β1
        @test b2 != β2

        @testset "Vector{PDBResidue}" begin

            @test isapprox(PDB.rmsd(α1, α2), 0.230, atol=0.001)
            @test isapprox(PDB.rmsd(β1, β2), 0.251, atol=0.001)

            @test isapprox(PDB.rmsd(a1, a2, superimposed=true), 0.230, atol=0.001)
            @test isapprox(PDB.rmsd(b1, b2, superimposed=true), 0.251, atol=0.001)

            ca_a1 = CAmatrix(a1)
            ca_a2 = CAmatrix(a2)
            ca_b1 = CAmatrix(b1)
            ca_b2 = CAmatrix(b2)

            @testset "Centered" begin
                @test isapprox(mean(ca_a1, dims=1), zeros(1,3), atol=1e-13)
                @test isapprox(mean(ca_a2, dims=1), zeros(1,3), atol=1e-13)
                @test isapprox(mean(ca_b1, dims=1), zeros(1,3), atol=1e-13)
                @test isapprox(mean(ca_b2, dims=1), zeros(1,3), atol=1e-13)

                @test PDB._iscentered(ca_a1)
                @test PDB._iscentered(ca_a2)
                @test PDB._iscentered(ca_b1)
                @test PDB._iscentered(ca_b2)
            end

            @testset "RMSD" begin

                @test rα == PDB.rmsd(ca_a1, ca_a2)
                @test rβ == PDB.rmsd(ca_b1, ca_b2)
            end
        end

        @testset "coordinatesmatrix and centered..." begin

            @test coordinatesmatrix(centeredresidues(α1)) ≈ coordinatesmatrix(a1)
            @test coordinatesmatrix(centeredresidues(β1)) ≈ coordinatesmatrix(b1)

            @test centeredcoordinates(α1) ≈ coordinatesmatrix(a1)
            @test centeredcoordinates(β1) ≈ coordinatesmatrix(b1)
        end

        @testset "Superimpose with centered PDBs" begin

            b12, b22, rβ2 = superimpose(b1, b2)
            @test rβ2 ≈ rβ
        end

        @testset "RMSF" begin

            @test rmsf(Vector{PDBResidue}[α1,α2]) == rmsf(Vector{PDBResidue}[α1,α2],[.5,.5])
            @test rmsf(Vector{PDBResidue}[β1,β2]) == rmsf(Vector{PDBResidue}[β1,β2],[.5,.5])
        end
    end

    @testset "PDBResidue without alpha-carbon" begin
        small_2WEL = read(joinpath(pwd(), "data", "2WEL_D_region.pdb"), PDBFile)
        small_6BAB = read(joinpath(pwd(), "data", "6BAB_D_region.pdb"), PDBFile)

        aln_2WEL, aln_6BAB, RMSD = superimpose(small_2WEL, small_6BAB)
        @test length(aln_2WEL) == 3
        @test length(aln_6BAB) == 3
        @test small_2WEL[2].atoms[1].coordinates ≉ aln_2WEL[2].atoms[1].coordinates
        @test small_6BAB[2].atoms[1].coordinates ≉ aln_6BAB[2].atoms[1].coordinates
        @test RMSD < 1.0e-14 # e.g. 6.9e-15, 3.7e-15 & 1.6e-15

        @testset "PDB without CA" begin

            filter!(atom -> atom.atom != "CA", small_2WEL[1].atoms)
            filter!(atom -> atom.atom != "CA", small_6BAB[3].atoms)
            @test_throws ArgumentError superimpose(small_2WEL, small_6BAB)
        end
    end
end


text:
function test_lsr1()
  ϵ = eps(Float64)
  rtol = sqrt(ϵ)

  @testset ExtendedTestSet "LSR1" begin
    n = 10
    mem = 5
    B = LSR1Operator(n, mem = mem, scaling = false)

    for t = 1:2
      @test norm(diag(B) - diag(Matrix(B))) <= rtol

      @test B.data.insert == 1
      @test norm(Matrix(B) - Matrix(1.0I, n, n)) <= ϵ

      # Test that only valid updates are accepted.
      s = simple_vector(Float64, n)
      y = B * s
      push!(B, s, y)
      @test B.data.insert == 1

      # Insert a few {s,y} pairs.
      for i = 1:(mem + 2)
        s = ones(n) * i
        y = [i; ones(n - 1)]
        push!(B, s, y)
      end

      @test check_hermitian(B)
      @test norm(diag(B) - diag(Matrix(B))) <= rtol

      v = simple_vector(Float64, n)
      @test norm(B * v - v) > rtol
      reset!(B)
      @test B.data.scaling_factor == 1.0
      @test norm(B * v - v) < rtol
    end

    # test against full SR1 without scaling
    mem = n
    LB = LSR1Operator(n, mem = mem, scaling = false)
    B = Matrix(1.0I, n, n)

    function sr1!(B, s, y)
      # dense SR1 update
      ymBs = y - B * s
      denom = dot(ymBs, s)
      if abs(denom) >= 1.0e-8 + 1.0e-8 * norm(s) * norm(ymBs)
        B = B + ymBs * ymBs' / denom
      end
      return B
    end

    @test norm(Matrix(LB) - B) < rtol * norm(B)
    @test norm(diag(LB) - diag(B)) < rtol * norm(diag(B))

    for k = 1:mem
      s = simple_vector(Float64, n)
      y = simple_vector(Float64, n)
      B = sr1!(B, s, y)
      LB = push!(LB, s, y)
      @test norm(Matrix(LB) - B) < rtol * norm(B)
      @test norm(diag(LB) - diag(B)) < rtol * norm(diag(B))
    end
  end

  @testset ExtendedTestSet "Different precision" begin
    n = 10
    mem = 5
    for T in (Float16, Float32, Float64, BigFloat)
      B = LSR1Operator(T, n, mem = mem)
      s = ones(T, n)
      y = ones(T, n)
      push!(B, s, y)
      @test eltype(B) == T
      v = [-(-one(T))^i for i = 1:n]
      @test eltype(B * v) == T
    end
  end

  @testset "LSR1 allocations" begin
    n = 100
    mem = 20
    B = LSR1Operator(n, mem = mem)
    for _ = 1:2:n
      s = rand(n)
      y = rand(n)
      push!(B, s, y)
    end
    x = rand(n)
    res = similar(x)
    mul!(res, B, x)  # warmup
    nallocs = @allocated mul!(res, B, x)
    @test nallocs == 0
    nallocs = @allocated diag!(B, x)
    @test nallocs == 0
  end

  @testset "LSR1 eigenvalues" begin
    n = 100
    mem = 20
    B = LSR1Operator(n, mem = mem)
    for _ = 1:2:n
      s = rand(n)
      y = rand(n)
      push!(B, s, y)
    end
    vals = eigs(B, nev = n)
    resid = vals[end]
    @test norm(resid) ≤ sqrt(eps(eltype(B)))
  end
end

test_lsr1()


text:
export iauPrec76
"""
IAU 1976 precession model.

This function forms the three Euler angles which implement general
precession between two dates, using the IAU 1976 model (as for the
FK5 catalog).

This function is part of the International Astronomical Union's
SOFA (Standards Of Fundamental Astronomy) software collection.

Status:  canonical model.

Given:
   date01,date02   double    TDB starting date (Note 1)
   date11,date12   double    TDB ending date (Note 1)

Returned:
   zeta            double    1st rotation: radians cw around z
   z               double    3rd rotation: radians cw around z
   theta           double    2nd rotation: radians ccw around y

Notes:

   1. The dates date01+date02 and date11+date12 are Julian Dates,
   apportioned in any convenient way between the arguments daten1
   and daten2.  For example, JD(TDB)=2450123.7 could be expressed in
   any of these ways, among others:

         daten1        daten2

      2450123.7           0.0       (JD method)
      2451545.0       -1421.3       (J2000 method)
      2400000.5       50123.2       (MJD method)
      2450123.5           0.2       (date & time method)

   The JD method is the most natural and convenient to use in cases
   where the loss of several decimal digits of resolution is
   acceptable.  The J2000 method is best matched to the way the
   argument is handled internally and will deliver the optimum
   optimum resolution.  The MJD method and the date & time methods
   are both good compromises between resolution and convenience.
   The two dates may be expressed using different methods, but at
   the risk of losing some resolution.

   2. The accumulated precession angles zeta, z, theta are expressed
   through canonical polynomials which are valid only for a limited
   time span.  In addition, the IAU 1976 precession rate is known to
   be imperfect.  The absolute accuracy of the present formulation
   is better than 0.1 arcsec from 1960AD to 2040AD, better than
   1 arcsec from 1640AD to 2360AD, and remains below 3 arcsec for
   the whole of the period 500BC to 3000AD.  The errors exceed
   10 arcsec outside the range 1200BC to 3900AD, exceed 100 arcsec
   outside 4200BC to 5600AD and exceed 1000 arcsec outside 6800BC to
   8200AD.

   3. The three angles are returned in the conventional order, which
   is not the same as the order of the corresponding Euler
   rotations.  The precession matrix is
   R_3(-z) x R_2(+theta) x R_3(-zeta).

Reference:

   Lieske, J.H., 1979, Astron.Astrophys. 73, 282, equations
   (6) & (7), p283.

This revision:  2013 November 19

SOFA release 2018-01-30

Copyright (C) 2018 IAU SOFA Board.  See notes at end.
"""

# void iauPrec76(double date01, double date02, double date11, double date12,
#                double *zeta, double *z, double *theta)

function iauPrec76(date01::Real, date02::Real, date11::Real, date12::Real)
   # Preallocate return values
   ref_zeta  = Ref{Float64}(0.0)
   ref_z     = Ref{Float64}(0.0)
   ref_theta = Ref{Float64}(0.0)

   ccall((:iauPrec76, libsofa_c), Cvoid,
         (Cdouble, Cdouble, Cdouble, Cdouble,
          Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}),
          convert(Float64, date01), convert(Float64, date02),
          convert(Float64, date11), convert(Float64, date12),
          ref_zeta, ref_z, ref_theta)

   return ref_zeta[], ref_z[], ref_theta[]
end


text:
using SafeTestsets

@safetestset "Vulkan.jl" begin
    include("base.jl")
end

max_stars_repo_name: JuliaGPU/VulkanAbstraction

max_issues_repo_name: JuliaGPU/VulkanAbstraction

max_forks_repo_name: JuliaGPU/VulkanAbstraction


text:
function instantiate_surface_waves()
    ∂t_uˢ(z, t) = exp(z/20) * cos(t)
    ∂t_vˢ(z, t) = exp(z/20) * cos(t)
    ∂z_uˢ(z, t) = exp(z/20) * cos(t)
    ∂z_vˢ(z, t) = exp(z/20) * cos(t)
    surface_waves = SurfaceWaves.UniformStokesDrift(∂t_uˢ=∂t_uˢ, ∂t_vˢ=∂t_vˢ,
                                                    ∂z_uˢ=∂z_uˢ, ∂z_vˢ=∂z_vˢ)

    return true
end

@testset "Surface waves" begin
    println("Testing surface waves...")

    @testset "Surface waves" begin
        @test instantiate_surface_waves()
    end
end


text:
"""
(1-exp(-Λ*x)/x
"""
function kernel(ω::Float)
    if ω < 1e-5
        return 1 - ω / 2 + ω^2 / 6 - ω^3 / 24 + ω^4 / 120 - ω^5 / 720
    else
        # return (1 - exp(-ω)) / ω
        return -expm1(-ω) / ω
    end
end

##################### Particle-hole symmetric kernel #############################

"""
particle-hole symmetric kernel: K(ω, τ)=e^{-ω*τ}+e^{-ω*(β-τ)}

KK=int_0^{1/2} dτ K(ω1,τ)*K(ω2,τ)=(1-e^{ω1+ω2})/(ω1+ω2)+(e^{-ω2}-e^{-ω1})/(ω1-ω2)
"""
function projPH_ω(Λ::Float, ω1::Float, ω2::Float)
        if ω1 > ω2
        return kernel(ω1 + ω2) + exp(-ω2) * kernel(ω1 - ω2)
    else
        return kernel(ω1 + ω2) + exp(-ω1) * kernel(ω2 - ω1)
    end
end

"""
particle-hole symmetric kernel: K(ω, τ)=e^{-ω*τ}+e^{-ω*(β-τ)}

KK=int_0^{Λ} dτ K(ω,t1)*K(ω2,t2)=(1-e^{t1+t2})/(t1+t2)+(1-e^{2β-t1-t2})/(2β-t1-t2)+(1-e^{β+t1-t2})/(β+t1-t2)+(1-e^{β-t1+t2})/(β-t1+t2)
"""
function projPH_τ(Λ::Float, t1::Float, t2::Float)
    # note that here Λ = \beta/2
    return kernel(t1 + t2) + kernel(4 * Λ - t1 - t2) + kernel(2 * Λ - t1 + t2) + kernel(2 * Λ + t1 - t2)
end

##################### Particle-hole asymmetric kernel #############################
"""
particle=hole asymmetric kernel: K(ω, τ)=e^{-ω*τ}-e^{-ω*(β-τ)}

KK=int_0^{1/2} dτ K(ω1,τ)*K(ω2,τ)=(1-e^{ω1+ω2})/(ω1+ω2)-(e^{-ω2}-e^{-ω1})/(ω1-ω2)
"""
function projPHA_ω(Λ::Float, ω1::Float, ω2::Float)
    if ω1 > ω2
    return kernel(ω1 + ω2) - exp(-ω2) * kernel(ω1 - ω2)
    else
    return kernel(ω1 + ω2) - exp(-ω1) * kernel(ω2 - ω1)
    end
end

"""
particle-hole asymmetric kernel: K(ω, τ)=e^{-ω*τ}-e^{-ω*(β-τ)}

KK=int_0^{Λ} dτ K(ω,t1)*K(ω2,t2)=(1-e^{t1+t2})/(t1+t2)+(1-e^{2β-t1-t2})/(2β-t1-t2)-(1-e^{β+t1-t2})/(β+t1-t2)-(1-e^{β-t1+t2})/(β-t1+t2)
"""
function projPHA_τ(Λ::Float, t1::Float, t2::Float)
    return kernel(t1 + t2) + kernel(4 * Λ - t1 - t2) - kernel(2 * Λ - t1 + t2) - kernel(2 * Λ + t1 - t2)
end


text:
__precompile__()
module DownloadFunc

#------------------------------------------------------------------#
#For the time being, we need remove_response function from obspy
#This will be replaced by SeisIO modules in the near future.
#Please activate obspy enviroment before launching Julia.
include("remove_response_obspy.jl")
using .Remove_response_obspy
#------------------------------------------------------------------#

using SeisIO

export seisdownload_NOISE, seisdownload_EARTHQUAKE

"""
    seisdownload_NOISE(startid, InputDict::Dict)

Download seismic data, removing instrumental response and saving into JLD2 file.

# Arguments
- `startid`         : start time id in starttimelist
- `InputDict::Dict` : dictionary which contains request information
"""
function seisdownload_NOISE(startid, InputDict::Dict)

    #stationlist
    stationlist     = InputDict["stationinfo"]["stationlist"]
    method      	= InputDict["stationinfo"]["stationmethod"]
    src             = InputDict["stationinfo"]["stationsrc"]
    starttime       = InputDict["starttime"]
    endtime         = InputDict["endtime"]
    DL_time_unit    = InputDict["DL_time_unit"]
    pre_filt        = InputDict["pre_filt"]

    #make stlist at all processors

    starttimelist = InputDict["starttimelist"]
    timestamplist = InputDict["DLtimestamplist"]

    #show progress
    if starttimelist[startid][end-8:end] == "T00:00:00"
        println("start downloading $(starttimelist[startid])")
    end

    S = SeisData(length(stationlist))
    for i = 1:length(stationlist)
        #---download data---#
        requeststr = stationlist[i]

		if InputDict["IsLocationBox"]
	        #argv = [method[i], requeststr, starttimelist[startid], DL_time_unit, InputDict["reg"], 0, src[i], false, "$requeststr.$startid.xml"]
	        ex = :(get_data($(method[i]), $(requeststr), s=$(starttimelist[startid]), t=$(DL_time_unit), reg=$(InputDict["reg"]), v=$(0), src=$(src[i]), xf=$("$requeststr.$startid.xml")))
	        Stemp = check_and_get_data(ex, requeststr)
		else
			#argv = [method[i], requeststr, starttimelist[startid], DL_time_unit, 0, src[i], false, "$requeststr.$startid.xml"]
			ex = :(get_data($(method[i]), $(requeststr), s=$(starttimelist[startid]), t=$(DL_time_unit), v=$(0), src=$(src[i]), xf=$("$requeststr.$startid.xml")))
			Stemp = check_and_get_data(ex, requeststr)
		end

        if Stemp.misc[1]["dlerror"] == 0 && InputDict["IsResponseRemove"]
            Remove_response_obspy.remove_response_obspy!(Stemp, "$requeststr.$startid.xml", pre_filt=pre_filt, zeropadlen = float(30*60), output="VEL")
            rm("$requeststr.$startid.xml")
        else
            rm("$requeststr.$startid.xml")
        end

        S[i] = Stemp[1]
    end

    return S
end

"""
    seisdownload_EARTHQUAKE(startid, InputDict::Dict)

Download seismic data, removing instrumental response and saving into JLD2 file.

# Arguments
- `startid`         : start time id in starttimelist
- `InputDict::Dict` : dictionary which contains request information
"""
function seisdownload_EARTHQUAKE(startid, InputDict::Dict)

	method		    = InputDict["method"]
	event		    = InputDict["event"]
	reg			    = InputDict["reg"]
    pre_filt        = InputDict["pre_filt"]

    #show progress
    if mod(startid, round(0.1*length(event))+1) == 0
        println("start downloading event number: $startid")
    end
    S = SeisData()

    #---download data---#
	for j = 1:length(event[startid]["pickphase"])
		net = event[startid]["pickphase"][j]["net"]
		sta = event[startid]["pickphase"][j]["sta"]
		loc = event[startid]["pickphase"][j]["loc"]
		cha = event[startid]["pickphase"][j]["cha"]
		src = event[startid]["pickphase"][j]["src"]

		starttime = string(event[startid]["pickphase"][j]["starttime"])
		endtime = string(event[startid]["pickphase"][j]["endtime"])

		# make multiple request str

    	requeststr = join([net,sta,loc,cha], ".")

		if InputDict["IsLocationBox"]
			# request with lat-lon box
    		#argv = [method, requeststr, starttime, endtime, InputDict["reg"], 0, src, false, "$requeststr.$startid.xml"]
		    ex = :(get_data($(method), $(requeststr), s=$(starttime), t=$(endtime), reg=$(InputDict["reg"]), v=$(0), src=$(src), xf=$("$requeststr.$startid.xml")))
		    Stemp = check_and_get_data(ex, requeststr)
		else
			# request with lat-lon box
    		#argv = [method, requeststr, starttime, endtime, 0, src, false, "$requeststr.$startid.xml"]
			ex = :(get_data($(method), $(requeststr), s=$(starttime), t=$(endtime), v=$(0), src=$(src), xf=$("$requeststr.$startid.xml")))
		    Stemp = check_and_get_data(ex, requeststr)
		end

	    if Stemp.misc[1]["dlerror"] == 0 && InputDict["IsResponseRemove"]
	        Remove_response_obspy.remove_response_obspy!(Stemp, "$requeststr.$startid.xml", pre_filt=pre_filt, zeropadlen = float(30*60), output="VEL")
			if InputDict["IsXMLfileRemoved"]
				rm("$requeststr.$startid.xml")
			else
				mkpath("./stationxml")
				mv("$requeststr.$startid.xml", "./stationxml/$requeststr.$startid.xml", force=true)
			end
		else
			if InputDict["IsXMLfileRemoved"]
				rm("$requeststr.$startid.xml")
			else
				mkpath("./stationxml")
				mv("$requeststr.$startid.xml", "./stationxml/$requeststr.$startid.xml", force=true)
			end
	    end

		append!(S, Stemp)
	end

    return S
end

"""
    check_and_get_data(ex::Expr, requeststr::String)

Download seismic data, removing instrumental response and saving into JLD2 file.

# Arguments
- `ex::Expr`        : expression of get data includin all request information

# Output
- `S::SeisData`     : downloaded SeisData
- `requeststr::String`     : request channel (e.g. "BP.LCCB..BP1")
"""
function check_and_get_data(ex::Expr, requeststr::String)
   try
	   #comment out below if you want to print contents of get_data()
	   #println(ex)
       S = eval(ex);
       S.misc[1]["dlerror"] = 0
       return S

   catch y
	   #println(y)
       S = SeisData(1)
       S.misc[1]["dlerror"] = 1
       S.id[1] = requeststr
       note!(S, 1, "station is not available for this request.")
       return S
   end
end

end


text:
# Get the latest commit as a string due to Ismael Venegas Castelló (@Ismael-VC)
"""
Returns the shortened SHA of the latest commit of a package.
"""
function latest_commit(pkg::String)::String
    original_directory = pwd()
    cd(Pkg.dir(pkg))
    commit = readstring(`git rev-parse --short HEAD`) |> chomp
    cd(original_directory)
    return commit
end

"""
Returns, for each solver, a vector of counts of the number of problems in the DataFrame
that is below the threshold values given.
"""
function profiles(names, df, tau, measure)
    profiles_out = []
    for name in names
        profile = zeros(tau)
        rows =  df[:Optimizer].==name
        for i = 1:length(tau)
            profile[i] = sum(df[rows, measure].<=tau[i])/length(unique(df[:Problem]))
        end
        push!(profiles_out, profile)
    end
    profiles_out
end

"""
Saves two figures showing performance profiles for the objective error and the
error in the minimizer across problems and solvers.
"""
function save_plots(version_dir, testset; tau = 10.0.^(-16:10), legendpos = :bottomright)

    if testset == :cutest
        teststr = "cutest"
    elseif testset == :optim
        teststr = "optim"
    elseif testset == :optim_linesearch
        teststr = "optim_linesearch"
    elseif testset == :cutest_linesearch
        teststr = "cutest_linesearch"
    else
        err("Symbol not supported")
    end

    str = version_dir*"/"*teststr*"_benchmark.csv"
    df = readtable(str)
    names = unique(df[:Optimizer])
    f_profiles = profiles(names, df, tau, :f_error)
    f_err = plot(tau, hcat(f_profiles...),
            label = hcat(names...),
            lc=[:black :red :green :black :red :green :black :red :green :black :red :green],
            ls=[:solid  :solid :solid :dash :dash :dash :dashdot :dashdot :dashdot :dot :dot :dot],
            size =(800,400),
            ylims = (0,1),
            line = :steppre, xscale=:log10, xlabel = "Error level", ylabel = "Proportion of problems",
            title = "Measure: f-f*",
            legend=legendpos)
    savefig("f_err_"*teststr)

    x_profiles = profiles(names, df, tau, :x_error)
    x_err = plot(tau, hcat(x_profiles...),
            label = hcat(names...),
            lc=[:black :red :green :black :red :green :black :red :green :black :red :green],
            ls=[:solid  :solid :solid :dash :dash :dash :dashdot :dashdot :dashdot :dot :dot :dot],
            size =(800,400),
            ylims = (0,1),
            line = :steppre, xscale=:log10, xlabel = "Error level", ylabel = "Proportion of problems",
            title = "Measure: sup-norm of x-x*.",
            legend=legendpos)
    savefig("x_err_"*teststr)
end


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

mutable struct IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion <: SwaggerModel
    additionalPrinterColumns::Any # spec type: Union{ Nothing, Vector{IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceColumnDefinition} } # spec name: additionalPrinterColumns
    name::Any # spec type: Union{ Nothing, String } # spec name: name
    schema::Any # spec type: Union{ Nothing, IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceValidation } # spec name: schema
    served::Any # spec type: Union{ Nothing, Bool } # spec name: served
    storage::Any # spec type: Union{ Nothing, Bool } # spec name: storage
    subresources::Any # spec type: Union{ Nothing, IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceSubresources } # spec name: subresources

    function IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion(;additionalPrinterColumns=nothing, name=nothing, schema=nothing, served=nothing, storage=nothing, subresources=nothing)
        o = new()
        validate_property(IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion, Symbol("additionalPrinterColumns"), additionalPrinterColumns)
        setfield!(o, Symbol("additionalPrinterColumns"), additionalPrinterColumns)
        validate_property(IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion, Symbol("name"), name)
        setfield!(o, Symbol("name"), name)
        validate_property(IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion, Symbol("schema"), schema)
        setfield!(o, Symbol("schema"), schema)
        validate_property(IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion, Symbol("served"), served)
        setfield!(o, Symbol("served"), served)
        validate_property(IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion, Symbol("storage"), storage)
        setfield!(o, Symbol("storage"), storage)
        validate_property(IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion, Symbol("subresources"), subresources)
        setfield!(o, Symbol("subresources"), subresources)
        o
    end
end # type IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion

const _property_map_IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion = Dict{Symbol,Symbol}(Symbol("additionalPrinterColumns")=>Symbol("additionalPrinterColumns"), Symbol("name")=>Symbol("name"), Symbol("schema")=>Symbol("schema"), Symbol("served")=>Symbol("served"), Symbol("storage")=>Symbol("storage"), Symbol("subresources")=>Symbol("subresources"))
const _property_types_IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion = Dict{Symbol,String}(Symbol("additionalPrinterColumns")=>"Vector{IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceColumnDefinition}", Symbol("name")=>"String", Symbol("schema")=>"IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceValidation", Symbol("served")=>"Bool", Symbol("storage")=>"Bool", Symbol("subresources")=>"IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceSubresources")
Base.propertynames(::Type{ IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion }) = collect(keys(_property_map_IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion))
Swagger.property_type(::Type{ IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion[name]))}
Swagger.field_name(::Type{ IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion }, property_name::Symbol) =  _property_map_IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion[property_name]

function check_required(o::IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion)
    (getproperty(o, Symbol("name")) === nothing) && (return false)
    (getproperty(o, Symbol("served")) === nothing) && (return false)
    (getproperty(o, Symbol("storage")) === nothing) && (return false)
    true
end

function validate_property(::Type{ IoK8sApiextensionsApiserverPkgApisApiextensionsV1beta1CustomResourceDefinitionVersion }, name::Symbol, val)
end


text:
# for plotif. This identifies a vector of colors
function identify_colors(g, xs, colors=(:red, :blue, :black))
    F = (a,b) -> begin
        ga,gb=g(a),g(b)
        ga * gb < 0 && return nothing
        ga >= 0 && return true
        return false
    end
    find_colors(F, xs, colors)
end

# F(a,b) returns true, false, or nothing
function find_colors(F, xs, colors=(:red, :blue, :black))
    n = length(xs)
    cols = repeat([colors[1]], n)
    for i in 1:n-1
        a,b = xs[i], xs[i+1]
        val = F(a,b)
        if val == nothing
            cols[i] = colors[3]
        elseif val
            cols[i] = colors[1]
        else
            cols[i] = colors[2]
        end
    end
    cols[end] = cols[end-1]
    cols
end

# some plotting utilities
"""
    rangeclamp(f, hi=20, lo=-hi; replacement=NaN)

Modify `f` so that values of `f(x)` outside of `[lo,hi]` are replaced by `replacement`.

Examples
```
f(x) = 1/x
plot(rangeclamp(f), -1, 1)
plot(rangeclamp(f, 10), -1, 1) # no `abs(y)` values exceeding 10
```
"""
rangeclamp(f, hi=20, lo=-hi; replacement=NaN) = x -> lo < f(x) < hi ? f(x) : replacement


text:
using NeXLMatrixCorrection
using CategoricalArrays
using NeXLSpectrum

function askratios(ffr::FitResult)::Vector{KRatio}
    res = KRatio[]
    for lbl in keys(ffr.kratios)
        if lbl isa NeXLSpectrum.CharXRayLabel
            push!(
                res,
                KRatio( #
                    lbl.xrays,
                    ffr.label.spectrum.properties,
                    lbl.spectrum.properties,
                    lbl.spectrum[:Composition],
                    ffr.kratios[lbl],
                ),
            )
        end
    end
    return res
end

"""
    Signature

Signature helps to define how the signature calculation is performed and to cache intermediate calculations for
speed.
"""
struct Signature
    kpure::Dict{CharXRay,<:AbstractFloat}
    matrix::Type{<:MatrixCorrection}
    fluorescence::Type{<:FluorescenceCorrection}
    kratioopt::KRatioOptimizer

    Signature(mc = XPP, fc = ReedFluorescence, kro = SimpleKRatioOptimizer(1.3)) =
        new(Dict{CharXRay,AbstractFloat}(), mc, fc, kro)
end

function kpure(sig::Signature, std::Material, elm::Element, xrays::Vector{CharXRay}, stdE0, unkE0, stdθ, unkθ)::AbstractFloat
    res = get(sig.kpure, brightest(xrays), -1.0)
    if res == -1.0
        flines = filter(cxr->energy(inner(cxr))<min(stdE0, unkE0), xrays)
        unkzaf = zafcorrection(sig.matrix, sig.fluorescence, Coating, std, flines, stdE0)
        stdzaf = zafcorrection(sig.matrix, sig.fluorescence, Coating, pure(elm), flines, unkE0)
        res = k(unkzaf, stdzaf, stdθ, unkθ)
        sig.kpure[brightest(xrays)] = res
    end
    return res
end

"""
    signature( #
        sig::Signature,
        krs::Vector{KRatio},
        special::Set{Element},
        drop::Set{Element}
    )::Dict{Element,Float64}

Computes a "particle signature" from the specified set of k-ratios.  A particle signature
is similar to normalized k-ratios relative to pure elements except 1) certain elements
can be removed from the normalization; and 2) these elements can be include in the result as
un-normalized.
"""
function signature( #
    sig::Signature,
    krs::Vector{KRatio},
    special::Set{Element},
    drop::Set{Element}
)::Dict{Element,<:AbstractFloat}
    kzs = Dict{Element,AbstractFloat}()
    #Scale the k-ratios relative to a pure element...
    for kr in filter(k->!(k.element in drop), optimizeks(sig.kratioopt, krs))
        # k_{unk,pure} = (I_{unk}/I_{std})*(I_{std}/I_{pure}) = kr.kratio*k(std,pure)
        kstdpure = isapprox(kr.standard[kr.element], 1.0, atol = 0.001) ? 1.0 : #
               kpure(
            sig,
            kr.standard,
            kr.element,
            kr.xrays,
            kr.stdProps[:BeamEnergy],
            kr.unkProps[:BeamEnergy],
            kr.stdProps[:TakeOffAngle],
            kr.unkProps[:TakeOffAngle],
        )
        kzs[kr.element] = kr.kratio * kstdpure
    end
    onorm = sum(NeXLUncertainties.value(kzs[elm]) for elm in keys(kzs))
    res = Dict(elm => kzs[elm] / onorm for elm in special)
    notspecial = filter(elm -> !(elm in special) , keys(kzs))
    norm = sum(NeXLUncertainties.value(kzs[elm]) for elm in notspecial)
    norm = norm > 0.0 ? norm : 1.0 # Rarely all elements are zero...
    merge!(res, Dict(elm => kzs[elm] / norm for elm in notspecial))
    return res
end

abstract type CullingRule end

struct NSigmaCulling <: CullingRule
    nsigma::Float64
end

function cull(cr::NSigmaCulling, kr::KRatio)
    k, s = NeXLUncertainties.value(kr.kratio), σ(kr.kratio)
    return k > cr.nsigma * s ? kr : KRatio(kr.xrays, kr.unkProps, kr.stdProps, kr.standard, UncertainValue(0.0, s))
end

struct NoCulling <: CullingRule end

function cull(cr::NoCulling, kr::KRatio)
    k = NeXLUncertainties.value(kr.kratio)
    return k > 0 ? kr : KRatio(kr.xrays, kr.unkProps, kr.stdProps, kr.standard, UncertainValue(0.0, σ(kr.kratio)))
end

function _quant(
    zep::Zeppelin,
    ffp::FilterFitPacket,
    rows::Union{AbstractVector{Int},UnitRange{Int}},
    strip::Set{Element}, # Elements to fit but not include in the result table.
    special::Set{Element}, # Element for special treatment in signature
    cullRule::CullingRule,
    writeResidual::Bool,
    withUncertainty::Bool,
    relocated::Bool
)
    sortedbysig(newcols, row) =
            sort(collect(zip(newcols, row)),lt=(i1,i2)->!isless(i1[1]==n"O" ? 0.0 : i1[2],i2[1]==n"O" ? 0.0 : i2[2]))
    # Create filtered references.  Not worth threading.
    filtrefs = ffp.references
    e0, toa = beamenergy(zep), sameproperty([ ref.label.spectrum for ref in filtrefs], :TakeOffAngle)
    # Create a list of columns (by element)
    newcols = sort(collect(filter(elm -> !(elm in strip), unique(collect( element(ref.label) for ref in filtrefs)))))
    quant = Array{Union{UncertainValue,Missing}}(missing, length(rows), length(newcols))
    # quantify and tabulate each particle
    if writeResidual
        newdir = joinpath(dirname(zep.headerfile), "Residual")
        if !isdir(newdir)
            mkdir(newdir)
        end
    end
    ignore = union(strip,special)
    counts = Array{Union{Missing,Float64}}(missing, length(rows))
    krvs = ThreadsX.map(enumerate(rows)) do (ir, row)
        unk = spectrum(zep, row, false, relocated)
        if !ismissing(unk)
            # Particle spectra are often missing critical data items...
            unk[:ProbeCurrent], unk[:LiveTime] = get(unk, :ProbeCurrent, 1.0), get(unk, :LiveTime, 1.0)
            unk[:BeamEnergy], unk[:TakeOffAngle] = get(unk, :BeamEnergy, e0), get(unk, :TakeOffAngle, toa)
            # Fit, cull and then compute the particle signature...
            res = fit_spectrum(unk, ffp)
            counts[ir] = NeXLSpectrum.characteristiccounts(res, ignore)
            if writeResidual
                filename = joinpath(dirname(zep.headerfile), "Residual", filenumber(zep, row)*".msa")
                savespectrum(ISOEMSA,filename, NeXLSpectrum.residual(res))
            end
            filter(kr -> kr.element in newcols, map(kr -> cull(cullRule, kr), askratios(res)))
        else
            KRatio[]
        end
    end
    nfirst=min(4, length(newcols)-length(special))
    felm = Array{Union{Element,Missing}}(missing, length(rows), 4)
    fsig = Array{Union{UncertainValue,Missing}}(missing, length(rows), 4)
    sigx = Signature(XPP, ReedFluorescence, SimpleKRatioOptimizer(1.3))
    for ir in filter(ir->!ismissing(krvs[ir]), eachindex(rows))
        sig = signature(sigx, krvs[ir], special, strip) # Not thread safe...
        quant[ir, :] = map(elm -> 100.0 * sig[elm], newcols)
        s2 = copy(sig)
        foreach(j->delete!(s2,j), special) # Special can't be a FIRSTELM, ...
        firstElms = topN(s2, nfirst)
        felm[ir, :] = map(j->j<=length(firstElms) && (NeXLUncertainties.value(firstElms[j].second) > 0.01) ? firstElms[j].first : missing, 1:4)
        fsig[ir, :] = map(j->j<=length(firstElms) && (NeXLUncertainties.value(firstElms[j].second) > 0.01) ? 100.0 * NeXLUncertainties.value(firstElms[j].second) : missing, 1:4)
    end
    fourelms = DataFrame( #
        FIRSTELM = felm[:,1], FIRSTPCT = NeXLUncertainties.value.(fsig[:,1]),
        SECONDELM = felm[:,2], SECONDPCT = NeXLUncertainties.value.(fsig[:,2]), #
        THIRDELM = felm[:,3],  THIRDPCT = NeXLUncertainties.value.(fsig[:,3]), #
        FOURTHELM = felm[:,4], FOURTHPCT = NeXLUncertainties.value.(fsig[:,4]), #
        COUNTS = counts) #
        # Return the uncertainties or not...
    cols = Pair{Symbol,AbstractVector{Union{Float64,Missing}}}[ ]
    # Handle missings...
    asval(v) = ismissing(v) ? missing : NeXLUncertainties.value(v)
    asσ(v) = ismissing(v) ? missing : σ(v)
    for (col, elm) in enumerate(newcols)
        push!(cols, convert(Symbol,elm) => asval.(quant[:,col]))
        if withUncertainty
            push!(cols, Symbol("U_$(uppercase(elm.symbol))_")=>asσ.(quant[:,col]))
        end
    end
    quantRes = DataFrame(cols...)
    return hcat(fourelms, quantRes)
end

function NeXLMatrixCorrection.quantify(
    zep::Zeppelin,
    ffp::FilterFitPacket,
    rows::Union{AbstractVector{Int},UnitRange{Int}}=eachparticle(zep);
    strip = Set{Element}( (n"C", ) ), # Elements to fit but not include in the result table.
    special = Set{Element}( ( n"O", ) ), # Element for special treatment in signature
    cullRule::CullingRule = NSigmaCulling(3.0),
    writeResidual::Bool = true,
    withUncertainty::Bool = true,
    relocated::Bool = true)
    qr = _quant(zep,ffp,rows,strip,special,cullRule,writeResidual,withUncertainty,relocated)
    # Remove old items...
    els = collect(elms(zep))
    removecols = map(elm -> uppercase(elm.symbol), els)
    append!(removecols, map(elm -> "U_$(uppercase(elm.symbol))_", els))
    append!(removecols, ALL_COMPOSITIONAL_COLUMNS)
    remaining = copy(zep.data[rows, filter(f -> !(f in removecols), names(zep.data))])
    data = hcat(remaining, qr)
    # Replace outdated header items
    header = copy(zep.header)
    headerfile =  joinpath(dirname(zep.headerfile), "npQuant.hdz") # So spectra and other path related items continue to work
    delete!(header, "MAX_PARTICLE")
    delete!(header, "MAX_RESIDUAL")
    # Add/replace header items
    i = findfirst(i->!haskey(header, "ANCESTOR[$i]"), 1:1000)
    header["ANCESTOR[$i]"]=zep.headerfile
    header["DATAFILES"] = replace(headerfile,r".[h|H][d|D][z|Z]$"=>".*")
    header["VEC_FILE"] = "NeXLParticle"
    return Zeppelin(headerfile, header, data, zep.classnames)
end

function NeXLMatrixCorrection.quantify(
    zep::Zeppelin,
    det::Detector,
    refs::Dict{Element,Spectrum},
    rows::Union{AbstractVector{Int},UnitRange{Int}}=eachparticle(zep);
    strip = Set{Element}( (n"C", ) ), # Elements to fit but not include in the result table.
    special = Set{Element}( ( n"O", ) ), # Element for special treatment in signature
    cullRule::CullingRule = NSigmaCulling(3.0),
    writeResidual::Bool = true,
    withUncertainty::Bool = true,
    relocated::Bool = true)
    # Create filtered references.  Not worth threading.
    ffp = references(
        [ reference(elm, spec, spec[:Composition]) for (elm, spec) in refs ],
        det
    )
    quantify(zep, ffp, rows, strip=strip, special=special, cullRule=cullRule, writeResidual=writeResidual, withUncertainty=withUncertainty, relocated=relocated)
end

topN(items::Dict, n::Int) =
    sort( [ items... ],lt=(a1,a2)->isless(a2[2],a1[2]))[1:min(n,length(items))]

max_stars_repo_name: NicholasWMRitchie/NeXLParticle

max_issues_repo_name: NicholasWMRitchie/NeXLParticle

max_forks_repo_name: NicholasWMRitchie/NeXLParticle