【问题标题】:How to read a H5 file containing satellite data in Python?如何用Python读取包含卫星数据的H5文件?
【发布时间】:2022-12-16 17:25:26
【问题描述】:

作为项目的一部分,我正在探索卫星数据,数据以 H5 格式提供。我是这种格式的新手,无法处理数据。我能够在名为 Panoply 的软件中打开该文件,发现 DHI 值以一种名为 Geo2D 的格式提供。无论如何将数据提取为 CSV 格式,如下所示:

X Y GHI
X1 Y1
X2 Y2

附上在 Panoply 中打开的文件的屏幕截图。

文件链接:https://drive.google.com/file/d/1xQHNgrlrbyNcb6UyV36xh-7zTfg3f8OQ/view

我尝试了以下代码来读取数据。我可以将它存储为 2d numpy 数组,但无法与位置一起存储。

`

import h5py
import numpy as np
import pandas as pd
import geopandas as gpd


#%%
f = h5py.File('mer.h5', 'r')

for key in f.keys():
    print(key) #Names of the root level object names in HDF5 file - can be groups or datasets.
    print(type(f[key])) # get the object type: usually group or dataset
    ls = list(f.keys())
   


key ='X'


masterdf=pd.DataFrame()


data = f.get(key)   
dataset1 = np.array(data)
masterdf = dataset1


np.savetxt("FILENAME.csv",dataset1, delimiter=",")


#masterdf.to_csv('new.csv')

enter image description here

enter image description here `

【问题讨论】:

    标签: python gis gdal h5py


    【解决方案1】:

    找到了一种读取数据、将其转换为数据框并转换投影参数的有效方法。

    代码在这里跟踪:https://github.com/rishikeshsreehari/boring-stuff-with-python/blob/main/data-from-hdf5-file/final_converter.py

    代码如下:

    import pandas as pd
    import h5py
    import time
    from pyproj import Proj, transform
    
    
    input_epsg=24378
    output_epsg=4326
    
    start_time = time.time()
    
    
    with h5py.File("mer.h5", "r") as file:
        df_X = pd.DataFrame(file.get("X")[:-2], columns=["X"])
        df_Y = pd.DataFrame(file.get("Y"), columns=["Y"])
        DHI = file.get("DHI")[0][:, :-2].reshape(-1)
    
    final = df_Y.merge(df_X, how="cross").assign(DHI=DHI)[["X", "Y", "DHI"]]
    
    
    
    final['X2'],final['Y2']=transform(input_epsg,output_epsg,final[["X"]].to_numpy(),final[["Y"]].to_numpy(),always_xy=True)
    
    
    #final.to_csv("final_converted1.csv", index=False)
    
    print("--- %s seconds ---" % (time.time() - start_time))
    

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 1970-01-01
      • 2017-03-02
      • 2011-11-08
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多