SOLbuilder.java [src/java/d/util] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package d.util;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Iterator;

/**
 *
 * @author sidereus
 */
public class SOLbuilder {

  DecimalFormat df1 = new DecimalFormat("0.00");

  private String first(String id_soil) {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append("*");
    strbuild.append(id_soil);
    return strbuild.toString();
  }

  private String init() {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append("@SITE");
    strbuild.append(String.format("%10s", "COUNTRY"));
    strbuild.append(String.format("%10s", "LAT"));
    strbuild.append(String.format("%10s", "LONG"));
    strbuild.append(String.format("%13s", "SCS FAMILY"));
    return strbuild.toString();
  }

  private String initInfo(String country, double latitude, double longitude, String family) {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append(" ");
    strbuild.append(String.format("%5s", ""));
    strbuild.append(String.format("%9s", country));
    strbuild.append(String.format("%10s", latitude));
    strbuild.append(String.format("%10s", longitude));
    strbuild.append("   ");
    strbuild.append(String.format("%10s", family));
    return strbuild.toString();
  }

  private String firstHeader() {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append("@");
    strbuild.append(String.format("%6s", "SCOM"));
    strbuild.append(String.format("%7s", "SALB"));
    strbuild.append(String.format("%7s", "SLU1"));
    strbuild.append(String.format("%7s", "SLDR"));
    strbuild.append(String.format("%7s", "SLRO"));
    strbuild.append(String.format("%7s", "SLNF"));
    strbuild.append(String.format("%7s", "SLPF"));
    strbuild.append(String.format("%7s", "SMHB"));
    strbuild.append(String.format("%7s", "SMPX"));
    strbuild.append(String.format("%7s", "SMKE"));
    return strbuild.toString();
  }

  private String firstAVGdata(String scom, double salb, double slu1,
      double sldr, double slro, double slnf,
      double slpf, String smhb, String smpx,
      String smke) {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append(String.format("%7s", scom));
    strbuild.append(String.format("%7s", df1.format(salb)));
    strbuild.append(String.format("%7s", df1.format(slu1)));
    strbuild.append(String.format("%7s", df1.format(sldr)));
    strbuild.append(String.format("%7s", df1.format(slro)));
    strbuild.append(String.format("%7s", df1.format(slnf)));
    strbuild.append(String.format("%7s", df1.format(slpf)));
    strbuild.append(String.format("%7s", smhb));
    strbuild.append(String.format("%7s", smpx));
    strbuild.append(String.format("%7s", smke));
    return strbuild.toString();
  }

  private String secondHeader() {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append("@");
    strbuild.append(String.format("%7s", "SLB"));
    strbuild.append(String.format("%8s", "SLMH"));
    strbuild.append(String.format("%8s", "SLLL"));
    strbuild.append(String.format("%8s", "SDUL"));
    strbuild.append(String.format("%8s", "SSAT"));
    strbuild.append(String.format("%8s", "SRGF"));
    strbuild.append(String.format("%8s", "SSKS"));
    strbuild.append(String.format("%8s", "SBDM"));
    strbuild.append(String.format("%8s", "SLOC"));
    strbuild.append(String.format("%8s", "SLCL"));
    strbuild.append(String.format("%8s", "SLSI"));
    strbuild.append(String.format("%8s", "SLCF"));
    strbuild.append(String.format("%8s", "SLNI"));
    strbuild.append(String.format("%8s", "SLHW"));
    strbuild.append(String.format("%8s", "SLHB"));
    strbuild.append(String.format("%8s", "SCEC"));
    strbuild.append(String.format("%8s", "SDAC"));
    return strbuild.toString();
  }

  private String thirdHeader() {
    StringBuilder strbuild = new StringBuilder();
    strbuild.append("@");
    strbuild.append(String.format("%7s", "SLB"));
    strbuild.append(String.format("%8s", "SLPX"));
    strbuild.append(String.format("%8s", "SLPT"));
    strbuild.append(String.format("%8s", "SLPO"));
    strbuild.append(String.format("%8s", "CACO3"));
    strbuild.append(String.format("%8s", "SLAL"));
    strbuild.append(String.format("%8s", "SLFE"));
    strbuild.append(String.format("%8s", "SLMN"));
    strbuild.append(String.format("%8s", "SLBS"));
    strbuild.append(String.format("%8s", "SLPA"));
    strbuild.append(String.format("%8s", "SLPB"));
    strbuild.append(String.format("%8s", "SLKE"));
    strbuild.append(String.format("%8s", "SLMG"));
    strbuild.append(String.format("%8s", "SLNA"));
    strbuild.append(String.format("%8s", "SLSU"));
    strbuild.append(String.format("%8s", "SLEC"));
    strbuild.append(String.format("%8s", "SLCA"));
    return strbuild.toString();
  }

  private void buildInfo(PrintWriter pw, String country, String id_soil, double latitude, double longitude, String family) {
    pw.println(first(id_soil));
    pw.println(init());
    pw.println(initInfo(country, latitude, longitude, family));
  }

  private void buildHeader(PrintWriter pw, String scom, double salb, double slu1,
      double sldr, double slro, double slnf,
      double slpf, String smhb, String smpx,
      String smke) {
    pw.println(firstHeader());
    pw.println(firstAVGdata(scom, salb, slu1,
        sldr, slro, slnf,
        slpf, smhb, smpx,
        smke));
  }

  private void buildFirstBody(PrintWriter pw, Iterator<String> lines) {
    pw.println(secondHeader());
    lines.forEachRemaining(line -> pw.println(line));
  }

  private void buildSecondBody(PrintWriter pw, Iterator<String> lines) {
    pw.println(thirdHeader());
    lines.forEachRemaining(line -> pw.println(line));
  }

  public void getSolFile(String filename,
      String country, String id_soil, double latitude, double longitude, String family,
      String scom, double salb, double slu1, double sldr, double slro, double slnf, double slpf, String smhb, String smpx, String smke,
      Iterator<String> firstlines, Iterator<String> secondlines) {

    FileWriter temp = null;
    PrintWriter pw = null;
    try {
      temp = new FileWriter(filename);
      pw = new PrintWriter(temp);
    } catch (FileNotFoundException ex) {
      throw new RuntimeException(ex);
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }

    buildInfo(pw, country, id_soil, latitude, longitude, family);
    buildHeader(pw, scom, salb, slu1, sldr, slro, slnf, slpf, smhb, smpx, smke);
    buildFirstBody(pw, firstlines);
    buildSecondBody(pw, secondlines);

    pw.close();
  }
}