#! /usr/bin/env python
from ppclass import pp
from    netCDF4               import    Dataset
from	numpy		      import	*
import  numpy                 as        np
import  matplotlib.pyplot     as        mpl
from matplotlib.cm import get_cmap
import pylab
import matplotlib.colors as mcolors

############################
filename1="diagfi2015_S.nc"
var="ch4_ice" #variable

xarea1="-180,-179"
xarea2="0,1"

tint1=["30.625","30.875","31.125","30.375"] #Time must be as written in the input file
tintstr1=["03:00","09:00","15:00","21:00"] #Time must be as written in the input file

tint2=["30.125","30.375","30.625","30.875"] #Time must be as written in the input file
tintstr2=["03:00","09:00","15:00","21:00"] #Time must be as written in the input file
nc1=Dataset(filename1)

lat=getvar(nc1,"latitude")
lon=getvar(nc1,"longitude")
alt=getvar(nc1,"altitude")
print(('alt=',alt))
############################

def getvar(filename,var,tint,xarea):
    myvar = pp(file=filename,var=var,t=tint,x=xarea,compute="nothing").getf()
    print((shape(myvar)))
    return myvar

def getfigvar(nbfig,nbrow,nbcol,i):
    mpl.subplot(nbrow,nbcol,i+1)
    if (i%2==0):
       tint=tint1
       tintstr=tintstr1
       xarea=xarea1
       mytitle="Lon: 180E, LT= "
    else:
       tint=tint2
       tintstr=tintstr2
       xarea=xarea2
       mytitle="Lon: 0E, LT= "

    print(("time numero :",int(i/2),' du fichier tint avec x=',xarea))   
    myvar=getvar(filename1,var,tint[int(i/2)],xarea)[0,:,:,0]
    myvar=myvar*1.e6

    # log
    norm=mcolors.LogNorm()
    lvls=np.logspace(np.log10(mymin),np.log10(mymax),8)
    #titi=[1.e-14,1.e-13,1.e-12,1.e-11]
    CF=mpl.contourf(lat, alt, myvar,levels=lvls,norm=norm,cmap=pal)
    cbar=mpl.colorbar(CF, shrink=1, format="%.2f",extend='both')
    cbar.ax.set_title("1E-6 kg/m2",y=1.04,fontsize=font)

    for t in cbar.ax.get_yticklabels():
      t.set_fontsize(font)

    mpl.title(mytitle+str(tintstr[int(i/2)]),fontsize=font)
    mpl.ylabel('Altitude (m)',labelpad=10,fontsize=font)
    mpl.xlabel('Latitude (deg)',labelpad=10, fontsize=font)
    mpl.xticks(xticks,fontsize=font)
    #mpl.xticks(fontsize=font)
    #mpl.yticks(yticks,fontsize=font)
    mpl.yticks(fontsize=font)
    pylab.ylim([0,3000])

#################"

nbfig=8
nbrow=4
nbcol=2

mpl.figure(figsize=(20, 10))
font=15

pal=get_cmap(name="PuBu")

xticks=[-90,-60,-30,0,30,60,90]
#yticks=np.linspace(0,240,9)

mymin=0.02
mymax=100


left  = None  # the left side of the subplots of the figure
right = None   # the right side of the subplots of the figure
bottom = None  # the bottom of the subplots of the figure
top = None     # the top of the subplots of the figure
wspace = None  # the amount of width reserved for blank space between subplots
hspace = 0.7  # the amount of height reserved for white space between subplots
mpl.subplots_adjust(left, bottom, right, top, wspace, hspace)

for i in range(nbfig):
    getfigvar(nbfig,nbrow,nbcol,i)


mpl.savefig('sectionch4cloud.eps',dpi=200)
mpl.savefig('sectionch4cloud.png',dpi=200)

mpl.show()




