Reading a NetCDF file with Matlab and R

Language

MATLAB

file_name = '/matlab/R2012a/toolbox/matlab/demos/example.nc';
var_name = 'temperature';
group_name = '/grid2/';
tmp = var_name;  % or tmp = group_name
 
% Display global information about a netcdf file
ncdisp(file_name);
% About a variable or a group
ncdisp(file_name, tmp);
% In the specified format 'full' or 'min'
ncdisp(file_name, tmp, 'min');    

% Get information about a netcdf file info = ncinfo(file_name) % About a variable var_info = ncinfo(file_name, var_name) % About a group gr_info = ncinfo(file_name, group_name)     % Read data from a vraiable of a netcdf file data = ncread(file_name, var_name); % Or % data = ncread(file_name, var_name, start, count, stride)     % Read attribute attr = ncreadatt(file_name, '/', 'creation_date') var_attr = ncreadatt(file_name, var_name, 'add_offset') gr_attr = ncreadatt(file_name, group_name, 'description')

R

#Intall the NetCDF libraries: sudo apt-get install libnetcdf-dev 
#Install the ncdf package in R: install.packages("ncdf")
 
library("ncdf")
 
filename = "/home/ndiaye/Projets/R/example.nc"
var_name = "temperature"
attr_name = "units"
 
# Open a netcdf file
fid <- open.ncdf(filename, write=FALSE)
 
# Get a netcdf file info
print(fid)
 
# Get the variable identifier
var_id <- varid.inq.ncdf(fid, var_name)
print(var_id)
 
# Get the name of a variable id 
my_var_name <- varname.inq.ncdf(fid, var_id)
print(my_var_name)
 
# Get a file attribute
attr <- att.get.ncdf(fid, 0,"creation_date")
print(attr)
# Get a variable attribute attr_value <- att.get.ncdf(fid, var_name, attr_name) print(attr_value)   # Get the size of a variable size <- varsize.ncdf(fid, var_id) print (size)   # Get dimensions of a variable ndims <- varndims.ncdf(fid, var_id) print (ndims)   # Read data from a netcdf file data <- get.var.ncdf(fid, var_name) print(data)   # Get the variable objet var_object <- var.inq.ncdf(fid,var_id ) print(var_object)   # Get the name from the variable object name <- var_object$name print (name)   # Get variable object units n_attr <- var_object[["units"]] print(n_attr)   # Close a netcdf file close.ncdf(fid)

More news

Highlights

AERIS/ICARE was migrated his good old FTP server to SFTP

For security reason, we are abandoning the FTP protocol in favor of SFTP on our distribution server. Depending of the way you are using this service, you can have to change the commands you are used to. Note that not all applications support the SFTP protocol, and some additional tools may need to be installed […]

01.03.2024

Tutorials

How to convert a matplotlib figure to a numpy array or a PIL image

Language/Format: Python
Description: For manipulating a figure build with matplotlib, it is sometimes requested to convert it in a format understandable by other python libraries. This can be useful for using scipy image filters or manually adding annotations for example.
This page details how to convert a matplotlib figure to a numpy 3D array of RGBA values, or directly to a PIL ( Python Imaging Library ) Image.
Author(s): Nicolas Pascal (ICARE)

10.02.2017

Tutorials

Writing an HDF file with C, FORTRAN, Python, MATLAB and R

Language/Format: Fortran,MATLAB,Python
Description: This page gives pieces of code to write data in an HDF4 file
Author(s): Nicolas PASCAL, Nicolas THOMAS, Aminata NDIAYE (CARE )

15.10.2014

Search