本文主要利用python 读取HDF5数据,如icesat2 atl08数据。
转载:https://hdfeos.org/zoo/index_openICESat2_Examples.php#ATL
输入数据:atl08 HDF5格式数据
功能:读取.h5 数据并可视化
代码理解:
step1:import packages
import os
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy.crs as ccrs #进行地图投影
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import h5py #读取.h5
step2:read HDF5 file
FILE_NAME = '' # hdf file的路径
step3:plot and visualize atl08
with h5py.File(FILE_NAME, mode='r') as f:
latvar = f['/gt1l/land_segments/latitude']
lat = latvar[:]
lonvar = f['/gt1l/land_segments/longitude']
lon = lonvar[:]
dset_name = '/gt1l/land_segments/dem_h'
datavar = f[dset_name]
data = datavar[:]
units = datavar.attrs['units']
long_name = datavar.attrs['long_name']
_FillValue = datavar.attrs['_FillValue']
# Handle FillValue
data[data == _FillValue] = np.nan
data = np.ma.masked_where(np.isnan(data), data)
# Find the middle location.
lat_m = lat[int(lat.shape[0]/2)]
lon_m = lon[int(lon.shape[0]/2)]
# Let's use ortho projection.定义投影
orth = ccrs.Orthographic(central_longitude=lon_m,
central_latitude=lat_m,
globe=None)
ax = plt.axes(projection=orth)
# Remove the following line to see a zoom-in view.
ax.set_global()
# Plot on map.
p = plt.scatter(lon, lat, c=data, s=1, cmap=plt.cm.jet,
transform=ccrs.PlateCarree())
# Put grids.
gl = ax.gridlines(draw_labels=True, dms=True)
# Put coast lines.
ax.coastlines()
# Put grid labels at left and bottom only.
gl.top_labels = False
gl.right_labels = False
# Put degree N/E label.
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
# Adjust colorbar size and location using fraction and pad.
cb = plt.colorbar(p, fraction=0.022, pad=0.01)
units = units.decode('ascii', 'replace')
cb.set_label(units, fontsize=8)
basename = os.path.basename(FILE_NAME)
long_name = long_name.decode('ascii', 'replace')
plt.title('{0}\n{1}'.format(basename, long_name))
fig = plt.gcf()
pngfile = "{0}.py.png".format(basename)
fig.savefig(pngfile)
运行结果:
补充:atl08数据产品参数说明( doi: 10.13203/j.whugis20200531)
参考链接:
python 读取hdf5:https://hdfeos.org/zoo/index_openICESat2_Examples.php#ATL
利用Cartopy库绘制地图:https://blog.csdn.net/x5675602/article/details/86429040
Cartopy中的地图投影:https://www.cnblogs.com/youxiaogang/p/14247184.html