Due to the current gap in continued funding from the U.S. National Science Foundation (NSF), the NSF Unidata Program Center has temporarily paused most operations. See NSF Unidata Pause in Most Operations for details.
import java.io.File; import java.io.IOException; import ucar.ma2.Array; import ucar.ma2.DataType; import ucar.ma2.InvalidRangeException; import ucar.nc2.Dimension; import ucar.nc2.NetcdfFile; import ucar.nc2.NetcdfFileWriteable; public class Foo {public static void main(String[] args) throws IOException, InvalidRangeException {
File tempFile = File.createTempFile("foo", "nc"); tempFile.deleteOnExit();NetcdfFileWriteable ncWriteable = NetcdfFileWriteable.createNew(tempFile.getAbsolutePath(), false);
try { Dimension timeDim = ncWriteable.addUnlimitedDimension("time");ncWriteable.addVariable("time", DataType.INT, new Dimension[]{timeDim}); ncWriteable.addVariableAttribute("time", "units", "hours since 1990-01-01");
ncWriteable.create(); Array timeData = Array.factory(DataType.INT, new int[]{1}); int[] time_origin = new int[] {0}; for (int time = 0; time < 10; time++) { timeData.setInt(timeData.getIndex(), time * 12); time_origin[0] = time; ncWriteable.write("time", time_origin, timeData); } // Prints "0 12 24 36 48 60 72 84 96 108", the expected result. System.out.println(ncWriteable.readSection("time")); } finally { ncWriteable.close(); } NetcdfFile ncFile = NetcdfFile.open(tempFile.getAbsolutePath()); try { // Prints "0 0 12 0 24 0 36 0 48 0". System.out.println(ncFile.readSection("time")); } finally { ncFile.close(); } } }
netcdf-java
archives: