cdm/src/main/java/ucar/nc2/NetcdfFileWriter.java
private void rewrite() throws IOException {
    // close existing file, rename and open as read-only
    spiw.flush();
    spiw.close();

    File prevFile = new File(location);
    if (!prevFile.exists()) {
      return;
    }

    File tmpFile = new File(location + ".tmp");
    if (tmpFile.exists()) {
      boolean ok = tmpFile.delete();
      if (!ok) log.warn("rewrite unable to delete {}", tmpFile.getPath());
    }
    if (!prevFile.renameTo(tmpFile)) {
      System.out.println(prevFile.getPath() + " prevFile.exists " + prevFile.exists() + " canRead = " + prevFile.canRead());
      System.out.println(tmpFile.getPath() + " tmpFile.exists " + tmpFile.exists() + " canWrite " + tmpFile.canWrite());
      throw new RuntimeException("Cant rename " + prevFile.getAbsolutePath() + " to " + tmpFile.getAbsolutePath());
    }

    NetcdfFile oldFile = NetcdfFile.open(tmpFile.getPath());

    /* use record dimension if it has one
    Structure recordVar = null;
    if (oldFile.hasUnlimitedDimension()) {
      oldFile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      recordVar = (Structure) oldFile.findVariable("record");
      /* if (recordVar != null) {
        Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
        if (!result)
          recordVar = null;
      }
      } */

    // create new file with current set of objects
    spiw.create(location, ncfile, extraHeader, preallocateSize, isLargeFile);
    spiw.setFill(fill);
    //isClosed = false;

    /* wait till header is written before adding the record variable to the file
    if (recordVar != null) {
      Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      if (!result)
        recordVar = null;
    } */

    FileWriter2 fileWriter2 = new FileWriter2(this);

    for (Variable v : ncfile.getVariables()) {
      String oldVarName = v.getFullName();
      Variable oldVar = oldFile.findVariable(oldVarName);
      if (oldVar != null) {
        fileWriter2.copyAll(oldVar, v);
      } else if (varRenameMap.containsKey(oldVarName)) {
        // var name has changed in ncfile - use the varRenameMap to find
        //  the correct variable name to request from oldFile
        String realOldVarName = varRenameMap.get(oldVarName);
        oldVar = oldFile.findVariable(realOldVarName);
        if (oldVar != null) {
          fileWriter2.copyAll(oldVar, v);
        }
      } else {
        String message = "Cannot find variable " + oldVarName + " to copy to new file.";
        log.warn(message);
        System.out.println(message);
      }
    }

    // delete old
    oldFile.close();
    if (!tmpFile.delete())
      throw new RuntimeException("Cant delete "+tmpFile.getAbsolutePath());
  }
private void rewrite() throws IOException {
    // close existing file, rename and open as read-only
    spiw.flush();
    spiw.close();

    File prevFile = new File(location);
    if (!prevFile.exists()) {
      return;
    }

    File tmpFile = new File(location + ".tmp");
    if (tmpFile.exists()) {
      boolean ok = tmpFile.delete();
      if (!ok) log.warn("rewrite unable to delete {}", tmpFile.getPath());
    }
    if (!prevFile.renameTo(tmpFile)) {
      System.out.println(prevFile.getPath() + " prevFile.exists " + prevFile.exists() + " canRead = " + prevFile.canRead());
      System.out.println(tmpFile.getPath() + " tmpFile.exists " + tmpFile.exists() + " canWrite " + tmpFile.canWrite());
      throw new RuntimeException("Cant rename " + prevFile.getAbsolutePath() + " to " + tmpFile.getAbsolutePath());
    }

    NetcdfFile oldFile = NetcdfFile.open(tmpFile.getPath());

    /* use record dimension if it has one
    Structure recordVar = null;
    if (oldFile.hasUnlimitedDimension()) {
      oldFile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      recordVar = (Structure) oldFile.findVariable("record");
      /* if (recordVar != null) {
        Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
        if (!result)
          recordVar = null;
      }
      } */

    // create new file with current set of objects
    spiw.create(location, ncfile, extraHeader, preallocateSize, isLargeFile);
    spiw.setFill(fill);
    //isClosed = false;

    /* wait till header is written before adding the record variable to the file
    if (recordVar != null) {
      Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      if (!result)
        recordVar = null;
    } */

    FileWriter2 fileWriter2 = new FileWriter2(this);

    for (Variable v : ncfile.getVariables()) {
      String oldVarName = v.getFullName();
      Variable oldVar = oldFile.findVariable(oldVarName);
      if (oldVar != null) {
        fileWriter2.copyAll(oldVar, v);
      } else if (varRenameMap.containsKey(oldVarName)) {
        // var name has changed in ncfile - use the varRenameMap to find
        //  the correct variable name to request from oldFile
        String realOldVarName = varRenameMap.get(oldVarName);
        oldVar = oldFile.findVariable(realOldVarName);
        if (oldVar != null) {
          fileWriter2.copyAll(oldVar, v);
        }
      } else {
        String message = "Cannot find variable " + oldVarName + " to copy to new file.";
        log.warn(message);
        System.out.println(message);
      }
    }

    // delete old
    oldFile.close();
    if (!tmpFile.delete())
      throw new RuntimeException("Cant delete "+tmpFile.getAbsolutePath());
  }
private void rewrite ( ) throws IOException { // close existing file, rename and open as read-only
 spiw . flush ( ) ; spiw . close ( ) ; File prevFile = new File ( location ) ; if ( ! prevFile . exists ( ) ) { return ; } File tmpFile = new File ( location + ".tmp" ) ; if ( tmpFile . exists ( ) ) { boolean ok = tmpFile . delete ( ) ; if ( ! ok ) log . warn ( "rewrite unable to delete {}" , tmpFile . getPath ( ) ) ; } if ( ! prevFile . renameTo ( tmpFile ) ) { System . out . println ( prevFile . getPath ( ) + " prevFile.exists " + prevFile . exists ( ) + " canRead = " + prevFile . canRead ( ) ) ; System . out . println ( tmpFile . getPath ( ) + " tmpFile.exists " + tmpFile . exists ( ) + " canWrite " + tmpFile . canWrite ( ) ) ; throw new RuntimeException ( "Cant rename " + prevFile . getAbsolutePath ( ) + " to " + tmpFile . getAbsolutePath ( ) ) ; } NetcdfFile oldFile = NetcdfFile . open ( tmpFile . getPath ( ) ) ; /* use record dimension if it has one
    Structure recordVar = null;
    if (oldFile.hasUnlimitedDimension()) {
      oldFile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      recordVar = (Structure) oldFile.findVariable("record");
      /* if (recordVar != null) {
        Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
        if (!result)
          recordVar = null;
      }
      } */ // create new file with current set of objects
 spiw . create ( location , ncfile , extraHeader , preallocateSize , isLargeFile ) ; spiw . setFill ( fill ) ; //isClosed = false;
 /* wait till header is written before adding the record variable to the file
    if (recordVar != null) {
      Boolean result = (Boolean) spiw.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
      if (!result)
        recordVar = null;
    } */ FileWriter2 fileWriter2 = new FileWriter2 ( this ) ; for ( Variable v : ncfile . getVariables ( ) ) { String oldVarName = v . getFullName ( ) ; Variable oldVar = oldFile . findVariable ( oldVarName ) ; if ( oldVar != null ) { fileWriter2 . copyAll ( oldVar , v ) ; } else if ( varRenameMap . containsKey ( oldVarName ) ) { // var name has changed in ncfile - use the varRenameMap to find
 //  the correct variable name to request from oldFile
 String realOldVarName = varRenameMap . get ( oldVarName ) ; oldVar = oldFile . findVariable ( realOldVarName ) ; if ( oldVar != null ) { fileWriter2 . copyAll ( oldVar , v ) ; } } else { String message = "Cannot find variable " + oldVarName + " to copy to new file." ; log . warn ( message ) ; System . out . println ( message ) ; } } // delete old
 oldFile . close ( ) ; if ( ! tmpFile . delete ( ) ) throw new RuntimeException ( "Cant delete " + tmpFile . getAbsolutePath ( ) ) ; }
rewrite entire file
rewrite entire file