/** @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();
  }