Hi,
I am using the current 4.1 netcdf-java library (4.1.20100311.2157 or change 14
I believe) and encountering a problem when trying to use redefine mode. A
similar problem also occurs using the latest 4.0 library.
Our aim is to create some command line tools that allow a netCDF file to be
build up. First, define a dimension, then add a variable to it, then populate
the variable. However when we close and reopen the file, any attempt to add a
variable results in the following stack trace:
Exception in thread "main" java.io.IOException: The parameter is incorrect
at java.io.RandomAccessFile.setLength(Native Method)
at ucar.unidata.io.RandomAccessFile.close(RandomAccessFile.java:339)
at ucar.nc2.iosp.netcdf3.N3iosp.close(N3iosp.java:878)
at ucar.nc2.NetcdfFileWriteable.close(NetcdfFileWriteable.java:769)
at au.csiro.netcdf.CreateFile.main(CreateFile.java:45)
The following code demonstrates the problem.
package au.csiro.netcdf;
import java.io.IOException;
import ucar.ma2.DataType;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFileWriteable;
public class CreateFile {
private static String dimensionName = "lat";
private static int dimensionSize = 20;
private static String variableName = "temp";
private static DataType variableDataType = DataType.getType("Float");
public static void main(String[] args) throws IOException {
String outputFilename = args[0];
NetcdfFileWriteable ncFile =
NetcdfFileWriteable.createNew(outputFilename, true);
try {
Dimension dimension = new Dimension(dimensionName, dimensionSize,
true /* isShared */,
false /* isUnlimited */, false /* isVariableLength */);
ncFile.addDimension(null, dimension);
ncFile.create();
} finally {
ncFile.close();
}
ncFile = NetcdfFileWriteable.openExisting(outputFilename, true);
try {
ncFile.setRedefineMode(true);
ncFile.addVariable(variableName, variableDataType, dimensionName);
ncFile.setRedefineMode(false);
} finally {
ncFile.close();
}
}
}
If the ncFile.close() and NetcdfFileWriteable.openExisting calls are commented
out the file is successfully created with the variable. Could anyone advise if
this is something I am doing wrong or a problem with the library please?
Cheers,
James Dempsey
Java Technical Lead | Software Services
Information Management and Technology
CSIRO Australia