Skip to content

Commit 0cc3229

Browse files
authored
Merge pull request #695 from samaloney/doc-quantity-gwcs
Add example creating GWCS from quantities and times.
2 parents 4858041 + eb98a8e commit 0cc3229

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

changelog/695.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a gallery example (:ref:`sphx_glr_generated_gallery_creating_a_gwcs_from_quantities.py`) showcasing how to create a GWCS from quantities.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
===============================================
3+
How to create an GWCS from quantities and times
4+
===============================================
5+
6+
This example shows how to create a GWCS from astropy quantities.
7+
"""
8+
import numpy as np
9+
from matplotlib import pyplot as plt
10+
11+
import astropy.units as u
12+
from astropy.time import Time
13+
14+
from ndcube import NDCube
15+
from ndcube.extra_coords import QuantityTableCoordinate, TimeTableCoordinate
16+
17+
##############################################################################
18+
# We aim to create coordinates that are focused around time and energies using astropy quantities.
19+
20+
energy = np.arange(10) * u.keV
21+
time = Time('2020-01-01 00:00:00') + np.arange(9)*u.s
22+
23+
##############################################################################
24+
# Then, we need to turn these into lookup tables using
25+
# `~ndcube.extra_coords.table_coord.QuantityTableCoordinate` and
26+
# `~ndcube.extra_coords.table_coord.TimeTableCoordinate` to create table coordinates.
27+
28+
energy_coord = QuantityTableCoordinate(energy, names='energy', physical_types='em.energy')
29+
print(energy_coord)
30+
31+
time_coord = TimeTableCoordinate(time, names='time', physical_types='time')
32+
print(time_coord)
33+
34+
##############################################################################
35+
# Now we need to combine table coordinates created above and extract the ``.wcs`` from the result.
36+
37+
wcs = (time_coord & energy_coord).wcs
38+
print(wcs)
39+
40+
##############################################################################
41+
# Now, we have all the pieces required to construct a `~ndcube.NDCube` with this data and the GWCS we just created.
42+
43+
data = np.random.rand(len(time), len(energy))
44+
cube = NDCube(data=data, wcs=wcs)
45+
print(cube)
46+
47+
##############################################################################
48+
# Finally, we will plot the cube.
49+
50+
cube.plot()
51+
52+
plt.show()

0 commit comments

Comments
 (0)