For the first time, I am using NetCDF data files containing daily data about soil moisture at a global level.

I want to be able to obtain monthly mean value of soil moisture for each grid cell.

There is one NetCDF file for each day, and each file contains four bands. I only need the first band which contains the data I want.

Using QGIS 1.8, I encounter the following problems:

1. Loading the rasters

When I want to load the 31 rasters related to January (example), I can select the 31 files, but then, I have to select the band I am interested in for each layer, one by one. Is there a way to load all of them, but automatically select the band I want?

enter image description here

2. Converting the layers

The data comes in a floating number format. I detected some issues when it comes to sum values. However, when I convert the layer to a .tiff raster, it seems to transform the value into an integer. This is what I want, but GDAL doesn't want to batch convert all the files into .tiff, either because of the file names of because of the multiband issue. Is there a way to batch convert NetCDF files into .tiff?

enter image description here

3. Batch summing the layers

Finally, even if I have the layer in .tiff format, I wonder if there is an easier way to sum all the values than by using RasterCalculator. Is there a tool than batch sums up rasters within a folder? Also, I noticed that I cannot use RasterCalculator directly on the NetCDF layers because of the file names. If the solution is on this trail, is there a way to batch change the name of a layer within a NetCDF file?


1. Loading the raster

Batch convert with gdal_translate the daily soil moisture NetCDF files into GDAL VRT format specifying the band you want to use. Example (band 1):

gdal_translate -of VRT -b 1 netcdf:inputfile.nc:variable_name outputfile.vrt

2. Converting the layers

Batch convert VRTs into GTiff format always with gdal_translate, if you really need it. Example:

gdal_translate -ot Int32 inputfile.vrt outputfile.tif

3. Batch summing the layers

Is there a tool than batch sums up rasters within a folder?

Use gdal_calc.py. Example:

gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"

Is there a way to batch change the name of a layer within a NetCDF file?

If you use the VRT format, you can name the layers as you want.

  • Thanks for the answer! I was exploring the gdal_translate function at this very moment. Now, I am able to choose the variable I want to convert, however, I don't know how to do it for all the files within a directory. For instance, if I have 31 NetCDF files in my folder, how can I ask gdal_translate to convert each file to VRT with only the variable I want? – Damien Oct 11 '13 at 14:39
  • You need a FOR cycle based on a variable: FOR /L %variable IN (start,step,end) DO command [command-parameters] – Antonio Falciano Oct 11 '13 at 15:01
  • Thanks for the tip. I must confess I'm a newbie in this kind of stuff. For what I understand, I should write the loop in a .bat file and execute it using the windows Shell. Am I right? Also, I read a bit in the doc, and I came out with this line of code: FOR /R C:\Users\ddeltenre\Desktop\Muddyareas\2005\1_January\ %inputfile IN (*.nc) DO gdal_translate -of VRT -b 1 netcdf:%inputfile.nc:sm %inputfile.vrt However, when I execute, I have error messages saying that some variables or strings are unexpected. Where am I wrong? – Damien Oct 15 '13 at 8:48
  • %inputfile is for use on command lines only. It should be substituted with %%inputfile when used in batch files. Then FOR /R loops through files in subfolders, are you sure? IMHO you should type FOR %%inputfile IN (C:\Users\ddeltenre\Desktop\Muddyareas\2005\1_January\*.nc) DO ... – Antonio Falciano Oct 15 '13 at 8:57
  • Ok, I adapted the command using %% and I replaced %%inputfile by %%g. Now, I think the syntax is correct because the program tries to do something. However, I obtain the following error: "gdal_translate is not recognized as an internal or external command operable program or batch file". How do I reach the GDAL functions that are installed within QGIS? – Damien Oct 15 '13 at 9:10

The NetCDF Operators (http://nco.sourceforge.net/nco.html) have the ncra or ncea programs which can average a set of your daily netcdf files into a monthly average.

ncra file201301*.nc mean201301.nc

ncwa can also sum or integrate across the a file http://nco.sourceforge.net/nco.html#Normalization-and-Integration but you might need to concatenate the dailys into a month to use it

ncrcat daily201301*.nc month201301.nc
ncwa -N month201301.nc sum201301.nc  # don't normalize the sum

You can change the layers/variables in the netcdf files with http://nco.sourceforge.net/nco.html#ncrename-netCDF-Renamer like:

ncrename -v badname,bettername myfilename.nc

The NetCDF operators are pretty fast tools for doing set-wise math on NetCDF files.

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.