Main Content

ncwriteschema

Add netCDF schema definitions to netCDF file

    Description

    ncwriteschema(filename,schema) creates or adds attributes, dimensions, variable definitions, and group structure defined in schema to the netCDF file filename.

    You can use ncwriteschema in combination with ncinfo to create a netCDF file based on the schema of an existing file.

    Note

    ncwriteschema does not write variable data. Use ncwrite to write data to the created variables. Unlimited dimensions have an initial size of 0 until you write data.

    Note

    ncwriteschema cannot change the format of an existing file. It cannot redefine existing variables or dimensions in filename. If schema contains attributes, dimensions, variable definitions, or a group structure that already exist in the file, ncwriteschema issues a warning but continues processing.

    example

    Examples

    collapse all

    Define the schema for a classic format netCDF file with two dimension definitions. Then write the schema to a netCDF file.

    mySchema.Name = "/";
    mySchema.Format = "classic";
    mySchema.Dimensions(1).Name = "time";
    mySchema.Dimensions(1).Length = Inf;
    mySchema.Dimensions(2).Name = "rows";
    mySchema.Dimensions(2).Length = 10;
    ncwriteschema("emptyFile.nc",mySchema)

    Display the contents of emptyfile.nc.

    ncdisp("emptyfile.nc")
    Source:
               pwd\emptyfile.nc
    Format:
               classic
    Dimensions:
               time = 0     (UNLIMITED)
               rows = 10

    Use ncinfo to get the schema of the peaks variable from the file example.nc, and write this schema to a new file.

    myVarSchema = ncinfo("example.nc","peaks");
    ncwriteschema("peaksFile.nc",myVarSchema)

    Read the data for peaks from example.nc, write it to the new file, and verify the results.

    peaksData = ncread("example.nc","peaks");
    ncwrite("peaksFile.nc","peaks",peaksData)
    ncdisp("peaksFile.nc")
    Source:
               pwd\peaksFile.nc
    Format:
               netcdf4
    Dimensions:
               x = 50
               y = 50
    Variables:
        peaks
               Size:       50x50
               Dimensions: x,y
               Datatype:   int16
               Attributes:
                           _FillValue  = -32767
                           description = 'z = peaks(50);'

    Input Arguments

    collapse all

    Filename, specified as a string scalar or character vector. Specify the name of an existing netCDF file or the name you want to assign to a new netCDF file.

    If filename does not exist, ncwriteschema creates the netCDF file using the netcdf4_classic format, unless the Format field in schema specifies another format.

    Example: "myFile.nc"

    NetCDF file, group, dimension, or variable schema, specified as a structure or structure array. The schema argument can represent either a netCDF file, a group in a netCDF file of format netcdf4, one or more netCDF dimensions, or one or more netCDF variables. A file or group schema can contain a field for a variable schema, a field for a dimension schema, or both. You can use the output of ncinfo as a schema.

    This table lists the fields in the different types of schemas. Optional fields are marked with an asterisk (*), and fields that apply only to data with format netcdf4 or netcdf4_classic are marked with a dagger (†).

    Schema TypeFieldDescription
    File or group schemaName"/" for a file schema; group name for a group schema, specified as a string scalar or character vector
    Groups (*)Group schema, specified as a structure or structure array
    Dimensions (*)Dimension schema, specified as a structure or structure array
    Variables (*)Variable schema, specified as a structure or structure array
    Attributes (*)Attribute schema, specified as a structure vector containing Name (string scalar or character vector) and Value fields
    Format (*)

    NetCDF file format, specified as classic, 64bit, netcdf4_classic, or netcdf4

    Note

    ncwriteschema ignores this field if the schema is a group schema.

    Dimension schemaNameDimension name, specified as a string scalar or character vector
    LengthDimension length, specified as a nonnegative integer (can be Inf or 0 for unlimited dimensions)
    Unlimited (*)

    Whether the dimension is unlimited, specified as a logical scalar

    Note

    If the value of this field is true, then ncwriteschema sets the Length field to 0.

    Format (*)

    NetCDF file format, specified as classic, 64bit, netcdf4_classic, or netcdf4

    Note

    ncwriteschema ignores this field if the dimension schema is a schema inside a file or group schema.

    Variable schemaNameVariable name, specified as a string scalar or character vector
    DimensionsDimension schema, specified as a structure or structure array
    DatatypeMATLAB® data type, specified as a string scalar or character vector
    Attributes (*)Attribute schema, specified as a structure vector containing Name (string scalar or character vector) and Value fields
    ChunkSize (*) (†)Chunk size for the variable, specified as a numeric scalar
    FillValue (*) (†)Fill value of the variable, specified as a numeric scalar or text
    DeflateValue (*) (†)Deflate compression level of the variable, specified as a nonnegative integer
    Shuffle (*) (†)Whether the shuffle filter is enabled, specified as a logical scalar
    Format (*)

    NetCDF file format, specified as classic, 64bit, netcdf4_classic, or netcdf4

    Note

    ncwriteschema ignores this field if the variable schema is a schema inside a file or group schema.

    Data Types: struct

    Version History

    Introduced in R2011a