- 积分
- 22715
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-7-23
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2020-4-6 17:56:03
|
显示全部楼层
您好,其实原理是完全一样的,只不过“欺骗”电脑,让纵坐标Y轴从纬度变成高度。画的时候仍然是二维数据,一个x的坐标和一个Y的坐标,对应一个值。这样就形成了一个二维的场。
我之前写了一个比较长的脚本,部分功能是实现画剖面,贴在这里了:- # -*- coding: utf-8 -*-
- import numpy as np
- import netCDF4 as nc
- # from mpl_toolkits.basemap import Basemap
- import matplotlib.pyplot as plt
- import time2page as tp
- import thetase
- import matplotlib as mpl
- # print "fadsfasdfasd"
- # exit()
- # filesample=open(r'D:\liaoningrenying\dongbeilengwo\thin_reso_data\data\filenamelist.txt')
- # filesample=np.loadtxt(r'h:\liaoningrenying\dongbeilengwo\thin_reso_data\data\filenamelist.txt',dtype='string',skiprows=15)
- filesample=np.loadtxt(r'h:\liaoningrenying\dongbeilengwo\thin_reso_data\data\filenamelist_filtered_by_rain_trend_newest.txt',dtype='string',skiprows=4)
- ######################下边是filenamelist.txt的内容,复制到此
- '''
- #注释:第一列为以时间命名的文件名,第二列为这个时刻在这个文件里是第几个时次。默认这个时间是降水开始的时间,但实际上需要更改,可另附一列。
- #第三列是6h降水最强、范围最大的时刻,基本都是位于中部;第四列的意义:第二列+第四列的数,即为第三列时间在该nc文件里对应的时次;第五列是6h降水开始在辽宁出现的时刻,紧接着是降水出现在辽宁的哪里
- #第七列是降水开始的时间相对于降水最长的时间,向前了几个时次
- #06z08jun2011.nc 10 由于没有对应的降水数据,改为00z14may2012.nc 9 12051408
- 00z10sep2008.nc 9 08091014 1 08090920 西部 -3
- 00z14may2012.nc 9 12051408 0 12051320 西部 -2
- 06z19apr2005.nc 10 05041920 1 05041902 西部 -3
- 12z04nov2006.nc 11 06110420 0 06110414 西部 -1
- 12z07jun2006.nc 11 06060714 -1 06060708 西部 -1
- 18z03may2008.nc 12 08050408 1 08050320 西部 -2
- 18z18may2006.nc 12 06051714 -6 06051708 西部 -1
- '''
- # print filesample
- # exit()
- dirthindata=r'h:\liaoningrenying\dongbeilengwo\thin_reso_data\data\\'
- def pingjun(gaokong):
- return np.mean(gaokong,axis=0)
- # lagt=-4
- for lagt in [-4,-3,-2,-1,0,1,2,3,4]:
- # lagt=-1;
- print lagt
- if lagt==-4:timelag='-24h'
- if lagt==-3:timelag='-18h'
- if lagt==-2:timelag='-12h'
- if lagt==-1:timelag='-06h'
- if lagt==0:timelag='+00h'
- if lagt==1:timelag='+06h'
- if lagt==2:timelag='+12h'
- if lagt==3:timelag='+18h'
- if lagt==4:timelag='+24h'
- z,w,t,d,u,v,r,clwc,ciwc,cc=[],[],[],[],[],[],[],[],[],[]
- vor=[]
- for file_pagenum in filesample:
- filename,page_origin,heavyrain_time,delta_p,delta_p2=file_pagenum[0],file_pagenum[1],file_pagenum[2],file_pagenum[3],file_pagenum[6];
- page=int(page_origin)+int(delta_p)+int(delta_p2) #这个page是6h降水开始出现的时刻
- # page=int(page_origin)+int(delta_p) #这个page是6h降水最强的时刻
- page=page-1 ###表示20时发现有降水/降水达到最强,则14时开始降水/降水加强
- page=page-1 ####再减1是因为对应的第1页在list里对应第0个时间维,换算成list数
- page=int(page)+lagt
- # print page,page_origin,delta_p,lagt
- # print filename,page
- ncfile=nc.Dataset(dirthindata+filename,'r')
- z1,w1,t1,d1,u1,v1,r1,clwc1,ciwc1,cc1=ncfile['z'][page,:,:,:],ncfile['w'][page,:,:,:],ncfile['t'][page,:,:,:],ncfile['d'][page,:,:,:],ncfile['u'][page,:,:,:],\
- ncfile['v'][page,:,:,:],ncfile['r'][page,:,:,:],ncfile['clwc'][page,:,:,:],ncfile['ciwc'][page,:,:,:],ncfile['cc'][page,:,:,:]
- vor1=ncfile['vo'][page,:,:,:]
- # exit()
- lats,lons,levs=ncfile['latitude'][:],ncfile['longitude'][:],ncfile['level'][:]
- z.append(z1);w.append(w1);t.append(t1);d.append(d1);u.append(u1)
- v.append(v1);r.append(r1);clwc.append(clwc1);ciwc.append(ciwc1)
- cc.append(cc1);vor.append(vor1)
- # print ncfile
- ncfile.close()
- # print lons
- z,w,t,d,u,v,r,clwc,ciwc,cc,vor=map(pingjun,(z,w,t,d,u,v,r,clwc,ciwc,cc,vor)) ####z shape :23 ,201,201
- lon,lat=np.meshgrid(lons,lats)
- qvdivs,qxs,qys,qss=[],[],[],[]
- for i in range(0,23):
- qvdiv_i,qx_i,qy_i,qs_i=thetase.qvdiv((lon,lat,u[i,:,:],v[i,:,:],t[i,:,:],r[i,:,:],levs[i]))
- qvdivs.append(qvdiv_i);qxs.append(qx_i);qys.append(qy_i);qss.append(qs_i)
- qvdivs=np.array(qvdivs);qxs=np.array(qxs);qys=np.array(qys);qss=np.array(qss)
- # print np.array(qvdivs)*1e6
- # exit()
- ######do slice cross-section#####
- # plt.figure(figsize=(1,1.2))
- fig,ax=plt.subplots(1,1)
- # plt.rcParams["figure.figsize"] = (10,14)
- # print "do cross-section:\n" \
- # "First ,latitude=41.5,then longitude=",lon_cross_measure
- # nn=list(lats).index(41.5)
- # def latslice(array):
- # a=array[:,nn,:]
- # return a
- # z,w,d,u,v,r,clwc,ciwc=map(latslice,(z,w,d,u,v,r,clwc,ciwc))
- #
- # xx,zz=np.meshgrid(lons,levs)
- # contour=ax.contour(xx,zz,w,colors='k');contour.clabel(fmt='%6.4f',inline_spacing=0,fontsize=7)
- # ax.set_xlim(118,127)
- # plt.show()
- # lon_cross_measure=123
- # nn=list(lons).index(lon_cross_measure)
- # def lonslice(array):
- # a=array[:,:,nn]
- # return a
- # #
- # z,w,t,d,u,v,r,clwc,ciwc,cc,vor=map(lonslice,(z,w,t,d,u,v,r,clwc,ciwc,cc,vor))
- # qvdiv,qx,qy,qs=map(lonslice,(qvdivs,qxs,qys,qss))
- # yy,zz=np.meshgrid(lats,levs)
- print clwc
- # exit()
- lat_cross_measure=41
- nn=list(lats).index(lat_cross_measure)
- def latslice(array):
- a=array[:,nn,:]
- return a
- z,w,t,d,u,v,r,clwc,ciwc,cc,vor=map(latslice,(z,w,t,d,u,v,r,clwc,ciwc,cc,vor))
- qvdiv,qx,qy,qs=map(latslice,(qvdivs,qxs,qys,qss))
- yy,zz=np.meshgrid(lons,levs)
- print clwc
- # exit()
- ts,e,ei,ew=thetase.rh_t_p_to_thetase((r,t,zz)) #,norm=mpl.colors.Normalize(vmin=300, vmax=330)
- de=e-ei;ew_ei=ew-ei
- ##### 计算非绝热加热潜热
- Cp=1.005
- L=(2500-2.4*(t-273.15))*1e3
- dqsdp=np.gradient(qs,axis=0)/np.gradient(levs)[:,None]/100
- # print dqsdp
- # exit()
- theta=t*(1000/levs)[:,None]**0.285
- Hcs=-1.0/(Cp*t/theta)*L*w*dqsdp
- # np.savetxt('test_Q.txt',Hcs)
- # exit()
- ##### output binary of ts for grads verifying
- # np.float32((r[::-1,::-1],t[::-1,::-1],v[::-1,::-1],w[::-1,::-1])).tofile('thetase_output_by_python_for_grads_verification.grd')
- # # print type(r[1,1])
- # # print ts.shape
- # print yy,'\n',zz[::-1,1]
- # exit()
- # tshade=ax.contourf(yy,zz,ts,range(295,350,3),cmap=plt.get_cmap('jet'));plt.colorbar(tshade)
- # thetase_contour=ax.contour(yy,zz,ts,range(295,350,5),colors='gray');thetase_contour.clabel(fmt='%3d',inline_spacing=-10,fontsize=14)
- # rcontour=ax.contour(yy,zz,r,range(10,91,10),colors='blue');rcontour.clabel(fmt='%02d',inline_spacing=-5,fontsize=12)
- # deshade=ax.contourf(yy,zz,de,[0,0.1,0.2,0.3,0.4],cmap=plt.get_cmap('coolwarm'));plt.colorbar(deshade)
- decontour=ax.contour(yy,zz,de,[0,0.1,0.2,0.3,0.4],colors='k');decontour.clabel(inline_spacing=-10,fontsize=8)#,[0,0.1,0.2,0.3,0.4]
- # qvdivshade=ax.contourf(yy,zz,qvdiv*1e6,range(-50,21,10),cmap=plt.get_cmap('coolwarm'));colorbar_instance=plt.colorbar(qvdivshade)
- # print colorbar_instance.ColorbarBase
- # exit()
- # vor_contour=ax.contour(yy,zz,vor*1e5,range(-10,11,2),colors='brown');vor_contour.clabel(fmt='%3d')
- # Hcs_shade=ax.contourf(yy,zz,Hcs,range(-300,301,50),cmap='coolwarm',extend='both');#plt.colorbar(Hcs_shade,extendfrac=0.2,orientation='horizontal')
- # Hcs_contour=ax.contour(yy,zz,Hcs);Hcs_contour.clabel()
- clwc_shade=ax.contourf(yy,zz,clwc*1e5,range(3,16,3),cmap=plt.get_cmap('Blues'),extend='both');#plt.colorbar(clwc_shade,extendfrac=0.2,orientation='horizontal') ####对于clwc
- # ciwc_shade=ax.contourf(yy,zz,ciwc*1e5,range(2,11,2),cmap=plt.get_cmap('Blues'),extend='both');plt.colorbar(ciwc_shade,extendfrac=0.2) ####对于ciwc
- # clwc_contour=ax.contour(yy,zz,clwc*1e5,range(0,21,2),colors='green');clwc_contour.clabel(fmt='%4.0f',inline_spacing=-10,fontsize=12)
- # clwc_contour=ax.contour(yy,zz,ciwc*1e5,range(2,11,2),colors='k');clwc_contour.clabel(fmt='%4.0f',inline_spacing=-10,fontsize=12)
- # quiver=ax.quiver(yy[:,::4],zz[:,::4],(u[:,::4]),-w[:,::4]*100,color='k',headwidth=3,headlength=4,width=0.002)
- tscontour=ax.contour(yy,zz,t-273.15,[-15,-7,-4,0],colors='r');tscontour.clabel(fmt='%d$^\circ$C',inline_spacing=-5,fontsize=12)
- # tscontour=ax.contour(yy,zz,t,[273.15],colors='r');tscontour.clabel(fmt='%6.2f',inline_spacing=-10,fontsize=12)
- # decontour=ax.contour(yy,zz,de*100,range(0,51,5),colors='k');decontour.clabel(fmt='%2d' ,inline_spacing=-10,fontsize=8) #,(0,0.5,1.0,1.5)
- # decontour=ax.contour(yy,zz,de,[0],colors='gray');decontour.clabel(inline_spacing=-10,fontsize=8) #,(0,0.5,1.0,1.5) np.arange(0,0.1,0.01)
- vor[vor<0]=np.nan;v0contour=ax.contour(yy,zz,v*vor,[0],colors='brown',linewidths=4)
- # qvdivcontour=ax.contour(yy,zz,qvdiv*1e5,(-4,-3,-2,-1),colors='darkgreen',linewidths=1,linestyles='-');qvdivcontour.clabel(fmt='%3d' ,inline_spacing=-5,fontsize=8)
- # cc_contour=ax.contour(yy,zz,cc,np.arange(0.1,1.1,0.2),colors='k');cc_contour.clabel(fmt='%3.1f',inline_spacing=-5,fontsize=12)
- # wcontour=ax.contour(yy,zz,w,np.arange(-1.0,1.1,0.2),colors='brown');wcontour.clabel(fmt='%5.1f',inline_spacing=-10,fontsize=12)
- zcontour=ax.contour(yy,zz,z/9.8,(500,1000,2000,3000,4300,5500,6500),colors='orange',linestyles='--');zcontour.clabel(fmt='%4d',inline_spacing=0,fontsize=8)
- # ciwc_contour=ax.contour(yy,zz,ciwc*1e5,range(0,10),colors='purple');ciwc_contour.clabel(fmt='%4.0f',inline_spacing=-10,fontsize=12)
- # print np.shape(-w*100)
- # it=2
- # stream=ax.streamplot(yy,zz,v,-100.0*w,density=2)
- # stream=plt.streamplot(yy,zz,v,100*w,density=2)
- #ax.set_ylim(500,1000)
- # plt.xticks(range(30,56,5),[str(x)+u'°N' for x in range(30,56,5)],fontsize=13)
- plt.xticks(range(110,131,5),[str(x)+u'°E' for x in range(110,131,5)],fontsize=13);ax.set_xlim(110,130)
- plt.yticks((1000,925,900,850,800,700,600,500,400,300,200),fontsize=13)
- # plt.xlabel('Latitude',fontsize=15);plt.ylabel('Level',fontsize=15)
- # ax.set_xlim(38,44);#ax.set_ylim(200,1000)
- ax.set_ylim(300,1000)
- plt.gca().invert_yaxis() #######把y坐标轴反转
- # plt.gca().invert_xaxis()
- plt.grid(True)
- # plt.title(u'Thetase cross-section plot along 123°E \n'+timelag,fontsize=18)
- # plt.savefig(r'H:\liaoningrenying\dongbeilengwo\\'+str(lat_cross_measure)+r'_cross_CLWC\\'+timelag+u'假相当位温_垂直环流_液水冰水含量_剖线'+str(lat_cross_measure)+'.png')
- plt.savefig(r'H:\liaoningrenying\dongbeilengwo\\'+str(lat_cross_measure)+r'_cross_CLWC\\'+timelag+u'假相当位温_垂直环流_液水冰水含量_剖线'+str(lat_cross_measure)+'.png')
- # plt.savefig(r'H:\liaoningrenying\dongbeilengwo\\'+str(lat_cross_measure)+r'_cross_CLWC\\'+timelag+u'假相当位温_垂直环流_CLWC_e-ei_剖线'+str(lat_cross_measure)+'.png')
- # plt.savefig(r'H:\liaoningrenying\dongbeilengwo\\'+str(lon_cross_measure)+r'_cross_CLWC\\'+timelag+u'假相当位温_垂直环流_液水冰水含量_剖线'+str(lon_cross_measure)+'.png',dpi=600)
- # print r'H:\liaoningrenying\dongbeilengwo\\'+str(lon_cross_measure)+r'_cross\\'+timelag+u'假相当位温_垂直环流_液水冰水含量_剖线'+str(lon_cross_measure)+'.png'
- # plt.savefig(r'H:\liaoningrenying\dongbeilengwo\\'+str(lat_cross_measure)+r'_cross_Q\\'+timelag+u'_Q_垂直环流_液水冰水含量_剖线'+str(lat_cross_measure)+'.png')
- # plt.show()
- plt.close()
- # break
复制代码
|
|