Examining NetCDF Files in R

I am currently working for a non profit organisation, working on enhancing the interaction between the energy industry and the weather, climate and broader environmental sciences community.

Not coming from a climate science background, I had to become quickly accustomed to the terminology and technologies associated with this field of research. A commonly used file types produced to represent weather and climate data, are Network Common Data Files (aka NetCDF files).

NetCDF are multi-dimensional array files. This enables scenarios such as time series area measurements to be recorded, for that example you may have longitude, latitude and time as dimensions. They are also self describing,  including metadata to give relevant information and context.

So, if you’ve come across a .nc file and are wondering how to view it. First I would recommend downloading a free program called Panoply, which allows a quick way to view the array and plot the data.

Panoply

Once you’ve established you have some NetCDF data, you want to start having a closer look, right? In this example I am going to use R to examine the .nc file.

Firstly, install the R ncdf4 package:

 

Start a new R script and load your ncdf4 package:

 

Point R to your working directory (where you have your .nc files):

 

Load in your NetCDF file. There are some example files you can try at the unidata site here. I’m going to use the sea surface temperatures data:

 

The following nc_open function opens the NetCDF file. The arguments I’ve used are:

  • filename – The NetCDF file.
  • write=FALSE – loads it as read only.
  • readunlim=TRUE – NetCDF4 files support unlimited dimensions, which can be big in large data files. You can set this to false to ignore unlimited dimensions.
  • verbose=FALSE – Turns off messages printed out during this function.

 

Now your NetCDF is loaded in and ready, you can print information about a netCDF file, including the variables and dimensions it contains by doing the following:

This will print out quite a large amount of information

 

You may want to just see the attributes:

 

Or the following will print out a nice summary ( nabbed from this article ):

 

You may want to check how big your file is before loading it in:

 

Finally, close the NetCDF file using the nc_close function:

 

At some point, I will follow this up with some examples on how to plot NetCDF data using R and some nice packages like ggplot2.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.