-
Notifications
You must be signed in to change notification settings - Fork 19
Mission Support - follow up #75
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
Open
Omiii-215
wants to merge
2
commits into
StingraySoftware:main
Choose a base branch
from
Omiii-215:mission-support-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,13 +97,13 @@ function check_gtis(gti::AbstractMatrix) | |
| gti_start = @view gti[:, 1] | ||
| gti_end = @view gti[:, 2] | ||
|
|
||
| if any(gti_end < gti_start) | ||
| if any(gti_end .< gti_start) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
| throw(ArgumentError( | ||
| "The GTI end times must be larger than the GTI start times." | ||
| )) | ||
| end | ||
|
|
||
| if any(@view(gti_start[begin+1:end]) < @view(gti_end[begin:end-1])) | ||
| if any(@view(gti_start[begin+1:end]) .< @view(gti_end[begin:end-1])) | ||
| throw(ArgumentError( | ||
| "This GTI has overlaps" | ||
| )) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| """ | ||
| Dictionary of simple conversion functions for different missions. | ||
|
|
||
| This dictionary provides PI (Pulse Invariant) to energy conversion functions | ||
| for various X-ray astronomy missions. Each function takes a PI channel value | ||
| and returns the corresponding energy in keV. | ||
|
|
||
| Supported missions: | ||
| - NuSTAR: Nuclear Spectroscopic Telescope Array | ||
| - XMM: X-ray Multi-Mirror Mission | ||
| - NICER: Neutron star Interior Composition Explorer | ||
| - IXPE: Imaging X-ray Polarimetry Explorer | ||
| - AXAF/Chandra: Advanced X-ray Astrophysics Facility | ||
| - XTE/RXTE: Rossi X-ray Timing Explorer | ||
| """ | ||
| const SIMPLE_CALIBRATION_FUNCS = Dict{String, Function}( | ||
| "nustar" => (pi) -> pi * 0.04 + 1.62, | ||
| "xmm" => (pi) -> pi * 0.001, | ||
| "nicer" => (pi) -> pi * 0.01, | ||
| "ixpe" => (pi) -> pi / 375 * 15, | ||
| "axaf" => (pi) -> (pi - 1) * 14.6e-3, # Chandra/AXAF | ||
| "chandra" => (pi) -> (pi - 1) * 14.6e-3, # Explicit chandra entry | ||
| "xte" => (pi) -> pi * 0.025 # RXTE/XTE | ||
| ) | ||
|
|
||
| """ | ||
| Abstract type for mission-specific calibration and interpretation. | ||
|
|
||
| This serves as the base type for all mission support implementations, | ||
| allowing for extensibility and type safety in mission-specific operations. | ||
| """ | ||
| abstract type AbstractMissionSupport end | ||
|
|
||
| """ | ||
| MissionSupport{T} <: AbstractMissionSupport | ||
|
|
||
| Structure containing mission-specific calibration and interpretation information. | ||
|
|
||
| This structure encapsulates all the necessary information for handling | ||
| data from a specific X-ray astronomy mission, including calibration | ||
| functions, energy column alternatives, and GTI extension preferences. | ||
|
|
||
| # Fields | ||
| - `name::String`: Mission name (normalized to lowercase) | ||
| - `instrument::Union{String, Nothing}`: Instrument identifier | ||
| - `epoch::Union{T, Nothing}`: Observation epoch in MJD (for time-dependent calibrations) | ||
| - `calibration_func::Function`: PI to energy conversion function | ||
| - `interpretation_func::Union{Function, Nothing}`: Mission-specific FITS interpretation function | ||
| - `energy_alternatives::Vector{String}`: Preferred energy column names in order of preference | ||
| - `gti_extensions::Vector{String}`: GTI extension names in order of preference | ||
|
|
||
| # Type Parameters | ||
| - `T`: Type of the epoch parameter (typically Float64) | ||
| """ | ||
| struct MissionSupport{T} <: AbstractMissionSupport | ||
| name::String | ||
| instrument::Union{String, Nothing} | ||
| epoch::Union{T, Nothing} | ||
| calibration_func::Function | ||
| interpretation_func::Union{Function, Nothing} | ||
| energy_alternatives::Vector{String} | ||
| gti_extensions::Vector{String} | ||
| end | ||
|
|
||
| """ | ||
| get_mission_support(mission::String, instrument=nothing, epoch=nothing) -> MissionSupport | ||
|
|
||
| Create mission support object with mission-specific parameters. | ||
|
|
||
| This function creates a MissionSupport object containing all the necessary | ||
| information for processing data from a specified X-ray astronomy mission. | ||
| It handles mission aliases (e.g., Chandra/AXAF) and provides appropriate | ||
| defaults for each mission. | ||
|
|
||
| # Arguments | ||
| - `mission::String`: Mission name (case-insensitive) | ||
| - `instrument::Union{String, Nothing}=nothing`: Instrument identifier | ||
| - `epoch::Union{Float64, Nothing}=nothing`: Observation epoch in MJD | ||
|
|
||
| # Returns | ||
| - `MissionSupport{Float64}`: Mission support object | ||
|
|
||
| # Throws | ||
| - `ArgumentError`: If mission name is empty | ||
|
|
||
| # Examples | ||
| ```julia | ||
| # Basic usage | ||
| ms = get_mission_support("nustar") | ||
|
|
||
| # With instrument specification | ||
| ms = get_mission_support("nustar", "FPM_A") | ||
|
|
||
| # With epoch for time-dependent calibrations | ||
| ms = get_mission_support("xte", "PCA", 50000.0) | ||
| ``` | ||
| """ | ||
| function get_mission_support(mission::String, | ||
| instrument::Union{String, Nothing}=nothing, | ||
| epoch::Union{Float64, Nothing}=nothing) | ||
|
|
||
| # Check for empty mission string | ||
| if isempty(mission) | ||
| throw(ArgumentError("Mission name cannot be empty")) | ||
| end | ||
|
|
||
| mission_lower = lowercase(mission) | ||
|
|
||
| if mission_lower in ["chandra", "axaf"] | ||
| mission_lower = "chandra" | ||
| end | ||
|
|
||
| calib_func = if haskey(SIMPLE_CALIBRATION_FUNCS, mission_lower) | ||
| SIMPLE_CALIBRATION_FUNCS[mission_lower] | ||
| else | ||
| @warn "Mission $mission not recognized, using identity function" | ||
| identity | ||
| end | ||
|
|
||
| energy_alts = if mission_lower in ["chandra", "axaf"] | ||
| ["ENERGY", "PI", "PHA"] | ||
| elseif mission_lower == "xte" | ||
| ["PHA", "PI", "ENERGY"] | ||
| elseif mission_lower == "nustar" | ||
| ["PI", "ENERGY", "PHA"] | ||
| else | ||
| ["ENERGY", "PI", "PHA"] | ||
| end | ||
|
|
||
| # Mission-specific GTI extensions | ||
| gti_exts = if mission_lower == "xmm" | ||
| ["GTI", "GTI0", "STDGTI"] | ||
| elseif mission_lower in ["chandra", "axaf"] | ||
| ["GTI", "GTI0", "GTI1", "GTI2", "GTI3"] | ||
| else | ||
| ["GTI", "STDGTI"] | ||
| end | ||
|
|
||
| MissionSupport{Float64}(mission_lower, instrument, epoch, calib_func, nothing, energy_alts, gti_exts) | ||
| end | ||
|
|
||
| """ | ||
| apply_calibration(mission_support::MissionSupport, pi_channels::AbstractArray) -> Vector{Float64} | ||
|
|
||
| Apply calibration function to PI channels. | ||
|
|
||
| Converts PI (Pulse Invariant) channel values to energies in keV using | ||
| the mission-specific calibration function stored in the MissionSupport object. | ||
|
|
||
| # Arguments | ||
| - `mission_support::MissionSupport`: Mission support object containing calibration function | ||
| - `pi_channels::AbstractArray{T}`: Array of PI channel values | ||
|
|
||
| # Returns | ||
| - `Vector{Float64}`: Array of energy values in keV | ||
|
|
||
| # Examples | ||
| ```julia | ||
| ms = get_mission_support("nustar") | ||
| pi_values = [100, 500, 1000] | ||
| energies = apply_calibration(ms, pi_values) | ||
| ``` | ||
| """ | ||
| function apply_calibration(mission_support::MissionSupport, pi_channels::AbstractArray{T}) where T | ||
| if isempty(pi_channels) | ||
| return similar(pi_channels, Float64) | ||
| end | ||
| return mission_support.calibration_func.(pi_channels) | ||
| end | ||
|
|
||
| """ | ||
| patch_mission_info(info::Dict{String,Any}, mission=nothing) -> Dict{String,Any} | ||
|
|
||
| Apply mission-specific patches to header information. | ||
|
|
||
| This function applies mission-specific modifications to FITS header information | ||
| to handle mission-specific quirks and conventions. It's based on the Python | ||
| implementation in Stingray's mission interpretation module. | ||
|
|
||
| # Arguments | ||
| - `info::Dict{String,Any}`: Dictionary containing header information | ||
| - `mission::Union{String,Nothing}=nothing`: Mission name | ||
|
|
||
| # Returns | ||
| - `Dict{String,Any}`: Patched header information dictionary | ||
|
|
||
| # Examples | ||
| ```julia | ||
| info = Dict("gti" => "STDGTI", "ecol" => "PHA") | ||
| patched = patch_mission_info(info, "xmm") # Adds GTI0 to gti field | ||
| ``` | ||
| """ | ||
| function patch_mission_info(info::Dict{String,Any}, mission::Union{String,Nothing}=nothing) | ||
| if isnothing(mission) | ||
| return info | ||
| end | ||
|
|
||
| mission_lower = lowercase(mission) | ||
| patched_info = copy(info) | ||
|
|
||
| # Normalize chandra/axaf | ||
| if mission_lower in ["chandra", "axaf"] | ||
| mission_lower = "chandra" | ||
| end | ||
|
|
||
| if mission_lower == "xmm" && haskey(patched_info, "gti") | ||
| patched_info["gti"] = string(patched_info["gti"], ",GTI0") | ||
| elseif mission_lower == "xte" && haskey(patched_info, "ecol") | ||
| patched_info["ecol"] = "PHA" | ||
| patched_info["ccol"] = "PCUID" | ||
| elseif mission_lower == "chandra" | ||
| # Chandra-specific patches | ||
| if haskey(patched_info, "DETNAM") | ||
| patched_info["detector"] = patched_info["DETNAM"] | ||
| end | ||
| if haskey(patched_info, "TIMESYS") | ||
| patched_info["time_system"] = patched_info["TIMESYS"] | ||
| end | ||
| end | ||
|
|
||
| return patched_info | ||
| end | ||
| function interpret_fits_data!(f::FITS, mission_support::MissionSupport) | ||
| # Placeholder for mission-specific interpretation | ||
| return nothing | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!