public static int writeVarEntry(PrintWriter PW, String name, String type, int varID) {
   PW.println("    _var = new XMLTag(\"var\");");
   PW.println("    _var.addTag(new XMLTag(\"id\",\"" + (varID++) + "\"));");
   PW.println("    _var.addTag(new XMLTag(\"name\",\"" + StringTools.nonSpace(name) + "\"));");
   PW.println("    _var.addTag(new XMLTag(\"desc\",\"" + name + "\"));");
   PW.println("    _var.addTag(new XMLTag(\"type\",\"" + type + "\"));");
   PW.println("    _fg.addTag(_var);");
   return varID;
 }
  public static void writeAuditInit(
      XMLTag oo, PrintWriter PW, XMLTag fg, ArrayList deathReasons, XMLTag theModel) {
    PW.println("  public static void _initialiseAuditOutput() {");
    PW.println("    XMLTag _fg;");
    PW.println("    XMLTag o;");
    // String speciesName = StringTools.nonSpace(speciesText);
    XMLTag[] species = theModel.getTag("species").getTags("species");
    boolean found = false;
    for (int i = 0; i < species.length; i++) {
      XMLTag speciesTag = theModel.getTag("species").getTags("species")[i];
      String speciesText = speciesTag.getAttribute("name");
      XMLTag theAuditLog = oo.getTagWhere("audit", "@species", speciesText);
      XMLTag[] theVars = theAuditLog.getTags("var");
      if (theVars.length > 0) {
        found = true;
        i = species.length;
      }
    }
    if (found) PW.println("    XMLTag _var;");
    for (int i = 0; i < species.length; i++) {
      XMLTag speciesTag = theModel.getTag("species").getTags("species")[i];
      if (speciesTag.getAttribute("fg").equals(fg.getValue("name"))) {
        String speciesText = speciesTag.getAttribute("name");
        String speciesName = StringTools.nonSpace(speciesText);
        PW.println("    _fg = new XMLTag(\"functionalgroup\");");
        PW.println("    _fg.addTag(new XMLTag(\"data\",\"fg_" + speciesName + ".bin\"));");
        PW.println(
            "    Logging.setFgFile("
                + String.valueOf(i)
                + ",Launcher.JarPath+File.separator+\"fg_"
                + speciesName
                + ".bin\");");
        PW.println(
            "    Logging.setFgTime("
                + String.valueOf(i)
                + ",Launcher.JarPath+File.separator+\"fg_"
                + speciesName
                + "_time.bin\");");
        if ((oo.getTag("lineage").getAttribute("active").equals("true"))) {
          PW.println(
              "    Logging.setLineage("
                  + String.valueOf(i)
                  + ",\"lineage_"
                  + speciesName
                  + ".bin\");");
          PW.println(
              "    _fg.addTag(new XMLTag(\"lineage\",\"lineage_" + speciesName + ".bin\"));");
        }
        if ((oo.getTag("lifespan").getAttribute("active").equals("true"))) {
          PW.println(
              "    Logging.setLifespan("
                  + String.valueOf(i)
                  + ",\"lifespan_"
                  + speciesName
                  + ".bin\");");
          PW.println(
              "    _fg.addTag(new XMLTag(\"lifespan\",\"lifespan_" + speciesName + ".bin\"));");
        }
        PW.println("    _fg.addTag(new XMLTag(\"name\",\"" + speciesText + "\"));");
        PW.println("    _fg.addTag(new XMLTag(\"time\",\"fg_" + speciesName + "_time.bin\"));");
        PW.println("    _fg.addTag(new XMLTag(\"type\",\"" + String.valueOf(i) + "\"));");
        PW.println("    _fg.addTag(new XMLTag(\"zip\",\"true\"));");

        PW.println("    o = new XMLTag(\"output\");");
        XMLTag ooSpecific = oo.getTagWhere("audit", "@species", speciesText);
        PW.println("    o.addTag(new XMLTag(\"after\",String.valueOf(SysVars._firstExecTime)));");
        PW.println(
            "    o.addTag(new XMLTag(\"freq\",\"" + ooSpecific.getAttribute("freq") + "\"));");
        PW.println("    o.addTag(new XMLTag(\"to\",\"" + ooSpecific.getAttribute("to") + "\"));");
        PW.println("    _fg.addTag(o);");
        handleVariables(DEFINITIONS, oo, PW, i, deathReasons, theModel);
        PW.println("    Logging.dataFormats.addTag(_fg);");
        PW.println("    Logging.dataFormats.write();");
      }
    }
    PW.println("  }");
  }
  public static void handleVariables(
      int mode, XMLTag oo, PrintWriter PW, int speciesNo, ArrayList deathReasons, XMLTag theModel) {
    int varID = 4;
    XMLTag speciesTag = theModel.getTag("species").getTags("species")[speciesNo];
    String speciesText = speciesTag.getAttribute("name");
    // String speciesName = StringTools.nonSpace(speciesText);
    int auditSpeciesNo = speciesNo;
    XMLTag fgTag = theModel.getTagWhere("functionalgroup", "name", speciesTag.getAttribute("fg"));
    XMLTag theAuditLog = oo.getTagWhere("audit", "@species", speciesText);

    XMLTag[] theVars = theAuditLog.getTags("var");
    if (theVars.length > 0) {
      if (mode == DEFINITIONS) {
        PW.println("    _var = new XMLTag(\"var\");");
        PW.println("    _var.addTag(new XMLTag(\"id\",\"1\"));");
        PW.println("    _var.addTag(new XMLTag(\"name\",\"TimeStep\"));");
        PW.println("    _var.addTag(new XMLTag(\"type\",\"long\"));");
        PW.println("    _fg.addTag(_var);");
        PW.println("    _var = new XMLTag(\"var\");");
        PW.println("    _var.addTag(new XMLTag(\"id\",\"2\"));");
        PW.println("    _var.addTag(new XMLTag(\"name\",\"id\"));");
        PW.println("    _var.addTag(new XMLTag(\"type\",\"long\"));");
        PW.println("    _fg.addTag(_var);");
        PW.println("    _var = new XMLTag(\"var\");");
        PW.println("    _var.addTag(new XMLTag(\"id\",\"3\"));");
        PW.println("    _var.addTag(new XMLTag(\"name\",\"Stage\"));");
        PW.println("    _var.addTag(new XMLTag(\"desc\",\"State\"));");
        PW.println("    _var.addTag(new XMLTag(\"type\",\"int\"));");
        PW.println("    _fg.addTag(_var);");
      } else {
        long auditFreq = SysVarsCompiler.getFreqTS(theModel, theAuditLog);
        PW.println(
            "    if ((log) && (SysVars._stageLogs[params._type][_CurrentStage]) && (Logging.isTime("
                + auditFreq
                + "L,"
                + theAuditLog.getAttribute("from")
                + "L,"
                + theAuditLog.getAttribute("to")
                + "L))) {");
        PW.println(
            "      Logging.writeFGLong(" + String.valueOf(auditSpeciesNo) + ",Kernel.myTime);");
        PW.println("      Logging.writeFGLong(" + String.valueOf(auditSpeciesNo) + ",id);");
        PW.println(
            "      Logging.writeFGInt(" + String.valueOf(auditSpeciesNo) + ",_CurrentStage);");
      }
      for (int i = 0; i < theVars.length; i++) {
        XMLTag var = theVars[i];
        String varName = var.getAttribute("name");
        String scope = var.getAttribute("scope");

        if (scope.equals(OutputDialog2.SCOPE_AT_AGENT)) {
          String realVar = varName.substring(0, varName.indexOf("("));
          if (mode == LOGGING)
            PW.println("      Logging.writeFGReal(" + auditSpeciesNo + "," + realVar + "[1]);");
          else varID = writeVarEntry(PW, realVar, "real", varID);

        } else if (scope.equals(OutputDialog2.SCOPE_AT_INCHEMICAL)) {
          String chemicalName = var.getAttribute("chem");
          String poolOrGain = var.getAttribute("chemtype");

          if (poolOrGain.equals("pool")) {
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",_"
                      + chemicalName
                      + "_Pool_Old);");
            else varID = writeVarEntry(PW, varName, "real", varID);
          } else if (poolOrGain.equals("gain")) {
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal(" + auditSpeciesNo + ",_" + chemicalName + "_Ing);");
            else varID = writeVarEntry(PW, varName, "real", varID);
          }

        } else if (scope.equals(OutputDialog2.SCOPE_AT_BIOLOGICAL)) { // Always a concentration
          final String species = var.getAttribute("species");
          final String stage = var.getAttribute("stage");
          final XMLTag[] speciesTags = theModel.getTag("species").getTags("species");
          int targetSpeciesNo = 0;
          while (!speciesTags[targetSpeciesNo].getAttribute("name").equals(species))
            targetSpeciesNo++;
          final XMLTag[] stages =
              theModel
                  .getTagWhere(
                      "functionalgroup", "name", speciesTags[targetSpeciesNo].getAttribute("fg"))
                  .getTags("stage");
          int targetStageNo = 0;
          while (!stages[targetStageNo].getValue("name").equals(stage)) targetStageNo++;
          if (mode == LOGGING)
            PW.println(
                "      Logging.writeFGReal("
                    + auditSpeciesNo
                    + ",Utils.getStar(blayer,"
                    + targetSpeciesNo
                    + ", "
                    + targetStageNo
                    + "));");
          else varID = writeVarEntry(PW, varName, "real", varID);

        } else if (scope.equals(OutputDialog2.SCOPE_AT_CHEMICAL)) {
          final String chemicalName = var.getAttribute("chem");
          final String chemType = var.getAttribute("chemtype");
          final XMLTag[] chems = theModel.getTags("chemical");
          int targetChem = 0;
          while (!chems[targetChem].getValue("name").equals(chemicalName)) targetChem++;

          if (chemType.equals(OutputDialog2.DEF_SOLN)) {
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",blayer.solution_chem["
                      + targetChem
                      + "]);");
            else varID = writeVarEntry(PW, varName, "real", varID);
          } else if (chemType.equals(OutputDialog2.DEF_BIOCONC)) {
            if ((var.getAttribute("species") != null) && (var.getAttribute("stage") != null)) {
              if (mode == LOGGING) {
                final String chemName = var.getAttribute("chem");
                final String species = var.getAttribute("species");
                final String stage = var.getAttribute("stage");
                final String fg =
                    theModel
                        .getTag("species")
                        .getTagWhere("species", "@name", species)
                        .getAttribute("fg");
                int specNo = 0;
                while (!theModel
                    .getTag("species")
                    .getTags()[specNo]
                    .getAttribute("name")
                    .equals(species)) specNo++;
                int stageNo = 0;
                while (!theModel
                    .getTagWhere("functionalgroup", "name", fg)
                    .getTags("stage")[stageNo]
                    .getValue("name")
                    .equals(stage)) stageNo++;
                PW.println(
                    "        Logging.writeFGReal("
                        + auditSpeciesNo
                        + ",blayer.sumFGPool_"
                        + chemName
                        + "("
                        + specNo
                        + ","
                        + stageNo
                        + "));");
              } else varID = writeVarEntry(PW, varName, "real", varID);
            } else {
              final String chemName = var.getAttribute("chem");
              if (mode == LOGGING)
                PW.println(
                    "      Logging.writeFGReal("
                        + auditSpeciesNo
                        + ",blayer.sumFGPool_"
                        + chemName
                        + "());");
              else varID = writeVarEntry(PW, varName, "real", varID);
            }
          } else if (chemType.equals(OutputDialog2.DEF_ING_BY)) {
            if (mode == LOGGING) {
              if ((var.getAttribute("stage") == null) && (var.getAttribute("species") == null)) {
                PW.println(
                    "        Logging.writeFGReal("
                        + auditSpeciesNo
                        + ",blayer.sumFGIng_"
                        + chemicalName
                        + "();");
              } else {
                String byStage = var.getAttribute("stage");
                String bySpecies = var.getAttribute("species");
                int bySpecNo = 0;
                while (!theModel
                    .getTag("species")
                    .getTags()[bySpecNo]
                    .getAttribute("name")
                    .equals(bySpecies)) bySpecNo++;
                String fg =
                    theModel
                        .getTag("species")
                        .getTagWhere("species", "@name", bySpecies)
                        .getAttribute("fg");
                int fromStageNo = 0;
                while (!theModel
                    .getTagWhere("functionalgroup", "name", fg)
                    .getTags("stage")[fromStageNo]
                    .getValue("name")
                    .equals(byStage)) fromStageNo++;
              }
            } else varID = writeVarEntry(PW, varName, "real", varID);

          } else if (chemType.equals(OutputDialog2.DEF_ING_FROM)) {
            if (mode == LOGGING) {
              final String fromSpecies = StringTools.nonSpace(var.getAttribute("fromspecies"));
              final String fromStage = StringTools.nonSpace(var.getAttribute("fromstage"));
              if ((var.getAttribute("byall") != null)
                  && (var.getAttribute("byall").equals("true"))) {
                PW.print(
                    "        Logging.writeFGReal("
                        + auditSpeciesNo
                        + ",blayer._"
                        + chemicalName
                        + "_from_");
                PW.println(fromSpecies + "_" + fromStage + "_by_all);");

              } else {
                final String bySpecies = StringTools.nonSpace(var.getAttribute("byspecies"));
                final String byStage = StringTools.nonSpace(var.getAttribute("bystage"));
                PW.print(
                    "        Logging.writeFGReal("
                        + auditSpeciesNo
                        + ",blayer._"
                        + chemicalName
                        + "_from_");
                PW.println(
                    fromSpecies + "_" + fromStage + "_by_" + bySpecies + "_" + byStage + ");");
              }

            } else varID = writeVarEntry(PW, varName, "real", varID);
          }
        } else if (scope.equals(OutputDialog2.SCOPE_AT_DEMOGRAPHIC)) {
          String species = var.getAttribute("species");
          String stage = var.getAttribute("stage");
          XMLTag[] speciesTags = theModel.getTag("species").getTags("species");
          int targetSpeciesNo = 0;
          while (!speciesTags[targetSpeciesNo].getAttribute("name").equals(species))
            targetSpeciesNo++;
          XMLTag[] stages =
              theModel
                  .getTagWhere(
                      "functionalgroup", "name", speciesTags[targetSpeciesNo].getAttribute("fg"))
                  .getTags("stage");
          int targetStageNo = 0;
          while (!stages[targetStageNo].getValue("name").equals(stage)) targetStageNo++;

          if (varName.startsWith(OutputDialog2.AGENT_NO_AGENTS + " : ")) {
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",blayer.AgentCounts["
                      + targetSpeciesNo
                      + "]["
                      + targetStageNo
                      + "]);");
            else varID = writeVarEntry(PW, varName, "real", varID);

          } else if (varName.startsWith(OutputDialog2.AGENT_BEING_INGESTED_ALL + " : ")) {
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",blayer.SpeciesStateDemography["
                      + targetSpeciesNo
                      + "]["
                      + targetStageNo
                      + "][SysVars.ingestionAll["
                      + targetSpeciesNo
                      + "]]);");
            else varID = writeVarEntry(PW, varName, "real", varID);

          } else if (varName.startsWith(OutputDialog2.AGENT_BEING_INGESTED_BY)) {
            if (mode == LOGGING) {
              String fg =
                  theModel
                      .getTag("species")
                      .getTagWhere("species", "@name", species)
                      .getAttribute("fg");
              int specNo = 0;
              while (!theModel
                  .getTag("species")
                  .getTags()[specNo]
                  .getAttribute("name")
                  .equals(species)) specNo++;
              int stageNo = 0;
              while (!theModel
                  .getTagWhere("functionalgroup", "name", fg)
                  .getTags("stage")[stageNo]
                  .getValue("name")
                  .equals(stage)) stageNo++;
              String predator =
                  varName.substring(OutputDialog2.AGENT_BEING_INGESTED_BY.length() + 3);
              final String predatorSpecies = predator.substring(0, predator.indexOf(":") - 1);
              predator = predator.substring(predator.indexOf(":") + 2);
              final String predatorStage = predator.substring(0, predator.indexOf(":") - 1);
              predator = predator.substring(predator.indexOf(":") + 2);
              final String preySpecies = predator.substring(0, predator.indexOf(":") - 1);
              PW.print(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",blayer.SpeciesStateDemography["
                      + specNo
                      + "]["
                      + stageNo
                      + "][");
              PW.print("SysVars._DR_" + StringTools.nonSpace(preySpecies) + "_");
              PW.print(StringTools.nonSpace(OutputDialog2.AGENT_BEING_INGESTED_BY));
              PW.print(StringTools.nonSpace(predatorSpecies) + "_");
              PW.print(StringTools.nonSpace(predatorStage) + "]);");
            } else varID = writeVarEntry(PW, varName, "real", varID);

          } else {
            String reason = varName.substring(0, varName.indexOf(":") - 1).trim();
            String fgName =
                theModel
                    .getTag("species")
                    .getTagWhere("species", "@name", var.getAttribute("species"))
                    .getAttribute("fg");
            int reasonNo = getDeathReason(fgName, reason, deathReasons);
            if (reasonNo == -1) System.out.println("Error getting death reasons");
            else {
              if (mode == LOGGING)
                PW.println(
                    "      Logging.writeFGReal("
                        + auditSpeciesNo
                        + ",blayer.SpeciesStateDemography["
                        + targetSpeciesNo
                        + "]["
                        + targetStageNo
                        + "]["
                        + reasonNo
                        + "]);");
              else varID = writeVarEntry(PW, varName, "real", varID);
            }
          }

        } else if (scope.equals(OutputDialog2.SCOPE_AT_PHYSICAL)) {
          if (mode == LOGGING) {
            if (varName.equals(OutputDialog2.DENSITY))
              PW.println(
                  "      Logging.writeFGReal(" + auditSpeciesNo + ",Utils.getDensity(z[1]));");
            else if (varName.equals(OutputDialog2.FULL_IRRADIANCE))
              PW.println(
                  "      Logging.writeFGReal(" + auditSpeciesNo + ",Utils.getFullIrrad(z[1]));");
            else if (varName.equals(OutputDialog2.SALINITY))
              PW.println(
                  "      Logging.writeFGReal(" + auditSpeciesNo + ",Utils.getSalinity(z[1]));");
            else if (varName.equals(OutputDialog2.TEMPERATURE))
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",Utils.getPureTemperature(z[1]));");
            else if (varName.equals(OutputDialog2.TEMPADJ))
              PW.println(
                  "      Logging.writeFGReal(" + auditSpeciesNo + ",Utils.getTemperature(z[1]));");
            else if (varName.equals(OutputDialog2.VISIBLE_IRRADIANCE))
              PW.println(
                  "      Logging.writeFGReal(" + auditSpeciesNo + ",Utils.getVisIrrad(z[1]));");
          } else varID = writeVarEntry(PW, varName, "real", varID);

        } else if (scope.equals(OutputDialog2.SCOPE_AT_LOCAL)) {
          if (varName.indexOf("(") >= 0)
            varName = varName.substring(0, varName.indexOf("(") - 1).trim();
          XMLTag findVar = fgTag.getTagWhere("local", "name", varName);
          if (findVar != null) {
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ","
                      + findVar.getValue("name")
                      + ");");
            else varID = writeVarEntry(PW, findVar.getValue("name"), "real", varID);
          }

        } else if (scope.equals(OutputDialog2.SCOPE_AT_VARIETYBASED)) {
          if (varName.indexOf("(") >= 0)
            varName = varName.substring(0, varName.indexOf("(") - 1).trim();
          String species = var.getAttribute("species");
          String stage = var.getAttribute("stage");
          XMLTag[] speciesTags = theModel.getTag("species").getTags("species");
          int targetSpeciesNo = 0;
          while (!speciesTags[targetSpeciesNo].getAttribute("name").equals(species))
            targetSpeciesNo++;
          XMLTag[] stages =
              theModel
                  .getTagWhere(
                      "functionalgroup", "name", speciesTags[targetSpeciesNo].getAttribute("fg"))
                  .getTags("stage");
          int targetStageNo = 0;
          while (!stages[targetStageNo].getValue("name").equals(stage)) targetStageNo++;

          String link = "";
          boolean variableFlag = false;
          String realVar = "";
          if (varName.equals("IngestedCells")) {
            int ingestionID = 0;
            for (int j = 0; j < targetSpeciesNo - 1; j++)
              ingestionID += getFGForSpeciesNo(j, theModel).getTags("stage").length;
            ingestionID += targetStageNo;
            if (mode == LOGGING)
              PW.println(
                  "      Logging.writeFGReal("
                      + auditSpeciesNo
                      + ",IngestedCells["
                      + ingestionID
                      + "]);");
            else varID = writeVarEntry(PW, "Ingested " + stage + " " + species, "real", varID);
          } else {
            String linkString = StringTools.nonSpace(species + "$" + stage);
            if (fgTag.getTagWhere("varietyvariable", "name", varName) != null) {
              link = fgTag.getTagWhere("varietyvariable", "name", varName).getAttribute("link");
              realVar = fgTag.getTagWhere("varietyvariable", "name", varName).getValue("name");
              variableFlag = true;
            } else {
              XMLTag findVar = fgTag.getTagWhere("varietylocal", "name", varName);
              if (findVar != null) {
                link = findVar.getAttribute("link");
                realVar = findVar.getValue("name");
                String foodSetName = speciesText + " : " + link;
                XMLTag[] foods =
                    theModel
                        .getTag("foodsets")
                        .getTagWhere("foodset", "@name", foodSetName)
                        .getTags("food");
                int j = 0;
                boolean found = false;
                while ((j < foods.length) && (!found)) {
                  String potentialFood =
                      StringTools.nonSpace(
                          foods[j].getAttribute("species") + "$" + foods[j].getAttribute("stage"));
                  if (potentialFood.equals(linkString)) found = true;
                  else j++;
                }

                if (mode == LOGGING) {
                  PW.print(
                      "      Logging.writeFGReal("
                          + auditSpeciesNo
                          + ","
                          + realVar
                          + "[params._speciesOfFG]["
                          + j
                          + "]");
                  if (variableFlag) PW.println(".get());");
                  else PW.println(");");
                } else
                  varID =
                      writeVarEntry(PW, realVar + " for " + stage + " " + species, "real", varID);
              }
            }
          }
        }
      }
      if (mode == LOGGING) PW.println("    }");
    }
  }
 public static void writeLoggingJava(String fileName, XMLTag model) {
   try {
     PrintWriter PW = StringTools.OpenOutputFile(fileName);
     PW.println(" package VEW.Sim;");
     PW.println("");
     PW.println("import gnu.trove.TLongHashSet;");
     PW.println("import VEW.Common.Random.*;");
     PW.println("import java.io.*;");
     PW.println("import java.util.zip.*;");
     PW.println("import VEW.Common.XML.*;");
     PW.println("import VEW.Sim.Kernel;");
     PW.println("import VEW.Sim.SysVars;");
     PW.println("");
     PW.println("import java.util.GregorianCalendar;");
     PW.println("import java.util.TimeZone;");
     PW.println("");
     PW.println("public class Logging {");
     PW.println("");
     PW.println("  public static XMLFile dataFormats;");
     PW.println("  private static boolean useFloats = true;");
     PW.println("  private static DataOutputStream wcFile;");
     PW.println("  private static DataOutputStream phFile;");
     PW.println("  private static DataOutputStream biFile;");
     PW.println("  private static DataOutputStream chFile;");
     PW.println("  private static DataOutputStream deFile;");
     PW.println("  private static DataOutputStream[] fgFiles;");
     PW.println("  private static DataOutputStream[] fgTimes;");
     PW.println("  private static DataOutputStream[] lineages;");
     PW.println("  private static DataOutputStream[] lifespans;");
     PW.println("");
     PW.println("  //private static long parishTime = 0;");
     PW.println("  private static TLongHashSet[] auditIDs;");
     PW.println("  private static boolean auditEnabled = false;");
     PW.println("  private static XMLFile auditXMLFile = null;");
     PW.println("  private static long firstSnap = 0;");
     PW.println("  private static long snapSpace = 0;");
     PW.println("  private static boolean newSeed = false;");
     PW.println("  private static boolean useSnapRead = false;");
     PW.println("  private static boolean useSnapWrite = false;");
     PW.println("  public static String snapFile;");
     PW.println("  private static boolean zip = true;");
     PW.println("  public static int snapVersion = 740;");
     PW.println("");
     PW.println("  private static final TimeZone GMTTimeZone = TimeZone.getTimeZone(\"GMT\");");
     PW.println(
         "  private static final GregorianCalendar anyDate = new GregorianCalendar(GMTTimeZone);");
     PW.println(
         "  private static final GregorianCalendar compareDate = new GregorianCalendar(GMTTimeZone);");
     PW.println("");
     PW.println("  private static long[] fgBytesWritten;");
     PW.println("");
     PW.println("  /* Constants for which file category to write */");
     PW.println("");
     PW.println("  public static final byte CREATE = 0;");
     PW.println("  public static final byte PCHANGEPARENT = 1;");
     PW.println("  public static final byte PCHANGECHILD = 2;");
     PW.println("  public static final byte SPLIT = 3;");
     PW.println("  public static final byte MERGE = 4;");
     PW.println("  public static final byte REMOVAL = 5;");
     PW.println("  public static final byte INGEST = 6;");
     PW.println("  public static final byte DIVIDE = 7;");
     PW.println("  public static final byte CHANGE = 8;");
     PW.println("");
     PW.println("  /* New variables for representing fg logging types */");
     PW.println("");
     PW.println("  private static boolean lineage = false;");
     PW.println("  private static boolean lifespan = false;");
     PW.println("  private static long startLSTime = 0;");
     PW.println("  private static long stopLSTime= 0;");
     PW.println("");
     PW.println("");
     PW.println("  public static void setLineage(boolean l, long start, long end) {");
     PW.println("    lineage = l;");
     PW.println("    startLSTime = start;");
     PW.println("    stopLSTime = end;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setLifespan(boolean l, long start, long end) {");
     PW.println("    lifespan = l;");
     PW.println("    startLSTime = start;");
     PW.println("    stopLSTime = end;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static boolean getLineage() { return lineage; }");
     PW.println("  public static boolean getLifespan() { return lifespan; }");
     PW.println("  public static void setZip(boolean b) { zip = b; }");
     PW.println("");
     PW.println("  public static void setFloat(boolean b) {");
     PW.println("    useFloats = b;");
     PW.println(
         "    if (dataFormats.getTag(\"format\")!=null) dataFormats.getTag(\"format\").removeFromParent();");
     PW.println("    if (b) dataFormats.addTag(new XMLTag(\"format\",\"float\")); else");
     PW.println("      dataFormats.addTag(new XMLTag(\"format\",\"double\"));");
     PW.println("    dataFormats.write();");
     PW.println("  }");
     PW.println("");
     PW.println("  /* Complete snapshot of current system state*/");
     PW.println("");
     PW.println("  public static boolean readingFromSnapshot() { return useSnapRead; }");
     PW.println("");
     PW.println("  public static void setSnap(long t1, long t2) {");
     PW.println("    firstSnap = t1;");
     PW.println("    snapSpace = t2;");
     PW.println("    if (t2!=0) useSnapWrite = true;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setSnap(String s, boolean _newSeed) {");
     PW.println("    snapFile = s;");
     PW.println("    useSnapRead = true;");
     PW.println("    newSeed = _newSeed;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void writeSnapshot(String f) {");
     PW.println("    try {");
     PW.println(
         "      BufferedOutputStream buffered = new BufferedOutputStream(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(f))));");
     PW.println("            DataOutputStream snapshot = new DataOutputStream(buffered);");
     PW.println("      final String _snap = new String(\"VEW-CP\");");
     PW.println("      for (int i=0; i<6; i++) snapshot.writeChar(_snap.charAt(i));");
     PW.println("      snapshot.writeInt(snapVersion);");
     PW.println("      snapshot.writeLong(Kernel.myTime);");
     PW.println("      ObjectOutputStream snapObj = new ObjectOutputStream(buffered);");
     PW.println("      snapObj.writeObject(Utils.rnd);");
     PW.println("      snapObj.flush();");
     PW.println("      Kernel.W.writePhysicsSnapshot(snapshot);");
     PW.println("      Kernel.W.writeBiologicalSnapshot(snapshot);");
     PW.println("      snapshot.flush();");
     PW.println("      snapObj.close();");
     PW.println("      snapshot.close();");
     PW.println("    } catch (Exception e) { e.printStackTrace();}");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void readSnapshot() {");
     PW.println("    try {");
     PW.println("      // Empty system");
     PW.println("      Kernel.W.clearSystem();");
     PW.println("      BufferedInputStream buffered = null;");
     PW.println("      if (new File(snapFile).exists()) {");
     PW.println(
         "        buffered = new BufferedInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(snapFile))));");
     PW.println("      } else {");
     PW.println("        String justFile = snapFile;");
     PW.println(
         "        while (justFile.indexOf(\"/\")>=0) justFile=justFile.substring(justFile.indexOf(\"/\")+1);");
     PW.println(
         "        while (justFile.indexOf(\"\\\\\")>=0) justFile=justFile.substring(justFile.indexOf(\"\\\\\")+1);");
     PW.println(
         "        java.util.jar.JarFile JF = new java.util.jar.JarFile(Launcher.JarPath+File.separator+\"VEW.jar\");");
     PW.println("        java.util.jar.JarEntry JE = JF.getJarEntry(justFile);");
     PW.println(
         "        buffered = new BufferedInputStream(new GZIPInputStream(new BufferedInputStream(JF.getInputStream(JE))));");
     PW.println("      }");
     PW.println("      DataInputStream snapshot = new DataInputStream(buffered);");
     PW.println("      StringBuffer s = new StringBuffer();");
     PW.println("      for (int i=0; i<6; i++) s.append(snapshot.readChar());");
     PW.println("      RanMT r = null;");
     PW.println("      if (!s.toString().equals(\"VEW-CP\")) {");
     PW.println("        System.out.println(\"Invalid snapshot\");");
     PW.println("      } else {");
     PW.println("        snapVersion = snapshot.readInt();");
     PW.println("        System.out.println(\"Snapshot version: \"+snapVersion);");
     PW.println("        SysVars._firstExecTime = snapshot.readLong();");
     PW.println("        ObjectInputStream snapObj = new ObjectInputStream(buffered);");
     PW.println("        r = (RanMT)snapObj.readObject();");
     PW.println("        Kernel.W.readPhysicsSnapshot(snapshot);");
     PW.println("        Kernel.W.readBiologicalSnapshot(snapshot);");
     PW.println("        snapObj.close();");
     PW.println("        snapshot.close();");
     PW.println("      }");
     PW.println("      if (!SysVars._seedForced) {");
     PW.println("        if ((newSeed) || (r==null)) {");
     PW.println("          Utils.rnd = new RanMT();");
     PW.println("          Utils.rnd.setSeed(SysVars._seed);");
     PW.println("        } else {");
     PW.println("          Utils.rnd = r;");
     PW.println("        }");
     PW.println("      }");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void writeBioReal(double v) { writeReal(biFile,v); }");
     PW.println("  public static void writeChemReal(double v) { writeReal(chFile,v); }");
     PW.println("  public static void writeDemReal(double v) { writeReal(deFile,v); }");
     PW.println("  public static void writePhyReal(double v) { writeReal(phFile,v); }");
     PW.println("  public static void writeWCReal(double v) { writeReal(wcFile,v); }");
     PW.println("");
     PW.println("  public static void writeFGReal(int fg, double d) {");
     PW.println("    writeReal(fgFiles[fg],d);");
     PW.println("    if (useFloats) fgBytesWritten[fg]+=4;");
     PW.println("    else fgBytesWritten[fg]+=8;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void writeFGLong(int fg, long l) {");
     PW.println("    try {");
     PW.println("      fgFiles[fg].writeLong(l);");
     PW.println("      fgBytesWritten[fg]+=8;");
     PW.println("    } catch (Exception e) {e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void writeFGInt(int fg, int i) {");
     PW.println("    try {");
     PW.println("      fgFiles[fg].writeInt(i);");
     PW.println("      fgBytesWritten[fg]+=4;");
     PW.println("    } catch (Exception e) {e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void  writeReal(DataOutputStream dos, double d) {");
     PW.println("    try {");
     PW.println("      if (useFloats) dos.writeFloat((float)d);");
     PW.println("      else dos.writeDouble(d);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void  writeLong(DataOutputStream dos, long l) {");
     PW.println("    try {");
     PW.println("      dos.writeLong(l);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setLineage(int fg, String file) {");
     PW.println("    if (zip) lineages[fg]=makeZip(file);");
     PW.println("    else lineages[fg]=makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setLifespan(int fg, String file) {");
     PW.println("    if (zip) lifespans[fg]=makeZip(file);");
     PW.println("    else lifespans[fg]=makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setFgTime(int fg, String file) {");
     PW.println("    if (zip) fgTimes[fg]= makeZip(file);");
     PW.println("    else fgTimes[fg] = makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setFgFile(int fg, String file) {");
     PW.println("    if (zip) fgFiles[fg] = makeZip(file);");
     PW.println("    else fgFiles[fg]= makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setWcFile(String file) {");
     PW.println("    if (zip) wcFile = makeZip(file);");
     PW.println("    else wcFile = makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setBioFile(String file) {");
     PW.println("    if (zip) biFile = makeZip(file);");
     PW.println("    else biFile = makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setChemFile(String file) {");
     PW.println("    if (zip) chFile = makeZip(file);");
     PW.println("    else chFile = makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setDemFile(String file) {");
     PW.println("    if (zip) deFile = makeZip(file);");
     PW.println("    else deFile = makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void setPhyFile(String file) {");
     PW.println("    if (zip) phFile = makeZip(file);");
     PW.println("    else phFile = makeNonZip(file);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static DataOutputStream makeZip(String file) {");
     PW.println("    try {");
     PW.println(
         "      return new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)))));");
     PW.println("    } catch (Exception e) { e.printStackTrace(); return null; }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static DataOutputStream makeNonZip(String file) {");
     PW.println("    try {");
     PW.println(
         "      return new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));");
     PW.println("    } catch (Exception e) { e.printStackTrace(); return null; }");
     PW.println("  }");
     PW.println("");
     PW.println("  /* Perform logging */");
     PW.println("");
     PW.println("  public static void setAuditFile(String f) {");
     PW.println("    auditEnabled = true;");
     PW.println("    auditXMLFile = XMLFile.LoadFile(f);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static boolean isAuditEnabled() {");
     PW.println("    return auditEnabled;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static boolean shouldLog(long pid, int fgid) {");
     PW.println("    return auditIDs[fgid].contains(pid);");
     PW.println("  }");
     PW.println("");
     PW.println("  /* Lineage format:");
     PW.println("");
     PW.println("     int  Time (measured in Times!)");
     PW.println("     byte TYPE (constant for create/pchange/split/merge/removal)");
     PW.println("     long ParentID");
     PW.println("     long ChildID");
     PW.println("        // NEW IN 3.1");
     PW.println("     int ParentStage");
     PW.println("     int ChildStage");
     PW.println("");
     PW.println("          -> Merge: new particle assumes ParentID, with new cellcount = C1+C2.");
     PW.println("  */");
     PW.println("");
     PW.println("");
     PW.println(
         "  public static void writeCreateToLineage(int RegisterNo, long Time, long ParentID, long ChildID, int ParentStage, int ChildStage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lineages[RegisterNo].writeLong(Time);");
     PW.println("      lineages[RegisterNo].writeByte(CREATE);");
     PW.println("      lineages[RegisterNo].writeLong(ParentID);");
     PW.println("      lineages[RegisterNo].writeLong(ChildID);");
     PW.println("      lineages[RegisterNo].writeInt(ParentStage);");
     PW.println("      lineages[RegisterNo].writeInt(ChildStage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writePChangeParentToLineage(int RegisterNo, long Time, long ParentID, int ParentStage, int ChildStage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lineages[RegisterNo].writeLong(Time);");
     PW.println("      lineages[RegisterNo].writeByte(PCHANGEPARENT);");
     PW.println("      lineages[RegisterNo].writeLong(ParentID);");
     PW.println("      lineages[RegisterNo].writeLong(-1);");
     PW.println("      lineages[RegisterNo].writeInt(ParentStage);");
     PW.println("      lineages[RegisterNo].writeInt(ChildStage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writePChangeChildToLineage(int RegisterNo, long Time, long ParentID, long ChildID, int ParentStage, int ChildStage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lineages[RegisterNo].writeLong(Time);");
     PW.println("      lineages[RegisterNo].writeByte(PCHANGECHILD);");
     PW.println("      lineages[RegisterNo].writeLong(ParentID);");
     PW.println("      lineages[RegisterNo].writeLong(ChildID);");
     PW.println("      lineages[RegisterNo].writeInt(ParentStage);");
     PW.println("      lineages[RegisterNo].writeInt(ChildStage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeSplitToLineage(int RegisterNo, long Time, long ParentID, long ChildID, int stage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lineages[RegisterNo].writeLong(Time);");
     PW.println("      lineages[RegisterNo].writeByte(SPLIT);");
     PW.println("      lineages[RegisterNo].writeLong(ParentID);");
     PW.println("      lineages[RegisterNo].writeLong(ChildID);");
     PW.println("      lineages[RegisterNo].writeInt(stage);");
     PW.println("      lineages[RegisterNo].writeInt(stage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeMergeToLineage(int RegisterNo, long Time, long ParentID, long ChildID, int stage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lineages[RegisterNo].writeLong(Time);");
     PW.println("      lineages[RegisterNo].writeByte(MERGE);");
     PW.println("      lineages[RegisterNo].writeLong(ParentID);");
     PW.println("      lineages[RegisterNo].writeLong(ChildID);");
     PW.println("      lineages[RegisterNo].writeInt(stage);");
     PW.println("      lineages[RegisterNo].writeInt(stage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeRemovalToLineage(int RegisterNo, long Time, long ParentID, int stage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lineages[RegisterNo].writeLong(Time);");
     PW.println("      lineages[RegisterNo].writeByte(REMOVAL);");
     PW.println("      lineages[RegisterNo].writeLong(ParentID);");
     PW.println("      lineages[RegisterNo].writeLong(0);");
     PW.println("      lineages[RegisterNo].writeInt(stage);");
     PW.println("      lineages[RegisterNo].writeInt(stage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  /* LifeSpan Register format:");
     PW.println("");
     PW.println("        int  Time");
     PW.println("        byte TYPE");
     PW.println("        int Reason");
     PW.println("        long ID1");
     PW.println("        long ID2");
     PW.println("        f/d  CellCount1");
     PW.println("        f/d  CellCount2");
     PW.println("");
     PW.println("           -> for ingest/divide, CellCount1 = initial, CellCount2 = after");
     PW.println("           -> for change, CellCount2 = (f/d) stageNumber.");
     PW.println(" */");
     PW.println("");
     PW.println(
         "  public static void writeCreateToLifespan(int RegisterNo, int Reason, long Time, long ParentID, long ChildID, double childC, int stage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(CREATE);");
     PW.println("      lifespans[RegisterNo].writeInt(Reason);");
     PW.println("      lifespans[RegisterNo].writeLong(ParentID);");
     PW.println("      lifespans[RegisterNo].writeLong(ChildID);");
     PW.println("      writeReal(lifespans[RegisterNo],childC);");
     PW.println("      writeReal(lifespans[RegisterNo],stage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writePChangeParentToLifespan(int RegisterNo, int Reason, long Time, long ParentID, double childC, int stage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(PCHANGEPARENT);");
     PW.println("      lifespans[RegisterNo].writeInt(Reason);");
     PW.println("      lifespans[RegisterNo].writeLong(ParentID);");
     PW.println("      lifespans[RegisterNo].writeLong(-1);");
     PW.println("      writeReal(lifespans[RegisterNo],childC);");
     PW.println("      writeReal(lifespans[RegisterNo],stage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writePChangeChildToLifespan(int RegisterNo, long Time, long ParentID, long ChildID, double childC, int stage) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(PCHANGECHILD);");
     PW.println(
         "      lifespans[RegisterNo].writeInt(-1); // No reason available - multiple reasons merged.");
     PW.println("      lifespans[RegisterNo].writeLong(ParentID);");
     PW.println("      lifespans[RegisterNo].writeLong(ChildID);");
     PW.println("      writeReal(lifespans[RegisterNo],childC); // Total new cell count.");
     PW.println("      writeReal(lifespans[RegisterNo],stage);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeSplitToLifespan(int RegisterNo, long Time, long ParentID, long ChildID, double childC) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(SPLIT);");
     PW.println("      lifespans[RegisterNo].writeInt(-1);");
     PW.println("      lifespans[RegisterNo].writeLong(ParentID);");
     PW.println("      lifespans[RegisterNo].writeLong(ChildID);");
     PW.println("      writeReal(lifespans[RegisterNo],childC);");
     PW.println("      writeReal(lifespans[RegisterNo],0.0);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeMergeToLifespan(int RegisterNo, long Time, long ParentID, long ChildID, double childC1, double childC2) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(MERGE);");
     PW.println("      lifespans[RegisterNo].writeInt(-1);");
     PW.println("      lifespans[RegisterNo].writeLong(ParentID);");
     PW.println("      lifespans[RegisterNo].writeLong(ChildID);");
     PW.println("      writeReal(lifespans[RegisterNo],childC1);");
     PW.println("      writeReal(lifespans[RegisterNo],childC2);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeRemovalToLifespan(int RegisterNo, int Reason, long Time, long ParentID, double childC1) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(REMOVAL);");
     PW.println("      lifespans[RegisterNo].writeInt(Reason);");
     PW.println("      lifespans[RegisterNo].writeLong(ParentID);");
     PW.println("      lifespans[RegisterNo].writeLong(-1);");
     PW.println("      writeReal(lifespans[RegisterNo],childC1);");
     PW.println("      writeReal(lifespans[RegisterNo],-1);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeIngestToLifespan(int RegisterNo, int Reason, long Time, long ID, double C1, double C2) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(INGEST);");
     PW.println("      lifespans[RegisterNo].writeInt(Reason);");
     PW.println("      lifespans[RegisterNo].writeLong(ID);");
     PW.println("      lifespans[RegisterNo].writeLong(0);");
     PW.println("      writeReal(lifespans[RegisterNo],C1);");
     PW.println("      writeReal(lifespans[RegisterNo],C2);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeDivideToLifespan(int RegisterNo, int Reason, long Time, long ID, double C1, double C2) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(DIVIDE);");
     PW.println("      lifespans[RegisterNo].writeInt(Reason);");
     PW.println("      lifespans[RegisterNo].writeLong(ID);");
     PW.println("      lifespans[RegisterNo].writeLong(0);");
     PW.println("      writeReal(lifespans[RegisterNo],C1);");
     PW.println("      writeReal(lifespans[RegisterNo],C2);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println(
         "  public static void writeChangeToLifespan(int RegisterNo, int Reason, long Time, long ID, double C1, int S) {");
     PW.println("    if ((Time>=startLSTime) && (Time<=stopLSTime)) try {");
     PW.println("      lifespans[RegisterNo].writeLong(Time);");
     PW.println("      lifespans[RegisterNo].writeByte(CHANGE);");
     PW.println("      lifespans[RegisterNo].writeInt(Reason);");
     PW.println("      lifespans[RegisterNo].writeLong(ID);");
     PW.println("      lifespans[RegisterNo].writeLong(0);");
     PW.println("      writeReal(lifespans[RegisterNo],C1);");
     PW.println("      writeReal(lifespans[RegisterNo],S);");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static boolean isYearlyTime(long freq, long after, long until) {");
     PW.println("    // True if the current time is between after and until, but disregard year.");
     PW.println("    anyDate.setTimeInMillis(after);");
     PW.println("    anyDate.set(GregorianCalendar.YEAR,1960);");
     PW.println("    final long start1960=anyDate.getTimeInMillis();");
     PW.println("    anyDate.setTimeInMillis(until);");
     PW.println("    anyDate.set(GregorianCalendar.YEAR,1960);");
     PW.println("    final long end1960=anyDate.getTimeInMillis();");
     PW.println("    anyDate.setTimeInMillis(Kernel.myTime);");
     PW.println("    anyDate.set(GregorianCalendar.YEAR,1960);");
     PW.println("    final long now1960=anyDate.getTimeInMillis();");
     PW.println("    if ((now1960<start1960) || (now1960>end1960)) return false;");
     PW.println(
         "    else return (((Kernel.myTime-SysVars.startTime) % (freq * SysVars.stepInMillis))==0);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void writeTimes() {");
     PW.println("    for (int i=0; i<fgTimes.length; i++)");
     PW.println("      if (fgTimes[i]!=null) writeLong(fgTimes[i],fgBytesWritten[i]);");
     PW.println("  }");
     PW.println("");
     PW.println("  public static String unitToTex(String u) {");
     PW.println("    String pre = \"\";");
     PW.println("    String code = \"\";");
     PW.println("    String exp = \"\";");
     PW.println("    String theunit = \"\";");
     PW.println("    while (u.length()>0) {");
     PW.println("      pre = u.substring(0,u.indexOf(\",\"));");
     PW.println("      u = u.substring(u.indexOf(\",\")+1);");
     PW.println("      code = u.substring(0,u.indexOf(\",\"));");
     PW.println("      u = u.substring(u.indexOf(\",\")+1);");
     PW.println("      if (u.indexOf(\",\")>=1) {");
     PW.println("        exp = u.substring(0,u.indexOf(\",\"));");
     PW.println("        u= u.substring(u.indexOf(\",\")+1);");
     PW.println("      } else {");
     PW.println("        exp = u;");
     PW.println("        u = \"\";");
     PW.println("      }");
     PW.println("      if (pre.equals(\"-12\")) pre = \"p\";");
     PW.println("      else if (pre.equals(\"-9\")) pre = \"n\";");
     PW.println("      else if (pre.equals(\"-6\")) pre = \"\\\\mu\";");
     PW.println("      else if (pre.equals(\"-3\")) pre = \"m\";");
     PW.println("      else if (pre.equals(\"12\")) pre = \"A\";");
     PW.println("      else if (pre.equals(\"9\")) pre = \"G\";");
     PW.println("      else if (pre.equals(\"6\")) pre = \"M\";");
     PW.println("      else if (pre.equals(\"3\")) pre = \"K\";");
     PW.println("      else pre = \"\";");
     PW.println("      if (!exp.equals(\"1\")) exp = \"{\"+exp+\"}\";");
     PW.println("      if (exp.equals(\"1\")) exp = \"\";");
     PW.println("      if (!exp.equals(\"9\")) theunit +=pre+code+exp;");
     PW.println("");
     PW.println("    }");
     PW.println("    return theunit;");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void checkWriteSnap() {");
     PW.println("    if (useSnapWrite) {");
     PW.println(
         "      if ((Kernel.myTime>=firstSnap) && (((Kernel.myTime-firstSnap)%snapSpace)==0)) {");
     PW.println("        writeSnapshot(Launcher.JarPath+File.separator+\"snap.\"+Kernel.myTime);");
     PW.println("        //System.out.println(\"Snap shot write : myTime = \"+Kernel.myTime);");
     PW.println("        //System.out.println(\"FIRST SNAP: \"+firstSnap);");
     PW.println("        //System.out.println(\"snapSPACE = \"+snapSpace);");
     PW.println(
         "        //System.out.println(\"Timestep = \"+Kernel.timeSteps+\" just written\");");
     PW.println("      }");
     PW.println("    }");
     PW.println("  }");
     PW.println("");
     PW.println(" public static boolean isTime(long freq, long after, long until) {");
     PW.println("   if ((Kernel.myTime<after) || (Kernel.myTime>until)) return false;");
     PW.println("   else return (((Kernel.myTime-SysVars.startTime) % freq)==0);");
     PW.println(" }");
     PW.println("");
     PW.println(" public static boolean isYearlyInterval(int years, long after, long until) {");
     PW.println("   if ((Kernel.myTime<after) || (Kernel.myTime>until)) return false;");
     PW.println("   else if (Kernel.myTime==after) return true;");
     PW.println("   else {");
     PW.println("     compareDate.setTimeInMillis(after);");
     PW.println("     long compareMillis = Kernel.myTime;");
     PW.println("     while (compareMillis<=until) {");
     PW.println("       compareDate.add(GregorianCalendar.YEAR,years);");
     PW.println("       compareMillis=compareDate.getTimeInMillis();");
     PW.println("       if (Kernel.myTime==compareMillis) return true;");
     PW.println("     }");
     PW.println("   }");
     PW.println("   return false;");
     PW.println(" }");
     PW.println("");
     PW.println("  public static void update() {");
     PW.println("    writeTimes();");
     PW.println("    SysVars.logPhyFieldData();");
     PW.println("    SysVars.logBioFieldData();");
     PW.println("    SysVars.logChemFieldData();");
     PW.println("    SysVars.logDemFieldData();");
     PW.println("    SysVars.logColData();");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void init(int speciesCount) {");
     PW.println("    fgBytesWritten = new long[speciesCount];");
     PW.println(
         "    dataFormats = new XMLFile(Launcher.JarPath+File.separator+\"DataFormats.xml\",\"dataformat\");");
     PW.println("    dataFormats.addTag(new XMLTag(\"version\",\"1.1\"));");
     PW.println("");
     PW.println("    /* Initialise IDs for audits */");
     PW.println("");
     PW.println("    if (Logging.isAuditEnabled()) {");
     PW.println("      auditIDs = new TLongHashSet[speciesCount];");
     PW.println("      for (int i=0; i<speciesCount; i++) {");
     PW.println("        String name = Kernel.getSpecies(i).name();");
     PW.println("        TLongHashSet ids = new TLongHashSet();");
     PW.println(
         "        XMLTag fgAudit = auditXMLFile.getTagWhere(\"functionalgroup\", \"name\", name);");
     PW.println("        if (fgAudit != null) {");
     PW.println("          XMLTag[] idList = fgAudit.getTags(\"group/id\");");
     PW.println("          if (idList != null) {");
     PW.println("            for (int j = 0; j < idList.length; j++) {");
     PW.println("              ids.add(Integer.parseInt(idList[j].getValue()));");
     PW.println("            }");
     PW.println("          }");
     PW.println("");
     PW.println("          idList = fgAudit.getTags(\"group/descendants/id\");");
     PW.println("          if (idList != null) {");
     PW.println("            for (int j = 0; j < idList.length; j++) {");
     PW.println("              ids.add(Integer.parseInt(idList[j].getValue()));");
     PW.println("            }");
     PW.println("          }");
     PW.println("        }");
     PW.println("");
     PW.println("        auditIDs[i] = ids;");
     PW.println("      }");
     PW.println("    }");
     PW.println("");
     PW.println("    fgFiles = new DataOutputStream[speciesCount];");
     PW.println("    fgTimes = new DataOutputStream[speciesCount];");
     PW.println("    if (lineage) lineages = new DataOutputStream[speciesCount];");
     PW.println("    if (lifespan) lifespans = new DataOutputStream[speciesCount];");
     PW.println("    dataFormats.addTag(\"deathreasons\");");
     PW.println("    dataFormats.addTag(\"states\");");
     PW.println("    SysVars.InitDeathReasons(dataFormats.getTag(\"dataformat/deathreasons\"));");
     PW.println("    SysVars.InitStates(dataFormats.getTag(\"dataformat/states\"));");
     PW.println("    SysVars.initColData();");
     PW.println("    SysVars.initBioFieldData();");
     PW.println("    SysVars.initChemFieldData();");
     PW.println("    SysVars.initDemFieldData();");
     PW.println("    SysVars.initPhyFieldData();");
     PW.println("    dataFormats.write();");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void flush() {");
     PW.println("    try {");
     PW.println("      if (wcFile!=null) wcFile.flush();");
     PW.println("      if (phFile!=null) phFile.flush();");
     PW.println("      if (biFile!=null) biFile.flush();");
     PW.println("      if (deFile!=null) deFile.flush();");
     PW.println("      if (chFile!=null) chFile.flush();");
     PW.println("      for (int i=0; i<fgFiles.length; i++) {");
     PW.println("        if (fgFiles[i]!=null) fgFiles[i].flush();");
     PW.println("        if (fgTimes[i]!=null) fgTimes[i].flush();");
     PW.println("      }");
     PW.println(
         "      if (lifespan) for (int i=0; i<lifespans.length; i++) lifespans[i].flush();");
     PW.println("      if (lineage) for (int i=0; i<lineages.length; i++) lineages[i].flush();");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("");
     PW.println("  public static void close() {");
     PW.println("    try {");
     PW.println("      flush();");
     PW.println("      if (wcFile!=null) wcFile.close();");
     PW.println("      if (phFile!=null) phFile.close();");
     PW.println("      if (biFile!=null) biFile.close();");
     PW.println("      if (deFile!=null) deFile.close();");
     PW.println("      if (chFile!=null) chFile.close();");
     PW.println("      for (int i=0; i<fgFiles.length; i++) {");
     PW.println("        if (fgFiles[i]!=null) fgFiles[i].close();");
     PW.println("        if (fgTimes[i]!=null) fgTimes[i].close();");
     PW.println("      }");
     PW.println(
         "      if (lifespan) for (int i=0; i<lifespans.length; i++) lifespans[i].close();");
     PW.println("      if (lineage) for (int i=0; i<lineages.length; i++) lineages[i].close();");
     PW.println("    } catch (Exception e) { e.printStackTrace(); }");
     PW.println("  }");
     PW.println("}");
     PW.flush();
     PW.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }