/**
   * Ensure there is a tmp folder for the intermediary file, and it only contains the original,
   * unedited population.
   */
  private static void setup(String outputFolder, String censusFolder) {
    /* Empty and create a temporary folder. */
    File tmpFolder = new File(outputFolder + "tmp/");
    if (tmpFolder.exists()) {
      LOG.warn("Temporary folder will be deleted!");
      LOG.warn("Deleting " + tmpFolder.getAbsolutePath());
      FileUtils.delete(tmpFolder);
    }
    tmpFolder.mkdirs();

    /* Copy the population from then Treasury 2014 data. */
    try {
      FileUtils.copyFile(
          new File(censusFolder + "population_withPlans.xml.gz"),
          new File(outputFolder + "tmp/persons.xml.gz"));
      FileUtils.copyFile(
          new File(censusFolder + "populationAttributes.xml.gz"),
          new File(outputFolder + "tmp/personAttributes.xml.gz"));
      FileUtils.copyFile(
          new File(censusFolder + "households.xml.gz"),
          new File(outputFolder + "tmp/households.xml.gz"));
      FileUtils.copyFile(
          new File(censusFolder + "householdAttributes_withPlanHome.xml.gz"),
          new File(outputFolder + "tmp/householdAttributes.xml.gz"));
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Cannot copy base treasure files.");
    }
  }
  public static void main(String[] args) {
    Header.printHeader(RunCapeTownFreight.class.toString(), args);

    String folder = args[0] + (args[0].endsWith("/") ? "" : "/");
    Machine machine = Machine.valueOf(args[1]);

    /* Check if output folder exists, and DELETE if it is there. */
    File f = new File(folder + "output/");
    if (f.exists() && f.isDirectory()) {
      LOG.warn("Deleting the output folder " + folder + "output/");
      FileUtils.delete(f);
    }

    /* Set up the simulation run. */
    Config config = setupConfig(folder, machine);
    Scenario sc = setupScenario(config);
    Controler controler = setupControler(sc);

    controler.run();

    Header.printFooter();
  }
  /** @param args */
  public static void main(String[] args) {
    Header.printHeader(ExtractVehiclesForSollyFrans.class.toString(), args);

    String path = "/home/jwjoubert/Documents/data/Digicore/ByMonth/201403/SollyFrans/xml/";
    String output = "/home/jwjoubert/Documents/data/Digicore/ByMonth/201403/SollyFrans/chains.csv";
    List<File> list =
        FileUtils.sampleFiles(new File(path), 100, FileUtils.getFileFilter(".xml.gz"));

    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation("WGS84_SA_Albers", "WGS84");

    BufferedWriter bw = IOUtils.getBufferedWriter(output);
    try {
      bw.write("Vehicle,Chain,Activity,X,Y,Long,Lat,Start,End");
      bw.newLine();

      for (File f : list) {
        DigicoreVehicleReader_v1 dvr = new DigicoreVehicleReader_v1();
        dvr.readFile(f.getAbsolutePath());
        DigicoreVehicle v = dvr.getVehicle();
        bw.write(v.getId().toString());
        bw.write(",");

        int chain = 1;
        for (DigicoreChain c : v.getChains()) {
          int act = 1;
          for (DigicoreActivity a : c.getAllActivities()) {
            Coord coord = a.getCoord();
            Coord coordWgs84 = ct.transform(coord);

            String s =
                String.format(
                    "%s,%d,%d,%.0f,%.0f,%.6f,%.6f,%s,%s\n",
                    v.getId().toString(),
                    chain,
                    act,
                    coord.getX(),
                    coord.getY(),
                    coordWgs84.getX(),
                    coordWgs84.getY(),
                    getNiceDate(a.getStartTimeGregorianCalendar()),
                    getNiceDate(a.getEndTimeGregorianCalendar()));
            bw.write(s);
            act++;
          }
          chain++;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Cannot write to " + output);
    } finally {
      try {
        bw.close();
      } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Cannot close " + output);
      }
    }

    Header.printFooter();
  }