def fitsFinalise(self): """finalise a saved on fly fits file...""" if self.asfits and self.finalise: self.finalise=0 self.fd.seek(0,2) pos=self.fd.tell() self.fd.seek(self.hdustart) nbytes=pos-2880-self.hdustart n=nbytes/self.datasize FITS.updateLastAxis(None,n,self.fd) self.fd.seek(0,2)#go to end extra=2880-pos%2880 if extra<2880: self.fd.write(" "*extra) #Now add the frame numbers and timestamps. self.fdfno.seek(0) FITS.updateLastAxis(None,n,self.fdfno) self.fdfno.seek(0) self.fd.write(self.fdfno.read()) pos=self.fd.tell() extra=2880-pos%2880 if extra<2880: self.fd.write(" "*extra) self.fdtme.seek(0) FITS.updateLastAxis(None,n,self.fdtme) self.fdtme.seek(0) self.fd.write(self.fdtme.read()) pos=self.fd.tell() extra=2880-pos%2880 if extra<2880: self.fd.write(" "*extra) self.fdtme.close() self.fdfno.close() try: os.unlink(self.name+"fno") os.unlink(self.name+"tme") except: pass
def fitsFinalise(self): """finalise a saved on fly fits file...""" if self.asfits and self.finalise: self.finalise = 0 self.fd.seek(0, 2) pos = self.fd.tell() self.fd.seek(self.hdustart) nbytes = pos - 2880 - self.hdustart n = nbytes / self.datasize FITS.updateLastAxis(None, n, self.fd) self.fd.seek(0, 2) #go to end extra = 2880 - pos % 2880 if extra < 2880: self.fd.write(" " * extra) #Now add the frame numbers and timestamps. self.fdfno.seek(0) FITS.updateLastAxis(None, n, self.fdfno) self.fdfno.seek(0) self.fd.write(self.fdfno.read()) pos = self.fd.tell() extra = 2880 - pos % 2880 if extra < 2880: self.fd.write(" " * extra) self.fdtme.seek(0) FITS.updateLastAxis(None, n, self.fdtme) self.fdtme.seek(0) self.fd.write(self.fdtme.read()) pos = self.fd.tell() extra = 2880 - pos % 2880 if extra < 2880: self.fd.write(" " * extra) self.fdtme.close() self.fdfno.close() try: os.unlink(self.name + "fno") os.unlink(self.name + "tme") except: pass
def tofits(self,fname,ffrom=None,fto=None,tfrom=None,tto=None): curshape=None curdtype=None fheader=None nentries=0 tlist=[] flist=[] ffits=open(fname,"w") firstHeader=1 while 1: hdr=self.fd.read(self.info.size*self.info.itemsize) if hdr=="": break elif len(hdr)<self.info.size*self.info.itemsize: print "Didn't read all of header" break info=numpy.fromstring(hdr,numpy.int32) fno=int(info[1]) ftime=float(info[2:4].view("d")) databytes=info[0]-(self.info.size-1)*self.info.itemsize fok=tok=0 if (ffrom==None or fno>=ffrom) and (fto==None or fno<=fto): #print fno fok=1 if (tfrom==None or ftime>=tfrom) and (tto==None or ftime<=tto): tok=1 if fok==1 and tok==1: frame=self.fd.read(databytes) if len(frame)!=databytes: print "Didn't read all of frame" break frame=numpy.fromstring(frame,chr(info[4])).byteswap() #can it be put into the existing HDU? If not, finalise current, and start a new one. if curshape!=databytes or curdtype!=chr(info[4]): #end the current HDU FITS.End(ffits) #Update FITS header if fheader!=None: FITS.updateLastAxis(None,nentries,fheader) del(fheader) #fheader.close() fheader=None #now write the frame number and time. ffits.close() if firstHeader==0: FITS.Write(numpy.array(flist).astype("i"),fname,writeMode="a") FITS.Write(numpy.array(tlist),fname,writeMode="a") ffits=open(fname,"a+") FITS.WriteHeader(ffits,[1,databytes/numpy.zeros((1,),chr(info[4])).itemsize],chr(info[4]),firstHeader=firstHeader) ffits.flush() firstHeader=0 fheader=numpy.memmap(fname,dtype="c",mode="r+",offset=ffits.tell()-2880) flist=[] tlist=[] nentries=0 curshape=databytes curdtype=chr(info[4]) #now write the data ffits.write(frame) nentries+=1 flist.append(fno) tlist.append(ftime) else: #skip the data self.fd.seek(databytes-1,1) if self.rd.read(1)=="": print "Didn't read all of the frame" break #now finalise the file. FITS.End(ffits) if fheader!=None: FITS.updateLastAxis(None,nentries,fheader) #fheader.close() del(fheader) fheader=None #now write the frame number and time. ffits.close() FITS.Write(numpy.array(flist).astype("i"),fname,writeMode="a") FITS.Write(numpy.array(tlist),fname,writeMode="a")
def tofits(self, fname, ffrom=None, fto=None, tfrom=None, tto=None): curshape = None curdtype = None fheader = None nentries = 0 tlist = [] flist = [] ffits = open(fname, "w") firstHeader = 1 while 1: hdr = self.fd.read(self.info.size * self.info.itemsize) if hdr == "": break elif len(hdr) < self.info.size * self.info.itemsize: print "Didn't read all of header" break info = numpy.fromstring(hdr, numpy.int32) fno = int(info[1]) ftime = float(info[2:4].view("d")) databytes = info[0] - (self.info.size - 1) * self.info.itemsize fok = tok = 0 if (ffrom == None or fno >= ffrom) and (fto == None or fno <= fto): #print fno fok = 1 if (tfrom == None or ftime >= tfrom) and (tto == None or ftime <= tto): tok = 1 if fok == 1 and tok == 1: frame = self.fd.read(databytes) if len(frame) != databytes: print "Didn't read all of frame" break frame = numpy.fromstring(frame, chr(info[4])) #can it be put into the existing HDU? If not, finalise current, and start a new one. if curshape != databytes or curdtype != chr(info[4]): #end the current HDU FITS.End(ffits) #Update FITS header if fheader != None: FITS.updateLastAxis(None, nentries, fheader) del (fheader) #fheader.close() fheader = None #now write the frame number and time. ffits.close() if firstHeader == 0: FITS.Write(numpy.array(flist).astype("i"), fname, writeMode="a", doByteSwap=self.doByteSwap) FITS.Write(numpy.array(tlist), fname, writeMode="a", doByteSwap=self.doByteSwap) ffits = open(fname, "a+") FITS.WriteHeader(ffits, [ 1, databytes / numpy.zeros( (1, ), chr(info[4])).itemsize ], chr(info[4]), firstHeader=firstHeader, doByteSwap=self.doByteSwap) ffits.flush() firstHeader = 0 fheader = numpy.memmap(fname, dtype="c", mode="r+", offset=ffits.tell() - 2880) flist = [] tlist = [] nentries = 0 curshape = databytes curdtype = chr(info[4]) #now write the data if self.doByteSwap and numpy.little_endian: ffits.write(frame.byteswap().data) else: ffits.write(frame) flist.append(fno) tlist.append(ftime) nentries += 1 else: #skip the data self.fd.seek(databytes - 1, 1) if self.rd.read(1) == "": print "Didn't read all of the frame" break #now finalise the file. FITS.End(ffits) if fheader is not None: FITS.updateLastAxis(None, nentries, fheader) #fheader.close() del (fheader) fheader = None #now write the frame number and time. ffits.close() FITS.Write(numpy.array(flist).astype("i"), fname, writeMode="a", doByteSwap=self.doByteSwap) FITS.Write(numpy.array(tlist), fname, writeMode="a", doByteSwap=self.doByteSwap)