[gdal-dev] NetCDF driver improvements (including groups support)

Julien Demaria Julien.Demaria at acri-st.fr
Wed Feb 11 03:24:15 PST 2015

Hi Mike,

Sorry, I’ve written “full support of NetCDF-4” but in fact NetCDF-4 compound types are not supported by my changes at this time, so NASA L3 binned products are not handled by GDAL.
NASA L3 binned products store pixels in vector of structures (compound types), with my changes GDAL can now list the subdatasets, but the compound types are still not handled and GDAL raise a warning and uses Float32 as default type, which is wrong, see the example below.
Ideally, we could wrap each field of a compound type as a separate GDAL band in the subdataset. But if we want support all cases we have also to handle compound types using other compound types, which is not easy technically, I think.

Note that NASA L3 mapped products are fully supported by GDAL (my changes only add capability to read metadata stored in specific groups, but main variables should be readable without my changes).

Example of NASA L3 binned product:

NetCDF representation of level-3_binned_data group:
group: level-3_binned_data {
    compound binListType {
      uint bin_num ;
      short nobs ;
      short nscenes ;
      float weights ;
      float time_rec ;
    }; // binListType
    compound binDataType {
      float sum ;
      float sum_squared ;
    }; // binDataType
    compound binIndexType {
      uint start_num ;
      uint begin ;
      uint extent ;
      uint max ;
    }; // binIndexType
                binListDim = UNLIMITED ; // (1671755 currently)
                binDataDim = UNLIMITED ; // (1671755 currently)
                binIndexDim = UNLIMITED ; // (4320 currently)
                binListType BinList(binListDim) ;
               binDataType chlor_a(binDataDim) ;
                binDataType chl_ocx(binDataDim) ;
                binIndexType BinIndex(binIndexDim) ;
  } // group level-3_binned_data

Corresponding GDAL Subdatasets:
  SUBDATASET_1_DESC=[1x1671755] /level-3_binned_data/BinList (x.�­)
  SUBDATASET_2_DESC=[1x1671755] /level-3_binned_data/chlor_a (x.�­)
  SUBDATASET_3_DESC=[1x1671755] /level-3_binned_data/chl_ocx (x.�­)
  SUBDATASET_4_DESC=[1x4320] /level-3_binned_data/BinIndex (x.�­)


De : Michael Sumner [mailto:mdsumner at gmail.com]
Envoyé : samedi 7 février 2015 02:03
À : Julien Demaria; gdal-dev at lists.osgeo.org
Objet : Re: [gdal-dev] NetCDF driver improvements (including groups support)

Hi, this is really interesting to me.
On Fri Feb 06 2015 at 03:11:02 Julien Demaria <Julien.Demaria at acri-st.fr<mailto:Julien.Demaria at acri-st.fr>> wrote:
Hi GDAL team,

I've implemented several improvements to the NetCDF driver and I would like to provide them to the community.
Main goal of the changes is to add full support of NetCDF-4 including groups.
NetCDF-4 is the future format of ESA Sentinel-3 products (no groups) and NASA Ocean Color team is switching their L2/L3 products to NetCDF-4 with groups (VIIRS has already switched to the new format in December).
With the changes NASA L2 products geolocation is automatically handled as geolocation arrays and can be reprojected using gdalwarp.

This sounds great, have you also worked with the L3 binned products? I have written my own suite of tools in R to read the NASA L3 bin files from the HDF4 files (using Rcpp), and have made some progress into the  NetCDF4 versions of these. Does your work on the GDAL driver include compound types like those in the L3 NetCDF4 bin files.  The structure is described here:


I prefer to work with the raw bin values usually rather than always apply a regridding - but having that capacity via GDAL would be great. I also wonder if it would be worth providing a vector driver for L3 bins, that would be my preferred usage ultimately - I need to  apply different algorithms for chlorophyll-a to the RRS values in the equal-area L3 bins, my R package is here, it's quite primitive and relies on the Rcpp route for C++,  but at least does what I need for now.


I'll try to have a closer look at your efforts here soon, but just wanted to reply now with my perspective.


Cheers, Mike.

I validated with autotests that nothing is broken in tests netcdf.py (excepting test 13 but see my point 5), netcdf_cf.py and hdf5.py, using NetCDF-3 and 4 libraries.
I've also tested the new functionalities on various NetCDF-4 files.
I think the only possible regression could be for marginal cases where a file was seen directly as a dataset and is now seen as multiple subdatasets (for example if a file has only one var in the top group and has nested groups containing variables), but I think this is not very common.

For the moment I have all these changes in local GIT separated commits on the latest gdal-1.11 branch, let me know what changes you want and how can I provide them.

Changes :

1) Implement full support for NetCDF-4 groups on reading:
    - explore recursively all nested groups to create the subdatasets list
    - subdatasets in nested groups use the /group1/group2/.../groupn/var standard
      NetCDF-4 convention, excepting for variables in the root group which do not
      have a leading slash for backward compatibility
    - when accessing a subdataset using NETCDF:$file:$path, the leading slash is optional
    - global attributes of each nested group are also collected in the GDAL dataset
      metadata, using the same convention /group1/group2/.../groupn/NC_GLOBAL#attr_name,
      excepting for the root group which do not have a leading slash for backward compatibility
    - when searching for a variable containing auxiliary information on the selected subdataset,
      like coordinate variables or grid_mapping, we now also search in parent groups (using NCDFResolveVar).
      I now this is something not specified at this time in the CF convention because CF does not know groups,
      but it seems logical to me to support this: NetCDF-4 specifies that dimensions of a group are
      shared to its nested groups, so associated coordinate variables could be defined as the same level of its
      corresponding dimension.
    - reference to coordinate variables using the "coordinates" attribute support now also absolute paths,
      this allow for example to specify coordinate variables located outside the group of the selected variable
      or its parents. Relative paths could be implemented if needed.
      This feature is used to add support for new NASA Ocean Color L2 products.

2) Implement full read/write support for new NetCDF4 types NC_UBYTE, NC_USHORT, NC_UINT and NC_STRING, only if NETCDF_HAS_NC4 is defined (and only if format=NC4 for writing).
    Support implemented for variables and attributes.
    NC_STRING type is supported for reading (scalar and arrays) attributes and is used for writing only for array attributes (scalar are still written as NC_CHAR).
    If NETCDF_HAS_NC4 is not defined or format!=NC4, NC_STRING array attributes are written as a single NC_CHAR string using the GDAL {v1,v2,...} convention.
    Add missing support for NC_BYTE in CreateBandMetadata() and NC_BYTE/SHORT in NCDFPut1DVar().

3) Add support for new NASA Ocean Color L2 products and ESA Sentinel-3 L1 or
    L2 products which use the NetCDF-4 format (with groups for NASA, see
    - NASA products: simulate a "coordinates" variable attribute to detect CF
      geolocation arrays, and set bBottomUp to FALSE
    - ESA products: set bBottomUp to FALSE and disable warning on missing
      Conventions attribute

4) Fix bug #4554 with a more generic solution by disabling the installation of the HDF5 atexit() cleanup routine using H5dont_atexit().
    Previous fix was to call GDALExit() (for the moment only defined gdalwarp.cpp) at the end of every program, which is more painful.

5) Fix implementation of GetScale/Offset to not always return pbSuccess=TRUE.
    Fix CopyMetadata to handle bands with only scale or offset.
    ==> WARNING this commit breaks the autotest netcdf_13 (check for scale/offset = 1.0/0.0 if no scale or offset is available), but for me it is not logical to return always pbSuccess=TRUE

6) Optimize IReadBlock() and CheckData() handling of partial blocks in the x axis by re-using the GDAL block buffer instead of allocating a new temporary buffer for each block.

7) Force block size to 1 scanline for bottom-up datasets if nBlockYSize != 1 instead of raising a fatal error
    ==> Solve a recent problem raised on the mailing list

8) Implement Get/SetUnitType using the standard "units" NetCDF attribute

9) Change default block size to 256x256 instead of scanline (only affect file without NetCDF chunking)
    ==> because I think this is better for a random access to the data, but I'm not sure if the community want this change which could impact performances

10) I've also implemented for my needs support for 1D variables by simulating 2D datasets with only one row (dimensionless variables are not supported for the moment),
       but this breaks backward compatibility because files containing only one variable and associated 1D coordinate variables are now seen as multiple sub datasets...
       and maybe this is not the goal of GDAL to give access to not-2D-raster variables (but sometimes it's useful ;-) )

Thanks for GDAL!


gdal-dev mailing list
gdal-dev at lists.osgeo.org<mailto:gdal-dev at lists.osgeo.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20150211/9543b6de/attachment-0001.html>

More information about the gdal-dev mailing list