Installing ncdf in R

NB a prerequisite is that the netcdf libraries are already installed.

  $ mkdir -p ~/.R/libs
  $ export R_LIBS=~/.R/libs
  $ echo 'R_LIBS=~/.R/libs/'
  $ export R_LIBS=~/.R/libs/
  $ echo 'R_LIBS=~/.R/libs/'
  $ echo 'R_LIBS=~/.R/libs/' >> ~/.Renviron
  #Download the ncdf package from CRAN
  #All this can be done in a temporary area ... (?)
  $ tar xvzf ncdf_1.6.tar.gz 
  $ R CMD INSTALL ncdf
  $ history
  ...
  ...
  # there are perhaps even easier ways to do this:

  $ install.packages("gplots", dependencies = TRUE)

  # no source was linked ... but the mirror choose automatically opens
  # and one just selects a site and the install happens!
  ...
  $ 

Making use of ncdf in R

  $ R
   Method='unspecified'
   Shortname='outfile'
   library(ncdf)
   nd = open.ncdf("interpolations.nc")
   x=get.var.ncdf(nd,"Original")
   y=get.var.ncdf(nd,"Interpolations")
   x[x==-1]=0
   v <- array(x, length(x))
   w <- array(y, length(y))
   xx=x[!is.na(x) & x>=0 & x < 100]
   yy=y[!is.na(x) & x>=0 & x < 100]
   yyy=yy[!is.na(yy) & yy>=0 & yy < 100]
   xxx=xx[!is.na(yy) & yy>=0 & yy < 100]
   jpeg(filename=paste(Shortname,'jpg',sep='.'))
   plot(xxx,yyy,pch=46,xlab='Original Value',ylab='Interpoltion',main=Method)
   XC=cor(xxx,yyy)
   ff <- lm(xxx ~ yyy)
   ff$coefficients[1]
   ff$coefficients[2]
   z=ff$coefficients[1]*xxx + ff$coefficients[2]
   #lines(xxx,z)
   text(0,95,"Linear Regression",pos=4)
   text(0,90,paste("Gradient =",format(ff$coefficients[1],digits=5)),pos=4)
   text(0,85,paste("Intercept =",format(ff$coefficients[2],digits=5)),pos=4)
   text(0,75,paste("Correlation coefficient =",format(XC,digits=5)),pos=4)
   dev.off()

Alternatively, place the code in a file and run from the command line:

 $ Rscript plot_netcdf.R "Test Plot" OUTPUT

that will produce the file OUTPUT.jpg with the plot title “Test Plot”.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • kvalobs/kvoss/system/qc2/8hdk376snf09zj37dk82s92/ncdf.txt
  • Last modified: 2022-05-31 09:29:32
  • (external edit)