## How do I create a plot from ChIPS in a script?

There are several ways to create a hard-copy version (e.g. postscript, PDF or PNG) of a visualization without creating an on-screen version: from ChIPS and from Python.

### Using the chips program

The -b option of ChIPS will run in "batch" mode; this turns off the window.redraw preference setting so that no on-screen updates are made and will cause ChIPS to exit rather than display its prompt once all the commands have been executed. In order to create any output you will need to use the print_window command.

As an example; the Two curves in a plot example in the gallery could be run as a script:

unix% cat overlay-plot
make_figure("flare.fits[cols time_bin,count_rate]",["symbol.style","none"])
add_curve("flare.fits[count_rate>5][cols time_bin,count_rate]")
crv = ChipsCurve()
crv.line.style = "noline"
crv.symbol.style = "circle"
crv.symbol.size = 2
crv.symbol.color = "red"
set_curve(crv)
log_scale(Y_AXIS)
add_hline(5,["style","longdash"])
set_plot_xlabel("Time bin")
set_plot_ylabel("Count rate (s^{-1})")
set_plot_title("Background light curve")
add_label(22,12,"Cut off at 5 count s^{-1}")
print_window("overlay-plot.eps", ["clobber", True])
print_window("overlay-plot.png", ["clobber", True])
print("Created: overlay-plot.eps and .png")


which can be "run" by saying:

unix% chips -b overlay-plot
Created: overlay-plot.eps and .png
unix% ls -1 overlay-plot.*
overlay-plot.eps
overlay-plot.png


where the clobber parameter is set to True so that it will not fail if the script has already been run.

Notes:

• One advantage to this approach is that you can call ChIPS without the -b option; this will create the plot and display it on screen, leaving you at the prompt. This allows you to easily try out further refinements or additions without having to continually re-run the script. Once you have worked out the changes you do want you can add them to the script.

• You can also run a script from within a ChIPS interactive session using the execfile command:

chips> clear()
chips> execfile("overlay-plot")
overlay-plot.eps
overlay-plot.png
chips>


which also allows you to make edits and additions. In this particular example we used the clear command to ensure that a new visualization was created, but there are times when you may wish to add to an existing one.

• Although the script is a list of Python commands, the name does not need to end in a .py extension.

• There is no way to pass in arguments to a script using ChIPS; for that you need to use Python directly.

### As a Python script

As the ChIPS interpreter is really just Python shell, you can also use python itself to create the plot; it means you have to explicitly load the ChIPS modules you need but does allow for the use of command-line arguments.

Here we convert the version used above to be run directly by Python by adding four lines to the start of the file (they have been emphasized in the display below):

unix% cat overlay-plot.py
#!/usr/bin/env python
from pychips import *
from pychips.hlui import *
set_preference("window.display", "false")
make_figure("flare.fits[cols time_bin,count_rate]",["symbol.style","none"])
add_curve("flare.fits[count_rate>5][cols time_bin,count_rate]")
crv = ChipsCurve()
crv.line.style = "noline"
crv.symbol.style = "circle"
crv.symbol.size = 2
crv.symbol.color = "red"
set_curve(crv)
log_scale(Y_AXIS)
add_hline(5,["style","longdash"])
set_plot_xlabel("Time bin")
set_plot_ylabel("Count rate (s^{-1})")
set_plot_title("Background light curve")
add_label(22,12,"Cut off at 5 count s^{-1}")
print_window("overlay-plot.eps", ["clobber", True])
print_window("overlay-plot.png", ["clobber", True])
print("Created: overlay-plot.eps and .png")


which can then be run by either of

unix% python overlay-plot.py
Created: overlay-plot.eps and .png
unix% ./overlay-plot.py
Created: overlay-plot.eps and .png


where the second version requires chmod u+x ./overlay-plot.py to have been used.

The two from ... lines make sure the ChIPS commands are available, the set_preference call makes sure that the on-screen version of the visualization is not created, and the first line is only needed if you want to run the script directly (as in the second example above).

### Running a script with arguments

Using Python to run the script, rather than ChIPS, allows you to provide arguments to your script by using the sys.argv array provided by the Python sys module. As an example:

unix% cat qplot.py
import sys
import time
from pychips import *
from pychips.hlui import *

narg = len(sys.argv)
if narg != 3:
raise TypeError("Usage: {0} infile outfile".format(sys.argv[0]))

set_preference("window.display", "false")
add_window(8.5, 11, "inches")
make_figure(sys.argv[1], ["line.style", "noline"])
add_label(0.95, 0.05, time.asctime(), ["coordsys", FRAME_NORM, "halign", 1])
print_window(sys.argv[2], ["clobber", True])


can be used to quickly create a US-letter sized plot by saying

unix% python qplot.py "srcs.dat[cols z,lx]" qplot.pdf


The add_label call places a label at the bottom-right of the frame with the current time (as returned by the asctime routine). The halign attribute is used to make sure the label is right-aligned to the given coordinate (95% of the frame width and 5% of its height) with the coordsys attribute defining the coordinate system.