Пример #1
0
def resetdefaults():
    """
    resetdefaults():
        Reset global plotting variables to default values.
    """
    global ppgplot_font_, ppgplot_linestyle_, ppgplot_linewidth_, \
           ppgplot_color_, ppgplot_font_size_
    ppgplot.pgscf(ppgplot_font_)
    ppgplot.pgsls(ppgplot_linestyle_)
    ppgplot.pgslw(ppgplot_linewidth_)
    ppgplot.pgsci(ppgplot_colors_[ppgplot_color_])
    ppgplot.pgsch(ppgplot_font_size_)
Пример #2
0
def resetdefaults():
    """
    resetdefaults():
        Reset global plotting variables to default values.
    """
    global ppgplot_font_, ppgplot_linestyle_, ppgplot_linewidth_, \
           ppgplot_color_, ppgplot_font_size_
    ppgplot.pgscf(ppgplot_font_)
    ppgplot.pgsls(ppgplot_linestyle_)
    ppgplot.pgslw(ppgplot_linewidth_)
    ppgplot.pgsci(ppgplot_colors_[ppgplot_color_])
    ppgplot.pgsch(ppgplot_font_size_)
Пример #3
0
def resetdefaults():
    """
    resetdefaults():
        Reset global plotting variables to default values.
    """
    global ppgplot_font_, ppgplot_linestyle_, ppgplot_linewidth_, \
           ppgplot_color_, ppgplot_font_size_
    ppgplot.pgscf(ppgplot_font_)
    ppgplot.pgsls(ppgplot_linestyle_)
    ppgplot.pgslw(ppgplot_linewidth_)
    ppgplot.pgsci(ppgplot_colors_[ppgplot_color_])
    # My little add-on to switch the background to white
    reset_colors()
    ppgplot.pgsch(ppgplot_font_size_)
Пример #4
0
def prepplot(rangex, rangey, title=None, labx=None, laby=None, \
             rangex2=None, rangey2=None, labx2=None, laby2=None, \
             logx=0, logy=0, logx2=0, logy2=0, font=ppgplot_font_, \
             fontsize=ppgplot_font_size_, id=0, aspect=1, ticks='in', \
             panels=[1,1], device=ppgplot_device_):
    """
    prepplot(rangex, rangey, ...)
        Open a PGPLOT device for plotting.
            'rangex' and 'rangey' are sequence objects giving min and
                max values for each axis.
        The optional entries are:
            title:    graph title                 (default = None)   
            labx:     label for the x-axis        (default = None)   
            laby:     label for the y-axis        (default = None)   
            rangex2:  ranges for 2nd x-axis       (default = None)   
            rangey2:  ranges for 2nd y-axis       (default = None)   
            labx2:    label for the 2nd x-axis    (default = None)   
            laby2:    label for the 2nd y-axis    (default = None)   
            logx:     make the 1st x-axis log     (default = 0 (no))
            logy:     make the 1st y-axis log     (default = 0 (no))
            logx2:    make the 2nd x-axis log     (default = 0 (no))
            logy2:    make the 2nd y-axis log     (default = 0 (no))
            font:     PGPLOT font to use          (default = 1 (normal))
            fontsize: PGPLOT font size to use     (default = 1.0 (normal))
            id:       Show ID line on plot        (default = 0 (no)) 
            aspect:   Aspect ratio                (default = 1 (square))
            ticks:    Ticks point in or out       (default = 'in')   
            panels:   Number of subpanels [r,c]   (default = [1,1])
            device:   PGPLOT device to use        (default = '/XWIN')
        Note:  Many default values are defined in global variables
            with names like ppgplot_font_ or ppgplot_device_.
    """
    global ppgplot_dev_open_, ppgplot_dev_prep_
    # Check if we will use second X or Y axes
    # Note:  if using a 2nd X axis, the range should correspond
    #   to the minimum and maximum values of the 1st X axis.  If
    #   using a 2nd Y axis, the range should correspond to the
    #   scalerange() values of the 1st Y axis.
    if rangex2 is None:
        rangex2=rangex
        otherxaxis=0
    else: otherxaxis=1
    if rangey2 is None:
        rangey2=rangey
        otheryaxis=0
    else: otheryaxis=1
    # Open the plot device
    if (not ppgplot_dev_open_):
        ppgplot.pgopen(device)
	# My little add-on to switch the background to white
	if device == '/XWIN':
	    reset_colors()
	if device == '/AQT':
	    ppgplot.pgsci(0)
        # Let the routines know that we already have a device open
        ppgplot_dev_open_ = 1
        # Set the aspect ratio
        ppgplot.pgpap(0.0, aspect)
        if (panels != [1,1]):
            # Set the number of panels
            ppgplot.pgsubp(panels[0], panels[1])
            ppgplot.pgpage()
    # Choose the font  
    ppgplot.pgscf(font)
    # Choose the font size
    ppgplot.pgsch(fontsize)
    # Choose the font size
    ppgplot.pgslw(ppgplot_linewidth_)
    # Plot the 2nd axis if needed first
    if otherxaxis or otheryaxis:
        ppgplot.pgvstd()
        ppgplot.pgswin(rangex2[0], rangex2[1], rangey2[0], rangey2[1])
        # Decide how the axes will be drawn
        if ticks=='in': env = "CMST"
        else: env = "CMSTI"
        if logx2: lxenv='L'
        else: lxenv=''
        if logy2: lyenv='L'
        else: lyenv=''
        if otherxaxis and otheryaxis:
            ppgplot.pgbox(env+lxenv, 0.0, 0, env+lyenv, 0.0, 0)
        elif otheryaxis:
            ppgplot.pgbox("", 0.0, 0, env+lyenv, 0.0, 0)
        else:
            ppgplot.pgbox(env+lxenv, 0.0, 0, "", 0.0, 0)
    # Now setup the primary axis
    ppgplot.pgvstd()
    ppgplot.pgswin(rangex[0], rangex[1], rangey[0], rangey[1])
    # Decide how the axes will be drawn
    if ticks=='in': env = "ST"
    else: env = "STI"
    if logx: lxenv='L'
    else: lxenv=''
    if logy: lyenv='L'
    else: lyenv=''
    if otherxaxis and otheryaxis:
        ppgplot.pgbox("BN"+env+lxenv, 0.0, 0, "BN"+env+lyenv, 0.0, 0)
    elif otheryaxis:
        ppgplot.pgbox("BCN"+env+lxenv, 0.0, 0, "BN"+env+lyenv, 0.0, 0)
    elif otherxaxis:
        ppgplot.pgbox("BN"+env+lxenv, 0.0, 0, "BCN"+env+lyenv, 0.0, 0)
    else:
        ppgplot.pgbox("BCN"+env+lxenv, 0.0, 0, "BCN"+env+lyenv, 0.0, 0)
    # My little add-on to switch the background to white
    if device == '/AQT' or device == '/XWIN':
	reset_colors()
    # Add labels
    if not title is None: ppgplot.pgmtxt("T", 3.2, 0.5, 0.5, title)
    ppgplot.pgmtxt("B", 3.0, 0.5, 0.5, labx)
    ppgplot.pgmtxt("L", 2.6, 0.5, 0.5, laby)
    if otherxaxis: ppgplot.pgmtxt("T", 2.0, 0.5, 0.5, labx2)
    if otheryaxis: ppgplot.pgmtxt("R", 3.0, 0.5, 0.5, laby2)
    # Add ID line if required
    if (id==1): ppgplot.pgiden()
    # Let the routines know that we have already prepped the device
    ppgplot_dev_prep_ = 1
Пример #5
0
def setmask(command, data, cdata):
    """
    Sets the mask on a dset and dumps a file containing the mask which can be applied
    to other dsets using 'appmask'. This is an interactive routine which will request
    input from the user and is better not used in batch processing. Repeated calls of
    this routine can be used to build complex masks. The masks are always applied in
    the original order so that you can mask then partially unmask for example. Note that
    whatever slot you choose to define the mask will always end up masked; if you don't
    want this you may want to make a copy.

    Interactive usage:

    setmask slot mfile append [device reset x1 x2 y1 y2] mask type

    Arguments:

    slot      -- an example slot to plot.
    mfile     -- mask file
    append    -- append to an old mask file if possible
    device    -- plot device, e.g. '/xs'
    reset     -- rest plot limits or not
    x1        -- left X plot limit
    x2        -- right X plot limit
    y1        -- bottom Y plot limit
    y2        -- top Y plot limit
    mask      -- mask 'M', or unmask 'U' or quit 'Q'.
    type      -- type of mask: 'X' masks using ranges in X

    Mask types:

    X  -- mask a range in X
    Y  -- mask a range in Y
    I  -- mask a range of pixel indices
    P  -- mask in 'phase', i.e. a range that repeats periodically.
    """

    import trm.dnl.mask as mask

    # generate arguments
    inpt = inp.Input(DINT_ENV, DINT_DEF, inp.clist(command))

    # register parameters
    inpt.register('slot',   inp.Input.LOCAL, inp.Input.PROMPT)
    inpt.register('mfile',  inp.Input.GLOBAL,  inp.Input.PROMPT)
    inpt.register('append', inp.Input.LOCAL,  inp.Input.PROMPT)
    inpt.register('device', inp.Input.LOCAL,  inp.Input.HIDE)
    inpt.register('reset',  inp.Input.LOCAL,  inp.Input.HIDE)
    inpt.register('x1',     inp.Input.LOCAL,  inp.Input.HIDE)
    inpt.register('x2',     inp.Input.LOCAL,  inp.Input.HIDE)
    inpt.register('y1',     inp.Input.LOCAL,  inp.Input.HIDE)
    inpt.register('y2',     inp.Input.LOCAL,  inp.Input.HIDE)
    inpt.register('mask',   inp.Input.LOCAL,  inp.Input.PROMPT)
    inpt.register('type',   inp.Input.LOCAL,  inp.Input.PROMPT)

    # get inputs
    slots  = inpt.get_value('slot', 'slot to plot for mask definition', '1')
    slist  = interp_slots(slots, True, data, nfind=1)
    dset   = data[slist[0]]

    device = inpt.get_value('device', 'plot device', '/xs')

    # mask file
    mfile  = inpt.get_value('mfile',  'mask file to save results to', subs.Fname('mask','.msk', subs.Fname.NEW))
    append = inpt.get_value('append', 'add to an old mask file if possible', True)

    if append and mfile.exists():
        mptr  = open(mfile,'rb')
        gmask = pickle.load(mptr)
        gmask.app_mask(dset)
        mptr.close()
    else:
        gmask = mask.Gmask()

    # other parameters
    reset = inpt.get_value('reset',   'reset plot limits automatically?', True)

    # compute default limits
    (x1,x2,y1,y2) = dset.plimits()
    if (x2 - x1) < (x1+x2)/2./100.:
        xoff = x1
        x1   = 0.
        x2  -= xoff
    else:
        xoff = 0.
    yoff = 0.

    if reset:
        inpt.set_default('x1', x1)
        inpt.set_default('x2', x2)
        inpt.set_default('y1', y1)
        inpt.set_default('y2', y2)

    x1 = inpt.get_value('x1', 'left-hand limit of plot', x1)
    x2 = inpt.get_value('x2', 'right-hand limit of plot', x2)
    y1 = inpt.get_value('y1', 'bottom limit of plot', y1)
    y2 = inpt.get_value('y2', 'top limit of plot', y2)

    m_or_u    = inpt.get_value('mask', 'M(ask), U(nmask) or Q(uit)?', 'm', lvals=['m', 'M', 'u', 'U', 'q', 'Q'])
    if m_or_u.upper() == 'M':
        mtext = 'mask'
    else:
        mtext = 'unmask'

    mask_type = inpt.get_value('type', 'X, Y, P(hase), I(ndex) or Q(uit)?', 'x',
                               lvals=['x', 'X', 'y', 'Y', 'p', 'P', 'i', 'I', 'q', 'Q'])

    # initialise plot
    try:
        pg.pgopen(device)
        pg.pgsch(1.5)
        pg.pgscf(2)
        pg.pgslw(2)
        pg.pgsci(4)
        pg.pgenv(x1,x2,y1,y2,0,0)
        (xlabel,ylabel) = dset.plabel(xoff,yoff)
        pg.pgsci(2)
        pg.pglab(xlabel, ylabel, dset.title)

        # plot the dset
        dset.plot(xoff,yoff)

        x = (x1+x2)/2.
        y = (y1+y2)/2.

        # now define masks
        ch = 'X'
        while ch.upper() != 'Q':

            # go through mask options
            if mask_type.upper() == 'X':

                print('Set cursor at the one end of the X range, Q to quit')
                (xm1,y,ch) = pg.pgband(7,0,x,y)
                if ch.upper() != 'Q':
                    print('Set cursor at the other end of ' +
                          'the X range, Q to quit')
                    xm2,y,ch = pg.pgband(7,0,xm1,y)
                    if ch.upper() != 'Q':
                        if xm1 > xm2: xm1,xm2 = xm2,xm1
                        umask = mask.Xmask(xoff+xm1, xoff+xm2, m_or_u.upper() == 'M')

            elif mask_type.upper() == 'I':

                print('Place cursor near a point and click to ' +
                      mtext + ' it, Q to quit')
                x,y,ch = pg.pgband(7,0,x,y)
                if ch.upper() != 'Q':
                    xmm1,xmm2,ymm1,ymm2 = pg.pgqvp(2)
                    xscale  = (xmm2-xmm1)/(x2-x1)
                    yscale  = (ymm2-ymm1)/(y2-y1)

                    # only consider good data of opposite 'polarity' to the
                    # change we are making.
                    ok  = (dset.good == True) & \
                          (dset.mask == (m_or_u.upper() == 'M'))

                    if len(dset.x.dat[ok == True]):
                        # compute physical squared distance of cursor from
                        # points
                        sqdist  = npy.power(
                            xscale*(dset.x.dat[ok]-(xoff+x)),2) + \
                            npy.power(yscale*(dset.y.dat[ok]-(yoff+y)),2)

                        # select the index giving the minimum distance
                        indices = npy.arange(len(dset))[ok]
                        index   = indices[sqdist.min() == sqdist][0]
                        umask   = mask.Imask(index, m_or_u.upper() == 'M')
                    else:
                        print('There seem to be no data to ' + mtext +
                              '; data already ' + mtext + 'ed are ignored.')
                        umask = None

            if ch.upper() != 'Q' and umask is not None:
                gmask.append(umask)
                umask.app_mask(dset)

                print('overplotting data')
                # over-plot the dset
                dset.plot(xoff,yoff)

        pg.pgclos()
    except pg.ioerror, err:
        raise DintError(str(err))
Пример #6
0
def prepplot(rangex, rangey, title=None, labx=None, laby=None, \
             rangex2=None, rangey2=None, labx2=None, laby2=None, \
             logx=0, logy=0, logx2=0, logy2=0, font=ppgplot_font_, \
             fontsize=ppgplot_font_size_, id=0, aspect=1, ticks='in', \
             panels=[1,1], device=ppgplot_device_):
    """
    prepplot(rangex, rangey, ...)
        Open a PGPLOT device for plotting.
            'rangex' and 'rangey' are sequence objects giving min and
                max values for each axis.
        The optional entries are:
            title:    graph title                 (default = None)   
            labx:     label for the x-axis        (default = None)   
            laby:     label for the y-axis        (default = None)   
            rangex2:  ranges for 2nd x-axis       (default = None)   
            rangey2:  ranges for 2nd y-axis       (default = None)   
            labx2:    label for the 2nd x-axis    (default = None)   
            laby2:    label for the 2nd y-axis    (default = None)   
            logx:     make the 1st x-axis log     (default = 0 (no))
            logy:     make the 1st y-axis log     (default = 0 (no))
            logx2:    make the 2nd x-axis log     (default = 0 (no))
            logy2:    make the 2nd y-axis log     (default = 0 (no))
            font:     PGPLOT font to use          (default = 1 (normal))
            fontsize: PGPLOT font size to use     (default = 1.0 (normal))
            id:       Show ID line on plot        (default = 0 (no)) 
            aspect:   Aspect ratio                (default = 1 (square))
            ticks:    Ticks point in or out       (default = 'in')   
            panels:   Number of subpanels [r,c]   (default = [1,1])
            device:   PGPLOT device to use        (default = '/XWIN')
        Note:  Many default values are defined in global variables
            with names like ppgplot_font_ or ppgplot_device_.
    """
    global ppgplot_dev_open_, ppgplot_dev_prep_
    # Check if we will use second X or Y axes
    # Note:  if using a 2nd X axis, the range should correspond
    #   to the minimum and maximum values of the 1st X axis.  If
    #   using a 2nd Y axis, the range should correspond to the
    #   scalerange() values of the 1st Y axis.
    if rangex2 is None:
        rangex2 = rangex
        otherxaxis = 0
    else:
        otherxaxis = 1
    if rangey2 is None:
        rangey2 = rangey
        otheryaxis = 0
    else:
        otheryaxis = 1
    # Open the plot device
    if (not ppgplot_dev_open_):
        ppgplot.pgopen(device)
        # Let the routines know that we already have a device open
        ppgplot_dev_open_ = 1
        # Set the aspect ratio
        ppgplot.pgpap(0.0, aspect)
        if (panels != [1, 1]):
            # Set the number of panels
            ppgplot.pgsubp(panels[0], panels[1])
            ppgplot.pgpage()
    # Choose the font
    ppgplot.pgscf(font)
    # Choose the font size
    ppgplot.pgsch(fontsize)
    # Choose the font size
    ppgplot.pgslw(ppgplot_linewidth_)
    # Plot the 2nd axis if needed first
    if otherxaxis or otheryaxis:
        ppgplot.pgvstd()
        ppgplot.pgswin(rangex2[0], rangex2[1], rangey2[0], rangey2[1])
        # Decide how the axes will be drawn
        if ticks == 'in': env = "CMST"
        else: env = "CMSTI"
        if logx2: lxenv = 'L'
        else: lxenv = ''
        if logy2: lyenv = 'L'
        else: lyenv = ''
        if otherxaxis and otheryaxis:
            ppgplot.pgbox(env + lxenv, 0.0, 0, env + lyenv, 0.0, 0)
        elif otheryaxis:
            ppgplot.pgbox("", 0.0, 0, env + lyenv, 0.0, 0)
        else:
            ppgplot.pgbox(env + lxenv, 0.0, 0, "", 0.0, 0)
    # Now setup the primary axis
    ppgplot.pgvstd()
    ppgplot.pgswin(rangex[0], rangex[1], rangey[0], rangey[1])
    # Decide how the axes will be drawn
    if ticks == 'in': env = "ST"
    else: env = "STI"
    if logx: lxenv = 'L'
    else: lxenv = ''
    if logy: lyenv = 'L'
    else: lyenv = ''
    if otherxaxis and otheryaxis:
        ppgplot.pgbox("BN" + env + lxenv, 0.0, 0, "BN" + env + lyenv, 0.0, 0)
    elif otheryaxis:
        ppgplot.pgbox("BCN" + env + lxenv, 0.0, 0, "BN" + env + lyenv, 0.0, 0)
    elif otherxaxis:
        ppgplot.pgbox("BN" + env + lxenv, 0.0, 0, "BCN" + env + lyenv, 0.0, 0)
    else:
        ppgplot.pgbox("BCN" + env + lxenv, 0.0, 0, "BCN" + env + lyenv, 0.0, 0)
    # Add labels
    if not title is None: ppgplot.pgmtxt("T", 3.2, 0.5, 0.5, title)
    ppgplot.pgmtxt("B", 3.0, 0.5, 0.5, labx)
    ppgplot.pgmtxt("L", 2.6, 0.5, 0.5, laby)
    if otherxaxis: ppgplot.pgmtxt("T", 2.0, 0.5, 0.5, labx2)
    if otheryaxis: ppgplot.pgmtxt("R", 3.0, 0.5, 0.5, laby2)
    # Add ID line if required
    if (id == 1): ppgplot.pgiden()
    # Let the routines know that we have already prepped the device
    ppgplot_dev_prep_ = 1
Пример #7
0
t_max_curve=30

# Plot A (Airmass-Time)
########################
# PS OUTPUT 
###############################################################
filename=sys.argv[1]
psfile = str(filename)+".ps" # print ("psfile\n")
ppgplot.pgbegin(0,"psfile/VCPS", 1, 1)
# pgbegin(0,"psfile/PS", 1, 1)

# Plot Setting
####################################################################
ppgplot.pgpaper(8,1.25) # window/paper size (width(inch), aspect)

ppgplot.pgscf(2)   # characte font (1: normal, 2: roman, 3: italic, 4: script)
ppgplot.pgslw(3) # line width
ppgplot.pgsvp(0.15, 0.9, 0.53, 0.89) # viewport in the window (relative)
ppgplot.pglab("", "", "Local Time [hour]")
ppgplot.pgsvp(0.12, 0.9, 0.53, 0.88) # viewport in the window (relative)
ppgplot.pglabel("", "Airmass", "") # label settingoto s

ppgplot.pgsch(1.0) # character height (size)
ppgplot.pgslw(3) # line width
ppgplot.pgsvp(0.15, 0.9, 0.53, 0.88) # viewport in the window (relative)
ppgplot.pgswin(t_min, t_max, a_max, a_min) # MIN,MAX of coordinate

ppgplot.pgbox('BCTS', 0.0, 0, 'BCTSNV1', 0.1, 0) # coordinate settings
ppgplot.pgbox('0', 0.0, 0, 'BCTSMV1', 0.1, 0) # coordinate settings

# Put Header/ Axes Label
Пример #8
0
def main():
    parser = OptionParser(usage)
    parser.add_option("-x", "--xwin", action="store_true", dest="xwin",
                      default=False, help="Don't make a postscript plot, just use an X-window")
    parser.add_option("-p", "--noplot", action="store_false", dest="makeplot",
                      default=True, help="Look for pulses but do not generate a plot")
    parser.add_option("-m", "--maxwidth", type="float", dest="maxwidth", default=0.0,
                      help="Set the max downsampling in sec (see below for default)")
    parser.add_option("-t", "--threshold", type="float", dest="threshold", default=5.0,
                      help="Set a different threshold SNR (default=5.0)")
    parser.add_option("-s", "--start", type="float", dest="T_start", default=0.0,
                      help="Only plot events occuring after this time (s)")
    parser.add_option("-e", "--end", type="float", dest="T_end", default=1e9,
                      help="Only plot events occuring before this time (s)")
    parser.add_option("-g", "--glob", type="string", dest="globexp", default=None,
                      help="Process the files from this glob expression")
    parser.add_option("-f", "--fast", action="store_true", dest="fast",
                      default=False, help="Use a faster method of de-trending (2x speedup)")
    parser.add_option("-i", "--id", type="int", dest="obsid",
                      default=0, help="enter an observation id")
    (opts, args) = parser.parse_args()
    if len(args)==0:
        if opts.globexp==None:
            print full_usage
            sys.exit(0)
        else:
            args = []
            for globexp in opts.globexp.split():
                args += glob.glob(globexp)
    useffts = True
    dosearch = True
    if opts.xwin:
        pgplot_device = "/XWIN"
    else:
        pgplot_device = ""

    fftlen = 8192     # Should be a power-of-two for best speed
    chunklen = 8000   # Must be at least max_downfact less than fftlen
    detrendlen = 1000 # length of a linear piecewise chunk of data for detrending
    blocks_per_chunk = chunklen / detrendlen
    overlap = (fftlen - chunklen)/2
    worklen = chunklen + 2*overlap  # currently it is fftlen...

    max_downfact = 30
    default_downfacts = [2, 3, 4, 6, 9, 14, 20, 30, 45, 70, 100, 150]



    if args[0].endswith(".singlepulse"):
        filenmbase = args[0][:args[0].rfind(".singlepulse")]
        dosearch = False
    elif args[0].endswith(".dat"):
        filenmbase = args[0][:args[0].rfind(".dat")]
    else:
        filenmbase = args[0]

    # Don't do a search, just read results and plot
    if not dosearch:
        info, DMs, candlist, num_v_DMstr = \
              read_singlepulse_files(args, opts.threshold, opts.T_start, opts.T_end)
        orig_N, orig_dt = int(info.N), info.dt
        obstime = orig_N * orig_dt
    else:
        DMs = []
        candlist = []
        num_v_DMstr = {}

        # Loop over the input files
        for filenm in args:
            if filenm.endswith(".dat"):
                filenmbase = filenm[:filenm.rfind(".dat")]
            else:
                filenmbase = filenm
            info = infodata.infodata(filenmbase+".inf")
            DMstr = "%.2f"%info.DM
            DMs.append(info.DM)
            N, dt = int(info.N), info.dt
            obstime = N * dt
            # Choose the maximum width to search based on time instead
            # of bins.  This helps prevent increased S/N when the downsampling
            # changes as the DM gets larger.
            if opts.maxwidth > 0.0:
                downfacts = [x for x in default_downfacts if x*dt <= opts.maxwidth]
            else:
                downfacts = [x for x in default_downfacts if x <= max_downfact]
            if len(downfacts) == 0:
                downfacts = [default_downfacts[0]]
            if (filenm == args[0]):
                orig_N = N
                orig_dt = dt
                if useffts:
                    fftd_kerns = make_fftd_kerns(downfacts, fftlen)
            if info.breaks:
                offregions = zip([x[1] for x in info.onoff[:-1]],
                                 [x[0] for x in info.onoff[1:]])
            outfile = open(filenmbase+'.singlepulse', mode='w')

            # Compute the file length in detrendlens
            roundN = N/detrendlen * detrendlen
            numchunks = roundN / chunklen
            # Read in the file
            print 'Reading "%s"...'%filenm
            timeseries = Num.fromfile(filenm, dtype=Num.float32, count=roundN)
            # Split the timeseries into chunks for detrending
            numblocks = roundN/detrendlen
            timeseries.shape = (numblocks, detrendlen)
            stds = Num.zeros(numblocks, dtype=Num.float64)
            # de-trend the data one chunk at a time
            print '  De-trending the data and computing statistics...'
            for ii, chunk in enumerate(timeseries):
                if opts.fast:  # use median removal instead of detrending (2x speedup)
                    tmpchunk = chunk.copy()
                    tmpchunk.sort()
                    med = tmpchunk[detrendlen/2]
                    chunk -= med
                    tmpchunk -= med
                else:
                    # The detrend calls are the most expensive in the program
                    timeseries[ii] = scipy.signal.detrend(chunk, type='linear')
                    tmpchunk = timeseries[ii].copy()
                    tmpchunk.sort()
                # The following gets rid of (hopefully) most of the 
                # outlying values (i.e. power dropouts and single pulses)
                # If you throw out 5% (2.5% at bottom and 2.5% at top)
                # of random gaussian deviates, the measured stdev is ~0.871
                # of the true stdev.  Thus the 1.0/0.871=1.148 correction below.
                # The following is roughly .std() since we already removed the median
                stds[ii] = Num.sqrt((tmpchunk[detrendlen/40:-detrendlen/40]**2.0).sum() /
                                    (0.95*detrendlen))
            stds *= 1.148
            # sort the standard deviations and separate those with
            # very low or very high values
            sort_stds = stds.copy()
            sort_stds.sort()
            # identify the differences with the larges values (this
            # will split off the chunks with very low and very high stds
            locut = (sort_stds[1:numblocks/2+1] -
                     sort_stds[:numblocks/2]).argmax() + 1
            hicut = (sort_stds[numblocks/2+1:] -
                     sort_stds[numblocks/2:-1]).argmax() + numblocks/2 - 2
            std_stds = scipy.std(sort_stds[locut:hicut])
            median_stds = sort_stds[(locut+hicut)/2]
            lo_std = median_stds - 4.0 * std_stds
            hi_std = median_stds + 4.0 * std_stds
            # Determine a list of "bad" chunks.  We will not search these.
            bad_blocks = Num.nonzero((stds < lo_std) | (stds > hi_std))[0]
            print "    pseudo-median block standard deviation = %.2f" % (median_stds)
            print "    identified %d bad blocks out of %d (i.e. %.2f%%)" % \
                  (len(bad_blocks), len(stds),
                   100.0*float(len(bad_blocks))/float(len(stds)))
            stds[bad_blocks] = median_stds
            print "  Now searching..."

            # Now normalize all of the data and reshape it to 1-D
            timeseries /= stds[:,Num.newaxis]
            timeseries.shape = (roundN,)
            # And set the data in the bad blocks to zeros
            # Even though we don't search these parts, it is important
            # because of the overlaps for the convolutions
            for bad_block in bad_blocks:
                loind, hiind = bad_block*detrendlen, (bad_block+1)*detrendlen
                timeseries[loind:hiind] = 0.0
            # Convert to a set for faster lookups below
            bad_blocks = set(bad_blocks)

            # Step through the data
            dm_candlist = []
            for chunknum in range(numchunks):
                loind = chunknum*chunklen-overlap
                hiind = (chunknum+1)*chunklen+overlap
                # Take care of beginning and end of file overlap issues
                if (chunknum==0): # Beginning of file
                    chunk = Num.zeros(worklen, dtype=Num.float32)
                    chunk[overlap:] = timeseries[loind+overlap:hiind]
                elif (chunknum==numchunks-1): # end of the timeseries
                    chunk = Num.zeros(worklen, dtype=Num.float32)
                    chunk[:-overlap] = timeseries[loind:hiind-overlap]
                else:
                    chunk = timeseries[loind:hiind]

                # Make a set with the current block numbers
                lowblock = blocks_per_chunk * chunknum
                currentblocks = set(Num.arange(blocks_per_chunk) + lowblock)
                localgoodblocks = Num.asarray(list(currentblocks -
                                                   bad_blocks)) - lowblock
                # Search this chunk if it is not all bad
                if len(localgoodblocks):
                    # This is the good part of the data (end effects removed)
                    goodchunk = chunk[overlap:-overlap]

                    # need to pass blocks/chunklen, localgoodblocks
                    # dm_candlist, dt, opts.threshold to cython routine

                    # Search non-downsampled data first
                    # NOTE:  these nonzero() calls are some of the most
                    #        expensive calls in the program.  Best bet would 
                    #        probably be to simply iterate over the goodchunk
                    #        in C and append to the candlist there.
                    hibins = Num.flatnonzero(goodchunk>opts.threshold)
                    hivals = goodchunk[hibins]
                    hibins += chunknum * chunklen
                    hiblocks = hibins/detrendlen
                    # Add the candidates (which are sorted by bin)
                    for bin, val, block in zip(hibins, hivals, hiblocks):
                        if block not in bad_blocks:
                            time = bin * dt
                            dm_candlist.append(candidate(info.DM, val, time, bin, 1))

                    # Prepare our data for the convolution
                    if useffts: fftd_chunk = rfft(chunk, -1)

                    # Now do the downsampling...
                    for ii, downfact in enumerate(downfacts):
                        if useffts: 
                            # Note:  FFT convolution is faster for _all_ downfacts, even 2
                            goodchunk = fft_convolve(fftd_chunk, fftd_kerns[ii],
                                                     overlap, -overlap)
                        else:
                            # The normalization of this kernel keeps the post-smoothing RMS = 1
                            kernel = Num.ones(downfact, dtype=Num.float32) / \
                                     Num.sqrt(downfact)
                            smoothed_chunk = scipy.signal.convolve(chunk, kernel, 1)
                            goodchunk = smoothed_chunk[overlap:-overlap]
                        #hibins = Num.nonzero(goodchunk>opts.threshold)[0]
                        hibins = Num.flatnonzero(goodchunk>opts.threshold)
                        hivals = goodchunk[hibins]
                        hibins += chunknum * chunklen
                        hiblocks = hibins/detrendlen
                        hibins = hibins.tolist()
                        hivals = hivals.tolist()
                        # Now walk through the new candidates and remove those
                        # that are not the highest but are within downfact/2
                        # bins of a higher signal pulse
                        hibins, hivals = prune_related1(hibins, hivals, downfact)
                        # Insert the new candidates into the candlist, but
                        # keep it sorted...
                        for bin, val, block in zip(hibins, hivals, hiblocks):
                            if block not in bad_blocks:
                                time = bin * dt
                                bisect.insort(dm_candlist,
                                              candidate(info.DM, val, time, bin, downfact))

            # Now walk through the dm_candlist and remove the ones that
            # are within the downsample proximity of a higher
            # signal-to-noise pulse
            dm_candlist = prune_related2(dm_candlist, downfacts)
            print "  Found %d pulse candidates"%len(dm_candlist)
            
            # Get rid of those near padding regions
            if info.breaks: prune_border_cases(dm_candlist, offregions)

            # Write the pulses to an ASCII output file
            if len(dm_candlist):
                conn = MySQLdb.connect (host = "localhost", user = "******", passwd = "ibmthinkpad", db = "flyseye")
                cursor = conn.cursor ()
                #dm_candlist.sort(cmp_sigma)
                outfile.write("# DM      Sigma      Time (s)     Sample    Downfact\n")
                for cand in dm_candlist:
                    outfile.write(str(cand))
                    sql = "INSERT INTO presto (obsid, dm, sigma, time, sample, downfact) VALUES (%10d, %7.2f, %7.2f, %13.6f, %10d, %3d)" % (opts.obsid, cand.DM, cand.sigma, cand.time, cand.bin, cand.downfact)
                    cursor.execute (sql)				
                cursor.close ()
                conn.close ()
            outfile.close()

            #row = cursor.fetchone ()
            #print "server version:", row[0]


            # Add these candidates to the overall candidate list
            for cand in dm_candlist:
                candlist.append(cand)
            num_v_DMstr[DMstr] = len(dm_candlist)

    if (opts.makeplot):

        # Step through the candidates to make a SNR list
        DMs.sort()
        maxsnr = 0.0
        snrs = []
        for cand in candlist:
            snrs.append(cand.sigma)
            #print "  snr%f"%cand.sigma
            if cand.sigma > maxsnr and cand.sigma < 2e9:
                maxsnr = cand.sigma
        	#print "update %f"%cand.sigma
	maxsnr = min(maxsnr,2e9)
        maxsnr = int(maxsnr) + 3
	#print "  Found %d pulse candidates"%maxsnr
        # Generate the SNR histogram
        snrs = Num.asarray(snrs)
        (num_v_snr, lo_snr, d_snr, num_out_of_range) = \
                    scipy.stats.histogram(snrs,
                                          int(maxsnr-opts.threshold+1),
                                          [opts.threshold, maxsnr])
        #print "  Found %d pulse candidates"%maxsnr
	snrs = Num.arange(maxsnr-opts.threshold+1, dtype=Num.float64) * d_snr \
               + lo_snr + 0.5*d_snr
        num_v_snr = num_v_snr.astype(Num.float32)
        num_v_snr[num_v_snr==0.0] = 0.001

        # Generate the DM histogram
        num_v_DM = Num.zeros(len(DMs))
        for ii, DM in enumerate(DMs):
            num_v_DM[ii] = num_v_DMstr["%.2f"%DM]
        DMs = Num.asarray(DMs)

        # open the plot device
        short_filenmbase = filenmbase[:filenmbase.find("_DM")]
        if opts.T_end > obstime:
            opts.T_end = obstime
        if pgplot_device:
            ppgplot.pgopen(pgplot_device)
        else:
            if (opts.T_start > 0.0 or opts.T_end < obstime):
                ppgplot.pgopen(short_filenmbase+'_%.0f-%.0fs_singlepulse.ps/VPS'%
                               (opts.T_start, opts.T_end))
            else:
                ppgplot.pgopen(short_filenmbase+'_singlepulse.ps/VPS')
        ppgplot.pgpap(7.5, 1.0)  # Width in inches, aspect
        ppgplot.pgscf(2)
        # plot the SNR histogram
        ppgplot.pgsvp(0.06, 0.31, 0.6, 0.87)
        ppgplot.pgswin(opts.threshold, maxsnr,
                       Num.log10(0.5), Num.log10(2*max(num_v_snr)))
        ppgplot.pgsch(0.8)
        ppgplot.pgbox("BCNST", 0, 0, "BCLNST", 0, 0)
        ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "Signal-to-Noise")
        ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Number of Pulses")
        ppgplot.pgsch(1.0)
        ppgplot.pgbin(snrs, Num.log10(num_v_snr), 1)

        # plot the DM histogram
        ppgplot.pgsvp(0.39, 0.64, 0.6, 0.87)
        ppgplot.pgswin(min(DMs)-0.5, max(DMs)+0.5, 0.0, 1.1*max(num_v_DM))
        ppgplot.pgsch(0.8)
        ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
        ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "DM (pc cm\u-3\d)")
        ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Number of Pulses")
        ppgplot.pgsch(1.0)
        ppgplot.pgbin(DMs, num_v_DM, 1)

        # plot the SNR vs DM plot 
        ppgplot.pgsvp(0.72, 0.97, 0.6, 0.87)
        ppgplot.pgswin(min(DMs)-0.5, max(DMs)+0.5, opts.threshold, maxsnr)
        ppgplot.pgsch(0.8)
        ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
        ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "DM (pc cm\u-3\d)")
        ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "Signal-to-Noise")
        ppgplot.pgsch(1.0)
        cand_ts = Num.zeros(len(candlist), dtype=Num.float32)
        cand_SNRs = Num.zeros(len(candlist), dtype=Num.float32)
        cand_DMs = Num.zeros(len(candlist), dtype=Num.float32)
        for ii, cand in enumerate(candlist):
            cand_ts[ii], cand_SNRs[ii], cand_DMs[ii] = \
                         cand.time, cand.sigma, cand.DM
        ppgplot.pgpt(cand_DMs, cand_SNRs, 20)

        # plot the DM vs Time plot
        ppgplot.pgsvp(0.06, 0.97, 0.08, 0.52)
        ppgplot.pgswin(opts.T_start, opts.T_end, min(DMs)-0.5, max(DMs)+0.5)
        ppgplot.pgsch(0.8)
        ppgplot.pgbox("BCNST", 0, 0, "BCNST", 0, 0)
        ppgplot.pgmtxt('B', 2.5, 0.5, 0.5, "Time (s)")
        ppgplot.pgmtxt('L', 1.8, 0.5, 0.5, "DM (pc cm\u-3\d)")
        # Circles are symbols 20-26 in increasing order
        snr_range = 12.0
        cand_symbols = (cand_SNRs-opts.threshold)/snr_range * 6.0 + 20.5
        cand_symbols = cand_symbols.astype(Num.int32)
        cand_symbols[cand_symbols>26] = 26
        for ii in [26, 25, 24, 23, 22, 21, 20]:
            inds = Num.nonzero(cand_symbols==ii)[0]
            ppgplot.pgpt(cand_ts[inds], cand_DMs[inds], ii)

        # Now fill the infomation area
        ppgplot.pgsvp(0.05, 0.95, 0.87, 0.97)
        ppgplot.pgsch(1.0)
        ppgplot.pgmtxt('T', 0.5, 0.0, 0.0,
                       "Single pulse results for '%s'"%short_filenmbase)
        ppgplot.pgsch(0.8)
        # first row
        ppgplot.pgmtxt('T', -1.1, 0.02, 0.0, 'Source: %s'%\
                       info.object)
        ppgplot.pgmtxt('T', -1.1, 0.33, 0.0, 'RA (J2000):')
        ppgplot.pgmtxt('T', -1.1, 0.5, 0.0, info.RA)
        ppgplot.pgmtxt('T', -1.1, 0.73, 0.0, 'N samples: %.0f'%orig_N)
        # second row
        ppgplot.pgmtxt('T', -2.4, 0.02, 0.0, 'Telescope: %s'%\
                       info.telescope)
        ppgplot.pgmtxt('T', -2.4, 0.33, 0.0, 'DEC (J2000):')
        ppgplot.pgmtxt('T', -2.4, 0.5, 0.0, info.DEC)
        ppgplot.pgmtxt('T', -2.4, 0.73, 0.0, 'Sampling time: %.2f \gms'%\
                       (orig_dt*1e6))
        # third row
        if info.instrument.find("pigot") >= 0:
            instrument = "Spigot"
        else:
            instrument = info.instrument
        ppgplot.pgmtxt('T', -3.7, 0.02, 0.0, 'Instrument: %s'%instrument)
        if (info.bary):
            ppgplot.pgmtxt('T', -3.7, 0.33, 0.0, 'MJD\dbary\u: %.12f'%info.epoch)
        else:
            ppgplot.pgmtxt('T', -3.7, 0.33, 0.0, 'MJD\dtopo\u: %.12f'%info.epoch)
        ppgplot.pgmtxt('T', -3.7, 0.73, 0.0, 'Freq\dctr\u: %.1f MHz'%\
                       ((info.numchan/2-0.5)*info.chan_width+info.lofreq))
        ppgplot.pgiden()
        ppgplot.pgend()