from matplotlib import pyplot as plt
import matplotlib.cm as cm
from  matplotlib.colors import Normalize as Normalize
from numpy import median, std
from mpl_toolkits.mplot3d import Axes3D
##from scipy.ndimage.filters import median_filter


##draw picture
def draw_pic(Data, Stars, Stars2):
    med=median(Data)
    stdv=std(Data)
    fig = plt.figure()
    plt.imshow(Data, cmap=cm.Greys_r, aspect='equal',
                             norm= Normalize(vmin=med-stdv*2., vmax=med+stdv*5.), interpolation='nearest')
    if Stars.size:
        plt.scatter(Stars[:,0], Stars[:,1], marker = 'o', facecolors='none', edgecolors='r')
    if Stars2.size:
        plt.scatter(Stars2[:,0], Stars2[:,1], marker = 'o', facecolors='none', edgecolors='b')
    plt.ion()
##    fig.draw()
    fig.show()
##    plt.pause(0.1)
##    print "Hit Space to close"
##    plt.waitforbuttonpress(0) # this will wait for indefinite time
##    plt.close(fig)

##draw_astrometry errors
def draw_astrometry(mag, X_Y, diff):
    ##Ra error vs mag
    fig = plt.figure()
    plt.plot(mag, diff[:, 0], 'bo')
    plt.ylabel('RA, (O-C)*cos(DEC)', fontsize=15)
    plt.xlabel('Vmag, APASS', fontsize=15)
    plt.title('Ra error vs Vmag')
    plt.grid()
##    plt.ioff()
    plt.draw()
    plt.pause(0.1)
    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

    ##De error vs mag
    fig = plt.figure()
    plt.plot(mag, diff[:, 1], 'bo')
    plt.ylabel('DEC, (O-C)', fontsize=15)
    plt.xlabel('Vmag, APASS', fontsize=15)
    plt.title('DEC error vs Vmag')
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

    ##Ra error vs X
    fig = plt.figure()
    plt.plot(X_Y[:, 0], diff[:, 0], 'bo')
    plt.ylabel('RA, (O-C)', fontsize=15)
    plt.xlabel('X (pix)', fontsize=15)
    plt.title('Ra error vs X')
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

    ##Ra error vs Y
    fig = plt.figure()
    plt.plot(X_Y[:, 1], diff[:, 0], 'bo')
    plt.ylabel('RA, (O-C)', fontsize=15)
    plt.xlabel('Y (pix)', fontsize=15)
    plt.title('Ra error vs Y')
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

    ##De error vs X
    fig = plt.figure()
    plt.plot(X_Y[:, 0], diff[:, 1], 'bo')
    plt.ylabel('DEC, (O-C)', fontsize=15)
    plt.xlabel('X (pix)', fontsize=15)
    plt.title('DEC error vs X')
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

    ##De error vs Y
    fig = plt.figure()
    plt.plot(X_Y[:, 1], diff[:, 1], 'bo')
    plt.ylabel('DEC, (O-C)', fontsize=15)
    plt.xlabel('X (pix)', fontsize=15)
    plt.title('DEC error vs Y')
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

##plot 3D O-C vs XY
def plot3D(X, Y, Z):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
##    Z = median_filter(Z, 3)
    ax.scatter(X, Y, Z, c='r', marker='.')
##    ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('O-C')
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

##

##analize photometry results
def draw_photometry(Stars_XY, Stars_Mag, Colors, dPSF_mag, Obj_XY, Obj_mag, Obj_Color):
    ##delta mag vs X
    fig = plt.figure()
    plt.plot(Stars_XY[:,0], dPSF_mag, 'bo')
    plt.plot(Obj_XY[0][0], 0, 'ro')
    plt.ylabel('O-C, Mag', fontsize=15)
    plt.xlabel('X (pix)', fontsize=15)
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

##

    ##delta mag vs Y
    fig = plt.figure()
    plt.plot(Stars_XY[:,1], dPSF_mag, 'bo')
    plt.plot(Obj_XY[0][1], 0, 'ro')
    plt.ylabel('O-C, Mag', fontsize=15)
    plt.xlabel('Y (pix)', fontsize=15)
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

##

    ##delta mag vs mag
    fig = plt.figure()
    plt.plot(Stars_Mag, dPSF_mag, 'bo')
    plt.plot(Obj_mag, 0, 'ro')
    plt.ylabel('O-C, Mag', fontsize=15)
    plt.xlabel('Mag', fontsize=15)
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

##

    ##delta mag vs color
    fig = plt.figure()
    plt.plot(Colors, dPSF_mag, 'bo')
    plt.plot(Obj_Color, 0, 'ro')
    plt.ylabel('O-C, Mag', fontsize=15)
    plt.xlabel('(B-V)', fontsize=15)
    plt.grid()
    plt.draw()
    plt.pause(0.1)
##    print "Hit Space to close"
    plt.waitforbuttonpress(0) # this will wait for indefinite time
    plt.close(fig)

##
    

