[3823] | 1 | #! /usr/bin/env python |
---|
| 2 | from ppclass import pp |
---|
| 3 | from netCDF4 import Dataset |
---|
| 4 | from numpy import * |
---|
| 5 | import numpy as np |
---|
| 6 | import matplotlib.pyplot as mpl |
---|
| 7 | from matplotlib.cm import get_cmap |
---|
| 8 | import pylab |
---|
| 9 | from matplotlib import ticker |
---|
| 10 | import matplotlib.colors as mcolors |
---|
| 11 | import datetime |
---|
| 12 | from mpl_toolkits.basemap import Basemap, shiftgrid |
---|
| 13 | |
---|
| 14 | ############################ |
---|
| 15 | filename1="diagfi2015.nc" |
---|
| 16 | filename2="diagfi2015_S.nc" |
---|
| 17 | var="tsurf" #variable |
---|
| 18 | var1="ch4_ice_col" |
---|
| 19 | var2="phisinit" |
---|
| 20 | tint=["30.625","30.875","31.125","30.375"] #Time must be as written in the input file |
---|
| 21 | tintstr=["03:00","09:00","15:00","21:00"] #Time must be as written in the input file |
---|
| 22 | |
---|
| 23 | font=15 |
---|
| 24 | |
---|
| 25 | nc1=Dataset(filename1) |
---|
| 26 | nc2=Dataset(filename2) |
---|
| 27 | |
---|
| 28 | lat=nc1.variables["lat"][:] |
---|
| 29 | lon=nc1.variables["lon"][:] |
---|
| 30 | |
---|
| 31 | # altitdue file 2 |
---|
| 32 | alt=nc2.variables["altitude"][:] |
---|
| 33 | ############################ |
---|
| 34 | |
---|
| 35 | def getvar(filename,var,tint): |
---|
| 36 | myvar = pp(file=filename,var=var,t=tint,compute="nothing").getf() # get data to be changed according to selected variable |
---|
| 37 | print((shape(myvar))) |
---|
| 38 | return myvar |
---|
| 39 | |
---|
| 40 | def swinglon(myvar): |
---|
| 41 | # changer les longitudes pour mettre TR au centre |
---|
| 42 | vec=shape(myvar) |
---|
| 43 | myvarbis=np.zeros(vec,dtype='f') |
---|
| 44 | # i lat : pas de changement |
---|
| 45 | # j lon : |
---|
| 46 | for i in range(vec[0]): |
---|
| 47 | for j in range(vec[1]): |
---|
| 48 | if j < int(vec[1]/2.) : |
---|
| 49 | myvarbis[i,j]=myvar[i,j+int(vec[1]/2.)] |
---|
| 50 | # myvar2bis[i,j]=myvar2[i,j+int(vec[1]/2)] |
---|
| 51 | else: |
---|
| 52 | myvarbis[i,j]=myvar[i,j-int(vec[1]/2.)] |
---|
| 53 | # myvar2bis[i,j]=myvar2[i,j-int(vec[1]/2)] |
---|
| 54 | return myvarbis |
---|
| 55 | |
---|
| 56 | def getwinds(lon,lat,vecx,vecy): |
---|
| 57 | svx='None' # arrow every svx box |
---|
| 58 | svy='None' |
---|
| 59 | svx=1 |
---|
| 60 | svy=1 |
---|
| 61 | angle='uv' # 'xy' |
---|
| 62 | color='black' # arrow color |
---|
| 63 | pivot='mid' # arrow around middle of box. Alternative : tip |
---|
| 64 | scale=2*33 # scale arrow |
---|
| 65 | width=0.002 # width arrow |
---|
| 66 | linewidths=0.5 # epaisseur contour arrow |
---|
| 67 | edgecolors='k' # couleur contour arrow |
---|
| 68 | |
---|
| 69 | # *scale*: [ *None* | float ] |
---|
| 70 | # Data units per arrow length unit, e.g., m/s per plot width; a smaller |
---|
| 71 | # scale parameter makes the arrow longer. If *None*, a simple |
---|
| 72 | # autoscaling algorithm is used, based on the average vector length |
---|
| 73 | # and the number of vectors. The arrow length unit is given by |
---|
| 74 | # the *scale_units* parameter |
---|
| 75 | |
---|
| 76 | # *scale_units*: *None*, or any of the *units* options. |
---|
| 77 | # For example, if *scale_units* is 'inches', *scale* is 2.0, and |
---|
| 78 | # ``(u,v) = (1,0)``, then the vector will be 0.5 inches long. |
---|
| 79 | # If *scale_units* is 'width', then the vector will be half the width |
---|
| 80 | # of the axes. |
---|
| 81 | |
---|
| 82 | # If *scale_units* is 'x' then the vector will be 0.5 x-axis |
---|
| 83 | # units. To plot vectors in the x-y plane, with u and v having |
---|
| 84 | # the same units as x and y, use |
---|
| 85 | # "angles='xy', scale_units='xy', scale=1". |
---|
| 86 | |
---|
| 87 | x, y = np.meshgrid(lon,lat) |
---|
| 88 | q = mpl.quiver( x[::svy,::svx],y[::svy,::svx],\ |
---|
| 89 | vecx[::svy,::svx],vecy[::svy,::svx],\ |
---|
| 90 | angles=angle,color=color,pivot=pivot,\ |
---|
| 91 | scale=scale,width=width,linewidths=linewidths,edgecolors=edgecolors) |
---|
| 92 | |
---|
| 93 | # make vector key. |
---|
| 94 | #keyh = 1.025 ; keyv = 1.05 # upper right corner over colorbar |
---|
| 95 | keyh = 0.97 ; keyv = 1.06 |
---|
| 96 | keyh = 0.03 ; keyv = 1.07 |
---|
| 97 | #keyh = -0.03 ; keyv = 1.08 # upper left corner |
---|
| 98 | labelpos='E' # position label compared to arrow : N S E W |
---|
| 99 | p = mpl.quiverkey(q,keyh,keyv,\ |
---|
| 100 | 5.0,r'$5 m/s$',\ |
---|
| 101 | fontproperties={'size': font,'weight': 'bold'},\ |
---|
| 102 | color='black',labelpos=labelpos,labelsep = 0.07) |
---|
| 103 | |
---|
| 104 | def getfigvar(nbfig,nbrow,nbcol,i): |
---|
| 105 | mpl.subplot(nbrow,nbcol,i+1) |
---|
| 106 | pal=get_cmap(name="PuBu") |
---|
| 107 | newlon=lon+180 |
---|
| 108 | |
---|
| 109 | mymin=0.1 |
---|
| 110 | mymax=50 |
---|
| 111 | |
---|
| 112 | # log |
---|
| 113 | norm=mcolors.LogNorm() |
---|
| 114 | lvls=np.logspace(np.log10(mymin),np.log10(mymax),15) |
---|
| 115 | #titi=[1.e-14,1.e-13,1.e-12,1.e-11] |
---|
| 116 | CF=mpl.contourf(newlon, lat, cloud,levels=lvls,norm=norm,cmap=pal) |
---|
| 117 | ''' |
---|
| 118 | # lin |
---|
| 119 | lev=np.linspace(mymin,mymax,6) |
---|
| 120 | CF=mpl.contourf(newlon, lat, cloud,lev,cmap=pal,extend='both') |
---|
| 121 | ''' |
---|
| 122 | yticks=[-90,-60,-30,0,30,60,90] |
---|
| 123 | xticks=[0,60,120,180,240,300,360] |
---|
| 124 | cbar=mpl.colorbar(CF,shrink=1, format="%1.1f") |
---|
| 125 | cbar.ax.set_title("1E-6 kg/m2",y=1.04,fontsize=font) |
---|
| 126 | |
---|
| 127 | for t in cbar.ax.get_yticklabels(): |
---|
| 128 | t.set_fontsize(font) |
---|
| 129 | |
---|
| 130 | vect=[47] |
---|
| 131 | print(('shape=',shape(newlon),shape(lat),shape(tmp))) |
---|
| 132 | CS=mpl.contour(newlon,lat,tmp,colors='k',linewidths=0.5) |
---|
| 133 | #### inline=1 : values over the line |
---|
| 134 | mpl.clabel(CS, inline=1, fontsize=20, fmt='%1.1f',inline_spacing=1) |
---|
| 135 | |
---|
| 136 | mpl.title('Local Time (180E) ='+str(tintstr[i]),fontsize=font) |
---|
| 137 | mpl.ylabel('Latitude (deg)',labelpad=10,fontsize=font) |
---|
| 138 | mpl.xlabel('Longitude (deg)',labelpad=10, fontsize=font) |
---|
| 139 | mpl.xticks(xticks,fontsize=font) |
---|
| 140 | mpl.yticks(yticks,fontsize=font) |
---|
| 141 | #getwinds(newlon,lat,u,v) |
---|
| 142 | |
---|
| 143 | def getnumalt(choicealt,alt): |
---|
| 144 | numalt=np.where(abs(alt-choicealt)==min(abs(alt-choicealt))) |
---|
| 145 | numalt=numalt[0][0] |
---|
| 146 | return numalt |
---|
| 147 | |
---|
| 148 | ####################### |
---|
| 149 | |
---|
| 150 | nbfig=size(tint) |
---|
| 151 | nbrow=2 |
---|
| 152 | nbcol=2 |
---|
| 153 | mpl.figure(figsize=(30, 15)) |
---|
| 154 | choicealt=30 #m |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | for i in range(nbfig): |
---|
| 158 | numalt=getnumalt(choicealt,alt) |
---|
| 159 | print((numalt,'alt=',alt[numalt],'m')) |
---|
| 160 | mycloud=getvar(filename1,var1,tint[i])[0,0,:,:] |
---|
| 161 | mytmp=getvar(filename1,var2,tint[i])[0,0,:,:] |
---|
| 162 | #myvar=getvar(filename2,var,tint[i])[0,0,:,:] |
---|
| 163 | |
---|
| 164 | cloud=swinglon(mycloud) |
---|
| 165 | cloud=cloud*1.e6 |
---|
| 166 | tmp=swinglon(mytmp) |
---|
| 167 | #myvarbis=swinglon(myvar) |
---|
| 168 | |
---|
| 169 | getfigvar(nbfig,nbrow,nbcol,i) |
---|
| 170 | |
---|
| 171 | left = None # the left side of the subplots of the figure |
---|
| 172 | right = None # the right side of the subplots of the figure |
---|
| 173 | bottom = None # the bottom of the subplots of the figure |
---|
| 174 | top = None # the top of the subplots of the figure |
---|
| 175 | wspace = None # the amount of width reserved for blank space between subplots |
---|
| 176 | hspace = None # the amount of height reserved for white space between subplots |
---|
| 177 | #mpl.subplots_adjust(left, bottom, right, top, wspace, hspace) |
---|
| 178 | #mpl.subplots_adjust(hspace = .1) |
---|
| 179 | |
---|
| 180 | mpl.savefig('mapch4cloud.eps',dpi=200) |
---|
| 181 | mpl.savefig('mapch4cloud.png',dpi=200) |
---|
| 182 | |
---|
| 183 | #mpl.show() |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | |
---|