Package pylal :: Package dq :: Module stateutils
[hide private]
[frames] | no frames]

Source Code for Module pylal.dq.stateutils

 1  #!/usr/bin/env python 
 2   
 3  # Copyright (C) 2011 Duncan Macleod 
 4  # 
 5  # This program is free software; you can redistribute it and/or modify it 
 6  # under the terms of the GNU General Public License as published by the 
 7  # Free Software Foundation; either version 3 of the License, or (at your 
 8  # option) any later version. 
 9  # 
10  # This program is distributed in the hope that it will be useful, but 
11  # WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
13  # Public License for more details. 
14  # 
15  # You should have received a copy of the GNU General Public License along 
16  # with this program; if not, write to the Free Software Foundation, Inc., 
17  # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
18   
19  """ 
20  This module provides some utilities for manipulating data from a bit-mask state vector channel. 
21  """ 
22   
23  from numpy import arange 
24  from glue import segments 
25   
26  # ============================================================================= 
27  # Convert a data quality bit mask into segments 
28  # ============================================================================= 
29   
30 -def _bits(i, n=8):
31 """ 32 Convert integer bit mask into binary bits. Returns a list of 0s or 1s 33 from 2^0 up to 2^n. 34 35 Example: 36 37 >>> _bits(295, n=8) 38 [1, 1, 1, 0, 0, 1, 0, 0] 39 """ 40 return [(0, 1)[int(i)>>j & 1] for j in xrange(n)]
41
42 -def tosegmentlistdict(timeseries, bitmask):
43 """ 44 Returns a glue.segments.segmentlistdict of active segments for each bit 45 in a bit-masked state vector TimeSeries. 46 """ 47 48 bits,flags = zip(*sorted(bitmask.items(), key=lambda (k,v): k)) 49 50 segdict = segments.segmentlistdict() 51 for flag in flags: 52 segdict[flag] = segments.segmentlist() 53 54 # convert DQ bits into segments 55 tarray = arange(timeseries.data.length) * float(timeseries.deltaT) +\ 56 float(timeseries.epoch) 57 for t,d in zip(tarray.astype(float), timeseries.data.data): 58 binary = _bits(d, bits[-1]+1) 59 seg = segments.segment(t, t-timeseries.deltaT) 60 for bit,flag in zip(bits, flags): 61 if binary[bit] == 1: 62 segdict[flag].append(seg) 63 64 segdict.coalesce() 65 return segdict
66