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

repetitive large blocks of code can be made into separate function #76

Open
hematthi opened this issue Jun 1, 2022 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@hematthi
Copy link
Collaborator

hematthi commented Jun 1, 2022

The following large blocks of code are identical and can be made into a separate function to reduce repetitive code:

for chid in 1:num_chunks(clt)
order = clt[1].order[chid]
if typeof(plan.line_list) <: BasicLineList2D
start_order_idx = searchsortedfirst(plan.line_list.order,order)
stop_order_idx = searchsortedlast(view(plan.line_list.order,start_order_idx:num_lines),order) + start_order_idx-1
else
start_order_idx = 1
stop_order_idx = num_lines
end
if (1<=start_order_idx<=stop_order_idx<=num_lines)
# find the maximum lower wavelength for the chunk, and the minumum upper wavelength, over all observations
λmin = maximum(map(obsid->first(clt.chunk_list[obsid].data[chid].λ), 1:length(clt) ))
λmax = minimum(map(obsid->last( clt.chunk_list[obsid].data[chid].λ), 1:length(clt) ))
# extend λmin/λmax by the velocity range over which we don't want the mask to change
λmin = λmin / (calc_doppler_factor(plan.v_center)*calc_doppler_factor(-plan.v_range_no_mask_change))
λmax = λmax / (calc_doppler_factor(plan.v_center)*calc_doppler_factor(plan.v_range_no_mask_change))
# extend λmin/λmax by the mask width
upper_edge_of_mask_for_line_at_λmin = λ_max(plan.mask_shape,λmin)
lower_edge_of_mask_for_line_at_λmax = λ_min(plan.mask_shape,λmax)
# find the first and last mask entries to use in each chunk
start_line_idx = searchsortedfirst(view(plan.line_list.λ,start_order_idx:stop_order_idx),upper_edge_of_mask_for_line_at_λmin) + start_order_idx-1
#stop_line_idx = num_lines+1 - searchsortedfirst(view(plan.line_list.λ,num_lines:-1:1),lower_edge_of_mask_for_line_at_λmax,rev=true)
stop_line_idx = start_order_idx-1 + searchsortedlast(view(plan.line_list.λ,start_order_idx:stop_order_idx),lower_edge_of_mask_for_line_at_λmax)
if verbose
flush(stdout)
println("extrema(plan.line_list.λ) = ",extrema(plan.line_list.λ) )
if (1 <= start_line_idx <= length(plan.line_list.λ)) && (1 <= stop_line_idx <= length(plan.line_list.λ))
println("start_order_idx = ", start_order_idx, " stop_order_idx = ", stop_order_idx)
println("start_line_idx = ", start_line_idx, " λ= ", plan.line_list.λ[start_line_idx]) #, " order= ", plan.line_list.order[start_line_idx])
println("stop_line_idx = ", stop_line_idx, " λ= ", plan.line_list.λ[stop_line_idx]) #, " order= ", plan.line_list.order[stop_line_idx])
end
println("upper_edge_of_mask_for_line_at_λmin = ", upper_edge_of_mask_for_line_at_λmin, " Lower_edge_of_mask_for_line_at_λmax = ", lower_edge_of_mask_for_line_at_λmax)
end
if (1 <= start_line_idx <= length(plan.line_list.λ)) && (1 <= stop_line_idx <= length(plan.line_list.λ))
if verbose && typeof(plan.line_list) <: BasicLineList2D
println("start_line_idx = ", start_line_idx, " λ= ", plan.line_list.λ[start_line_idx], " order= ", plan.line_list.order[start_line_idx])
println("stop_line_idx = ", stop_line_idx, " λ= ", plan.line_list.λ[stop_line_idx], " order= ", plan.line_list.order[stop_line_idx])
println(" Using ", length(start_line_idx:stop_line_idx), " lines for chunk ", chid, " order ", order)
end
if ! issorted( view(plan.line_list.λ, start_line_idx:stop_line_idx ) )
println("# View is not sorted. Why?")
for i in start_line_idx:stop_line_idx
println("i= ", i, " λ= ", plan.line_list.λ[i], " order= ", plan.line_list.order[i])
end
end
@assert issorted( view(plan.line_list.λ, start_line_idx:stop_line_idx ) )
# line_list_for_chunk = BasicLineList2D(view(plan.line_list.λ,start_line_idx:stop_line_idx), view(plan.line_list.weight,start_line_idx:stop_line_idx), fill(order,length(start_line_idx:stop_line_idx)) )
line_list_for_chunk = BasicLineList(view(plan.line_list.λ,start_line_idx:stop_line_idx), view(plan.line_list.weight,start_line_idx:stop_line_idx) )
else # No lines in this chunk!
line_list_for_chunk = EmptyBasicLineList()
end
else
line_list_for_chunk = EmptyBasicLineList()
end
#create a plan for this chunk that only includes the mask entries we want for this chunk
plan_for_chunk[chid] = BasicCCFPlan( line_list=line_list_for_chunk, midpoint=plan.v_center, step=plan.v_step, max=plan.v_max, mask_shape=plan.mask_shape )
end

for chid in 1:num_chunks(clt)
order = clt[1].order[chid]
if typeof(plan.line_list) <: BasicLineList2D
start_order_idx = searchsortedfirst(plan.line_list.order,order)
stop_order_idx = searchsortedlast(view(plan.line_list.order,start_order_idx:num_lines),order) + start_order_idx-1
else
start_order_idx = 1
stop_order_idx = num_lines
end
if (1<=start_order_idx<=stop_order_idx<=num_lines)
# find the maximum lower wavelength for the chunk, and the minumum upper wavelength, over all observations
λmin = maximum(map(obsid->first(clt.chunk_list[obsid].data[chid].λ), 1:length(clt) ))
λmax = minimum(map(obsid->last( clt.chunk_list[obsid].data[chid].λ), 1:length(clt) ))
# extend λmin/λmax by the velocity range over which we don't want the mask to change
λmin = λmin / (calc_doppler_factor(plan.v_center)*calc_doppler_factor(-plan.v_range_no_mask_change))
λmax = λmax / (calc_doppler_factor(plan.v_center)*calc_doppler_factor(plan.v_range_no_mask_change))
# extend λmin/λmax by the mask width
upper_edge_of_mask_for_line_at_λmin = λ_max(plan.mask_shape,λmin)
lower_edge_of_mask_for_line_at_λmax = λ_min(plan.mask_shape,λmax)
# find the first and last mask entries to use in each chunk
start_line_idx = searchsortedfirst(view(plan.line_list.λ,start_order_idx:stop_order_idx),upper_edge_of_mask_for_line_at_λmin) + start_order_idx-1
stop_line_idx = start_order_idx-1 + searchsortedlast(view(plan.line_list.λ,start_order_idx:stop_order_idx),lower_edge_of_mask_for_line_at_λmax)
if verbose
flush(stdout)
println("extrema(plan.line_list.λ) = ",extrema(plan.line_list.λ) )
if (1 <= start_line_idx <= length(plan.line_list.λ)) && (1 <= stop_line_idx <= length(plan.line_list.λ))
println("start_order_idx = ", start_order_idx, " stop_order_idx = ", stop_order_idx)
println("start_line_idx = ", start_line_idx, " λ= ", plan.line_list.λ[start_line_idx]) #, " order= ", plan.line_list.order[start_line_idx])
println("stop_line_idx = ", stop_line_idx, " λ= ", plan.line_list.λ[stop_line_idx]) #, " order= ", plan.line_list.order[stop_line_idx])
end
println("upper_edge_of_mask_for_line_at_λmin = ", upper_edge_of_mask_for_line_at_λmin, " Lower_edge_of_mask_for_line_at_λmax = ", lower_edge_of_mask_for_line_at_λmax)
end
if (1 <= start_line_idx <= length(plan.line_list.λ)) && (1 <= stop_line_idx <= length(plan.line_list.λ))
if verbose && typeof(plan.line_list) <: BasicLineList2D
println("start_line_idx = ", start_line_idx, " λ= ", plan.line_list.λ[start_line_idx], " order= ", plan.line_list.order[start_line_idx])
println("stop_line_idx = ", stop_line_idx, " λ= ", plan.line_list.λ[stop_line_idx], " order= ", plan.line_list.order[stop_line_idx])
println(" Using ", length(start_line_idx:stop_line_idx), " lines for chunk ", chid, " order ", order)
end
if ! issorted( view(plan.line_list.λ, start_line_idx:stop_line_idx ) )
println("# View is not sorted. Why?")
for i in start_line_idx:stop_line_idx
println("i= ", i, " λ= ", plan.line_list.λ[i], " order= ", plan.line_list.order[i])
end
end
@assert issorted( view(plan.line_list.λ, start_line_idx:stop_line_idx ) )
# line_list_for_chunk = BasicLineList(view(plan.line_list.λ,start_line_idx:stop_line_idx), view(plan.line_list.weight,start_line_idx:stop_line_idx) )
line_list_for_chunk = BasicLineList(view(plan.line_list.λ,start_line_idx:stop_line_idx), view(plan.line_list.weight,start_line_idx:stop_line_idx) )
else # No lines in this chunk!
line_list_for_chunk = EmptyBasicLineList()
end
else
line_list_for_chunk = EmptyBasicLineList()
end
#create a plan for this chunk that only includes the mask entries we want for this chunk
plan_for_chunk[chid] = BasicCCFPlan( line_list=line_list_for_chunk, midpoint=plan.v_center, step=plan.v_step, max=plan.v_max, mask_shape=plan.mask_shape )
end

This would also help improve readability and bug-fixing.

@hematthi hematthi added the enhancement New feature or request label Jun 1, 2022
@hematthi
Copy link
Collaborator Author

hematthi commented Jun 1, 2022

The same block of code exists here too (other than lines 40-46):

for chid in 1:num_chunks(clt)
order = clt[1].order[chid]
if typeof(plan.line_list) <: BasicLineList2D
start_order_idx = searchsortedfirst(plan.line_list.order,order)
stop_order_idx = searchsortedlast(view(plan.line_list.order,start_order_idx:num_lines),order) + start_order_idx-1
else
start_order_idx = 1
stop_order_idx = num_lines
end
if verbose
println("# Chunk ", chid, " order= ", order)
println("# start_order_idx = ", start_order_idx, " stop_order_idx = ", stop_order_idx)
if (1<=start_order_idx<=stop_order_idx<=num_lines) && typeof(plan.line_list) <: BasicLineList2D
println(" order = ", plan.line_list.order[start_order_idx], " - ", plan.line_list.order[stop_order_idx])
end
end
if (1<=start_order_idx<=stop_order_idx<=num_lines)
# find the maximum lower wavelength for the chunk, and the minumum upper wavelength, over all observations
λmin = maximum(map(obsid->first(clt.chunk_list[obsid].data[chid].λ), 1:length(clt) ))
λmax = minimum(map(obsid->last( clt.chunk_list[obsid].data[chid].λ), 1:length(clt) ))
# extend λmin/λmax by the velocity range over which we don't want the mask to change
λmin = λmin / (calc_doppler_factor(plan.v_center)*calc_doppler_factor(-plan.v_range_no_mask_change))
λmax = λmax / (calc_doppler_factor(plan.v_center)*calc_doppler_factor(plan.v_range_no_mask_change))
# extend λmin/λmax by the mask width
upper_edge_of_mask_for_line_at_λmin = λ_max(plan.mask_shape,λmin)
lower_edge_of_mask_for_line_at_λmax = λ_min(plan.mask_shape,λmax)
# find the first and last mask entries to use in each chunk
start_line_idx = searchsortedfirst(view(plan.line_list.λ,start_order_idx:stop_order_idx),upper_edge_of_mask_for_line_at_λmin) + start_order_idx-1
stop_line_idx = start_order_idx-1 + searchsortedlast(view(plan.line_list.λ,start_order_idx:stop_order_idx),lower_edge_of_mask_for_line_at_λmax)
if verbose
flush(stdout)
println("extrema(plan.line_list.λ) = ",extrema(plan.line_list.λ) )
if (1 <= start_line_idx <= length(plan.line_list.λ)) && (1 <= stop_line_idx <= length(plan.line_list.λ))
println("start_order_idx = ", start_order_idx, " stop_order_idx = ", stop_order_idx)
println("start_line_idx = ", start_line_idx, " λ= ", plan.line_list.λ[start_line_idx]) #, " order= ", plan.line_list.order[start_line_idx])
println("stop_line_idx = ", stop_line_idx, " λ= ", plan.line_list.λ[stop_line_idx]) #, " order= ", plan.line_list.order[stop_line_idx])
end
println("upper_edge_of_mask_for_line_at_λmin = ", upper_edge_of_mask_for_line_at_λmin, " Lower_edge_of_mask_for_line_at_λmax = ", lower_edge_of_mask_for_line_at_λmax)
end
if (1 <= start_line_idx <= length(plan.line_list.λ)) && (1 <= stop_line_idx <= length(plan.line_list.λ))
if verbose && typeof(plan.line_list) <: BasicLineList2D
println("start_line_idx = ", start_line_idx, " λ= ", plan.line_list.λ[start_line_idx], " order= ", plan.line_list.order[start_line_idx])
println("stop_line_idx = ", stop_line_idx, " λ= ", plan.line_list.λ[stop_line_idx], " order= ", plan.line_list.order[stop_line_idx])
println(" Using ", length(start_line_idx:stop_line_idx), " lines for chunk ", chid, " order ", order)
end
if ! issorted( view(plan.line_list.λ, start_line_idx:stop_line_idx ) )
println("# View is not sorted. Why?")
for i in start_line_idx:stop_line_idx
println("i= ", i, " λ= ", plan.line_list.λ[i], " order= ", plan.line_list.order[i])
end
end
@assert issorted( view(plan.line_list.λ, start_line_idx:stop_line_idx ) )
# line_list_for_chunk = BasicLineList2D(view(plan.line_list.λ,start_line_idx:stop_line_idx), view(plan.line_list.weight,start_line_idx:stop_line_idx), fill(order,length(start_line_idx:stop_line_idx)) )
line_list_for_chunk = BasicLineList(view(plan.line_list.λ,start_line_idx:stop_line_idx), view(plan.line_list.weight,start_line_idx:stop_line_idx) )
else # No lines in this chunk!
line_list_for_chunk = EmptyBasicLineList()
end
else
line_list_for_chunk = EmptyBasicLineList()
end
#create a plan for this chunk that only includes the mask entries we want for this chunk
plan_for_chunk[chid] = BasicCCFPlan( line_list=line_list_for_chunk, midpoint=plan.v_center, step=plan.v_step, max=plan.v_max, mask_shape=plan.mask_shape )
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant