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 / ayMadrid.org
Last active August 29, 2015 14:23
Ayuntamiento de Madrid
@oscarperpinan
oscarperpinan / mappingFlows.R
Created April 14, 2015 05:24
An alternative implementation of "Mapping Flows in R" (http://spatial.ly/2015/03/mapping-flows/) using `data.table` and `lattice`
### DATA SECTION
library(data.table)
## Read data with 'data.table::fread'
input <- fread("wu03ew_v1.csv", select = 1:3)
setnames(input, 1:3, new = c("origin", "destination","total"))
## Coordinates
centroids <- fread("msoa_popweightedcentroids.csv")
## 'Code' is the key to be used in the joins
@oscarperpinan
oscarperpinan / mapsWithR.R
Created March 30, 2015 07:33
Respuesta a comentario en blog
library(sp)
## Grabar tabla excel en formato CSV usando ; como separador
## Leemos saltando dos primeras líneas
pts <- read.csv2('/tmp/listado-longitud-latitud-municipios-espana.csv', skip = 2)
## Transformamos a SPDF usando columnas de longitud-latitud como coordenadas
munPts <- SpatialPointsDataFrame(pts[, c(5, 4)], pts[,-c(4, 5)],
proj4string = CRS('+proj=longlat +datum=WGS84'))
## Pintamos el resultado para comprobar
spplot(munPts["Altitud"])
@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')
@oscarperpinan
oscarperpinan / readMACC.R
Last active August 29, 2015 14:15
MACC example
## Ejemplo para leer netCDF con R
library(raster)
library(zoo)
## Coodinates of point (lat,lon)
latitude=37
longitude=-2
z=500
## lon ranges from 0 to 360, so correction for convention of lon <0
if (longitude < 0) longitude <- longitude + 360
library(raster)
library(zoo)
## Indice temporal completo
tt <- seq(as.Date('2015-01-01'), as.Date('2015-12-31'), by = 'month')
tt <- as.yearmon(tt)
## Datos disponibles: indice temporal incompleto
tt1 <- tt[-c(5, 8, 9)]
f <- system.file("external/test.grd", package="raster")
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)
@oscarperpinan
oscarperpinan / orgChapter.tex
Created March 10, 2014 13:13
LaTeX file exported from orgChapter.org
\section{Code blocks}
\label{sec-1}
\label{sec:sectionA}
Code is enclosed within \texttt{\#+begin\_src/\#+end\_src}
\href{http://orgmode.org/manual/Structure-of-code-blocks.html}{blocks}.
\lstset{language=R,numbers=none}
\begin{lstlisting}
x <- 1:10
y <- 1:10
@oscarperpinan
oscarperpinan / master.tex
Last active August 29, 2015 13:57
Master file for texPart.tex
\documentclass[smallroyalvopaper]{memoir}
\settypeblocksize{6.5in}{4.5in}{*}
\setlrmargins{*}{*}{1}
\checkandfixthelayout
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Misc packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[english]{babel}
\usepackage[usenames,dvipsnames]{xcolor}