def StartAcquisition(protocolReactor): global CPTH global id global StartTime global dataset StartTime = datetime.datetime.now() filename = os.path.join(INSDIR+'/Python/temp', StartTime.strftime("%Y%m%d-%H%M%S")+'-record%s'%id+'.hdf5') # Creates HDF5 file to save signals in real time fid = h5db.hdf(filename, 'w') header = {'MAC': CPTH.macAddress, 'date': datetime.datetime.utcnow().isoformat()} fid.addInfo(header) signalsRT = {} blockShape = (CPTH.nSamples,) keysSorted = dataset.keys() keysSorted.sort() for i,j in enumerate(dataset): mdata = {'type': '/AnalogInputs/Analog%s'%i, 'name': dataset[keysSorted[i]]['label'].split(' [')[0],'labels': [dataset[keysSorted[i]]['label']]} dataName = dataset[keysSorted[i]]['label'].split(' [')[0] signalsRT['Analog%s'%i] = fid.addSignalRT(mdata['type'], mdata, dataName, blockShape) for i in range(4) : mdata = {'type': '/DigitalInputs/Digital%s'%i, 'name': 'Digital%s'%i,'labels': ['Digital%s'%i]} dataName = 'Digital%s'%i signalsRT['Digital%s'%i] = fid.addSignalRT(mdata['type'], mdata, dataName, blockShape) mdata = {'type': '/others', 'name': 'SeqN', 'labels':['SeqN']} signalsRT['SeqN'] = fid.addSignalRT(mdata['type'], mdata, 'SeqN', blockShape) CPTH.file=fid CPTH.RT = signalsRT CPTH.filename = filename # Sends command to start acquisition CPTH.acquire = True CPTH.reactorr = protocolReactor id=id+1 print "Acquisition set to start"
def saveFile(path,file,type): try: global CPTH file.decode("utf-8","ignore") if type=="txt": if file[-4:] == '.txt': file = file[:-4] f=h5py.File(CPTH.filename); #Txt File mdata = f.attrs['json'] head = eval(mdata) sig = listHDF5Signals(f) FinalArray = numpy.zeros((len(f[sig[0]].value),len(sig))) h = [None] * len(sig) for s in sig: if 'SeqN' in s: FinalArray[:,0] = f[s].value h[0] = s.split('/')[-1] elif 'Digital' in s: d = s.split('/')[-1][-1] FinalArray[:,int(d)+1] = f[s].value h[int(d)+1] = s.split('/')[-1] elif 'Analog' in s: a = s.split('/')[-2][-1] FinalArray[:,int(a)+5] = f[s].value h[int(a)+5] = s.split('/')[-1] head['ChannelsOrder'] = h numpy.savetxt(path+'/'+file+'.txt',FinalArray,fmt='%0d',delimiter='\t',header=json.dumps(head)) f.close() #hdf5 file else: if file[-5:] == '.hdf5': file = file[:-5] shutil.copy(CPTH.filename,os.path.join(path,file+'.hdf5')) f = h5db.hdf(os.path.join(path,file+'.hdf5')) mdata = f.getInfo()['header'] mdata['name'] = file f.addInfo(mdata) f.close() except Exception: print traceback.format_exc() return 'FileSaved()'