wrfout数据兰伯特投影python读取

1. 利用get_cartopy读取

# 导入必要的库
import netCDF4 as nc
from wrf import getvar, get_cartopy

# 打开WRF输出文件
wrfout_file = './wrfout_d01_2022-06-02_00_00_00'  # 替换为你的WRF文件路径
nc_file = nc.Dataset(wrfout_file)

# 使用wrf-python从WRF文件中获取投影信息
wrf_proj = get_cartopy(getvar(nc_file, "T2"))

2.利用cartopy.crs创建兰伯特投影

从WRF输出文件中读取的实际投影参数,

import netCDF4 as nc

# 打开wrfout文件
wrfout_file = './wrfout_d01_2022-06-02_00_00_00'
nc_file = nc.Dataset(wrfout_file)

# 获取并打印地图投影相关信息
map_proj = nc_file.getncattr('MAP_PROJ')  # 获取地图投影类型
cen_lat = nc_file.getncattr('CEN_LAT')  # 获取中心点纬度
cen_lon = nc_file.getncattr('CEN_LON')  # 获取中心点经度
true_lat1 = nc_file.getncattr('TRUELAT1')  # 获取第一个标准纬度
true_lat2 = nc_file.getncattr('TRUELAT2')  # 获取第二个标准纬度
stand_lon = nc_file.getncattr('STAND_LON')  # 获取标准经度

# 打印投影信息
print(f"地图投影类型(数字代码):{map_proj}")
print(f"中心点纬度:{cen_lat}")
print(f"中心点经度:{cen_lon}")
print(f"第一个标准纬度:{true_lat1}")
print(f"第二个标准纬度:{true_lat2}")
print(f"标准经度:{stand_lon}")

 输出结果

地图投影类型(数字代码):1 ##1代表兰勃特等角圆锥投影(Lambert Conformal Conic),2 代表极地立体投影(Polar Stereographic)等,具体的映射关系可以在WRF文档或相关资源中找到。
中心点纬度:36.31600570678711
中心点经度:102.81600952148438
第一个标准纬度:36.316001892089844
第二个标准纬度:36.316001892089844
标准经度:102.81600189208984

然后利用cartopy.crs进行创建

import cartopy.crs as ccrs

# 创建兰伯特等角圆锥投影对象
lambert_proj = ccrs.LambertConformal(central_longitude=cen_lon,
                                     central_latitude=cen_lat,
                                     standard_parallels=(true_lat1,),
                                     globe=None)

3. 绘制上述投影地图进行对比

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature

# 创建绘图对象
fig, axarr = plt.subplots(1, 2, figsize=(20, 10),
                          subplot_kw={'projection': wrf_proj})

# 左图:使用wrf-python读取的投影信息绘制地图
ax = axarr[0]
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.set_extent([73, 135, 18, 54], crs=ccrs.PlateCarree())
ax.set_title('WRF Projection (get_cartopy)')
ax.gridlines(draw_labels=True, color='gray', linestyle='--', linewidth=0.5)

# 右图:使用手动设置的投影参数绘制地图
ax = axarr[1]
ax = plt.subplot(1, 2, 2, projection=lambert_proj))
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.set_extent([73, 135, 18, 54], crs=ccrs.PlateCarree())
ax.set_title('Manual Lambert Conformal Projection')
ax.gridlines(draw_labels=True, color='gray', linestyle='--', linewidth=0.5)

# 显示图形
plt.tight_layout()  # 调整子图间距
plt.show()

结果:

4. 完整代码

# 导入必要的库
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import netCDF4 as nc
from wrf import getvar, get_cartopy

# 打开WRF输出文件
wrfout_file = './wrfout_d01_2022-06-02_00_00_00'  # 替换为你的WRF文件路径
nc_file = nc.Dataset(wrfout_file)

# 使用wrf-python从WRF文件中获取投影信息
wrf_proj = get_cartopy(getvar(nc_file, "T2"))

# 使用cartopy.crs创建兰伯特等角圆锥投影
# 获取地图投影相关信息
map_proj = nc_file.getncattr('MAP_PROJ')  # 获取地图投影类型
cen_lat = nc_file.getncattr('CEN_LAT')  # 获取中心点纬度
cen_lon = nc_file.getncattr('CEN_LON')  # 获取中心点经度
true_lat1 = nc_file.getncattr('TRUELAT1')  # 获取第一个标准纬度
true_lat2 = nc_file.getncattr('TRUELAT2')  # 获取第二个标准纬度
stand_lon = nc_file.getncattr('STAND_LON')  # 获取标准经度
# 创建投影
lambert_proj = ccrs.LambertConformal(central_longitude=cen_lon,
                                     central_latitude=cen_lat,
                                     standard_parallels=(true_lat1,),
                                     globe=None)

# 绘图对比
# 创建绘图对象
fig, axarr = plt.subplots(1, 2, figsize=(20, 10),
                          subplot_kw={'projection': wrf_proj})

# 左图:使用wrf-python读取的投影信息绘制地图
ax = axarr[0]
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.set_extent([73, 135, 18, 54], crs=ccrs.PlateCarree())
ax.set_title('WRF Projection (get_cartopy)')
ax.gridlines(draw_labels=True, color='gray', linestyle='--', linewidth=0.5)

# 右图:使用手动设置的投影参数绘制地图
ax = axarr[1]
ax = plt.subplot(1, 2, 2, projection=lambert_proj)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.set_extent([73, 135, 18, 54], crs=ccrs.PlateCarree())
ax.set_title('Manual Lambert Conformal Projection')
ax.gridlines(draw_labels=True, color='gray', linestyle='--', linewidth=0.5)

# 显示图形
plt.tight_layout()  # 调整子图间距
plt.show()

5. 本文中使用库的版本信息

windows 系统下查看信息方法

! conda list | findstr matplotlib
! conda list | findstr cartopy
! conda list | findstr netCDF4
! conda list | findstr wrf-python
! python --version

输出结果

matplotlib                3.7.2           py311h1ea47a8_0    conda-forge
matplotlib-base           3.7.2           py311h6e989c2_0    conda-forge
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
cartopy                   0.21.1          py311h178a126_1    conda-forge
wrf-python                1.3.4.1         py311hc52a59b_3    conda-forge
Python                    3.11.4

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值