Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
316 changes: 153 additions & 163 deletions notebooks/examples/velocity_frames.ipynb

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cell 6: sdfits["CTYPE1"][0] returns nan

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, sometimes notebook plots just hang but I don't belive that has anything to do with this PR.

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions src/dysh/spectra/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,37 @@ def rest_value(self, value: Quantity):
self.meta["RESTFREQ"] = value.to("Hz").value
self.meta["RESTFRQ"] = value.to("Hz").value

def with_spectral_axis_unit(self, *args, **kwargs):
spec = super().with_spectral_axis_unit(*args, **kwargs)
spec._observer = self.observer.copy()
spec._spectral_axis.observer = self.spectral_axis.observer.copy()
spec._target = self.target.copy()
spec._spectral_axis.target = self.spectral_axis.target.copy()
return spec

def set_spectral_axis(self, unit=None, toframe=None, doppler_convention=None) -> None:
"""
Modifies the spectral axis to have units of `unit` in reference frame `toframe`
using Doppler convention `doppler_convention`. This changes the `Spectrum`.

Parameters
----------
unit : `~astropy.units.quantity.Quantity` or str that can be converted to Quantity
The unit to which the axis is to be converted.
toframe : str
The coordinate frame to convert to, e.g. 'hcrs', 'icrs'.
doppler_convention : None or {'optical', 'radio', 'relativistic'}
The Doppler convention to use when converting to velocity.
One of 'optical', 'radio', or 'relativistic'.
"""
if unit is None and toframe is not None:
self.set_frame(toframe)
if unit is None and doppler_convention is not None:
self.set_convention(doppler_convention)
if unit is not None:
sa = self.velocity_axis_to(unit=unit, toframe=toframe, doppler_convention=doppler_convention)
self._spectral_axis = sa

def axis_velocity(self, unit=KMS):
"""Get the spectral axis in velocity units.
*Note*: This is not the same as `Spectrum.velocity`, which includes the source radial velocity.
Expand Down
20 changes: 20 additions & 0 deletions src/dysh/spectra/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,3 +1300,23 @@ def test_header(self, capsys):
s.header()
captured = capsys.readouterr()
assert captured.out == expected

def test_set_spectral_axis(self):
"""
Test that the spectral_axis is updated.
"""

sp = Spectrum.fake_spectrum()
sp.set_spectral_axis(toframe="galactocentric")
assert sp.observer.name == "galactocentric"
# Try all three options in case the defaults change.
if sp.doppler_convention != "radio":
expected = "radio"
elif sp.doppler_convention != "optical":
expected = "optical"
else:
expected = "relativistic"
sp.set_spectral_axis(doppler_convention=expected)
assert sp.doppler_convention == expected
sp.set_spectral_axis(unit="km/s")
assert sp.spectral_axis.unit.to_string() == "km / s"
Loading