




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
第python對站點數(shù)據(jù)做EOF且做插值繪制填色圖目錄前言讀取存儲的數(shù)據(jù)插值EOF處理定義繪圖函數(shù)并繪圖展示結(jié)果總結(jié)
前言
讀取站點資料數(shù)據(jù)對站點數(shù)據(jù)進行插值,插值到規(guī)則網(wǎng)格上繪制EOF第一模態(tài)和第二模態(tài)的空間分布圖繪制PC序列
關(guān)于插值,這里主要提供了兩個插值函數(shù),一個是一般常用的規(guī)則網(wǎng)格插值:
griddata
另一個是metpy中的:
inverse_distance_to_grid()
本來只是測驗一下不同插值方法,但是發(fā)現(xiàn)兩種插值方法的結(jié)果差別很大,由于對于站點數(shù)據(jù)處理較少,所以不太清楚具體原因。如果有知道的朋友可以告知一下,不甚感謝!
本次數(shù)據(jù)存儲的文件格式為.txt,讀取的方法是通過pandas.read_csv()
同時,之前一直嘗試使用proplot進行繪圖,較長時間不用matplotlib.pyplot繪圖了,也當(dāng)做是復(fù)習(xí)一下繪圖過程。
繪圖中的代碼主要通過封裝函數(shù),這樣做的好處是大大減少了代碼量。
導(dǎo)入必要的庫:
fromeofs.standardimportEof
importmatplotlib.pyplotasplt
importnumpyasnp
fromerpolateimportgriddata
importpandasaspd
importmatplotlib.pyplotasplt
importcartopy.crsasccrs
importcartopy.featureascfeature
fromcartopy.mpl.tickerimportLongitudeFormatter,LatitudeFormatter
fromerpolateimportinverse_distance_to_grid
出現(xiàn)找不到庫的報錯,這里使用condainstallpackagename安裝一下就好
讀取存儲的數(shù)據(jù)
#####################readstationdata##########################################
path=r'D:/data.txt'
file=pd.read_csv(path,sep="\t",
header=None,
names=['station','lat','lon','year','data'],
na_values=-99.90)
data=file['data'].to_numpy()
lon=file['lon'].to_numpy()
lat=file['lat'].to_numpy()
year=file['year'].to_numpy()
station=file['station'].to_numpy()
year_max=np.max(year)
year_min=np.min(year)
year_range=np.arange(year_min,year_max+1,1)
data_all=data.reshape(70,53)
lon_all=lon.reshape(70,53)/100
lat_all=lat.reshape(70,53)/100
lon_real=lon_all[:,0]
lat_real=lat_all[:,0]
這里將讀取的數(shù)據(jù)全部轉(zhuǎn)為array格式,方便查看以及后續(xù)處理。本來存儲的文件中是沒有相關(guān)的經(jīng)度、緯度、站點、時間的名稱的,這里我是自己加在上面方面數(shù)據(jù)讀取的。
本次處理的數(shù)據(jù)包含70個站點,53年
插值
#####################interpdata##########################################
###interpdatatotargetgrid
###settargetgrid
lon_target=np.arange(115,135.5,0.5)
lat_target=np.arange(38,55.5,0.5)
x_t,y_t=np.meshgrid(lon_target,lat_target)
z=np.zeros((len(year_range),lat_target.shape[0],lon_target.shape[0]))
foriinrange(len(year_range)):
print(i)
#z[i]=inverse_distance_to_grid(lon_real,lat_real,
#data_all[:,i],
#x_t,y_t,r=15,min_neighbors=3)
z[i]=griddata((lon_real,lat_real),
data_all[:,i],
(x_t,y_t),method='cubic')
這里顯示了使用griddata()的插值過程,metpy的過程注釋掉了,需要測試的同學(xué)之間取消注釋即可。
注意點:插值過程需要先設(shè)置目標(biāo)的插值網(wǎng)格。
EOF處理
#計算緯度權(quán)重
lat_new=np.array(lat_target)
coslat=np.cos(np.deg2rad(lat_new))
wgts=np.sqrt(coslat)[...,np.newaxis]
#創(chuàng)建EOF分解器
solver=Eof(z,weights=wgts)
eof=solver.eofsAsCorrelation(neofs=2)
#此處的neofs的值是我們需要的空間模態(tài)數(shù)
pc=solver.pcs(npcs=2,pcscaling=1)#方差
var=solver.varianceFraction(neigs=2)
這里沒啥好說的,需要幾個模態(tài)自由選擇即可
定義繪圖函數(shù)并繪圖
#####################defplotfunction##########################################
defcontourf(ax,i,level,cmap):
extents=[115,135,35,55]
ax.set_extent(extents,crs=proj)
ax.add_feature(cfeature.LAND,edgecolor='black',facecolor='silver',
ax.add_feature(cfeature.LAKES,edgecolor='black',facecolor='w',
ax.add_feature(cfeature.BORDERS)
xtick=np.arange(extents[0],extents[1],5)
ytick=np.arange(extents[2],extents[3],5)
ax.coastlines()
tick_proj=ccrs.PlateCarree()
c=ax.contourf(lon_target,lat_target,eof[i],
transform=ccrs.PlateCarree(),
levels=level,
extend='both',
cmap=cmap)
ax.set_xticks(xtick,crs=tick_proj)
ax.set_xticks(xtick,crs=tick_proj)
ax.set_yticks(ytick,crs=tick_proj)
ax.set_yticks(ytick,crs=tick_proj)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
plt.yticks(fontproperties='TimesNewRoman',size=10)
plt.xticks(fontproperties='TimesNewRoman',size=10)
ax.tick_params(which='major',direction='out',
length=4,width=0.5,
pad=5,bottom=True,left=True,right=True,top=True)
ax.tick_params(which='minor',direction='out',
bottom=True,left=True,right=True,top=True)
ax.set_title('EOF'+str(i),loc='left',fontsize=15)
returnc
defp_line(ax,i):
ax.set_title('pc'+str(i)+'',loc='left',fontsize=15)
#ax.set_ylim(-3.5,3.5)
ax.axhline(0,line)
ax.plot(year_range,pc[:,i],color='blue')
ax.set_ylim(-3,3)
#####################plot##########################################
fig=plt.figure(figsize=(8,6),dpi=200)
proj=ccrs.PlateCarree()
contourf_1=fig.add_axes([0.02,0.63,0.5,0.3],projection=proj)
c1=contourf(contourf_1,0,np.arange(0.7,1,0.05),plt.cm.bwr)
plot_1=fig.add_axes([0.45,0.63,0.5,0.3])
p_line(plot_1,0)
contourf_2=fig.add_axes([0.02,0.15,0.5,0.3],projection=proj)
c2=contourf(contourf_2,1,np.arange(-0.5,0.6,0.1),plt.cm.bwr)
plot_2=fig.add_axes([0.45,0.15,0.5,0.3],)
p_line(plot_2,1)
cbposition1=fig.add_axes([0.16,0.55,0.22,0.02])
cb=fig.colorbar(c1,cax=cbposition1,
orientation='horizontal',format='%.1f')
cb.ax.tick_params(which='both',direction='in')
cbposition2=fig.add_axes([0.16,0.08,0.22,0.02])
cb2=fig.colorbar(c2,cax=cbposition2,
orientation='horizontal',format='%.1f')
cb2.ax.tick_params(which='both',direction='in')
plt.show()
這里將大部分重復(fù)的繪圖代碼,進行了封裝,通過封裝好的函數(shù)進行調(diào)用,大大簡潔了代碼量。相關(guān)的封裝過程之前也有講過,可以翻看之前的記錄。
展示結(jié)果
使用griddata的繪圖結(jié)果:
使用metpt插值函數(shù)的結(jié)果:
附上全部的繪圖代碼:
#-*-coding:utf-8-*-
CreatedonFriSep2317:46:422025
@author:Administrator
fromeofs.standardimportEof
importmatplotlib.pyplotasplt
importnumpyasnp
fromerpolateimportgriddata
importpandasaspd
importmatplotlib.pyplotasplt
importcartopy.crsasccrs
importcartopy.featureascfeature
fromcartopy.mpl.tickerimportLongitudeFormatter,LatitudeFormatter
fromerpolateimportinverse_distance_to_grid
#####
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中考?xì)v史總復(fù)習(xí)《世界歷史》九年級下冊 (世界現(xiàn)代史)全冊知識提綱
- 廚藝展示活動方案
- 《課程普及與自主》課件
- 車輛定金簽字合同協(xié)議
- 轉(zhuǎn)讓市區(qū)公墓合同協(xié)議
- 木片購銷協(xié)議書
- 農(nóng)村土地合作開發(fā)畜牧養(yǎng)殖協(xié)議
- 更換原件協(xié)議書
- 歺廳股份協(xié)議書
- 產(chǎn)品定制及購銷協(xié)議合同書
- 2024年版《安全生產(chǎn)法》
- 汽車吊吊裝施工方案及流程
- XXX防校園欺凌工作領(lǐng)導(dǎo)小組及職責(zé)
- 物業(yè)服務(wù)管理培訓(xùn)課件
- 乳腺癌中醫(yī)特色護理
- 2024年山西省華遠(yuǎn)國際陸港集團有限公司招聘筆試參考題庫含答案解析
- 沐足樓面服務(wù)員禮貌禮節(jié)培訓(xùn)
- 水肥一體化施工組織設(shè)
- 新疆特崗幼兒園學(xué)前教育模擬測試題
- 教育研究方法教育行動研究法
- 藥浴嬰幼兒計劃書
評論
0/150
提交評論