guiTimeseries_Model.java [src/java/cfa] Revision: 5149545339c5424c7bc37f3a0cd787c1c62b1060  Date: Tue Jun 17 15:32:38 MDT 2014
package cfa;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.Year;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
* Last Updated: 17-June-2014
* @author Tyler Wible
* @since 24-June-2011
*/
public class guiTimeseries_Model {
    //Inputs
    String mainFolder = "C:/Projects/TylerWible/CodeDirectories/NetBeans/CSIP/data/CFA/Timeseries";
    String organizationName = "USGS";//"Colorado Dept. of Public Health & Environment";//
    String stationID = "06764880";//"000028";//
    String stationName = "South Platte River at Roscoe, Nebr.";//"BIG THOMPSON R NEAR MOUTH";//
    String wqTest = "flow";//"00600        Total nitrogen, water, unfiltered, milligrams per liter, mg/L";
    String beginDate = "";
    String endDate = "";
    String timeStep = "Daily";//"Yearly";//"Monthly";//
    String method = "Max";//"Min";//"Average";//"Total";//
    double highPercentile = 0.75;
    double lowPercentile = 0.25;
    int numBins = 10;
    String period1Begin = "";
    String period1End = "";
    String period2Begin = "";
    String period2End = "";
    String period3Begin = "";
    String period3End = "";
    boolean medianTF = false;
    String userData = "";//"Date\tFlow\n1999-04-29\t8.3\n1999-05-09\t60.2\n1999-05-29\t20.1";//
    boolean mergeDatasets = false;//true;// 
    String mergeMethod = "user";//"public";//"max";//"average";//"min";//
    
    //Outputs
    String len = "-1";
    String start = "?";
    String end = "?";
    String units = "?";
    double max = -1;
    double min = -1;
    double upperQuartile = -1;
    double lowerQuartile = -1;
    double median = -1;
    double mean = -1;
    double standardDeviation = -1;
    double variance = -1;
    double skew = -1;
    double coeffvar = -1;
    
    String len_period1 = "-1";
    double max_period1 = -1;
    double min_period1 = -1;
    double upperQuartile_period1 = -1;
    double lowerQuartile_period1 = -1;
    double median_period1 = -1;
    double mean_period1 = -1;
    double standardDeviation_period1 = -1;
    double variance_period1 = -1;
    double skew_period1 = -1;
    double coeffvar_period1 = -1;
    
    String len_period2 = "-1";
    double max_period2 = -1;
    double min_period2 = -1;
    double upperQuartile_period2 = -1;
    double lowerQuartile_period2 = -1;
    double median_period2 = -1;
    double mean_period2 = -1;
    double standardDeviation_period2 = -1;
    double variance_period2 = -1;
    double skew_period2 = -1;
    double coeffvar_period2 = -1;
    
    String len_period3 = "-1";
    double max_period3 = -1;
    double min_period3 = -1;
    double upperQuartile_period3 = -1;
    double lowerQuartile_period3 = -1;
    double median_period3 = -1;
    double mean_period3 = -1;
    double standardDeviation_period3 = -1;
    double variance_period3 = -1;
    double skew_period3 = -1;
    double coeffvar_period3 = -1;
    
    
    //Gets
    public File getParagraph() {
        return new File(mainFolder, "timeseries_summary.txt");
    }
    public File getFlowStatistics_summary() {
        FlowStatistics flowStats = new FlowStatistics();
        return new File(mainFolder, flowStats.getFlowStatistics_summary());
    }
    public File getTimeseriesOutput(){
        return new File(mainFolder, "timeseries_graph.out");
    }
    public File getBoxplotOutput(){
        return new File(mainFolder, "timeseries_boxplot.out");
    }
    public String getGraph() {
        return "timeseries_graph.jpg";
    }
    public String getBoxplot() {
        return "timeseries_boxplot.jpg";
    }
    public String getHistogram() {
        return "timeseries_histogram.jpg";
    }
    public String getMonthlyGraph() {
        return "timeseries_monthlygraph.jpg";
    }
    public String getMonthlyBoxplot() {
        return "timeseries_monthlyboxplot.jpg";
    }
    public String getCDF() {
        return "timeseries_cdf.jpg";
    }
    public String getLen(){
        return len;
    }
    public String getStart(){
        return start;
    }
    public String getEnd(){
        return end;
    }
    public String getUnits(){
        return units;
    }
    public String getMax(){
        return String.valueOf(max);
    }
    public String getMin(){
        return String.valueOf(min);
    }
    public String getUpperQuartile(){
        return String.valueOf(upperQuartile);
    }
    public String getLowerQuartile(){
        return String.valueOf(lowerQuartile);
    }
    public String getMedian(){
        return String.valueOf(median);
    }
    public String getMean(){
        return String.valueOf(mean);
    }
    public String getStandardDeviation(){
        return String.valueOf(standardDeviation);
    }
    public String getVariance(){
        return String.valueOf(variance);
    }
    public String getSkewness(){
        return String.valueOf(skew);
    }
    public String getCoefficientOfVariation(){
        return String.valueOf(coeffvar);
    }
    public String getLen_period1(){
        return len_period1;
    }
    public String getMax_period1(){
        return String.valueOf(max_period1);
    }
    public String getMin_period1(){
        return String.valueOf(min_period1);
    }
    public String getUpperQuartile_period1(){
        return String.valueOf(upperQuartile_period1);
    }
    public String getLowerQuartile_period1(){
        return String.valueOf(lowerQuartile_period1);
    }
    public String getMedian_period1(){
        return String.valueOf(median_period1);
    }
    public String getMean_period1(){
        return String.valueOf(mean_period1);
    }
    public String getStandardDeviation_period1(){
        return String.valueOf(standardDeviation_period1);
    }
    public String getVariance_period1(){
        return String.valueOf(variance_period1);
    }
    public String getSkewness_period1(){
        return String.valueOf(skew_period1);
    }
    public String getCoefficientOfVariation_period1(){
        return String.valueOf(coeffvar_period1);
    }
    public String getLen_period2(){
        return len_period2;
    }
    public String getMax_period2(){
        return String.valueOf(max_period2);
    }
    public String getMin_period2(){
        return String.valueOf(min_period2);
    }
    public String getUpperQuartile_period2(){
        return String.valueOf(upperQuartile_period2);
    }
    public String getLowerQuartile_period2(){
        return String.valueOf(lowerQuartile_period2);
    }
    public String getMedian_period2(){
        return String.valueOf(median_period2);
    }
    public String getMean_period2(){
        return String.valueOf(mean_period2);
    }
    public String getStandardDeviation_period2(){
        return String.valueOf(standardDeviation_period2);
    }
    public String getVariance_period2(){
        return String.valueOf(variance_period2);
    }
    public String getSkewness_period2(){
        return String.valueOf(skew_period2);
    }
    public String getCoefficientOfVariation_period2(){
        return String.valueOf(coeffvar_period2);
    }
    public String getLen_period3(){
        return len_period3;
    }
    public String getMax_period3(){
        return String.valueOf(max_period3);
    }
    public String getMin_period3(){
        return String.valueOf(min_period3);
    }
    public String getUpperQuartile_period3(){
        return String.valueOf(upperQuartile_period3);
    }
    public String getLowerQuartile_period3(){
        return String.valueOf(lowerQuartile_period3);
    }
    public String getMedian_period3(){
        return String.valueOf(median_period3);
    }
    public String getMean_period3(){
        return String.valueOf(mean_period3);
    }
    public String getStandardDeviation_period3(){
        return String.valueOf(standardDeviation_period3);
    }
    public String getVariance_period3(){
        return String.valueOf(variance_period3);
    }
    public String getSkewness_period3(){
        return String.valueOf(skew_period3);
    }
    public String getCoefficientOfVariation_period3(){
        return String.valueOf(coeffvar_period3);
    }
    
    //Sets
    public void setMainFolder(String mainFolder) {
        this.mainFolder = mainFolder;
    }
    public void setOrganizationName(String organizationName) {
        this.organizationName = organizationName;
    }
    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
    public void setStationName(String stationName) {
        this.stationName = stationName;
    }
    public void setStationID(String stationID) {
        this.stationID = stationID;
    }
    public void setWQtest(String wqTest) {
        this.wqTest = wqTest;
    }
    public void setTimeStep(String timeStep) {
        this.timeStep = timeStep;
    }
    public void setMethod(String method) {
        this.method = method;
    }
    public void setHighPercentile(double highPercentile) {
        this.highPercentile = highPercentile;
    }
    public void setLowPercentile(double lowPercentile) {
        this.lowPercentile = lowPercentile;
    }
    public void setNumberOfBins(int numBins) {
        this.numBins = numBins;
    }
    public void setPeriod1Begin(String period1Begin) {
        this.period1Begin = period1Begin;
    }
    public void setPeriod1End(String period1End) {
        this.period1End = period1End;
    }
    public void setPeriod2Begin(String period2Begin) {
        this.period2Begin = period2Begin;
    }
    public void setPeriod2End(String period2End) {
        this.period2End = period2End;
    }
    public void setPeriod3Begin(String period3Begin) {
        this.period3Begin = period3Begin;
    }
    public void setPeriod3End(String period3End) {
        this.period3End = period3End;
    }
    public void setMedianTF(boolean medianTF) {
        this.medianTF = medianTF;
    }
    public void setUserData(String userData) {
        this.userData = userData;
    }
    public void setMergeDatasets(boolean mergeDatasets) {
        this.mergeDatasets = mergeDatasets;
    }
    public void setMergeMethod(String mergeMethod) {
        this.mergeMethod = mergeMethod;
    }
    /**
     * Main statistics function calls other functions to calculate each statistic value then stores the results as global variables
     * @param dataList  data on which statistical values are desired
     * @param flag  a flag for which results the statistics will be stored to, either "all", "period1", "period2", or "period3"
     */
    private void CalculateStatistics(ArrayList<Double> dataList, String flag) {
        DoubleMath doubleMath = new DoubleMath();
        
        double temp1 = doubleMath.round(doubleMath.Min_Max(dataList, true),3);//Call Max function
        double temp2 = doubleMath.round(doubleMath.Min_Max(dataList, false),3);//Call Min function
        double temp3 = doubleMath.round(doubleMath.Percentile_function(dataList,0.75),3);//Call Upper Quartile function
        double temp4 = doubleMath.round(doubleMath.Percentile_function(dataList,0.25),3);//Call Lower Quartile function
        double temp5 = doubleMath.round(doubleMath.Median(dataList),3);//Call Median function
        double temp6 = doubleMath.round(doubleMath.Average(dataList),3);//Call Mean function
        double temp7 = doubleMath.round(doubleMath.StandardDeviationSample(dataList),3);//Call standard deviation
        double temp8 = doubleMath.round(doubleMath.VarianceSample(dataList), 3);
        double temp9 = doubleMath.round(doubleMath.SkewnessSample(dataList),8);
        double temp10 = doubleMath.round(doubleMath.CoefficientOfVariation(dataList),3);
        
        if(flag.equalsIgnoreCase("all")){
            max = temp1;
            min = temp2;
            upperQuartile = temp3;
            lowerQuartile = temp4;
            median = temp5;
            mean = temp6;
            standardDeviation = temp7;
            variance = temp8;
            skew = temp9;
            coeffvar = temp10;
        }else if(flag.equalsIgnoreCase("period1")){
            max_period1 = temp1;
            min_period1 = temp2;
            upperQuartile_period1 = temp3;
            lowerQuartile_period1 = temp4;
            median_period1 = temp5;
            mean_period1 = temp6;
            standardDeviation_period1 = temp7;
            variance_period1 = temp8;
            skew_period1 = temp9;
            coeffvar_period1 = temp10;
        }else if(flag.equalsIgnoreCase("period2")){
            max_period2 = temp1;
            min_period2 = temp2;
            upperQuartile_period2 = temp3;
            lowerQuartile_period2 = temp4;
            median_period2 = temp5;
            mean_period2 = temp6;
            standardDeviation_period2 = temp7;
            variance_period2 = temp8;
            skew_period2 = temp9;
            coeffvar_period2 = temp10;
        }else if(flag.equalsIgnoreCase("period3")){
            max_period3 = temp1;
            min_period3 = temp2;
            upperQuartile_period3 = temp3;
            lowerQuartile_period3 = temp4;
            median_period3 = temp5;
            mean_period3 = temp6;
            standardDeviation_period3 = temp7;
            variance_period3 = temp8;
            skew_period3 = temp9;
            coeffvar_period3 = temp10;
        }
    }
    /**
     * Graph the time series and user data and save the resulting graph to the specified location
     * @param sortedData  the String[][] containing sorted data for the time series 
     * (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm if 
     * timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param sortedData_user  the String[][] containing sorted user data for the 
     * time series (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm 
     * if timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param color  the color of the merged dataset (Java.Color)
     * @param color2  the color of the user dataset (Java.Color)
     * @param showLine  a boolean, if true lines will be shown on the graph, if false only shapes for the data
     * @param yAxisTitle  the String label for the y axis of the graph
     * @param units  the units of the current graph to be used in labeling for the legend
     * @param medianTF  if true then the median value will be plotted for period analyses, if false the mean (average) will be used
     */
    private void createTimeseriesGraph(String[][] sortedData,
                                       String[][] sortedData_user,
                                       Color color,
                                       Color color2,
                                       boolean showLine,
                                       String yAxisTitle,
                                       String units,
                                       boolean medianTF) throws ParseException {
        //Change analysis period dates into calendar objects
        SimpleDateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date period1Begin_date = new Date();
        Date period1End_date = new Date();
        Date period2Begin_date = new Date();
        Date period2End_date = new Date();
        Date period3Begin_date = new Date();
        Date period3End_date = new Date();
        
        if(!period1Begin.isEmpty() && !period1End.isEmpty()){
            period1Begin_date = desiredDateFormat.parse(period1Begin);
            period1End_date = desiredDateFormat.parse(period1End);
        }
        if(!period2Begin.isEmpty() && !period2End.isEmpty()){
            period2Begin_date = desiredDateFormat.parse(period2Begin);
            period2End_date = desiredDateFormat.parse(period2End);
        }
        if(!period3Begin.isEmpty() && !period3End.isEmpty()){
            period3Begin_date = desiredDateFormat.parse(period3Begin);
            period3End_date = desiredDateFormat.parse(period3End);
        }
        
        //Create TimeSeries graph of merged data
        TimeSeries series = new TimeSeries(stationID + ": Data");
        ArrayList<Double> period1 = new ArrayList<Double>();
        ArrayList<Double> period2 = new ArrayList<Double>();
        ArrayList<Double> period3 = new ArrayList<Double>();
        for(int i=0; i < sortedData.length; i++) {
            double value = Double.parseDouble(sortedData[i][1]);
            String tmpStr = sortedData[i][0];

            if(timeStep.equalsIgnoreCase("daily")){
                double d = Double.parseDouble(tmpStr.substring(8));
                double m = Double.parseDouble(tmpStr.substring(5,7));
                double y = Double.parseDouble(tmpStr.substring(0,4));
                int day =  (int)d;
                int month = (int)m;
                int year = (int)y;
                Day date = new Day(day,month,year);//day,month,year
                series.add(date, value);
                
            }else if(timeStep.equalsIgnoreCase("monthly")){
                desiredDateFormat = new SimpleDateFormat("yyyy-MM");
                double m = Double.parseDouble(tmpStr.substring(5,7));
                double y = Double.parseDouble(tmpStr.substring(0,4));
                int month = (int)m;
                int year = (int)y;
                Month date = new Month(month,year);//month,year
                series.add(date, value);
                
            }else if(timeStep.equalsIgnoreCase("yearly")){
                desiredDateFormat = new SimpleDateFormat("yyyy");
                double y = Double.parseDouble(tmpStr.substring(0,4));
                int year = (int)y;
                Year date = new Year(year);//year
                series.add(date, value);
            }
            
            //Check periods
            Date newDate = desiredDateFormat.parse(tmpStr);
            if(newDate.compareTo(period1Begin_date) >= 0 && newDate.compareTo(period1End_date) <= 0){
                period1.add(value);
            }else if(newDate.compareTo(period2Begin_date) >= 0 && newDate.compareTo(period2End_date) <= 0){
                period2.add(value);
            }else if(newDate.compareTo(period3Begin_date) >= 0 && newDate.compareTo(period3End_date) <= 0){
                period3.add(value);
            }
        }
        
        //Create Time Series graph of user data
        TimeSeries series2 = new TimeSeries(stationID + ": User Data");
        for(int i=0; i < sortedData_user.length; i++) {
            double value = Double.parseDouble(sortedData_user[i][1]);
            String tmpStr = sortedData_user[i][0];

            if(timeStep.equalsIgnoreCase("daily")){
                double d = Double.parseDouble(tmpStr.substring(8));
                double m = Double.parseDouble(tmpStr.substring(5,7));
                double y = Double.parseDouble(tmpStr.substring(0,4));
                int day =  (int)d;
                int month = (int)m;
                int year = (int)y;
                Day date = new Day(day,month,year);//day,month,year
                series2.add(date, value);
            }else if(timeStep.equalsIgnoreCase("monthly")){
                double m = Double.parseDouble(tmpStr.substring(5,7));
                double y = Double.parseDouble(tmpStr.substring(0,4));
                int month = (int)m;
                int year = (int)y;
                Month date = new Month(month,year);//month,year
                series2.add(date, value);
            }else if(timeStep.equalsIgnoreCase("yearly")){
                double y = Double.parseDouble(tmpStr.substring(0,4));
                int year = (int)y;
                Year date = new Year(year);//year
                series2.add(date, value);
            }
        }
		
        //Create renderer, and axis for timeseries graph
        Graphing graphing = new Graphing();
        XYPlot plotTime = new XYPlot();
        boolean showLegend = false;
        
        //Create user data points
        if(sortedData_user.length != 0){//only show user points if it is not zero
            plotTime = graphing.graphTimeData(plotTime, series, showLine, color, false, false, false, true, 0);
            plotTime = graphing.graphTimeData(plotTime, series2, showLine, color2, false, false, false, true, 1);
            showLegend = true;
        }else{
            plotTime = graphing.graphTimeData(plotTime, series, showLine, color, false, false, false, true, 0);
        }
        
        //Create period analysis average lines
        if(!period1Begin.isEmpty() && !period1End.isEmpty()){
            CalculateStatistics(period1,"period1");
            this.len_period1 = String.valueOf(period1.size());
            double value = mean_period1;
            String label = "Average";
            if(medianTF){
                value = median_period1;
                label = "Median";
            }
            TimeSeries periodSeries = new TimeSeries("Period 1 " + label + ": " + value + " " + units);
            
            //Set Start Point
            double d = Double.parseDouble(period1Begin.substring(8));
            double m = Double.parseDouble(period1Begin.substring(5,7));
            double y = Double.parseDouble(period1Begin.substring(0,4));
            int day =  (int)d;
            int month = (int)m;
            int year = (int)y;
            Day date = new Day(day,month,year);//day,month,year
            periodSeries.add(date, value);
            
            //Set End Point
            d = Double.parseDouble(period1End.substring(8));
            m = Double.parseDouble(period1End.substring(5,7));
            y = Double.parseDouble(period1End.substring(0,4));
            day =  (int)d;
            month = (int)m;
            year = (int)y;
            date = new Day(day,month,year);//day,month,year
            periodSeries.add(date, value);
            
            plotTime = graphing.graphTimeData(plotTime, periodSeries, true, Color.RED, false, false, true, true, 2);
            showLegend = true;
        }
        if(!period2Begin.isEmpty() && !period2End.isEmpty()){
            CalculateStatistics(period2,"period2");
            this.len_period2 = String.valueOf(period2.size());
            double value = mean_period2;
            String label = "Average";
            if(medianTF){
                value = median_period2;
                label = "Median";
            }
            TimeSeries periodSeries = new TimeSeries("Period 2 " + label + ": " + value + " " + units);
            
            //Set Start Point
            double d = Double.parseDouble(period2Begin.substring(8));
            double m = Double.parseDouble(period2Begin.substring(5,7));
            double y = Double.parseDouble(period2Begin.substring(0,4));
            int day =  (int)d;
            int month = (int)m;
            int year = (int)y;
            Day date = new Day(day,month,year);//day,month,year
            periodSeries.add(date, value);
            
            //Set End Point
            d = Double.parseDouble(period2End.substring(8));
            m = Double.parseDouble(period2End.substring(5,7));
            y = Double.parseDouble(period2End.substring(0,4));
            day =  (int)d;
            month = (int)m;
            year = (int)y;
            date = new Day(day,month,year);//day,month,year
            periodSeries.add(date, value);
            
            plotTime = graphing.graphTimeData(plotTime, periodSeries, true, Color.BLACK, false, false, true, true, 3);
            showLegend = true;
        }
        if(!period3Begin.isEmpty() && !period3End.isEmpty()){
            CalculateStatistics(period3,"period3");
            this.len_period3 = String.valueOf(period3.size());
            double value = mean_period3;
            String label = "Average";
            if(medianTF){
                value = median_period3;
                label = "Median";
            }
            TimeSeries periodSeries = new TimeSeries("Period 3 " + label + ": " + value + " " + units);
            
            //Set Start Point
            double d = Double.parseDouble(period3Begin.substring(8));
            double m = Double.parseDouble(period3Begin.substring(5,7));
            double y = Double.parseDouble(period3Begin.substring(0,4));
            int day =  (int)d;
            int month = (int)m;
            int year = (int)y;
            Day date = new Day(day,month,year);//day,month,year
            periodSeries.add(date, value);
            
            //Set End Point
            d = Double.parseDouble(period3End.substring(8));
            m = Double.parseDouble(period3End.substring(5,7));
            y = Double.parseDouble(period3End.substring(0,4));
            day =  (int)d;
            month = (int)m;
            year = (int)y;
            date = new Day(day,month,year);//day,month,year
            periodSeries.add(date, value);
            
            plotTime = graphing.graphTimeData(plotTime, periodSeries, true, Color.LIGHT_GRAY, false, false, true, true, 4);
            showLegend = true;
        }
      
        //Create Y Axis
        ValueAxis rangeTime = new NumberAxis(yAxisTitle);
        plotTime.setRangeAxis(0, rangeTime);
        
        //Create X Axis
        DateAxis domainTime = new DateAxis("Date");
        domainTime.setLowerMargin(0.05);
        domainTime.setUpperMargin(0.05);
        plotTime.setDomainAxis(0, domainTime);

        //Set extra plot preferences
        plotTime = graphing.setTimeAxisPreferences(plotTime);

        //Create the chart with the plot and a legend
        String title = "Time Series for Station: " + stationID + "; " + stationName;
        JFreeChart chart = new JFreeChart(title, graphing.titleFont, plotTime, showLegend);
        
        //Set legend Font
        if(showLegend){
            LegendTitle legendTitle = chart.getLegend();
            legendTitle.setItemFont(graphing.masterFont);
        }
        
        //Save resulting graph for use later
        try{
            String path = mainFolder + File.separator + getGraph();
            ChartUtilities.saveChartAsJPEG(new File(path), chart, 1280, 800);
            System.out.println("JFreeChart created properly at: " + path);

        }catch(IOException e){
            System.err.println("A problem occurred while trying to creating the chart.");
        }
    }
    /**
     * Creates a boxplot of the timeseries data to be displayed next to a summary of the statistics of the timeseries data
     * @param yAxisTitle  a String of the title for the Y axis of the boxplot
     */
    private void createTimeseriesBoxplot(String[][] data, String yAxisTitle){    	
    	Graphing graphing = new Graphing();
        //Create boxplot of the timeseries data
        XYPlot plot = new XYPlot();

        //Create X Axis
        ValueAxis xAxis = new NumberAxis("");
        xAxis.setRange(0, 10);
        xAxis.setLabelFont(graphing.masterFont);
        xAxis.setTickLabelFont(graphing.masterFont);
        xAxis.setTickLabelsVisible(false);
        plot.setDomainAxis(0, xAxis);
        
    	ValueAxis yAxis = new NumberAxis(yAxisTitle);
        yAxis.setLabelFont(graphing.masterFont);
        yAxis.setTickLabelFont(graphing.masterFont);
        plot.setRangeAxis(0, yAxis);

    	//Calculate and add Median to dataset
        XYSeries median_series = new XYSeries("Median");
        median_series.add(5, median);

        //Create median Line
        XYDataset median_scatter = new XYSeriesCollection(median_series);
        XYItemRenderer renderer_median = new XYLineAndShapeRenderer(false, true);
        renderer_median.setSeriesShape(0, new Rectangle2D.Double(-4.0, 0.0, 8.0, 1));//new Ellipse2D.Double(-4, -4, 8, 8));
        renderer_median.setSeriesPaint(0, Color.red);
        renderer_median.setSeriesVisibleInLegend(0, false);
        plot.setDataset(0, median_scatter);
        plot.setRenderer(0, renderer_median);


        //Create quartile Box shapes for the box plot
        //Create XYSeries for the box shape
        XYSeries shapeSeries = new XYSeries("Shape");
        shapeSeries.add(5, lowerQuartile);
        shapeSeries.add(5, upperQuartile);

        //Create the quartile rectangle shape
        XYDataset shapeDataset = new XYSeriesCollection(shapeSeries);
        XYItemRenderer renderer_shape = new XYLineAndShapeRenderer(true, false);
        Stroke thickness = new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
        renderer_shape.setSeriesStroke(0, thickness);
        renderer_shape.setSeriesPaint(0, Color.blue);
        renderer_shape.setSeriesVisibleInLegend(0, false);
        plot.setDataset(1, shapeDataset);
        plot.setRenderer(1, renderer_shape);

        
        //Creates 1.5 * Interquartile Range (IQR) lines
        //Create XYSeries for the min-max lines
        double IQR = upperQuartile - lowerQuartile;
        double lowerLimit = lowerQuartile - 1.5*IQR;
        double upperLimit = upperQuartile + 1.5*IQR;
        if(lowerLimit < min){
            lowerLimit = min;
        }
        if(upperLimit > max){
            upperLimit = max;
        }
        XYSeries lineSeries = new XYSeries("Line");
        lineSeries.add(5, lowerLimit);
        lineSeries.add(5, upperLimit);

        //Create the 1.5*IQR lines
        XYDataset lineDataset = new XYSeriesCollection(lineSeries);
        XYItemRenderer lineRenderer = new XYLineAndShapeRenderer(true, true);
        Stroke thickness2 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
        lineRenderer.setSeriesStroke(0, thickness2);
        lineRenderer.setSeriesShape(0, new Rectangle2D.Double(-10.0, 0.0, 20.0, 1));
        lineRenderer.setSeriesPaint(0, Color.black);
        lineRenderer.setSeriesVisibleInLegend(0, false);
        plot.setDataset(2, lineDataset);
        plot.setRenderer(2, lineRenderer);
        
        //Calculate and create Outliers (# < lowerQuartile - 1.5*IQR or # > upperQuartile + 1.5*IQR)
        //Calculate and create Extreme Outliers (# < lowerQuartile - 3*IQR or # > upperQuartile + 3*IQR)
        XYSeries outliers = new XYSeries("Outliers");
        XYSeries extremeOutliers = new XYSeries("Extreme Outliers");
        for(int i=0; i<data.length; i++){
            double value = Double.parseDouble(data[i][1]);
            //Lower outliers
            if(value < (lowerQuartile - 1.5*IQR) && value > (lowerQuartile - 3*IQR)){
                outliers.add(5, value);
            }
            //Upper outliers
            if(value > (upperQuartile + 1.5*IQR) && value < (lowerQuartile + 3*IQR)){
                outliers.add(5, value);
            }

            //Extreme Lower outliers
            if(value < (lowerQuartile - 3*IQR)){
                extremeOutliers.add(5, value);
            }
            //Extreme Upper outliers
            if(value > (lowerQuartile + 3*IQR)){
                extremeOutliers.add(5, value);
            }
        }

        //Create outlier scatter
        XYDataset outlier_scatter = new XYSeriesCollection(outliers);
        XYItemRenderer renderer_outlier = new XYLineAndShapeRenderer(false, true);
        renderer_outlier.setSeriesShape(0, new Ellipse2D.Double(-2.0, 2.0, 4.0, 4.0));
        renderer_outlier.setSeriesPaint(0, Color.DARK_GRAY);
        if(outliers.isEmpty()){
            renderer_outlier.setSeriesVisibleInLegend(0, false);        	
        }
        plot.setDataset(3, outlier_scatter);
        plot.setRenderer(3, renderer_outlier);
        
        //Create extreme outlier scatter
        XYDataset extremeOutlier_scatter = new XYSeriesCollection(extremeOutliers);
        XYItemRenderer renderer_ExtremeOutlier = new XYLineAndShapeRenderer(false, true);
        renderer_ExtremeOutlier.setSeriesShape(0, new Ellipse2D.Double(-2.0, 2.0, 4.0, 4.0));
        renderer_ExtremeOutlier.setSeriesPaint(0, Color.red);
        if(extremeOutliers.isEmpty()){
            renderer_ExtremeOutlier.setSeriesVisibleInLegend(0, false);        	
        }
        plot.setDataset(4, extremeOutlier_scatter);
        plot.setRenderer(4, renderer_ExtremeOutlier);

        //Put the line on the first Domain and first Range
        plot.mapDatasetToDomainAxis(0, 0);
        plot.mapDatasetToRangeAxis(0, 0);

        //Set extra plot preferences
        plot.setOutlinePaint(Color.black);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.black);
        
        
        //Create the chart with the plot and a legend
        JFreeChart chart = new JFreeChart("Boxplot of Timeseries Data", graphing.titleFont, plot, true);

        //Save resulting graph for use later
        try{
            String path = mainFolder + File.separator + getBoxplot();
            ChartUtilities.saveChartAsJPEG(new File(path), chart, 200, 400);
            System.out.println("JFreeChart created properly at: " + path);

        }catch(IOException e){
            System.err.println("A problem occurred while trying to creating the chart.");
        }
    }
    /**
     * Graph a histogram of the time series and user data and save the resulting graph to the specified location
     * @param sortedData  the String[][] containing sorted data for the time series 
     * (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm if 
     * timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param sortedData_user  the String[][] containing sorted user data for the 
     * time series (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm 
     * if timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param color  the color of the graph for flow (blue) or water quality (magenta)
     * @param xAxisTitle  the String label for the y axis of the graph
     */
    private void createTimeseriesHistogram(String[][] sortedData,
                                           String[][] sortedData_user,
                                           Color color,
                                           String xAxisTitle){
        //Initialize other classes
        Graphing graphing = new Graphing();
        DoubleMath doubleMath = new DoubleMath();
        
        //Retrieve Data
        double[] data = new double[sortedData.length];
        for(int i=0; i < sortedData.length; i++) {
            data[i] = Double.parseDouble(sortedData[i][1]);
        }
        double[] data_user = new double[sortedData_user.length];
        for(int i=0; i < sortedData_user.length; i++) {
            data_user[i] = Double.parseDouble(sortedData_user[i][1]);
        }
        
        //Determine bin range
        double interval = (max - min) / numBins;
        double[] lowerLimit = new double[numBins];
        double[] upperLimit = new double[numBins];
        for(int i=0; i<numBins; i++){
            lowerLimit[i] = min + i*interval;
            upperLimit[i] = min + (i+1)*interval;
        }
        
        //Create TimeSeries graph
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(int j=0; j<numBins; j++){
            //Count data within the bin range
            int count = 0;
            for(int i=0; i < sortedData.length; i++) {
                if(data[i] > lowerLimit[j] && data[i] <= upperLimit[j]){
                    count++;
                }
            }
            double[] binCenter = {lowerLimit[j], upperLimit[j]};
            double temp = doubleMath.Average(binCenter)*100;
            String categoryTitle = String.valueOf(Math.round(temp/100));
            dataset.addValue(count, "All Data", categoryTitle);
            
            if(sortedData_user.length > 0){
                //Count user data within the bin range
                count = 0;
                for(int i=0; i < sortedData_user.length; i++) {
                    if(data_user[i] > lowerLimit[j] && data[i] <= upperLimit[j]){
                        count++;
                    }
                }
                dataset.addValue(count, "User Data", categoryTitle);
            }
        }
        
        //Define renderer properties for bar graph
        BarRenderer renderer = new BarRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setSeriesPaint(0, color);
        if(sortedData_user.length > 0){
            renderer.setSeriesPaint(1, Color.GRAY);
        }
        
        //Define axis and properties
        NumberAxis numberaxis = new NumberAxis("Count");
        CategoryAxis Xaxis = new CategoryAxis(xAxisTitle);
        
        //Graph the dataset using the renderer and create a chart
        CategoryPlot plot = new CategoryPlot(dataset, Xaxis, numberaxis, renderer);
        
        //Set extra plot preferences
        graphing.setCategoryAxisPreferences(plot);
        
        //Create the chart with the plot
        String graphTitle = "Histogram for Station: " + stationID + "; " + stationName;
        JFreeChart chart = new JFreeChart(graphTitle, graphing.titleFont, plot, false);
        
        //Save resulting graph for use later
        try{
            String path = mainFolder + File.separator + getHistogram();
            ChartUtilities.saveChartAsJPEG(new File(path), chart, 1280, 800);
            System.out.println("JFreeChart created properly at: " + path);
        }catch(IOException e){
            System.err.println("A problem occurred while trying to creating the chart.");
        }
    }
    /**
     * Graph the monthly average time series and user data and save the resulting graph to the specified location.
     * Also creates a boxplot for each month's data in a new graph
     * @param sortedData  the String[][] containing sorted data for the time series 
     * (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm if 
     * timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param color  the color of the merged dataset (Java.Color)
     * @param yAxisTitle  the String label for the y axis of the graph
     */
    private void createTimeseriesMonthlyGraphs(String[][] sortedData,
                                               Color color,
                                               String yAxisTitle) throws ParseException {
        //Convert sortedData to monthly data
        DoubleArray doubleArray = new DoubleArray();
        sortedData = doubleArray.computeFlowMethod(sortedData, "Monthly", "Average", false);
        
        //Calculate monthly averages for the entire dataset
        XYPlot boxPlot = new XYPlot();
        Graphing graphing = new Graphing();
        XYSeries series = new XYSeries(stationID + ": Data");
        boolean showOutliers = false, showExtremeOutliers = false;
        int boxSeriesIndex = 0;
        for(int j=1; j<=12; j++){
            ArrayList<Double> monthlyData = new ArrayList<Double>();
            double flow = 0;
            double ctr = 0;
            
            for(int i=0; i<sortedData.length; i++){
                double month = Double.parseDouble(sortedData[i][0].substring(5));
                double value = Double.parseDouble(sortedData[i][1]);
                int month_int = (int) month;
                if(month_int == j){
                    monthlyData.add(value);
                    flow = flow + value;
                    ctr = ctr + 1;
                }
            }
            //Calculate monthly average
            series.add(j, flow/ctr);
            
            //Create Monthly boxplot
            if(monthlyData.size() > 4){
                //Create quartile rectangle, min-max line, and median line
                Object[] returnArray = graphing.boxplot_shapes(boxPlot, j, monthlyData, boxSeriesIndex, showOutliers, showExtremeOutliers);
                boxPlot = (XYPlot) returnArray[0];
                showOutliers = (Boolean) returnArray[1];
                showExtremeOutliers = (Boolean) returnArray[2];
                boxSeriesIndex = (Integer) returnArray[3];
            }
        }
        
        //Check to show legend on boxplot graph
        boolean showBoxplotLegend = false;
        if(showOutliers || showExtremeOutliers){
            showBoxplotLegend = true;
        }
		
        //Create renderer, and axis for timeseries graph
        XYPlot plot = new XYPlot();
        int seriesIndex = 0;
        XYDataset xyDataset = new XYSeriesCollection(series);
        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        renderer.setSeriesPaint(seriesIndex, color);
        renderer.setSeriesVisibleInLegend(seriesIndex, false);
        renderer.setSeriesStroke(seriesIndex, 
                new BasicStroke(
                    3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {6.0f, 0.0f}, 0.0f
                ));
        
        //Set the line data, renderer, and axis into plot
        plot.setDataset(seriesIndex, xyDataset);
        plot.setRenderer(seriesIndex, renderer);
        seriesIndex++;

        //Map the line to the first Domain and first Range
        plot.mapDatasetToDomainAxis(0, 0);
        plot.mapDatasetToRangeAxis(0, 0);
        boxPlot.mapDatasetToDomainAxis(0, 0);
        boxPlot.mapDatasetToRangeAxis(0, 0);
        
        //Graph a line for each year of monthly data in time period
        String currentYear = start.substring(0,4);
        String finalYear = end.substring(0,4);
        boolean moreYears = sortedData.length > 0;
        while(moreYears){
            //Get current year's data and graph it
            String[][] partialData = doubleArray.getYearsData(sortedData, currentYear);
            double[][] currentYearData = new double[partialData.length][2];
            for(int i=0; i<partialData.length; i++){
                currentYearData[i][0] = Double.parseDouble(partialData[i][0].substring(5));//month
                currentYearData[i][1] = Double.parseDouble(partialData[i][1]);//value
            }
            graphing.graphSeries(plot, currentYearData, Color.lightGray, seriesIndex);
            seriesIndex++;

            int nextYear = Integer.parseInt(currentYear) + 1;
            if(finalYear.compareToIgnoreCase(String.valueOf(nextYear)) >= 0){
                currentYear = String.valueOf(nextYear);
            }else{
                moreYears = false;
            }
        }
        
        //Create Y Axis
        ValueAxis rangeAxis = new NumberAxis(yAxisTitle);
        plot.setRangeAxis(0, rangeAxis);
        ValueAxis boxYaxis = new NumberAxis(yAxisTitle);
        boxPlot.setRangeAxis(0, boxYaxis);
        
        //Create X Axis
        NumberTickUnit temp = new NumberTickUnit(1);
        NumberAxis domainAxis = new NumberAxis("Month");
        domainAxis.setRange(1, 12);
        domainAxis.setTickUnit(temp);
        plot.setDomainAxis(0, domainAxis);
        NumberAxis boxXaxis = new NumberAxis("Month");
        boxXaxis.setRange(1, 12);
        boxXaxis.setTickUnit(temp);
        boxPlot.setDomainAxis(0, boxXaxis);
        
        //Set extra plot preferences
        plot = graphing.setAxisPreferences(plot);
        boxPlot = graphing.setAxisPreferences(boxPlot);

        //Create the charts out of the plots
        String graph_title = "Monthly Averages for Station: " + stationID + "; " + stationName;
        JFreeChart chart = new JFreeChart(graph_title, graphing.titleFont, plot, false);
        JFreeChart boxChart = new JFreeChart(graph_title, graphing.titleFont, boxPlot, showBoxplotLegend);
        if(showBoxplotLegend){
            LegendTitle legendTitle = boxChart.getLegend();
            legendTitle.setItemFont(graphing.masterFont);
        }
        
        //Save monthly timeseries graph for use later
        try{
            String path = mainFolder + File.separator + getMonthlyGraph();
            ChartUtilities.saveChartAsJPEG(new File(path), chart, 1280, 800);
            System.out.println("JFreeChart created properly at: " + path);

        }catch(IOException e){
            System.err.println("A problem occurred while trying to creating the chart.");
        }
        
        //Save monthly boxplot graph
        try{
            String path = mainFolder + File.separator + getMonthlyBoxplot();
            ChartUtilities.saveChartAsJPEG(new File(path), boxChart, 1280, 800);
            System.out.println("JFreeChart created properly at: " + path);
        }catch(IOException e){
            System.out.println("A problem occurred while trying to creating the chart for station '" + stationName + "'. Error: USGSLDC0003");
            System.out.println(e.toString());
        }
    }
    /**
     * Graph the quantile plot CDF of the time series data and save the resulting graph to the specified location.
     * @param sortedData  the String[][] containing sorted data for the time series 
     * (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm if 
     * timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param sortedData_user  the String[][] containing sorted user data for the 
     * time series (column 1 = dates (yyyy-mm-dd if timeStep = "daily", yyyy-mm 
     * if timeStep = "monthly", yyyy if timeStep = "yearly") column 2 = value
     * @param color  the color of the merged dataset (Java.Color)
     * @param color2  the color of the user dataset (Java.Color)
     * @param xAxisTitle  the String label for the x axis of the graph
     */
    private void createTimeseriesCDF(String[][] sortedData,
                                     String[][] sortedData_user,
                                     Color color,
                                     Color color2,
                                     String xAxisTitle) throws ParseException {
        DoubleArray doubleArray = new DoubleArray();
        double[][] xyRanks = doubleArray.weibullPlottingPosition(sortedData);
        double[][] xyRanks_user = doubleArray.weibullPlottingPosition(sortedData_user);
        
        //Calculate monthly averages for the entire dataset
        XYSeries series = new XYSeries(stationID + ": Data");
        for(int i=0; i<xyRanks.length; i++){
            double x = xyRanks[i][1]; //flow
            double y = 1 - (xyRanks[i][0]/100); //rank
            series.add(x, y);
        }
        XYSeries series_user = new XYSeries(stationID + ": User Data");
        for(int i=0; i<xyRanks_user.length; i++){
            double x = xyRanks_user[i][1]; //flow
            double y = 1 - (xyRanks_user[i][0]/100); //rank
            series_user.add(x, y);
        }
		
        //Create renderer, and axis for timeseries graph
        XYPlot plot = new XYPlot();
        XYDataset xyDataset = new XYSeriesCollection(series);
        XYDataset xyDataset_user = new XYSeriesCollection(series_user);
        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        XYItemRenderer renderer_user = new XYLineAndShapeRenderer(true, false);
        renderer.setSeriesPaint(0, color);
        renderer_user.setSeriesPaint(0, color2);
        
        //Set the line data, renderer, and axis into plot
        plot.setDataset(0, xyDataset);
        plot.setDataset(1, xyDataset_user);
        plot.setRenderer(0, renderer);
        plot.setRenderer(1, renderer_user);

        //Map the line to the first Domain and first Range
        plot.mapDatasetToDomainAxis(0, 0);
        plot.mapDatasetToRangeAxis(0, 0);
        
        //Create Y Axis
        ValueAxis rangeAxis = new NumberAxis("Cumulative Frequency");
        rangeAxis.setRange(0, 1);
        plot.setRangeAxis(0, rangeAxis);
        
        //Create X Axis
        NumberAxis domainAxis = new NumberAxis(xAxisTitle);
        plot.setDomainAxis(0, domainAxis);
        
        //Set extra plot preferences
        Graphing graphing = new Graphing();
        plot = graphing.setAxisPreferences(plot);

        //Create the charts out of the plots
        String graph_title = "CDF for " + timeStep + " data at Station: " + stationID + "; " + stationName;
        JFreeChart chart = new JFreeChart(graph_title, graphing.titleFont, plot, false);
        
        //Save monthly timeseries graph for use later
        try{
            String path = mainFolder + File.separator + getCDF();
            ChartUtilities.saveChartAsJPEG(new File(path), chart, 1280, 800);
            System.out.println("JFreeChart created properly at: " + path);

        }catch(IOException e){
            System.err.println("A problem occurred while trying to creating the chart.");
        }
    }
    /**
     * Writes out the dynamically created paragraph to be displayed to the user along with the LDC graph
     * @param dynamicParagraph  string array to be written as each line of the text file
     * @param partialpath  the partial folder path of the file to be written
     * @throws IOException
     */
    private void writeSummary(String[] dynamicParagraph, String partialpath) throws IOException{
        String path = partialpath + File.separator + "timeseries_summary.txt";
        FileWriter writer =  new FileWriter(path, false);
        PrintWriter print_line = new PrintWriter(writer);

        //Output data to text file
        for(int i = 0; i < dynamicParagraph.length; i++) {
            print_line.printf("%s" + "%n", dynamicParagraph[i]);
        }
        print_line.close();
        writer.close();
        System.out.println("Text File located at:\t" + path);
    }
    /**
     * Writes out the error message, if any, for finding the file and then exits the program
     * @param error  string array to be written as each line of an error message
     * @throws IOException
     */
    public void writeError(ArrayList<String> error) throws IOException{
        //Output data to text file
        String errorContents = error.get(0);
        for(int i=1; i<error.size(); i++){
            errorContents = errorContents + "\n" + error.get(i);
        }
        throw new IOException("Error encountered. Please see the following message for details: \n" + errorContents);
    }
    /**
     * Primary TimeSeries
     * It calls the subfunctions based on user selection/inputs.
     * Calls STORET or USGS database queries and their respective subfunctions
     * @throws IOException 
     * @throws InterruptedException 
     */
    public void run() throws IOException, InterruptedException, ParseException {
        //Inputs
        //assert args.length > 0;
        //String mainFolder 		= args[0];
        //String fileName 		= args[1];
        //String organizationName = args[2];
        //String stationID 		= args[3];
        //String stationName		= args[4];
        //String wqTest 			= args[5];
        //String beginDate		= args[6];
        //String endDate	 		= args[7];
        //String timeStep			= args[8];
        //String method			= args[9];
        
        //If no date input, make it the maximum of available data
        if(beginDate == null || beginDate.equalsIgnoreCase("")){
            beginDate = "1900-01-01";
        }
        if(endDate == null || endDate.equalsIgnoreCase("")){
            // Pull current date for upper limit of data search
            DateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date currentDate = new Date();
            endDate = desiredDateFormat.format(currentDate);
        }
        
        //Artificial limit due to the properties of the Jfreechart object "Date"
        if(beginDate.compareToIgnoreCase("1900-01-01") < 0){
            beginDate = "1900-01-01";
        }
        if(endDate.compareToIgnoreCase("1900-01-01") < 0){
            endDate = "1900-01-01";
        }
        
        //Initialize graph variables
        String yAxisTitle = "y axis";
        String monthlyYaxisTitle = "y axis";
        String WQlabel = "??";
        String graphUnits = "??";
        Color color = Color.black, color2 = Color.black;
        boolean showLine = true;
        
        Data data = new Data();
        String[][] sortableData = new String[0][2];
        if(wqTest.equalsIgnoreCase("flow")){
            //Check if any flow data exists
            sortableData = data.extractFlowData(mainFolder, organizationName, stationID, beginDate, endDate, userData);

            //Define other graph information
            graphUnits = "cfs";
            yAxisTitle = timeStep + " " + method + " Flow [" + graphUnits + "]";
            monthlyYaxisTitle = "Monthly Average Flow [" + graphUnits + "]";
            color = Color.blue;
            color2 = Color.DARK_GRAY;
            showLine = true;
        }else{
            //Search for WQ data
            Object[] returnArray = data.extractWQdata(mainFolder, organizationName, stationID, beginDate, endDate, userData, wqTest);
            sortableData = (String[][]) returnArray[0];
            graphUnits = (String) returnArray[1];
            WQlabel = (String) returnArray[2];

            //Define other graph information
            yAxisTitle = timeStep + " " + method + " " + WQlabel + " [" + graphUnits + "]";
            monthlyYaxisTitle = "Monthly Average " + WQlabel + " [" + graphUnits + "]";
            color = Color.magenta;
            color2 = Color.BLUE;
            showLine = false;
        }
        
        //Check if merging the datasets is desired, if so get the user data
        String[][] sortableData_user = new String[0][0];
        if(mergeDatasets){
            User_Data user_Data = new User_Data();
            sortableData_user = user_Data.readUserFile(userData, wqTest, beginDate, endDate);
        }
        
        //Sort the Data by date to remove duplicate date entries
        DoubleArray doubleArray = new DoubleArray();
        String[][] sortedData = doubleArray.removeDuplicateDates(sortableData);
        String[][] sortedData_user = doubleArray.removeDuplicateDates(sortableData_user);

        //Merge the two datasets (if user data is empty nothing will be merged)
        String[][] sortedData_combined = doubleArray.mergeData(sortedData, sortedData_user, mergeMethod);
        if(sortedData_combined.length == 0){
            ArrayList<String> errorMessage = new ArrayList<String>();
            if(sortableData.length == 0){
                String database = "USGS";
                if(!organizationName.equals("USGS")){
                    database = "STORET";
                }
                errorMessage.add("There is no available " + wqTest + " data in the " + database + " database for station '" + stationID + "' and the specified date range.");
            }
            if(sortedData_user.length == 0){
                errorMessage.add("There is no available uploaded data for station '" + stationID + "' and the specified date range");
            }
            writeError(errorMessage);
        }
        
        //Create monthly average chart from daily data before it is converted to another timestep
        this.start = sortedData_combined[0][0];
        this.end = sortedData_combined[sortedData_combined.length - 1][0];
        createTimeseriesMonthlyGraphs(sortedData_combined, color, monthlyYaxisTitle);//this is here because it needs to operate on the original daily data
        //Calculate additional stats of daily data: Hydrologic Indicators of Alteration
        FlowStatistics flowStats = new FlowStatistics();
        flowStats.calculateAnnualStatisticsSummaries(mainFolder, sortedData_combined, highPercentile, lowPercentile, 0, 0, graphUnits);
        
        //Perform analysis method on data
        sortedData_combined = doubleArray.computeFlowMethod(sortedData_combined, timeStep, method, false);
        sortedData_user = doubleArray.computeFlowMethod(sortedData_user, timeStep, method, false);
        
        //Calculate stats of data
        ArrayList<Double> dataList = new ArrayList<Double>();
        for(int i=0; i<sortedData_combined.length; i++){
            dataList.add(Double.parseDouble(sortedData_combined[i][1]));
        }
        CalculateStatistics(dataList, "all");
        
        //Calculate outliers for JHighChart boxplot output file
        double IQR = upperQuartile - lowerQuartile;// Find IQR
    	ArrayList<Double> outliers = new ArrayList<Double>();// Get daily outliers
    	for (int i = 0; i < dataList.size(); i++){
            if(dataList.get(i) < (lowerQuartile - 1.5 * IQR) || dataList.get(i) > (upperQuartile + 1.5 * IQR)){		
                outliers.add(dataList.get(i));
            }
    	}
        
        //Write output for JHighChart boxplot and timeseries for use on eRAMS
        doubleArray.writeBoxplot(mainFolder, lowerQuartile, upperQuartile, outliers, median, min, "timeseries");
        doubleArray.writeTimeSeries(mainFolder, sortedData_combined, timeStep, "timeseries", false);
        
        //Graph the timeseries data
        createTimeseriesGraph(sortedData_combined, sortedData_user, color, color2, showLine, yAxisTitle, graphUnits, medianTF);
        createTimeseriesBoxplot(sortedData_combined, yAxisTitle);
        createTimeseriesHistogram(sortedData_combined, sortedData_user, color, yAxisTitle);
        createTimeseriesCDF(sortedData_combined, sortedData_user, color, color2, yAxisTitle);
        
        //Create dynamic summary paragraph
        this.len = String.valueOf(sortedData_combined.length);
        this.units = graphUnits;
        
        //Create the correct dynamic paragraph
        String[] dynamic_paragraph = new String[9];
        dynamic_paragraph[0] = "Time Series Graph Overview: ";
        dynamic_paragraph[1] = "A time series graph is a straight scale graphing of available flow data with the oldest date on the bottom left and the most recent date on the bottom right with flows on the y axis.  This can be useful to identify hydrographs from storm runoff for small time frames (ie. less than a couple days worth of data points)";
        dynamic_paragraph[2] = "";
        dynamic_paragraph[3] = "";
        dynamic_paragraph[4] = "References:";
        dynamic_paragraph[6] = "Cleland, B. R. November 2003. TMDL Development from the 'Bottom Up' Part III: Duration Curves and Wet-Weather Assessments. National TMDL Science and Policy 2003."; 
        dynamic_paragraph[7] = "Cleland, B. R. August 2007. An Approach for Using Load Duration Curves in the Development of TMDLs. National TMDL Science and Policy 2007.";
        
        //Get today's date for the source reference
        DateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date currentDate = new Date();
        String today = desiredDateFormat.format(currentDate);
        if(organizationName.equals("USGS")){
            dynamic_paragraph[5] = "Stream flow data and water quality test data courtesy of the U.S. Geological Survey, National Water Information System: Web Interface. http://waterdata.usgs.gov/nwis, accessed " + today;
        }else{
            dynamic_paragraph[5] = "Stream flow data and water quality test data courtesy of the U.S. Environmental Protection Agency, STORET. http://www.epa.gov/storet/index.html accessed " + today;
        }
        

        //Write dynamic Paragraph to text file
        writeSummary(dynamic_paragraph, mainFolder);
    }
    public static void main(String[] args) throws IOException, InterruptedException, Exception {
        guiTimeseries_Model timeseries_Model = new guiTimeseries_Model();
        
        //Run model
        timeseries_Model.run();
    }
}