Data.java [src/java/cfa] Revision: b385354fd36172ba68b5ca842b4a63ac7e555424  Date: Mon Jun 09 10:19:52 MDT 2014
package cfa;
import java.io.IOException;

/**
* Last Updated: 9-February-2014
* @author Tyler Wible
* @since 25-January-2014
*/
public class Data {
    public String[][] extractFlowData(String mainFolder, String organizationName, String stationID, String beginDate, String endDate, String userData) throws IOException, InterruptedException{
        //Depending on the provided inputs, search for and return flow data
        USGS_Data usgs_Data = new USGS_Data();
        User_Data user_Data = new User_Data();
        STORET_Data storet_Data = new STORET_Data();
        
        String[][] flowData = new String[0][2];
        if(organizationName.equalsIgnoreCase("USGS")){
            
            //Search for USGS flow data
            flowData = usgs_Data.getUSGSflowData(stationID, beginDate, endDate);
            //If there is minimal flow data, extract discharge data from the water quality database
            if(flowData.length < 10){
                //Retrieve all WQ data from USGS website
                String[][] allWQdata = usgs_Data.getUSGSwqData(stationID);
                //Extract and combine USGS discharge water quality codes with the flow dataset
                flowData = usgs_Data.getUSGSwqFlowData(flowData, allWQdata, beginDate, endDate);
            }

        }else if(organizationName.equalsIgnoreCase("UserData")){
            //Find the user uploaded data file and uses this for a timeseries graph
            flowData = user_Data.readUserFile(userData, "flow", beginDate, endDate);

        }else{
            //Search for STORET peak flow data
            String zip_location = storet_Data.downloadSTORET(mainFolder, organizationName, stationID, "flow", beginDate, endDate);

            //Unzip results file and extract all flow data
            flowData = storet_Data.Unzip_STORETDownloadFiles(zip_location, "flow", true);
        }
        
        return flowData;
    }
    public Object[] extractWQdata(String mainFolder, String organizationName, String stationID, String beginDate, String endDate, String userData, String wqTest) throws IOException, InterruptedException{
        USGS_Data usgs_Data = new USGS_Data();
        User_Data user_Data = new User_Data();
        STORET_Data storet_Data = new STORET_Data();
        
        String[][] WQdata = new String[0][2];
        String WQlabel = "??";
        String graphUnits = "??";
        if(organizationName.equalsIgnoreCase("USGS")){            
            if(wqTest.length() > 5){
                //Pull only the code portion of the WQ test
                int endIndex = wqTest.lastIndexOf(", ");
                if(endIndex == -1){
                    endIndex = wqTest.lastIndexOf("--");
                }
                WQlabel = wqTest.substring(11,endIndex);//cut off the "98335      " part before the test name and the units after the name
                WQlabel = WQlabel.split(",")[0];
                wqTest = wqTest.substring(0,5);//pull just the 5 digit USGS WQ code
            }
            
            //Retrieve all WQ data from USGS website
            String[][] allWQdata = usgs_Data.getUSGSwqData(stationID);
            //Extract USGS water quality code for current wqTest only
            WQdata = usgs_Data.minimizeUSGSWQdata(allWQdata, wqTest, beginDate, endDate);
            
            //Get Units and conversion for current WQ test
            graphUnits = usgs_Data.getUSGSwqUnits(wqTest);            

        }else if(organizationName.equalsIgnoreCase("UserData")){
            if(wqTest.length() > 5){
                //Pull only the code portion of the WQ test
                int endIndex = wqTest.lastIndexOf(", ");
                if(endIndex == -1){
                    endIndex = wqTest.lastIndexOf("--");
                }
                WQlabel = wqTest.substring(11,endIndex);//cut off the "98335      " part before the test name and the units after the name
                WQlabel = WQlabel.split(",")[0];
                wqTest = wqTest.substring(0,5);//pull just the 5 digit USGS WQ code
            }
            
            //Find the user uploaded data file and uses this for a timeseries graph
            Object[] returnArray = user_Data.readUserFile(userData, wqTest, beginDate, endDate);
            WQdata = (String[][]) returnArray[1];
            
            //Use the header to get the WQ test name
            graphUnits = usgs_Data.getUSGSwqUnits(wqTest);//Because user uploaded file headers are wqTest
            
        }else{
            //Search for STORET flow data
            String zipLocation = storet_Data.downloadSTORET(mainFolder, organizationName, stationID, "flow", beginDate, endDate);
            
            //Unzip results file and extract all flow and WQ results
            WQdata = storet_Data.Unzip_STORETDownloadFiles(zipLocation, wqTest, true);
            WQlabel = wqTest;
            graphUnits = "mg/L";
        }
        
        Object[] returnArray = {WQdata, graphUnits, WQlabel};
        return returnArray;
    }
    public Object[] extractFlow_and_WQdata(String mainFolder, String organizationName, String stationID, String beginDate, String endDate, String userData, String wqTest) throws IOException, InterruptedException{
        USGS_Data usgs_Data = new USGS_Data();
        User_Data user_Data = new User_Data();
        STORET_Data storet_Data = new STORET_Data();
        
        String[][] flowData = new String[0][2];
        String[][] WQdata = new String[0][2];
        if(organizationName.equalsIgnoreCase("USGS")){
            
            //Search for USGS flow data
            flowData = usgs_Data.getUSGSflowData(stationID, beginDate, endDate);
            //Retrieve all WQ data from USGS website
            String[][] allWQdata = usgs_Data.getUSGSwqData(stationID);
            
            if(wqTest.length() > 5){
                //Pull only the code portion of the WQ test
                int endIndex = wqTest.lastIndexOf(", ");
                if(endIndex == -1){
                    endIndex = wqTest.lastIndexOf("--");
                }
                String WQlabel = wqTest.substring(11,endIndex);//cut off the "98335      " part before the test name and the units after the name
                WQlabel = WQlabel.split(",")[0];
                wqTest = wqTest.substring(0,5);//pull just the 5 digit USGS WQ code
            }
            
            //Extract USGS water quality code for current wqTest only
            WQdata = usgs_Data.minimizeUSGSWQdata(allWQdata, wqTest, beginDate, endDate);
            //Extract and combine USGS discharge water quality codes with the flow dataset
            flowData = usgs_Data.getUSGSwqFlowData(flowData, allWQdata, beginDate, endDate);

        }else if(organizationName.equalsIgnoreCase("UserData")){
            //Find the user uploaded data file and uses this for a timeseries graph
            Object[] returnArray = user_Data.readUserFileLDC(userData, wqTest, beginDate, endDate);
            flowData = (String[][]) returnArray[0];
            WQdata = (String[][]) returnArray[1];
            
        }else{
            //Search for STORET flow data
            String zipLocation = storet_Data.downloadSTORET(mainFolder, organizationName, stationID, "flow", beginDate, endDate);

            //Unzip results file and extract all flow and WQ results
            flowData = storet_Data.Unzip_STORETDownloadFiles(zipLocation, "flow", false);
            WQdata = storet_Data.Unzip_STORETDownloadFiles(zipLocation, wqTest, true);
        }
        
        Object[] returnArray = {flowData, WQdata};
        return returnArray;
    }
    public double[][] extractFloodData(String mainFolder, String organizationName, String stationID, String beginDate, String endDate, String userData) throws IOException{
        USGS_Data usgs_Data = new USGS_Data();
        User_Data user_Data = new User_Data();
        STORET_Data storet_Data = new STORET_Data();
        DoubleArray doubleArray = new DoubleArray();
        
        double[][] peakFlowData = new double[0][0];
        if(organizationName.equalsIgnoreCase("USGS")){
            //Search for USGS peak flow data
            peakFlowData = usgs_Data.getUSGSPeakData(stationID, beginDate, endDate);

        }else if(organizationName.equalsIgnoreCase("UserData")){
            //Find the user uploaded data file and uses this for a timeseries graph
            String[][] flowData = user_Data.readUserFile(userData, "flow", beginDate, endDate);

            //Removed duplicate dates
            flowData = doubleArray.removeDuplicateDates(flowData);

            //Convert into an annual peak time series
            peakFlowData = doubleArray.convertSTORETpeakData(flowData);

        }else{
            //Search for STORET peak flow data
            System.out.println("calling downloadSTORET");
            String zip_location = storet_Data.downloadSTORET(mainFolder, organizationName, stationID, "flow", beginDate, endDate);

            //Unzip results file and extract all flow data
            String[][] flowData = storet_Data.Unzip_STORETDownloadFiles(zip_location, "flow", true);
            
            //Removed duplicate dates
            flowData = doubleArray.removeDuplicateDates(flowData);

            //Convert into an annual peak time series
            peakFlowData = doubleArray.convertSTORETpeakData(flowData);
        }
        
        return peakFlowData;
    }
}