R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.0 Patched (2005-05-12), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("ncdf-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('ncdf') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "aput.var.ncdf" > > ### * aput.var.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: put.var.ncdf > ### Title: Write data to a netCDF file > ### Aliases: put.var.ncdf > ### Keywords: utilities > > ### ** Examples > > # Make a few dimensions we can use > nx <- 3 > ny <- 4 > nt <- 5 > xvals <- (1:nx)*100. > dimX <- dim.def.ncdf( "X", "meters", xvals ) > dimY <- dim.def.ncdf( "Y", "meters", (1:ny)*100. ) > dimT <- dim.def.ncdf( "Time", "seconds", (1:nt)/100., unlim=TRUE ) > > # Make varables of various dimensionality, for illustration purposes > mv <- 1.e30 # missing value to use > var1d <- var.def.ncdf( "var1d", "units", dimX, mv ) > var2d <- var.def.ncdf( "var2d", "units", list(dimX,dimY), mv ) > var3d <- var.def.ncdf( "var3d", "units", list(dimX,dimY,dimT), mv ) > > # Create the test file > nc <- create.ncdf( "writevals.nc", list(var1d,var2d,var3d) ) > > # Write some data to the file > data1d <- runif(nx) > put.var.ncdf( nc, var1d, data1d ) # no start or count: write all values > put.var.ncdf( nc, var1d, 27.5, start=3, count=1 ) # Write a value to the third slot > > data2d <- runif(nx*ny) > put.var.ncdf( nc, var2d, data2d ) # no start or count: write all values > # Write a 1-d slice to the 2d var > put.var.ncdf( nc, var2d, data1d, start=c(1,2), count=c(nx,1) ) > # Note how "-1" in the count means "the whole dimension length", > # which equals nx in this case > put.var.ncdf( nc, var2d, data1d, start=c(1,3), count=c(-1,1) ) > > # The 3-d variable has an unlimited dimension. We'll loop over the timesteps, > # writing one 2-d slice per timestep. > for( i in 1:nt) + put.var.ncdf( nc, var3d, data2d, start=c(1,1,i), count=c(-1,-1,1) ) > > close.ncdf(nc) [[1]] [1] 6 > > #---------------------------------------------------------------------- > # Illustrate creating a character type variable > #---------------------------------------------------------------------- > cnames <- c('red', 'orange', 'green', 'yellow', 'puce', 'colorwithverylongname' ) > nstrings <- length(cnames) > > #-------------------------------------------------------------- > # Make dimensions. Setting 'dimnchar' to have a length of 12 > # means that the maximum color name > # length can be 12. Longer names will be truncated to this. > #-------------------------------------------------------------- > dimnchar <- dim.def.ncdf('nchar', '', 1:12 ) > dimcolorno <- dim.def.ncdf('colorno', '', 1:nstrings ) > > varcolors <- var.def.ncdf('colors', '', list(dimnchar, dimcolorno), + NA, prec="char" ) > > ncid <- create.ncdf( 'colornames.nc', list(varcolors) ) > > put.var.ncdf( ncid, 'colors', cnames ) > > close.ncdf( ncid ) [[1]] [1] 6 > > > > cleanEx(); ..nameEx <- "att.get.ncdf" > > ### * att.get.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: att.get.ncdf > ### Title: Get attribute from netCDF file > ### Aliases: att.get.ncdf > ### Keywords: utilities > > ### ** Examples > > # Make a simple netCDF file > filename <- "atttest_types.nc" > dim <- dim.def.ncdf( "X", "inches", 1:12 ) > var <- var.def.ncdf( "Data", "unitless", dim, -1 ) > ncnew <- create.ncdf( filename, var ) > > # Define some attributes of various types > attvaldbl <- 3.1415926536 > att.put.ncdf( ncnew, var, "testatt_dbl", attvaldbl, prec="double" ) [[1]] [1] 6 > attvalsingle <- c(1.0,4.0,9.0,16.0) > att.put.ncdf( ncnew, var, "testatt_single", attvalsingle ) [[1]] [1] 6 > # varid=0 means it's a global attribute > att.put.ncdf( ncnew, 0, "globalatt_int", 32000, prec="int" ) [[1]] [1] 6 > att.put.ncdf( ncnew, 0, "globalatt_short", 7, prec="short" ) [[1]] [1] 6 > att.put.ncdf( ncnew, 0, "description", + "this is a test file with attributes of various types") [[1]] [1] 6 > close.ncdf(ncnew) [[1]] [1] 6 > > # Now illustrate the use of the att.get.ncdf function by reading them back in > doitfor <- function( nc, var, attname ) { + av <- att.get.ncdf( nc, var, attname ) + if( av$hasatt ) { + print(paste("File",nc$filename,", var",var,"DOES have attribute", + attname)) + print(paste("Storage mode:",storage.mode(av$value))) + print("Attribute value:") + print(av$value) + } else { + print(paste("File",nc$filename,", var",var,"does NOT have attribute", + attname)) + } + } > > nc <- open.ncdf( filename ) > var <- "Data" > doitfor( nc, var, "testatt_dbl" ) [1] "File atttest_types.nc , var Data DOES have attribute testatt_dbl" [1] "Storage mode: double" [1] "Attribute value:" [1] 3.141593 > doitfor( nc, var, "testatt_single" ) [1] "File atttest_types.nc , var Data DOES have attribute testatt_single" [1] "Storage mode: double" [1] "Attribute value:" [1] 1 4 9 16 > doitfor( nc, var, "testatt_wacko" ) [1] "File atttest_types.nc , var Data does NOT have attribute testatt_wacko" > doitfor( nc, 0, "globalatt_int" ) [1] "File atttest_types.nc , var 0 DOES have attribute globalatt_int" [1] "Storage mode: integer" [1] "Attribute value:" [1] 32000 > doitfor( nc, 0, "globalatt_short" ) [1] "File atttest_types.nc , var 0 DOES have attribute globalatt_short" [1] "Storage mode: integer" [1] "Attribute value:" [1] 7 > doitfor( nc, 0, "description" ) [1] "File atttest_types.nc , var 0 DOES have attribute description" [1] "Storage mode: character" [1] "Attribute value:" [1] "this is a test file with attributes of various types" > > > > cleanEx(); ..nameEx <- "att.put.ncdf" > > ### * att.put.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: att.put.ncdf > ### Title: Put an attribute into a netCDF file > ### Aliases: att.put.ncdf > ### Keywords: utilities > > ### ** Examples > > # Make a simple netCDF file > filename <- "atttest_types.nc" > dim <- dim.def.ncdf( "X", "inches", 1:12 ) > var <- var.def.ncdf( "Data", "unitless", dim, -1 ) > ncnew <- create.ncdf( filename, var ) > > # Define some attributes of various types > attvaldbl <- 3.1415926536 > att.put.ncdf( ncnew, var, "testatt_dbl", attvaldbl, prec="double" ) [[1]] [1] 7 > attvalsingle <- c(1.0,4.0,9.0,16.0) > att.put.ncdf( ncnew, var, "testatt_single", attvalsingle ) [[1]] [1] 7 > # varid=0 means it's a global attribute > att.put.ncdf( ncnew, 0, "globalatt_int", 32000, prec="int" ) [[1]] [1] 7 > att.put.ncdf( ncnew, 0, "globalatt_short", 7, prec="short" ) [[1]] [1] 7 > att.put.ncdf( ncnew, 0, "description", + "this is a test file with attributes of various types") [[1]] [1] 7 > close.ncdf(ncnew) [[1]] [1] 7 > > > > cleanEx(); ..nameEx <- "close.ncdf" > > ### * close.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: close.ncdf > ### Title: Close a netCDF File > ### Aliases: close.ncdf > ### Keywords: utilities > > ### ** Examples > > ## Not run: nc <- open.ncdf("salinity.nc") > ## Not run: data <- get.var.ncdf( nc ) # Read the "only" var in the file > ## Not run: close.ncdf(nc) > > > > cleanEx(); ..nameEx <- "create.ncdf" > > ### * create.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: create.ncdf > ### Title: Create a netCDF File > ### Aliases: create.ncdf > ### Keywords: utilities > > ### ** Examples > > # Define an integer dimension > dimState <- dim.def.ncdf( "StateNo", "count", 1:50 ) > > # Make an integer variable. Note that an integer variable can have > # a double precision dimension, or vice versa; there is no fixed > # relationship between the precision of the dimension and that of the > # associated variable. We just make an integer variable here for > # illustration purposes. > varPop <- var.def.ncdf("Pop", "count", dimState, -1, + longname="Population", prec="integer") > > # Create a netCDF file with this variable > ncnew <- create.ncdf( "states_population.nc", varPop ) > > # Write some values to this variable on disk. > popAlabama <- 4447100 > put.var.ncdf( ncnew, varPop, popAlabama, start=1, count=1 ) > > close.ncdf(ncnew) [[1]] [1] 7 > > > > cleanEx(); ..nameEx <- "dim.def.ncdf" > > ### * dim.def.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: dim.def.ncdf > ### Title: Define a netCDF Dimension > ### Aliases: dim.def.ncdf > ### Keywords: utilities > > ### ** Examples > > # Define some straightforward dimensions > x <- dim.def.ncdf( "Lon", "degreesE", 0.5:359.5) > y <- dim.def.ncdf( "Lat", "degreesN", as.double(-89:89)) > t <- dim.def.ncdf( "Time", "days since 1900-01-01", 1:10, unlim=TRUE) > > # Make a variable with those dimensions. Note order: time is LAST > salinity <- var.def.ncdf("Salinity", "ppt", list(x,y,t), 1.e30 ) > > # Create a netCDF file with this variable > ncnew <- create.ncdf( "salinity.nc", salinity ) > > close.ncdf(ncnew) [[1]] [1] 7 > > # Now, illustrate some manipulations of the 'dim.ncdf' object. > filename <- "salinity.nc" > nc <- open.ncdf( filename ) > print(paste("File",filename,"contains",nc$ndims,"dimensions")) [1] "File salinity.nc contains 3 dimensions" > for( i in 1:nc$ndims ) { + print(paste("Here is information about dimension number",i,":")) + d <- nc$dim[[i]] + print(paste(" Name :",d$name)) + print(paste(" Units :",d$units)) + print(paste(" Length:",d$len)) + print(" Values:") + print(d$vals) + print(paste(" Unlimited:",d$unlim)) + } [1] "Here is information about dimension number 1 :" [1] " Name : Lon" [1] " Units : degreesE" [1] " Length: 360" [1] " Values:" [1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 [13] 12.5 13.5 14.5 15.5 16.5 17.5 18.5 19.5 20.5 21.5 22.5 23.5 [25] 24.5 25.5 26.5 27.5 28.5 29.5 30.5 31.5 32.5 33.5 34.5 35.5 [37] 36.5 37.5 38.5 39.5 40.5 41.5 42.5 43.5 44.5 45.5 46.5 47.5 [49] 48.5 49.5 50.5 51.5 52.5 53.5 54.5 55.5 56.5 57.5 58.5 59.5 [61] 60.5 61.5 62.5 63.5 64.5 65.5 66.5 67.5 68.5 69.5 70.5 71.5 [73] 72.5 73.5 74.5 75.5 76.5 77.5 78.5 79.5 80.5 81.5 82.5 83.5 [85] 84.5 85.5 86.5 87.5 88.5 89.5 90.5 91.5 92.5 93.5 94.5 95.5 [97] 96.5 97.5 98.5 99.5 100.5 101.5 102.5 103.5 104.5 105.5 106.5 107.5 [109] 108.5 109.5 110.5 111.5 112.5 113.5 114.5 115.5 116.5 117.5 118.5 119.5 [121] 120.5 121.5 122.5 123.5 124.5 125.5 126.5 127.5 128.5 129.5 130.5 131.5 [133] 132.5 133.5 134.5 135.5 136.5 137.5 138.5 139.5 140.5 141.5 142.5 143.5 [145] 144.5 145.5 146.5 147.5 148.5 149.5 150.5 151.5 152.5 153.5 154.5 155.5 [157] 156.5 157.5 158.5 159.5 160.5 161.5 162.5 163.5 164.5 165.5 166.5 167.5 [169] 168.5 169.5 170.5 171.5 172.5 173.5 174.5 175.5 176.5 177.5 178.5 179.5 [181] 180.5 181.5 182.5 183.5 184.5 185.5 186.5 187.5 188.5 189.5 190.5 191.5 [193] 192.5 193.5 194.5 195.5 196.5 197.5 198.5 199.5 200.5 201.5 202.5 203.5 [205] 204.5 205.5 206.5 207.5 208.5 209.5 210.5 211.5 212.5 213.5 214.5 215.5 [217] 216.5 217.5 218.5 219.5 220.5 221.5 222.5 223.5 224.5 225.5 226.5 227.5 [229] 228.5 229.5 230.5 231.5 232.5 233.5 234.5 235.5 236.5 237.5 238.5 239.5 [241] 240.5 241.5 242.5 243.5 244.5 245.5 246.5 247.5 248.5 249.5 250.5 251.5 [253] 252.5 253.5 254.5 255.5 256.5 257.5 258.5 259.5 260.5 261.5 262.5 263.5 [265] 264.5 265.5 266.5 267.5 268.5 269.5 270.5 271.5 272.5 273.5 274.5 275.5 [277] 276.5 277.5 278.5 279.5 280.5 281.5 282.5 283.5 284.5 285.5 286.5 287.5 [289] 288.5 289.5 290.5 291.5 292.5 293.5 294.5 295.5 296.5 297.5 298.5 299.5 [301] 300.5 301.5 302.5 303.5 304.5 305.5 306.5 307.5 308.5 309.5 310.5 311.5 [313] 312.5 313.5 314.5 315.5 316.5 317.5 318.5 319.5 320.5 321.5 322.5 323.5 [325] 324.5 325.5 326.5 327.5 328.5 329.5 330.5 331.5 332.5 333.5 334.5 335.5 [337] 336.5 337.5 338.5 339.5 340.5 341.5 342.5 343.5 344.5 345.5 346.5 347.5 [349] 348.5 349.5 350.5 351.5 352.5 353.5 354.5 355.5 356.5 357.5 358.5 359.5 [1] " Unlimited: FALSE" [1] "Here is information about dimension number 2 :" [1] " Name : Lat" [1] " Units : degreesN" [1] " Length: 179" [1] " Values:" [1] -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 -74 -73 -72 [19] -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 [37] -53 -52 -51 -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 [55] -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 [73] -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 [91] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [109] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [127] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [145] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 [163] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 [1] " Unlimited: FALSE" [1] "Here is information about dimension number 3 :" [1] " Name : Time" [1] " Units : days since 1900-01-01" [1] " Length: 10" [1] " Values:" [1] 1 2 3 4 5 6 7 8 9 10 [1] " Unlimited: TRUE" > > > > cleanEx(); ..nameEx <- "enddef.ncdf" > > ### * enddef.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: enddef.ncdf > ### Title: Takes a netCDF file out of define mode > ### Aliases: enddef.ncdf > ### Keywords: utilities > > ### ** Examples > > # This function is for advanced useage only, and will never > # be needed by the typical user's R code. > > > > cleanEx(); ..nameEx <- "get.var.ncdf" > > ### * get.var.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: get.var.ncdf > ### Title: Read data from a netCDF file > ### Aliases: get.var.ncdf > ### Keywords: utilities > > ### ** Examples > > # Start with the simplest example. If the file only has one variable in it, > # you can read the data as easily as this: > # > nc <- open.ncdf("salinity.nc") > # NOTE how not specifying 'varid' reads the "only" var in the file > data <- get.var.ncdf( nc ) > close.ncdf(nc) [[1]] [1] 8 > > # In this next example we read values from file "writevals.nc", which is created by > # the R code in the example section for function "put.var.ncdf". We open the > # file with readunlim=FALSE for potentially faster access, and to illustrate > # (below) how to read in the unlimited dimension's values. > # > nc <- open.ncdf( "writevals.nc", readunlim=FALSE ) > > print(paste("The file has",nc$nvars,"variables")) [1] "The file has 3 variables" > > # This illustrates how to read all the data from a variable > v1 <- nc$var[[1]] > data1 <- get.var.ncdf( nc, v1 ) # by default, reads ALL the data > print(paste("Data for var ",v1$name,":",sep="")) [1] "Data for var var1d:" > print(data1) [1] 0.2655087 0.3721239 27.5000000 > > # This shows how the shape of the read data is preserved > v2 <- nc$var[[2]] > data2 <- get.var.ncdf( nc, v2 ) > print(paste("Var 2 has name",v2$name,"and is of shape",dim(data2), + ". Here are the values:")) [1] "Var 2 has name var2d and is of shape 3 . Here are the values:" [2] "Var 2 has name var2d and is of shape 4 . Here are the values:" > print(data2) [,1] [,2] [,3] [,4] [1,] 0.9082078 0.2655087 0.2655087 0.6870229 [2,] 0.2016819 0.3721239 0.3721239 0.3841037 [3,] 0.8983897 0.5728534 0.5728534 0.7698414 > > # This illustrates how to read data one timestep at a time. In this > # example we will elaborately show how to deal with a variable whose > # shape is completely unknown (i.e., how many dimensions, and what their > # sizes are). We will also, for illustration of a common case, show how > # to read in the values of the time dimension at each timestep. > v3 <- nc$var[[3]] > varsize <- v3$varsize > ndims <- v3$ndims > nt <- varsize[ndims] # Remember timelike dim is always the LAST dimension! > for( i in 1:nt ) { + # Initialize start and count to read one timestep of the variable. + start <- rep(1,ndims) # begin with start=(1,1,1,...,1) + start[ndims] <- i # change to start=(1,1,1,...,i) to read timestep i + count <- varsize # begin w/count=(nx,ny,nz,...,nt), reads entire var + count[ndims] <- 1 # change to count=(nx,ny,nz,...,1) to read 1 tstep + data3 <- get.var.ncdf( nc, v3, start=start, count=count ) + + # Now read in the value of the 'timelike' dimension + timeval <- get.var.ncdf( nc, v3$dim[[ndims]]$name, start=i, count=1 ) + + print(paste("Data for variable",v3$name,"at timestep",i, + " (time value=",timeval,v3$dim[[ndims]]$units,"):")) + print(data3) + } [1] "Data for variable var3d at timestep 1 (time value= 0.01 seconds ):" [,1] [,2] [,3] [,4] [1,] 0.9082078 0.9446753 0.06178627 0.6870229 [2,] 0.2016819 0.6607978 0.20597458 0.3841037 [3,] 0.8983897 0.6291140 0.17655675 0.7698414 [1] "Data for variable var3d at timestep 2 (time value= 0.02 seconds ):" [,1] [,2] [,3] [,4] [1,] 0.9082078 0.9446753 0.06178627 0.6870229 [2,] 0.2016819 0.6607978 0.20597458 0.3841037 [3,] 0.8983897 0.6291140 0.17655675 0.7698414 [1] "Data for variable var3d at timestep 3 (time value= 0.03 seconds ):" [,1] [,2] [,3] [,4] [1,] 0.9082078 0.9446753 0.06178627 0.6870229 [2,] 0.2016819 0.6607978 0.20597458 0.3841037 [3,] 0.8983897 0.6291140 0.17655675 0.7698414 [1] "Data for variable var3d at timestep 4 (time value= 0.04 seconds ):" [,1] [,2] [,3] [,4] [1,] 0.9082078 0.9446753 0.06178627 0.6870229 [2,] 0.2016819 0.6607978 0.20597458 0.3841037 [3,] 0.8983897 0.6291140 0.17655675 0.7698414 [1] "Data for variable var3d at timestep 5 (time value= 0.05 seconds ):" [,1] [,2] [,3] [,4] [1,] 0.9082078 0.9446753 0.06178627 0.6870229 [2,] 0.2016819 0.6607978 0.20597458 0.3841037 [3,] 0.8983897 0.6291140 0.17655675 0.7698414 > > close.ncdf(nc) [[1]] [1] 8 > > > > cleanEx(); ..nameEx <- "open.ncdf" > > ### * open.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: open.ncdf > ### Title: Open a netCDF File > ### Aliases: open.ncdf > ### Keywords: utilities > > ### ** Examples > > # Define an integer dimension > dimState <- dim.def.ncdf( "StateNo", "count", 1:50 ) > > # Make an integer variable. Note that an integer variable can have > # a double precision dimension, or vice versa; there is no fixed > # relationship between the precision of the dimension and that of the > # associated variable. We just make an integer variable here for > # illustration purposes. > varPop <- var.def.ncdf("Pop", "count", dimState, -1, + longname="Population", prec="integer") > > # Create a netCDF file with this variable > ncnew <- create.ncdf( "states_population.nc", varPop ) > > # Write some values to this variable on disk. > popAlabama <- 4447100 > put.var.ncdf( ncnew, varPop, popAlabama, start=1, count=1 ) > > # Add source info metadata to file > att.put.ncdf( ncnew, 0, 'source', 'Census 2000 from census bureau web site') [[1]] [1] 8 > > close.ncdf(ncnew) [[1]] [1] 8 > > # Now open the file and read its data > ncold <- open.ncdf("states_population.nc") > data <- get.var.ncdf(ncold) > print("here is the data in the file:") [1] "here is the data in the file:" > print(data) [1] 4447100 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [7] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [13] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [19] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [25] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [31] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [37] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [43] -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647 [49] -2147483647 -2147483647 > close.ncdf( ncold ) [[1]] [1] 8 > > > > cleanEx(); ..nameEx <- "print.ncdf" > > ### * print.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: print.ncdf > ### Title: Print Information About a netCDF File > ### Aliases: print.ncdf > ### Keywords: utilities > > ### ** Examples > > # Open a netCDF file, print information about it > nc <- open.ncdf( "salinity.nc" ) > print(nc) [1] "file salinity.nc has 3 dimensions:" [1] "Lon Size: 360" [1] "Lat Size: 179" [1] "Time Size: 10" [1] "------------------------" [1] "file salinity.nc has 1 variables:" [1] "float Salinity[Lon,Lat,Time] Longname:Salinity Missval:1.00000001504747e+30" > > > > cleanEx(); ..nameEx <- "redef.ncdf" > > ### * redef.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: redef.ncdf > ### Title: Puts a netCDF file back into define mode > ### Aliases: redef.ncdf > ### Keywords: utilities > > ### ** Examples > > # This function is for advanced useage only, and will never > # be needed by the typical user's R code. > > > > cleanEx(); ..nameEx <- "set.missval.ncdf" > > ### * set.missval.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: set.missval.ncdf > ### Title: Set the Missing Value Attribute For a netCDF Variable > ### Aliases: set.missval.ncdf > ### Keywords: utilities > > ### ** Examples > > # Make an example netCDF file with a given missing value. We will > # then change the missing value in the file using set.missval.ncdf. > > origMissVal <- -1. > dimX <- dim.def.ncdf( "X", "meters", 1:7 ) > varAlt <- var.def.ncdf( "Altitude", "km", dimX, origMissVal ) > ncnew <- create.ncdf( "transect.nc", varAlt ) > data <- c(10.,2.,NA,1.,7.,NA,8.) > put.var.ncdf( ncnew, varAlt, data ) > close.ncdf(ncnew) [[1]] [1] 9 > > # At this point, the actual data values in the netCDF > # file will be: 10 2 -1 1 7 -1 8 > # because the "NA" values were filled with the missing > # value, -1. Also, the missing_value attribute of variable > # "varAlt" will be equal to -1. > > # Now change the missing value to something else. Remember > # we have to open the file as writable to be able to change > # the missing value on disk! > > newMissVal <- 999.9 > nc <- open.ncdf( "transect.nc", write=TRUE ) > varname <- "Altitude" > data <- get.var.ncdf( nc, varname ) # data now has: 10., 2., NA, 1., 7., NA, 8. > print(data) [1] 10 2 NA 1 7 NA 8 > set.missval.ncdf( nc, varname, newMissVal ) [1] "just set missval for var with index= 1 and name= Altitude to -1" [[1]] [1] 9 > put.var.ncdf( nc, varname, data ) > close.ncdf(nc) [[1]] [1] 9 > > # Now, the actual data values in the netCDF file will be: > # 10 2 999.9 1 7 999.9 8 > # and the variable's "missing_value" attributre will be 999.9 > > # **NOTE** that we had to explicitly read in the data and write > # it out again in order for the on-disk missing values in the > # data array to change! The on-disk missing_value attribute for > # the variable is set automatically by this function, but it is > # up to you whether or not you want to read in all the file's > # data and change the values to the new missing value. > > > > cleanEx(); ..nameEx <- "sync.ncdf" > > ### * sync.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: sync.ncdf > ### Title: Synchronize (flush to disk) a netCDF File > ### Aliases: sync.ncdf > ### Keywords: utilities > > ### ** Examples > > # The time you would use the sync.ncdf function is when you have an unlimited > # dimension and are writing to the file timestep-by-timestep. Make a netCDF file > # that has an unlimited dimension for illustration. > nx <- 5 > ny <- 8 > dimx <- dim.def.ncdf( "X", "meters", 1:nx ) > dimy <- dim.def.ncdf( "Y", "meters", 1:ny ) > dimt <- dim.def.ncdf( "Time", "days since 1900-01-01", 0, unlim=TRUE ) > > vartemp <- var.def.ncdf( "Temperature", "degC", list(dimx,dimy,dimt), 1.e30 ) > nc <- create.ncdf( "temperature.nc", vartemp ) > > nt <- 10 # Imagine this is actually some very large number of timesteps > for( i in 1:nt ) { + # Long, slow computation to get the data ... for illustration, we just + # use the following: + data <- runif(nx*ny) + + # Write the data to this timestep + put.var.ncdf( nc, vartemp, data, start=c(1,1,i), count=c(nx,ny,1) ) + + # Write the time value for this timestep as well + timeval <- i*10 + put.var.ncdf( nc, dimt, timeval, start=i, count=1 ) + + # Flush this timestep's data to the file so we don't lose it + # if there is a crash or other problem + sync.ncdf( nc ) + } > > # Always remember to close the file when done!! > close.ncdf(nc) [[1]] [1] 9 > > > > cleanEx(); ..nameEx <- "var.def.ncdf" > > ### * var.def.ncdf > > flush(stderr()); flush(stdout()) > > ### Name: var.def.ncdf > ### Title: Define a netCDF Variable > ### Aliases: var.def.ncdf > ### Keywords: utilities > > ### ** Examples > > # Define an integer dimension > dimState <- dim.def.ncdf( "StateNo", "count", 1:50 ) > > # Make an integer variable. Note that an integer variable can have > # a double precision dimension, or vice versa; there is no fixed > # relationship between the precision of the dimension and that of the > # associated variable. We just make an integer variable here for > # illustration purposes. > varPop <- var.def.ncdf("Pop", "count", dimState, -1, + longname="Population", prec="integer") > > # Create a netCDF file with this variable > ncnew <- create.ncdf( "states_population.nc", varPop ) > > # Write some values to this variable on disk. > popAlabama <- 4447100 > put.var.ncdf( ncnew, varPop, popAlabama, start=1, count=1 ) > > # Add source info metadata to file > att.put.ncdf( ncnew, 0, 'source', 'Census 2000 from census bureau web site') [[1]] [1] 9 > > close.ncdf(ncnew) [[1]] [1] 9 > > # Now illustrate some manipulations of the var.ncdf object > filename <- "states_population.nc" > nc <- open.ncdf(filename) > print(paste("File",nc$filename,"contains",nc$nvars,"variables")) [1] "File states_population.nc contains 1 variables" > for( i in 1:nc$nvars ) { + v <- nc$var[[i]] + print(paste("Here is information on variable number",i)) + print(paste(" Name: ",v$name)) + print(paste(" Units:",v$units)) + print(paste(" Missing value:",v$missval)) + print(paste(" # dimensions :",v$ndims)) + print(paste(" Variable size:",v$varsize)) + } [1] "Here is information on variable number 1" [1] " Name: Pop" [1] " Units: count" [1] " Missing value: -1" [1] " # dimensions : 1" [1] " Variable size: 50" > > # Illustrate creating variables of various types. You will find > # that the type of the missing_value attribute automatically follows > # the type of the variable. > dimt <- dim.def.ncdf( "Time", "days", 1:3 ) > missval <- -1 > varShort <- var.def.ncdf( "varShort", "meters", dimt, missval, prec="short") > varInt <- var.def.ncdf( "varInt", "meters", dimt, missval, prec="integer") > varFloat <- var.def.ncdf( "varFloat", "meters", dimt, missval, prec="single") > varDouble<- var.def.ncdf( "varDouble","meters", dimt, missval, prec="double") > nctypes <- create.ncdf("vartypes.nc", list(varShort,varInt,varFloat,varDouble) ) > close.ncdf(nctypes) [[1]] [1] 10 > > > > ### *