# do not execute on CRAN: 
# https://stackoverflow.com/questions/28961431/computationally-heavy-r-vignettes
is_check <- ("CheckExEnv" %in% search()) || any(c("_R_CHECK_TIMINGS_",
             "_R_CHECK_LICENSE_") %in% names(Sys.getenv()))
#knitr::opts_chunk$set(eval = !is_check)
#rmarkdown::render("vignettes/useCase.Rmd")
#knitr::opts_knit$set(root.dir = '..')
knitr::opts_chunk$set(
    #, fig.align = "center"
    #, fig.width = 3.27, fig.height = 2.5, dev.args = list(pointsize = 10)
    #,cache = TRUE
    #, fig.width = 4.3, fig.height = 3.2, dev.args = list(pointsize = 10)
    #, fig.width = 6.3, fig.height = 6.2, dev.args = list(pointsize = 10)
    # works with html but causes problems with latex
    #,out.extra = 'style = "display:block; margin: auto"' 
    )
knitr::knit_hooks$set(spar = function(before, options, envir) {
    if (before) {
        par(las = 1 )                   #also y axis labels horizontal
        par(mar = c(2.0,3.3,0,0) + 0.3 )  #margins
        par(tck = 0.02 )                          #axe-tick length inside plots             
        par(mgp = c(1.1,0.2,0) )  #positioning of axis title, axis labels, axis
     }
})
#themeTw <- theme_bw(base_size = 10) + theme(axis.title = element_text(size = 9))
#bgiDir <- "~/bgi"

NetCDF in R Tutorial

from Dept. Geography, Univ. Oregon

Create and write a netCDF file (ncdf4 version)

In this example, the arrays created in [netCDF-df-to-array.html] are written out using the ncdf4 package.

library(ncdf4)

# define dimensions
londim <- ncdim_def("lon","degrees_east",as.double(lon3)) 
latdim <- ncdim_def("lat","degrees_north",as.double(lat3)) 
timedim <- ncdim_def("time",tunits3,as.double(t3))

# define variables
fillvalue <- 1e32
dlname <- "2m air temperature"
tmp_def <- ncvar_def("tmp","deg_C",list(londim,latdim,timedim),fillvalue,dlname,prec = "single")
dlname <- "mean_temperture_warmest_month"
mtwa_def <- ncvar_def("mtwa","deg_C",list(londim,latdim),fillvalue,dlname,prec = "single")
dlname <- "mean_temperature_coldest_month"
mtco_def <- ncvar_def("mtco","deg_C",list(londim,latdim),fillvalue,dlname,prec = "single")
dlname <- "mean_annual_temperature"
mat_def <- ncvar_def("mat","deg_C",list(londim,latdim),fillvalue,dlname,prec = "single")

# create netCDF file and put arrays
ncfname <- "cru10min30_ncdf4.nc"
ncout <- nc_create(ncfname,list(tmp_def,mtco_def,mtwa_def,mat_def), force_v4 = T)

# put variables
ncvar_put(ncout,tmp_def,tmp_array3)
ncvar_put(ncout,mtwa_def,mtwa_array3)
ncvar_put(ncout,mtco_def,mtco_array3)
ncvar_put(ncout,mat_def,mat_array3)

# put additional attributes into dimension and data variables
ncatt_put(ncout,"lon","axis","X") #,verbose = FALSE) #,definemode = FALSE)
ncatt_put(ncout,"lat","axis","Y")
ncatt_put(ncout,"time","axis","T")

# add global attributes
ncatt_put(ncout,0,"title",title$value)
ncatt_put(ncout,0,"institution",institution$value)
ncatt_put(ncout,0,"source",datasource$value)
ncatt_put(ncout,0,"references",references$value)
history <- paste("P.J. Bartlein", date(), sep = ", ")
ncatt_put(ncout,0,"history",history)
ncatt_put(ncout,0,"Conventions",Conventions$value)

# close the file, writing data to disk
nc_close(ncout)


bgctw/ncdfTools documentation built on Jan. 29, 2020, 1:16 p.m.