netcdfwrite <- function(lon,lat,data,filename="data.nc",time=1,mv=-999) { # Writes two-dimensional map or three-dimensional array of data, longitude vector, # latitude vector and time vector into a netcdf file. # # Description: # # Returns a netcdf file that contains a two-dimensional map or a # three-dimensional array of data, a longitude vector, a latitude vector # and a time vector # # Usage: # # netcdfwrite(lon,lat,data,filename,time,mv) # # Arguments: # # data: two-dimensional map with first dimension longitude and # second dimension latitude or three-dimensional array # with first dimension longitude, second dimension latitude and # third dimension time, containing the data to be written # to the netcdf file # lon: a vector containing the longitudes of data # lat: a vector containing the latitudes of data # time: a vector containing the times of data in hours # since 1900-1-1 00:00:0.0 . Default is 1, which is # appropriate for writing a two-dimensional map # filename: string with the name of the netcdf file to be created. # Default is "data.nc" # mv: Value attributed to missing data. Default is -999 # # Output: # # netcdf-file containing the data # # Author: # # Dag Johan Steinskog 5 January 2006 # Caio Coelho # # Example: # # x <- seq(-20, 20, 5) # y <- seq(30, 60, 5) # dims <- c(length(x), length(y), 100) # data <- array(rnorm(prod(dims)), dims) # time <- 1:100 # # write a two-dimensional map of data to the netcdf file # netcdfwrite(x,y,data[,,3],filename="data.nc") # # write a three-dimensional array of data to the netcdf file # netcdfwrite(x,y,data,filename="data.nc",time) if(length(time)==1){data<-array(data,c(dim(data)[1],dim(data)[2],1))} #create netcdf file #ncfile <- create.nc(filename,clobber) ncfile <- create.nc(filename) #give dimensions to variables dim.def.nc(ncfile,"lon",length(lon)) dim.def.nc(ncfile,"lat",length(lat)) dim.def.nc(ncfile,"time",length(time)) # create variables var.def.nc(ncfile,"lon","NC_FLOAT","lon") var.def.nc(ncfile,"lat","NC_FLOAT","lat") var.def.nc(ncfile,"time","NC_DOUBLE","time") var.def.nc(ncfile,"data","NC_FLOAT",c("lon","lat","time")) #var.def.nc(ncfile,"data","NC_DOUBLE",c("lon","lat")) # add data to variables var.put.nc(ncfile,"lat",lat) var.put.nc(ncfile,"lon",lon) var.put.nc(ncfile,"time",time) var.put.nc(ncfile,"data",data) # add description information to variables att.put.nc(ncfile,"data","missing_value","NC_FLOAT",mv) att.put.nc(ncfile,"lon","long_name","NC_FLOAT","Longitude") att.put.nc(ncfile,"lon","units","NC_FLOAT","degrees_east") att.put.nc(ncfile,"lon","axis","NC_FLOAT","X") att.put.nc(ncfile,"lat","long_name","NC_FLOAT","Latitude") att.put.nc(ncfile,"lat","units","NC_FLOAT","degrees_north") att.put.nc(ncfile,"lat","axis","NC_FLOAT","Y") att.put.nc(ncfile,"time","long_name","NC_DOUBLE","Time") att.put.nc(ncfile,"time","units","NC_DOUBLE","hours since 1900-1-1 00:00:0.0") att.put.nc(ncfile,"time","delta_t","NC_DOUBLE","0000-01-00 00:00:00") # syncronize and close the netcdf file sync.nc(ncfile) close.nc(ncfile) }