![]() |
NetCDF 4.9.3
|
The netcdf-c library uses an internal dispatch mechanism as the means for wrapping the netcdf-c API around a wide variety of underlying storage and stream data formats. As of last check, the following formats are supported and each has its own dispatch table.
Warning: some of the listed function signatures may be out of date and the specific code should be consulted to see the actual parameters.
Format | Directory | NC_FORMATX Name |
---|---|---|
NetCDF-classic | libsrc | NC_FORMATX_NC3 |
NetCDF-enhanced | libhdf5 | NC_FORMATX_NC_HDF5 |
HDF4 | libhdf4 | NC_FORMATX_NC_HDF4 |
PNetCDF | libsrcp | NC_FORMATX_PNETCDF |
DAP2 | libdap2 | NC_FORMATX_DAP2 |
DAP4 | libdap4 | NC_FORMATX_DAP4 |
UDF0 | N.A. | NC_FORMATX_UDF0 |
UDF1 | N.A. | NC_FORMATX_UDF1 |
NCZarr | libnczarr | NC_FORMATX_NCZARR |
Note that UDF0 and UDF1 allow for user-defined dispatch tables to be implemented.
The idea is that when a user opens or creates a netcdf file, a specific dispatch table is chosen. A dispatch table is a struct containing an entry for (almost) every function in the netcdf-c API. During execution, netcdf API calls are channeled through that dispatch table to the appropriate function for implementing that API call. The functions in the dispatch table are not quite the same as those defined in netcdf.h. For simplicity and compactness, some netcdf.h API calls are mapped to the same dispatch table function. In addition to the functions, the first entry in the table defines the model that this dispatch table implements. It will be one of the NC_FORMATX_XXX values. The second entry in the table is the version of the dispatch table. The rule is that previous entries may not be removed, but new entries may be added, and adding new entries increases the version number.
The dispatch table represents a distillation of the netcdf API down to a minimal set of internal operations. The format of the dispatch table is defined in the file libdispatch/ncdispatch.h. Every new dispatch table must define this minimal set of operations.
In order to make this process concrete, let us assume we plan to add an in-memory implementation of netcdf-3.
Define a –-enable flag option for configure.ac. For our example, we assume the option "--enable-ncm" and the internal corresponding flag "enable_ncm". If you examine the existing configure.ac and see how, for example, –enable_dap2 is defined, then it should be clear how to do it for your code.
Choose some prefix of characters to identify the new dispatch system. In effect we are defining a name-space. For our in-memory system, we will choose "NCM" and "ncm". NCM is used for non-static procedures to be entered into the dispatch table and ncm for all other non-static procedures. Note that the chosen prefix should probably start with "nc" or "NC" in order to avoid name conflicts outside the netcdf-c library.
Modify the file include/netcdf.h to add an NC_FORMATX_XXX flag by adding a flag for this dispatch format at the appropriate places.
Add any format specific new error codes.
Modify the file include/ncdispatch.h to add format specific data and initialization functions; note the use of our NCM namespace.
Define the functions necessary to fill in the dispatch table. As a rule, we assume that a new directory is defined, libsrcm, say. Within this directory, we need to define Makefile.am and CMakeLists.txt. We also need to define the source files containing the dispatch table and the functions to be placed in the dispatch table -– call them ncmdispatch.c and ncmdispatch.h. Look at libsrc/nc3dispatch.[ch] or libnczarr/zdispatch.[ch] for examples.
Similarly, it is best to take existing Makefile.am and CMakeLists.txt files (from libsrcp for example) and modify them.
Provide for the inclusion of this library in the final libnetcdf library. This is accomplished by modifying liblib/Makefile.am by adding something like the following.
Modify the NC_initialize function in liblib/nc_initialize.c by adding appropriate references to the NCM dispatch function.
Finalization is handled in an analogous fashion.
Typically, tests for a new dispatcher are kept in a separate directory with a related name. For our running example, it might be ncm_test. The file ncm_test/Makefile.am will look something like this.
Provide for libnetcdfm to be constructed by adding the following to the top-level Makefile.am.
The dispatch table is ultimately chosen by the function NC_infermodel() in libdispatch/dinfermodel.c. This function is invoked by the NC_create and the NC_open procedures. This can be, unfortunately, a complex process. The detailed operation of NC_infermodel() is defined in the companion document in docs/dinternal.md.
In any case, the choice of dispatch table is currently based on the following pieces of information.
The NC_infermodel function returns two values.
The entries in the dispatch table do not necessarily correspond to the external API. In many cases, multiple related API functions are merged into a single dispatch table entry.
The create table entry and the open table entry in the dispatch table have the following signatures respectively.
The key difference is that these are the union of all the possible create/open signatures from the include/netcdfXXX.h files. Note especially the last three parameters. The parameters argument is a pointer to arbitrary data to provide extra info to the dispatcher. The table argument is included in case the create function (e.g. NCM_create_) needs to invoke other dispatch functions. The very last argument, ncp, is a pointer to an NC instance. The raw NC instance will have been created by *libdispatch/dfile.c and is passed to e.g. open with the expectation that it will be filled in by the dispatch open function.
Most of the parameters are similar to the netcdf API parameters. The last parameter, however, is the type of the data in memory. Additionally, instead of using an "int islong" parameter, the memtype will be either NC_INT or NC_INT64, depending on the value of sizeof(long). This means that even netcdf-3 code must be prepared to encounter the NC_INT64 type.
Again, the key difference is the memtype parameter. As with put/get_vara, it used NC_INT64 to encode the long case.
It is sometimes not necessary to implement all the functions in the dispatch table. Some pre-defined functions are available which may be used in many cases.
Many of The netCDF inquiry functions operate from an in-memory model of metadata. Once a file is opened, or a file is created, this in-memory metadata model is kept up to date. Consequenty the inquiry functions do not depend on the dispatch layer code. These functions can be used by all dispatch layers which use the internal netCDF enhanced data model.
The mapped (varm) get/put functions have been implemented in terms of the array (vara) functions. So dispatch layers need only implement the vara functions, and can use the following functions to get the and varm functions:
For the netcdf-3 format, the strided functions (nc_get/put_vars) are similarly implemented in terms of the vara functions. So the following convenience functions are available.
For the netcdf-4 format, the vars functions actually exist, so the default vars functions are not used.
Some dispatch layers are read-only (ex. HDF4). Any function which writes to a file, including nc_create(), needs to return error code NC_EPERM. The following read-only functions are available so that these don't have to be re-implemented in each read-only dispatch layer:
There are two functions that are only used in the classic code. All other dispatch layers (except PnetCDF) return error NC_ENOTNC3 for these functions. The following functions are provided for this purpose:
The HDF4 dispatch layer is about the simplest possible dispatch layer. It is read-only, classic model. It will serve as a nice, simple example of a dispatch layer.
Note that the HDF4 layer is optional in the netCDF build. Not all users will have HDF4 installed, and those users will not build with the HDF4 dispatch layer enabled. For this reason HDF4 code is guarded as follows.
Code in libhdf4 is only compiled if HDF4 is turned on in the build.
Adding the HDF4 dispatch table will first require changes to a number of header files.
In the main netcdf.h file, we add the following to the list of NC_FORMATX_XXX definitions
In ncdispatch.h we add the following:
The netcdf_meta.h file allows for easy determination of what features are in use. For HDF4, the following is added – as set by ./configure:
The file hdf4dispatch.h contains prototypes and macro definitions used within the HDF4 code in libhdf4. This include file should not be used anywhere except in libhdf4. It can be kept in either the include directory or (preferably) the libhdf4 directory.
The file nc_initialize.c is modified to include the following:
In order for a dispatch layer to be used, it must be correctly determined in functions NC_open() or NC_create() in libdispatch/dfile.c. HDF4 has a magic number that is detected in NC_interpret_magic_number(), which allows NC_open to automatically detect an HDF4 file.
Once HDF4 is detected, the model variable is set to NC_FORMATX_NC_HDF4, and later this is used in a case statement:
This sets the dispatcher to the HDF4 dispatcher, which is defined in the libhdf4 directory.
The file hdf4dispatch.c contains the definition of the HDF4 dispatch table. It looks like this:
Note that most functions use some of the predefined dispatch functions. Functions that start with NC_RO* are read-only, they return NC_EPERM. Functions that start with NOTNC4* return NC_ENOTNC4.
Only the functions that start with NC_HDF4* need to be implemented for the HDF4 dispatch layer. There are 6 such functions:
The code in hdf4file.c opens the HDF4 SD dataset, and reads the metadata. This metadata is stored in the netCDF internal metadata model, allowing the inq functions to work.
The code in hdf4var.c does an nc_get_vara() on the HDF4 SD dataset. This is all that is needed for all the nc_get_* functions to work.
When new entries are added to the struct NC_Dispatch type located in include/netcdf_dispatch.h.in
it is necessary to do two things.
Modifying the dispatch version requires two steps:
The two should agree in value.
When dynamically adding a dispatch table – in nc_def_user_format (see libdispatch/dfile.c) – the version of the new table is compared with that of the built-in NC_DISPATCH_VERSION; if they differ, then an error is returned from that function.
As mentioned above, the dispatch table is inferred using the following information:
The primary function for doing this inference is in the file libdispatch/dinfermodel.c via the API in include/ncmodel.h. The term model is used here to include (at least) the following information (see the structure type NCmodel in include/ncmodel.h).
The construction of the model is primarily carried out by the function NC*infermodel() (in libdispatch/dinfermodel.c). It is given the following parameters:
As a rule, these values are used in the this order to infer the model.
If the path appears to be a URL, then it is parsed. Information is extracted from the URL, and specifically, the fragment key "mode=" is the critical element. The URL will be rewritten to a canonical form with the following changes.
The final result is the canonical form of the URL and is returned in the newpathp argument described above.
The mode list then is used as part of the inference process to choose a dispatch table.
Author: Dennis Heimbigner
Email: dmh at ucar dot edu
Initial Version: 12/22/2021
Last Revised: 11/15/2022