aztools.misc

Miscellaneous Utilities

Functions

split_array(arr, length, *args[, strict])

Split an array arr to segments of length length.

group_array(arr[, by_n, bins])

Group elements of array arr given one of the criterion

write_pha_spec(bin1, bin2, arr, err, stem, **kwargs)

Write some data array to pha file so it can be

write_2d_veusz(fname, arr[, xcent, ycent, append])

Write a 2d array to a file for veusz viewing

set_fancy_plot()

Some settings for plt that make nicer plots

sync_lcurve(lc_list[, tbase])

Synchronize a list of arrays or LCurves

lcurve_to_segments(lcurves, seglen[, strict])

Split an LCurve or a list of them to segments.

read_fits_lcurve(fits_file, **kwargs)

Read a light cuurve from fits file

run_cmd_line_tool(cmd[, env, allow_fail, logfile])

Run a command line tool

add_spectra(speclist, outfile, **kwargs)

Call addspec to combine spectra.

parallelize(func[, use_irun])

A wrapper to make a function run in parallel

Module Contents

split_array(arr: numpy.ndarray, length: int, *args, strict: bool = False, **kwargs)[source]

Split an array arr to segments of length length.

Parameters:
  • arr (np.ndarray) – The array to be split.

  • length (int) – The desired length of the segments

  • strict (bool) – If True, force all segments to have length length. Some data may be discarded

  • split (any other arrays similar to arr to be)

Keywords

overlap: int

number < length of overlap between segments.

split_at_gaps: bool

Split at non-finite values. Default True.

min_seg_length: int.

Minimum seg length to keep. Used when strict=False

approx: int

length is used as an approximation.

returns:

A tuple with

rtype:

(result, indx, …{other arrays if given})

group_array(arr: numpy.ndarray, by_n: numpy.ndarray | list = None, bins: numpy.ndarray | list = None, **kwargs)[source]

Group elements of array arr given one of the criterion

Parameters:
  • arr (np.ndarray) – The array to be grouped. Assumed 1d.

  • by_n (np.ndarray or list) – [nstart, nfac] creates a new groupd at [nstart, nstart*nfac, nstart*nfac^2 …]

  • bins (np.ndarray or list) – A list of bin boundaries. Values outside these bins are discarded

  • Keywords

  • --------

  • do_unique (bool) – if True, the groupping is done for the unique values.

  • min_per_bin (int) – The minimum number of elements per bin.

write_pha_spec(bin1: list | numpy.ndarray, bin2: list | numpy.ndarray, arr: numpy.ndarray, err: numpy.ndarray, stem: str, **kwargs)[source]

Write some data array to pha file so it can be used inside xspec

Parameters:
  • bin1 (list of np.ndarray.) – The lower boundaries of bins

  • bin2 (list of np.ndarray.) – The upper boundaries of bins

  • arr (np.ndarray) – The array of data values to be written.

  • err (np.ndarray) – The array of measurement error corresponding to arr.

  • Keywords

  • --------

  • stem (str:) – Stem name for the output spectra -> {stem}.pha|rsp

write_2d_veusz(fname: str, arr: numpy.ndarray, xcent: numpy.ndarray = None, ycent: numpy.ndarray = None, append: bool = False)[source]

Write a 2d array to a file for veusz viewing

Parameters:
  • fname (str) – The name of file to write.

  • arr (np.ndarray) – The array to write, its shape is (len(xcent), len(ycent))

  • xcent (np.ndarray:) – Central points of X-axis.

  • ycent (np.ndarray:) – Central points of Y-axis.

  • append (bool) – Append to file? Default=False

set_fancy_plot()[source]

Some settings for plt that make nicer plots

sync_lcurve(lc_list: list | numpy.ndarray, tbase: numpy.ndarray = None)[source]

Synchronize a list of arrays or LCurves

Parameters:
  • lc_list (list) – A list of arrays or a list of LCurve objects. if arrays, the shape is (nlcurve, 3 (or 4 with fexp), ntime). The 3 is for (time, rate, rerr)

  • tbase (np.ndarray) – The time array to use for reference. If not given, use the intersection of all time arrays

Return type:

a list of sync’ed arrays/light curves

lcurve_to_segments(lcurves, seglen: float, strict: bool = False, **kwargs)[source]

Split an LCurve or a list of them to segments. Useful to be used with calculate_psd|lag etc.

Parameters:
  • lcurves (LCurve) – an LCurve or a list of them

  • seglen (float) – segment length in seconds.

  • strict (bool) – force all segments to have length length. Some data may be discarded

  • Keywords

  • --------

  • uneven (The light curves are uneven, so the splitting produces) – segments that have the same number of points. Default: False

  • split_array (**other arguments to be passed to)

Returns:

  • rate, rerr, time, seg_idx

  • seg_idx is the indices used to create the segments.

read_fits_lcurve(fits_file: str, **kwargs)[source]

Read a light cuurve from fits file

Parameters:
  • fits_file (str) – Name of the fits file.

  • Keywords

  • --------

  • min_exp (float) – minimum fractional exposure to allow. Default 0.0 for all

  • rate_tbl (str or int) – Name or number of hdu that contains the light curve data. Default: RATE

  • rate_col (str or int) – Name or number of rate column. Default: RATE

  • time_col (str or int) – Name or number of time column. Default: TIME

  • rerr_col (str or int) – Name or number of rerr column. Default: ERROR

  • fexp_col (str or int) – Name or number of the fracexp column. Default: FRACEXP

  • gti_table (str or int) – Name or number of gti extension hdu. Default: GTI

  • dt_key (str or int) – Name of time sampling keyword in header. Default: TIMEDEL

  • gti_skip (float) – How many seconds to skip at the gti boundaries. Default: 0

  • verbose (bool) – Print progress

Returns:

lcurve_data (shape

Return type:

4,nt containing, time, rate, rerr, fexp), deltat

run_cmd_line_tool(cmd: str, env: dict = None, allow_fail: bool = True, logfile: str = None)[source]

Run a command line tool

Parameters:
  • cmd (str) – The command string to be run where the parameters are in the string

  • env (dict) – Dictionary of environment variables to be used by the task

  • allow_fail (bool) – If True and the task fails, return without raising an exception

  • logfile (str) – File name to long output/error to. If None, no logs are produced

add_spectra(speclist: list, outfile: str, **kwargs)[source]

Call addspec to combine spectra.

do it multiple times if num of spectra above kwargs[‘nmax’]

Parameters:
  • speclist (list) – list of the names of the spectral files to be added

  • outfile (str) – output name root

  • Keywords

  • ---------

  • nmax (int) – add a maximum of nmax spectra at a time

  • (qaddrmf (other parameters for addspec)

  • qsubback

  • clobber)

parallelize(func, use_irun=True)[source]

A wrapper to make a function run in parallel

Parameters:
  • func (method) – The method to parallelize. It can have any args or kwargs.

  • use_irun (bool) – If True, pass a keyword argument irun as int to func that holds the call number in the sequence of parallel calls. See description of the returned function below

Returns:

  • return a method with parameters that are lists of args/kwargs

  • to be passed to func. The returned method takes these special kwargs