Table Of Contents

Previous topic

33. Overview Menus

Next topic

35. Overview Objects

This Page

34. Overview netCDF Files

If you import data into UniPlot, the data will be saved in a netCDF file. The standard extension is .nc. netCDF is a file format for measured data developed by the University Corporation for Atmospheric Research (see Copyright netCDF).

34.1. netCDF-up

A new alternative file format has been added for netCDF files (netCDF-up) with a number of advantages

Advantages of netCDF-up

  • If attributes or channels a added, modified or removed the complete file must be copied. This is not necessary for netCDF-up files. This results in much higher performance of these operations.
  • Stability: In netCDF nc_abort cannot be used to rewind modifications. If a number of changes are executed and one modification fails because of power failure, a network problem or a software bug the complete file is destroyed. For netCDF-up files all modifications can be undone since the file was opened since the last call of nc_sync.
  • Reduced file size because parts of the file are saved compressed. The following table compare the file size of three typical MDF/INCA files with netCDF- and netCDF-up files (all values in kBytes):
File Orginal (MDF/VS100) netCDF (Original) netCDF-up
File small 11 17 12
File medium 934 1426 453
File big 13727 33883 4271

Sometimes (for example pure random numbers) netCDF-up files can be bigger than netCDF fields. But this will be a rare case.

  • No Limitation in size - no 2 GBytes limit.
  • The next UniPlot version 6.0 will use the same file format used for netCDF-up files (see Overview UPDF Files). This makes it relatively simple to embed netCDF-up files in the IPW files of UniPlot 6.0. It will be possible to edit datasets using the standard browser.

34.2. Functions

  Tools
NC_CreateMeanCycleFile NC_CreateMeanCycleFile creates a new NC file with the mean, minimum and maximum cycle of an NC data file containing multiple cycles (segments).
NC_Edit NC_Edit opens a dialog box to edit a netCDF data file (NC file).
NC_ExportData NC_ExportData converts netCDF data files (.nc) into another data format.
NC_GetVarNames NC_GetVarNames returns the variable names of a netCDF data file (NC file).
NC_Interpolation :ref:`NC_Interpolation ` creates a new NC file with interpolated data. The function can be used to change the sampling rate (frequency resolution). MDF/Inca files with multiple time channels can be interpolated onto a common time channel.
NC_MapInterpolation NC_MapInterpolation calculates new channels by map interpolation and adds the new channels to an NC file.
nc_from_obj nc_from_obj creates a netCDF file from a special structured UniScript object.
nc_to_obj nc_to_obj reads a netCDF file or a selection of channels of a netCDF file into a UniScript object.
  Open and Close
nc_abort nc_abort closes or deletes a netCDF file.
nc_close nc_close closes an open netCDF file.
nc_create nc_create creates a new netCDF file.
nc_open nc_open opens an existing netCDF file.
nc_sync nc_sync writes all buffered data to the file.
  Global Functions
nc_copy nc_copy copies all variables and attributes of an NC file to a new NC file.
nc_diminq_size nc_diminq_size returns the size of a dimension when given its ID.
nc_endef nc_endef takes an open netCDF file out of define mode. The changes made to the netCDF file while it was in define mode are checked and committed to disk if no problems occurred. In particular, non-record variables are filled with their fill-values unless nc_setfill has been called with the argument NC_NOFILL. The netCDF file is then placed in data mode, so that variable data can be read or written.
nc_fatal_error nc_fatal_error.
nc_filename nc_filename returns the file name for a given ncid.
nc_get_option nc_get_option gets an option.
nc_inquire_format nc_inquire_format returns the file format for a given ncid.
nc_inquire_ndims nc_inquire_ndims returns the number of dimensions in a netCDF file.
nc_inquire_ngatts nc_inquire_ngatts returns the number of global attributes in a netCDF file.
nc_inquire_nvars nc_inquire_nvars returns the number of variables in a netCDF file.
nc_inquire_recdim nc_inquire_recdim returns the ID number of the record variable in a netCDF file.
nc_last_error nc_last_error returns the last error that occurred of netCDF-up files. If the last invoked nc_-function returned without an error the function will return 0.
nc_redef nc_redef puts an open netCDF file into define mode, so dimensions, variables, and attributes can be added or renamed and attributes can be deleted.
nc_set_option nc_set_option sets an option.
nc_seterror_options nc_seterror_options sets the error options for the netCDF functions.
nc_setfill nc_setfill determines whether or not variable prefilling will be done.
nc_str_error nc_str_error.
  Dimensions
nc_dimdef nc_dimdef adds a new dimension to an open netCDF file.
nc_dimid nc_dimid returns the ID of a netCDF dimension when given the name of the dimension.
nc_diminq_name nc_diminq_name returns the name of a dimension when given its ID.
nc_dimredim nc_dimredim modifies the size of a dimension. The netCDF file must be set to define mode (siehe nc_endef/nc_redef).
nc_dimrename nc_dimrename renames a given dimension.
  Variables
nc_makevalidname nc_makevalidname returns a valid netCDF variable name for a given name.
nc_varcopy nc_varcopy copies the data of a variable from one NC file to another NC file. The variable must exist in both files and must have the same number of points.
nc_vardef nc_vardef adds a new variable to an open netCDF file in define mode.
nc_vardelete nc_vardelete deletes a given variable. A variable can only be deleted if the file is not in definition mode.
nc_varget nc_varget reads data values from a netCDF variable of an open netCDF file. The file must be in data mode.
nc_varget_missing nc_varget_missing reads data values from a netCDF variable of an open netCDF file. The file must be in data mode.
nc_varid nc_varid returns the ID of a netCDF variable when given its name.
nc_varinq_changed nc_varinq_changed returns the change counter of a netCDF variable.
nc_varinq_datatype nc_varinq_datatype returns the data type of a variable when given its ID.
nc_varinq_dimids nc_varinq_dimids returns a vector of dimension IDs.
nc_varinq_name The nc_varinq_name function returns the name of a netCDF variable when given its ID.
nc_varinq_natts The nc_varinq_natts function returns the number of attributes for a variable, given its ID.
nc_varput The nc_varput functions writes data values into a netCDF variable of an open netCDF file. The file must be in data mode.
nc_varput_missing The nc_varput_missing functions writes data values into a netCDF variable of an open netCDF file. The file must be in data mode. Missing values must have the value MISSING_VALUE.
nc_varrename nc_varrename renames a given netCDF variable.
  Attributes
nc_attcopy nc_attcopy copies a given attribute into a different NC file.
nc_attdelete nc_attdelete deletes a given attribut. The file must be in define mode.
nc_attget nc_attget returns the value(s) of a netCDF attribute.
nc_attinq_datatype nc_attinq_datatype returns the data type of an attribute when given its ID and name.
nc_attinq_len nc_attinq_len returns the number of values stored in the attribute. If the attribute is of type NC_CHAR, the function returns the string length.
nc_attname nc_attname returns the attribute name.
nc_attput nc_attput changes a variable attribute or global attribute of an open nc file. If the attribute is new, or if the space required to store the attribute is greater than before, the nc file must be in define mode.
nc_attrename nc_attrename renames a given attribute.

34.3. netCDF-up

netCDF-up is a file format with an interface that is mostly compatible to the netCDF-Library.

The command

WriteProfileInt("Settings", "standard-netcdf-files", 2);

can be used to enable the netCDF-up interface. The command File=>Import Data will create netCDF-up files, if the new interface is enabled.

Attention: This is a beta version of the new interface. It may contain many bugs and should be used for test purposes only. The release version will be available with 6.0.

The following command will switch back to the standard netCDF library:

// Create standard netCDF files
WriteProfileInt("Settings", "standard-netcdf-files", 1);

or

// netCDF with Intel byte order (Default)
WriteProfileInt("Settings", "standard-netcdf-files", 0);

Advantages of netCDF-up

  • UniPlot 6.0 files (IPW) will be based on the same data format as the netCDF-up files (see Overview UPDF Files). Therefore netCDF-up files can be easily embedded into UniPlot files.
  • Adding and Deleting attributes or channels is much faster. For a netCDF file these functions mean that the complete file has to be copied. This is not necessary for netCDF-up.
  • Stability: In netCDF the nc_abort function can be used to undo changes to a data file. If the function fails, the file is damaged. For netCDF-up, changes can be undone without risk to damaging the file.
  • More compact files, because elements will be saved compressed.

34.4. Example

The following function creates a netCDF file. In this example the data is passed as a string matrix (smData).

def NC_WriteSimpleNCFile(ssFileName, ssNetCDFName, svChanName, svUnits, ...
            rvDataType, smData, ssCreator)
{
    nCols = nc(smData);
    nRows = nr(smData);
    ncid = nc_create(ssNetCDFName, NC_CLOBBER|0x10000);
    if (ncid == -1) {
        ssError = sprintf(_s("Cannot create file %s - Is file already open?");
        MessageBox(ssError, ssNetCDFName), "ICONSTOP");
        return "#IMPORTERROR#";
    }
    dimids = nc_dimdef(ncid, "nRows", nRows);
    // Globale Attribute:
    nc_attput(ncid, -1, "Origin", NC_CHAR, _StandardFileName(ssFileName));
    nc_attput(ncid, -1, "Source", NC_CHAR, "Uniplot Software GmbH");
    nc_attput(ncid, -1, "Creator", NC_CHAR, ssCreator);
    rvRange = [1, nRows];
    nc_attput(ncid, -1, "Range", NC_LONG, rvRange);
    nc_attput(ncid, -1, "Reference", NC_CHAR, "");
    nc_attput(ncid, -1, "Date", NC_CHAR, sum(date()));
    nc_attput(ncid, -1, "Time", NC_CHAR, sum(time()));
    nc_attput(ncid, -1, "Title", NC_CHAR, "");
    nc_attput(ncid, -1, "Comment1", NC_CHAR, "");
    nc_attput(ncid, -1, "Comment2", NC_CHAR, "");
    nc_attput(ncid, -1, "Comment3", NC_CHAR, "");
    for (i in 1:nCols) {
        if (NC_DATETIME == rvDataType[i]) {
            varid = nc_vardef(ncid, svChanName[i], NC_DOUBLE, dimids);
        } else {
            varid = nc_vardef(ncid, svChanName[i], rvDataType[i], dimids);
        }
        nc_attput(ncid, varid, "title", NC_CHAR, svChanName[i]);
        nc_attput(ncid, varid, "units", NC_CHAR, svUnits[i]);
        nc_attput(ncid, varid, "long_name", NC_CHAR, svChanName[i] + ..
                               "  [" + svUnits[i] + "]");
        nc_attput(ncid, varid, "scale_factor", NC_DOUBLE, 1.0);
        nc_attput(ncid, varid, "add_offset", NC_DOUBLE, 0.0);
        nc_attput(ncid, varid, "Description", NC_CHAR, "");
        nc_attput(ncid, varid, "Comment", NC_CHAR, "");
        nc_attput(ncid, varid, "ChanType", NC_CHAR, "DateTime");
    }
    nc_setfill(ncid, NC_NOFILL);
    nc_endef(ncid);
    for (i in 1:nCols) {
        if (rvDataType[i] == 100) {
            rvData = DT_ParseDateTime(smData[;i]);
        } else {
            rvData = strtod(smData[;i]); // string to double
        }
        nc_varput(ncid, i-1, 0, nRows, rvData);
    }
    nc_close(ncid);
    return ssNetCDFName;
}