# EXAMPLE R-CODE FOR ACCESSING NETCDF FILES # by Karsten Steinhaeuser, March 2009 # Load the ncdf library library(ncdf) ### TEMPERATURE ### # open the netCDF file for reading ncdata <- open.ncdf("air.2m.1950-1999.nc") # print metadata print(ncdata) # get latitude coordinates lat <- get.var.ncdf(ncdata,"lat") # get longitude coordinates lon <- get.var.ncdf(ncdata,"lon") # get time axis time <- get.var.ncdf(ncdata,"time") # read air temperature data into an array air <- get.var.ncdf(ncdata,"air") # close the netCDF file close.ncdf(ncdata); # BEGIN ANALYSIS HERE... # Example: convert temperature from Kelvin to Degrees Celsius air <- air-273; ### PRECIPITATION ### # open the netCDF file for reading ncdata <- open.ncdf("prate.sfc.1950-1999.nc") # print metadata print(ncdata) # get latitude coordinates lat <- get.var.ncdf(ncdata,"lat") # get longitude coordinates lon <- get.var.ncdf(ncdata,"lon") # get time axis time <- get.var.ncdf(ncdata,"time") # read air temperature data into an array prate <- get.var.ncdf(ncdata,"prate") # close the netCDF file close.ncdf(ncdata); # BEGIN ANALYSIS HERE... # Example: convert precipitation from kilograms per square meter per second # to millimeters per day prate <- prate*86400;