介绍就不多说了,类似特殊的(*.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