1

I have these 3 files here. Trying to stack them and extract a variable called "OzoneTropColumn". I am able to extract data for single file but unable to extract for multiple files.

library(raster)
library(ncdf4)
list_col1 <- list.files("E:/TES", pattern = "*.hdf", full.names = TRUE) 
ncin1 <- raster::stack(list_col1, varname = "Data Fields/OzoneTropColumn", ncdf=TRUE)

Any help would be much appreciated.

Thank you

Dipu
  • 123
  • 2
  • 14

2 Answers2

1

I don't know if this really works since I am no expert in raster or importing hdf files. But this code did load the three files in R for me but still gave some warnings:-

library(raster)
library(ncdf4)
list_col1 <- as.list(list.files("E:/TES", pattern = "*.hdf",
                                full.names = TRUE))
ncin1 <- raster::stack(list_col1,
                       varname = "Data Fields/OzoneTropColumn",
                       ncdf=TRUE)

[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"

But does shows me that it has read the 3 files

ncin1[[1]]
class      : RasterLayer 
dimensions : 83, 90, 7470  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0.5, 90.5, 0.5, 83.5  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : /home/shawn/Downloads/TES/TES-Aura_L3-O3-M2004m09_F01_12.hdf 
names      : Data.Fields.OzoneTropColumn.1 
zvar       : Data Fields/OzoneTropColumn

ncin1[[2]]
class      : RasterLayer 
dimensions : 83, 90, 7470  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0.5, 90.5, 0.5, 83.5  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : /home/shawn/Downloads/TES/TES-Aura_L3-O3-M2004m10_F01_12.hdf 
names      : Data.Fields.OzoneTropColumn.2 
zvar       : Data Fields/OzoneTropColumn

ncin1[[3]]
class      : RasterLayer 
dimensions : 83, 90, 7470  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0.5, 90.5, 0.5, 83.5  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : /home/shawn/Downloads/TES/TES-Aura_L3-O3-M2004m11_F01_12.hdf 
names      : Data.Fields.OzoneTropColumn.3 
zvar       : Data Fields/OzoneTropColumn
Shawn Brar
  • 1,346
  • 3
  • 17
  • Only mistake I made was instead of as.list(list.files("E:/TES", pattern = "*.hdf", full.names = TRUE)) . I used "list_col <- list.files("E:/TES", pattern = "*.hdf", full.names = TRUE). It did list the files though but I dnt know why it didn't worked without "as.list". – Dipu Aug 01 '21 at 16:49
  • The reason why it didn't work without the `as.list` is if you check the documentation of the function, it is written `filename (character), Raster* object, missing (to create an empty RasterStack), SpatialGrid*, SpatialPixels*, or list (of filenames and/or Raster* objects). `. So in our case it requires us to pass a `list` and not a `character` vector. – Shawn Brar Aug 02 '21 at 06:40
0

The script posted by Robert works well the only difference there was using as.list and without using as.list]1]1. I tried before using without as.list. The extracted data loses its georeference actual data extensionfor reason unknown. So had to give extent and projection after raster stack. Thanks Robert. Difference between as.list and without as.list

Dipu
  • 123
  • 2
  • 14