Package pylal :: Module followup_utils
[hide private]
[frames] | no frames]

Source Code for Module pylal.followup_utils

  1  #!/usr/bin/env python 
  2  # 
  3  # Copyright (C) 2009 Cristina Valeria Torres 
  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 2 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  __author__ = 'Cristina Valeria Torres <cristina.torres@ligo.org>' 
 20   
 21  import sys 
 22  import numpy 
 23  import os, shutil 
 24  import urllib 
 25  import sqlite3 
 26   
 27  from subprocess import * 
 28  import copy 
 29  import re 
 30  from StringIO import StringIO 
 31  import glob 
 32  import fileinput 
 33  import linecache 
 34  import string 
 35  import random 
 36  import numpy 
 37  import cPickle 
 38  import gzip 
 39  from scipy import interpolate 
 40  from commands import getstatusoutput 
 41  import math 
 42  import fnmatch 
 43  from optparse import * 
 44  from types import * 
 45  import matplotlib 
 46  matplotlib.use('Agg') 
 47  import operator 
 48  from UserDict import UserDict 
 49   
 50  from glue import segments 
 51  from glue import segmentsUtils 
 52  from glue.ligolw import ligolw 
 53  from glue.ligolw import table 
 54  from glue.ligolw import lsctables 
 55  from glue.ligolw import utils 
 56  from glue.ligolw import dbtables 
 57  from pylal import CoincInspiralUtils 
 58  from pylal import frutils 
 59  from glue import iterutils 
 60  from glue import pipeline 
 61  from glue.lal import * 
 62  from glue import lal 
 63  from glue import markup 
 64  from lalapps import inspiralutils 
 65  from glue.segmentdb import segmentdb_utils 
 66  from glue.segmentdb import query_engine 
 67  from pylal.xlal import date as xlaldate 
 68  #Part of bandaid 
 69  from xml import sax 
 70  from pylal import db_thinca_rings 
 71  from pylal import git_version 
 72  from pylal.xlal.datatypes.ligotimegps import LIGOTimeGPS 
 73  from StringIO import StringIO 
 74   
 75  ##################################################### 
 76  ## Function related to handling and processing of  ## 
 77  ## frame data manipulation as part of followup     ## 
 78  ## procedures                                      ## 
 79  ##################################################### 
 80   
81 -class connectToFrameData:
82 """ 83 A class that uses classes defined in pylal.frutils. 84 This handles searching for data via ligo_data_find 85 and creating an object which can fetch specific 86 channels from that data epoch given a frame type. 87 """
88 - def __init__(self,\ 89 gpsStart=None,\ 90 gpsEnd=None,\ 91 frameType="R",\ 92 observatory=None,\ 93 urlType="local",\ 94 noGaps=True,\ 95 verbose=False):
96 self.__emptyquery__="""ligo_data_find --gaps \ 97 --type=%s --observatory=%s --gps-start-time=%s \ 98 --gps-end-time=%s --url-type=%s --lal-cache --no-proxy""" 99 if gpsStart == None or gpsEnd == None: 100 print "Error with gps arguments given are NoneType!" 101 self.globalStart=int(numpy.floor(gpsStart)) 102 self.globalEnd=int(numpy.ceil(gpsEnd)) 103 self.observatory=observatory 104 self.frameType=frameType 105 self.urlType=urlType 106 self.query=self.__emptyquery__%(self.frameType,\ 107 self.observatory,\ 108 self.globalStart,\ 109 self.globalEnd,\ 110 self.urlType) 111 # 112 # Create a connection to the data 113 # 114 (errorCode,cmdOutput)=getstatusoutput(self.query) 115 self.dataStream=None 116 if errorCode != 0: 117 sys.stderr.write("%s\n"%cmdOutput) 118 raise Exception, "Error access ligo_data_find" 119 memFP=StringIO(cmdOutput) 120 self.dataStream=frutils.FrameCache(lal.Cache.fromfile(memFP)) 121 memFP.close()
122
123 - def getDataStream(self,\ 124 channel=None,\ 125 gpsStart=None,\ 126 gpsEnd=None,\ 127 verbose=None):
128 """ 129 Fetch data inside the bounds specified during 130 the init call to this class. If gpsStart and gpsEnd 131 are None then we fetch data around gpsZero with 132 a window of pm gpsWindow. 133 """ 134 if gpsStart == None: 135 gpsStart=self.globalStart 136 if gpsEnd == None: 137 gpsEnd=self.globalEnd 138 gpsStart=int(numpy.floor(gpsStart)) 139 gpsEnd=int(numpy.ceil(gpsEnd)) 140 if channel == None: 141 raise Exception, "Channel name passed to method as None" 142 return self.dataStream.fetch(channel,gpsStart,gpsEnd)
143
144 - def saveAsTextDataStream(self,\ 145 channel=None,\ 146 gpsStart=None,\ 147 gpsEnd=None,\ 148 filename=None, 149 verbose=None):
150 """ 151 Write the data from the stream to an ascii text file 152 """ 153 if gpsStart==None: 154 gpsStart=self.globalStart 155 if gpsEnd==None: 156 gpsEnd=self.globalEnd 157 if filename==None: 158 filename="default_data_file.txt" 159 if channel==None: 160 raise Exception, "No channel name given to method." 161 tmpData=self.getDataStream(channel,gpsStart,gpsEnd) 162 tmpDataDict=tmpData.metadata.todict() 163 myFP=file(filename,'w') 164 for myKey,myVal in tmpMetaData.iteritems(): 165 myFP.write("#%s:%s\n"%(myKey,myVal)) 166 myFP.write("#t0:%s\n"%(gpsStart)) 167 for datum in tmpData.tolist(): 168 myFP.write("%s\n"%datum) 169 myFP.close()
170
171 - def convertFrObjectToXYLists(self,dataStream):
172 """ 173 Returns two lists as (tStamps,dataPoints) the method 174 expects you to give is an object FrameCache from 175 frutils.FrameCache(xxxx).fetch() or use this getDataStream() 176 method to get a variable of this type. 177 """ 178 metaDataDict=dataStream.metadata.todict() 179 dT=float(metaDataDict["dt"]) 180 segmentInfo=metaDataDict["segments"] 181 dataPoints=dataStream.tolist() 182 myStart=segmentInfo[0][0] 183 myStop=myStart+len(dataPoints)*dT 184 tStamps=arange(myStart,myStop,dT) 185 return (tStamps,dataPoints)
186