guiDownload_Data.java [src/java/cfa] Revision: 9c9d8df5ca0e3a6720b7a988951da99376960c05  Date: Thu Jun 05 11:52:26 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: 25-January-2014
* @author Tyler Wible
* @since 21-June-2012
*/
public class guiDownload_Data {
    String mainFolder = "C:/Projects/TylerWible/CodeDirectories/NetBeans/CSIP/data/CFA";
    String organizationName = "USGS";//"Colorado Dept. of Public Health & Environment";//"California Gulch (US EPA Region 8)";
    String stationID = "06752260";//"000028";//"EF-01";//"L01";
    String wqTest = "flow";//"00600";//"Nitrogen, Nitrate (NO3) as NO3";//"Fecal Coliform";//
    String beginDate = "";
    String endDate = "";
        
        
    //Gets
    public File getOutput() {
        return new File(mainFolder, "CFA_dataDownload.txt");
    }
    
    //Sets
     public void setMainFolder(String mainFolder) {
        this.mainFolder = mainFolder;
    }
    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;
    }
    /**
     * Writes out the output file to the specified location
     * @param textData  ArrayList<String> to be written as each line of the text file
     * @param partialpath  the partial folder path of the file to be written
     * @throws IOException
     */
    public void writeOutputFile(ArrayList<String> textData, String partialpath) throws IOException{
        String path = partialpath + File.separator + "CFA_dataDownload.txt";
        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 LDC download program
     * 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 {
        //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);
        }
		
        //Create result text files
        ArrayList<String> textData = new ArrayList<String>();
        //Determine the requested download type and download data from the correct site (USGS or STORET)
        if(organizationName.equalsIgnoreCase("USGS")){
            USGS_Data usgs_Data = new USGS_Data();
            //Determine the type of download for USGS, flow, flood flow, WQ data (some), WQ data (all)
            if(wqTest.equalsIgnoreCase("flow")){
                //Retrieve flow data from USGS website
                textData = usgs_Data.DownloadFlowWebpage(stationID, beginDate, endDate);

            }else if(wqTest.equalsIgnoreCase("Flood Flow")){
                //Retrieve flood flow data from USGS website
                textData = usgs_Data.DownloadPeakFlowWebpage(stationID);

            }else if(wqTest.equalsIgnoreCase("all")){
                //Retrive all WQ data from USGS website
                textData = usgs_Data.DownloadWQwebpage(stationID);

            }else{
                //Retrive only the selected WQ data from USGS website
                textData = usgs_Data.getUSGSwqData_partial(stationID, wqTest);
            }
        }else{
            //Search for STORET data
            STORET_Data storet_Data = new STORET_Data();
            if(wqTest.equalsIgnoreCase("Flood Flow")){
                //Since STORET doesn't have annual flood flow values, just return normal flow data to the user
                wqTest = "flow";
            }
            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);
        }


        //write out data to text file
         writeOutputFile(textData, mainFolder);
    }
    public static void main(String[] args) throws IOException, InterruptedException{
        guiDownload_Data downloadModel = new guiDownload_Data();
        
        //Set inputs
//        assert args.length > 0;
//        downloadModel.setMainFolder(args[0]);
//        downloadModel.setFileName(args[1]);
//        downloadModel.setOrganizationName(args[2]);
//        downloadModel.setStationID(args[3]);
//        downloadModel.setWaterQualityTest(args[4]);
//        downloadModel.setBeginDate(args[5]);
//        downloadModel.setEndDate(args[6]);

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