Question

I am a relatively new R user. I am having problems extracting the same file from several HDF5 files. This is what I did using the rhdf5 package:

files <- list.files(pattern = ".he5",full.names=TRUE)

The file I can't extract from all the HDF5 Files has the extension

"/HDFEOS/SWATHS/ColumnAmountNO2/Data Fields/ColumnAmountNO2Trop"

When I run the command below for individual files, it works:

NO2he5<-h5read("MA.he5","/HDFEOS/SWATHS/ColumnAmountNO2/Data Fields/ColumnAmountNO2Trop")

How do I translate this to a function that can extract the layer simultaneously for multiple hdf5 files, please?

Was it helpful?

Solution

See lapply:

library(rhdf5)
files <- list.files(pattern = ".he5", full.names = TRUE)
attribute <- "/HDFEOS/SWATHS/ColumnAmountNO2/Data Fields/ColumnAmountNO2Trop"
out.list <- lapply(files, h5read, attribute)

This will store all the read objects into a single object: a list. It is the preferred approach versus creating as many objects as you have files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top