-
Notifications
You must be signed in to change notification settings - Fork 19
Gridpp in R
Thomas Nipen edited this page Oct 16, 2022
·
12 revisions
[Under development]
The package can be loaded as follows (from the build/extras/SWIG/R folder):
dyn.load("gridpp.so")
source("gridpp.R")
cacheMetaData(1)Functions can be called in the same way as in python. Objects can also be created in the same way. However, class member functions are called in a different way.
structure = BarnesStructure(10000, 0)
p1 = Point(0, 0)
p2 = Point(0, 0.1)
Structure_corr(structure, p1, p2)To get ready for the examples in the next sections, run the code below to set up necessary variables (you will also need the test datasets). This retrieves air temperature and precipitation from the observation, analysis, and forecast files as well as metadata about the grids.
dyn.load("R/gridpp.so")
source("R/gridpp.R")
cacheMetaData(1)
require(ncdf4)
array2list = function(ar) {
q = lapply(seq(dim(ar)[2]), function(x) ar[ , x])
return(q)
}
nc = nc_open("analysis.nc")
ilats = ncvar_get(nc, 'latitude')
ilons = ncvar_get(nc, 'longitude')
ielevs = ncvar_get(nc, 'surface_geopotential')
igrid = Grid(array2list(ilats), array2list(ilons), array2list(ielevs))
temp_analysis = ncvar_get(nc, 'air_temperature_2m')
nc_close(nc)
nc = nc_open("output.nc")
olats = ncvar_get(nc, 'latitude')
olons = ncvar_get(nc, 'longitude')
oelevs = ncvar_get(nc, 'altitude')
ogrid = Grid(array2list(olats), array2list(olons), array2list(oelevs))
nc_close(nc)
nc = nc_open("obs.nc")
plats = ncvar_get(nc, 'latitude')
plons = ncvar_get(nc, 'longitude')
pelevs = ncvar_get(nc, 'altitude')
points = Points(plats, plons, pelevs)
temp_obs = ncvar_get(nc, 'air_temperature_2m')
precip_obs = ncvar_get(nc, 'precipitation_amount')
nc_close(nc)structure = BarnesStructure(10000, 0)
pbackground = nearest(igrid, points, array2list(temp_analysis[,,1]))
ratios = rep(0.1 , length(pbackground))
max_points = 5
ovalues = optimal_interpolation(igrid, array2list(temp_analysis[,,1]), points, temp_obs, ratios, pbackground, structure, max_points)