ClimateStation.java [src/java/svap/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 svap.utils;

import data.table.Table;
import data.table.column.ColumnDouble;
import data.table.column.ColumnString;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import gisobjects.GISObjectFactory;
import java.util.ArrayList;
import java.util.StringTokenizer;
import static org.apache.commons.lang.math.NumberUtils.isNumber;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class ClimateStation extends Table {

    public static final String STATION_ID = "station_id";
    public static final String STATION_NAME = "station_name";
    public static final String AVG_TEMP = "average_temperature";
    public static final String STATION_DISTANCE = "distance_to_station";
    public static final String AVG_LOW_TEMP = "average_minimum_temperature";
    public static final String AVG_HI_TEMP = "average_maximum_temperature";
    public static final String AVG_PCPN = "average_precipitation";

    static final String[] STATION_ID_DATA = {STATION_ID, "Weather station identifier."};
    static final String[] STATION_NAME_DATA = {STATION_NAME, "Weather station name"};
    static final String[] AVG_LOW_TEMP_DATA = {AVG_LOW_TEMP, "Average ambient minimum temperature for this station for the date range specified.", "Fahrenheit", "%.2f"};
    static final String[] AVG_HI_TEMP_DATA = {AVG_HI_TEMP, "Average ambient maximum temperature for this station for the date range specified.", "Fahrenheit", "%.2f"};
    static final String[] AVG_TEMP_DATA = {AVG_TEMP, "Average ambient temperature for this station for the date range specified.", "Fahrenheit", "%.2f"};
    static final String[] AVG_PCPN_DATA = {AVG_PCPN, "Average precipitation for this station for the date range specified.", "Inches", "%.2f"};
    static final String[] STATION_DISTANCE_DATA = {STATION_DISTANCE, "Distance from the climate station to the location specified.", "Miles", "%.2f"};

    ArrayList<String> sids = new ArrayList<>();
    protected String state;
    protected int elevation;
    protected GISObject coordinates;
    protected String uid;
    protected String validDates;
    protected ArrayList<Double> avgtData = new ArrayList<>();
    protected ArrayList<Double> avgHiTData = new ArrayList<>();
    protected ArrayList<Double> avgLowTData = new ArrayList<>();
    protected ArrayList<Double> avgPcpnData = new ArrayList<>();

    public ClimateStation() {
        addDataColumn(STATION_ID, new ColumnString(STATION_ID_DATA));
        addDataColumn(STATION_NAME, new ColumnString(STATION_NAME_DATA));
        addDataColumn(AVG_TEMP, new ColumnDouble(AVG_TEMP_DATA));
        addDataColumn(AVG_HI_TEMP, new ColumnDouble(AVG_HI_TEMP_DATA));
        addDataColumn(AVG_LOW_TEMP, new ColumnDouble(AVG_LOW_TEMP_DATA));
        addDataColumn(AVG_PCPN, new ColumnDouble(AVG_PCPN_DATA));
        addDataColumn(STATION_DISTANCE, new ColumnDouble(STATION_DISTANCE_DATA));
    }

    public ClimateStation(String name, String sids, String state, int elevation, String uid, String validDates, String ll) throws GISObjectException {
        addDataColumn(STATION_ID, new ColumnString(STATION_ID_DATA));
        addDataColumn(STATION_NAME, new ColumnString(STATION_NAME_DATA));
        addDataColumn(AVG_TEMP, new ColumnDouble(AVG_TEMP_DATA));
        addDataColumn(AVG_HI_TEMP, new ColumnDouble(AVG_HI_TEMP_DATA));
        addDataColumn(AVG_LOW_TEMP, new ColumnDouble(AVG_LOW_TEMP_DATA));
        addDataColumn(AVG_PCPN, new ColumnDouble(AVG_PCPN_DATA));
        addDataColumn(STATION_DISTANCE, new ColumnDouble(STATION_DISTANCE_DATA));

        name(name);
        parseSids(sids);
        this.state = state;
        this.elevation = elevation;
        this.coordinates = GISObjectFactory.createGISObject("POINT " + ll.replace(",", " ").replace("[", "(").replace("]", ")"), null);
        this.uid = uid;
        this.validDates = validDates;

    }

    public void getDataFromJSON(JSONObject jsonArray) throws JSONException, GISObjectException {
        name(jsonArray.getString("name"));
        parseSids(jsonArray.getString("sids").replace("[", "").replace("]", "").replace("\"", ""));
        state = jsonArray.getString("state");
        elevation = jsonArray.getInt("elev");
        validDates = jsonArray.getString("valid_daterange");
        uid = jsonArray.getString("uid");
        String ll = jsonArray.getString("ll");
        coordinates = GISObjectFactory.createGISObject("POINT " + ll.replace(",", " ").replace("[", "(").replace("]", ")"), null);
    }

    public String name() {
        return (String) columns.get(STATION_NAME).getValue();
    }

    public void name(String value) {
        columns.get(STATION_NAME).setValue(value);
    }

    public double distance() {
        return (double) columns.get(STATION_DISTANCE).getValue();
    }

    public void distance(double value) {
        columns.get(STATION_DISTANCE).setValue(value);
    }

    public ArrayList<String> sids() {
        return sids;
    }

    public String sid(int index) {
        if ((index >= 0) && (sids.size() > index)) {
            return sids.get(index);
        }
        return null;
    }

    public void sid(String value) {
        columns.get(STATION_ID).setValue(value);
    }

    public String state() {
        return state;
    }

    public int elevation() {
        return elevation;
    }

    public GISObject coordinates() {
        return coordinates;
    }

    public void readJSONData(JSONArray avgtArray) throws JSONException {
        int counter = 0;
        int hiTempCounter = 0;
        int lowTempCounter = 0;
        int pcpnCounter = 0;

        if (null != avgtArray) {
            AverageTemp(0.0);
            AverageLowTemp(0.0);
            AverageHiTemp(0.0);
            AveragePcpn(0.0);
            
            for (int i = 0; i < avgtArray.length(); i++) {
                JSONArray tData = avgtArray.getJSONArray(i);
                String tempString = tData.getString(1);
                if (isNumber(tempString)) {
                    double tTemp = Double.parseDouble(tempString);
                    AverageTemp(AverageTemp() + tTemp);
                    avgtData.add(tTemp);
                    counter++;
                }

                tempString = tData.getString(2);
                if (isNumber(tempString)) {
                    double tTemp = Double.parseDouble(tempString);
                    AverageLowTemp(AverageLowTemp() + tTemp);
                    avgLowTData.add(tTemp);
                    lowTempCounter++;
                }

                tempString = tData.getString(3);
                if (isNumber(tempString)) {
                    double tTemp = Double.parseDouble(tempString);
                    AverageHiTemp(AverageHiTemp() + tTemp);
                    avgHiTData.add(tTemp);
                    hiTempCounter++;
                }

                tempString = tData.getString(4);
                if (isNumber(tempString)) {
                    double tPcpn = Double.parseDouble(tempString);
                    AveragePcpn(AveragePcpn() + tPcpn);
                    avgPcpnData.add(tPcpn);
                    pcpnCounter++;
                }
            }
        }

        if (!Double.isNaN(AverageTemp()) && (0 < counter)) {
            AverageTemp(AverageTemp() / counter);
        }

        if (!Double.isNaN(AverageLowTemp()) && (0 < lowTempCounter)) {
            AverageLowTemp(AverageLowTemp() / lowTempCounter);
        }
        if (!Double.isNaN(AverageHiTemp()) && (0 < hiTempCounter)) {
            AverageHiTemp(AverageHiTemp() / hiTempCounter);
        }
        if (!Double.isNaN(AveragePcpn()) && (0 < pcpnCounter)) {
            AveragePcpn(AveragePcpn() / pcpnCounter);
        }
    }

    public double AverageTemp() {
        return (double) columns.get(AVG_TEMP).getValue();
    }

    public void AverageTemp(Double value) {
        columns.get(AVG_TEMP).setValue(value);
    }

    public double AverageHiTemp() {
        return (double) columns.get(AVG_HI_TEMP).getValue();
    }

    public void AverageHiTemp(Double value) {
        columns.get(AVG_HI_TEMP).setValue(value);
    }

    public double AverageLowTemp() {
        return (double) columns.get(AVG_LOW_TEMP).getValue();
    }

    public void AverageLowTemp(Double value) {
        columns.get(AVG_LOW_TEMP).setValue(value);
    }

    public double AveragePcpn() {
        return (double) columns.get(AVG_PCPN).getValue();
    }

    public void AveragePcpn(Double value) {
        columns.get(AVG_PCPN).setValue(value);
    }

    public String uid() {
        return uid;
    }

    public String validDates() {
        return validDates;
    }

    private void parseSids(String sidString) {
        StringTokenizer st = new StringTokenizer(sidString, ",");
        while (st.hasMoreTokens()) {
            sids.add(st.nextToken());
        }

        sid(sids.get(0));
    }
}