This is an example of using the spatial.R code to aggregate gridded weather data. Specifically, we aggregate gridded maximum temperature data to Brazilian municipalities.

First, load the required packages. Note that this library, research-common, is in my local folder ~/projects/research-common, but it could be anywhere.


In [16]:
library(PBSmapping)
library(ncdf4)
source("~/projects/research-common/R/distance.R")
source("~/projects/research-common/R/spatial.R", chdir=T)

Load the shapefile. I use PBSmapping, which represents shapefiles as lists of points.


In [28]:
shape <- importShapefile("~/data/political/brazil/BRA_adm2/BRA_adm2")

Load the gridded data.


In [8]:
ncdf <- nc_open("~/data/agmerra/monthly/tasmax_agmerra_1980-2010.mm.nc4")
longitude <- ncvar_get(ncdf, "lon")
latitude <- ncvar_get(ncdf, "lat")

The prepareEvents function identifies the center of each grid cell, which will be used to make the averages. In a loop over multiple files, it's best to only do this once.


In [10]:
events <- prepareEvents(longitude, latitude, attr(shape, "projection"))

Get out the weather data itself. It's important that this has dimensions of the order longitude, latitude, time.


In [25]:
lonlattimeraster <- ncvar_get(ncdf, "tasmax")

Also get out the time variable, for producing output.


In [14]:
time <- ncvar_get(ncdf, "time")

Iterate through all of the polygons, constructing averages and adding them to the result dataframe.


In [26]:
values <- data.frame(PID=c(), time=c(), average=c())
for (pid in 1:10) {
    print(pid)
    average <- 
        spaceTimeRasterAverage(events, lonlattimeraster,
                               subset(shape, PID == pid))
    values <- rbind(values, data.frame(PID=pid, time, average))
}


[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Here's what the final result looks like.


In [27]:
values


PIDtimeaverage
1 43859.04303.7097
1 43888.04303.9345
1 43919.04304.1944
1 43949.04303.4933
1 43980.04302.4032
1 44010.04301.1283
1 44041.04300.2048
1 44072.04301.2161
1 44102.04301.6150
1 44133.04302.1299
1 44163.04302.3475
1 44194.04303.4847
1 44225.04303.9887
1 44253.04304.1089
1 44284.04302.1355
1 44314.04302.2700
1 44345.04301.2113
1 44375.04299.9608
1 44406.04299.5403
1 44437.04300.5734
1 44467.04301.3025
1 44498.04301.0186
1 44528.04302.8075
1 44559.04303.8790
1 44590.04303.6194
1 44618.04304.2107
1 44649.04304.7847
1 44679.04301.8208
1 44710.04300.4879
1 44740.04301.1892
10 54268.04304.0499
10 54299.04305.0580
10 54329.04304.6178
10 54360.04304.3485
10 54390.04304.0840
10 54421.04303.6433
10 54452.04303.1919
10 54480.04303.3282
10 54511.04304.1026
10 54541.04303.9692
10 54572.04303.7490
10 54602.04303.0998
10 54633.04305.0337
10 54664.04305.2790
10 54694.04305.9802
10 54725.04305.2855
10 54755.04305.1216
10 54786.04303.5356
10 54817.04303.2183
10 54845.04304.2313
10 54876.04304.3804
10 54906.04303.9482
10 54937.04303.0795
10 54967.04302.3376
10 54998.04302.3311
10 55029.04302.9334
10 55059.04302.4094
10 55090.04301.5102
10 55120.04301.1220
10 55151.04301.9252