Reading a NetCDF file with Python, Matlab and R

Visualisation tools

There are several turn-key desktop tools designed to data and image visualization of a NetCDF file like:

  •  Panoply designed essentially for NetCDF files that use the Climate Forecast conventions metadata
  • HDFview : able to open all NetCDF (and HDF) files

Language

In the following examples, the NetCDF file downloadable here is used.

PYTHON

1. Using the netCDF4 library:

# First, you need to install the netCDF4 library if you haven’t already
# pip install netCDF4

import netCDF4 as nc

# Specify the file path
file_path = 'sresa1b_ncar_ccsm3-example.nc'

# Open the NetCDF file
ds = nc.Dataset(file_path)

# Print the file details
print(ds)

# Access a specific variable
tas = ds.variables['tas'][:]

# Check the dimensions and attributes of the variable
print(ds.variables['tas'].dimensions)
print(ds.variables['tas'].shape)
print(ds.variables['tas'].units)

# Close the dataset after using it
ds.close()

2. Using the xarray library:

# Install xarray and netCDF4 together if you haven’t done so
# pip install xarray netCDF4

import xarray as xr

# Open the NetCDF file using xarray
ds = xr.open_dataset('sresa1b_ncar_ccsm3-example.nc')

# Print the dataset details
print(ds)

# Access a specific variable
tas = ds['tas']

# Print the values of the lat variable
print(tas.values)

# You can also access dimensions and coordinates
print(ds.dims)
print(ds.coords)

# Close the dataset (optional, handled automatically with xarray)
ds.close()

MATLAB

file_name = 'sresa1b_ncar_ccsm3-example.nc';
    var_name = 'tas';
 
    % Display global information about a netcdf file
    ncdisp(file_name);
    % About a variable or a group
   ncdisp(file_name, var_name);
       

% 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 var_attr = ncreadatt(file_name, var_name, 'units')

R

#Intall the NetCDF libraries: sudo apt-get install libnetcdf-dev 
#Install the ncdf package in R: install.packages("ncdf4")
 
# Charger la bibliothèque ncdf4
library(ncdf4)

# Ouvrir le fichier NetCDF
nc_data <- nc_open("sresa1b_ncar_ccsm3-example.nc")

# Afficher les informations sur le fichier
print(nc_data)

# Lire une variable spécifique (par exemple, "tas")
tas <- ncvar_get(nc_data, "tas")

# Afficher les données de la variable
print(tas)

# Obtenir les attributs de la variable "tas"
tas_attributes <- ncatt_get(nc_data, "tas")
print(tas_attributes)

# Fermer le fichier NetCDF
nc_close(nc_data)

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