WaterData_User.java [src/WaterData] Revision: default  Date:
package WaterData;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
* Last Updated: 9-April-2019
* @author Tyler Wible
* @since 12-July-2012
*/
public class WaterData_User implements WaterDataInterface {
    public String database = "UserData";
    /**
     * * @param userDataRaw  The tab-delimited string of user data with header  
     * The expected format of the string is a tab-delimited text file 
     * (\n separating lines) with dates in the first column and daily 
     * average cfs values in the second column (\t separating columns).  
     * (Acceptable date formats for the first column are:
     * yyyy-mm-dd, 
     * yyyy-mm-d, 
     * yyyy-m-dd, 
     * yyyy-m-d, 
     * yyyy/mm/dd, 
     * yyyy/mm/d, 
     * yyyy/m/dd, 
     * yyyy/m/d)
     * (ex. "Date\tFlow\n1999-04-29\t8.3\n1999-05-09\t60.2\n1999-05-29\t20.1")
     */
    String userDataRaw = null;
    public WaterData_User(String userDataProvided) {
        userDataRaw = userDataProvided;
    }

    @Override
    public String getDataSourceCitation(){
        //Get today's date for the source reference
        DateFormat sourceDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String today = sourceDateFormat.format(new Date());
        
        //Cite user uploaded data
        String dataSource = "Stream flow data provided by the user. Flow analysis accessed: " + today;
        return dataSource;
    }
    
    @Override
    public ArrayList<String> extractFlowData_raw(String directory, String orgId, String stationId, String startDate, String endDate) throws WaterDataException {
        //Parse flow data from the raw user data string
        ArrayList<String> stationData = UserData.parseUserDataRaw(userDataRaw, startDate, endDate, "flow");
        return stationData;
    }

    @Override
    public String[][] extractFlowData_formatted(String directory, String orgId, String stationId, String startDate, String endDate) throws WaterDataException {
        //Fetch flow data
        ArrayList<String> stationData = extractFlowData_raw(directory, orgId, stationId, startDate, endDate);
        
        //Reformat data
        String[][] returnArray = new String[stationData.size()][2];
        for(int i=0; i<returnArray.length; i++){
            String[] currentColumns = stationData.get(i).split("\t");
            //currentColumns[0] = date
            //currentColumns[1] = value
            returnArray[i][0] = currentColumns[0];
            returnArray[i][1] = currentColumns[1];
        }
        return returnArray;
    }

    @Override
    public ArrayList<String> extractWaterQualityData_raw(String directory, String orgId, String stationId, String startDate, String endDate, String wqTest) throws WaterDataException {
        //Parse water quality data from the raw user data string
        ArrayList<String> stationData = UserData.parseUserDataRaw(userDataRaw, startDate, endDate, wqTest);
        return stationData;
    }

    @Override
    public String[][] extractWaterQualityData_formatted(String directory, String orgId, String stationId, String startDate, String endDate, String wqTest) throws WaterDataException {
        //Fetch water quality data
        ArrayList<String> stationData = extractWaterQualityData_raw(directory, orgId, stationId, startDate, endDate, wqTest);
        
        //Reformat data
        String[][] returnArray = new String[stationData.size()][2];
        for(int i=0; i<returnArray.length; i++){
            String[] currentColumns = stationData.get(i).split("\t");
            //currentColumns[0] = date
            //currentColumns[1] = value
            returnArray[i][0] = currentColumns[0];
            returnArray[i][1] = currentColumns[1];
        }
        return returnArray;
    }

    @Override
    public ArrayList<String> extractFloodData_raw(String directory, String orgId, String stationId, String startDate, String endDate) throws WaterDataException {
        //Parse flow data from the raw user data string
        ArrayList<String> stationData = UserData.parseUserDataRaw(userDataRaw, startDate, endDate, "flood");
        return stationData;
    }

    @Override
    public double[][] extractFloodData_formatted(String directory, String orgId, String stationId, String startDate, String endDate) throws WaterDataException {
        //Fetch flow data
        ArrayList<String> stationData = extractFloodData_raw(directory, orgId, stationId, startDate, endDate);
        
        //Reformat data
        double[][] returnArray = new double[stationData.size()][2];
        for(int i=0; i<returnArray.length; i++){
            String[] currentColumns = stationData.get(i).split("\t");
            //currentColumns[0] = date (year)
            //currentColumns[1] = value
            returnArray[i][0] = Double.valueOf(WaterQualityInfo.getWaterYear(currentColumns[0]));
            returnArray[i][1] = Double.valueOf(currentColumns[1]);
        }
        return returnArray;
    }

    @Override
    public ArrayList<String> extractInstantaneousFlowData_raw(String directory, String stationId, String startDate, String endDate) throws WaterDataException {
        //Parse flow data from the raw user data string
        ArrayList<String> stationData = UserData.parseUserDataRaw(userDataRaw, startDate, endDate, "flow");
        return stationData;
    }

    @Override
    public String[][] extractInstantaneousFlowData_formatted(String directory, String stationId, String startDate, String endDate) throws WaterDataException {
        //Fetch flow data
        ArrayList<String> stationData = extractInstantaneousFlowData_raw(directory, stationId, startDate, endDate);
        
        //Reformat data
        String[][] returnArray = new String[stationData.size()][2];
        for(int i=0; i<returnArray.length; i++){
            String[] currentColumns = stationData.get(i).split("\t");
            //currentColumns[0] = date
            //currentColumns[1] = value
            returnArray[i][0] = currentColumns[0];
            returnArray[i][1] = currentColumns[2];
        }
        return returnArray;
    }

    @Override
    public ArrayList<String> extractStageDischarge_raw(String stationId) throws WaterDataException {
        throw new WaterDataException("The " + database + " database contains no stage-discharge (rating curve) data.");
    }

    @Override
    public double[][] extractStageDischarge_formatted(String stationId) throws WaterDataException {
        throw new WaterDataException("The " + database + " database contains no stage-discharge (rating curve) data.");
    }
}