Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more documentation for functions and clean out old/commented code #44

Open
hematthi opened this issue May 20, 2022 · 1 comment
Open
Labels
documentation Improvements or additions to documentation

Comments

@hematthi
Copy link
Collaborator

function filter_line_list(df::DataFrame, inst::IT ; λmin::Real = default_λmin, λmax::Real = default_λmax ) where { IT<:HARPSN.AnyHARPSN }
df |> @filter(λmin <= _.lambda <= λmax) |>
# @filter( _.lambda < 6000.0 ) |> # Avoid tellurics at redder wavelengths
# @filter( _.lambda >6157 || _.lambda < 6155 ) |> # Avoid "line" w/ large variability
DataFrame
end
""" read_telluric_ranges( filename )
Return DataFrame (keys lambda_lo and lambda_hi) with wavelength ranges to be avoided as containing tellurics
based on provided CSV file. Assumes path is included in filename.
"""
function read_telluric_ranges(fn::String)
@assert occursin(r"\.csv$",fn)
#println("Trying to read from >",fn,"<.")
@assert isfile(fn)
df = CSV.read(fn, DataFrame)
@assert size(df,1) >= 1
@assert hasproperty(df,:lambda_lo)
@assert hasproperty(df,:lambda_hi)
return df
end
function make_λ_list_for_bad_columns(line_list::DataFrame, harpsn_data::DT ) where {
T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3},
IT<:HARPSN.AnyHARPSN, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT}, DT<:AbstractArray{ST,1} }
order_list = 1:size(first(harpsn_data).λ,2)
df_bad_col_λs = DataFrame(:order=>Int[], :lambda_lo=>Float64[], :lambda_hi=>Float64[])
for order in order_list
λ_extrema = NaNMath.extrema(first(harpsn_data).λ[:,order])
if isnan(first(λ_extrema)) || isnan(last(λ_extrema)) continue end
for bcr in bad_col_ranges(HARPSN2D(), order)
pixlo = first(bcr)
pixhi = pixlo + 1
if pixhi > size(first(harpsn_data).λ,1)
pixhi -= 1
pixlo -= 1
end
doppler_factors = map(obsid-> haskey(harpsn_data[obsid].metadata,:doppler_factor) ? harpsn_data[obsid].metadata[:doppler_factor] : 1 , 1:length(harpsn_data))
Δλ_pixel = (first(harpsn_data).λ[pixhi,order] - first(harpsn_data).λ[pixlo,order]) * doppler_factors[1]
(λ_lo, λ_hi) = mapreduce(obsid->extrema(harpsn_data[obsid].λ[bcr,order]) .* doppler_factors[obsid],
(a,b) -> (min(a[1],b[1]), max(a[2],b[2])), 1:length(harpsn_data) )
λ_lo -= Δλ_pixel/2
λ_hi += Δλ_pixel/2
push!(df_bad_col_λs, Dict(:order=>order, :lambda_lo=>λ_lo, :lambda_hi=>λ_hi) )
end
end
return df_bad_col_λs
end
#=
#old neid function
function make_good_orders_pixels_df(neid_data::DT ; orders::A4 = orders_to_use_default(first(neid_data).inst) ) where {
T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3},
IT<:NEID.AnyNEID, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT}, DT<:AbstractArray{ST,1},
T4 <: Integer, A4<:AbstractArray{T4} }
@warn "Deprecated use make_good_orders_pixels_df(inst) instead."
df = DataFrame(order=Int[],pixels=UnitRange[])
for ord in orders
pixel_array = EchelleInstruments.calc_complement_index_ranges(get_pixel_range(NEID2D(),ord),EchelleInstruments.NEID.bad_col_ranges(NEID2D(),ord))
order_array = repeat([ord],length(pixel_array))
df_tmp = DataFrame(:order=>order_array,:pixels=>pixel_array)
append!(df,df_tmp)
end
return df
end
function make_good_orders_pixels_df(inst::IT ; orders::A4 = orders_to_use_default(inst) ) where {
IT<:NEID.AnyNEID, T4 <: Integer, A4<:AbstractArray{T4} }
df = DataFrame(order=Int[],pixels=UnitRange[])
for ord in orders
pixel_array = EchelleInstruments.calc_complement_index_ranges(get_pixel_range(NEID2D(),ord),EchelleInstruments.NEID.bad_col_ranges(NEID2D(),ord))
order_array = repeat([ord],length(pixel_array))
df_tmp = DataFrame(:order=>order_array,:pixels=>pixel_array)
append!(df,df_tmp)
end
return df
end
=#
#=
function make_clean_line_list_from_bad_columns(line_list::DataFrame, neid_data::DT ;
Δv_to_avoid_tellurics::Real = 0.0, v_center_to_avoid_tellurics::Real = 0.0
) where { T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3}, IT<:NEID.AnyNEID, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT}, DT<:AbstractArray{ST,1} }
if size(df_bad_col_λs,1) < 1 return line_list end
line_list_to_search_for_tellurics = copy(line_list)
line_list_to_search_for_tellurics.lambda_lo = line_list_to_search_for_tellurics.lambda./calc_doppler_factor(Δv_to_avoid_tellurics).*calc_doppler_factor(v_center_to_avoid_tellurics)
line_list_to_search_for_tellurics.lambda_hi = line_list_to_search_for_tellurics.lambda.*calc_doppler_factor(Δv_to_avoid_tellurics).*calc_doppler_factor(v_center_to_avoid_tellurics)
line_list_no_tellurics_df = line_list_to_search_for_tellurics |> # @filter(λmin <= _.lambda <= λmax) |>
@filter( !RvSpectMLBase.is_in_wavelength_range_list(_.lambda, order=_.order, list=df_bad_col_λs ) ) |>
DataFrame
end
=#
#old neid functions

@hematthi
Copy link
Collaborator Author

Same for harps:

function filter_line_list(df::DataFrame, inst::IT ; λmin::Real = default_λmin, λmax::Real = default_λmax ) where { IT<:HARPS.AnyHARPS }
df |> @filter(λmin <= _.lambda <= λmax) |>
# @filter( _.lambda < 6000.0 ) |> # Avoid tellurics at redder wavelengths
# @filter( _.lambda >6157 || _.lambda < 6155 ) |> # Avoid "line" w/ large variability
DataFrame
end
""" read_telluric_ranges( filename )
Return DataFrame (keys lambda_lo and lambda_hi) with wavelength ranges to be avoided as containing tellurics
based on provided CSV file. Assumes path is included in filename.
"""
function read_telluric_ranges(fn::String)
@assert occursin(r"\.csv$",fn)
#println("Trying to read from >",fn,"<.")
@assert isfile(fn)
df = CSV.read(fn, DataFrame)
@assert size(df,1) >= 1
@assert hasproperty(df,:lambda_lo)
@assert hasproperty(df,:lambda_hi)
return df
end
function make_λ_list_for_bad_columns(line_list::DataFrame, harps_data::DT ) where {
T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3},
IT<:HARPS.AnyHARPS, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT}, DT<:AbstractArray{ST,1} }
order_list = 1:size(first(harps_data).λ,2)
df_bad_col_λs = DataFrame(:order=>Int[], :lambda_lo=>Float64[], :lambda_hi=>Float64[])
for order in order_list
λ_extrema = NaNMath.extrema(first(harps_data).λ[:,order])
if isnan(first(λ_extrema)) || isnan(last(λ_extrema)) continue end
for bcr in bad_col_ranges(HARPS2D(), order)
pixlo = first(bcr)
pixhi = pixlo + 1
if pixhi > size(first(harps_data).λ,1)
pixhi -= 1
pixlo -= 1
end
doppler_factors = map(obsid-> haskey(harps_data[obsid].metadata,:doppler_factor) ? harps_data[obsid].metadata[:doppler_factor] : 1 , 1:length(harps_data))
Δλ_pixel = (first(harps_data).λ[pixhi,order] - first(harps_data).λ[pixlo,order]) * doppler_factors[1]
(λ_lo, λ_hi) = mapreduce(obsid->extrema(harps_data[obsid].λ[bcr,order]) .* doppler_factors[obsid],
(a,b) -> (min(a[1],b[1]), max(a[2],b[2])), 1:length(harps_data) )
λ_lo -= Δλ_pixel/2
λ_hi += Δλ_pixel/2
push!(df_bad_col_λs, Dict(:order=>order, :lambda_lo=>λ_lo, :lambda_hi=>λ_hi) )
end
end
return df_bad_col_λs
end
#=
#old neid functions
function make_good_orders_pixels_df(neid_data::DT ; orders::A4 = orders_to_use_default(first(neid_data).inst) ) where {
T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3},
IT<:NEID.AnyNEID, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT}, DT<:AbstractArray{ST,1},
T4 <: Integer, A4<:AbstractArray{T4} }
@warn "Deprecated use make_good_orders_pixels_df(inst) instead."
df = DataFrame(order=Int[],pixels=UnitRange[])
for ord in orders
pixel_array = EchelleInstruments.calc_complement_index_ranges(get_pixel_range(NEID2D(),ord),EchelleInstruments.NEID.bad_col_ranges(NEID2D(),ord))
order_array = repeat([ord],length(pixel_array))
df_tmp = DataFrame(:order=>order_array,:pixels=>pixel_array)
append!(df,df_tmp)
end
return df
end
function make_good_orders_pixels_df(inst::IT ; orders::A4 = orders_to_use_default(inst) ) where {
IT<:NEID.AnyNEID, T4 <: Integer, A4<:AbstractArray{T4} }
df = DataFrame(order=Int[],pixels=UnitRange[])
for ord in orders
pixel_array = EchelleInstruments.calc_complement_index_ranges(get_pixel_range(NEID2D(),ord),EchelleInstruments.NEID.bad_col_ranges(NEID2D(),ord))
order_array = repeat([ord],length(pixel_array))
df_tmp = DataFrame(:order=>order_array,:pixels=>pixel_array)
append!(df,df_tmp)
end
return df
end
=#
#=
function make_clean_line_list_from_bad_columns(line_list::DataFrame, neid_data::DT ;
Δv_to_avoid_tellurics::Real = 0.0, v_center_to_avoid_tellurics::Real = 0.0
) where { T1<:Real, A1<:AbstractArray{T1}, T2<:Real, A2<:AbstractArray{T2}, T3<:Real, A3<:AbstractArray{T3}, IT<:NEID.AnyNEID, ST<:Spectra2DBasic{T1,T2,T3,A1,A2,A3,IT}, DT<:AbstractArray{ST,1} }
if size(df_bad_col_λs,1) < 1 return line_list end
line_list_to_search_for_tellurics = copy(line_list)
line_list_to_search_for_tellurics.lambda_lo = line_list_to_search_for_tellurics.lambda./calc_doppler_factor(Δv_to_avoid_tellurics).*calc_doppler_factor(v_center_to_avoid_tellurics)
line_list_to_search_for_tellurics.lambda_hi = line_list_to_search_for_tellurics.lambda.*calc_doppler_factor(Δv_to_avoid_tellurics).*calc_doppler_factor(v_center_to_avoid_tellurics)
line_list_no_tellurics_df = line_list_to_search_for_tellurics |> # @filter(λmin <= _.lambda <= λmax) |>
@filter( !RvSpectMLBase.is_in_wavelength_range_list(_.lambda, order=_.order, list=df_bad_col_λs ) ) |>
DataFrame
end
=#
#old neid functions

@hematthi hematthi added the documentation Improvements or additions to documentation label May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant