SlopeSteepness.java [tools/MetaModelTools/src/data/interpretors] Revision: b79adc7db8b552d8d32592f6e1e919f0f8ce3926  Date: Fri Dec 06 15:08:53 MST 2019
package data.interpretors;

import csip.ServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */


public class SlopeSteepness {

    private String messages = "";
    private boolean badData = false;
    private HashMap<String, SlopeData> slopes;

    public SlopeSteepness(String slopeFileData) throws ServiceException {
        try {
            readSlopeData(slopeFileData);
        } catch (IOException ex) {
            Logger.getLogger(WindGenData.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean badSlopeData() {
        return badData;
    }

    public String slopeDataMessages() {
        return messages;
    }

    public SlopeData slope( String id){
        if (slopes.containsKey(id)){
            return slopes.get(id);
        }
        
        return null;
    }
    
    private void readSlopeData(String fileContents) throws IOException, ServiceException {
        if ((null != fileContents) && (!fileContents.isEmpty())) {

            BufferedReader slopeDataReader = new BufferedReader(new StringReader(fileContents));
            String slopeLine;

            //Strip header line
            slopeDataReader.readLine();
            
            while ((slopeLine = slopeDataReader.readLine()) != null) {
                String[] elements = slopeLine.trim().split("{");
                
                if ( elements.length >= 2){
                    String id = elements[0].replace(",", " ").trim();
                    String dataLine = "{" + elements[1].trim();
                    
                    SlopeData slopeData = new SlopeData(id, dataLine);
                    slopes.put(id, slopeData);
                }
            }
        }
    }

    public class SlopeData {

        private double min, max, mean, median, sum, majority;
        private String id = null;

        public SlopeData(String _id, String dataLine) throws ServiceException {
            if (null != dataLine) {
                String inLine = dataLine.trim();

                if (inLine.startsWith("{")) {
                    id = _id;

                    String[] values = inLine.split(",");

                    for (int i = 0; i < values.length; i++) {
                        String[] data = values[i].trim().split(":");
                        if (data.length >= 2) {
                            switch (data[0].toLowerCase()) {
                                case "'min'":
                                    min = Double.parseDouble(data[1].trim());
                                    break;

                                case "'max'":
                                    max = Double.parseDouble(data[1].trim());
                                    break;

                                case "'mean'":
                                    mean = Double.parseDouble(data[1].trim());
                                    break;

                                case "'median'":
                                    median = Double.parseDouble(data[1].trim());
                                    break;

                                case "'sum'":
                                    sum = Double.parseDouble(data[1].trim());
                                    break;

                                case "'majority'":
                                    majority = Double.parseDouble(data[1].trim());
                                    break;
                            }
                        } else {
                            break;
                        }
                    }
                }
            } else {
                throw new ServiceException("No input provided to the SlopeData object");
            }
        }

        public double min() {
            return min;
        }

        public double max() {
            return min;
        }

        public double mean() {
            return min;
        }

        public double median() {
            return min;
        }

        public double sum() {
            return min;
        }

        public double majority() {
            return min;
        }

        public String id() {
            return id;
        }
    }
}