I´d like to create a netcdf file containing a mean and stddev for some variables for 12 months.

At this moment each geotiff (270MB) is either a mean or stddev of a month.

Now my problem is that I have no idea how to proceed from here, I have read plenty of documents about how to handle netcdf (How to extract, adapt and convert to other formats).

Is there a way to either use gdal or tools like ncdump to create a netcdf base file and then add each geotiff with extra metadata (variable, month and stddev or mean)? Or should the geotiff already have this metadata before using gdal to create a netcdf?

Here something similar is being done but I am not sure where in the python code the tiffs are imported and how each tiff is set to a certain date (My python skills are not up to par).


I figured out the answer some time ago but I hope this will help other people that are dipping their toes into the NetCDF file format.

I found that the easiest way to create a new NetCDF file containing several GeoTIFF files is using GDAL and then using NCO and CDO tools (Provided by unidata) to add the appropriate metadata.

First you convert the GeoTIFF´s to NetCDF using:

gdal_translate -of netCDF -co "FOMRAT=NC4" foo.tif foo.nc

This creates a netCDF in NC4 format (which is the newest version at this time) with some basic metadata like coordinate system, pixel size etc.

Then I rename the variable information to what I want it to be (In my case SST) with the appropriately registered long and short name From the unidata convention page):

ncrename -v Band1,SST foo.nc
ncatted -O -a long_name,SST,o,c,sea_surface_temperature foo.nc

The first renames the variable Band1 to the wanted variable SST. The second adds the long name of the SST variable.

In my case I added time data to the new nc file using:

ncap2 -Oh -s "tin=$d;" -S mean.nco foo.nc new_foo.nc

where the mean.nco file looks as follows:

/***.nco ncap2 script***/
time@units="days since 2000-00-00 00:00:00";
/***SST_M@long_name="Sea surface temperature mean";

This adds the third coordinate time where T=$d, which in my case was the january or 1. Then this time is set to the file using:

cdo settime,$d new_foo.nc newer_foo.nc

Finally all the files converted to NetCDF are joined together using:

cdo mergetime folder_containing_all_nc/*.nc final.nc

This joins all files with the appropriate timestamp to one file. This can be with several parameters in one file.

Hope this helps someone.

Greetings, Frans

  • Just to clarify, this process creates a series of individual nc files (in this case for Jan 1 2000, one for each date, and then joins them together while maintaining the metadata
    – mmann1123
    Aug 13 '19 at 17:39
  • Great explanation. Using it right now! Just beware of the typo in gdal_translate command. Its FORMAT and not FOMRAT
    – Daniel
    May 6 '20 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.