# This provides utilities for plotting things on a 
# 1D or 2D mesh or a slice of a 3D mesh.

try :
  import numpy as np
except ImportError :
  print "Error importing Numpy"
try :
  import mpl_toolkits.mplot3d.axes3d as p3
  import matplotlib.pyplot as plt
except ImportError :
  print "Error importing matplotlib"

global __detranuselatex__ = False

try :
    import os
    print "Checking for LaTeX for nicer plotting labels..."
    if (os.system("latex")==0) :
	    from matplotlib import rc
	    rc('text', usetex=True)
	    rc('font', family='serif')
	    __detranuselatex__ = True
except ImportError :
    print "Warning: LaTeX labels being skipped"

def plot_quadrature(quad) :
   """ Plots a quadrature.
   """

   try :
     D = quad.dimension()
   except :
     print "Error getting quadrature dimension... maybe not a quadrature object?"
     return

   if D == 1 :

     # Get the abscissa and weights
     mu = np.asarray(quad.cosines(0))
     wt = np.asarray(quad.weights())
     # Plot
     plt.plot(mu, wt, 'bo')
     if __detranuselatex__ :
       plt.xlabel('$\mu$')
     else :
       plt.xlabel('mu')
     plt.ylabel('weight')
     plt.show()

   else :

     # Get the abscissa and weights
     mu  = np.asarray(quad.cosines(0))
     eta = np.asarray(quad.cosines(1))
     xi  = np.asarray(quad.cosines(2))
     wt  = np.asarray(quad.weights())
     # Plot.  Note, using my (old) version of matplotlib, the colors
     # are not translated to the scatter plot.  The sizes are, but
     # it's not really enough.  What's a good solution?
     fig = plt.figure()
     ax = p3.Axes3D(fig)
     ax.view_init(30, 60) 
     myplot = ax.scatter(mu,eta,xi,c=wt, s=100000*wt**2, marker='^')
     labels = ['mu','eta','xi']
     if __detranuselatex__ :
       labels ['$\mu$', '$\eta$', '$\\xi$']
	 ax.set_xlabel(labels[0])
	 ax.set_ylabel(labels[1])
	 ax.set_zlabel(labels[2])
     fig.colorbar(myplot)
     plt.show()
