Пример #1
0
def dynamicSpectrum(filfn,
                    start_time,
                    time_window,
                    timeFactor=1,
                    freqFactor=1,
                    dm=0.,
                    applyGauss=False,
                    fGauss=1.,
                    tGauss=1.,
                    rfi=[]):
    """Return a time-frequency cut-out from a filterbank file
    filfn: str, filterbank filename
    start_time: float, start time in seconds
    time_window: float, time window in seconds
    timeFactor: int, decimation factor
    freqFactor: int, decimation factor
    dm: float, dispersion measure
    applyGauss: bool, apply a Gaussian filter
    fGauss: float, Gaussian filte in frequency
    tGauss: float, Gaussian filte in time
    rfi: list of int pairs, start and stop index of freq channels to replace with noise

    returns: 2-D float array
    """
    fil = filterbankio.Filterbank(filfn)

    tInt = fil.header['tsamp']  # get tInt
    freqsHz = fil.freqs * 1e6  # generate array of freqs in Hz

    waterfall = np.reshape(
        fil.data, (fil.data.shape[0],
                   fil.data.shape[2]))  # reshape to (n integrations, n freqs)

    ddwaterfall = dedispersion.incoherent(
        freqsHz, waterfall, tInt, dm, boundary='wrap')  # apply dedispersion

    # Time Decimation
    # average down by N time samples
    if waterfall.shape[0] % timeFactor == 0:
        decwaterfall = waterfall.reshape(waterfall.shape[0] / timeFactor,
                                         timeFactor,
                                         waterfall.shape[1]).mean(axis=1)
        decddwaterfall = ddwaterfall.reshape(ddwaterfall.shape[0] / timeFactor,
                                             timeFactor,
                                             ddwaterfall.shape[1]).mean(axis=1)
        tInt *= timeFactor
    else:
        print 'WARNING: %i time samples is NOT divisible by %i, zero-padding spectrum to usable size' % (
            waterfall.shape[0], timeFactor)
        zeros = np.zeros((timeFactor - (waterfall.shape[0] % timeFactor),
                          waterfall.shape[1]))
        decwaterfall = np.concatenate((waterfall, zeros))
        decddwaterfall = np.concatenate((ddwaterfall, zeros))
        decwaterfall = decwaterfall.reshape(decwaterfall.shape[0] / timeFactor,
                                            timeFactor,
                                            decwaterfall.shape[1]).mean(axis=1)
        decddwaterfall = decddwaterfall.reshape(
            decddwaterfall.shape[0] / timeFactor, timeFactor,
            decddwaterfall.shape[1]).mean(axis=1)
        tInt *= timeFactor

# Frequency Decimation
    if decwaterfall.shape[1] % freqFactor == 0:
        decwaterfall = decwaterfall.reshape(decwaterfall.shape[0],
                                            decwaterfall.shape[1] / freqFactor,
                                            freqFactor).mean(axis=2)
        decddwaterfall = decddwaterfall.reshape(
            decddwaterfall.shape[0], decddwaterfall.shape[1] / freqFactor,
            freqFactor).mean(axis=2)
        freqsHz = freqsHz[::freqFactor]
    else:
        print 'WARNING: %i frequency channels is NOT divisible by %i, ignoring option' % (
            decwaterfall.shape[1], freqFactor)

    # cut out region
    if start_time is None: startIdx = 0
    else: startIdx = int(start_time / tInt)

    if time_window is None:
        endIdx = decwaterfall.shape[0]
    else:
        endIdx = startIdx + int(time_window / tInt)
        if endIdx > decwaterfall.shape[0]:
            print 'Warning: time window (-w) in conjunction with start time (-s) results in a window extending beyond the filterbank file, clipping to maximum size'
            endIdx = decwaterfall.shape[0]

    # RFI replacement
    rfiMask = np.zeros_like(decddwaterfall)
    for freqPair in rfi:
        rfiMask[:, freqPair[0]:freqPair[1]] = 1.
    dsMean = np.ma.array(decddwaterfall, mask=rfiMask).mean()
    dsStd = np.ma.array(decddwaterfall, mask=rfiMask).std()
    for freqPair in rfi:
        decddwaterfall[:, freqPair[0]:freqPair[1]] = np.random.normal(
            dsMean,
            dsStd,
            size=decddwaterfall[:, freqPair[0]:freqPair[1]].shape)
        decwaterfall[:, freqPair[0]:freqPair[1]] = np.random.normal(
            dsMean, dsStd, size=decwaterfall[:, freqPair[0]:freqPair[1]].shape)
        #decddwaterfall[:,freqPair[0]:freqPair[1]] = np.zeros(decddwaterfall[:,freqPair[0]:freqPair[1]].shape)
        #decwaterfall[:,freqPair[0]:freqPair[1]] = np.zeros(decwaterfall[:,freqPair[0]:freqPair[1]].shape)

    decwaterfall = decwaterfall[startIdx:endIdx, :]
    decddwaterfall = decddwaterfall[startIdx:endIdx, :]

    if applyGauss:
        gaussFilter = gaussianFilter(decddwaterfall.shape, tGauss, fGauss)
        decddwaterfall = convolveTaper(gaussFilter, decddwaterfall)
        decwaterfall = convolveTaper(gaussFilter, decwaterfall)

    return decwaterfall, decddwaterfall, tInt, freqsHz / 1e6
Пример #2
0
def dmSpace(filfn,
            start_time,
            time_window,
            minDM,
            maxDM,
            dmStep,
            timeFactor=1,
            rfi=[]):
    """Return a time-frequency cut-out from a filterbank file
    filfn: str, filterbank filename
    start_time: float, start time in seconds
    time_window: float, time window in seconds
    timeFactor: int, decimation factor
    minDM: int, minimum DM trial
    maxDM: int, maximum DM trial
    dmStep: int, trail step size
    rfi: list of int pairs, start and stop index of freq channels to replace with noise

    returns: 2-D float array
    """
    fil = filterbankio.Filterbank(filfn)

    tInt = fil.header['tsamp']  # get tInt
    freqsHz = fil.freqs * 1e6  # generate array of freqs in Hz

    print 'Maximum delay (ms) based on maximum DM (%f):' % maxDM, deltat(
        maxDM, freqsHz[0] / 1e6, freqsHz[-1] / 1e6)

    testDMs = np.arange(minDM, maxDM, dmStep)

    waterfall = np.reshape(
        fil.data, (fil.data.shape[0],
                   fil.data.shape[2]))  # reshape to (n integrations, n freqs)

    # cut out region
    if start_time is None: startIdx = 0
    else: startIdx = int(start_time / tInt)

    if time_window is None:
        endIdx = waterfall.shape[0]
    else:
        endIdx = startIdx + int(time_window / tInt)
        if endIdx > waterfall.shape[0]:
            print 'Warning: time window (-w) in conjunction with start time (-s) results in a window extending beyond the filterbank file, clipping to maximum size'
            endIdx = waterfall.shape[0]

    # RFI replacement
    rfiMask = np.zeros_like(waterfall)
    for freqPair in rfi:
        rfiMask[:, freqPair[0]:freqPair[1]] = 1.
    dsMean = np.ma.array(waterfall, mask=rfiMask).mean()
    dsStd = np.ma.array(waterfall, mask=rfiMask).std()
    for freqPair in rfi:
        waterfall[:, freqPair[0]:freqPair[1]] = np.random.normal(
            dsMean, dsStd, size=waterfall[:, freqPair[0]:freqPair[1]].shape)

    waterfall = waterfall[startIdx:endIdx, :]

    dmSpaceArr = np.zeros((testDMs.shape[0], waterfall.shape[0]))

    for dmid, dm in enumerate(testDMs):
        dmSpaceArr[dmid, :] = np.mean(dedispersion.incoherent(freqsHz,
                                                              waterfall,
                                                              tInt,
                                                              dm,
                                                              boundary='wrap'),
                                      axis=1)

# Time Decimation
# average down by N time samples
    if dmSpaceArr.shape[1] % timeFactor == 0:
        decdmSpaceArr = dmSpaceArr.reshape(dmSpaceArr.shape[0],
                                           dmSpaceArr.shape[1] / timeFactor,
                                           timeFactor).mean(axis=2)
        tInt *= timeFactor
    else:
        print 'WARNING: %i time samples is NOT divisible by %i, zero-padding spectrum to usable size' % (
            dmSpaceArr.shape[1], timeFactor)
        zeros = np.zeros((dmSpaceArr.shape[0],
                          timeFactor - (dmSpaceArr.shape[1] % timeFactor)))
        decdmSpaceArr = np.concatenate((dmSpaceArr, zeros), axis=1)
        decdmSpaceArr = decdmSpaceArr.reshape(
            decdmSpaceArr.shape[0], decdmSpaceArr.shape[1] / timeFactor,
            timeFactor).mean(axis=2)
        decdmSpaceArr = decdmSpaceArr[:, :-1]  # drop last integration
        tInt *= timeFactor

    return decdmSpaceArr, tInt, freqsHz / 1e6
Пример #3
0
        default=None,
        help=
        'Average in time by N samples, similar to SIGPROC decimate -t option')
    o.add_option('-f',
                 '--freq',
                 dest='freqFactor',
                 type='int',
                 default=1,
                 help='Average in freq by N samples')
    o.add_option('--dark',
                 dest='dark',
                 action='store_true',
                 help='Plot with a dark background style')
    opts, args = o.parse_args(sys.argv[1:])

    fil = filterbankio.Filterbank(args[0])

    tInt = fil.header['tsamp']  # get tInt
    freqsHz = fil.freqs * 1e6  # generate array of freqs in Hz

    waterfall = np.reshape(
        fil.data, (fil.data.shape[0],
                   fil.data.shape[2]))  # reshape to (n integrations, n freqs)

    if np.isnan(waterfall).any():
        waterfall = np.nan_to_num(waterfall).astype('float64')

    if not opts.timeFactor is None:  # average down by N time samples, waterfall.shape[0] must be divisible by N
        if waterfall.shape[0] % opts.timeFactor == 0:
            waterfall = waterfall.reshape(waterfall.shape[0] / opts.timeFactor,
                                          opts.timeFactor,
Пример #4
0
    o = OptionParser()
    o.set_usage('%prog [options] FIL') #tells you the syntax for running the script
    o.set_description(__doc__)
    o.add_option('-d', '--dm', dest='dm', default=0., type='float',
        help='Despersion Measure to correct, default: 0.')
    o.add_option('-v', '--verbose', dest='verbose', action='store_true',
        help='Verbose output')
    o.add_option('-t', '--time', dest='timeFactor', type='int', default=2,
        help='Average in time by N samples, similar to SIGPROC decimate -t option')
    o.add_option('-M', '--meta', dest='meta', default=None,
        help='Metadata pickle file used to print buffer stats, generated in generateDedispFigures.py')
    opts, args = o.parse_args(sys.argv[1:]) #sys.argv[0] is the name of the program being executed

    dm = opts.dm #set dm as input

    fil = filterbankio.Filterbank(args[0]) #args[] is list full of declared arguments

    tInt = fil.header['tsamp'] # get tInt
    freqsHz = fil.freqs * 1e6 # generate array of freqs in Hz

    waterfall = np.reshape(fil.data, (fil.data.shape[0], fil.data.shape[2])) # reshape to (n integrations, n freqs)

    #create max size array with fil.data values and zeros
    if waterfall.shape[0] != 32768: # expand array to be full size
        zeros = np.zeros((32768 - waterfall.shape[0], waterfall.shape[1]))
        waterfall = np.concatenate((waterfall, zeros))

    if np.isnan(waterfall).any(): waterfall = np.nan_to_num(waterfall).astype('float64') #some sort of data cleaning thing

    if not opts.timeFactor is None: # average down by N time samples, waterfall.shape[0] must be divisible by N
        if waterfall.shape[0] % opts.timeFactor==0:
Пример #5
0
    o.add_option(
        '--prefix',
        dest='prefix',
        default='bandpass',
        help='Prefix of files to write bandpass to, default: bandpass')
    o.add_option('--minmax',
                 dest='minmax',
                 action='store_true',
                 help='Plot the min/max bounds')
    opts, args = o.parse_args(sys.argv[1:])

    if not opts.nodisplay or opts.savefig:
        fig = plt.figure(figsize=(12, 6))  # (width, height)

    for fbIdx, fbFn in enumerate(args):
        fil = filterbankio.Filterbank(fbFn)

        tInt = fil.header['tsamp']  # get tInt
        freqsHz = fil.freqs * 1e6  # generate array of freqs in Hz

        waterfall = np.reshape(
            fil.data,
            (fil.data.shape[0],
             fil.data.shape[2]))  # reshape to (n integrations, n freqs)

        # Select time subsets to plot and save to file
        if opts.start_time is None:
            startIdx = 0
        else:
            startIdx = int(opts.start_time / tInt)
            if startIdx > waterfall.shape[0]: