如何使用Python處理HDF格式數(shù)據(jù)及可視化問題
原文鏈接:https://blog.csdn.net/Fairy_Nan/article/details/105914203
HDF也是一種自描述格式文件,主要用于存儲和分發(fā)科學數(shù)據(jù)。氣象領域中衛(wèi)星數(shù)據(jù)經(jīng)常使用此格式,比如MODIS,OMI,LIS/OTD等衛(wèi)星產(chǎn)品。對HDF格式細節(jié)感興趣的可以Google了解一下。
這一次呢還是以Python為主,來介紹如何處理HDF格式數(shù)據(jù)。Python中有不少庫都可以用來處理HDF格式數(shù)據(jù),比如h5py可以處理HDF5格式(pandas中 read_hdf 函數(shù)),pyhdf可以用來處理HDF4格式。此外,gdal也可以處理HDF(NetCDF,GRIB等)格式數(shù)據(jù)。
安裝
首先安裝相關庫

上述庫均可以通過conda包管理器進行安裝,如果conda包管理器無法安裝,對于windows系統(tǒng),可以查找是否存在已打包的安裝包,而unix系統(tǒng)可以通過源碼編譯安裝。
數(shù)據(jù)處理和可視化
以LIS/OTD衛(wèi)星閃電成像數(shù)據(jù)為例,處理HDF4格式數(shù)據(jù)并進行繪圖:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm, colors
import seaborn as sns
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from pyhdf.SD import SD, SDC
sns.set_context('talk', font_scale=1.3)
data = SD('LISOTD_LRMTS_V2.3.2014.hdf', SDC.READ)
lon = data.select('Longitude')
lat = data.select('Latitude')
flash = data.select('LRMTS_COM_FR')
# 設置colormap
collev= ['#ffffff', '#ab18b0', '#07048f', '#1ba01f', '#dfdf18', '#e88f14', '#c87d23', '#d30001', '#383838']
levels = [0, 0.01, 0.02, 0.04, 0.06, 0.1, 0.12, 0.15, 0.18, 0.2]
cmaps = colors.ListedColormap(collev, 'indexed')
norm = colors.BoundaryNorm(levels, cmaps.N)
proj = ccrs.PlateCarree()
fig, ax = plt.subplots(figsize=(16, 9), subplot_kw=dict(projection=proj))
LON, LAT= np.meshgrid(lon[:], lat[:])
con = ax.contourf(LON, LAT, flash[:, :, 150], cmap=cmaps, norm=norm, levels=levels, extend='max')
cb = fig.colorbar(con, shrink=0.75, pad=0.02)
cb.cmap.set_over('#000000')
cb.ax.tick_params(direction='in', length=5)
ax.coastlines()
ax.set_xticks(np.linspace(-180, 180, 5), crs=proj)
ax.set_yticks(np.linspace(-90, 90, 5), crs=proj)
lon_formatter= LongitudeFormatter(zero_direction_label=True)
lat_formatter= LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)

某月全球閃電密度分布
上述示例基于pyhdf進行HDF4格式數(shù)據(jù)處理和可視化,HDF4文件中包含的變量和屬性獲取方式見文末的Notebook,其中給出了 更詳細的示例。
以下基于h5py讀取HDF5格式數(shù)據(jù),以OMI衛(wèi)星O3數(shù)據(jù)為例:
import h5py
data = h5py.File('TES-Aura_L3-O3-M2005m07_F01_10.he5')
lon = data.get('/HDFEOS/GRIDS/NadirGrid/Data Fields/Longitude').value
lat = data.get('/HDFEOS/GRIDS/NadirGrid/Data Fields/Latitude').value
o3 = data.get('/HDFEOS/GRIDS/NadirGrid/Data Fields/O3').value
proj = ccrs.PlateCarree()
fig, ax = plt.subplots(figsize=(16, 9), subplot_kw=dict(projection=proj))
LON, LAT = np.meshgrid(lon[:], lat[:])
con = ax.contourf(LON, LAT, o3[10, :, :]*1e6, np.arange(0, 8.01, 0.1), vmin=0, vmax=8, cmap=cm.RdGy_r)
ax.coastlines()
ax.set_xticks(np.linspace(-180, 180, 5), crs=proj)
ax.set_yticks(np.linspace(-90, 90, 5), crs=proj)
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
cb = fig.colorbar(con, shrink=0.75, pad=0.02)
cb.set_ticks(np.arange(0, 8.01, 1))
cb.ax.tick_params(direction='in', length=5)
上述示例中使用類似unix中路徑的方式獲取相關變量,這在HDF格式數(shù)據(jù)中稱為Groups。不同的組可以包含子組,從而形成類似嵌套的形式。詳細的介紹可Google了解。

總結
到此這篇關于如何使用Python處理HDF格式數(shù)據(jù)及可視化問題的文章就介紹到這了,更多相關Python處理HDF格式數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python中的 Matplotlib 繪制多子圖時的重疊問題及解決方案
當使用 Matplotlib 繪制多個子圖(subplots)時,如果標簽或標題文字交叉或重疊,遇到這樣的問題如何處理呢,下面小編給大家介紹了python中的 Matplotlib 繪制多子圖時的重疊問題及解決方案,需要的朋友可以參考下2024-06-06
python時間序列數(shù)據(jù)相減的實現(xiàn)
本文主要介紹了python時間序列數(shù)據(jù)相減的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
Tensorflow 利用tf.contrib.learn建立輸入函數(shù)的方法
這篇文章主要介紹了Tensorflow 利用tf.contrib.learn建立輸入函數(shù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

