PYTHON:在底图上覆盖netCDF数据(contourf)

时间:2015-11-04 13:50:50

标签: python netcdf matplotlib-basemap

我是python和netCDF的新手,但是现在已经在以下问题上工作了好几天而没有任何进展。我的代码基于我阅读的各种教程和示例,我无法弄清楚出了什么问题!

基本上我希望能够将一些netCDF数据的图叠加到底图上。我的代码(完整)如下:

import numpy as np
from netCDF4 import Dataset
# from scipy.io import netcdf
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

when = 0 # improve this variable later so that user input can be date/ time

filename = '/net/glusterfs_surft/surft/data/LakeData/JRA55/raw/jra55_tmp.1964010100_1964123121.nc' # input the complete filepath here
# open the file at the address 'filename' for reading:
fopen = Dataset(filename, 'r') # <-- turn on if using netCDF4
# fopen = netcdf.netcdf_file(filename, 'r') <-- turn on if using scipy.io

# variables in JRA-55 are:
# TMP_GDS0_HTGL
# initial_time0_hours
# initial_time0_encoded
# initial_time0
# g0_lon_2 (runs from 0 to 360E in 1.25 deg steps)
# g0_lat_1 (runs from 90 to -90 in 1.25 deg steps)

# now set variables x, y and 'data':
x = fopen.variables['g0_lon_2'][:] # this is a 1D longitude array
y = fopen.variables['g0_lat_1'][:] # this is a 1D latitude array 
data = fopen.variables['TMP_GDS0_HTGL'][:] 
# this is a 3D array with temperature saved at each point in 2D space and time
# reduce data to a 2D array for a specific time:
data_when = data[when,:,:]

#close the file at the address
fopen.close()

# create a basemap to plot onto:
m = Basemap(width=5000000, height=3500000,\
        resolution='l', projection='stere',\
        lat_ts=40, lat_0=50, lon_0=0)
m.drawcoastlines()
m.drawparallels(np.arange(-80.,81,20))
m.drawmeridians(np.arange(-180.,181,20))
# 
#   add other basemap drawing options here 
#

# convert 1D matrices into 2D fill matrices for processing:
xx, yy = np.meshgrid(x, y)

plt.contourf(xx, yy, data_when, latlon=True)

plt.show()

如果我注释掉底图部分,那么我的数据如下所示:

world map, no borders, temperature contours

如果我包含它们(不需要注释plt.contourf(xx, yy, data_when, latlon=True)),我得到的图像是:

stereographic projection at lat = 50, lon = 0, with borders, no contours

我希望能够在一个图中显示这些图像的相应部分,但不知道如何。使用的地图投影并不重要,但图像应与数据和底图相对应。

谢谢!我希望你能帮忙!

1 个答案:

答案 0 :(得分:0)

以下版本的代码功能齐全。感谢sea_hydro获得评论中提供的帮助。我希望代码的工作版本对希望解决这个问题的其他新手有用!

import numpy as np
from netCDF4 import Dataset
# from scipy.io import netcdf
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

when = 0 # improve this variable later so that user input can be date/ time

filename = 'some_filepath' # input the complete filepath here
# open the file at the address 'filename' for reading:
fopen = Dataset(filename, 'r') # <-- turn on if using netCDF4
# fopen = netcdf.netcdf_file(filename, 'r') <-- turn on if using scipy.io

# now set variables x, y and 'data':
x = fopen.variables['lon_var'][:] # this is a 1D longitude array
y = fopen.variables['lat_var'][:] # this is a 1D latitude array 
data = fopen.variables['data_var'][:] 
# this is a 3D array with a value saved at each point in 2D space and time
# reduce data to a 2D array for a specific time:
data_when = data[when,:,:]

#close the file at the address
fopen.close()

# create a basemap to plot onto:
m = Basemap(width=10000000, height=7000000,\
        resolution='l', projection='stere',\
        lat_ts=40, lat_0=50, lon_0=0)
m.drawcoastlines()
m.drawparallels(np.arange(-80.,81,20))
m.drawmeridians(np.arange(-180.,181,20))
# 
#   add other basemap drawing options here 
#

此代码使用立体地图投影。其他底图选项可以在in the matlpotlib basemap toolkit documentation

找到
# convert 1D matrices into 2D fill matrices for processing:
xx, yy = np.meshgrid(x, y)
xx, yy = m(xx, yy)
plt.contourf(xx, yy, data_when)

plt.show()

其他图也可以叠加。