Introduction
This article explains how to extract data from a 3 dimension NetCDF file using different options and save the output as a CSV (comma separated variables) file.
You are expected to have installed python 2.7 or later, and the CDS API on a Linux machine before you continue.
First option : Python Script
The first option is to use a python script (below). The script allows you to covert data from NetCDF in two different ways, as explained in the workflow below:
- Retrieve data with the CDS API and store as a netCDF4 file in the working directory.
- Extract the variable from the NetCDF file and get the dimensions (i.e. time, latitudes and longitudes)
- Extract each time as a 2D pandas DataFrame and write it to the CSV file
- Write the data as a table with 4 columns: time, latitude, longitude, value
Please note that the netCDF file could contain an additional dimension when there is a mix of ERA5 and ERA5T data. For more details please have a look at the following link: ERA5 CDS requests which return a mixture of ERA5 and ERA5T data
Second option : Panoply
A second option is to convert the data using the NASA 'Panoply' software. User can find the option under File → Export data → As CSV. The data are saved in the file maintaining the structure of the lot/lan matrix, but different times are divided by an empty row.
Third option : Windows users
A third option to convert the data from NetCDF to CSV, for Windows users, is download and install netcdf4excel. The plug-in opens directly NetCDF files in Microsoft Excel maintaining conventions for the NetCDF variables. Please see the link for more details: http://netcdf4excel.github.io/.
Other solutions
For Unix users, there are others options provided by some common NetCDF software packages. Please the links for more details:
- ncdump → http://manpages.ubuntu.com/manpages/xenial/man1/ncdump-hdf.1.html
- cdo → https://code.mpimet.mpg.de/projects/cdo/wiki/Tutorial
- ncks → https://www.unidata.ucar.edu/support/help/MailArchives/netcdf/msg08405.html
This document has been produced in the context of the Copernicus Atmosphere Monitoring Service (CAMS) and the Copernicus Climate Change Service (C3S).
The activities leading to these results have been contracted by the European Centre for Medium-Range Weather Forecasts, operator of CAMS and C3S on behalf of the European Union (Delegation Agreement signed on 11/11/2014). All information in this document is provided "as is" and no guarantee or warranty is given that the information is fit for any particular purpose.
The users thereof use the information at their sole risk and liability. For the avoidance of all doubt, the European Commission and the European Centre for Medium-Range Weather Forecasts have no liability in respect of this document, which is merely representing the author's view.