RvSpectML/EchelleInstruments.jl

add documentation for function

hematthi opened this issue · 0 comments

function find_ranges_with_tellurics_in_order(spectrum::ST, order::Integer; telluric_threshold::Real = 1, min_Δv_clean::Real = default_min_Δv_clean, verbose::Bool = false) where { T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3}, IT<:EXPRES.AnyEXPRES, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT} }
@assert haskey(spectrum.metadata,:tellurics)
telluric_ranges = Vector{Tuple{Int64,Int64}}(undef,0)
c = RvSpectMLBase.speed_of_light_mps
all_wavelengths = range(first(spectrum.λ[:,order]),stop=last(spectrum.λ[:,order]),length=size(spectrum.λ,1) )
tellurics = spectrum.metadata[:tellurics]
start = findfirst(x->!isnan(x),view(tellurics,:,order) )
stop = findlast(x->!isnan(x),view(tellurics,:,order) )
if isnothing(start) || isnothing(stop)
if verbose println("# The entire order ", order, " was NaNs!?!") end
return Vector{Tuple{eltype(spectrum.λ), eltype(spectrum.λ)} }(undef,0)
end
@assert stop >= start +1
in_telluric = tellurics[start,order] < telluric_threshold
idx_start_this_telluric = in_telluric ? start : 0
idx_stop_this_telluric = 0
for i in start+1:stop
if in_telluric
if !(tellurics[i,order] < telluric_threshold) # exitted telluric
idx_stop_this_telluric = i-1
#println("# Order = " , order, " Adding pixels = ", idx_start_this_telluric, " - ", idx_stop_this_telluric, " λ = ", spectrum.λ[idx_start_this_telluric,order], " - ", spectrum.λ[idx_stop_this_telluric,order] )
push!(telluric_ranges, (idx_start_this_telluric,idx_stop_this_telluric) )
#push!(telluric_ranges,(spectrum.λ[idx_start_this_telluric,order], spectrum.λ[idx_stop_this_telluric,order]) )
in_telluric = false
end
else
if (tellurics[i,order] < telluric_threshold) # entered telluric
idx_start_this_telluric = i
if length(telluric_ranges) >= 1
idx_last_start = last(telluric_ranges)[1]
idx_last_stop = last(telluric_ranges)[2]
if spectrum.λ[idx_start_this_telluric,order] - spectrum.λ[idx_last_stop,order] <= min_Δv_clean/c * spectrum.λ[idx_start_this_telluric,order]
idx_start_this_telluric = first(last(telluric_ranges))
pop!(telluric_ranges)
end
end
in_telluric = true
end
end
end
if in_telluric
idx_stop_this_telluric = stop
#println("# Order = " , order, " Adding pixels = ", idx_start_this_telluric, " - ", idx_stop_this_telluric, " λ = ", spectrum.λ[idx_start_this_telluric,order], " - ", spectrum.λ[idx_stop_this_telluric,order] )
push!(telluric_ranges,(idx_start_this_telluric,idx_stop_this_telluric) )
end
#lambda_range = map(r->(spectrum.λ[first(r),order], spectrum.λ[last(r),order] ), telluric_ranges)
DataFrame(:lambda_lo=>map(r->spectrum.λ[first(r),order], telluric_ranges), :lambda_hi=>map(r->spectrum.λ[last(r),order], telluric_ranges) )
end