This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Ed Yu wrote:
yeah, rank 0 is not obvious, you need to use 0 length array of Dimension. heres an example:Hi all, I'm trying to create a scalar variable say: interval = 60 I have trouble using the java api to add a variable without a dimension. Did I completely misunderstand the NetCDF concept? If I make the variable interval a dimension with rank-1 (vector), I was able to create the netcdf file. Any comments and sample code is appreciated. Thanks a bunch!
public void testWrite() {
NetcdfFileWriteable ncfile = new NetcdfFileWriteable();
ncfile.setName("mydata.nc");
ncfile.addVariable("interval", int.class, new Dimension[0]);
// create the file
try {
ncfile.create();
} catch (IOException e) {
System.err.println("ERROR creating file");
assert(false);
}
// write scaler
try {
ArrayInt.D0 scalarA = new ArrayInt.D0();
scalarA.set( 60);
ncfile.write("interval", scalarA);
} catch (IOException e) {
System.err.println("ERROR writing Achar4");
assert(false);
}
// read it back
// read scalar variable
Variable scalar = ncfile.findVariable("interval"));
try {
A = scalar.read();
} catch (IOException e) {
assert(false);
}
ArrayInt.D0 scalarA = (ArrayInt.D0) A;
int vali = scalarA.get();
assert(vali == 60);
// all done
try {
ncfile.close();
} catch (IOException e) {
System.err.println("ERROR writing file");
assert(false);
}
System.out.println( "*****************Test Write done");
}