This is a practical example in NetCDF4 format and for data reading, writing, and transforming. This dataset is a partial output of the Surface Air Temperature at Reference Height (TREFHT) which is monthly averaged of Jan. 2004 from a CAM5 simulation. This dataset only contains a tiny part of ultra-large simulations conducted by Mr Prabhat and Michael Wehner of Lawrence Berkeley National Laboratory.

An R data file contains two lists: `def`

for structure
definition of “TREFHT” in `ncvar4`

class (see pbdNCDF4
package for details), and `data`

for output values of simulation in a
matrix where rows are for 1152 longitudes and columns are for 768 latitudes.

Version 5.0 of the Community Atmosphere Model (CAM) is the latest in a series of global atmosphere models developed primarily at the National Center for Atmospheric Research (NCAR).

`TREFHT`

contains two lists: `def`

and `data`

.

`def`

is a list contains usual definitions of NetCDF4. In this case,
they define the variable “TREFHT” including 2D dimensions 1152
longitudes and 768 latitudes, 1 time step, the unit in Kelvin, ... etc.

`data`

contains values in matrix with dimension *1152*768*. Note that this matrix stores data in C format (column
major), so it needs a transpose to obtains the R/Fortran format (row major).
Also, the longitude order is not the same as the maps package. Please
see the example below for the adjustment or by calling
`demo('trefht','pbdDEMO')`

inside an R session.

Mr Prabhat and Michael Wehner.

More datasets are available on ESGF (http://www.earthsystemgrid.org/) through the C20C project (on the NERSC portal).

CAM5: http://www.cesm.ucar.edu/models/cesm1.0/cam/

Programming with Big Data in R Website: http://r-pbd.org/

`ncvar_put_2D`

and `ncvar_get_2D`

.

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | ```
## Not run: library(maps)
library(RColorBrewer)
library(pbdDEMO, quiet = TRUE)
lon <- TREFHT$def$dim[[1]]$vals # longitude
lat <- TREFHT$def$dim[[2]]$vals # latitude
da <- TREFHT$data # surface temperature
# Define Axes.
x <- c(lon[lon > 180] -360, lon[lon <= 195]) # adjustment for maps
y <- lat
z <- rbind(da[lon > 180,], da[lon <= 195,]) # adjustment for maps
xlim <- range(x)
ylim <- range(y)
zlim <- range(z)
col.z <- c(colorRampPalette(c("#0000FF", "#2BFCD3"))(100),
colorRampPalette(c("#2BFCD3", "#5300AB"))(100),
colorRampPalette(c("#5300AB", "#7CFA82"))(100),
colorRampPalette(c("#7CFA82", "#A90055"))(100),
colorRampPalette(c("#A90055", "#D6FC28"))(100),
colorRampPalette(c("#D6FC28", "#FE0001"))(100))
# Plot
layout(matrix(c(1, 2), ncol = 1), heights = c(2, 1))
par(mar = c(4, 4, 4, 0))
plot(NULL, NULL, xlim = xlim, ylim = ylim, type = "n", axes = FALSE,
xlab = "Longitude", ylab = "Latitude", main = "TREFHT (Jan. 2004)")
image(x, y, z, zlim = zlim, xlim = xlim, ylim = ylim,
col = col.z, add = TRUE)
# Add Map.
map(add = TRUE)
abline(h = c(-23.5, 0, 23.5), v = 0, lty = 2)
xtickets <- seq(-180, 180, by = 30)
ytickets <- seq(-90, 90, by = 30)
box()
axis(1, at = xtickets, labels = xtickets)
axis(2, at = ytickets, labels = ytickets)
# Add Legend.
z.temp <- matrix(seq(zlim[1], zlim[2], length = 500), ncol = 1)
ztickets <- seq(230, 300, by = 10)
par(mar = c(4, 4, 0, 1))
plot(NULL, NULL, xlim = zlim, ylim = c(0, 1), type = "n", axes = FALSE,
xlab = "TREFHT (Kelvin)", ylab = "")
image(z.temp, 0, z.temp, zlim = zlim, xlim = zlim, ylim = c(0, 1),
col = col.z, add = TRUE)
axis(1, at = ztickets, labels = ztickets)
## End(Not run)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.