将降水数据绘制到matplotlib底图地图上

问题描述 投票:0回答:1

受到来自this website的填充轮廓的示例Plot峡谷的启发,我想制作一张昨天降水数据的图,投影到地图上。但是,不再使用该网站的示例,因为降水数据的数据格式已经改变。

我的方法如下:

  1. 从国家气象服务网站下载netCDF4文件
  2. 打开netCDF4文件并提取相关信息
  3. Basemap创建一张地图
  4. 将降水数据投影到地图上

我想我的问题是我不理解netCDF4文件格式,特别是元数据,因为关于降水数据的网格原点的信息必须隐藏在其中的某个地方。

我的代码如下:

from datetime import datetime, timedelta
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
import os.path
import urllib
from mpl_toolkits.basemap import Basemap

# set date for precipitation (1 day ago)
precip_date  = datetime.utcnow() - timedelta(days=1)
precip_fname = 'nws_precip_1day_{0:%Y%m%d}_conus.nc'.format( precip_date )
precip_url   = 'http://water.weather.gov/precip/downloads/{0:%Y/%m/%d}/{1}'.format( precip_date, precip_fname )

# download netCDF4-file if it does not exist already
if not os.path.isfile( precip_fname ):
    urllib.urlretrieve( precip_url, precip_fname )

# read netCDF4 dataset and extract relevant data
precip_dSet = netCDF4.Dataset( precip_fname )
# spatial coordinates
precip_x    = precip_dSet['x'][:]
precip_y    = precip_dSet['y'][:]
# precipitation data (is masked array in netCDF4-dataset)
precip_data = np.ma.getdata( precip_dSet['observation'][:] )
# grid information 
precip_lat0  = precip_dSet[ precip_dSet['observation'].grid_mapping ].latitude_of_projection_origin
precip_lon0  = precip_dSet[ precip_dSet['observation'].grid_mapping ].straight_vertical_longitude_from_pole
precip_latts = precip_dSet[ precip_dSet['observation'].grid_mapping ].standard_parallel
# close netCDF4 dataset
precip_dSet.close()

fig1, ax1 = plt.subplots(1,1, figsize=(9,6) )

# create the map
my_map = Basemap( projection='stere', resolution='l', 
                  width=(precip_x.max()-precip_x.min()), 
                  height=(precip_y.max()-precip_y.min()),
                  lat_0=30,                         # what is the correct value here?
                  lon_0=precip_lon0,
                  lat_ts=precip_latts
                )
# white background
my_map.drawmapboundary( fill_color='white' )
# grey coastlines, country borders, state borders
my_map.drawcoastlines( color='0.1' )
my_map.drawcountries( color='0.5' )
my_map.drawstates( color='0.8' )

# contour plot of precipitation data
# create the grid for the precipitation data
precip_lons, precip_lats = my_map.makegrid( precip_x.shape[0], precip_y.shape[0] )
precip_xx, precip_yy     = my_map( precip_lons, precip_lats ) 
# make the contour plot
cont_precip = my_map.contourf( precip_xx, precip_yy, precip_data )

plt.show()

这是输出的样子(是的,对于最终的绘图,颜色级别必须调整):

enter image description here

我知道这是一个非常具体的问题,所以任何建议/提示都非常感谢。

python matplotlib-basemap netcdf4
1个回答
1
投票

如果我理解正确,你可以制作情节,但想要添加额外内容的提示?

xarray是使用netCDF文件的绝佳工具箱。它的工作方式与pandas类似,但对于netCDF文件,对'netCDF4'有很大的改进:

http://xarray.pydata.org/en/stable/

要指定特定轮廓,您可以输入级别:

 cont_precip = my_map.contourf( precip_xx, precip_yy, precip_data,levels=[10,20,30]) # Edit for exact contours needed

如果你想要,你可以添加一个颜色栏:

fig1.colorbar(cont_precip,ax=ax1)
© www.soinside.com 2019 - 2024. All rights reserved.