Skip to content

Instantly share code, notes, and snippets.

@oscarperpinan
Created February 14, 2015 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarperpinan/e86d61832fc384904a4a to your computer and use it in GitHub Desktop.
Save oscarperpinan/e86d61832fc384904a4a to your computer and use it in GitHub Desktop.
library(zoo)
library(rasterVis)
## DESCRIPTION: http://iridl.ldeo.columbia.edu/SOURCES/.CAC/.sst/
setwd(tempdir())
download.file('http://iridl.ldeo.columbia.edu/SOURCES/.CAC/.sst/data.nc', destfile = 'SST.nc')
SST <- stack('SST.nc')
idx <- seq(as.Date('1970-01-01'), as.Date('2003-03-01'), by='month')
tt <- as.yearmon(idx)
SST <- setZ(SST, tt)
names(SST) <- as.character(tt)
## Extract month value from a Date or yearmon object
month <- function(x)format(x, '%m')
## Compute anomaly using monthly grouping with ave
anomaly <- function(x){
## Monthly means
mm <- ave(x, month(tt), FUN = mean)
## Monthly standard deviation
msd <- ave(x, month(tt), FUN = sd)
## anomaly
(x - mm)/msd
}
## Use anomaly with calc
SSTanom <- calc(SST, anomaly)
SSTanom <- setZ(SSTanom, tt)
## Ok, let's see the result
hovmoller(SSTanom, par.settings = RdBuTheme)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment