def onclick(event):
    global rad_rings  # naughty, I know, I know...
    global taun_rings
    global badring

    for case in switch(event.key):

        newt = event.xdata
        newtau = event.ydata
        print 'newtau is %f' % newtau
        if newtau > 1.0:
            newtau = 1.0
        if newtau < 0.0:
            newtau = 0.000001
        newr = hjd_to_ring(newt + hjd_minr)

        if case('r'):
            exorings.plot_tilt_faceon(rad_rings, taun_rings, res, hjd_minr, dstar)

            break

        if case('v'):
            exorings.plot_tilt_faceon_vect(rad_rings, taun_rings, res, \
                hjd_minr, rstart, rend, dstar)

            break

        if case('a'):
            print 'a pressed, adding a new ring'
            # assume that r is ordered

            print ' inserting new ring date %d and radius %d' % (newt, newr)
            bigr = np.append(rad_rings, newr)
            bigtau = np.append(taun_rings, exorings.tau_to_y(newtau))

            # index sort r, rearrange both r and tau to this
            sortedr = np.argsort(bigr)
            rad_rings = bigr[sortedr]
            taun_rings = bigtau[sortedr]
            break

        if case('d'):
            if rad_rings.size > 1:
                # find closest ring distance
                rsel_idx = ind_ring(rad_rings, newr)
                rad_rings = np.delete(rad_rings, rsel_idx)
                taun_rings = np.delete(taun_rings, rsel_idx)
            break

        if case('m'):
            rsel_idx = ind_ring(rad_rings, newr)

            rad_rings[rsel_idx] = newr
            taun_rings[rsel_idx] = exorings.tau_to_y(newtau)

            break

        if case('b'):
            badring *= -1
            print badring

            break

        if case('o'):
            taun_rings = fmin(ringfunc, taun_rings, maxiter=1000, \
                args=(time, flux, flux_err, rad_rings, res, kern, dstar))

            break

        if case('q'):
            print 'q is for quitters'
            break
        if case():
            print 'not a recognised keypress'


    # print stats of fit
    calc_ring_stats(taun_rings, time, flux, flux_err, rad_rings, res, \
        kern, dstar, rings_tmin, rings_tmax)

    # regenerate drawing
    strip, dummy, g = exorings.ellipse_strip(rad_rings, exorings.y_to_tau(taun_rings), \
        res[0], res[1], res[2], res[3], kern, dstar)
    gt_abs = np.abs(g[0]-hjd_minr)
    g1 = g[1]
    gt_ingr = gt_abs[(g[0] <= hjd_minr)]
    gt_egr = gt_abs[(g[0] > hjd_minr)]
    g1_ingr = g1[(g[0] <= hjd_minr)]
    g1_egr = g1[(g[0] > hjd_minr)]

    # save the current axis ranges
    x1, x2 = h1.get_xlim()
    y1, y2 = h1.get_ylim()

    h1.cla()
    plot_folded_phot(fig_fold)
    h1.plot(event.xdata, event.ydata, color='green')
    h1.plot(np.abs(g[0]-hjd_minr), g[1])
    h1.plot(gt_ingr, g1_ingr, color='blue')
    h1.plot(gt_egr, g1_egr, color='red')
    h1.plot(np.abs(g[0]-hjd_minr), g[2], color='orange')
    h1.set_xlabel('Time from eclipse midpoint [days]')
    h1.set_ylabel('Transmission')

    # zoom back the the last view
    h1.set_xlim([x1, x2])
    h1.set_ylim([y1, y2])
    plt.draw()

    # save tmp version
    # erase old version otherwise write_ring_fits throws a fit
    if os.path.isfile('tmp.fits'):
        os.remove('tmp.fits')
    exorings.write_ring_fits('tmp.fits', res, taun_rings, rad_rings, dstar)
dataset/ETHPy150Open mkenworthy/exorings/disk_sim.py/onclick/original