Bob
Bob

Reputation: 1

Applying a dataframe function to raster brick

I’m using musica package, for downscaling climate projections

For example, the musica library has a set of function that are designed to be apply on data frame, But I want to apply it to my raster stack, and the function needs argument like,

dec = decomp(basin_PT$obs_ctrl, period = c('Y5', 'Y1', 'M6', 'M3', 'M1', 'D20'))

where obs_ctrl is a data.table with 3 variables: Date ( date object 1980-2005,), pr( vector of rainfall value), and ts( vector of temperature value). Applying to raster brick I first create my date object,

cal <- "365_day"
origin <- "1980-01-01"
origin.pcict <- as.PCICt(origin, cal)
seconds.per.day <- 86400
ts.dat.days <- 0:1989
ts.dat.pcict <- origin.pcict + (ts.dat.days * seconds.per.day)
#convert the object to POSIXlt
dtm<-as.POSIXlt(ts.dat.pcict)

And then, create the raster brick, I have raster brick of 9490 layers (daily gridded rainfaill data), Here is a raster brick

#####Load required package ######### library(musica) library(data.table) library(lubridate) library(raster) library(PCICt)

pr_files= list.files(ipath,pattern='.asc')
rrr<- stack(pr_files)

And, overly the decomp function to the raster,

rse <- function(dtm, rrr ,period = c('Y5', 'Y1', 'M6', 'M3', 'M1', 'D20')){
  musica::decomp(dtm, pr,period)
  }

When applying the function

c <- raster::overlay(rrr, dtm, fun = rse)

, I received the following error,

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘overlay’ for signature ‘"RasterStack", "POSIXlt"’

Is there anyway for applying a dataframe function to raster brick? Thanks for your help

Upvotes: 0

Views: 321

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47501

You need to provide some simple example data (created with some code, there are numerous examples on-line and in the raster help files, e.g. in ?overlay) so that your code can run and we can answer your question.

This function seems odd

rse <- function(dtm, rrr ,period = c('Y5', 'Y1', 'M6', 'M3', 'M1', 'D20')){
  musica::decomp(dtm, pr,period)
}

There is an argument rrr that is not used. It should probably be pr

You can probably also leave dtm out and do

rse <- function(pr, period = c('Y5', 'Y1', 'M6', 'M3', 'M1', 'D20')){
  musica::decomp(dtm, pr, period)
}

r <- raster::overlay(rrr, fun = rse)

overlay will then find dtm in your global environment.

Upvotes: 0

Related Questions