def getFrameInfo(self, acq_frame_nb): arr = self.__comm().getBuffer(acq_frame_nb) arr.resize(240, 566) data = arr[:, 5:565] FrameInfo = Core.HwFrameInfoType(acq_frame_nb,\ data,Core.Timestamp(),0,\ Core.HwFrameInfoType.Transfer) return FrameInfo
def data2FrameInfo(self, data): arr = data.buffer arr_type = arr.dtype.type arr_height, arr_width = arr.shape image_type = self.DataType2ImageType[arr_type] buffer_ctrl = self.m_hw_inter.getHwCtrlObj(Core.HwCap.Buffer) start_ts = buffer_ctrl.getStartTimestamp() fdim = Core.FrameDim(arr_width, arr_height, image_type) timestamp = Core.Timestamp(data.timestamp) valid_pixels = Core.Point(fdim.getSize()).getArea() ownership = Core.HwFrameInfoType.Managed finfo = Core.HwFrameInfoType(data.frameNumber, arr, timestamp, valid_pixels, ownership) return finfo, fdim
def getFrameInfo(self, acq_frame_nb): deb.Param('acq_frame_nb: %d' % acq_frame_nb) com = self.__com() fullPath = os.path.join( com.getFilePath(), '%s%.5d%s' % (com.getFileBase(), acq_frame_nb, com.getFileExtension())) deb.Trace('Try to read : %s' % fullPath) if os.access(fullPath, os.R_OK): try: # f = EdfFile.EdfFile(fullPath) # data = f.GetData(0) f = open(nextFullPath, 'rb') f.seek(0x200) s = f.read(2048 * 2048 * 2) d = numpy.fromstring(s, numpy.uint16) data = d.reshape(2048, 2048) except: pass else: frInfo = Core.HwFrameInfoType(acq_frame_nb, data, Core.Timestamp(), 0, Core.HwFrameInfoType.Transfer) return frInfo
def run(self): self.__cond.acquire() print "run lock" while self.__run : ### CONFIG COMMAND ### if self.__command == self.COM_CONF : print "Communication: Config Command" for i,mask in map(None, range(2), [0x1,0x2]): # Save DACL values for each module # Has to be in reverse order to avoid # overwriting of following pixels for k in range(119,-1,-1): for j in range(7): calbuff = (ct.c_uint*len(self.__DACL[i][j][k][2:82]))( \ *(self.__DACL[i][j][k][2:82]*8+1)) if self.__dll.xpci_modSaveConfigL( \ mask, 0, j, k, calbuff) !=0 : #self.__cond.release() raise Exception,"Communication Error: Configuration saving failure (%#.2X, %d, %d, %d)" % (mask, i, j, k) # Load DACL Values if self.__dll.xpci_modDetLoadConfig(mask,0,0) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure" # Load register values retbuff = (ct.c_uint * (16*self.__ModNb))() if self.__dll.xpci_modLoadConfigG(self.__ModMask, 0x7F, ITUNE_V32 \ ,145, retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (self.__ModMask, 0x7F) if self.__dll.xpci_modLoadConfigG(self.__ModMask, 0x7F, AMP_TP_V32 \ , 0, retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (self.__ModMask, 0x7F) if self.__dll.xpci_modLoadConfigG(self.__ModMask, 0x7F, IMFP_V32 \ , 52, retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (self.__ModMask, 0x7F) if self.__dll.xpci_modLoadConfigG(self.__ModMask, 0x7F, IOTA_V32 \ , 40, retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (self.__ModMask, 0x7F) if self.__dll.xpci_modLoadConfigG(self.__ModMask, 0x7F, IPRE_V32 \ , 60, retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (self.__ModMask, 0x7F) if self.__dll.xpci_modLoadConfigG(self.__ModMask, 0x7F, IBUFFER_V32\ , 0, retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (self.__ModMask, 0x7F) for i in range(2): mmask = 0x01 << i for j in range(7): cmask = 0x01 << j if self.__dll.xpci_modLoadConfigG(mmask, cmask, ITHL_V32 \ ,int(self.__ITHL[i][j]), retbuff) != 0: #self.__cond.release() raise Exception,"Communication Error: Configuration loading failure (%#.2X, %#.2X)" % (mmask, cmask) self.__configured = True self.__command = self.COM_NONE ### START COMMAND ### if self.__command == self.COM_START : print "Communication: Start Command (t %f)" % (time.clock()) if self.__dll.xpci_sendExposureParameters( \ self.__ModMask,\ self.__TExp_m,\ self.__TExp_l,\ self.__TLat_m,\ self.__TLat_l,\ self.__TIni_m,\ self.__TIni_l,\ self.__TSht_m,\ self.__TSht_l,\ self.__TOvf_m,\ self.__TOvf_l,\ self.__TrigMode,\ 0,0,self.__FramesNb,\ self.__OutMode,\ B2,0,0) != 0: #self.__cond.release() raise Exception, "Communication Error: failure sending exposure parameters" #self.__cond.release() #print "run unlock" print "Communication: Get buffer (t %f)" % (time.clock()) if self.__dll.xpci_getImgSeq_imxpad( \ B2, self.__ModMask, 7, \ self.__FramesNb, \ self.__ImgBuff, 0) != 0: #self.__cond.acquire() #print "run lock" #self.__cond.release() raise Exception, "Communication Error: failure getting images" print "Communication: Write buffer (t %f)" % (time.clock()) self.__FramesRdy = self.__FramesNb #data = np.zeros([240,560],dtype='uint16') if self.__frameCB: for i in range(self.__FramesRdy): arr = np.array(self.__ImgBuff[i][0:self.__BuffLen], dtype='uint16') arr.resize(240,566) data = np.array(arr[:,5:565], dtype='uint16') print "Save frame",i," (t %f)" % (time.clock()) FrameInfo = Core.HwFrameInfoType(i,\ data,Core.Timestamp(),0,\ Core.HwFrameInfoType.Transfer) self.__frameCB.newFrameReady(FrameInfo) print "Frame",i,"ready (t %f)" % (time.clock()) self.__command = self.COM_NONE ### TEST COMMAND ### if self.__command == self.COM_TEST : print "Communication: Test Command" #self.__cond.release() #print "run unlock" if self.__dll.xpci_modLoadAutoTest(self.__ModMask, \ 0x4F6B, \ self.__TestMode ): #self.__cond.acquire() #print "run lock" #self.__cond.release() raise Exception, "Communication Error: failure loading autotest\n" self.setNbBuffers(1) #self.__cond.acquire() print "run lock" if self.__dll.xpci_readOneImage( \ B2, self.__ModMask, 7, \ self.__ImgBuff) !=0: #self.__cond.release() raise Exception, "Communication Error: failure loading autotest\n" self.__command = self.COM_NONE ### NO COMMAND ### # By default wait for a command else : print "Communication: No Command" while self.__run == True and self.__command == self.COM_NONE: print "Run sleep" self.__cond.wait() print "Run wake up" self.__cond.release() print "run unlock" time.sleep(0.01)
def run(self): lastDirectoryTime = None #if not self.__dirFd: return #@todo should throw an execption with self.__cond: #print " #1 "*100 while (self.__continue): #newDirectoryTime = os.fstat(self.__dirFd).st_mtime newDirectoryTime = os.stat(self.__com().getFilePath()).st_mtime #print " #2 "*100 print "###ImgReader: waiting", self.__waitFlag #print "Last directory time:",lastDirectoryTime while (self.__waitFlag or (self.__continue and lastDirectoryTime == newDirectoryTime)): self.__cond.wait(1) # print "New directory time:",newDirectoryTime # print "Wait?",self.__waitFlag #newDirectoryTime = os.fstat(self.__dirFd).st_mtime newDirectoryTime = os.stat( self.__com().getFilePath()).st_mtime #print " #3 "*100 print "###ImgReader: working" while (self.__continue and not self.__waitFlag): nextFrameId = self.__lastImageRead + 1 self.__cond.release() #print " #4 "*100 try: nextFullPath = os.path.join( self.__com().getFilePath(), '%s%.5d%s' % (self.__com().getFileBase(), nextFrameId, self.__com().getFileExtension())) except: #print " #5 "*100 self.__cond.acquire() raise if os.access(nextFullPath, os.R_OK): #print " #6 "*100 try: # f = EdfFile.EdfFile(nextFullPath) # data = f.GetData(0) # Whait for the file to be written while os.stat(nextFullPath ).st_size != 512 + 2048 * 2048 * 2: print "###ImgReader: Waiting for file %s to be ready" % nextFullPath time.sleep(0.1) if self.__ctSaving == None: self.__ctSaving = self.__com().getCtSavingLink( ) f = open(nextFullPath, 'rb') for line in f: if "DATE" in line: date = line.split("=")[1].split(";")[0] break stamp = time.mktime(time.strptime(date)) self.__ctSaving.addToFrameHeader( nextFrameId, ["TimeStamp", str(stamp)]) f.seek(0x200) s = f.read(2048 * 2048 * 2) d = numpy.fromstring(s, numpy.uint16) data = d.reshape(2048, 2048) except Exception, e: print "###ImgReader: Exception %s" % e self.__cond.acquire() raise Exception, e break else: print "###ImgReader: Reading file", nextFullPath #print " #8 "*100 try: buffer_ctrl = self.__buffer_ctrl() continueFlag = True if buffer_ctrl._cbk: hw_frame_info = Core.HwFrameInfoType( nextFrameId, data, Core.Timestamp(), 0, Core.HwFrameInfoType.Transfer) #print hw_frame_info continueFlag = buffer_ctrl._cbk.newFrameReady( hw_frame_info) outputFileName = ( self.__ctSaving.getDirectory() + "/" + self.__ctSaving.getPrefix() + self.__ctSaving.getParameters().indexFormat + self.__ctSaving.getSuffix()) % nextFrameId if os.access(buffer_ctrl._xfilename, os.R_OK): fx = open(buffer_ctrl._xfilename, 'w') fx.write("%d %s\n" % (random.randint( 0, 9999), outputFileName)) fx.close() del data del f #remove old image from buffer (tmp_fs) idImage2remove = nextFrameId - buffer_ctrl.getNbBuffers( ) if idImage2remove >= 0: fullImagePath2remove = os.path.join( self.__com().getFilePath(), '%s%.5d%s' % (self.__com().getFileBase(), idImage2remove, self.__com().getFileExtension())) os.unlink(fullImagePath2remove) except Exception, e: print "###ImgReader: Exception %s ." % e #print " #9 "*100 self.__cond.acquire() raise Exception, e self.__cond.acquire() self.__lastImageRead = nextFrameId self.__waitFlag = not continueFlag else: # We didn't managed to access the file #print " #10 "*100 print "###ImgReader: File", nextFullPath, "not found" try: ErrorFlag = False lastDirectoryTime = newDirectoryTime #Get all images from directory files = [ x for x in os.listdir(self.__com().getFilePath()) if os.path.splitext(x)[-1] == '.edf' ] if files: files.sort(_invert_sort_file) lastImageName, _ = os.path.splitext(files[0]) lastImageId = int(lastImageName.split('_')[-1]) #We're probably losing some frames if lastImageId - nextFrameId > 10: ErrorFlag = True deb.Error( 'Missing frame %d, Acquisition is in Fault stat' % nextFrameId) #buffer_ctrl = self.__buffer_ctrl() com = self.__com() if com: com.stop_acquisition() except Exception, e: print "###ImgReader: Exception %s .." % e #print " #11 "*100 self.__cond.acquire() raise Exception, e self.__cond.acquire() if ErrorFlag: #print " #12 "*100 self.__waitFlag = True self.__readError = True break