netcdfread<-function(file,lonname='longitude',latname='latitude',timename='time',dataname,unpack=F) { # # Extract longitude vector, latitude vector, time vector and # data array from a netcdf file. # # Description: # # Returns a vector of longitudes, a vector of latitudes, a vector of times # and an array of data from a netcdf file. # # Usage: # # netcdfread(file,lonname,latname,timename,dataname,unpack=F) # # Arguments: # # file: String containing the full path and netcdf file name # # lonname: String containing the name of the longitude variable. # Default name is "longitude". # # latname: String containing the name of the latitude variable # Default name is "latitude". # # timename: String containing the name of the time variable. # Default name is "time" # # dataname: String containing the name of the data variable # # unpack: Logical. If TRUE unpacks data contained in variable # dataname by performing the following transformation: # unpacked_data_value=packed_data_value*scale_factor+add_offset, # where, scale_factor and add_offset are constant netcdf attributes # of variable dataname. Default is FALSE. # # Output: # # $lonncfile: Vector of longitudes # $latncfile: Vector of latitudes # $timencfile: Vector of times # $datancfile: Array of data # # Author: # # Chris Ferro 9 June 2005 # Dag Johan Steinskog # Caio Coelho # # Examples: # # # First run the line below to extract the data structure/information # # so that you are able to know the names of variables # netcdfinfo("netcdf_file_name.nc") # # netcdfread("netcdf_file_name.nc","lon_name","lat_name","time_name","data_name") # netcdfread("/home/username/netcdf_file_name.nc","lon_name","lat_name","time_name","data_name") # # Note: The user should use a netcdf file to be able to run these examples # open a netcdf file ncfile <- open.nc(file) # get latitude and longitude lonncfileaux <- var.get.nc(ncfile, lonname) latncfileaux <- var.get.nc(ncfile, latname) # get data and time datancfile <- var.get.nc(ncfile, dataname) timencfile <- var.get.nc(ncfile, timename) if(unpack){ datancfile<-datancfile*att.get.nc(ncfile,dataname,"scale_factor")+att.get.nc(ncfile,dataname,"add_offset") } # sort latitude and longitude in ascending order lonncfile <- sort(lonncfileaux) latncfile <- sort(latncfileaux) # rearange data datancfile<-datancfile[order(lonncfileaux),order(latncfileaux),] invisible(list(lonncfile=lonncfile,latncfile=latncfile,timencfile=timencfile,datancfile=datancfile)) }