HDF5数据读取之利用h5py模块读取含多个嵌套Group的数据

介绍就不多说了,类似特殊的(*.nc)文件
看成是文件夹下的子文件夹里有数据需要提取
直接上代码

# -*- coding: utf-8 -*-
import numpy as np
import h5py
import os


class Create_hdf_addxy:
    def __init__(self, filePath):
        self.filePath = filePath  # 文件路径

    def creat_addxy(self):
        curPath = os.path.abspath(os.path.dirname(__file__))
        rootPath = os.path.split(curPath)[0]
        rootDir = os.path.split(rootPath)[0]
        projDir = os.path.split(rootDir)[0]
        filename = os.path.splitext(os.path.split(self.filePath)[1])[0]
        outputfile = projDir + '/data/workspace/txtfile/' + filename + '.txt'
        dataset = 'VCDTropo'
        file_info = h5py.File(self.filePath, 'r')


        ntc_VCDTropo = file_info['DETAILED_RESULTS/NO2'][dataset][()]
        lat = file_info['GEOLOCATION/LatitudeCentre'][()]
        lon = file_info['GEOLOCATION/LongitudeCentre'][()]

        null_index = np.where(ntc_VCDTropo < 0)
        index = list(null_index[0])
        ntc_arr = np.delete(ntc_VCDTropo, index)
        lon_arr = np.delete(lon, index)
        lat_arr = np.delete(lat, index)
        # 统一数量级
        ntc_arr = ntc_arr


        fid = np.array(range(len(ntc_arr))).tolist()
        ntc_arr_list = ntc_arr.tolist()
        lon_arr_list = lon_arr.tolist()
        lat_arr_list = lat_arr.tolist()
        all_arr = [fid, lon_arr_list, lat_arr_list, ntc_arr_list]
        all_arr = np.array(all_arr)
        all_arr = np.transpose(all_arr)
        all_arr = all_arr.tolist()
        # a=','.join(all_arr)
        a = [str(i).replace('[', '').replace(']', '').replace(' ', '') for i in all_arr]
        # 添加头文件
        with open(outputfile, 'w') as f:
            f.write('fid,lon,lat,value' + '\n')
        with open(outputfile, 'a') as f:
            s = '\n'.join(a)
            f.write(s)
        return outputfile
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值