/** * Abort writing to this file. The file is closed. * @throws java.io.IOException */ public void abort() throws java.io.IOException { if (spiw != null) { spiw.close(); spiw = null; } }
/** * After you have added all of the Dimensions, Variables, and Attributes, * call create() to actually create the file. You must be in define mode. * After this call, you are no longer in define mode. * @throws java.io.IOException if I/O error */ public void create() throws java.io.IOException { if (!defineMode) throw new UnsupportedOperationException("not in define mode"); if (!isNewFile) throw new UnsupportedOperationException("can only call create on a new file"); ncfile.finish(); // ?? spiw.setFill( fill); // ?? spiw.create(location, ncfile, extraHeader, preallocateSize, isLargeFile); defineMode = false; }
/** * Flush anything written to disk. * @throws IOException if I/O error */ public void flush() throws java.io.IOException { spiw.flush(); }
/** * Set the fill flag: call before calling create() or doing any data writing. Only used by netcdf-3 (?). * If true, the data is first written with fill values. * Default is fill = false. * Leave false if you expect to write all data values, set to true if you want to be * sure that unwritten data values have the fill value in it. * * @param fill set fill mode true or false */ public void setFill(boolean fill) { this.fill = fill; spiw.setFill(fill); }
/** * Write data to the named variable. Must not be in define mode. * * @param v variable to write to * @param origin offset within the variable to start writing. * @param values write this array; must be same type and rank as Variable * @throws IOException if I/O error * @throws ucar.ma2.InvalidRangeException if values Array has illegal shape */ public void write(Variable v, int[] origin, Array values) throws java.io.IOException, InvalidRangeException { if (defineMode) throw new UnsupportedOperationException("in define mode"); spiw.writeData(v, new Section(origin, values.getShape()), values); v.invalidateCache(); }
/** * Update the value of an existing attribute. Attribute is found by name, which must match exactly. * You cannot make an attribute longer, or change the number of values. * For strings: truncate if longer, zero fill if shorter. Strings are padded to 4 byte boundaries, ok to use padding if it exists. * For numerics: must have same number of values. * This is really a netcdf-3 writing only. netcdf-4 attributes can be changed without rewriting. * * @param v2 variable, or null for global attribute * @param att replace with this value * @throws IOException if I/O error */ public void updateAttribute(ucar.nc2.Variable v2, Attribute att) throws IOException { // if (defineMode) // throw new UnsupportedOperationException("in define mode"); spiw.updateAttribute(v2, att); }
/** * Set the redefine mode. * Designed to emulate nc_redef (redefineMode = true) and * nc_enddef (redefineMode = false) * @param redefineMode start or end define mode * @return true if it had to rewrite the entire file, false if it wrote the header in place * @throws java.io.IOException on read/write error */ public boolean setRedefineMode(boolean redefineMode) throws IOException { if (redefineMode && !defineMode) { defineMode = true; } else if (!redefineMode && defineMode) { // we were in defineMode, and now we are ending. defineMode = false; finish(); // try to rewrite header, if it fails, then we have to rewrite entire file boolean ok = spiw.rewriteHeader(isLargeFile); // LOOK seems like we should be using isNewFile if (!ok) rewrite(); return !ok; } return false; }
/** * Open an existing Netcdf file for writing data. * * @param location open an existing file at this location * @deprecated use openExisting(String filename, boolean fill) * @throws java.io.IOException on read error */ public NetcdfFileWriteable(String location) throws IOException { super(); this.location = location; ucar.unidata.io.RandomAccessFile raf = new ucar.unidata.io.RandomAccessFile(location, "rw"); spi = SPFactory.getServiceProvider(); spiw = (IOServiceProviderWriter) spi; spiw.open(raf, this, null); }
spiw.openForWriting(raf, ncfile, null); else defineMode = true;
/** * Set the fill flag: call before calling create() or doing any data writing. Only used by netcdf-3 (?). * If true, the data is first written with fill values. * Default is fill = false. * Leave false if you expect to write all data values, set to true if you want to be * sure that unwritten data values have the fill value in it. * * @param fill set fill mode true or false */ public void setFill(boolean fill) { this.fill = fill; spiw.setFill( fill); }
/** * Write data to the named variable. Must not be in define mode. * * @param v variable to write to * @param origin offset within the variable to start writing. * @param values write this array; must be same type and rank as Variable * @throws IOException if I/O error * @throws ucar.ma2.InvalidRangeException if values Array has illegal shape */ public void write(Variable v, int[] origin, Array values) throws java.io.IOException, InvalidRangeException { if (defineMode) throw new UnsupportedOperationException("in define mode"); spiw.writeData(v, new Section(origin, values.getShape()), values); v.invalidateCache(); }
/** * Update the value of an existing attribute. Attribute is found by name, which must match exactly. * You cannot make an attribute longer, or change the number of values. * For strings: truncate if longer, zero fill if shorter. Strings are padded to 4 byte boundaries, ok to use padding if it exists. * For numerics: must have same number of values. * * @param v2 variable, or null for global attribute * @param att replace with this value * @throws IOException if I/O error */ public void updateAttribute(ucar.nc2.Variable v2, Attribute att) throws IOException { if (defineMode) throw new UnsupportedOperationException("in define mode"); spiw.updateAttribute(v2, att); }
/** * Set the redefine mode. * Designed to emulate nc_redef (redefineMode = true) and * nc_enddef (redefineMode = false) * @param redefineMode start or end define mode * @return true if it had to rewrite the entire file, false if it wrote the header in place * @throws java.io.IOException on read/write error */ public boolean setRedefineMode(boolean redefineMode) throws IOException { if (redefineMode && !defineMode) { defineMode = true; } else if (!redefineMode && defineMode) { defineMode = false; ncfile.finish(); // try to rewrite header, if it fails, then we have to rewrite entire file boolean ok = spiw.rewriteHeader( isLargeFile); // LOOK seems like we should be using isNewFile if (!ok) rewrite(); return !ok; } return false; }
/** * Open an existing Netcdf file for writing data. * * @param location open an existing file at this location * @deprecated use openExisting(String filename, boolean fill) * @throws java.io.IOException on read error */ public NetcdfFileWriteable(String location) throws IOException { super(); this.location = location; ucar.unidata.io.RandomAccessFile raf = new ucar.unidata.io.RandomAccessFile(location, "rw"); spi = SPFactory.getServiceProvider(); spiw = (IOServiceProviderWriter) spi; spiw.open(raf, this, null); }
spiw.openForWriting(raf, ncfile, null); else defineMode = true;
private void rewrite() throws IOException { spiw.flush(); spiw.close(); spiw.create(location, this, extraHeader, preallocateSize, isLargeFile); spiw.setFill( fill); Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); if (!result) recordVar = null;