I am working with netcdf4 files that contain data over several days (one layer per day) and depth (7 different depth, 1 layer for each)
What I am trying to do is to integrate the data over depth (i.e sum of all the depth layers), while keeping the days as distinct layers, so computing a single 106 x 109 x 12 raster stack...
If I stack everything, then I loose the day layers...
mylist <- list()
for(i in 1:7){
mylist[[i]] <- raster::brick("Data/Final_datasets_R/Environmentals/phy2016.nc", level=i)
}
names(mylist) <- paste0("phy.2016 ",c(0,3,10,15,20,30,50),"m")
phy.2016 <- stack(mylist)
sum <- calc(phy.2016, sum)
The stack line returns me a stack of 84 layers, over which I don't manage to compute a sum without loosing the time dimension.
Would someone have any solution or suggestion ?
Thanks in advance !!!
UPDATE: I ended up finding a solution to "reverse" the nesting system of the list. Posting here in case it can help someone that encounters the same problem.
mylist <- list()
for (i in 1:7) {
mylist[[i]] <-
raster::brick("Data/Final_datasets_R/Environmentals/phy2016.nc", level =i)
}
This was the loop to open all the levels of the variable as stated in the question.
Then to reverse the indexing system:
mylist2 <- list()
for (i in 1:nlayers(mylist[[i]])) {
mylist2[[i]] <- lapply(mylist, `[[`, i)
}
From a list of 7 elements (depth) with 12 layers (days), we produce a list of 12 elements (days) with 7 layers (depth).