diff --git a/core/python/util.py b/core/python/util.py index a8f2047b..5127ed75 100644 --- a/core/python/util.py +++ b/core/python/util.py @@ -39,6 +39,26 @@ def Dump(frame, type=None, added_message = None): print(frame) +@indexmod +class InjectFrame(object): + """ + Inject an arbitrary frame into a pipeline. + + Arguments + --------- + frame : G3Frame + The frame to inject + """ + def __init__(self, frame): + self.frame = frame + def __call__(self, frame): + if self.frame is None: + return + out = [self.frame, frame] + self.frame = None + return out + + @indexmod def InjectDebug(frame, type=None, debug_start_func = None): '''starts a pdb session when a frame of type shows up. diff --git a/gcp/python/ARCFile.py b/gcp/python/ARCFile.py new file mode 100644 index 00000000..8705bc19 --- /dev/null +++ b/gcp/python/ARCFile.py @@ -0,0 +1,39 @@ +from spt3g.gcp import ARCFileReader, ARCExtract + +class ARCFile(object): + '''Iterable class for ARC files, as created by GCP. Loop through frames by doing something like: + f = gcp.ARCFile('/path/to/arc.dat') + for frame in f: + print( frame ) + + An entire file can also be read into an indexable list by doing: + f = list(gcp.ARCFile('/path/to/arc.dat')) + ''' + def __init__(self, path, extract=False): + self.reader = ARCFileReader(path) + self.extract = extract + + def __iter__(self): + return self + + def next(self): + frames = self.reader.Process(None) + if len(frames) == 0: + raise StopIteration('No more frames in file') + if len(frames) > 1: + raise ValueError('Too many frames returned by reader') + frame = frames[0] + + if self.extract: + # calibrate and parse arc frames + from spt3g.core import G3Pipeline, G3InfiniteSource, InjectFrame + + pipe = G3Pipeline() + pipe.Add(G3InfiniteSource, n=1) + pipe.Add(InjectFrame, frame=frame) + pipe.Add(ARCExtract) + pipe.Run() + + return frame + + __next__ = next diff --git a/gcp/python/__init__.py b/gcp/python/__init__.py index 9e254acb..caa02500 100644 --- a/gcp/python/__init__.py +++ b/gcp/python/__init__.py @@ -5,3 +5,4 @@ from .ARCHKExtractor import UnpackSPTpolHKData from .GCPDataTee import GCPHousekeepingTee, GCPSignalledHousekeeping, GCPBoloDataTee, PagerWatchdog, DAQWatchdog from .InfluxDB import UpdateDB +from .ARCFile import ARCFile