Source code for batse5bp.ascii64

"""
Objects providing access to 64 ms count data as compiled in the ASCII data
files at the COSSC.

These files compile DISCLA, PREB, and DISCSC data over an interval determined by
examination of trigger and pre-trigger data by the BATSE team.

NOTE:  The DISCLA data is in 1024 ms bins; the counts are divided by 16 to
distribute across the tabulated 64 ms bins.  The DISCLA bins can overlap with
the 1st PREB bin, in which case the PREB overlap is subtracted and the remaining
counts divided among earlier bins.

For full documentation, see:

ftp://legacy.gsfc.nasa.gov/compton/data/batse/ascii_data/64ms/README

Created 2012-10-22 by Tom Loredo
"""

from os.path import join, exists
import cPickle
import urllib

import numpy as np
from numpy import loadtxt, empty, linspace, array

from locations import root, raw_cache


# TODO:  Should probably fetch and cache the compressed group directories
# to save bandwidth and speed access of large parts of the catalog
# (perhaps optionally).


[docs]class ASCII64: """ Provide access to the 64 ms count data compiled in the ASCII data files hosted by the COSSC. """ def __init__(self, grb): """ Load ASCII 64 ms count data for the GRB instance `grb`. If the data is not already locally cached, it is fetched from the SSC, loaded, and cached for future use. Otherwise, the cached copy is loaded. """ self.grb = grb a64_path = join(grb.grb_dir, grb.a64_fname) if exists(a64_path): ifile = open(a64_path, 'rb') data = cPickle.load(ifile) ifile.close() else: a64_raw = join(root, raw_cache, grb.a64_fname) if not exists(a64_raw): urllib.urlretrieve(grb.a64_remote, a64_raw) dfile = open(a64_raw, 'r') dfile.readline() # header meta = dfile.readline().strip().split() try: assert int(meta[0]) == grb.trigger except: raise ValueError('Trigger \# mismatch for ASCII64 data!') self.n_asc64 = int(meta[1]) self.n_discla = int(meta[2]) self.olap = int(meta[3]) # Data are indexed as [ch, time]. raw = loadtxt(dfile, unpack=True) self.raw = raw # Break up the data into the (early) DISCLA bins, the (late) 64 ms # bins (PREB + DISCSC), and a possible (mid) truncated DISCLA bin. self.n_early = self.n_discla / 16 early = empty((4, self.n_early), int) for ch in range(0,4): for i in xrange(self.n_early): j = 16 * i early[ch,i] = int(round(16*raw[ch,j])) self.early = early n_trail = self.n_discla - 16*self.n_early if n_trail == 0: self.n_mid = 0 mid = None else: self.n_mid = 1 mid = empty((4,1), int) # must be 4x1 for concat with others j = self.n_early*16 for ch in range(0,4): mid[ch] = int(round(n_trail*raw[ch,j])) self.mid = mid self.n_late = self.n_asc64 - self.n_discla late = empty((4,self.n_late), int) for ch in range(4): for j in xrange(self.n_late): late[ch,j] = int(raw[ch,self.n_discla+j]) self.late = late self.n_bins = self.n_early + self.n_mid + self.n_late # Create vectors of bin time interval boundaries. self.t_early = linspace(0, 1.024*self.n_early, self.n_early+1) last = self.t_early[-1] if self.n_mid == 0: self.t_mid = array([last, last]) else: self.t_mid = array([last, last+n_trail*0.064]) last = self.t_mid[-1] self.t_late = linspace(last, last + self.n_late*0.064, self.n_late+1) # Shift the times to be relative to the trigger time, which is # 2.048 s after the start of the PREB data (the first 64 ms data). t_trig = self.t_late[0] + 2.048 self.t_early = self.t_early - t_trig self.t_mid = self.t_mid - t_trig self.t_late = self.t_late - t_trig # Create a concatenated set of counts and times. if self.n_mid == 0: self.counts = np.concatenate((self.early, self.late), axis=1) self.times = np.append(self.t_early, self.t_late[1:]) else: self.counts = np.concatenate((self.early, self.mid, self.late), axis=1) self.times = np.append(self.t_early, self.t_late)
[docs] def rates(self): """ Return time bin centers & widths (vectors) & count rates (4-row matrix). These values are useful for plotting. """ centers = 0.5*(self.times[:-1] + self.times[1:]) widths = self.times[1:] - self.times[:-1] rates = self.counts / widths return centers, widths, rates