getdata: Extracting Data from NetCDF File Connection

Description Usage Arguments Details Value Author(s) Examples

Description

Built to extract certain variables/forecast times from NetCDF files.

Usage

1
2
## Basic usage
getdata(nc, init, varname, steps, level = NULL, subset = NULL, silent = FALSE )

Arguments

nc

Open ncdf4 connection

init

Date, POSIXt, or character object which can be converted into POSIXt. Date/time when the forecast was initialized. Crucial to compute the forecast steps (lead times).

varname

character, name of the variable to load.

steps

Optional. If not set all forecast steps from the nc file will be returned. Single or multiple steps can be defined (numeric).

level

Used when loading variables from pressure level files where the level has to be specified as a numeric value.

subset

Optional, default NULL. If not set the whole grid from the nc file will be returned. Can also be a extent object or a RasterLayer object. See details.

silent

logical, default is FALSE. If TRUE some messages will be suppressed.

Details

subset allows to only load parts of the data from the nc connection. If subset is of class Extent (see extent) then only (a spatial) subset of the data will be taken. If subset is of class RasterLayer (see raster) the extent of the RasterLayer will be used to crop the data, the resolution (or grid specification) of this RasterLayer will be used to re-sample the data. This allows to change the resolution when loading the data.

Value

Returns a RasterLayer (if only one step) is loaded or a RasterStack object where each layer corresponds to a forecast step. Names of the layers contain the initial initial date plus the forecast step.

Author(s)

Reto Stauffer

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## Not run: 
library('maps'); library("raster")

# Open netcdf connections
init <- as.POSIXct("2011-11-18 00:00")
init_str <- strftime(init,"
ncs <- nc_open(sprintf("costgrib/ECMWF_cost_sfc_
ncp <- nc_open(sprintf("costgrib/ECMWF_cost_pl_

# Loading "tcw" from surface file, step 24, full field
x <- getdata(ncs,init,"tcw",24)
plot(x); map(add=T)

# Using an extent subset and loading three different steps
x <- getdata(ncs,init,"tcw",c(12,24,48),subset=extent(c(10,30,40,50)))
plot(x)

# Loading pressure level data (u on 700 hPa) and resample the data
# onto a coarser resolution.
template <- raster(xmn=9.5,xmx=30.5,ymn=39.5,ymx=50.5,nrows=21,ncols=11,
               crs=crs("+proj=longlat +datum=WGS84 +ellps=WGS84 +no_defs +towgs84=0,0,0"))
x <- getdata(ncp,init,"u",144,level=700,subset=template)
plot(x); map(add=T)

# Close connections
nc_close(ncs)
nc_close(ncp)

## End(Not run)

retostauffer/Rcost documentation built on May 7, 2019, 9:43 p.m.