Juggling NetCDF file in CDAT and NetCDF4-python

People working in the field of atmospheric science and climate change often have to deal with data stored in a netCDF file format.

NetCDF file has an extension .nc and is used as a container of gridded data of different dimensions, variables and their associated attributes. Usually netCDF classic format is used to store the climate data.

Here we will use two different packages of python to access a model output in netcdf file format and access its variables.

We will be using the file which you can download from the following link. CAM_run_2003_01_daily_data.nc

CDAT way

CDAT is a set of different python packages which together form a complete environment for climate data exploration and its visualization. It can access data in netCDF, HDF4 and grib data formats. For visualizing the results it includes packages such as VCS and matplotlib. To install CDAT using Conda run :
conda create -n cdat8 -c cdat/label/v80 -c conda-forge -c cdat python=2.7 cdat

#--- Import the cdms2 module
import cdms2 
#--- Load the netcdf file
f = cdms2.open('CAM_run_2003_01_daily_data.nc')
#--- Store the variable names in a list 
variable_names = f.listvariables()
print (variable_names)
## ['CLDTOT', 'CLOUD', 'P0', 'PS', 'hyam', 'hybm', 'time_bnds']
#--- Read the variable CLOUD 
CLOUD_data = f('CLOUD')
print (CLOUD_data.shape)
## (31, 30, 192, 288)

Here (31,30,192,288) is the shape of the data where the first dimension is time, second dimension is levels, third dimension is latitude and fourth dimension is longitude.

To know more about CDAT, follow the documentation


NetCDF4-python way

To install netcdf4-python using Conda run :
conda install -c conda-forge netcdf4

#--- Import the netCDF4 module
import netCDF4 
#--- Load the netcdf file
f = netCDF4.Dataset('CAM_run_2003_01_daily_data.nc')
#--- Store the variable names in a list 
variable_names = f.variables.keys() # variables in unicode
variable_names = [x.encode('UTF8') for x in variable_names] # encode to string
print (variable_names) 
## ['CLDTOT', 'CLOUD', 'P0', 'PS', 'hyam', 'hybm', 'lat', 'lev', 'lon', 'time', 'time_bnds']
#--- Read the variable CLOUD 
CLOUD_data = f.variables['CLOUD']
print (CLOUD_data.shape)
## (31, 30, 192, 288)

To know more about netcdf4-python, follow the documentation

Puneet Sharma
Research Scholar

My research interests include cloud & aerosol modeling and statistics.

comments powered by Disqus