-
Notifications
You must be signed in to change notification settings - Fork 9
Enable phase_center inputs to be SkyCoord #77
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
DanRyanIrish
wants to merge
13
commits into
TCDSolar:main
Choose a base branch
from
DanRyanIrish:phase_center_skycoord
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
13 commits
Select commit
Hold shift + click to select a range
7fd3c6d
Add generic projective frame.
DanRyanIrish 2586df5
Delete xrayvision/coordinates/.frames.py.swp
DanRyanIrish 8967aba
Delete xrayvision/coordinates/tests/.test_frames.py.swp
DanRyanIrish 8d8ca4a
Enable SkyCoord support by phase_center.
DanRyanIrish b8ad4e8
Fixes codestyle.
DanRyanIrish 255f815
Add changelog 77.
DanRyanIrish 1afc9e2
Remove unwanted quantity decorator from image_to_vis.
DanRyanIrish d7e1a90
Fix bug in setting crpix in generate_header.
DanRyanIrish 71936b6
Fix bug function calculating wcs from projective frame in case when o…
DanRyanIrish e8c5f54
Adapt STIX example to use Projective frame.
DanRyanIrish 8a654db
Apply some bugs found in code review.
DanRyanIrish 0ed96eb
Merge branch 'imaging_frame' into phase_center_skycoord
DanRyanIrish 10b647a
Fix codestyle.
DanRyanIrish 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add `~xrayvision.coordinates.frames.Projective` coordinate frame. This is intended to represent the projective coordinate system of images with unknown pointing. |
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 @@ | ||
| Enable ``phase_center`` inputs to functions that calculate visibilities to be given as `astropy.coordinates.SkyCoord`. |
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
Empty file.
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,122 @@ | ||
| import astropy.coordinates | ||
| import astropy.units as u | ||
| from astropy.wcs import WCS | ||
| from sunpy.coordinates.frameattributes import ObserverCoordinateAttribute | ||
| from sunpy.coordinates.frames import HeliographicStonyhurst, SunPyBaseCoordinateFrame | ||
| from sunpy.sun.constants import radius as _RSUN | ||
|
|
||
| __all__ = ["Projective"] | ||
|
|
||
|
|
||
| X_CTYPE = "PJLN" | ||
| Y_CTYPE = "PJLT" | ||
|
|
||
|
|
||
| class Projective(SunPyBaseCoordinateFrame): | ||
| """A generic projective coordinate frame for an image taken by an arbitrary imager.""" | ||
|
|
||
| observer = ObserverCoordinateAttribute(HeliographicStonyhurst) | ||
| rsun = astropy.coordinates.QuantityAttribute(default=_RSUN, unit=u.km) | ||
| frame_specific_representation_info = { | ||
| astropy.coordinates.SphericalRepresentation: [ | ||
| astropy.coordinates.RepresentationMapping("lon", u.arcsec), | ||
| astropy.coordinates.RepresentationMapping("lat", u.arcsec), | ||
| astropy.coordinates.RepresentationMapping("distance", "distance"), | ||
| ], | ||
| astropy.coordinates.UnitSphericalRepresentation: [ | ||
| astropy.coordinates.RepresentationMapping("lon", u.arcsec), | ||
| astropy.coordinates.RepresentationMapping("lat", u.arcsec), | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def projective_wcs_to_frame(wcs): | ||
| r""" | ||
| This function registers the coordinate frames to their FITS-WCS coordinate | ||
| type values in the `astropy.wcs.utils.wcs_to_celestial_frame` registry. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| wcs : `astropy.wcs.WCS` | ||
|
|
||
| Returns | ||
| ------- | ||
| : `Projective` | ||
| """ | ||
| if hasattr(wcs, "coordinate_frame"): | ||
| return wcs.coordinate_frame | ||
|
|
||
| # Not a lat,lon coordinate system bail out early | ||
| if set(wcs.wcs.ctype) != {X_CTYPE, Y_CTYPE}: | ||
| return None | ||
|
|
||
| dateavg = wcs.wcs.dateobs | ||
|
|
||
| rsun = wcs.wcs.aux.rsun_ref | ||
| if rsun is not None: | ||
| rsun *= u.m | ||
|
|
||
| hgs_longitude = wcs.wcs.aux.hgln_obs | ||
| hgs_latitude = wcs.wcs.aux.hglt_obs | ||
| hgs_distance = wcs.wcs.aux.dsun_obs | ||
|
|
||
| observer = HeliographicStonyhurst( | ||
| lat=hgs_latitude * u.deg, lon=hgs_longitude * u.deg, radius=hgs_distance * u.m, obstime=dateavg, rsun=rsun | ||
| ) | ||
|
|
||
| frame_args = {"obstime": dateavg, "observer": observer, "rsun": rsun} | ||
|
|
||
| return Projective(**frame_args) | ||
|
|
||
|
|
||
| def projective_frame_to_wcs(frame, projection="TAN"): | ||
| r""" | ||
| For a given frame, this function returns the corresponding WCS object. | ||
|
|
||
| It registers the WCS coordinates types from their associated frame in the | ||
| `astropy.wcs.utils.celestial_frame_to_wcs` registry. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| frame : `Projective` | ||
| projection : `str`, optional | ||
|
|
||
| Returns | ||
| ------- | ||
| `astropy.wcs.WCS` | ||
| """ | ||
| # Bail out early if not STIXImaging frame | ||
| if not isinstance(frame, Projective): | ||
| return None | ||
|
|
||
| wcs = WCS(naxis=2) | ||
| wcs.wcs.aux.rsun_ref = frame.rsun.to_value(u.m) | ||
|
|
||
| # Sometimes obs_coord can be a SkyCoord, so convert down to a frame | ||
| obs_frame = frame.observer | ||
| if hasattr(obs_frame, "frame"): | ||
| obs_frame = frame.observer.frame | ||
|
|
||
| if obs_frame: | ||
| wcs.wcs.aux.hgln_obs = obs_frame.lon.to_value(u.deg) | ||
| wcs.wcs.aux.hglt_obs = obs_frame.lat.to_value(u.deg) | ||
| wcs.wcs.aux.dsun_obs = obs_frame.radius.to_value(u.m) | ||
|
|
||
| if frame.obstime: | ||
| wcs.wcs.dateobs = frame.obstime.utc.iso | ||
| wcs.wcs.cunit = ["arcsec", "arcsec"] | ||
| wcs.wcs.ctype = [X_CTYPE, Y_CTYPE] | ||
|
|
||
| return wcs | ||
|
|
||
|
|
||
| # Remove once min version of sunpy has https://github.com/sunpy/sunpy/pull/7594 | ||
| astropy.wcs.utils.WCS_FRAME_MAPPINGS.insert(-1, [projective_wcs_to_frame]) | ||
| astropy.wcs.utils.FRAME_WCS_MAPPINGS.insert(-1, [projective_frame_to_wcs]) | ||
|
|
||
| PROJECTIVE_CTYPE_TO_UCD1 = { | ||
| "PJLT": "custom:pos.projective.lat", | ||
| "PJLN": "custom:pos.projective.lon", | ||
| "PJRZ": "custom:pos.projective.z", | ||
| } | ||
| astropy.wcs.wcsapi.fitswcs.CTYPE_TO_UCD1.update(PROJECTIVE_CTYPE_TO_UCD1) |
Empty file.
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,73 @@ | ||
| import astropy.units as u | ||
| import numpy as np | ||
| import pytest | ||
| from astropy.wcs import WCS | ||
| from sunpy.coordinates import HeliographicStonyhurst | ||
|
|
||
| from xrayvision.coordinates.frames import Projective, projective_frame_to_wcs, projective_wcs_to_frame | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def projective_wcs(): | ||
| w = WCS(naxis=2) | ||
|
|
||
| w.wcs.dateobs = "2024-01-01 00:00:00.000" | ||
| w.wcs.crpix = [10, 20] | ||
| w.wcs.cdelt = np.array([2, 2]) | ||
| w.wcs.crval = [0, 0] | ||
| w.wcs.ctype = ["PJLN", "PJLT"] | ||
|
|
||
| w.wcs.aux.hgln_obs = 10 | ||
| w.wcs.aux.hglt_obs = 20 | ||
| w.wcs.aux.dsun_obs = 1.5e11 | ||
|
|
||
| return w | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def projective_frame(): | ||
| obstime = "2024-01-01" | ||
| observer = HeliographicStonyhurst(10 * u.deg, 20 * u.deg, 1.5e11 * u.m, obstime=obstime) | ||
|
|
||
| frame_args = {"obstime": obstime, "observer": observer, "rsun": 695_700_000 * u.m} | ||
|
|
||
| frame = Projective(**frame_args) | ||
| return frame | ||
|
|
||
|
|
||
| def test_projective_wcs_to_frame(projective_wcs): | ||
| frame = projective_wcs_to_frame(projective_wcs) | ||
| assert isinstance(frame, Projective) | ||
|
|
||
| assert frame.obstime.isot == "2024-01-01T00:00:00.000" | ||
| assert frame.rsun == 695700 * u.km | ||
| assert frame.observer == HeliographicStonyhurst( | ||
| 10 * u.deg, 20 * u.deg, 1.5e11 * u.m, obstime="2024-01-01T00:00:00.000" | ||
| ) | ||
|
|
||
|
|
||
| def test_projective_wcs_to_frame_none(): | ||
| w = WCS(naxis=2) | ||
| w.wcs.ctype = ["ham", "cheese"] | ||
| frame = projective_wcs_to_frame(w) | ||
|
|
||
| assert frame is None | ||
|
|
||
|
|
||
| def test_projective_frame_to_wcs(projective_frame): | ||
| wcs = projective_frame_to_wcs(projective_frame) | ||
|
|
||
| assert isinstance(wcs, WCS) | ||
| assert wcs.wcs.ctype[0] == "PJLN" | ||
| assert wcs.wcs.cunit[0] == "arcsec" | ||
| assert wcs.wcs.dateobs == "2024-01-01 00:00:00.000" | ||
|
|
||
| assert wcs.wcs.aux.rsun_ref == projective_frame.rsun.to_value(u.m) | ||
| assert wcs.wcs.aux.dsun_obs == 1.5e11 | ||
| assert wcs.wcs.aux.hgln_obs == 10 | ||
| assert wcs.wcs.aux.hglt_obs == 20 | ||
|
|
||
|
|
||
| def test_projective_frame_to_wcs_none(): | ||
| wcs = projective_frame_to_wcs(HeliographicStonyhurst()) | ||
| assert wcs is None |
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.
Indexing a SkyCoord object as if it were a sequence may result in an error since phase_center is expected to be a scalar SkyCoord. Use the existing attributes (e.g. phase_center.Tx and phase_center.Ty) instead of indexing.