示例#1
0
class LogWriter(object):
    """ This provides a writer to the SOFIE HDF FORMAT system. Use with caution, Pytables
    does not support concurrent writes to an HDF file. (Currently not used in the ExperimentControl Package.)"""

    previousParsedValue = None

    def __init__(self, filename='', runName='test'):
        self.is_open = False
        self.open(filename, runName)

    def __del__(self):
        if self.is_open:
            self.fd.close()
            self.fdP.close()

    def open(self, filename='', runName='test'):
        if filename == '':
            filename = datetime.datetime.now().isoformat() + '.h5'

        if self.is_open == True:
            self.close()

        self.fd = AntRawDataAccess(filename, runName + '/' + SPARKSUBDIRECTORY)
        self.fdP = AntParsedDataAccess(filename,
                                       runName + '/' + SPARKSUBDIRECTORY)

        self.is_open = True
        #TODO write meta information...

    def close(self):
        if self.is_open:
            self.fd.close()
            self.fdP.close()
            self.is_open = False

    def _logEvent(self, event, data=None):
        timestamp = time.time()
        if event == EVENT_READ and len(data) > 0:
            #logging.debug(len(data))
            information = {'packet': data, 'timestamp': timestamp}
            try:
                #logging.debug(information)
                informationRaw = ant_sample(information)
                if informationRaw == []:
                    logging.debug('NOT A BROADCAST')
                    return
                logging.debug('Information Raw:' + str(informationRaw))
                informationParsed = ant_sample_interpret(informationRaw,\
                    self.previousParsedValue)
                self.previousParsedValue = informationParsed
                self.fd.write(informationRaw)
                self.fdP.write(informationParsed)
            except exceptions.UnknownParseError, e:
                pass
            except exceptions.ChecksumParseError, e:
                logging.debug('CHECKSUM ERROR:' + e.message)
class LogWriter(object):
    """ This provides a writer to the SOFIE HDF FORMAT system. Use with caution, Pytables
    does not support concurrent writes to an HDF file. (Currently not used in the ExperimentControl Package.)"""
    
    previousParsedValue = None;

    def __init__(self, filename='',runName='test'):
        self.is_open = False
        self.open(filename,runName)

    def __del__(self):
        if self.is_open:
            self.fd.close()
            self.fdP.close()

    def open(self, filename='',runName='test'):
        if filename == '':
            filename = datetime.datetime.now().isoformat() + '.h5'

        if self.is_open == True:
            self.close()

        self.fd = AntRawDataAccess(filename,runName+'/'+SPARKSUBDIRECTORY)
        self.fdP = AntParsedDataAccess(filename,runName+'/'+SPARKSUBDIRECTORY)

        self.is_open = True
        #TODO write meta information...
        
    def close(self):
        if self.is_open:
            self.fd.close()
            self.fdP.close()
            self.is_open = False

    def _logEvent(self, event, data=None):
        timestamp = time.time()
        if event == EVENT_READ and len(data)>0:
            #logging.debug(len(data))
            information = {'packet':data,'timestamp':timestamp}
            try:
                #logging.debug(information)
                informationRaw = ant_sample(information)
                if informationRaw == []:
                    logging.debug('NOT A BROADCAST')
                    return
                logging.debug('Information Raw:'+str(informationRaw))
                informationParsed = ant_sample_interpret(informationRaw,\
                    self.previousParsedValue)
                self.previousParsedValue = informationParsed
                self.fd.write(informationRaw)
                self.fdP.write(informationParsed)
            except exceptions.UnknownParseError,e:
                pass
            except exceptions.ChecksumParseError,e:
                logging.debug('CHECKSUM ERROR:'+e.message)
示例#3
0
    def open(self, filename='', runName='test'):
        if filename == '':
            filename = datetime.datetime.now().isoformat() + '.h5'

        if self.is_open == True:
            self.close()

        self.fd = AntRawDataAccess(filename, runName + '/' + SPARKSUBDIRECTORY)
        self.fdP = AntParsedDataAccess(filename,
                                       runName + '/' + SPARKSUBDIRECTORY)

        self.is_open = True
 def test_parse_speed_cadence(self):
     #Read Test Data
     if exists('sample-tmp.h5'):
         unlink('sample-tmp.h5')
     copyfile('test-data/sample.h5','sample-tmp.h5')
     antAccess = AntRawDataAccess('sample-tmp.h5','/test/goldenembeded');
     antWriter = AntParsedDataAccess('sample-tmp.h5','/test/goldenembeded');
     firstSpeed=0;
     cumulativeRev=0;
     i=0
     previousRow = None
     for row in antAccess:
         #logging.debug('Type Row:'+str(type(row))+' Row: '+str(row))
         parsedRow = parse_sample_interpret(row,previousRow)
         logging.debug(row['Timestamp'])
         cspMSB = row['CumulativeSpeedRevolutionCountMSB']
         cspLSB = row['CumulativeSpeedRevolutionCountLSB']
         self.assertEqual(parsedRow['CumulativeSpeedRevolutionCount'],+\
             cspMSB*256+cspLSB)
         self.assertEqual(parsedRow['BikeCadence'],0)
         logging.debug('SPEED:'+str(i)+':'+str(parsedRow['BikeSpeed']))
         if i>19 and i < 33:
             self.assertTrue(parsedRow['BikeSpeed']>0)
         i+=1;
         self.assertEqual(parsedRow['BikeCadence'],0)
         secondSpeed = parsedRow['BikeSpeedEventTime']
         for column in parsedRow.values():
             #logging.debug('Column: '+str(column))
             self.assertTrue((type(column)==int) or (type(column)==float) )
         cumulativeSecond = parsedRow['CumulativeSpeedRevolutionCount']
         if secondSpeed > firstSpeed:
             self.assertGreater(cumulativeSecond, cumulativeRev, 'Greater')
         firstSpeed = secondSpeed
         cumulativeRev = cumulativeSecond
         logging.debug(parsedRow)
         antWriter.write(parsedRow)
         previousRow = parsedRow
     antAccess.close()
     i=0
     for row in antWriter:
         if i == 0:
             self.assertEqual(row['CumulativeSpeedRevolutionCount'],231*256+120)
         self.assertEqual(row['BikeCadence'],0)
         i+=1
    def open(self, filename='',runName='test'):
        if filename == '':
            filename = datetime.datetime.now().isoformat() + '.h5'

        if self.is_open == True:
            self.close()

        self.fd = AntRawDataAccess(filename,runName+'/'+SPARKSUBDIRECTORY)
        self.fdP = AntParsedDataAccess(filename,runName+'/'+SPARKSUBDIRECTORY)

        self.is_open = True
    def test_readline_complex(self):
        writerData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", 14334546]
        self.awriter.close()
        unlink("simple-test-data.h5")
        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test/again")
        self.awriter.write(writerData)
        self.awriter.close()

        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test")
        data = self.awriter.readlines()

        self.assertEqual(len(data), 0)

        logging.debug("Setting the run: ")
        self.awriter.setRunName("/test/again")
        logging.debug("Reading /test/again")
        data = self.awriter.readlines()
        logging.debug("The Data: " + str(data))
        data = list(data[0])
        writerPacket = [ord(number) for number in writerData if isinstance(number, str)]
        expected = writerPacket + [writerData[-1]]
        self.assertEqual(expected.sort(), data.sort())
    def test_write_parsed(self):
        if exists("sample-tmp.h5"):
            unlink("sample-tmp.h5")
        copyfile("test-data/sample.h5", "sample-tmp.h5")
        self.areader = AntRawDataAccess("sample-tmp.h5", "/test/goldenembeded")
        self.awriter = AntParsedDataAccess("sample-tmp.h5", "/test/goldenembeded")

        for raw in self.areader:
            parsed = parse_sample_interpret(raw)
            logging.debug(parsed)
            logging.debug("Parsed Time Stamp: " + str(parsed["Timestamp"]))
            self.awriter.write(parsed)

        for rawData, parsedData in zip(self.areader, self.awriter):
            self.assertEqual(rawData["Timestamp"], parsedData["Timestamp"])
    def test_readline(self):
        writerData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", 14334546]
        self.awriter.write(writerData)
        self.awriter.close()
        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test")
        data = self.awriter.readlines()

        logging.debug(data)
        logging.debug(data.dtype)
        # readindices = [column[0] for column in data.dtype.items()]
        # logging.debug(readindices)
        logging.debug(data[0])
        data = list(data[0])
        writerPacket = [ord(number) for number in writerData if isinstance(number, str)]
        expected = writerPacket + [writerData[-1]]
        self.assertEqual(expected.sort(), data.sort())
            print 'Parsing Failed: ' + str(e)
            continue
        try:
            logging.debug(parsedLine)
            theWriter.write(parsedLine)
            print '.',
            if shouldCsv:
                awritercsv.write(parsedLine)
                print 'c',
        except AntRawParserLengthNotCorrect, e:
            print 'x'


    if shouldParse:
        if extension == '.gce':
            parsedWriter = AntParsedDataAccess(outfile,
                                               runNameExtended)
        elif extension == '.ant':
            parsedWriter = AntParsedDataAccess(outfile,
                                               runNameExtended)
        elif extension == '.c3d':
            parsedWriter = None
        elif extension == '.csv':
            parsedWriter = None
        else:
            raise FileNotSupportedException('File not Supported: ' + extension)

        print '\n-------------------------------\nParsing Data\n'

        if shouldCsv:
            aparsedwritercsv = SofieCsvAccess(csvOutParsed)
        previousParsedLine = None
 def setUp(self):
     if os.path.isfile("simple-test-data.h5"):
         unlink("simple-test-data.h5")
     logging.info("AntRawDataAccess Test")
     self.awriter = AntRawDataAccess("simple-test-data.h5", "/test")
class AntRawDataAccessTest(unittest.TestCase):
    def setUp(self):
        if os.path.isfile("simple-test-data.h5"):
            unlink("simple-test-data.h5")
        logging.info("AntRawDataAccess Test")
        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test")

    def tearDown(self):
        self.awriter.close()

    def test_write(self):
        self.awriter.write(["1", "2", "3", "4", "5", "6", "7", "8", "9", 14334546])

    def test_readline(self):
        writerData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", 14334546]
        self.awriter.write(writerData)
        self.awriter.close()
        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test")
        data = self.awriter.readlines()

        logging.debug(data)
        logging.debug(data.dtype)
        # readindices = [column[0] for column in data.dtype.items()]
        # logging.debug(readindices)
        logging.debug(data[0])
        data = list(data[0])
        writerPacket = [ord(number) for number in writerData if isinstance(number, str)]
        expected = writerPacket + [writerData[-1]]
        self.assertEqual(expected.sort(), data.sort())

    def test_readline_complex(self):
        writerData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", 14334546]
        self.awriter.close()
        unlink("simple-test-data.h5")
        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test/again")
        self.awriter.write(writerData)
        self.awriter.close()

        self.awriter = AntRawDataAccess("simple-test-data.h5", "/test")
        data = self.awriter.readlines()

        self.assertEqual(len(data), 0)

        logging.debug("Setting the run: ")
        self.awriter.setRunName("/test/again")
        logging.debug("Reading /test/again")
        data = self.awriter.readlines()
        logging.debug("The Data: " + str(data))
        data = list(data[0])
        writerPacket = [ord(number) for number in writerData if isinstance(number, str)]
        expected = writerPacket + [writerData[-1]]
        self.assertEqual(expected.sort(), data.sort())

    def test_write_parsed(self):
        if exists("sample-tmp.h5"):
            unlink("sample-tmp.h5")
        copyfile("test-data/sample.h5", "sample-tmp.h5")
        self.areader = AntRawDataAccess("sample-tmp.h5", "/test/goldenembeded")
        self.awriter = AntParsedDataAccess("sample-tmp.h5", "/test/goldenembeded")

        for raw in self.areader:
            parsed = parse_sample_interpret(raw)
            logging.debug(parsed)
            logging.debug("Parsed Time Stamp: " + str(parsed["Timestamp"]))
            self.awriter.write(parsed)

        for rawData, parsedData in zip(self.areader, self.awriter):
            self.assertEqual(rawData["Timestamp"], parsedData["Timestamp"])