Skip banner and navigation tools.

 |  site map

Common module-level functions

There are a handful of functions which are called by various elements of the swifttools.ukssdc module, so rather than documenting them every time they appear, they are introduced here, and I will link back here in the other documentation as and when these functions are used.

In most cases, you will never actually call these functions direct (indeed, they're not exported into any of the module namespaces†), but the functions that you do use will ultimately make use of these functions, passing on appropriate arguments; those arguments usually appear as **kwargs in the docstrings of the functions you are calling. The purpose of this page is to introduce those arguments so you know what they mean.

Because these functions are not intended to be called directly, to demonstrate them I will use the swifttools.ukssdc.data.GRB module, but it's the common behaviour I'm trying to show.

The handful of functions you can call direct are documented at the end of this page.

(† Just to be awkward, there is one function which doesn't fall into this divide. plotLightCurve() is directly available if you want it, but equally the various modules in this package all provide wrappers to it).

Page contents

General notes

Nearly every function takes the following two arguments:

These are not listed for each function below.

When using the data module, you can supply these to whichever function you are calling and they will be passed through. If you are using the query module then you do not specify these, they are properties of your query object, and are passed from that. If this makes no sense, it will when you've read about those two modules.

Functions called indirectly

Let's start with some terminology. These are not (with one exception) functions that you are going to call directly. You are going to call some function in the data or query module, and it will itself call these ones, passing on any relevant arguments you supplied. I am going to refer to the function you actually called as "the parent function" throughout this page.

With that one exception (plotLightCurves()) these functions are not exported into the main namespace; you get at them through the parent functions, but the parent functions' docstrings don't detail all of the individual arguments that can get passed through (unless the parent also uses them). That's why I've written this page, but you can get at the docstrings if you wish, as below:

import swifttools.ukssdc.data as ud
help(ud.download._getLightCurve)

Note that the function name is _getLightCurve - it starts with _. For all functions shown in this section (except, again, plotLightCurves()) you can get at the help as above, prepending an underscore to the function name.


getLightCurve()

The getLightCurve() function is called in cases where you want to, well, get light curves. In all cases (I think) the parent function is getLightCurves() (i.e. almost the same, but in the plural).

This function can download the light curve files from the UKSSDC server and save them to disk and/or it can read the light curve data from the UKSSDC and store it in a light curve dict. When you call something which uses getLightCurve() you can set the following arguments which are passed through:

Note: nosys and incbad relate to specifics of the light curves the UKSSDC tools produce, to learn more, please see the light curve documentation.

(Small note: at the present time the xrt_prods.XRTProductRequest.retrieveLightCurve() function does not use this common function, because of the need for that module to support deprecated behaviour. This may change in future. The SXPS parts of the data and query modules also do not use this for very, very boring reasons, but their options are intentionally as similar as possible to those discussed above).


saveLightCurveFromDict()

Some functions let you save a light curve dict to files. This is slightly different from saving the light curve data directly from the URLs using getLightCurve(saveData=True) as above, and the usage and reasons for this will be covered in the different modules where we actually employ this functionality. For now, let's just deal with the function. In most cases this is called by parent functions called saveLightCurves().

Functions that call this can pass through the following arguments:


plotLightCurve()

This is the one function which you can call directly or indirectly. For those objects which can store light curves internally (XRTProductRequest, GRBQuery, SXPSQuery) a wrapper function is provided, for light curves you store in your own variables (e.g. those obtained via the swifttools.ukssdc.data module) you call the function directly. I have documented it in this section as it fits with the two functions above, I think.

To call the function directly you will first need to import it, e.g.

from swifttools.ukssdc import plotLightCurve

And its arguments are:

Note If you call this via one of the class functions (called plotLightCurve() everywhere except in XRTProductRequest, where it is plotLC() for backwards compatibility), the first, positional argument (lcDict) is removed or replaced; see the relevant class documentation for details.

This function uses matplotlib.pyplot and uses the subplots() function. This returns two arguments which in matplotlib conventions are stored as fig and ax, and are the outputs of subplots(). These are returned so that you can carry out subsequent manipulation, but you can also pass those objects into the call, if you want to plot on an existing canvas you have created, or (for example), plot multiple light curves on the same plot using this function - examples of this appear in various of the documentation pages herein.


Rebinning light curves

Both the swifttools.ukssdc.data.GRB and swifttools.ukssdc.xrt_prods modules give you the option to rebin a light curve. These use a series of common functions which are documented here; for a worked example see the data.GRB documentation.

There are a few functions, first the actual function to request rebinning, then functions to check on the status of the rebinning job and to get the products. These are all wrapped by functions of the same name in the module you are using, so as above I will just detail the arguments that you set, that get passed through to these functions, and anything they return.

One important note before I proceed though: in what follows below I have noted that the request to rebin a curve returns a job ID, and that you then have to pass this to the other functions. This is not true for the XRTProductRequest object. In that case, the ID is stored in your object, and passing it around is handled internally by the class wrapper functions to those detailed below.

rebinLightCurve()

rebinLightCurve() is the function which requests something be rebinned. There are many arguments that you can supply, but many of these depends upon how you want the light curve to be binned. This is controlled by the binMeth parameter, which is mandatory, and must be one of:

By a curious coincidence*, all of the other parmeters are identical to the parameters you would set in the xrt_prods module and so are documented already but I have copied that documentation below, rather than making you jump around too many pages.

One note before you read on though: this function rebinLightCurve() returns an integer number which you need to capture in a variable; this is the identifier for your rebin job, and is necessary for checking if the job is complete, and for getting the light curve.

(* It isn't coincidence.)

Arguments for all light curve types
Parameter Mandatory? Type Description Default
binMeth Yes str Which binning method to use. Must be one of {'counts', 'time', 'snapshot', 'obsid'} --
minEnergy No float/int Minimum energy for the main light curve, in keV 0.3
maxEnergy No float/int Maximum energy for the main light curve, in keV 10
softLo No float/int Minimum energy for the soft-band, in keV. Must be ≥minEnergy 0.3
softHi No float/int Maximum energy for the soft-band, in keV. Must be ≤maxEnergy 1.5
hardLo No float/int Minimum energy for the hard-band, in keV. Must be ≥minEnergy 1.5
hardHi No float/int Maximum energy for the hard-band, in keV. Must be ≤maxEnergy 10
minSig No float/int Minimum significance (in Gaussian σ) for a bin to be considered a detection 3
grades No str What event grades to include. Can be 'all', '0' or '4' 'all'
allowUL No str Whether upper limit are allowed. Must be one of {'no' 'pc', 'wt', 'both'}. 'both'
allowBayes No str Whether Bayesian bins are allowed. Must be one of {'no' 'pc', 'wt', 'both'}. 'both'
bayesCounts No float,int Threshold for counts in a bin, below which to use Bayesian statistics. 15
bayesSNR No float,int Threshold for S/N in a bin, below which to use Bayesian statistics to measure count rate and error. 2.4
timeFormat No str The units to use on the time axis. Must be one of {'s'(=seconds), 'm'(=MJD)} 's'
whichData No str Which observations to use for the light curve. Must be one of {'all', 'user'} 'all'
useObs If whichData='user' str The specific observations to use to create the light curve [Details] --
Arguments for ‘counts’ binning

‘Counts’ binning is where a bin requires a certain number of counts to be considered complete (unless it spans the maximum inter-observation gap). This is the binning method used by the XRT GRB light curve repository.

Parameter Mandatory? Type Description Default
pcCounts Yes int Minimum counts in a PC-mode bin for it to be full (at 1 ct/sec if dynamic binning is on) --
wtCounts Yes int Minimum counts in a WT-mode bin for it to be full (at 1 ct/sec if dynamic binning is on) --
dynamic Yes bool Whether dynamic binning is enabled or not --
rateFact No float,int Rate factor for dynamic binning 10.
binFact No float,int Binning factor for dynamic binning 1.5
pcBinTime No float,int Minimum bin duration in PC mode in seconds 2.51
wtBinTime No float,int Minimum bin duration in WT mode in seconds 0.5
minCounts No int The absolute minimum counts/bin that dynamic binning can't fall below, unless the maxgap parameters below force truncation 15
minSNR No float,int The minimum S/N a bin must have to be considered full, unless the maxgap parameters below force truncation 2.4
pcMaxGap No float,int The maximum observing gap a PC mode bin can straddle - even if not 'full' it will be stopped at this length (in seconds). 108
wtMaxGap No float,int The maximum observing gap a WT mode bin can straddle - even if not 'full' it will be stopped at this length (in seconds). 108
Arguments for fixed-time binning
Parameter Mandatory? Type Description Default
pcBinTime No float,int Bin duration in PC mode in seconds 2.51
wtBinTime No float,int Bin duration in WT mode in seconds 0.5
matchHR No bool Whether the hardness ratio should have the same bins as the main curve. True
pcHRBinTime No float,int Hardness ratio bin duration in PC mode in seconds 2.51
wtHRBinTime No float,int Hardness ratio bin duration in WT mode in seconds 0.5
minFracExp No float,int Minimum fractional exposure a bin can have, bins with a lower fractional exposure are discarded. 0.

The other binning methods have no specific arguments.

checkRebinStatus()

The checkRebinStatus function, well, checks on the status of your rebinning request. If takes a single argument, which is the jobID, the value returned by rebinLightCurve().

It returns a dict with two keys. statusCode which is a numerical value and statusText which is a text description of the status. The codes are:

rebinComplete()

This function simply tells you whether your rebin job is complete. If takes a single argument, which is the jobID, the value retured by rebinLightCurve(), and returns a bool (True meaning, "Yes, it's complete").

getRebinnedLightCurve()

This function again just takes a jobID, and literally all it does it then call getLightCurve(), so I will not repeat the documentation for that here, you can scroll up.


saveSpectrum()

The ability to save spectral data to disk appears in almost every area of the swifttools.ukssdc module, and in each case it is powered behind the scenes by the same function, described here. There are two things that can be saved to disk: gif images of the spectrum and fit, and .tar.gz archives containing the spectral files. The common arguments passed through to this common function are:


Functions you call

There are three functions (plus plotLightCurve(), above) which are designed to be called directly by you, and no wrappers are supplied. These are all related to combining light curve data.

The first of these, mergeLightCurveBins() is for use with a light curve dict and can be applied to datasets containing count-rates or upper limits.

The second, mergeUpperLimits() is used for combining upper limits not in a light curve dict, but as returned by the SXPS upper limit tool.

The third, bayesRate() is a helper function used by the above, but may be of occasional use to you as well.

All of these are in the swifttools.ukssdc module and I recommend using them like this:

import swifttools.ukssdc as uk

uk.mergeLightCurveBins(...)

etc.

mergeLightCurveBins()

This function is used to combine bins within a single "dataset" within a light curve dict; for example, you may wish to sum up all upper limits to give the deepest possible limit, or combine some bins where the source is faint to give smaller errors etc.

Note At the present time, this function only allows combining bins within the same "dataset". That is, if you want to combine some bins from the set of upper limits with some bins from the set of detections you will -- at present -- have to do this yourself.

This function works by summing up the number of counts in the identified bins and the number of expected background counts, and then using the bayesRate() function to determine the number of expected counts (and confidence interval) using the method of Kraft, Burrows & Nousek (1991). The exposure is also summed across the bins, and the (exposure-weighted) mean correction factor is found, and the count-rate and confidence interval thus found.

It should be noted that combining bins can change their "type", in particular, combining upper limits can result in a detection. The mergeLightCurveBins() function lets you force the result to be an upper limit, or a count-rate (with 1-sigma errors), or to determine its type automatically. You can also choose whether the result should be added into the light curve supplied or not, and whether the bins that have been merged should be removed from the light curve.

mergeLightCurveBins() takes the following arguments:

Note on insert A new, merged bin can only be inserted into the light curve if it is the same type (count-rate with errors, or upper limit) as the rest of the light curve. Accordingly, if you set insert=True this will force the merged bin to be of the same type as the supplied light curve, ignoring the forceRate and forceUL arguments (if set). insert='match' is more equivocal: the function will test to see if the merged bin constitutes a detection, it will then only be inserted into the light curve if its type matches that of the light curve. You can determine the upshot of this from the function's return.

Return data The function returns a tuple with three entries: (isUL, inserted, newData)

Quick demonstration of rows If you don't want to have to read another page to see how to define a subset of rows to filter, then here's an example of me asking to only merge the bins in a specified time range:

ul = lcData['PCUL']  # Makes the next lines more readable
res = uk.mergeLightCurveBins(ul,
                             rows=(ul['Time']>59770)&(ul['Time']>59790) )

mergeUpperLimits()

mergeUpperLimits() is analogous to the above mergeLightCurveBins() function, except that it works on the set of SXPS upper limits produced by swifttools.ukssdc.data.SXPS.getUpperLimits(). It returns a dict of results, and does not have the match or remove options.

The return value is a dict with the keys: UpperLimit, Counts, BGCounts, CorrectionFactor, Rate, RatePos, RateNeg, IsDetected for each band requested.

bayesRate()

bayesRate is an implementation of the Kraft, Burrows & Nousek (1991) method for calculating confidence intervals.

This receives three positional arguments:

The return is a tuple of (min, max, mean) number of source counts.