NASA Logo, National Aeronautics and Space Administration
SPDF

CDF (V3.9.1 version) Patch for MATLAB


Mathworks MATLAB supports CDF reading and writing with two groups of modules, cdflib and cdfread/cdfinfo/cdfwrite. The module cdflib enables creating/reading/writing portions of CDF variables, while cdfread/cdfwrite reads/writes whole variables. Each release of MATLAB is based on a particular version of CDF; for instance MATLAB version 2018a is based on CDF V3.6.1. While this CDF version includes the newer types: CDF_INT8 and CDF_TIME_TT2000, however, the MATLAB modules still do not support them.

The CDF patch provides modified and expanded capabilities of the original cdfread/cdfinfo/cdfwrite from MATLAB.

Times in CDF are generally stored in one of three forms: CDF_EPOCH (8-byte floating milliseconds from 0AD), CDF_EPOCH16 (two 8-byte floats of seconds from 0AD and picoseconds of that second), and CDF_TIME_TT2000 (8 byte integer of nanoseconds from 2000 AD). SPDF's spdfcdfread can handle all time types, and optionally can convert their values to MATLAB's datenum. SPDF's spdfcdfread also provides cdfepoch and cdftt2000 objects (more accurate but not as efficient as MATLAB's datenum).

If you get error messages reading a CDF file while using MATALB distributed CDF package/modules, we strongly encourage you to try the patch from this page. The patch includes many user suggested enhancements and features. This patch works only for MATLAB version R2007a and later. .

NOTE: Use MATLAB's cdflib.getLibraryVersion function to its CDF's library version. From this patch, you do this: spdfcdfinfo() to show the library version this patch is based.

Please note: MATLAB's CDFLIB package was developed and is maintained sorely by MATLAB.

What's Included in the Patch

  • All SPDF-distributed modules are now renamed with a prefix of 'SPDF', e.g., SPDFCDFREAD, SPDFCDFWRITE, etc. Each module still keeps its original functionality and may have a few new enhancements/features.
  • Extra information is being collected by the SPDFCDFINFO. This new information will make it easier to create new CDF files, based on a template as the 'MASTER' CDF to provide the file settings, and variable and attributes specifications. SPDFCDFWRITE is enhanced to use the information for the CDF write.
  • SPDFCDFREAD is modified to have: 'CombineRecords', true as the default, due to its greatly improved performance and smaller returned data size.
  • A complete set of epoch data handling modules were added. These modules can encode, parse, compute, and break down the epoch data, in CDF_EPOCH, CDF_EPOCH16, and TT2000 format. These modules are:
    spdfbreakdownepoch spdfcomputeepoch spdfepochtodatenum
    spdfbreakdownepoch16 spdfcomputeepoch16 spdfepoch16todatenum
    spdfbreakdowntt2000 spdfcomputett2000spdftt2000todatenum
    spdfdatenumtoepoch spdfparseepoch spdfencodeepoch
    spdfdatenumtoepoch16spdfparseepoch16 spdfencodeepoch16
    spdfdatenumtott2000 spdfparsett2000 spdfencodett2000
    spdfcdfleapsecondsinfo spdfepochunixtime spdfepoch16unixtime
    spdftt2000unixtime

    Use "help" for their information.

Download and Installation Instructions

This latest patch is based on CDF Version 3.9.1 with the module (.m) patched version 3.9.1. You can enter "spdfcdfinfo()" within MATLAB to view the versions.

Windows:

  1. Download one of following self-extracting files (likely the 64-bit version) built from MS Visual Studio 2015: The shared library included has the latest leap second table embedded.
  2. Optionally, download the leap second table. Do this only if you intend to use the previous MATLAB patch.
  3. Double-click the downloaded file to extract the files included in the patch to a directory. The files will be extracted into c:\matlab_cdf391_patch by default.
  4. If you have downloaded the leap second table, then set the environment variable CDF_LEAPSECONDSTABLE to the table.
    set CDF_LEAPSECONDSTABLE='<patch_install_dir>\CDFLeapSeconds.txt'

    This table will be used, instead of the hard-coded one embedded in the released library. Not setting the environment variable will result in the use of the table from the library. The step is necessary when the leap second table is different from the one in the library. Need to set the environment variable before running CDF modules under MATLAB.
  5. Invoke MATLAB and issue the following command at the MATLAB prompt:

    addpath '<patch_install_dir>'

    For example, if the patch is installed under C:\matlab_cdf391_patch, the command should be

    addpath 'C:\matlab_cdf391_patch'

  6. Use MATLAB as usual: e.g.,
    spdfcdfinfo();
    data=spdfcdfread('test.cdf');

  7. Check the leap second table being used (relevant only to TT2000 time):
    spdfcdfleapsecondsinfo('DUMP', false)

Mac OS X:

  1. Download the following compressed tar files (64-bit mode):
    • Mac OS X Intel-based: Patch.
    • Mac OS X Apple Silicon (M*)-based:
      • MATLAB released version before R2023b: Patch.
      • MATLAB released version R2023b and later: Patch.
  2. Optionally, download the leap second table. Do this only if you intend to use the previous MATLAB patch.
  3. Move the MATLAB-CDF-Patch directory in the downloaded file to the directory you want the patch to be installed under.
  4. Extract the files in the patch using the following command:

    tar zxvf <downloaded compressed tar file>

    [The files will be extracted into matlab_cdf391_patch-[32|64] directory, depending the tarfile being extracted.]
  5. If you have downloaded the leap second table, then set the environment variable CDF_LEAPSECONDSTABLE to the table:
    setenv CDF_LEAPSECONDSTABLE "<patch_install_dir>/CDFLeapSeconds.txt" (for c/tcsh shell)
    export CDF_LEAPSECONDSTABLE="<patch_install_dir>/CDFLeapSeconds.txt" (for bash shell)

    This table will be used, instead of the hard-coded one embedded in the released library. Not setting the environment variable will result in the use of the table from the library. The step is necessary when the leap second table is different from the one in the library. Need to set the environment variable before running CDF modules under MATLAB.
  6. Invoke MATLAB and issue the following command at the MATLAB prompt:

    addpath '<patch_install_dir>'

    For example, if the 64-bit patch is installed under /Users/cdf, the command should be

    addpath '/Users/cdf/matlab_CDF391_Patch'

  7. Use MATLAB as usual: e.g.,
    spdfcdfinfo();
    data=spdfcdfread('test.cdf');

  8. Check the leap second table being used (relevant only to TT2000 time):
    spdfcdfleapsecondsinfo('DUMP', false)

Linux:

  1. Download one of the following compressed tar files (likely the 64-bit version):

  2. The shared library included has the latest leap second table embedded.
  3. Optionally, download the leap second table. Do this only if you intend to use the previous MATLAB patch
  4. Go to the directory where you want the patch to be installed under.
  5. Extract the files in the patch using the following command:

    tar zxvf <downloaded compressed tar file>

    [The files will be extracted into matlab_cdf391_patch-[32|64] directory, depending the tarfile being extracted.]
  6. If you have downloaded the leap second table, then set the environment variable CDF_LEAPSECONDSTABLE to the table:
    setenv CDF_LEAPSECONDSTABLE "<patch_install_dir>/CDFLeapSeconds.txt" (for c/tcsh shell)
    export CDF_LEAPSECONDSTABLE="<patch_install_dir>/CDFLeapSeconds.txt" (for bash shell)

    This table will be used, instead of the hard-coded one embedded in the released library. Not setting the environment variable will result in the use of the table from the library. The step is necessary when the leap second table is different from the one in the library. Need to set the environment variable before running CDF modules under MATLAB.
  7. Invoke MATLAB and issue the following command at the MATLAB prompt:

    addpath '<patch_install_dir>'

    For example, if the 64-bit patch is installed under /home/cdf, the command should be

    addpath '/home/cdf/matlab_cdf391_patch'

  8. Use MATLAB as usual: e.g.,
    spdfcdfinfo();
    data=spdfcdfread('test.cdf');

  9. Check the leap second table being used (relevant only to TT2000 time):
    spdfcdfleapsecondsinfo('DUMP', false)

If you have any questions or encounter problems, please send an email to cdfsupport and we'll respond as soon as possible.