简体   繁体   中英

Create a loop for a list of list from RasterBrick in R

I'm on a project in remote sensing running on R. I've got a RasterBrick(x) with the raster for all the dates I'm interested in, a Time Serie with the dates corresponding (called time in the function), and a function which works as I want it when processed manually (z is the pixel I want) :

function(x,z)
{
d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
l[[z]]<-list(n$output[[1]]$Tt)
}

The bfastts function is used to create a ts object containing the values of one pixel along the time serie, the bfast is another processing some statisticals of which I only want one result (this is the third line)? None of this two functions are mine, and they are stable and foundable in the R package repository.

So, I would like to add "another level" of function (sorry for my vocabulary which may not be very precise) which would allow to run this function automatically. My expected result would be a list of the result of the function above, so in other words a list of each pixel's time series.

I've tried this (x is still the RasterBrick) :

function(x)
{
  z<-nrow(x)*ncol(x)
  j<-last(z[[1]])
  l<-vector('list',length = j)
  index<-function(x)
    {
    d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
    n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
    l[[z]]<-list(n$output[[1]]$Tt) # this is to add the newly created element to the list
  }
  lapply(x, FUN='index')
}

but I'm getting an answer that it is not possible to coerce a S4 object to a vector, I guess the problem is in lapply who doesn't like the RasterBrick class... Furthermore I want a list of list in output, and not a list of RasterBrick (I think I understood lapply returns a list of object with the same class as x).

I've tried different workaround, none succesfully, which is not surprising giving my low level in programming, and this one seems to me the closest to what I need. I don't think I fully understand neither how lapply works nor the use of a function in a function.

Thank you very much if you can help me. Cheers Guillaume

因此,以防万一它对某人有用,这是我解决此问题的方法(最终看起来很简单),“砖”对象是RasterBrick:

pixelts<- as.list(as.data.frame(t(as.data.frame(brick))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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