SantanderMetGroup / climate4R

An R Framework for Climate Data Access and Post-processing
130 stars 44 forks source link

Creating monthly climatologies with climatology and loadGridData #10

Closed catubela closed 4 years ago

catubela commented 4 years ago

This isn't an issue, but more of a request for advice: I want to ascertain if I am understanding/using the 'climatology' function and the 'loadGridData' function properly.

I want to create a monthly climatology of cmip data. I have six example historical files in a folder (1849-1855 daily) for tasmax which I aggregated and then loaded using: test<-loadGridData("test.ncml","tasmax",dictionary = dictionary,aggr.m = "mean") This returns a dataset of size: 73 x 256 x 512. What I want is a final file of size 12 x 256 x 512 (one grid for each month averaged across all the years chosen)

I think I need to write a loop, wherein I load each month of data at a time and compute the monthly climatology:

for (i in1:12){
test<-loadGridData("test.ncml","tasmax",dictionary = dictionary,season=i)
A [i]<- lapply (1, function (x) climatology(test))
rm(test)
}

Am I missing something here in the 'aggr' function? It seems that the aggr.m option gives all the (individual) month means (As opposed to a mean for all Januarys, a mean for all Februarys, a mean for all Marchs etc). Thanks for your time.

jbedia commented 4 years ago

In order to subset by months or years, you should use the function subsetGrid. In your case, something along these lines should do the job, returning a list of 12 elements, corresponding to the Jan-Dec climatologies:

test <- loadGridData("test.ncml", var = "tasmax", dictionary = dictionary, aggr.m = "mean")
l <- lapply(1:12, function(i) {
    subsetGrid(test, season = i) %>% climatology()
})

After this, you can "stack" all the monthly climatologies together in the same grid using makeMultiGrid, for instance for plotting purposes:

mg <- do.call("makeMultiGrid", c(l, skip.temporal.check = TRUE))
require(visualizeR)
spatialPlot(mg, names.attr = month.abb, backdrop.theme = "coastline")

Hope this helps

catubela commented 4 years ago

Thanks for that. That's very helpful.