def fb_avg(fname, tgoal=1000, fgoal=128, fscr=None): """ Downsample a filterbank file to appropriate resolution for plotting. """ fr = FilReader(fname) if fscr is None: fscr = int(fr.header['nchans'] / fgoal) tscr = 2**int(np.log2(fr.header['nsamples'] / tgoal)) gulp = tscr nchan = fr.header['nchans'] nblock = fr.header['nsamples'] / tscr nsub = nchan / fscr output = np.empty((nsub, nblock)) for nsamps, i, data in fr.readPlan(gulp, verbose=False): if nsamps != gulp: #print 'breaking at %d'%i break t = data.reshape( (gulp, nchan)).transpose().astype(np.float32).mean(axis=1) output[:, i] = np.average(t.reshape(nchan / fscr, fscr), axis=1) freqs = np.arange( fr.header['nchans']) * fr.header['foff'] + fr.header['fch1'] freqs = freqs.reshape(len(freqs) / fscr, fscr).mean(axis=1) times = np.arange(nblock) * fr.header['tsamp'] * gulp return output, freqs, times
def fil_splitter(fil_fns, outfn, nsub, tqdm_desc=None, verbose=True): """ Split filterbank data to different subband files """ start_time = timeit.default_timer() if isinstance(fil_fns, str): fil_fns = [fil_fns] if len(fil_fns) > 1: raise TypeError(f'Input files: "{len(fil_fns)}". Not supported yet!.') if nsub == 1: raise TypeError(f'Nsub: {nsub}. No need for conversion!.') myFil = FilReader(fil_fns[0]) out_files = [] hdr_changes = {} chanpersub = myFil.header.nchans // nsub for isub in range(nsub): hdr_changes["nchans"] = chanpersub hdr_changes[ "fch1"] = myFil.header.fch1 + isub * chanpersub * myFil.header.foff outfil = f'{outfn.split(".fil")[0]}_sub{str(isub).zfill(2)}.fil' out_files.append( myFil.header.prepOutfile(outfil, hdr_changes, nbits=myFil.header.nbits)) if verbose: print("Splitting Filterbank:") print("---------------------------------") gulpsize = 32768 for nsamps, ii, data in tqdm(myFil.readPlan(gulp=gulpsize, verbose=False), total=np.ceil(myFil.header.nsamples / gulpsize).astype('int'), desc=tqdm_desc): for isub, out_file in enumerate(out_files): data = data.reshape(nsamps, myFil.header.nchans) subint_tofil = data[:, chanpersub * isub:chanpersub * (isub + 1)] out_file.cwrite(subint_tofil.ravel()) # Now close each of the filterbank file. for out_file in out_files: out_file.close() end_time = timeit.default_timer() if verbose: print(f'Done') print(f'Execution time: {(end_time-start_time):.3f} seconds\n')
def fb_avg(fname,tbingoal=1000,fgoal=128,fscr=None,last_minute=False): """ Downsample a filterbank file to appropriate resolution for plotting. """ fr = FilReader(fname) if fscr is None: fscr = int(fr.header['nchans']/fgoal) nsamp = fr.header['nsamples'] if (nsamp*fr.header['tsamp']) < 60: last_minute = False if last_minute: nsamp = int(60./fr.header['tsamp'])+1 try: tscr = 2**int(np.log2(nsamp/tbingoal)) except OverflowError: tscr = 1 gulp = tscr nchan = fr.header['nchans'] nblock = nsamp/tscr nsub = nchan/fscr output = np.empty( (nsub, nblock) ) start = 0 if last_minute: start = max(0,int(fr.header['nsamples']-nsamp)-1) for nsamps,i,data in fr.readPlan(gulp,start=start,verbose=False): if nsamps != gulp: #print 'breaking at %d'%i break t = data.reshape((gulp,nchan)).transpose().astype(np.float32).mean(axis=1) output[:,i] = np.average(t.reshape(nchan/fscr,fscr),axis=1) freqs = np.arange(fr.header['nchans'])*fr.header['foff']+fr.header['fch1'] freqs = freqs.reshape(len(freqs)/fscr,fscr).mean(axis=1) times = (start + np.arange(nblock)*gulp)*fr.header['tsamp'] return output,freqs,times