Last modified: 12 December 2018

Can I read in data from a file and then plot it?


The Crates module provides data input and output routines for Python. It can be used to read in data from a file which can then be plotted - perhaps after manipulation - using ChIPS. The following code shows how the columns time, rate, and error can be plotted (after converting the time column from seconds to an offset in kiloseconds). In this example the input file is assumed to be a FITS binary table but ASCII tables can also be used.

# The following two lines are not needed when running ChIPS
# but are if the file is to be executed by python
from pycrates import *
from pychips.hlui import *

cr = read_file("lc.fits")
t = copy_colvals(cr, "time")
x = (t - t[0]) / 1e3
y = copy_colvals(cr,"rate")
dy = copy_colvals(cr,"error")

add_curve(x, y, dy, ["line.style","none"])
set_plot_xlabel(r"\Delta T (ks)")
set_plot_ylabel("Rate (count s^{-1})")
set_plot_title(get_keyval(cr, "OBJECT"))

The get_keyval call is used to extract the value of the OBJECT keyword from the FITS header of lc.fits, which is then used to create the title for the plot.