20 Using NetCDF files

20.1 Learning Objectives

In this lesson, you will:

  • Learn to read data from a NetCDF file
  • Wrangle the example data into a data frame
  • Make some plots

20.2 Introduction

NetCDF files are hierarchical data files that contain embedded metadata and allow for efficient extraction of data. They are particularly useful for storing large data, such as raster data and model outputs.

This lesson draws from a previous lesson written by Leah Wasser, available here.

This R blog post also contains some good introduction material.

20.3 Reading in data

First let’s load the ncdf4 package

Let’s grab an example file. Download the .nc file from Fiamma Straneo. 2019. Temperature and salinity profiles adjacent to a tidewater glacier in Sarqardleq Fjord, West Greenland, collected during July 2013. Arctic Data Center. doi:10.18739/A2B853H78. http://doi.org/10.18739/A2B853H78

First we open a connection to our NetCDF file using nc_open.

You can print information about what is contained in the file using the print function on the nc object.

The netcdf file has a lot of information in the top level. You can navigate through the nc connection using the list selector operator. For example:

You can return the names of the variables by using the attributes function on the var element within the nc object.

Note that we haven’t read in any data yet - we have only read in all of the attributes, which are all of the different fields used to store metadata.

You can retrieve individual variables by calling ncvar_get, and the variable by name.

Note that if the file also has dimension variables, you can retrieve these values the same way as if they were variables.

Read in the depth dimension variable.

20.4 Reshaping the data into a data.frame

Depending on what your analysis goals are, you may want to convert your data into a data.frame structure. These data would work well in one since it is not a big dataset, and it is not gridded. Other dataset types, like gridded raster data, should be dealt with differently (such as using the raster package).

First, we might want to convert the MATLAB date-time number to a POSIXct number.

Next we coerce the salinity matrix, which is represented with rows according to time and columns according to depth, into a data frame,

We then assign column names to the character value of our depth vector.

And finally, we add the time column to our matrix, gather over the depth columns, and turn the depth column back to a numeric value,