简体   繁体   English

将 HDF 数据读取到 3d 数组并在 python 中另存为数据帧

[英]Read a HDF data to a 3d array and save as a dataframe in python

I am currently working on the NASA aerosol optical depth data (MCD19A2), which is a NASA satellite level three product.我目前在做 NASA 气溶胶光学深度数据(MCD19A2),这是 NASA 卫星三级产品。 I have uploaded the data .我已经上传了数据 I want to save the data as a dataframe including all the information of longitude and latitude, and values.我想将数据保存为一个数据框,包括经度和纬度的所有信息以及值。 I have successfully converted the 0.47um band file into a three-dimensional array.我已经成功将 0.47um 波段文件转换为三维数组。 I want to ask how to convert this array into a correct dataframe includes X, Y and the value.我想问一下如何将此数组转换为正确的数据帧,包括 X、Y 和值。

Below are the codes I have tried:以下是我尝试过的代码:

from osgeo import gdal
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

rds = gdal.Open("MCD19A2.A2006001.h26v04.006.2018036214627.hdf")
names=rds.GetSubDatasets()
names[0][0]
*'HDF4_EOS:EOS_GRID:"MCD19A2.A2006001.h26v04.006.2018036214627.hdf":grid1km:Optical_Depth_047'*
aod_047 = gdal.Open(names[0][0])
a47=aod_047.ReadAsArray()
a47[1].shape
(1200,1200)

I would like the result to be like我希望结果像

X (n=1200) X (n=1200) Y (n=1200)是 (n=1200) AOD_047 AOD_047
8896067 8896067 5559289 5559289 0.0123 0.0123

I know that in R this can be done by我知道在 R 中这可以通过

require('gdalUtils')
require('raster')
require('rgdal')


file.name<-"MCD19A2.A2006001.h26v04.006.2018036214627.hdf"
sds <- get_subdatasets(file.name)
gdal_translate(sds[1], dst_dataset = paste0('tmp047', basename(file.name), '.tiff'), b = nband) 
r.047 <- raster(paste0('tmp047', basename(file.name), '.tiff'))
df.047 <- raster::as.data.frame(r.047, xy = T)
names(df.047)[3] <- 'AOD_047'

But, R really relies on memory and saving to 'tif' and reading 'tif' is using a lot of memory.但是,R 确实依赖于内存并保存到 'tif' 并且读取 'tif' 会占用大量内存。 So I want to do this task in python.所以我想在python中完成这个任务。 Thanks a lot for your help.非常感谢你的帮助。

You can use pandas :您可以使用pandas

import pandas as pd
df=pd.read_hdf('filename.hdf')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM