• 【无标题】


    1. #导入模块
    2. import numpy as np
    3. import cartopy
    4. import matplotlib.pyplot as plt
    5. import cartopy.crs as ccrs
    6. from cartopy.feature import ShapelyFeature
    7. from cartopy.io.shapereader import Reader
    8. from cartopy.feature import NaturalEarthFeature,COLORS
    9. from matplotlib.cm import get_cmap
    10. from matplotlib.colors import from_levels_and_colors
    11. import matplotlib.ticker as mticker
    12. from matplotlib.animation import FuncAnimation
    13. from IPython.display import HTML
    14. from netCDF4 import Dataset
    15. from xarray import DataArray
    16. from wrf import getvar, interplevel, vertcross, vinterp, ALL_TIMES, CoordPair, xy_to_ll,\
    17. ll_to_xy, to_np, get_cartopy, latlon_coords, cartopy_xlim, cartopy_ylim
    18. import os
    19. import warnings
    20. warnings.filterwarnings('ignore')
    21. #读取文件(利用循环遍历文件夹内所有nc文件)
    22. wrf_dir = "/H/w_test/"
    23. wrf_file = ["wrfout_d01_2020-03-01_00.nc",
    24. "wrfout_d01_2021-03-01_00.nc",
    25. "wrfout_d01_2022-03-01_00.nc"]
    26. wrf_files = [os.path.abspath(os.path.join(wrf_dir,f)) for f in wrf_file]
    27. for f in wrf_files:
    28. if not os.path.exists(f):
    29. raise ValueError("{} does not exist."
    30. "check for typos or incorrect directory.".format(f))
    31. #读取文件夹内单一文件
    32. def single_wrf_file():
    33. global wrf_files
    34. return wrf_files[0]
    35. #读取文件夹内所有文件
    36. def multiple_wrf_files():
    37. global wrf_files
    38. return wrf_files
    39. #本案例只读取第一个nc文件
    40. file_path = single_wrf_file()
    41. wrf_f = Dataset(file_path)
    42. print(file_path)
    43. #print(wrf_f)
    44. #获取文件中的地形变量
    45. terrain = getvar(wrf_f, "ter", timeidx = 0)
    46. cart_proj = get_cartopy(terrain)
    47. #获取变量的经纬度坐标,并设置投影。
    48. lats, lons = latlon_coords(terrain)
    49. extent = [70, 140, 10,60] #限定绘图范围
    50. cart_proj = get_cartopy(terrain) #创建投影,对应数据集中terrain的投影
    51. #叠加中国地图
    52. china = "/china-boundary/china-boundary.shp"
    53. fig = plt.figure(figsize=(10,8))
    54. geo_axes = plt.axes(projection=cart_proj)
    55. #states = NaturalEarthFeature(category='cultural', scale='50m', facecolor='none',name='admin_1_states_provinces')
    56. cmap = cartopy.feature.ShapelyFeature(Reader(china).geometries(), crs=ccrs.PlateCarree(), edgecolor="r", facecolor="none")
    57. geo_axes.add_feature(cmap, linewidth=1)
    58. #geo_axes.add_feature(states, linewidth=0.9)
    59. #geo_axes.coastlines("50m", linewidth = 0.8)
    60. levels = np.arange(1, 8000, 500)
    61. plt.contour(to_np(lons), to_np(lats), to_np(terrain),
    62. levels=levels, colors="black", transform = ccrs.PlateCarree())
    63. plt.contourf(to_np(lons), to_np(lats),
    64. to_np(terrain), levels=levels,
    65. transform=ccrs.PlateCarree(),
    66. cmap=get_cmap("jet"))
    67. #plt.rcParams["font.sans-serif"]=["SimHei"]
    68. #添加刻度信息
    69. gl = geo_axes.gridlines(draw_labels = True, linestyle=":", crs=ccrs.PlateCarree(), linewidth=1, color="grey", x_inline=False, y_inline=False)
    70. #gl.top_labels = False #关闭上部经纬度标签
    71. gl.right_labels = False
    72. #gl.xlabels_bottom = True
    73. #gl.xformatter = LONGITUDE_FORMATTER #使横坐标转化为经纬度格式
    74. #gl.yformatter = LATITUDE_FORMATTER
    75. gl.xlocator = mticker.FixedLocator(np.arange(70,140,10))
    76. gl.ylocator = mticker.FixedLocator(np.arange(10,60,10))
    77. gl.xlabel_style ={"size":10, "color":"black"} #修改经纬度坐标网格字体大小
    78. gl.ylabel_style ={"size":10, "color":"black"} #修改经纬度坐标网格字体大小
    79. gl.rotate_labels = False
    80. #添加colorbar
    81. plt.colorbar(ax=geo_axes, shrink=0.58)
    82. plt.show()
    83. #保存图片
    84. #plt.savefig("../wrf-domain.jpg", dpi = 600)

  • 相关阅读:
    想干程序员这行的,真的很需要这个,T0 级别的《面试资料全集》--Java 问题收录及答案详解,持续更新中...
    OpenAI怎么写作「谷歌小发猫写作」
    DLT645-2007智能电表通讯规约 协议读取数据实战
    静态 友元 常量
    使用LLama和ChatGPT为多聊天后端构建微服务
    如何使用CloakQuest3r获取受安全服务保护的网站真实IP地址
    tomcat 的缓存机制
    2022TWS蓝牙耳机推荐,盘点600元真无线蓝牙耳机
    window.requestAnimationFrame Web3D渲染帧率控制
    智慧公厕:提升城市形象,为市民带来极致体验
  • 原文地址:https://blog.csdn.net/A18040554844/article/details/133581467