Beispiel #1
0
 def __init__(self, fil_filenm):
     self.fil_filenm = fil_filenm
     self.basefilenm = fil_filenm.rstrip(".fil")
     self.beam = int(self.basefilenm[-1])
     filhdr, self.hdrlen = sigproc.read_header(fil_filenm)
     self.orig_filenm = filhdr['rawdatafile']
     self.MJD = filhdr['tstart']
     self.nchans = filhdr['nchans']
     self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
     self.ra_string = psr_utils.coord_to_string(\
         *psr_utils.rad_to_hms(self.ra_rad))
     self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
     self.dec_string = psr_utils.coord_to_string(\
         *psr_utils.rad_to_dms(self.dec_rad))
     self.az = filhdr['az_start']
     self.el = 90.0-filhdr['za_start']
     self.BW = abs(filhdr['foff']) * filhdr['nchans']
     self.dt = filhdr['tsamp']
     self.orig_N = sigproc.samples_per_file(fil_filenm, filhdr, self.hdrlen)
     self.orig_T = self.orig_N * self.dt
     self.N = psr_utils.choose_N(self.orig_N)
     self.T = self.N * self.dt
     # Update the RA and DEC from the database file if required
     newposn = read_db_posn(self.orig_filenm, self.beam)
     if newposn is not None:
         self.ra_string, self.dec_string = newposn
         # ... and use them to update the filterbank file
         fix_fil_posn(fil_filenm, self.hdrlen,
                      self.ra_string, self.dec_string)
     # Determine the average barycentric velocity of the observation
     self.baryv = presto.get_baryv(self.ra_string, self.dec_string,
                                   self.MJD, self.T, obs="AO")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_directory,
                                   str(int(self.MJD)),
                                   self.basefilenm[:-2],
                                   str(self.beam))
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.subbanding_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
Beispiel #2
0
    def __init__(self, fits_filenm):
        self.fits_filenm = fits_filenm
        self.basefilenm = fits_filenm[:fits_filenm.find(".fits")]
        self.dsbasefilenm = fits_filenm[:fits_filenm.rfind("_")]
        fitshandle = pyfits.open(fits_filenm)
        self.MJD = fitshandle[0].header['STT_IMJD'] + fitshandle[0].header[
            'STT_SMJD'] / 86400.0 + fitshandle[0].header['STT_OFFS'] / 86400.0
        self.nchans = fitshandle[0].header['OBSNCHAN']
        self.ra_string = fitshandle[0].header['RA']
        self.dec_string = fitshandle[0].header['DEC']
        self.str_coords = "J" + "".join(self.ra_string.split(":")[:2])
        self.str_coords += "".join(self.dec_string.split(":")[:2])
        self.nbits = fitshandle[0].header['BITPIX']

        self.raw_N = fitshandle[1].header['NAXIS2'] * fitshandle[1].header[
            'NSBLK']
        self.dt = fitshandle[1].header['TBIN'] * 1000000
        self.raw_T = self.raw_N * self.dt
        self.N = raw_N
        if self.dt == 163.84:
            self.N = self.N / 2
        self.T = self.N * self.dt
        self.srcname = fitshandle[0].header['SRC_NAME']
        # Determine the average barycentric velocity of the observation
        self.baryv = presto.get_baryv(self.ra_string,
                                      self.dec_string,
                                      self.MJD,
                                      self.T,
                                      obs="GB")
        # Where to dump all the results
        # Directory structure is under the base_output_directory
        # according to base/MJD/filenmbase/beam
        self.outputdir = os.path.join(base_output_dir, str(int(self.MJD)),
                                      self.srcname)
        # Figure out which host we are processing on
        self.hostname = socket.gethostname()
        # The fraction of the data recommended to be masked by rfifind
        self.masked_fraction = 0.0
        # Initialize our timers
        self.rfifind_time = 0.0
        self.downsample_time = 0.0
        self.dedispersing_time = 0.0
        self.FFT_time = 0.0
        self.lo_accelsearch_time = 0.0
        self.hi_accelsearch_time = 0.0
        self.singlepulse_time = 0.0
        self.sifting_time = 0.0
        self.folding_time = 0.0
        self.total_time = 0.0
        # Inialize some candidate counters
        self.num_sifted_cands = 0
        self.num_folded_cands = 0
        self.num_single_cands = 0
Beispiel #3
0
def multimoment(pulses,idL,inc=12):
  pulses.sort_values(['SAP','BEAM'],inplace=True)
  last_beam = -1
  last_sap = -1
  freq = np.linspace(F_MIN,F_MAX,2592)
  v = 0
  multimoment = np.zeros(pulses.shape[0],dtype=np.float32)
  
  for i,(idx,puls) in enumerate(pulses.iterrows()):
    if (puls.BEAM != last_beam) | (puls.SAP != last_sap):
      beam = puls.BEAM.astype(int)
      sap = puls.SAP.astype(int)
      
      #Open the fits file
      if beam==inc: stokes = 'incoherentstokes'
      else: stokes = 'stokes'
      filename = '{folder}/{idL}_red/{stokes}/SAP{sap}/BEAM{beam}/{idL}_SAP{sap}_BEAM{beam}.fits'.format(folder=PATH.RAW_FOLDER,idL=idL,stokes=stokes,sap=sap,beam=beam)
      try: fits = pyfits.open(filename,memmap=True)
      except IOError: continue
      
      last_beam = beam
      last_sap = sap
  
      header = Utilities.read_header(filename)
      MJD = header['STT_IMJD'] + header['STT_SMJD'] / 86400.
      v = presto.get_baryv(header['RA'],header['DEC'],MJD,1800.,obs='LF')

      if puls.DM < DM_STEP1: sample = puls.Sample
      elif puls.DM < DM_STEP2: sample = puls.Sample * 2
      else: sample = puls.Sample
      
      sample += np.round(sample*v).astype(int)
      duration = np.int(np.round(puls.Duration/RES))
    
      #Load the spectrum
      spectrum = Utilities.read_fits(fits,puls.DM.copy(),sample.copy(),duration,0)
    
      #De-dispersion
      time = (4149 * puls.DM * (np.power(freq,-2) - F_MAX**-2) / RES).round().astype(np.int)
      spectrum = np.sum([spectrum[time+x,np.arange(2592)] for x in range(duration)],axis=0)
      
      I1 = spectrum.size * np.sum(spectrum**2)
      I2 = np.sum(spectrum)**2
      
      multimoment[i] = (I1 - I2) / I2

  pulses['multimoment'] = multimoment # <= MULTIMOMENT

  return
Beispiel #4
0
 def __init__(self, fits_filenm):
     self.fits_filenm = fits_filenm
     self.basefilenm = fits_filenm[:fits_filenm.find(".fits")]
     self.dsbasefilenm = fits_filenm[:fits_filenm.rfind("_")]
     fitshandle=pyfits.open(fits_filenm)
     self.MJD = fitshandle[0].header['STT_IMJD']+fitshandle[0].header['STT_SMJD']/86400.0+fitshandle[0].header['STT_OFFS']/86400.0
     self.nchans = fitshandle[0].header['OBSNCHAN']
     self.ra_string = fitshandle[0].header['RA']
     self.dec_string = fitshandle[0].header['DEC']
     self.str_coords = "J"+"".join(self.ra_string.split(":")[:2])
     self.str_coords += "".join(self.dec_string.split(":")[:2])
     self.nbits=fitshandle[0].header['BITPIX']
     
     self.raw_N=fitshandle[1].header['NAXIS2']*fitshandle[1].header['NSBLK']
     self.dt=fitshandle[1].header['TBIN']*1000000
     self.raw_T = self.raw_N * self.dt
     self.N = raw_N
     if self.dt == 163.84:
       self.N=self.N/2
     self.T = self.N * self.dt
     self.srcname=fitshandle[0].header['SRC_NAME']
     # Determine the average barycentric velocity of the observation
     self.baryv = presto.get_baryv(self.ra_string, self.dec_string,
                                   self.MJD, self.T, obs="GB")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_dir,
                                   str(int(self.MJD)),
                                   self.srcname)
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
Beispiel #5
0
 def __init__(self, fil_filenm):
     self.fil_filenm = fil_filenm
     self.basefilenm = fil_filenm[:fil_filenm.find(".fil")]
     filhdr, hdrlen = sigproc.read_header(fil_filenm)
     self.MJD = filhdr['tstart']
     self.nchans = filhdr['nchans']
     self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
     self.ra_string = pu.coord_to_string(*pu.rad_to_hms(self.ra_rad))
     self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
     self.dec_string = pu.coord_to_string(*pu.rad_to_dms(self.dec_rad))
     self.str_coords = "J"+"".join(self.ra_string.split(":")[:2])
     if self.dec_rad >= 0.0:  self.str_coords += "+"
     self.str_coords += "".join(self.dec_string.split(":")[:2])
     self.az = filhdr['az_start']
     self.el = 90.0-filhdr['za_start']
     fillen = os.stat(fil_filenm)[6]
     self.raw_N = (fillen-hdrlen)/(filhdr['nbits']/8)/filhdr['nchans']
     self.dt = filhdr['tsamp']
     self.raw_T = self.raw_N * self.dt
     self.N = orig_N
     self.T = self.N * self.dt
     # Determine the average barycentric velocity of the observation
     self.baryv = presto.get_baryv(self.ra_string, self.dec_string,
                                   self.MJD, self.T, obs="GB")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_dir,
                                   str(int(self.MJD)),
                                   self.str_coords)
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
Beispiel #6
0
assert(rs[nn/2]==0.0)
assert(pr[nn/2]==1.0)
assert(round(pz[nn/2]-0.227675, 6)==0)
assert(round(pw[nn/2]-0.019462, 6)==0)
print "success"

print "Testing angle functions...",
dd1 = 15.25
dd2 = presto.dms2rad(*presto.deg2dms(dd1))*presto.RADTODEG
assert(round(dd1-dd2, 12)==0)
dd1 = -0.5
dd2 = presto.dms2rad(*presto.deg2dms(dd1))*presto.RADTODEG
assert(round(dd1-dd2, 12)==0)
hh1 = 12.125
hh2 = presto.hms2rad(*presto.hours2hms(hh1))*presto.RADTODEG/15.0
assert(round(hh1-hh2, 12)==0)
hh1 = -0.5
hh2 = presto.hms2rad(*presto.hours2hms(hh1))*presto.RADTODEG/15.0
assert(round(hh1-hh2, 12)==0)
ang = presto.sphere_ang_diff(10.0*presto.DEGTORAD, 10.0*presto.DEGTORAD,
                             50.0*presto.DEGTORAD, -10.0*presto.DEGTORAD)
assert(round(160334.960*presto.ARCSEC2RAD-ang, 7)==0)
print "success"

print "Testing get_baryv (barycenter)...",
vavg1 = presto.get_baryv("18:24:32.9520", "-24:52:12.0000",
                         56421.44222222222222, 214.5386496, obs="GB")
vavg2 = -7.2069293455783169e-05
assert(round(vavg1-vavg2, 10)==0)
print "success"
Beispiel #7
0
assert (round(pw[nn / 2] - 0.019462, 6) == 0)
print "success"

print "Testing angle functions...",
dd1 = 15.25
dd2 = presto.dms2rad(*presto.deg2dms(dd1)) * presto.RADTODEG
assert (round(dd1 - dd2, 12) == 0)
dd1 = -0.5
dd2 = presto.dms2rad(*presto.deg2dms(dd1)) * presto.RADTODEG
assert (round(dd1 - dd2, 12) == 0)
hh1 = 12.125
hh2 = presto.hms2rad(*presto.hours2hms(hh1)) * presto.RADTODEG / 15.0
assert (round(hh1 - hh2, 12) == 0)
hh1 = -0.5
hh2 = presto.hms2rad(*presto.hours2hms(hh1)) * presto.RADTODEG / 15.0
assert (round(hh1 - hh2, 12) == 0)
ang = presto.sphere_ang_diff(10.0 * presto.DEGTORAD, 10.0 * presto.DEGTORAD,
                             50.0 * presto.DEGTORAD, -10.0 * presto.DEGTORAD)
assert (round(160334.960 * presto.ARCSEC2RAD - ang, 7) == 0)
print "success"

print "Testing get_baryv (barycenter)...",
vavg1 = presto.get_baryv("18:24:32.9520",
                         "-24:52:12.0000",
                         56421.44222222222222,
                         214.5386496,
                         obs="GB")
vavg2 = -7.2069293455783169e-05
assert (round(vavg1 - vavg2, 10) == 0)
print "success"
Beispiel #8
0
 def __init__(self, fil_filenm):
     self.fil_filenm = fil_filenm
     self.basefilenm = fil_filenm.rstrip(".fil")
     self.beam = int(self.basefilenm[-1])
     filhdr, self.hdrlen = sigproc.read_header(fil_filenm)
     self.orig_filenm = filhdr['rawdatafile']
     self.MJD = filhdr['tstart']
     self.nchans = filhdr['nchans']
     self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
     self.ra_string = psr_utils.coord_to_string(\
         *psr_utils.rad_to_hms(self.ra_rad))
     self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
     self.dec_string = psr_utils.coord_to_string(\
         *psr_utils.rad_to_dms(self.dec_rad))
     self.az = filhdr['az_start']
     self.el = 90.0 - filhdr['za_start']
     self.BW = abs(filhdr['foff']) * filhdr['nchans']
     self.dt = filhdr['tsamp']
     self.orig_N = sigproc.samples_per_file(fil_filenm, filhdr, self.hdrlen)
     self.orig_T = self.orig_N * self.dt
     self.N = psr_utils.choose_N(self.orig_N)
     self.T = self.N * self.dt
     # Update the RA and DEC from the database file if required
     newposn = read_db_posn(self.orig_filenm, self.beam)
     if newposn is not None:
         self.ra_string, self.dec_string = newposn
         # ... and use them to update the filterbank file
         fix_fil_posn(fil_filenm, self.hdrlen, self.ra_string,
                      self.dec_string)
     # Determine the average barycentric velocity of the observation
     self.baryv = presto.get_baryv(self.ra_string,
                                   self.dec_string,
                                   self.MJD,
                                   self.T,
                                   obs="AO")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_directory,
                                   str(int(self.MJD)), self.basefilenm[:-2],
                                   str(self.beam))
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.subbanding_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
 def bary(sample):
   MJD = header['STT_IMJD'] + header['STT_SMJD'] / 86400.
   v = presto.get_baryv(header['RA'],header['DEC'],MJD,1800.,obs='LF')
   sample += np.round(sample*v).astype(int)
   return sample
Beispiel #10
0
def main():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-n",
                      "--number",
                      type="int",
                      dest="nM",
                      default=40,
                      help="Number of points in each chunk (millions)")
    parser.add_option("-o",
                      "--outdir",
                      type="string",
                      dest="outdir",
                      default=".",
                      help="Output directory to store results")
    parser.add_option("-d",
                      "--workdir",
                      type="string",
                      dest="workdir",
                      default=".",
                      help="Working directory for search")
    parser.add_option("-l",
                      "--flo",
                      type="float",
                      dest="flo",
                      default=10.0,
                      help="Low frequency (Hz) to search")
    parser.add_option("-f",
                      "--frac",
                      type="float",
                      dest="frac",
                      default=0.5,
                      help="Fraction to overlap")
    parser.add_option("-x",
                      "--fhi",
                      type="float",
                      dest="fhi",
                      default=10000.0,
                      help="High frequency (Hz) to search")
    parser.add_option("-z",
                      "--zmax",
                      type="int",
                      dest="zmax",
                      default=160,
                      help="Maximum fourier drift (bins) to search")
    parser.add_option("-w",
                      "--wmax",
                      type="int",
                      dest="wmax",
                      default=400,
                      help="Maximum fourier drift deriv (bins) to search")
    parser.add_option("-a",
                      "--numharm",
                      type="int",
                      dest="numharm",
                      default=4,
                      help="Number of harmonics to sum when searching")
    parser.add_option("-s",
                      "--sigma",
                      type="float",
                      dest="sigma",
                      default=2.0,
                      help="Cutoff sigma to consider a candidate")
    (options, args) = parser.parse_args()
    if (options.outdir[-1] != "/"):
        options.outdir = options.outdir + "/"
    if (options.workdir != '.'):
        chdir(options.workdir)
    if (options.nM >= 1000000):
        if (options.nM % 1000000):
            print "If you specify --num nM to be > 1000000, it must be divisible by 1000000."
            exit(1)
    else:
        options.nM *= 1000000
    short_nM = options.nM / 1000000

    # The basename of the data files
    if argv[1].endswith(".dat"):
        basename = "../" + argv[1][:-4]
    else:
        basename = "../" + argv[1]

    # Get the bird file (the first birdie file in the directory!)
    birdname = glob("../*.birds")
    if birdname:
        birdname = birdname[0]

    outnamebase = options.outdir + basename[3:]
    inf = read_inffile(basename)
    idata = infodata.infodata(basename + ".inf")
    N = inf.N
    t0i = inf.mjd_i
    t0f = inf.mjd_f
    num = 0
    point = 0
    T = options.nM * inf.dt / 86400.0
    baryv = get_baryv(idata.RA, idata.DEC, idata.epoch, T, obs='GB')
    print "Baryv = ", baryv
    inf.N = options.nM
    inf.numonoff = 0
    nM = options.nM / 1000000
    while (point + options.nM < N):
        pM = point / 1000000
        outname = basename[3:] + '_%03dM' % nM + '_%02d' % num
        stdout.write('\n' + outname + '\n\n')
        inf.name = outname
        tstartf = inf.mjd_f + num * T * options.frac
        if (tstartf > 1.0):
            tstartf = tstartf - 1.0
            inf.mjd_i = inf.mjd_i + 1
        inf.mjd_f = tstartf
        writeinf(inf)
        myexecute('dd if=' + basename + '.dat of=' + outname +
                  '.dat bs=4000000 skip=' + ` pM ` + ' count=' + ` nM `)
        myexecute('realfft ' + outname + '.dat')
        myexecute('rm -f ' + outname + '.dat')
        myexecute('cp ' + birdname + ' ' + outname + '.birds')
        myexecute('makezaplist.py ' + outname + '.birds')
        myexecute('rm -f ' + outname + '.birds')
        myexecute('zapbirds -zap -zapfile ' + outname +
                  '.zaplist -baryv %g ' % baryv + outname + '.fft')
        myexecute('rm -f ' + outname + '.zaplist')
        myexecute(
            'accelsearch -sigma %.2f -zmax %d -wmax %d -numharm %d -flo %f -fhi %f '
            % (options.sigma, options.zmax, options.wmax, options.numharm,
               options.flo, options.fhi) + outname + '.fft')
        myexecute('rm ' + outname + '.fft ' + outname +
                  '_ACCEL_%d.txtcand' % options.zmax)
        myexecute('cp ' + outname + '.inf ' + options.outdir)
        num = num + 1
        point = point + int(options.nM * options.frac)
Beispiel #11
0
def DynamicSpectrum(puls,filename,bary=True,mask=False,ax=False,res=RES,f_min=F_MIN,f_max=F_MAX):
  '''
  puls = {'DM':, 'Sigma':, 'Time':, 'Sample':, 'Downfact':, 'Downsample':, }
  '''
  if not os.path.isfile(filename): return
  
  sample = puls['Sample'] * puls['Downsample']
  
  if bary:
    file_type, header = Utilities.read_header(filename)
    if file_type == 'fil':
      MJD = header['tstart']
      RA = str(header['src_raj'])
      if header['src_raj'] >= 100000:
        RA = '{hh}:{mm}:{ss}'.format(hh=RA[:2], mm=RA[2:4], ss=RA[4:])
      else:
        RA = '{hh}:{mm}:{ss}'.format(hh=RA[:1], mm=RA[1:3], ss=RA[3:])
      DEC = str(header['src_dej'])
      if header['src_dej'] >= 100000:
        DEC = '{hh}:{mm}:{ss}'.format(hh=DEC[:2], mm=DEC[2:4], ss=DEC[4:])
      else:
        DEC = '{hh}:{mm}:{ss}'.format(hh=DEC[:1], mm=DEC[1:3], ss=DEC[3:])
    elif file_type == 'fits':
      MJD = header['STT_IMJD'] + header['STT_SMJD'] / 86400.
      RA = header['RA']
      DEC = header['DEC']
    else: raise KeyError('File format not supported')
    
    try: v = presto.get_baryv(RA,DEC,MJD,1800.,obs='LF')
    except NameError: 
      logging.warning("LSPplot - Additional modules missing")
      return
    sample += np.round(sample*v).astype(int)
    
  if isinstance(puls['Downfact'], float):
    duration = np.int(np.round(puls.Duration/res)) * puls['Downsample']
  else: duration = puls['Downsample'] * puls['Downfact']
  spectra_border = 20
  offset = duration*spectra_border
  
  #Load the spectrum
  if filename.endswith('.fits'):
    spectrum = read_fits(filename,puls['DM'],sample,duration,offset,RFI_reduct=True)
  elif filename.endswith('.fil'):
    spectrum = read_filterbank(filename,puls['DM'],sample,duration,offset,RFI_reduct=True,mask=mask)
  else:
    print "File type not recognised. This script only works with fits or fil files."
    return
  
  #De-dispersion
  freq = np.linspace(f_min,f_max,spectrum.shape[1])
  time = (4149 * puls['DM'] * (f_max**-2 - np.power(freq,-2)) / res).round().astype(np.int)
  for i in range(spectrum.shape[1]):
    spectrum[:,i] = np.roll(spectrum[:,i], time[i])
  spectrum = spectrum[:2*offset+duration]
  
  spectrum = np.mean(np.reshape(spectrum,[spectrum.shape[0]/duration,duration,spectrum.shape[1]]),axis=1)
  div = closest_int_above(spectrum.shape[1], n=64)
  spectrum = np.mean(np.reshape(spectrum,[spectrum.shape[0],spectrum.shape[1]/div,div]),axis=2)
  
  fig = plt.figure(figsize=(16,9))
  ax1 = plt.subplot2grid((4,1),(0,0),rowspan=3)
  ax2 = plt.subplot2grid((4,1),(3,0))

  #if not ax: ax = plt.subplot(111)
  extent = [(sample-offset)*res,(sample+duration+offset)*res,f_min,f_max]
  ax1.imshow(spectrum.T,cmap='Greys',origin="lower",aspect='auto',interpolation='nearest',extent=extent)
  ax1.scatter((sample+duration/2)*res,f_min+1,marker='^',s=1000,c='r')
  ax1.axis(extent)
  ax1.set_ylabel('Frequency (MHz)')
  
  flux =  spectrum.mean(axis=1)
  flux -= np.median(flux)
  flux /= flux.max()
  ax2.plot(flux,'k-')
  ax2.set_ylabel('Flux (rel.)')
  ax2.set_xlabel('Time (s)')
  
  return