Skip to content

Commit

Permalink
Add gcp.ARCFile class analogous to core.G3File (#74)
Browse files Browse the repository at this point in the history
Optional `extract` keyword argument to also run `gcp.ARCExtract` on each frame.

Also, add a `core.InjectFrame` utility function for injecting arbitrary frames into a pipeline, used with `ARCFile(..., extract=True)`.
  • Loading branch information
arahlin authored Mar 3, 2022
1 parent a75e311 commit 6f6c05c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions gcp/python/ARCFile.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions gcp/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .ARCHKExtractor import UnpackSPTpolHKData
from .GCPDataTee import GCPHousekeepingTee, GCPSignalledHousekeeping, GCPBoloDataTee, PagerWatchdog, DAQWatchdog
from .InfluxDB import UpdateDB
from .ARCFile import ARCFile

0 comments on commit 6f6c05c

Please sign in to comment.