[GIS] how to prepare tiffs to create a netcdf file

gdalnetcdf

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).

Best Answer

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***/
defdim("time",1);
time[time]=tin;
time@long_name="Time";
time@units="days since 2000-00-00 00:00:00";
time@standard_name="time";
time@axis="T";
time@coordinate_defines="point";
time@calendar="standard";
/***SST_M@long_name="Sea surface temperature mean";
SST_M@units="Celsius"
/***********/

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

Related Question