def create_filterbank_file(outfn, header, spectra=None, nbits=8, \ verbose=False, mode='append'): """Write filterbank header and spectra to file. Input: outfn: The outfile filterbank file's name. header: A dictionary of header paramters and values. spectra: Spectra to write to file. (Default: don't write any spectra - i.e. write out header only) nbits: The number of bits per sample of the filterbank file. This value always overrides the value in the header dictionary. (Default: 8 - i.e. each sample is an 8-bit integer) verbose: If True, be verbose (Default: be quiet) mode: Mode for writing (can be 'append' or 'write') Output: fbfile: The resulting FilterbankFile object opened in read-write mode. """ dtype = get_dtype(nbits) # Get dtype. This will check to ensure # 'nbits' is valid. header['nbits'] = nbits outfile = open(outfn, 'wb') outfile.write(sigproc.addto_hdr("HEADER_START", None)) for paramname in header.keys(): if paramname not in sigproc.header_params: # Only add recognized parameters continue if verbose: print "Writing header param (%s)" % paramname value = header[paramname] outfile.write(sigproc.addto_hdr(paramname, value)) outfile.write(sigproc.addto_hdr("HEADER_END", None)) if spectra is not None: spectra.flatten().astype(dtype).tofile(outfile) outfile.close() return FilterbankFile(outfn, mode=mode)
def write_spectra(self, spectra, ispec): """Write spectra to the file if is writable. Input: spectra: The spectra to append. The new spectra must have the correct number of channels (ie dimension of axis=1. ispec: The index of the spectrum of where to start writing. Outputs: None """ if 'r+' not in self.filfile.mode.lower(): raise ValueError("FilterbankFile object for '%s' is not writable." % \ self.filename) nspec, nchans = spectra.shape if nchans != self.nchans: raise ValueError("Cannot write spectra. Incorrect shape. " \ "Number of channels in file: %d; Number of " \ "channels in spectra to write: %d" % \ (self.nchans, nchans)) if ispec > self.nspec: raise ValueError("Cannot write past end of file! " \ "Present number of spectra: %d; " \ "Requested index of write: %d" % \ (self.nspec, ispec)) data = spectra.flatten() np.clip(data, self.dtype_min, self.dtype_max, out=data) # Move to requested position pos = self.header_size + ispec * self.bytes_per_spectrum self.filfile.seek(pos, os.SEEK_SET) self.filfile.write(data.astype(self.dtype)) if nspec + ispec > self.nspec: self.nspec = nspec + ispec
def append_spectra(self, spectra): """Append spectra to the file if is not read-only. Input: spectra: The spectra to append. The new spectra must have the correct number of channels (ie dimension of axis=1. Outputs: None """ if self.filfile.mode.lower() in ('r', 'rb'): raise ValueError("FilterbankFile object for '%s' is read-only." % \ self.filename) nspec, nchans = spectra.shape if nchans != self.nchans: raise ValueError("Cannot append spectra. Incorrect shape. " \ "Number of channels in file: %d; Number of " \ "channels in spectra to append: %d" % \ (self.nchans, nchans)) data = spectra.flatten() np.clip(data, self.dtype_min, self.dtype_max, out=data) # Move to end of file self.filfile.seek(0, os.SEEK_END) self.filfile.write(data.astype(self.dtype)) self.nspec += nspec