ctl和grd文件转nc并作图

本文参考了CSDN以及气象家园上的一些代码。

one step:进入必要库

from xgrads import CtlDescriptor, open_CtlDataset
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import cartopy.io.shapereader as shpreader
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter #导入Cartopy专门提供的经纬度的Formatter

two step:文件类型转换

# 将grd转化为nc文件
ds1 = open_CtlDataset(r'D:/practice/zc.ctl', encoding='utf-8')
ctl1 = CtlDescriptor(file=r'D:/practice/zc.ctl', encoding='utf-8')
ds1.attrs['pdef'] = 'None'
ds1.to_netcdf(r'D:/practice/Output/zc.nc')

three step:读取nc文件

filename1 = 'D:/practice/Output/zc.nc'
ds1 = xr.open_dataset(filename1)
lon = np.array(ds1['lon'])
lat = np.array(ds1['lat'])
zc = np.array(ds1['zc'])

................................................数据处理较简单,故省略...................................................

four step:绘图设置

china = shpreader.Reader('E:/Shp/Shp/世界国家-我方标准界线 (1)/世界国家.shp').geometries()
# 设置投影
fig = plt.figure(dpi=100) #不设置画布大小
ax3 = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree(central_longitude=180))
ax3.set_xticks(x_extent, crs=ccrs.PlateCarree())#添加经纬度
ax3.set_yticks(y_extent, crs=ccrs.PlateCarree())
c3 = ax3.contourf(lon, lat, z, levels=np.arange(5200, 6000, 100), vmin=5200, vmax=6000, transform=ccrs.PlateCarree(),
                  cmap='RdBu_r',extend = 'both')
ax3.add_geometries(china, ccrs.PlateCarree(), facecolor='none', edgecolor='dimgray', linewidth=0.5, zorder=2)
d3 = ax3.quiver(lon, lat, u, v, color='black', width=0.0018, scale=600, headwidth=4, headlength=5,transform=ccrs.PlateCarree())
ax3.tick_params(labelsize=5)
# ax3.set_ylabel("Forecast2 uvwind HGT")
ax3.set_title('Forecast uvwind HGT', loc='left', fontsize=6)
o=plt.colorbar(c3,shrink=1 ,orientation='horizontal')
o.ax.tick_params(labelsize=6)
o.set_ticks([5200, 5400,5600,5800])
gl = ax3.gridlines(alpha=0.5, linestyle='--') # , draw_labels=True, dms=True, x_inline=False, y_inline=False)
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax3.xaxis.set_major_formatter(lon_formatter)
ax3.yaxis.set_major_formatter(lat_formatter)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值