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 QuantityTableCoordinate and 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)
QuantityTableCoordinate ['energy'] ['em.energy']:
<Quantity [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.] keV>
TimeTableCoordinate ['time'] ['time']:
['2020-01-01 00:00:00.000' '2020-01-01 00:00:01.000'
 '2020-01-01 00:00:02.000' '2020-01-01 00:00:03.000'
 '2020-01-01 00:00:04.000' '2020-01-01 00:00:05.000'
 '2020-01-01 00:00:06.000' '2020-01-01 00:00:07.000'
 '2020-01-01 00:00:08.000']

Now we need to combine table coordinates created above and extract the .wcs from the result.

wcs = (time_coord & energy_coord).wcs
print(wcs)
     From        Transform
-------------- -------------
    Frame-b1cd CompoundModel
CompositeFrame          None

Now, we have all the pieces required to construct a 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)
NDCube
------
Shape: (9, 10)
Physical Types of Axes: [('em.energy',), ('time',)]
Unit: None
Data Type: float64

Finally, we will plot the cube.

cube.plot()

plt.show()
creating a gwcs from quantities

Total running time of the script: (0 minutes 0.321 seconds)

Gallery generated by Sphinx-Gallery