Last active
August 31, 2015 07:31
-
-
Save oscarperpinan/36fe51c6160d25afacdd to your computer and use it in GitHub Desktop.
solaR and raster to compute monthly averages of solar radiation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(solaR) | |
library(raster) | |
## Replace it with your data file(s) with global radiation on the | |
## horizontal plane | |
SIS <- brick('/home/datos/CMSAF/CMSAF_2010_2011_SISdm/SISd2010.grd') | |
## This line is only needed to make this example faster. You don't | |
## need in your code. | |
SIStoy <- crop(SIS, extent(-0.2, 0.2, 39.8, 40.2)) | |
names(SIStoy) <- paste0('d', 1:365) | |
## Create a new layer with the latitude values, because it is needed in the 'calcGef' function. | |
lat <- init(SIStoy, 'y') | |
names(lat) <- 'lat' | |
## Stack together the latitude layer and the radiation RasterStack | |
SIStoy <- stack(lat, SIStoy) | |
## Auxiliary function to be used with 'raster::calc' | |
myGef <- function(x) | |
{ | |
## x is a vector, corresponding to each cell of the RasterStack | |
## object, with an element per each layer (366 = 365 + 1 in this | |
## example). The first element is the latitude, and the remaining | |
## elements are the radiation values. | |
lat <- x[1] | |
## Time series using `zoo`, and fBTd to define the time index | |
G0 <- zoo(x[-1], fBTd(mode = 'serie')) | |
## Now use the solaR::calcGef function with them | |
gef <- calcGef(lat, modeRad = 'bd', dataRad = G0) | |
## And return the monthly averages of one of the variables | |
## provided by this function, for example Gefd | |
as.data.frameM(gef)$Gefd | |
} | |
## Finally, use raster::calc with the auxilary function applied to the | |
## RasterStack object. | |
GefM <- calc(SIStoy, fun = myGef) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment