# Define an integer dimension
dimState <- ncdim_def( "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 <- ncvar_def("Pop", "count", dimState, -1,
longname="Population", prec="integer")
# Create a netCDF file with this variable
ncnew <- nc_create( "states_population.nc", varPop )
# Write some values to this variable on disk.
popAlabama <- 4447100
ncvar_put( ncnew, varPop, popAlabama, start=1, count=1 )
# Add source info metadata to file
ncatt_put( ncnew, 0, "source", "Census 2000 from census bureau web site")
nc_close(ncnew)
# Now open the file and read its data
ncold <- nc_open("states_population.nc")
data <- ncvar_get(ncold)
print("here is the data in the file:")
print(data)
nc_close( ncold )
Run the code above in your browser using DataLab