Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

BTdump.py

00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2003
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "GLAST LAT Data Format (LDF) data dumper functions for Beam Test"
00012 __author__   = "R. Claus <Claus@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "6/17/2006"
00014 __version__  = "$Revision: 1.1 $"
00015 __credits__  = "SLAC"
00016 
00017 import copyright_SLAC
00018 
00019 import os, stat, struct
00020 
00021 import LDF
00022 
00023 import LDFdumper as LD
00024 from   LDFdumper import _u
00025 
00026 from   LDFdump               import myLATcomponentIterator
00027 from   ancillaryContribution import AncillaryContribution
00028 
00029 import sys
00030 
00031 
00032 def _testDg(dgBuf):
00033   """Iterate over a buffer containing a list of LATdatagrams.
00034   """
00035   class myEBFeventIterator(LDF.LDBI_EBFeventIterator):
00036     def __init__(self, lci):
00037       LDF.LDBI_EBFeventIterator.__init__(self, lci)
00038       self.__lci = lci
00039 
00040     def process(self, evt):
00041       LD.EBFevent(evt).dump()
00042       if evt.status() != 0:
00043         print
00044         print "Bad status %d in event header" % (evt.status())
00045         print "Aborting processing of event"
00046         print
00047         return evt.status()
00048       else:
00049         self.__lci.iterate(evt)
00050         return self.__lci.status()
00051 
00052     def handleError(self, event, code, p1, p2):
00053       if code == LDF.EBFeventIterator.ERR_NonEBFevent:
00054         print >> sys.stderr, "EBFeventIterator::iterate: ", \
00055                             "Encountered a non-EBFevent datagram contribution ", \
00056                             "with typeId %08x\n" % (p1)
00057         return 1
00058       else:
00059         print >> sys.stderr, "myEBFeventIterator.handleError got unknown error code:", \
00060                              "  code=%d, p1=%d, p2=%d\n" % ( code, p1, p2)
00061       return 0
00062 
00063   class myASCtileContributionIterator(LD.ASCtileContributionIteratorDump):
00064     def __init__(self):
00065       LD.ASCtileContributionIteratorDump.__init__(self)
00066 
00067   class myASCcontributionIterator(LD.ASCcontributionIteratorDump):
00068     def __init__(self):
00069       LD.ASCcontributionIteratorDump.__init__(self)
00070 
00071   class myLATcontributionIterator(LDF.LDBI_LATcontributionIterator):
00072     __typeIds = [AncillaryContribution.EventId,
00073                  AncillaryContribution.HeaderId,
00074                  AncillaryContribution.TrailerId]
00075     def __init__(self, eei):
00076       LDF.LDBI_LATcontributionIterator.__init__(self, eei)
00077 
00078     def handleError(self, contribution, code, p1, p2):
00079       if code == LDF.LATcontributionIterator.ERR_UDFcontribution:
00080         typeId = _u(p1)
00081         if typeId & 0x000fffff in myLATcontributionIterator.__typeIds:
00082           import Numeric
00083           buffer = Numeric.fromstring(contribution.string(0), 'i')
00084           i      = 0
00085           prefix = "  "
00086           print "ANC:"
00087           print "%s  %08x, %08x, %08x, %08x" % (prefix,
00088                                                 _u(buffer[0  ]),_u(buffer[i+1]),
00089                                                 _u(buffer[i+2]),_u(buffer[i+3]))
00090           print
00091           return 0
00092         else:
00093           print >> sys.stderr, "LATcontributionIterator::UDF: ", \
00094                                "Found unrecognized LATdatagram contribution type 0x%08X" % \
00095                                (typeId)
00096           return -1
00097       else:
00098         print >> sys.stderr, "myLATcontributionIterator.handleError got unknown error code:", \
00099                              "  code=%d, p1=%d, p2=%d\n" % ( code, p1, p2)
00100       return 0
00101 
00102     def ASC(self, start, end):
00103       print ""
00104       print "ASC"
00105       asci = myASCcontributionIterator()
00106       asci.iterate(start)
00107       return asci.status()
00108 
00109   class myLATdatagramIterator(LDF.LDBI_LATdatagramIterator):
00110     def __init__(self, lci):
00111       LDF.LDBI_LATdatagramIterator.__init__(self, lci)
00112       self.__lci = lci
00113 
00114     def handleError(self, contribution, code, p1, p2):
00115       if code == LDF.LATdatagramIterator.ERR_IDmismatch:
00116         print >> sys.stderr, "LATdatagramIterator.iterate: ", \
00117                              "Identity mismatch: got %08x, expected %08x\n" % \
00118                             (p1, p2)
00119         return -1
00120       else:
00121         print >> sys.stderr, "myLATcontributionIterator.handleError got unknown error code:", \
00122                              "  code=%d, p1=%d, p2=%d\n" % ( code, p1, p2)
00123       return 0
00124 
00125     def process(self, dg):
00126       LD.LATdatagram(dg).dump()
00127 
00128       self.__lci.iterate(dg)      # Returns the number of objects iterated over
00129       status = self.__lci.status()
00130       if status:  print "Datagram iteration returned status 0x%08x = %d" % \
00131          (_u(status), _u(status))
00132 
00133       return 0
00134 
00135   latCompIt    = myLATcomponentIterator()
00136   ebfEvtIt     = myEBFeventIterator(latCompIt)
00137   ascContIt    = myASCcontributionIterator()
00138   latContIt    = myLATcontributionIterator(ebfEvtIt)
00139   latDgIt      = myLATdatagramIterator(latContIt)
00140   latDataBufIt = LDF.LATdataBufferIterator(latDgIt)
00141 
00142   latDataBufIt.iterate(dgBuf, len(dgBuf)) # Returns # of objects iterated over
00143   return latDgIt.status()
00144 
00145 def testDG(dg):
00146   _testDg(dg)
00147 
00148 def testRaw(filename="muon-5.arch", skip=0, saveFile=None):
00149   """This function is meant for use with datagram encapsulated EBF event files.
00150   It assumes the first contribution to the datagram is an EBF event and ignores
00151   any subsequent contributions.  An EBF event as defined here contains an
00152   identification value in its first longword and is followed by the raw EBF
00153   event format as described in the TEM, AEM, GEM, etc. documents.
00154   """
00155   ebfFile = open(filename, 'rb')
00156   fileLen = os.stat(filename)[stat.ST_SIZE]
00157   if saveFile is not None:  saveFile = open(saveFile, 'wb')
00158   n = 0
00159 
00160   while fileLen > 0:
00161     try:
00162       data               = ebfFile.read(8)
00163       if len(data) < 8:  break
00164       (identity, length) = struct.unpack('!LL', data)
00165       data              += ebfFile.read(length - 8)
00166 
00167       if n == skip:
00168         if saveFile is not None:  saveFile.write(data)
00169         print
00170         status = _testDg(data)
00171         if status > 0: break
00172         if status < 0:
00173           skip = -status
00174           n    = 0
00175       else:
00176         if n % 30000 == 0:
00177           print "\n%7d" % (n),
00178         else:
00179           if n % 1000 == 0:
00180             print ".",
00181         n += 1
00182 
00183       fileLen -= length
00184     except KeyboardInterrupt:
00185       n = skip
00186     except MemoryError, e:
00187       print "MemoryError exception: ", e
00188       print "Length = 0x%08x" % length
00189 
00190   if saveFile is not None:  saveFile.close()
00191   ebfFile.close()
00192 
00193 
00194 if __name__ == '__main__':
00195   import sys
00196 
00197   def usage():
00198     print "%s <filename>" % sys.argv[0]
00199     print
00200     print "  runs testRaw(<filename>)"
00201     print
00202     print "Other functions are available by running with python -i:"
00203     print "  test(<string buffer>)"
00204     print "  testEBF(<filename>, <identity>)"
00205     print "  testRaw(<filename>, <skip count>, <save filename>"
00206     print "  testFITS(<filename>)"
00207     print "  testFITS_Dist(<filename>, <multicast address>)"
00208     print "  testRaw_Dist(<server>)"
00209     print "  testHex(<filename>)"
00210 
00211 
00212   if len(sys.argv) == 1:
00213     usage()
00214   else:
00215     testRaw(sys.argv[1])

Generated on Fri Jul 21 13:26:27 2006 for LATTE R04-12-00 by doxygen 1.4.3