read.nc: Read a NetCDF Dataset

Description Usage Arguments Details Value Author(s) References Examples

View source: R/RNetCDF.R

Description

Read all data from a NetCDF dataset.

Usage

1
   read.nc(ncfile, unpack=TRUE)

Arguments

ncfile

Object of class "NetCDF" which points to the NetCDF dataset (as returned from open.nc).

unpack

Unpack "packed" variables if set to TRUE (default).

Details

This function reads all variable data from a NetCDF dataset into a single list. The list elements (arrays) have the same names as the variables in the NetCDF dataset.

Packed variables can optionally be returned in an unpacked state (see var.get.nc for more information).

Value

A list with the list elements containing the variable data of the NetCDF dataset.

Author(s)

Pavel Michna, Milton Woods

References

http://www.unidata.ucar.edu/software/netcdf/

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
30
31
##  Create a new NetCDF dataset and define two dimensions
nc <- create.nc("read.nc")

dim.def.nc(nc, "station", 5)
dim.def.nc(nc, "time", unlim=TRUE)
dim.def.nc(nc, "max_string_length", 32)

##  Create three variables, one as coordinate variable
var.def.nc(nc, "time", "NC_INT", "time")
var.def.nc(nc, "temperature", "NC_DOUBLE", c(0,1))
var.def.nc(nc, "name", "NC_CHAR", c("max_string_length", "station"))

##  Put some missing_value attribute for temperature
att.put.nc(nc, "temperature", "missing_value", "NC_DOUBLE", -99999.9)

##  Define variable values
mytime        <- c(1:2)
mytemperature <- c(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, NA, NA, 9.9)
myname        <- c("alfa", "bravo", "charlie", "delta", "echo")

##  Put the data
var.put.nc(nc, "time", mytime, 1, length(mytime))
var.put.nc(nc, "temperature", mytemperature, c(1,1), c(5,2))
var.put.nc(nc, "name", myname, c(1,1), c(32,5))

sync.nc(nc)

##  Get the data
read.nc(nc)

close.nc(nc)

Example output

$time
[1] 1 2

$temperature
     [,1] [,2]
[1,]  1.1  6.6
[2,]  2.2  7.7
[3,]  3.3   NA
[4,]  4.4   NA
[5,]  5.5  9.9

$name
[1] "alfa"    "bravo"   "charlie" "delta"   "echo"   

RNetCDF documentation built on May 2, 2019, 6:12 p.m.