WeightsParser.java [src/java/m/weps] Revision: 613ae993137bbe0b772b0b332095dbff3a9ff293  Date: Wed Oct 24 10:52:02 MDT 2018
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.weps;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import oms3.annotations.Execute;
import oms3.annotations.In;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author wlloyd
 */
public class WeightsParser {

    static final Logger log = Logger.getLogger(WeightsParser.class.getName());

    @In
    public String weightsFile;
    @In
    public String weightsErr;
    @In
    public int exitValue;

    public int station1 = 0;
    public double weight1 = 0.0;
    public int station2 = 0;
    public double weight2 = 0.0;
    public int station3 = 0;
    public double weight3 = 0.0;
    public int[] stations;
    public String[] weights;

    // WES method
    @Execute
    public void parse() {
        if (log.isLoggable(Level.INFO)) {
            log.info("weights file: " + weightsFile);
            log.info("weights err: " + weightsErr);
        }
        if (exitValue == 0) {

            // Find where point is described
            String sPattern = "point:";
            int start = weightsFile.lastIndexOf(sPattern);
            int end = weightsFile.indexOf('\n', start) + 1;
            // skip this line
            weightsFile = weightsFile.substring(end);
            if (log.isLoggable(Level.INFO)) {
                log.info("new filesubset=" + weightsFile);
            }
            end = weightsFile.indexOf('\n') + 1;
            // skip next line
            weightsFile = weightsFile.substring(end);
            end = weightsFile.indexOf('\n') + 1;
            // skip all comment lines
            String line = "#";
            do {
                weightsFile = weightsFile.substring(end);
                end = weightsFile.indexOf('\n') + 1;
                line = weightsFile.substring(0, end).trim();
            } while (line.charAt(0) == '#');
            // waste an extra line
            weightsFile = weightsFile.substring(end);
            end = weightsFile.indexOf('\n') + 1;
            line = weightsFile.substring(0, end).trim();
            if (log.isLoggable(Level.INFO)) {
                log.info("line=" + line);
            }
            station1 = Integer.parseInt(line.substring(0, line.indexOf(' ')));
            weight1 = Double.parseDouble(line.substring(line.indexOf(' ')));
            // next line
            weightsFile = weightsFile.substring(end);
            end = weightsFile.indexOf('\n') + 1;
            line = weightsFile.substring(0, end).trim();
            if (log.isLoggable(Level.INFO)) {
                log.info("line=" + line);
            }
            station2 = Integer.parseInt(line.substring(0, line.indexOf(' ')));
            weight2 = Double.parseDouble(line.substring(line.indexOf(' ')));
            // last line
            weightsFile = weightsFile.substring(end);
            end = weightsFile.indexOf('\n') + 1;
            line = weightsFile.substring(0, end).trim();
            if (log.isLoggable(Level.INFO)) {
                log.info("line=" + line);
            }
            station3 = Integer.parseInt(line.substring(0, line.indexOf(' ')));
            weight3 = Double.parseDouble(line.substring(line.indexOf(' ')));

            if (log.isLoggable(Level.INFO)) {
                log.info("station1=" + station1 + " weight1=" + weight1);
                log.info("station2=" + station2 + " weight2=" + weight2);
                log.info("station3=" + station3 + " weight3=" + weight3);
            }
        }
    }
    //Brad method
    public void parse(File weightsFile) throws IOException{
        List<String> lines;
        String station_data[];
        int counter = 0;
        String trimmedLine;
        
        stations = new int[]{0,0,0};
        weights = new String[]{"","",""};
        
        lines = FileUtils.readLines(weightsFile);
        
        for(String line : lines){
            log.info("LINE="+line);
            trimmedLine = line.trim().replace("  ", " ");
            log.info("TRIMMEDLINE="+trimmedLine);
            if(trimmedLine.length() > 0 && trimmedLine.charAt(0) != '#'){
                station_data = trimmedLine.split(" ");
                if(station_data.length == 2){
                    stations[counter] = Integer.parseInt(station_data[0]);
                    log.info("STATION="+stations[counter]);
                    weights[counter] = Double.toString(Double.parseDouble(station_data[1])); // easiest way to trim off E+xx
                    log.info("WEIGHT="+weights[counter]);
                    counter++;
                }
            }
        }

    }
}