Skip to content

Commit 5a97f83

Browse files
authored
Merge pull request #266 from abostroem/replace_gala
Replace gala dependency with standalone files
2 parents 05f6a16 + 0bb9ffe commit 5a97f83

File tree

7 files changed

+180
-29
lines changed

7 files changed

+180
-29
lines changed

episodes/02-coords.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ perpendicular to the direction of the stream (the y-axis in Figure 1).
395395
These are called the $\phi_1$ and $\phi_2$ coordinates, respectively.
396396

397397
```python
398-
from gala.coordinates import GD1Koposov10
398+
from gd1 import GD1Koposov10
399399

400400
gd1_frame = GD1Koposov10()
401401
gd1_frame
@@ -405,6 +405,21 @@ gd1_frame
405405
<GD1Koposov10 Frame>
406406
```
407407

408+
::::::::::::::::::::::::::::::::::::::::: callout
409+
410+
## GD1Koposov10 import
411+
412+
The `GD1Koposov10` reference frame is part of the `gala` package. For convience we provide
413+
the stand alone file as part of the `student_download` zip file you downloaded. If your notebook
414+
is not in your `student_download` directory, you will need to add the following before the import
415+
statement
416+
```python
417+
import sys
418+
sys.path.append(<path to student_download>)
419+
```
420+
Where `<path to student_download>` is the path to your `student_download` directory ending with `student_download`.
421+
::::::::::::::::::::::::::::::::::::::::::::::::::
422+
408423
We can use it to find the coordinates of Betelgeuse in the GD-1 frame,
409424
like this:
410425

episodes/03-transform.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,21 @@ Whether you are working from a new notebook or coming back from a checkpoint,
6363
reloading the data will save you from having to run the query again.
6464

6565
If you are starting this episode here or starting this episode in a new notebook,
66-
you will need to run the following lines of code.
66+
you will need to run the following lines of code. The last two lines of the import
67+
statements assume your notebook is being run in the `student_download` directory.
68+
If you are running it from a different directory you will need to add the path using
69+
`sys.path.append(<path to student download>)` where `<path to student_download>` is
70+
the path to your `student_download` directory ending with `student_download`.
6771

6872
This imports previously imported functions:
6973

7074
```python
7175
import astropy.units as u
7276
from astropy.coordinates import SkyCoord
73-
from gala.coordinates import GD1Koposov10
7477
from astropy.table import Table
7578

7679
from episode_functions import *
80+
from gd1 import GD1Koposov10
7781
```
7882

7983
The following code loads in the data (instructions for downloading data can be
@@ -430,10 +434,11 @@ That might seem like a strange thing to do, but here is the motivation:
430434
With this preparation, we can use `reflex_correct` from Gala
431435
([documentation
432436
here](https://gala-astro.readthedocs.io/en/latest/api/gala.coordinates.reflex_correct.html))
433-
to correct for the motion of the solar system.
437+
to correct for the motion of the solar system which we have included as a stand alone file
438+
in the `student_download`.
434439

435440
```python
436-
from gala.coordinates import reflex_correct
441+
from reflex import reflex_correct
437442

438443
skycoord_gd1 = reflex_correct(transformed)
439444
```

episodes/05-select.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ Whether you are working from a new notebook or coming back from a checkpoint,
6464
reloading the data will save you from having to run the query again.
6565

6666
If you are starting this episode here or starting this episode in a new notebook,
67-
you will need to run the following lines of code.
67+
you will need to run the following lines of code. The last two lines of the import
68+
statements assume your notebook is being run in the `student_download` directory.
69+
If you are running it from a different directory you will need to add the path using
70+
`sys.path.append(<path to student download>)` where `<path to student_download>` is
71+
the path to your `student_download` directory ending with `student_download`.
72+
6873

6974
This imports previously imported functions:
7075

@@ -77,6 +82,8 @@ import matplotlib.pyplot as plt
7782
import pandas as pd
7883

7984
from episode_functions import *
85+
from gd1 import GD1Koposov10
86+
from reflex import reflex_correct
8087
```
8188

8289
The following code loads in the data (instructions for downloading data can be

student_download/episode_functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import astropy.units as u
99
from astropy.coordinates import SkyCoord
10-
from gala.coordinates import GD1Koposov10, reflex_correct
10+
from gd1 import GD1Koposov10
11+
from reflex import reflex_correct
1112

1213
##########################
1314
# Episode 2

student_download/gd1.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Astropy coordinate class for the GD-1 coordinate system"""
2+
3+
import astropy.coordinates as coord
4+
import astropy.units as u
5+
import numpy as np
6+
from astropy.coordinates import frame_transform_graph
7+
8+
__all__ = ["GD1Koposov10"]
9+
10+
11+
class GD1Koposov10(coord.BaseCoordinateFrame):
12+
"""
13+
A Heliocentric spherical coordinate system defined by the orbit of the GD1 stream,
14+
as described in Koposov et al. 2010 (see: `<http://arxiv.org/abs/0907.1085>`_).
15+
16+
For more information about this class, see the Astropy documentation on coordinate
17+
frames in :mod:`~astropy.coordinates`.
18+
19+
Parameters
20+
----------
21+
representation : :class:`~astropy.coordinates.BaseRepresentation` or None
22+
A representation object or None to have no data (or use the other keywords)
23+
phi1 : angle_like, optional, must be keyword
24+
The longitude-like angle corresponding to GD-1's orbit.
25+
phi2 : angle_like, optional, must be keyword
26+
The latitude-like angle corresponding to GD-1's orbit.
27+
distance : :class:`~astropy.units.Quantity`, optional, must be keyword
28+
The Distance for this object along the line-of-sight.
29+
pm_phi1_cosphi2 : :class:`~astropy.units.Quantity`, optional, must be keyword
30+
The proper motion in the longitude-like direction corresponding to
31+
the GD-1 stream's orbit.
32+
pm_phi2 : :class:`~astropy.units.Quantity`, optional, must be keyword
33+
The proper motion in the latitude-like direction perpendicular to the
34+
GD-1 stream's orbit.
35+
radial_velocity : :class:`~astropy.units.Quantity`, optional, must be keyword
36+
The radial velocity for this object along the line-of-sight.
37+
38+
"""
39+
40+
default_representation = coord.SphericalRepresentation
41+
default_differential = coord.SphericalCosLatDifferential
42+
43+
frame_specific_representation_info = {
44+
coord.SphericalRepresentation: [
45+
coord.RepresentationMapping("lon", "phi1"),
46+
coord.RepresentationMapping("lat", "phi2"),
47+
coord.RepresentationMapping("distance", "distance"),
48+
],
49+
}
50+
51+
_default_wrap_angle = 180 * u.deg
52+
53+
def __init__(self, *args, **kwargs):
54+
wrap = kwargs.pop("wrap_longitude", True)
55+
super().__init__(*args, **kwargs)
56+
if wrap and isinstance(
57+
self._data,
58+
coord.UnitSphericalRepresentation | coord.SphericalRepresentation,
59+
):
60+
self._data.lon.wrap_angle = self._default_wrap_angle
61+
62+
# TODO: remove this. This is a hack required as of astropy v3.1 in order
63+
# to have the longitude components wrap at the desired angle
64+
def represent_as(self, base, s="base", in_frame_units=False):
65+
r = super().represent_as(base, s=s, in_frame_units=in_frame_units)
66+
if hasattr(r, "lon"):
67+
r.lon.wrap_angle = self._default_wrap_angle
68+
return r
69+
70+
represent_as.__doc__ = coord.BaseCoordinateFrame.represent_as.__doc__
71+
72+
73+
# Rotation matrix as defined in the Appendix of Koposov et al. (2010)
74+
R = np.array(
75+
[
76+
[-0.4776303088, -0.1738432154, 0.8611897727],
77+
[0.510844589, -0.8524449229, 0.111245042],
78+
[0.7147776536, 0.4930681392, 0.4959603976],
79+
]
80+
)
81+
82+
83+
@frame_transform_graph.transform(coord.StaticMatrixTransform, coord.ICRS, GD1Koposov10)
84+
def icrs_to_gd1():
85+
"""
86+
Compute the transformation from ICRS spherical to heliocentric GD-1 coordinates.
87+
"""
88+
return R
89+
90+
91+
@frame_transform_graph.transform(coord.StaticMatrixTransform, GD1Koposov10, coord.ICRS)
92+
def gd1_to_icrs():
93+
"""
94+
Compute the transformation from heliocentric GD-1 coordinates to ICRS spherical.
95+
"""
96+
return icrs_to_gd1().T

student_download/reflex.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import astropy.coordinates as coord
2+
3+
__all__ = ["reflex_correct"]
4+
5+
6+
def reflex_correct(coords, galactocentric_frame=None):
7+
"""Correct the input Astropy coordinate object for solar reflex motion.
8+
9+
The input coordinate instance must have distance and radial velocity information.
10+
So, if the radial velocity is not known, fill the radial velocity values with zeros
11+
to reflex-correct the proper motions.
12+
13+
Parameters
14+
----------
15+
coords : `~astropy.coordinates.SkyCoord`
16+
The Astropy coordinate object with position and velocity information.
17+
galactocentric_frame : `~astropy.coordinates.Galactocentric` (optional)
18+
To change properties of the Galactocentric frame, like the height of the
19+
sun above the midplane, or the velocity of the sun in a Galactocentric
20+
intertial frame, set arguments of the
21+
`~astropy.coordinates.Galactocentric` object and pass in to this
22+
function with your coordinates.
23+
24+
Returns
25+
-------
26+
coords : `~astropy.coordinates.SkyCoord`
27+
The coordinates in the same frame as input, but with solar motion
28+
removed.
29+
30+
"""
31+
c = coord.SkyCoord(coords)
32+
33+
# If not specified, use the Astropy default Galactocentric frame
34+
if galactocentric_frame is None:
35+
galactocentric_frame = coord.Galactocentric()
36+
37+
v_sun = galactocentric_frame.galcen_v_sun
38+
39+
observed = c.transform_to(galactocentric_frame)
40+
rep = observed.cartesian.without_differentials()
41+
rep = rep.with_differentials(observed.cartesian.differentials["s"] + v_sun)
42+
fr = galactocentric_frame.realize_frame(rep).transform_to(c.frame)
43+
return coord.SkyCoord(fr)

student_download/test_setup.ipynb

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,15 @@
195195
"metadata": {},
196196
"outputs": [],
197197
"source": [
198-
"import gala.coordinates as gc"
198+
"from gd1 import GD1Koposov10\n",
199+
"from reflex import reflex_correct"
199200
]
200201
},
201202
{
202203
"cell_type": "code",
203204
"execution_count": 7,
204205
"metadata": {},
205-
"outputs": [
206-
{
207-
"name": "stdout",
208-
"output_type": "stream",
209-
"text": [
210-
"Created TAP+ (v20200428.1) - Connection:\n",
211-
"\tHost: gea.esac.esa.int\n",
212-
"\tUse HTTPS: True\n",
213-
"\tPort: 443\n",
214-
"\tSSL Port: 443\n",
215-
"Created TAP+ (v20200428.1) - Connection:\n",
216-
"\tHost: gea.esac.esa.int\n",
217-
"\tUse HTTPS: True\n",
218-
"\tPort: 443\n",
219-
"\tSSL Port: 443\n"
220-
]
221-
}
222-
],
206+
"outputs": [],
223207
"source": [
224208
"# Note: running this import statement opens a connection\n",
225209
"# to a Gaia server, so it will fail if you are not connected\n",
@@ -258,9 +242,9 @@
258242
],
259243
"metadata": {
260244
"kernelspec": {
261-
"display_name": "Python (AstronomicalData)",
245+
"display_name": "Python 3 (ipykernel)",
262246
"language": "python",
263-
"name": "astronomicaldata"
247+
"name": "python3"
264248
},
265249
"language_info": {
266250
"codemirror_mode": {
@@ -272,7 +256,7 @@
272256
"name": "python",
273257
"nbconvert_exporter": "python",
274258
"pygments_lexer": "ipython3",
275-
"version": "3.9.6"
259+
"version": "3.9.17"
276260
}
277261
},
278262
"nbformat": 4,

0 commit comments

Comments
 (0)