@Override public void close() throws IOException { writer.close(); }
@Override public void close() throws IOException { writer.close(); }
@Override public void close() throws IOException { writer.close(); }
public void close() throws KlabException { if (_open) { flush(); _open = false; try { _ncfile.close(); } catch (IOException e) { throw new KlabIOException(e); } } }
@Override public void close() { try { f.create(); for (Var<?> var : map.keySet()) { List<?> list = map.get(var); int[] shape = new int[] { list.size() }; Array data = Array.factory(DataType.getType(var.cls()), shape); for (int i = 0; i < list.size(); i++) { data.setObject(i, list.get(i)); } f.write(var.variable(), data); } f.close(); } catch (IOException e) { throw new RuntimeException(e); } catch (InvalidRangeException e) { throw new RuntimeException(e); } }
/** * * @return scanIndexArray an {@link ucar.ma2.Array Array} containing scan indices of all scans * @throws IOException */ private Array getScanIndexArray() throws IOException { // Populate the scan indices // Create a simple 1D array of type int, element i of the shape array contains the length of // the (i+1)th dimension of the array Array scanIndexArray = Array.factory(int.class, new int[] {totalScans}); int idx = 0; for (MsScan scan : scans) { if (canceled) { writer.close(); return null; } scanStartPositions[idx + 1] = scanStartPositions[idx] + scan.getNumberOfDataPoints(); idx++; } for (int i = 0; i < scanStartPositions.length - 1; i++) scanIndexArray.setInt(i, scanStartPositions[i]); return scanIndexArray; }
/** * Write the NetCDF file * @throws IOException * @throws InvalidRangeException */ public void write() throws IOException, InvalidRangeException { // end of define mode writer.create(); // Setting values for (NetCDFDimensionMapping mapper : dimensionsManager.getDimensions()) { crsWriter.setCoordinateVariable(mapper); } writeDataValues(); // Close the writer writer.close(); }
/** * Write the NetCDF file * * @throws IOException * @throws InvalidRangeException */ public void write() throws IOException, ucar.ma2.InvalidRangeException { // end of define mode writer.create(); try { // Setting values for (NetCDFDimensionsManager.NetCDFDimensionMapping mapper : dimensionsManager.getDimensions()) { crsWriter.setCoordinateVariable(mapper); } writeDataValues(); } finally { // Close the writer writer.close(); } }
public void rewrite() throws IOException, InvalidRangeException { Attribute attr = ncIn.getRootGroup().findAttribute("featureType"); if(attr.getStringValue().contains("RADIAL")) isRadial = true; createGroup(null, ncIn.getRootGroup()); ncOut.create(); transferData(ncIn.getRootGroup()); ncOut.close(); }
public void rewrite() throws IOException, InvalidRangeException { Attribute attr = ncIn.getRootGroup().findAttribute("featureType"); if(attr.getStringValue().contains("RADIAL")) isRadial = true; createGroup(null, ncIn.getRootGroup()); ncOut.create(); transferData(ncIn.getRootGroup()); ncOut.close(); }
public void rewrite() throws IOException, InvalidRangeException { Attribute attr = ncIn.getRootGroup().findAttribute("featureType"); if(attr.getStringValue().contains("RADIAL")) isRadial = true; createGroup(null, ncIn.getRootGroup()); ncOut.create(); transferData(ncIn.getRootGroup()); ncOut.close(); }
@Test public void testUnlimitedDimension() throws IOException, InvalidRangeException { String location = tempFolder.newFile().getAbsolutePath(); File f = new File(location); assert f.delete(); Variable time; try (NetcdfFileWriter writer = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, location)) { logger.debug("write to file = {}", new File(location).getAbsolutePath()); Dimension timeDim = writer.addUnlimitedDimension("time"); List<Dimension> dims = new ArrayList<>(); dims.add(timeDim); time = writer.addVariable(null, "time", DataType.DOUBLE, dims); writer.create(); Array data = Array.makeFromJavaArray(new double[]{0, 1, 2, 3}); writer.write(time, data); writer.close(); try (NetcdfFileWriter writer2 = NetcdfFileWriter.openExisting(location)) { time = writer2.findVariable("time"); int[] origin = new int[1]; origin[0] = (int) time.getSize(); writer2.write(time, origin, data); } try (NetcdfFile file = NetcdfFile.open(location)) { time = file.findVariable("time"); assert time.getSize() == 8 : "failed to append to unlimited dimension"; } } }
public void alterExistingFile(String url) throws IOException { NetcdfFileWriter file = null; try { file = NetcdfFileWriter.openExisting(url); file.setRedefineMode(true); //Group rootGroup = file.getRootGroup(); //Group headerDataGroup = new Group(file, rootGroup, "header_data"); //file.addGroup(rootGroup, headerDataGroup); file.addVariable(null, newVarName, DataType.FLOAT, "z y x"); } finally { if (file != null) { file.setRedefineMode(false); file.flush(); file.close(); } } }
private NetcdfFileWriter createTimeLatLonDataCube(String filename, double[] latitudes, double[] longitudes) throws IOException { NetcdfFileWriter writeableFile = NetcdfFileWriter.createNew(filename, true); // define dimensions, including unlimited Dimension latDim = writeableFile.addDimension(latVar, latitudes.length); Dimension lonDim = writeableFile.addDimension(lonVar, longitudes.length); Dimension timeDim = writeableFile.addUnlimitedDimension(timeVar); // define Variables defineHeader(writeableFile, timeVar, latVar, lonVar, timeVar+" "+latVar+" "+lonVar); // create the file writeableFile.create(); writeableFile.close(); return writeableFile; }
public void makeNetCDF() throws IOException, InvalidRangeException { String helloGreek = makeString(helloGreekCode, true); helloGreek = ""; //Normalizer.normalize(helloGreek, Normalizer.Form.NFC); System.out.println("normalized= "+showString(helloGreek)); String filename = "C:/data/unicode/helloNorm.nc"; NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(filename, true); ucar.nc2.Dimension dim = ncfile.addDimension(helloGreek, 20); ncfile.addVariable(helloGreek, DataType.CHAR, helloGreek); ncfile.addVariableAttribute(helloGreek, "units", helloGreek); ncfile.create(); ArrayChar.D1 data = new ArrayChar.D1(dim.getLength()); data.setString(helloGreek); ncfile.write(helloGreek, data); ncfile.close(); NetcdfFile nc = NetcdfFile.open(filename); Variable v = ncfile.findVariable(helloGreek); assert v != null; assert v.getShortName().equals(helloGreek); Attribute att = v.findAttribute("units"); assert att != null; assert att.isString(); assert(helloGreek.equals(att.getStringValue())); nc.close(); }
public void finish() throws IOException { if (llbb != null) { writer.updateAttribute(null, new Attribute(ACDD.LAT_MIN, llbb.getLowerLeftPoint().getLatitude())); writer.updateAttribute(null, new Attribute(ACDD.LAT_MAX, llbb.getUpperRightPoint().getLatitude())); writer.updateAttribute(null, new Attribute(ACDD.LON_MIN, llbb.getLowerLeftPoint().getLongitude())); writer.updateAttribute(null, new Attribute(ACDD.LON_MAX, llbb.getUpperRightPoint().getLongitude())); } if (!noTimeCoverage) { if (minDate == null) minDate = CalendarDate.present(); if (maxDate == null) maxDate = CalendarDate.present(); writer.updateAttribute(null, new Attribute(ACDD.TIME_START, CalendarDateFormatter.toDateTimeStringISO(minDate))); writer.updateAttribute(null, new Attribute(ACDD.TIME_END, CalendarDateFormatter.toDateTimeStringISO(maxDate))); } writer.close(); }
public void finish() throws IOException { if (llbb != null) { writer.updateAttribute(null, new Attribute(ACDD.LAT_MIN, llbb.getLowerLeftPoint().getLatitude())); writer.updateAttribute(null, new Attribute(ACDD.LAT_MAX, llbb.getUpperRightPoint().getLatitude())); writer.updateAttribute(null, new Attribute(ACDD.LON_MIN, llbb.getLowerLeftPoint().getLongitude())); writer.updateAttribute(null, new Attribute(ACDD.LON_MAX, llbb.getUpperRightPoint().getLongitude())); } if (!noTimeCoverage) { if (minDate == null) minDate = CalendarDate.present(); if (maxDate == null) maxDate = CalendarDate.present(); writer.updateAttribute(null, new Attribute(ACDD.TIME_START, CalendarDateFormatter.toDateTimeStringISO(minDate))); writer.updateAttribute(null, new Attribute(ACDD.TIME_END, CalendarDateFormatter.toDateTimeStringISO(maxDate))); } writer.close(); }
public void finish() throws IOException { writer.updateAttribute(null, new Attribute(CDM.LAT_MIN, llbb.getLowerLeftPoint().getLatitude())); writer.updateAttribute(null, new Attribute(CDM.LAT_MAX, llbb.getUpperRightPoint().getLatitude())); writer.updateAttribute(null, new Attribute(CDM.LON_MIN, llbb.getLowerLeftPoint().getLongitude())); writer.updateAttribute(null, new Attribute(CDM.LON_MAX, llbb.getUpperRightPoint().getLongitude())); if((writer.getNetcdfFile().findAttribute("@"+CDM.TIME_START) != null) && (writer.getNetcdfFile().findAttribute("@"+CDM.TIME_END) != null )){ if (minDate == null) minDate = CalendarDate.present(); if (maxDate == null) maxDate = CalendarDate.present(); writer.updateAttribute(null, new Attribute(CDM.TIME_START, CalendarDateFormatter.toDateTimeStringISO(minDate))); writer.updateAttribute(null, new Attribute(CDM.TIME_END, CalendarDateFormatter.toDateTimeStringISO(maxDate))); } writer.close(); }
@Test public void checkPaddingOnWriteReadOneDimCharArrayOnlyRecordVar() throws IOException, InvalidRangeException { File tmpDataDir = tempFolder.newFolder(); File testFile = new File(tmpDataDir, "file.nc"); try(NetcdfFileWriter ncfWriteable = NetcdfFileWriter.createNew(testFile.getPath(), true)) { Dimension recDim = ncfWriteable.addUnlimitedDimension("v"); Variable var = ncfWriteable.addVariable("v", DataType.CHAR, "v"); assertEquals(1, var.getElementSize()); ncfWriteable.create(); N3header.Vinfo vinfo = (N3header.Vinfo) var.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); char[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; Array dataArray = Array.factory(DataType.CHAR, new int[]{data.length}, data); ncfWriteable.write(var.getFullNameEscaped(), dataArray); ncfWriteable.close(); NetcdfFile ncf = NetcdfFile.open(testFile.getPath()); Variable readVar = ncf.findVariable("v"); assertEquals(readVar.getDataType(), DataType.CHAR); assertEquals(1, readVar.getElementSize()); vinfo = (N3header.Vinfo) readVar.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); int[] org = {0}; char[] readdata = (char[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray(); assertArrayEquals(data, readdata); } }
@Test public void checkPaddingOnWriteReadOneDimByteArrayOnlyRecordVar() throws IOException, InvalidRangeException { File tmpDataDir = tempFolder.newFolder(); File testFile = new File(tmpDataDir, "file.nc"); try( NetcdfFileWriter ncfWriteable = NetcdfFileWriter.createNew(testFile.getPath(), true)) { Dimension recDim = ncfWriteable.addUnlimitedDimension("v"); Variable var = ncfWriteable.addVariable("v", DataType.BYTE, "v"); assertEquals(1, var.getElementSize()); ncfWriteable.create(); N3header.Vinfo vinfo = (N3header.Vinfo) var.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -2, -3, -4, -5, -6, -7, -8, -9}; Array dataArray = Array.factory(DataType.BYTE, new int[]{data.length}, data); ncfWriteable.write(var.getFullNameEscaped(), dataArray); ncfWriteable.close(); NetcdfFile ncf = NetcdfFile.open(testFile.getPath()); Variable readVar = ncf.findVariable("v"); assertEquals(readVar.getDataType(), DataType.BYTE); assertEquals(1, readVar.getElementSize()); vinfo = (N3header.Vinfo) readVar.getSPobject(); assertTrue(vinfo.isRecord); assertEquals(1, vinfo.vsize); int[] org = {0}; byte[] readdata = (byte[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray(); assertArrayEquals(data, readdata); } }