guiDownload_Data.java [src/java/cfa] Revision: cb4f727e2ce1fb1fddb7c6c6259950fdaed2df8b  Date: Fri Jul 11 16:39:28 MDT 2014
package cfa;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
* Last Updated: 11-July-2014
* @author Tyler Wible
* @since 21-June-2012
*/
public class guiDownload_Data {
    String mainFolder = "C:/Projects/TylerWible/CodeDirectories/NetBeans/CSIP/data/CFA";
    String database = "USGS";//"CDWR";//"STORET";//"UserData";//
    String organizationName = "USGS";//"Co. Division of Water Resources";//"Colorado Dept. of Public Health & Environment";//"California Gulch (US EPA Region 8)";
    String stationID = "06752260";//"CLAGRECO";//"000028";//"EF-01";//"L01";//
    String wqTest = "flow";//"00600";//"Nitrogen, Nitrate (NO3) as NO3";//"Fecal Coliform";//"15-min flow";//"stage-discharge";//
    String beginDate = "";
    String endDate = "";
    
    
    //Outputs
    String start = "-1";
    String end = "-1";
    
    
    //Gets
    public File getOutput() {
        return new File(mainFolder, "CFA_dataDownload.txt");
    }
    public String getStart(){
        return start;
    }
    public String getEnd(){
        return end;
    }
    
    //Sets
     public void setMainFolder(String mainFolder) {
        this.mainFolder = mainFolder;
    }
    public void setDatabase(String database) {
        this.database = database;
    }
    public void setOrganizationName(String organizationName) {
        this.organizationName = organizationName;
    }
    public void setStationID(String stationID) {
        this.stationID = stationID;
    }
    public void setWaterQualityTest(String wqTest) {
        this.wqTest = wqTest;
    }
    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
    /**
     * Main CDWR data download subroutine
     * @return  an ArrayList<String> containing the result file of the download request
     * @throws Exception 
     */
    private ArrayList<String> downloadCDWRdata() throws Exception{
        CDWR_Data cdwr_Data = new CDWR_Data();
        ArrayList<String> textData = new ArrayList<String>();
        
        //Determine the type of download for CDWR: flow, hourly-flow, no aggrigation (15-min) flow, or stage-discharge
        if(wqTest.equalsIgnoreCase("flow")){
            //Search for CDWR flow data
            Object[] returnArray = cdwr_Data.getCDWRflowData(stationID, beginDate, endDate, "Daily");
            textData = (ArrayList<String>) returnArray[0];
            //flowData = (String[][]) returnArray[1];
            start = (String) returnArray[2];
            end = (String) returnArray[3];

        }else if(wqTest.equalsIgnoreCase("Flood Flow") || wqTest.equalsIgnoreCase("15-min flow")){
            //Search for CDWR flow data
            Object[] returnArray = cdwr_Data.getCDWRflowData(stationID, beginDate, endDate, "15-min");
            textData = (ArrayList<String>) returnArray[0];
            //flowData = (String[][]) returnArray[1];
            start = (String) returnArray[2];
            end = (String) returnArray[3];

        }else if(wqTest.equalsIgnoreCase("stage-discharge")){
            Object[] returnArray = cdwr_Data.getCDWRratingCurve(stationID);
            textData = (ArrayList<String>) returnArray[0];
            //double[][] ratingCurve = (double[][]) returnArray[1];

        }else{
            ArrayList<String> errorMessage = new ArrayList<String>();
            errorMessage.add("There is no available data available for " + wqTest + " in the CDWR database. This feature is only available for stations in the USGS or STORET databases.");
            writeError(errorMessage);
        }
        
        //Check if there is actually data
        if(textData.size() == 1){
            //If only the headers and no data, replace with an error 
            textData.set(0, "There is no available flow data in the " + database + " database for station '" + stationID + "' and the specified date range.");
            textData.add("The CDWR database is sensitive to the begin date used, try specifying a later begin date");
        }
        
        return textData;
    }
    /**
     * Main STORET data download subroutine
     * @return  an ArrayList<String> containing the result file of the download request
     * @throws IOException 
     */
    private ArrayList<String> downloadSTORETdata() throws IOException{
        STORET_Data storet_Data = new STORET_Data();
        ArrayList<String> textData = new ArrayList<String>();
        
        //Determine the type of download for STORET: flow, water quality (individual test)
        if(wqTest.equalsIgnoreCase("Flood Flow")){
            //Since STORET doesn't have annual flood flow values, just return normal flow data to the user
            wqTest = "flow";
        }else if(wqTest.equalsIgnoreCase("15-min flow")){
            //Since STORET doesn't have 15-minute data, thrown an error and alert the user of this
            ArrayList<String> errorMessage = new ArrayList<String>();
            errorMessage.add("There is no available 15-minute flow data available for the STORET database. This feature is only available for stations in the USGS NWIS database.");
            writeError(errorMessage);
        }else if(wqTest.equalsIgnoreCase("stage-discharge")){
            //Since STORET doesn't have stage-discharge/rating curve data, thrown an error and alert the user of this
            ArrayList<String> errorMessage = new ArrayList<String>();
            errorMessage.add("There is no available stage-discharge data available for the STORET database. This feature is only available for stations in the USGS NWIS database.");
            writeError(errorMessage);
        }
        String zip_location = storet_Data.downloadSTORET(mainFolder, organizationName, stationID, wqTest, beginDate, endDate);

        //Unzip results file and extract all flow data
        textData = storet_Data.Unzip_STORETDownloadFilesAll(zip_location);
        
        return textData;
    }
    /**
     * Main USGS data download subroutine
     * @return  an ArrayList<String> containing the result file of the download request
     * @throws IOException
     * @throws InterruptedException 
     */
    private ArrayList<String> downloadUSGSdata() throws IOException, InterruptedException{
        USGS_Data usgs_Data = new USGS_Data();
        ArrayList<String> textData = new ArrayList<String>();
        
        //Determine the type of download for USGS: flow, flood flow, 15-minute-flow, stage-discharge, WQ data (all), WQ data (individual test)
        if(wqTest.equalsIgnoreCase("flow")){
            //Retrieve flow data from USGS website
            Object[] returnArray = usgs_Data.getUSGSflowData(stationID, beginDate, endDate);
            textData = (ArrayList<String>) returnArray[0];
            //String[][] flowData = (String[][]) returnArray[1];
            start = (String) returnArray[2];
            end = (String) returnArray[3];

        }else if(wqTest.equalsIgnoreCase("Flood Flow")){
            //Retrieve flood flow data from USGS website
            Object[] returnArray = usgs_Data.getUSGSPeakData(stationID, beginDate, endDate);
            textData = (ArrayList<String>) returnArray[0];
            //double[][] floodData = (double[][]) returnArray[1];
            start = String.valueOf((double) returnArray[2]);
            end = String.valueOf((double) returnArray[3]);

        }else if(wqTest.equalsIgnoreCase("15-min flow")){
            //Retrive 15-minute flow data from USGS website
            Object[] returnArray = usgs_Data.getUSGS15minFlowData(stationID, beginDate, endDate);
            textData = (ArrayList<String>) returnArray[0];
            //String[][] flowData = (String[][]) returnArray[1];
            start = (String) returnArray[2];
            end = (String) returnArray[3];

        }else if(wqTest.equalsIgnoreCase("stage-discharge")){
            //Retrive rating curve data from USGS website
            Object[] returnArray = usgs_Data.getUSGSratingCurve(stationID);
            textData = (ArrayList<String>) returnArray[0];
            //double[][] ratingCurve = (double[][]) returnArray[1];

        }else if(wqTest.equalsIgnoreCase("all")){
            //Retrive all WQ data from USGS website
            Object[] returnArray = usgs_Data.getUSGSwqData(stationID);
            textData = (ArrayList<String>) returnArray[0];
            //String[][] allWQdata = (String[][]) returnArray1[1];
            start = (String) returnArray[2];
            end = (String) returnArray[3];

        }else{
            //Retrive only the selected WQ data from USGS website
            textData = usgs_Data.getUSGSwqData_partial(stationID, wqTest);
        }
        
        return textData;
    }
    /**
     * 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);
    }
    /**
     * Writes out the output file to the specified location
     * @param textData  ArrayList<String> to be written as each line of the text file
     * @throws IOException
     */
    public void writeOutputFile(ArrayList<String> textData) throws IOException{
        String path = mainFolder + File.separator + getOutput().getName();
        FileWriter writer =  new FileWriter(path, false);
        PrintWriter print_line = new PrintWriter(writer);

        //Output data to text file
        for(int i=0; i<textData.size(); i++){
            print_line.printf("%s" + "%n", textData.get(i));
        }
        print_line.close();
        writer.close();
        System.out.println("File located at: " + path);
    }
    /**
     * Primary download function, queries CDWR, STORET, or USGS database
     * @throws IOException 
     * @throws InterruptedException 
     */
    public void run() throws IOException, InterruptedException, Exception {
        //Inputs
//        assert args.length > 0;
//        String mainFolder 	= args[0];
//        String fileName 	= args[1];
//        String organizationName = args[2];
//        String stationID 	= args[3]; 
//        String wqTest 		= args[4];
//        String beginDate 	= args[5];
//        String endDate 		= args[6];


        //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);
        }
		
        //Determine the requested download type and download data from the correct site
        ArrayList<String> textData = new ArrayList<String>();
        if(database.equalsIgnoreCase("USGS")){
            textData = downloadUSGSdata();
            
        }else if(database.equalsIgnoreCase("STORET")){
            textData = downloadSTORETdata();
            
        }else if(database.equalsIgnoreCase("CDWR")){
            textData = downloadCDWRdata();
        }

        //write out data to text file
         writeOutputFile(textData);
    }
    public static void main(String[] args) throws IOException, InterruptedException, Exception{
        guiDownload_Data downloadModel = new guiDownload_Data();

        //Run Model
        downloadModel.run();
    }
}