Chapter 1 Read NetCDF in R
First you need to install some R packages in order to read the NetCDF data into your R environment:
1.1 Explore NetCDF
Now we use the R package to load an example NetCDF dataset into R. The function nc_open opens a data stream, but does not yet load all the data into the memory. We can use the netcdf4.helpers package to request some more information about the NetCDF dataset. For instance, we can requests the dates.
library(ncdf4)
library(ncdf4.helpers)
nc <- ncdf4::nc_open("data/OUT_EVAP_monthly_1971.nc") # or slimply use nc_open()
datetimes <- ncdf4.helpers::nc.get.time.series(nc)
head(datetimes)## [1] "1971-01-18 00:00:00" "1971-02-16 12:00:00" "1971-03-18 00:00:00" "1971-04-17 12:00:00" "1971-05-18 00:00:00" "1971-06-17 12:00:00"
## [1] "1971-07-18 00:00:00" "1971-08-18 00:00:00" "1971-09-17 12:00:00" "1971-10-18 00:00:00" "1971-11-17 12:00:00" "1971-12-18 00:00:00"
Request information about the variables.
## [1] "OUT_EVAP"
1.2 Get Data
With the function ncvar_get, we can load data of a specific variable into an array. Use the start and count parameter to load slices in the available dimensions.
FIGURE 1.1: Simple image plot of a single data slice in the time dimension.