Changeset 1089 in lmdz_wrf
- Timestamp:
- Sep 9, 2016, 5:33:19 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/drawing_tools.py
r1086 r1089 1755 1755 elif u == 'J/kg': lu='$Jkg^{-1}$' 1756 1756 elif u == 'Jkg-1': lu='$Jkg^{-1}$' 1757 elif u == 'K/h': lu='$Kh^{-1}$' 1758 elif u == 'Kh-1': lu='$Kh^{-1}$' 1757 1759 elif u == 'K/m': lu='$Km^{-1}$' 1758 1760 elif u == 'Km-1': lu='$Km^{-1}$' … … 3925 3927 x = lon0 3926 3928 y = lat0 3929 3930 vsend = np.zeros((2), dtype=np.float) 3931 # Changing limits of the colors 3932 if type(vs[0]) != type(np.float(1.)): 3933 if vs[0] == 'Srange': 3934 vsend[0] = np.min(varsv) 3935 vsend[1] = np.max(varsv) 3936 elif vs[0][0:11] == 'Saroundmean': 3937 meanv = np.mean(varsv) 3938 permean = np.float(vs[0].split('@')[1]) 3939 minv = np.min(varsv)*permean 3940 maxv = np.max(varsv)*permean 3941 minextrm = np.min([np.abs(meanv-minv), np.abs(maxv-meanv)]) 3942 vsend[0] = meanv-minextrm 3943 vsend[1] = meanv+minextrm 3944 elif vs[0][0:13] == 'Saroundminmax': 3945 permean = np.float(vs[0].split('@')[1]) 3946 minv = np.min(varsv)*permean 3947 maxv = np.max(varsv)*permean 3948 vsend[0] = minv 3949 vsend[1] = maxv 3950 elif vs[0][0:17] == 'Saroundpercentile': 3951 medianv = np.median(varsv) 3952 valper = np.float(vs[0].split('@')[1]) 3953 minv = np.percentile(varsv, valper) 3954 maxv = np.percentile(varsv, 100.-valper) 3955 minextrm = np.min([np.abs(medianv-minv), np.abs(maxv-medianv)]) 3956 vsend[0] = medianv-minextrm 3957 vsend[1] = medianv+minextrm 3958 elif vs[0][0:5] == 'Smean': 3959 meanv = np.mean(varsv) 3960 permean = np.float(vs[0].split('@')[1]) 3961 minv = np.min(varsv)*permean 3962 maxv = np.max(varsv)*permean 3963 minextrm = np.min([np.abs(meanv-minv), np.abs(maxv-meanv)]) 3964 vsend[0] = -minextrm 3965 vsend[1] = minextrm 3966 elif vs[0][0:7] == 'Smedian': 3967 medianv = np.median(varsv) 3968 permedian = np.float(vs[0].split('@')[1]) 3969 minv = np.min(varsv)*permedian 3970 maxv = np.max(varsv)*permedian 3971 minextrm = np.min([np.abs(medianv-minv), np.abs(maxv-medianv)]) 3972 vsend[0] = -minextrm 3973 vsend[1] = minextrm 3974 elif vs[0][0:11] == 'Spercentile': 3975 medianv = np.median(varsv) 3976 valper = np.float(vs[0].split('@')[1]) 3977 minv = np.percentile(varsv, valper) 3978 maxv = np.percentile(varsv, 100.-valper) 3979 minextrm = np.min([np.abs(medianv-minv), np.abs(maxv-medianv)]) 3980 vsend[0] = -minextrm 3981 vsend[1] = minextrm 3982 else: 3983 print errormsg 3984 print ' ' + fname + ": range '" + vs[0] + "' not ready!!!" 3985 quit(-1) 3986 print ' ' + fname + ': modified shadow min,max:',vsend 3987 else: 3988 vsend[0] = vs[0] 3989 3990 if type(vs[0]) != type(np.float(1.)): 3991 if vs[1] == 'range': 3992 vsend[1] = np.max(varsv) 3993 else: 3994 vsend[1] = vs[1] 3995 3996 plt.rc('text', usetex=True) 3997 3998 plt.pcolormesh(x, y, varsv, cmap=plt.get_cmap(colorbar), vmin=vsend[0], vmax=vsend[1]) 3999 cbar = plt.colorbar() 4000 4001 if not mapv is None: 4002 if colorbar == 'gist_gray': 4003 m.drawcoastlines(color="red") 4004 else: 4005 m.drawcoastlines() 4006 4007 meridians = pretty_int(nlon,xlon,5) 4008 m.drawmeridians(meridians,labels=[True,False,False,True]) 4009 parallels = pretty_int(nlat,xlat,5) 4010 m.drawparallels(parallels,labels=[False,True,True,False]) 4011 4012 plt.xlabel('W-E') 4013 plt.ylabel('S-N') 4014 else: 4015 plt.xlabel(variables_values(dimn[1])[0].replace('_','\_') + ' (' + \ 4016 units_lunits(dimxu) + ')') 4017 plt.ylabel(variables_values(dimn[0])[0].replace('_','\_') + ' (' + \ 4018 units_lunits(dimyu) + ')') 4019 4020 txpos = pretty_int(x.min(),x.max(),5) 4021 typos = pretty_int(y.min(),y.max(),5) 4022 txlabels = list(txpos) 4023 for i in range(len(txlabels)): txlabels[i] = str(txlabels[i]) 4024 tylabels = list(typos) 4025 for i in range(len(tylabels)): tylabels[i] = str(tylabels[i]) 4026 4027 # set the limits of the plot to the limits of the data 4028 4029 if searchInlist(revas,'transpose'): 4030 x0 = y 4031 y0 = x 4032 x = x0 4033 y = y0 4034 # print 'Lluis reva0:',reva0,'x min,max:',x.min(),x.max(),' y min,max:',y.min(),y.max() 4035 4036 if reva0 == 'flip': 4037 if reva.split('@')[1] == 'x': 4038 plt.axis([x.max(), x.min(), y.min(), y.max()]) 4039 elif reva.split('@')[1] == 'y': 4040 plt.axis([x.min(), x.max(), y.max(), y.min()]) 4041 else: 4042 plt.axis([x.max(), x.min(), y.max(), y.min()]) 4043 else: 4044 plt.axis([x.min(), x.max(), y.min(), y.max()]) 4045 4046 if mapv is None: 4047 plt.xticks(txpos, txlabels) 4048 plt.yticks(typos, tylabels) 4049 4050 # units labels 4051 cbar.set_label(vnames.replace('_','\_') + ' (' + units_lunits(uts) + ')') 4052 4053 figname = '2Dfields_shadow' 4054 graphtit = vtit.replace('_','\_').replace('&','\&') 4055 4056 plt.title(graphtit) 4057 4058 output_kind(kfig, figname, ifclose) 4059 4060 return 4061 4062 #Nvals=50 4063 #vals1 = np.zeros((Nvals,Nvals), dtype= np.float) 4064 #for j in range(Nvals): 4065 # for i in range(Nvals): 4066 # vals1[j,i]=np.sqrt((j-Nvals/2)**2. + (i-Nvals/2)**2.) 4067 4068 #plot_2D_shadow(vals1, 'var1', np.arange(50)*1., np.arange(50)*1., 'ms-1', \ 4069 # 'm', ['lat','lon'], 'rainbow', [0, Nvals], 'ms-1', 'test var1', 'pdf', 'None', \ 4070 # None, True) 4071 #quit() 4072 4073 def plot_2D_shadow_time(varsv,vnames,dimxv,dimyv,dimxu,dimyu,dimn,colorbar,vs,uts, \ 4074 vtit,kfig,reva,taxis,tpos,tlabs,ifclose): 4075 """ Plotting a 2D field with one of the axes being time 4076 varsv= 2D values to plot with shading 4077 vnames= shading variable name for the figure 4078 dim[x/y]v= values at the axes of x and y 4079 dim[x/y]u= units at the axes of x and y 4080 dimn= dimension names to plot 4081 colorbar= name of the color bar to use 4082 vs= minmum and maximum values to plot in shadow or: 4083 'Srange': for full range 4084 'Saroundmean@val': for mean-xtrm,mean+xtrm where xtrm = np.min(mean-min@val,max@val-mean) 4085 'Saroundminmax@val': for min*val,max*val 4086 'Saroundpercentile@val': for median-xtrm,median+xtrm where xtrm = np.min(median-percentile_(val), 4087 percentile_(100-val)-median) 4088 'Smean@val': for -xtrm,xtrm where xtrm = np.min(mean-min*@val,max*@val-mean) 4089 'Smedian@val': for -xtrm,xtrm where xtrm = np.min(median-min@val,max@val-median) 4090 'Spercentile@val': for -xtrm,xtrm where xtrm = np.min(median-percentile_(val), 4091 percentile_(100-val)-median) 4092 uts= units of the variable to shadow 4093 vtit= title of the variable 4094 kfig= kind of figure (jpg, pdf, png) 4095 reva= 4096 * 'transpose': reverse the axes (x-->y, y-->x) 4097 * 'flip'@[x/y]: flip the axis x or y 4098 taxis= Which is the time-axis 4099 tpos= positions of the time ticks 4100 tlabs= labels of the time ticks 4101 ifclose= boolean value whether figure should be close (finish) or not 4102 """ 4103 fname = 'plot_2D_shadow_time' 4104 4105 if varsv == 'h': 4106 print fname + '_____________________________________________________________' 4107 print plot_2D_shadow_time.__doc__ 4108 quit() 4109 4110 # Definning ticks labels 4111 if taxis == 'x': 4112 txpos = tpos 4113 txlabels = tlabs 4114 plxlabel = dimxu 4115 typos = pretty_int(np.min(dimyv),np.max(dimyv),10) 4116 tylabels = list(typos) 4117 for i in range(len(tylabels)): tylabels[i] = str(tylabels[i]) 4118 plylabel = variables_values(dimn[0])[0].replace('_','\_') + ' (' + \ 4119 units_lunits(dimyu) + ')' 4120 else: 4121 txpos = pretty_int(np.min(dimxv),np.max(dimxv),10) 4122 txlabels = list(txpos) 4123 plxlabel = variables_values(dimn[1])[0].replace('_','\_') + ' (' + \ 4124 units_lunits(dimxu) + ')' 4125 typos = tpos 4126 tylabels = tlabs 4127 plylabel = dimyu 4128 4129 # Transposing/flipping axis 4130 if reva.find('|') != 0: 4131 revas = reva.split('|') 4132 reva0 = '' 4133 else: 4134 revas = [reva] 4135 reva0 = reva 4136 4137 for rev in revas: 4138 if rev[0:4] == 'flip': 4139 reva0 = 'flip' 4140 if len(reva.split('@')) != 2: 4141 print errormsg 4142 print ' ' + fname + ': flip is given', reva, 'but not axis!' 4143 quit(-1) 4144 else: 4145 print " flipping '" + rev.split('@')[1] + "' axis !" 4146 4147 if rev == 'transpose': 4148 print ' reversing the axes of the figure (x-->y, y-->x)!!' 4149 # Flipping values of variable 4150 varsv = np.transpose(varsv) 4151 dxv = dimyv 4152 dyv = dimxv 4153 dimxv = dxv 4154 dimyv = dyv 4155 4156 if len(dimxv.shape) == 3: 4157 dxget='1,2' 4158 elif len(dimxv.shape) == 2: 4159 dxget='0,1' 4160 elif len(dimxv.shape) == 1: 4161 dxget='0' 4162 else: 4163 print errormsg 4164 print ' ' + fname + ': shape of x-values:',dimxv.shape,'not ready!!' 4165 quit(-1) 4166 4167 if len(dimyv.shape) == 3: 4168 dyget='1,2' 4169 elif len(dimyv.shape) == 2: 4170 dyget='0,1' 4171 elif len(dimyv.shape) == 1: 4172 dyget='0' 4173 else: 4174 print errormsg 4175 print ' ' + fname + ': shape of y-values:',dimyv.shape,'not ready!!' 4176 quit(-1) 4177 4178 x,y = dxdy_lonlat(dimxv,dimyv,dxget,dyget) 4179 4180 plt.rc('text', usetex=True) 3927 4181 3928 4182 vsend = np.zeros((2), dtype=np.float) … … 3991 4245 vsend[1] = vs[1] 3992 4246 3993 plt.rc('text', usetex=True)3994 3995 plt.pcolormesh(x, y, varsv, cmap=plt.get_cmap(colorbar), vmin=vsend[0], vmax=vsend[1])3996 cbar = plt.colorbar()3997 3998 if not mapv is None:3999 if colorbar == 'gist_gray':4000 m.drawcoastlines(color="red")4001 else:4002 m.drawcoastlines()4003 4004 meridians = pretty_int(nlon,xlon,5)4005 m.drawmeridians(meridians,labels=[True,False,False,True])4006 parallels = pretty_int(nlat,xlat,5)4007 m.drawparallels(parallels,labels=[False,True,True,False])4008 4009 plt.xlabel('W-E')4010 plt.ylabel('S-N')4011 else:4012 plt.xlabel(variables_values(dimn[1])[0].replace('_','\_') + ' (' + \4013 units_lunits(dimxu) + ')')4014 plt.ylabel(variables_values(dimn[0])[0].replace('_','\_') + ' (' + \4015 units_lunits(dimyu) + ')')4016 4017 txpos = pretty_int(x.min(),x.max(),5)4018 typos = pretty_int(y.min(),y.max(),5)4019 txlabels = list(txpos)4020 for i in range(len(txlabels)): txlabels[i] = str(txlabels[i])4021 tylabels = list(typos)4022 for i in range(len(tylabels)): tylabels[i] = str(tylabels[i])4023 4024 # set the limits of the plot to the limits of the data4025 4026 if searchInlist(revas,'transpose'):4027 x0 = y4028 y0 = x4029 x = x04030 y = y04031 # print 'Lluis reva0:',reva0,'x min,max:',x.min(),x.max(),' y min,max:',y.min(),y.max()4032 4033 if reva0 == 'flip':4034 if reva.split('@')[1] == 'x':4035 plt.axis([x.max(), x.min(), y.min(), y.max()])4036 elif reva.split('@')[1] == 'y':4037 plt.axis([x.min(), x.max(), y.max(), y.min()])4038 else:4039 plt.axis([x.max(), x.min(), y.max(), y.min()])4040 else:4041 plt.axis([x.min(), x.max(), y.min(), y.max()])4042 4043 if mapv is None:4044 plt.xticks(txpos, txlabels)4045 plt.yticks(typos, tylabels)4046 4047 # units labels4048 cbar.set_label(vnames.replace('_','\_') + ' (' + units_lunits(uts) + ')')4049 4050 figname = '2Dfields_shadow'4051 graphtit = vtit.replace('_','\_').replace('&','\&')4052 4053 plt.title(graphtit)4054 4055 output_kind(kfig, figname, ifclose)4056 4057 return4058 4059 #Nvals=504060 #vals1 = np.zeros((Nvals,Nvals), dtype= np.float)4061 #for j in range(Nvals):4062 # for i in range(Nvals):4063 # vals1[j,i]=np.sqrt((j-Nvals/2)**2. + (i-Nvals/2)**2.)4064 4065 #plot_2D_shadow(vals1, 'var1', np.arange(50)*1., np.arange(50)*1., 'ms-1', \4066 # 'm', ['lat','lon'], 'rainbow', [0, Nvals], 'ms-1', 'test var1', 'pdf', 'None', \4067 # None, True)4068 #quit()4069 4070 def plot_2D_shadow_time(varsv,vnames,dimxv,dimyv,dimxu,dimyu,dimn,colorbar,vs,uts, \4071 vtit,kfig,reva,taxis,tpos,tlabs,ifclose):4072 """ Plotting a 2D field with one of the axes being time4073 varsv= 2D values to plot with shading4074 vnames= shading variable name for the figure4075 dim[x/y]v= values at the axes of x and y4076 dim[x/y]u= units at the axes of x and y4077 dimn= dimension names to plot4078 colorbar= name of the color bar to use4079 vs= minmum and maximum values to plot in shadow or:4080 'Srange': for full range4081 'Saroundmean@val': for mean-xtrm,mean+xtrm where xtrm = np.min(mean-min@val,max@val-mean)4082 'Saroundminmax@val': for min*val,max*val4083 'Saroundpercentile@val': for median-xtrm,median+xtrm where xtrm = np.min(median-percentile_(val),4084 percentile_(100-val)-median)4085 'Smean@val': for -xtrm,xtrm where xtrm = np.min(mean-min*@val,max*@val-mean)4086 'Smedian@val': for -xtrm,xtrm where xtrm = np.min(median-min@val,max@val-median)4087 'Spercentile@val': for -xtrm,xtrm where xtrm = np.min(median-percentile_(val),4088 percentile_(100-val)-median)4089 uts= units of the variable to shadow4090 vtit= title of the variable4091 kfig= kind of figure (jpg, pdf, png)4092 reva=4093 * 'transpose': reverse the axes (x-->y, y-->x)4094 * 'flip'@[x/y]: flip the axis x or y4095 taxis= Which is the time-axis4096 tpos= positions of the time ticks4097 tlabs= labels of the time ticks4098 ifclose= boolean value whether figure should be close (finish) or not4099 """4100 fname = 'plot_2D_shadow_time'4101 4102 if varsv == 'h':4103 print fname + '_____________________________________________________________'4104 print plot_2D_shadow_time.__doc__4105 quit()4106 4107 # Definning ticks labels4108 if taxis == 'x':4109 txpos = tpos4110 txlabels = tlabs4111 plxlabel = dimxu4112 typos = pretty_int(np.min(dimyv),np.max(dimyv),10)4113 tylabels = list(typos)4114 for i in range(len(tylabels)): tylabels[i] = str(tylabels[i])4115 plylabel = variables_values(dimn[0])[0].replace('_','\_') + ' (' + \4116 units_lunits(dimyu) + ')'4117 else:4118 txpos = pretty_int(np.min(dimxv),np.max(dimxv),10)4119 txlabels = list(txpos)4120 plxlabel = variables_values(dimn[1])[0].replace('_','\_') + ' (' + \4121 units_lunits(dimxu) + ')'4122 typos = tpos4123 tylabels = tlabs4124 plylabel = dimyu4125 4126 # Transposing/flipping axis4127 if reva.find('|') != 0:4128 revas = reva.split('|')4129 reva0 = ''4130 else:4131 revas = [reva]4132 reva0 = reva4133 4134 for rev in revas:4135 if rev[0:4] == 'flip':4136 reva0 = 'flip'4137 if len(reva.split('@')) != 2:4138 print errormsg4139 print ' ' + fname + ': flip is given', reva, 'but not axis!'4140 quit(-1)4141 else:4142 print " flipping '" + rev.split('@')[1] + "' axis !"4143 4144 if rev == 'transpose':4145 print ' reversing the axes of the figure (x-->y, y-->x)!!'4146 # Flipping values of variable4147 varsv = np.transpose(varsv)4148 dxv = dimyv4149 dyv = dimxv4150 dimxv = dxv4151 dimyv = dyv4152 4153 if len(dimxv.shape) == 3:4154 dxget='1,2'4155 elif len(dimxv.shape) == 2:4156 dxget='0,1'4157 elif len(dimxv.shape) == 1:4158 dxget='0'4159 else:4160 print errormsg4161 print ' ' + fname + ': shape of x-values:',dimxv.shape,'not ready!!'4162 quit(-1)4163 4164 if len(dimyv.shape) == 3:4165 dyget='1,2'4166 elif len(dimyv.shape) == 2:4167 dyget='0,1'4168 elif len(dimyv.shape) == 1:4169 dyget='0'4170 else:4171 print errormsg4172 print ' ' + fname + ': shape of y-values:',dimyv.shape,'not ready!!'4173 quit(-1)4174 4175 x,y = dxdy_lonlat(dimxv,dimyv,dxget,dyget)4176 4177 plt.rc('text', usetex=True)4178 4179 vsend = np.zeros((2), dtype=np.float)4180 # Changing limits of the colors4181 if type(vs[0]) != type(np.float(1.)):4182 if vs[0] == 'Srange':4183 vsend[0] = np.min(varsv)4184 elif vs[0][0:11] == 'Saroundmean':4185 meanv = np.mean(varsv)4186 permean = np.float(vs[0].split('@')[1])4187 minv = np.min(varsv)*permean4188 maxv = np.max(varsv)*permean4189 minextrm = np.min([np.abs(meanv-minv), np.abs(maxv-meanv)])4190 vsend[0] = meanv-minextrm4191 vsend[1] = meanv+minextrm4192 elif vs[0][0:13] == 'Saroundminmax':4193 permean = np.float(vs[0].split('@')[1])4194 minv = np.min(varsv)*permean4195 maxv = np.max(varsv)*permean4196 vsend[0] = minv4197 vsend[1] = maxv4198 elif vs[0][0:17] == 'Saroundpercentile':4199 medianv = np.median(varsv)4200 valper = np.float(vs[0].split('@')[1])4201 minv = np.percentile(varsv, valper)4202 maxv = np.percentile(varsv, 100.-valper)4203 minextrm = np.min([np.abs(medianv-minv), np.abs(maxv-medianv)])4204 vsend[0] = medianv-minextrm4205 vsend[1] = medianv+minextrm4206 elif vs[0][0:5] == 'Smean':4207 meanv = np.mean(varsv)4208 permean = np.float(vs[0].split('@')[1])4209 minv = np.min(varsv)*permean4210 maxv = np.max(varsv)*permean4211 minextrm = np.min([np.abs(meanv-minv), np.abs(maxv-meanv)])4212 vsend[0] = -minextrm4213 vsend[1] = minextrm4214 elif vs[0][0:7] == 'Smedian':4215 medianv = np.median(varsv)4216 permedian = np.float(vs[0].split('@')[1])4217 minv = np.min(varsv)*permedian4218 maxv = np.max(varsv)*permedian4219 minextrm = np.min([np.abs(medianv-minv), np.abs(maxv-medianv)])4220 vsend[0] = -minextrm4221 vsend[1] = minextrm4222 elif vs[0][0:11] == 'Spercentile':4223 medianv = np.median(varsv)4224 valper = np.float(vs[0].split('@')[1])4225 minv = np.percentile(varsv, valper)4226 maxv = np.percentile(varsv, 100.-valper)4227 minextrm = np.min([np.abs(medianv-minv), np.abs(maxv-medianv)])4228 vsend[0] = -minextrm4229 vsend[1] = minextrm4230 else:4231 print errormsg4232 print ' ' + fname + ": range '" + vs[0] + "' not ready!!!"4233 quit(-1)4234 print ' ' + fname + ': modified shadow min,max:',vsend4235 else:4236 vsend[0] = vs[0]4237 4238 if type(vs[0]) != type(np.float(1.)):4239 if vs[1] == 'range':4240 vsend[1] = np.max(varsv)4241 else:4242 vsend[1] = vs[1]4243 4244 4247 plt.pcolormesh(x, y, varsv, cmap=plt.get_cmap(colorbar), vmin=vsend[0], vmax=vsend[1]) 4245 4248 cbar = plt.colorbar()
Note: See TracChangeset
for help on using the changeset viewer.