0
点赞
收藏
分享

微信扫一扫

Python 批量处理 ICESat-2 ATL08 数据

骨灰级搬砖工 2022-04-14 阅读 16
python

改编内容,原文链接:​​​​​​ICESat2学习笔记10 :python批量读取处理ATL08数据_晚秋10的博客-CSDN博客

如果报错KeyError: 'Unable to open object (component not found)',请先检查h5存放路径是否只包含源数据,如果存在,请删除。在代码运行后会生成额外h5文件,再次运行就会无法识别。

代码包括了简单数据筛选以及批量导出csv,代码如下:

import pandas as pd
import h5py
import os


def read_atl08(fname):
    # Each beam is a group
    group = ['/gt1r', '/gt2r', '/gt3r']

    # Loop trough beams
    for k, g in enumerate(group):

        with h5py.File(fname, 'r') as fi:
            # Define the variables and construction, open the h5 files and check it out
            lat = fi[g + '/land_segments/latitude'][:]
            lon = fi[g + '/land_segments/longitude'][:]
            h_canopy = fi[g + '/land_segments/canopy/h_canopy'][:]
            h_canopy_uncertainty = fi[g + '/land_segments/canopy/h_canopy_uncertainty'][:]

        # Define output file name
        ofile = fname.replace('.h5', '_' + g[1:] + '.h5')

        # Save variables
        with h5py.File(ofile, 'w') as f:
            f['lon'] = lon
            f['lat'] = lat
            f['h_canopy'] = h_canopy
            f['h_canopy_uncertainty'] = h_canopy_uncertainty
            print('out ->', ofile)
        # Save as csv
        ofilecsv = fname.replace('.h5', '_' + g[1:] + '.csv')
        result = pd.DataFrame()
        result['lon'] = lon
        result['lat'] = lat
        result['h_canopy'] = h_canopy
        result['h_canopy_uncertainty'] = h_canopy_uncertainty
        # Filter the data
        for i in range(0, result.shape[0]):
            if result['h_canopy_uncertainty'][i] > 20 or result['h_canopy'][i] < 3 or result['h_canopy'][i] > 120:
                result = result.drop(i)

        print('out ->', ofilecsv)
        result.to_csv(ofilecsv, index=None)


def readMultiH5(dir):
    # Iterate the dir, deal with all h5 files
    # For root_dir, sub_dir, files in os.walk(r'' + dir):
    for root_dir, sub_dir, files in os.walk(dir):
        for file in files:
            if file.endswith('h5'):
                # Absolute path
                file_name = os.path.join(root_dir, file)
                read_atl08(file_name)


# Execute
root_dir = r'F:\WYD'  # Root path
readMultiH5(root_dir)

代码比较简单,如果遇到问题请联系QQ:1262840380

举报

相关推荐

0 条评论