在python basemap中绘制gebco数据

2024-10-01 13:44:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我下载了一些gebco测深数据作为netCDF文件。我想用python basemap来绘制它。我试过了

import netCDF4
from mpl_toolkits.basemap import Basemap


# Load data
dataset = netCDF4.Dataset('/home/david/Desktop/GEBCO/gebco_08_-30_45_5_65.nc')

# Extract variables
x = dataset.variables['x_range']
y = dataset.variables['y_range']
spacing = dataset.variables['spacing']

# Data limits
nx = (x[-1]-x[0])/spacing[0]   # num pts in x-dir
ny = (y[-1]-y[0])/spacing[1]   # num pts in y-dir

# Reshape data
zz = dataset.variables['z']
Z = zz[:].reshape(ny, nx)



# setup basemap.
m = Basemap(llcrnrlon=-30,llcrnrlat=45.0,urcrnrlon=5.0,urcrnrlat=65.0,
            resolution='i',projection='stere',lon_0=-15.0,lat_0=55.0)


# Set up grid
lons, lats = m.makegrid(nx, ny)
x, y = m(lons, lats)

m.contourf(x, y, flipud(Z))
m.fillcontinents(color='grey')
m.drawparallels(np.arange(10,70,10), labels=[1,0,0,0])
m.drawmeridians(np.arange(-80, 5, 10), labels=[0,0,0,1])

下面的数字不正确。问题源于如何定义区域。对于底图,区域由左下角lat,lon和右上角lat,lon定义。但gebco数据采用沿中心线定义的最大和最小lon/lat。 任何人对gebco数据有任何经验或看到解决方案??在

谢谢 D map


Tags: 数据importdata定义variablesdatasetnetcdf4lon
1条回答
网友
1楼 · 发布于 2024-10-01 13:44:53

所以为了记录在案,以下是有效的答案,使用上面的评论:

import netCDF4
from mpl_toolkits.basemap import Basemap

# Load data
dataset = netCDF4.Dataset('/usgs/data1/rsignell/bathy/gebco_08_-30_-45_5_65.nc')

# Extract variables
x = dataset.variables['x_range']
y = dataset.variables['y_range']
spacing = dataset.variables['spacing']

# Compute Lat/Lon
nx = (x[-1]-x[0])/spacing[0]   # num pts in x-dir
ny = (y[-1]-y[0])/spacing[1]   # num pts in y-dir

lon = np.linspace(x[0],x[-1],nx)
lat = np.linspace(y[0],y[-1],ny)

# Reshape data
zz = dataset.variables['z']
Z = zz[:].reshape(ny, nx)

# setup basemap.
m = Basemap(llcrnrlon=-30,llcrnrlat=45.0,urcrnrlon=5.0,urcrnrlat=65.0,
            resolution='i',projection='stere',lon_0=-15.0,lat_0=55.0)

x,y = m(*np.meshgrid(lon,lat))

m.contourf(x, y, flipud(Z));
m.fillcontinents(color='grey');
m.drawparallels(np.arange(10,70,10), labels=[1,0,0,0]);
m.drawmeridians(np.arange(-80, 5, 10), labels=[0,0,0,1]);

它产生了这个图。enter image description here

相关问题 更多 >