Skip to content

Instantly share code, notes, and snippets.

View oscarperpinan's full-sized avatar

Oscar Perpiñán Lamigueiro oscarperpinan

View GitHub Profile
@oscarperpinan
oscarperpinan / solarSpatial.R
Last active August 31, 2015 07:31
solaR and raster to compute monthly averages of solar radiation
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)
@oscarperpinan
oscarperpinan / SPEI.R
Created March 25, 2015 10:26
SPEI example
library(raster)
library(rasterVis)
library(zoo)
## Download SPEI file
setwd(tempdir())
download.file('https://digital.csic.es/bitstream/10261/104742/5/SPEI_03.nc', 'SPEI_03.nc', method = 'wget')
## Read it with `raster`
SPEI <- brick('SPEI_03.nc')
library(data.table)
data(movies, package='ggplot2')
library(lattice)
movies <- data.table(movies)
nBins <- 500
## 2D Binning
movies[, c('ratBin', 'votesBin'):=list(floor(rating/nBins), floor(votes/nBins))]
library(data.table)
library(lattice)
## Create a toy example
dt <- data.table(A=rnorm(1e7, mean=0, sd=1))
## Bin the data using equal-width intervals
brks <- seq(-6, 6, length=10000)
## and := to avoid an additional copy
dt[,bin:=findInterval(A, brks)]
## Finally aggregate with mean by bin
library(solaR)
## Monthly averages of daily values (Wh/m2)
G0dm <- c(2.766,3.491,4.494,5.912,6.989,7.742,7.919,7.027,5.369,3.562,2.814,2.179)*1000;
Ta <- c(10, 14.1, 15.6, 17.2, 19.3, 21.2, 28.4, 29.9, 24.3, 18.2, 17.2, 15.2)
g0 <- calcG0(lat=37.2, modeRad='aguiar', dataRad=G0dm)
xyplot(g0)
library(solaR)
## Download data from NREL (see help(calcG0))
NRELurl <- 'http://goo.gl/fFEBN'
dat <- read.table(NRELurl, header=TRUE, sep=',')
names(dat) <- c('date', 'hour', 'G0', 'B', 'D0', 'Ta')
##B is direct normal. We need direct horizontal.
dat$B0 <- dat$G0-dat$D0
@oscarperpinan
oscarperpinan / siar.geojson
Created September 20, 2013 08:05
SIAR stations
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@oscarperpinan
oscarperpinan / siar.R
Created September 20, 2013 08:09
Code to create siar.geojson
library(sp)
library(rgdal)
setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/solar/gh-pages/data/SIAR.csv', 'siar.csv', method='wget')
siar <- read.csv('siar.csv')
summary(siar)
siarSP <- SpatialPointsDataFrame(siar[,c(6, 7)], siar[,-c(6,7)])
writeOGR(siarSP, 'siar.geojson', 'siarSP', driver='GeoJSON')
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@oscarperpinan
oscarperpinan / stamenPoly.R
Last active December 28, 2015 09:59
Variation of https://gist.github.com/oscarperpinan/5451682 using SpatialPolygons
library(sp)
library(ggmap)
## latticeExtra must be loaded after ggmap because both ggplot2 and
## latticeExtra define a 'layer' function. We need the definition from
## latticeExtra.
library(latticeExtra)
## We only need maptools to get an example
library(maptools)
SIDS <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
IDvar="FIPSNO",