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

上述庫均可以通過conda包管理器進(jìn)行安裝,如果conda包管理器無法安裝,對(duì)于windows系統(tǒng),可以查找是否存在已打包的安裝包,而unix系統(tǒng)可以通過源碼編譯安裝。
數(shù)據(jù)處理和可視化
以LIS/OTD衛(wèi)星閃電成像數(shù)據(jù)為例,處理HDF4格式數(shù)據(jù)并進(jìn)行繪圖:
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')
# 設(shè)置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進(jìn)行HDF4格式數(shù)據(jù)處理和可視化,HDF4文件中包含的變量和屬性獲取方式見文末的Notebook,其中給出了 更詳細(xì)的示例。
以下基于h5py讀取HDF5格式數(shù)據(jù),以O(shè)MI衛(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中路徑的方式獲取相關(guān)變量,這在HDF格式數(shù)據(jù)中稱為Groups。不同的組可以包含子組,從而形成類似嵌套的形式。詳細(xì)的介紹可Google了解。

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

