Although only part of the netCDF API is implemented in this library, you can achieve nearly all operations on netCDF variables and attributes. A char type attribute is treated as string for input and output; and only string, integer, and double types are allowed for writing to a attribute. The limitation, not affecting reading, is partly due to the supported data type of Z-Script. In practice other types of attributes are hardly used.
| Function | Parameter Type | Remark |
| netcdf(filename[,mode]) | string, string | Returns a netCDF object. The mode determines witting ("w"), creation ("c"), or reading ("r" or none). |
| :version() | Returns the netCDF library version string. | |
| :defdim(name, size) | string, integer | Defines the dimension for the named variable and returns the variable ID. If size <= 0, defines the dimension as unlimited. The function should be called right after file creation. The unlimited dimension should be the last dimension. |
| :defvar(name, type, dimid0[, dimid1...]) | string, string, integer[ integer...] | Defines data type and dimensions for the named variable and returns the variable ID. The type should be "char", "byte", "short", "int", "float", or "double". The number of augments after the type argument determines the dimensions. Each dimension size is determined by the size of the specified dimension (by dimension ID) . If unlimited dimension id is used, it must be dimid0. |
| :size(name) | string | Returns the an array containing the total number of data in the named variable and the datum size in byte. |
| :getvar(name, ptr, n, esize[, start1, cout1...]) | string, user, integer, integer[ integer, integer...] | Reads data from the named variable to the pointer of the user object. The pointer points to a data array of n size with element size of esize. A start-count argument pair specifies where to start and how many data to read in the corresponding dimension If no start-count arguments, the functiond reads all data. If start-count is only partially specified, the unspecified start will be 0 and count will be 1. |
| :putvar(name, ptr[, start1, cout1...]) | string, user[ integer, integer...] | Writes data to the named variable. Refer to getvar() for more explanations of parameters. |
| :getatt(varname, attname) | string, string | Returns the named attribute of the named variable as string for text or as array for numbers. |
| :putatt(varname, attname, att1[, att2...]) | string, string, string or number[, number...] | Deletes, creates, and then writes contents to the named attribute for the named variable. Supported attribute types include string, integer, and double number. If att1 is string, the rest of the arguments will be ignored; otherwise all attributes are treated as data of an array. If varname="", the attribute is global. |
| :defined(varname) | string | Returns true if the named variable exists; otherwise returns false. |
| :info([fname]) | string | Displays headers of the netCDF file. If the file name is given, results will be saved to it. |
| :inqdim() | Returns dimension names and lengths in an array. | |
| :inqvar() | Returns the number of variables. | |
| :inqvar(i) | integer | Returns the data type, number of dimensions, and dimension names of the ith variable in an array. |
| :inqatt(name) | string | Returns attributes of the name variable in an array. If the name is "GLOBAL", returns the global attributes. |
load("matrix", "netcdf.dll");
nc = netcdf("test.nc", "c"); // create
// it seems global attribute must be created before
// define any other variables.
nc:putatt("", "global", "from ABC...........");
nc:putatt("", "local", "to CDE...........");
// define dimensions and dimension variables
id1 = nc:defdim("lat", 19);
nc:defvar("lat", "int", id1);
id2 = nc:defdim("lon", 36);
nc:defvar("lon", "int", id2);
id3 = nc:defdim("time", 0); // unlimited
nc:defvar("time", "int", id3);
// define variables
iv1 = nc:defvar("data1", "short", id1, id2);
iv2 = nc:defvar("data2", "double", id3, id1, id2);
// write attributes
nc:putatt("lat", "unit", "degree");
nc:putatt("lat", "range", -90, 90);
csv(nc:getatt("lat", "unit"));
b = nc:getatt("lat", "range");
csv(b[0], b[1]);
a = matrix("int", 19, 1);
a:fill(-90, 10);
p = a:ptr();
nc:putvar("lat", p[0]);
a:resize(36, 1);
a:fill(0, 10);
p = a:ptr();
nc:putvar("lon", p[0]);
a = matrix("short", 19, 36);
a:fill(0, 1);
p = a:ptr();
nc:putvar("data1", p[0]);
a = matrix("int", 5, 1);
a:fill(1, 1);
p = a:ptr();
nc:putvar("time", p[0], 0, 5);
a = matrix("double", 19, 36);
a:fill(0, 1);
p = a:ptr();
nc:putvar("data2", p[0], 0, 1, 0, 19, 0, 36);
a:fill(0, 10);
nc:putvar("data2", p[0], 1, 1, 0, 19, 0, 36);
nc:info();
/////////////////////// example for reading /////////////////////////
load("matrix", "netcdf.dll");
nc = netcdf("test.nc");
a = matrix("double", 19, 36);
p = a:ptr();
csv(p[0], p[1], p[2]);
nc:getvar("data2", p[0], p[1], p[2], 0, 1, 0, 19, 0, 36);
a:print();