DSSATresultParser.java [src/java/m/dssat/utils] 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 m.dssat.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

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

  private final File file;
  private final String[] params;


  public DSSATresultParser(String[] params, File file) {
    this.params = params;
    this.file = file;
  }


  public PlantGro_datastructure parse() {

    BufferedReader bf = null;
    InputStreamReader isr = null;
    try {
      FileInputStream is = new FileInputStream(file);
      isr = new InputStreamReader(is);
    } catch (FileNotFoundException ex) {
      throw new RuntimeException(ex.getMessage());
    }
    bf = new BufferedReader(isr);
    if (!bf.markSupported()) {
      throw new RuntimeException("Mark not supported");
    }
    try {
      bf.readLine();
      bf.mark(0);
    } catch (IOException ex) {
      throw new RuntimeException(ex.getMessage());
    }

    return parse(bf);

  }


  private PlantGro_datastructure parse(BufferedReader bf) {
    PlantGro_datastructure ds = new PlantGro_datastructure();
    String line = null;
    try {
      while ((line = bf.readLine()) != null) {
        if (line.contains("TREATMENT")) {
          String treatment = line.split(":")[1].split("\\s{2,}")[0];
          parseTreatment(bf, treatment, ds);
        }
      }
    } catch (IOException ex) {
      throw new RuntimeException(ex.getMessage());
    }

    return ds;
  }


  private void parseTreatment(BufferedReader bf, String treatment, PlantGro_datastructure ds) throws IOException {

    int[] columnIndeces = getColumnIndeces(bf);

    String previousLine = bf.readLine();
    String currentLine = bf.readLine();
    while (!currentLine.isEmpty()) {
      previousLine = currentLine;
      currentLine = bf.readLine();
      if (currentLine == null) {
        break;
      }
    }
    String[] linesplit = previousLine.split("\\s+");
    Map<String, Harvest> mapParams = new HashMap();
    for (int i = 0; i < columnIndeces.length; i++) {
      int index = columnIndeces[i];
      if (index != -1) {
        Double val = Double.parseDouble(linesplit[index]);
        int year = Integer.parseInt(linesplit[1]);
        int doy = Integer.parseInt(linesplit[2]);
        Harvest h = new Harvest(val, year, doy);
        mapParams.put(params[i], h);
      }
    }
    ds.add(treatment, mapParams);
  }


  private int[] getColumnIndeces(BufferedReader bf) {
    String line = null;
    boolean headerFound = Boolean.FALSE;

    try {
      while (!headerFound) {
        if ((line = bf.readLine()).contains("@YEAR")) {
          headerFound = Boolean.TRUE;
        }
      }
    } catch (IOException ex) {
      throw new RuntimeException(ex.getMessage());
    }

    if (!headerFound) {
      String msg = "No header found in file";
      throw new NullPointerException(msg);
    } else {
      return getColumnIndeces(line);
    }
  }


  private int[] getColumnIndeces(String line) {
    String lowerCaseLine = line.toLowerCase();
    int[] colIndeces = new int[params.length];
    for (int i = 0; i < params.length; i++) {
      String param = params[i];
      param = param.toLowerCase();
      if (lowerCaseLine.contains(param)) {
        String[] tmp = line.split("\\s+");
        int index = 1;
        for (String s : tmp) {
          if (s.toLowerCase().equals(param)) {
            colIndeces[i] = index;
          }
          index++;
        }
      } else {
        colIndeces[i] = -1;
      }
    }
    return colIndeces;
  }

}