This exercise will provide some code for manipulating climate change data from the Regional Climate Downscaling by copy the link into your browser:
http://regclim.coas.oregonstate.edu/data-access/index.html or just select the link here: Regional Climate Downscaling. IMPORTANT: For each climate projection, must change name in first command and file name in last command.

1. Exercise 2.5 - Download and extract zip folder into your preferred location
2. Set working directory to the extracted folder in R under File - Change dir...
3. First we need to load the packages needed for the exercise
library(ncdf)
4. Now open the script "NetCDF_Script.R" and run code directly from the script
5. Open netCDF and setting verbose=true provides details about the data in the netcdf file including the varid. You need to know the varid to select the variable you want to extract/summarize. Note: the dimensions x, y, time also get a varid so you will need to subtract 3 from the varid of interest to get the correct one.

dat <- open.ncdf("Monthly_AvgMinTemp_1995-99_MPI.nc", write=TRUE, readunlim=TRUE, verbose=TRUE)

# Read data this loads all the data from the downloaded variable into the
tmin #object
tmin <- dat\$var[]
tmin

#####################################
#The following illustrates how to read the data
#####################################
print(paste(tmin\$name)) #in this case the 'field name' is TAMIN

# Grab data for TAMIN variable and place in object df1
df1 <- get.var.ncdf(dat, tmin)
head(df1, n = 10L) # head(x, n = 6L, ...); head returns the first data entries,
#x is the object, n sets the number of entries displayed. tail returns the last of the data #entries.

#Dimensions of df1 (x, y, time)
dim(df1)

#Dimensions can also be examined one at a time
dim(df1) # number of x grids (36)
dim(df1) # number of y grids (21)
dim(df1) # number of months in file (49)
#NOTE: FILE INCLUDES MONTHS OTHER THAN JANUARY (Jans are 1,13,25,37,49)

#Check first element
df1[1,1,1]

#Check first January for all x,y
df1[,,1]

#Create a new matrix which is monthly averages for each grid cell. Make the new matix #the same size (i.e. same number of rows and columns as there are in the #dataframe df1
sum1 <- array(data=NA, c(dim(df1),dim(df1) ))
dim(sum1)

#Create January mean TAMIN for each x-y coordinate
for(i in 1:dim(df1)){ # loop over x-coords
for(j in 1:dim(df1)){ # loop over y-coords
sum1[i, j] <- (df1[i,j,1]+df1[i,j,13]+df1[i,j,25]+df1[i,j,37]+df1[i,j,49])/5
}
}

#head(sum1) ## useful for large files
sum1

###########################################################

#Create netcdf file from sum1 (contains matrix of new data)

###########################################################

#Get x and y coordinates from original "dat" ncdf file

x = get.var.ncdf(nc=dat,var )

y = get.var.ncdf(nc=dat,var )

#Check dimensions

length(x)

length(y)

dim(sum1)

#Define the netcdf coordinate variables - note that these are coming

#from the dat file with actual values

dim1 = dim.def.ncdf( "X","meters", as.double(x))

dim2 = dim.def.ncdf( "Y","meters", as.double(y))

#Define the EMPTY (climate) netcdf variable and define names that will be used in the #var.def.ncdf function.

#Define climate variable names

new.name <- 'mintemp'

#Define units of measurement for variable

units <- 'degreesC'

#Define long name for variable

long.name <- 'Jan average min temperature'

varz = var.def.ncdf(new.name,units, list(dim1,dim2), -1, longname=long.name)

#Associate the netcdf variable with a netcdf file, put the variable into the file, and close

nc.ex = create.ncdf( "MPI1999-95.nc", varz )

put.var.ncdf(nc.ex, varz, sum1)

close.ncdf(nc.ex)