-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #695 from samaloney/doc-quantity-gwcs
Add example creating GWCS from quantities and times.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
Added a gallery example (:ref:`sphx_glr_generated_gallery_creating_a_gwcs_from_quantities.py`) showcasing how to create a GWCS from quantities. |
This file contains 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,52 @@ | ||
""" | ||
=============================================== | ||
How to create an GWCS from quantities and times | ||
=============================================== | ||
This example shows how to create a GWCS from astropy quantities. | ||
""" | ||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
|
||
import astropy.units as u | ||
from astropy.time import Time | ||
|
||
from ndcube import NDCube | ||
from ndcube.extra_coords import QuantityTableCoordinate, TimeTableCoordinate | ||
|
||
############################################################################## | ||
# We aim to create coordinates that are focused around time and energies using astropy quantities. | ||
|
||
energy = np.arange(10) * u.keV | ||
time = Time('2020-01-01 00:00:00') + np.arange(9)*u.s | ||
|
||
############################################################################## | ||
# Then, we need to turn these into lookup tables using | ||
# `~ndcube.extra_coords.table_coord.QuantityTableCoordinate` and | ||
# `~ndcube.extra_coords.table_coord.TimeTableCoordinate` to create table coordinates. | ||
|
||
energy_coord = QuantityTableCoordinate(energy, names='energy', physical_types='em.energy') | ||
print(energy_coord) | ||
|
||
time_coord = TimeTableCoordinate(time, names='time', physical_types='time') | ||
print(time_coord) | ||
|
||
############################################################################## | ||
# Now we need to combine table coordinates created above and extract the ``.wcs`` from the result. | ||
|
||
wcs = (time_coord & energy_coord).wcs | ||
print(wcs) | ||
|
||
############################################################################## | ||
# Now, we have all the pieces required to construct a `~ndcube.NDCube` with this data and the GWCS we just created. | ||
|
||
data = np.random.rand(len(time), len(energy)) | ||
cube = NDCube(data=data, wcs=wcs) | ||
print(cube) | ||
|
||
############################################################################## | ||
# Finally, we will plot the cube. | ||
|
||
cube.plot() | ||
|
||
plt.show() |