DoubleArray.java [src/java/cfa] Revision: a4cafa17609dccf465be70c05ea2c0eb461c7bb8 Date: Fri Jun 06 11:22:44 MDT 2014
package cfa;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
class DateComparator implements Comparator<String[]>{
//Compares the first entry of sorted data as flow values and sorts largest to smallest
public int compare(final String[] entry1, final String[] entry2) {
String value1 = entry1[0];
String value2 = entry2[0];
int comparison = value1.compareTo(value2);
if(comparison>0){
return 1;
}else if(comparison<0){
return -1;
}else{
return 0;
}
}
}
/**
* Last Updated: 5-June-2014
* @author Tyler Wible
* @since 21-June-2012
*/
public class DoubleArray {
/**
* Append a value on to the end of list of values
* @param currentArray current list of values (double[])
* @param newValue new value to append to the list
* @return the new list containing the currentArray list of values and the appened newValue
*/
public double[] appendcolumn(double[] currentArray, double newValue){
//Initialize new array to contain the new value
double[] newArray = new double[currentArray.length + 1];
//Fill the new array
for(int i=0; i<currentArray.length; i++){
newArray[i] = currentArray[i];
}
//Append newValue
newArray[currentArray.length] = newValue;
return newArray;
}
/**
* Append a value on to the end of list of values
* @param currentArray current list of values (double[])
* @param newValue new value to append to the list
* @return the new list containing the currentArray list of values and the appened newValue
*/
public Double[] appendcolumn(double[] currentArray, double[] newValue){
//Initialize new array to contain the new value
Double[] newArray = new Double[currentArray.length + newValue.length];
//Fill the new array
for(int i=0; i<newArray.length; i++){
if(i<currentArray.length){
newArray[i] = currentArray[i];
}else{
newArray[i] = newValue[i - currentArray.length];
}
}
return newArray;
}
/**
* Appends an array onto another array to create a 2D array. double[][] newArray = {currentArray, newArray};
* @param currentArray1 current list of values (double[])
* @param currentArray2 new value to append to the list (double[])
* @return the new list containing the currentArray list of values and the appened newValue
*/
public double[][] appendcolumn_Matrix(double[] currentArray1, double[] currentArray2){
//Check that the arrays are the same size to be combined
if(currentArray1.length != currentArray2.length){
System.out.println("The arrays are not the same size so they can not be combined");
return new double[0][0];
}
//Initialize new array to contain the new value
double[][] newArray = new double[currentArray1.length][2];
//Fill the new array
for(int i=0; i<newArray.length; i++){
newArray[i][0] = currentArray1[i];
newArray[i][1] = currentArray2[i];
}
return newArray;
}
/**
* Append a value on to the end of list of values
* @param currentArray current list of values (double[][])
* @param newValue new value to append to the list (double[])
* @return the new list containing the currentArray list of values and the appened newValue
*/
public double[][] appendcolumn_Matrix(double[][] array1, double[] array2){
//Check that the arrays are the same size to be combined
if(array1.length != array2.length){
System.out.println("The arrays are not the same size so they can not be combined");
return new double[0][0];
}
//Initialize new array to contain the new value
double[][] newArray = new double[array1.length][array1[0].length + 1];
//Fill the new array
for(int i=0; i<newArray.length; i++){
for(int j=0; j<array1[i].length; j++){
newArray[i][j] = array1[i][j];
}
newArray[i][newArray[i].length - 1] = array2[i];
}
return newArray;
}
/**
* Replace the first ("numberOfRows" + 1) of "startingArray" with those of "replacingArray"
* @param startingArray the double[] array to begin duplicating
* @param replacingArray the double[] array to be used replacing the first (rows + 1) of starting Array
* @param numberOfRows the number of rows in startingArray to be replaced by those in replacingArray
* @return a duplicate of startingArray with the first rows ("numberOfRows" + 1) being from replacingArray not startingArray
*/
public double[] replaceRows(double[] startingArray, double[] replacingArray, int numberOfRows){
//Matlab code: m(Z+1:end) = mh(Z+1:end);
double[] newArray = new double[startingArray.length];
for(int i=0; i<startingArray.length; i++){
if(i<(numberOfRows+1)){
//Replace the elements of startingArray with those of replacingArray
newArray[i] = replacingArray[i];
}else{
//Otherwise keep the elements of startingArray
newArray[i] = startingArray[i];
}
}
return newArray;
}
/**
* Keeps only the rows in startingArray that are not listed in rowIndexList
* @param startingArray The array to be minimized (double[][])
* @param rowIndexList a row index of the rows not to be included in the new minimized array (ArrayList<Integer>)
* @return the minimized version of the startingArray (double[][])
*/
public double[][] replaceRows(double[][] startingArray, ArrayList<Integer> rowIndexList){
//Initialize new array
double[][] newArray = new double[startingArray.length][startingArray[0].length];
//Keep only the rows in startingArray that are not contained in the rowIndexList
int indexCtr = 0;
int currentRowIndex = rowIndexList.get(indexCtr);
for(int i=0; i<startingArray.length; i++){//Loop rows of startingArray
if(i != currentRowIndex){//Check if the current row should be kept or not
//Keep current row's infomation
newArray[i][0] = startingArray[i][0];//Populate the newArray with the content of startingArray
newArray[i][1] = startingArray[i][1];
}else{
//Don't keep current row's information
newArray[i][0] = startingArray[i][0];//Populate the newArray with the content of startingArray
newArray[i][1] = 99999;//replace the values below 0.002
indexCtr++;
if(indexCtr >= rowIndexList.size()){
//If the indexCtr == rowIndexList.size then the last row index has been used out
//of the rowIndexList so define an empty early index to keep the loop going
currentRowIndex = 0;
}else{
//Then get the next row
currentRowIndex = rowIndexList.get(indexCtr);
}
}
}
return newArray;
}
/**
* Gets the unique values of the specified column of a double[][] array
* @param dataArray the double array
* @param column the desired column of the double array
* @return a double[] array of the unique valued contained in the specified column of the dataArray
*/
public double[] getUnique(double[][] dataArray, int column){
//Matlab code: P = unique(ktable(:,2));
double[] currentColumn = new double[dataArray.length];
for(int i=0; i<dataArray.length; i++){
currentColumn[i] = dataArray[i][column];
}
Arrays.sort(currentColumn);
//Check and remove duplicate values for a list of unique values
double[] uniqueColumn = getUnique(currentColumn);
return uniqueColumn;
}
/**
* Gets the unique values of the double[] array
* @param currentColumn the double array
* @return a double[] array of the unique valued contained in the dataArray
*/
public double[] getUnique(double[] currentColumn){
Arrays.sort(currentColumn);
//Check and remove duplicate values for a list of unique values
int ctr=0;
for(int i=0; i<currentColumn.length; i++){
if(i == 0){
ctr++;
continue;
}
if(Double.compare(currentColumn[i-1],currentColumn[i]) != 0){
ctr++;
}
}
double[] uniqueColumn = new double[ctr];
ctr=0;
for(int i=0; i<currentColumn.length; i++){
if(i==0){
uniqueColumn[ctr] = currentColumn[i];
ctr++;
continue;
}
if(Double.compare(currentColumn[i-1],currentColumn[i]) != 0){
uniqueColumn[ctr] = currentColumn[i];
ctr++;
}
}
return uniqueColumn;
}
/**
* Creates an ArrayList<Integer> of the rows in dataArray that have the column number "column" equal to "currentValue"
* @param dataArray the double[][] data array
* @param column the column of interest of the data array
* @param currentValue the desired value from the data array
* @return an ArrayList<Integer> of the rows in dataArray[][column] == currentValue
*/
public ArrayList<Integer> findRowsEqual(double[][] dataArray, int column, double currentValue){
ArrayList<Integer> rowIndexList = new ArrayList<Integer>();
for(int i=0; i<dataArray.length; i++){
if(Double.compare(dataArray[i][column],currentValue) == 0){
rowIndexList.add(i);
}
}
return rowIndexList;
}
/**
* Creates an ArrayList<Integer> of the rows in dataArray that have the column number "column" less or greater than to "currentValue"
* @param dataArray the double[][] data array
* @param column the column of interest of the data array
* @param currentValue the desired value from the data array
* @param lessThan if true, then all the rows with data less than the currentValue will be returned, if false all the rows with data greater than the current value will be returned
* @return an ArrayList<Integer> of the rows in dataArray[][column] less than or greater than (depending on the value of "lessThan" provided) currentValue
*/
public ArrayList<Integer> findRowsConditional(double[][] dataArray, int column, double currentValue, boolean lessThan){
ArrayList<Integer> rowIndexList = new ArrayList<Integer>();
for(int i=0; i<dataArray.length; i++){
if(lessThan){//If lessThan == true, then find values less than the currentValue
if(Double.compare(dataArray[i][column],currentValue) < 0){
rowIndexList.add(i);
}
}else{//If lessThan == false, then find values greater than the currentValue
if(Double.compare(dataArray[i][column],currentValue) > 0){
rowIndexList.add(i);
}
}
}
return rowIndexList;
}
/**
* Creates an ArrayList<Integer> of the rows in dataArray that have the column number "column" less/equal to or greater than to "currentValue"
* @param dataArray the double[][] data array
* @param column the column of interest of the data array
* @param currentValue the desired value from the data array
* @param lessThan if true, then all the rows with data less than the currentValue will be returned, if false all the rows with data greater than the current value will be returned
* @return an ArrayList<Integer> of the rows in dataArray[][column] less than/equal to or greater than (depending on the value of "lessThan" provided) currentValue
*/
public ArrayList<Integer> findRowsConditional(double[] dataArray, double currentValue, boolean lessThan){
ArrayList<Integer> rowIndexList = new ArrayList<Integer>();
for(int i=0; i<dataArray.length; i++){
if(lessThan){//If lessThan == true, then find values less than the currentValue
if(Double.compare(dataArray[i],currentValue) <= 0){
rowIndexList.add(i);
}
}else{//If lessThan == false, then find values greater than the currentValue
if(Double.compare(dataArray[i],currentValue) >= 0){
rowIndexList.add(i);
}
}
}
return rowIndexList;
}
/**
* Creates a double[] array of the elements in dataArray[all][column]
* @param dataArray the double[][] data array
* @param column the desired column from data array
* @return a double[] array of the elements in dataArray[all][column]
*/
public double[] getColumn(double[][] dataArray, int column){
//Gets the values of dataArray[all][column] in a double[]
double[] currentElements = new double[dataArray.length];
for(int i=0; i<dataArray.length; i++){
currentElements[i] = dataArray[i][column];
}
return currentElements;
}
/**
* Creates a double[] array of the elements in dataArray[rowIndexList][column]
* @param dataArray the double[][] data array
* @param column the desired column from data array
* @param rowIndexList an ArrayList<Integer> containing the desired row indices from dataArray
* @return a double[] array of the elements in dataArray[rowIndexList][column]
*/
public double[] getColumn(double[][] dataArray, int column, ArrayList<Integer> rowIndexList){
//Gets the values of dataArray[rowIndexList][column] in a double[]
double[] currentElements = new double[rowIndexList.size()];
int indexCtr = 0;
for(int i=0; i<dataArray.length; i++){
if(i == rowIndexList.get(indexCtr)){
currentElements[indexCtr] = dataArray[i][column];
indexCtr++;
if(indexCtr == rowIndexList.size()){
//If the indexCtr == rowIndexList.size then the last row
//index has been used out of the rowIndexList so exit the loop and return the list
break;
}
}
}
return currentElements;
}
/**
* Creates a double[] array of the elements in dataArray[row][all]
* @param dataArray the double[][] data array
* @param row the desired row from data array
* @return a double[] array of the elements in dataArray[row][all]
*/
public double[] getRow(double[][] dataArray, int row){
//Gets the values of dataArray[row][all] in a double[]
double[] currentElements = new double[dataArray[row].length];
for(int i=0; i<dataArray[row].length; i++){
currentElements[i] = dataArray[row][i];
}
return currentElements;
}
/**
* Creates a double[] array of the elements in dataArray[row][columnIndexList]
* @param dataArray the double[][] data array
* @param row the desired row from data array
* @param columnIndexList an ArrayList<Integer> containing the desired row indices from dataArray
* @return a double[] array of the elements in dataArray[rowIndexList][column]
*/
public double[] getRow(double[][] dataArray, int row, ArrayList<Integer> columnIndexList){
//Gets the values of dataArray[rowIndexList][column] in a double[]
double[] currentElements = new double[columnIndexList.size()];
int indexCtr = 0;
for(int i=0; i<dataArray[row].length; i++){
if(i == columnIndexList.get(indexCtr)){
currentElements[indexCtr] = dataArray[row][i];
indexCtr++;
if(indexCtr == columnIndexList.size()){
//If the indexCtr == rowIndexList.size then the last row
//index has been used out of the rowIndexList so exit the loop and return the list
break;
}
}
}
return currentElements;
}
/**
* Creates a double[] array of the elements in dataArray[rowIndexList]
* @param dataArray the double[] data array
* @param rowIndexList an ArrayList<Integer> containing the desired row indices from dataArray
* @return a double[] array of the elements in dataArray[rowIndexList]
*/
public double[] getRows(double[] dataArray, ArrayList<Integer> rowIndexList){
//Gets the values of dataArray[rowIndexList] in a double[]
double[] newArray = new double[rowIndexList.size()];
int indexCtr = 0;
for(int i=0; i<dataArray.length; i++){
if(i == rowIndexList.get(indexCtr)){
newArray[indexCtr] = dataArray[i];
indexCtr++;
if(indexCtr == rowIndexList.size()){
//If the indexCtr == rowIndexList.size then the last row
//index has been used out of the rowIndexList so exit the loop and return the list
break;
}
}
}
return newArray;
}
/**
* Creates a double[][] array of the elements in dataArray[rowIndexList][] (aka it keeps some rows and all columns)
* @param dataArray the double[][] data array
* @param rowIndexList an ArrayList<Integer> containing the desired row indices from dataArray
* @return a double[][] array containing the desired rows from rowIndexList and all the columns
*/
public double[][] getDataArrayRows(double[][] dataArray, ArrayList<Integer> rowIndexList){
//Gets the values of dataArray[rowIndexList][all] in a double[][]
double[][] currentElements = new double[rowIndexList.size()][dataArray[0].length];
if(rowIndexList.size() == 0){
return currentElements;
}
int indexCtr = 0;
for(int i=0; i<dataArray.length; i++){//Loop rows of data array
if(i == rowIndexList.get(indexCtr)){//Check if current row is a desired row from the rowIndexList
for(int j=0; j<dataArray[i].length; j++){//Loop columns of data array
currentElements[indexCtr][j] = dataArray[i][j];
}
indexCtr++;
if(indexCtr == rowIndexList.size()){
//If the indexCtr == rowIndexList.size then the last row
//index has been used out of the rowIndexList so exit the loop and return the list
break;
}
}
}
return currentElements;
}
/**
* Compares array1 and array2 and returns an ArrayList<Integer> of the "j" rows in which array1[i] = array2[j]
* @param array1 double[] first arary to compare
* @param array2 double[] second array to compare
* @return an ArrayList<Integer> containing the rows in which array1[i] = array2[i]
*/
public ArrayList<Integer> intersect(double[] array1, double[] array2){
ArrayList<Integer> rowIndex = new ArrayList<Integer>();
//Find the "j" rows where array1[i] = array2[j]
for(int i=0; i<array1.length; i++){
for(int j=0; j<array2.length; j++){
if(Double.compare(array1[i], array2[j]) == 0){
rowIndex.add(j);
}
}
}
Collections.sort(rowIndex);
return rowIndex;
}
/**
* Creates an double[] array of random numbers of array.length = size
* @param size the number of elements desired in the array
* @return
*/
public double[] RandomArray(int size){
double[] randomArray = new double[size];
for(int i=0; i<size; i++){
randomArray[i] = Math.random();
}
return randomArray;
}
/**
* Merges the public dataset (first input) with the user dataset (second input)
* based on the merge method selected
* @param publicDataset a String[][] array with the first column containing dates (yyyy-mm-dd)
* and the second column values of flow or water quality test to be treated as the "public dataset"
* for merge method purposes
* @param userDataset a String[][] array with the first column containing dates (yyyy-mm-dd)
* and the second column values of flow or water quality test to be treated as the "user dataset"
* for merge method purposes
* @param mergeMethod the method to merge the two datasets, one of the following:
* "user" = overwrite public data with user data
* "public" = overwrite user data with public data
* "maximum" = take the maximum value for that day from both datasets
* "average" = take the average of the two values for that day from both datasets
* "minimum" = take the minimum value for that day from both datasets
* @return
*/
public String[][] mergeData(String[][] publicDataset, String[][] userDataset, String mergeMethod){
//Allocate an array of values to determine if userdata needs to be kept (true) or merged (false)
int ctr = 0;
boolean[] addUserDataset = new boolean[userDataset.length];
for(int i=0; i<userDataset.length; i++){
addUserDataset[i] = true;
for(int j=0; j<publicDataset.length; j++){
if(userDataset[i][0].equalsIgnoreCase(publicDataset[j][0])){
addUserDataset[i] = false;
ctr++;
break;
}
}
}
String[][] mergeDataset = new String[publicDataset.length + userDataset.length - ctr][2];
boolean merge = false;
ctr = 0;
for(int i=0; i<publicDataset.length; i++){
for(int j=0; j<userDataset.length; j++){
//If the dates match, merge the datasets
if(publicDataset[i][0].equalsIgnoreCase(userDataset[j][0])){
if(mergeMethod.equalsIgnoreCase("user")){
mergeDataset[ctr][0] = userDataset[j][0];
mergeDataset[ctr][1] = userDataset[j][1];
ctr++;
}else if(mergeMethod.equalsIgnoreCase("public")){
mergeDataset[ctr][0] = publicDataset[i][0];
mergeDataset[ctr][1] = publicDataset[i][1];
ctr++;
}else if(mergeMethod.equalsIgnoreCase("max")){
double userValue = Double.valueOf(userDataset[j][1]);
double publicValue = Double.valueOf(publicDataset[i][1]);
if(Double.compare(userValue, publicValue) < 0){
mergeDataset[ctr][1] = String.valueOf(publicValue);
}else{
mergeDataset[ctr][1] = String.valueOf(userValue);
}
mergeDataset[ctr][0] = publicDataset[i][0];
ctr++;
}else if(mergeMethod.equalsIgnoreCase("average")){
double userValue = Double.valueOf(userDataset[j][1]);
double publicValue = Double.valueOf(publicDataset[i][1]);
mergeDataset[ctr][0] = publicDataset[i][0];
mergeDataset[ctr][1] = String.valueOf((userValue + publicValue)/2);
ctr++;
}else if(mergeMethod.equalsIgnoreCase("min")){
double userValue = Double.valueOf(userDataset[j][1]);
double publicValue = Double.valueOf(publicDataset[i][1]);
if(Double.compare(userValue, publicValue) < 0){
mergeDataset[ctr][1] = String.valueOf(userValue);
}else{
mergeDataset[ctr][1] = String.valueOf(publicValue);
}
mergeDataset[ctr][0] = publicDataset[i][0];
ctr++;
}
merge = true;
break;
}
}
if(!merge){
mergeDataset[ctr][0] = publicDataset[i][0];
mergeDataset[ctr][1] = publicDataset[i][1];
ctr++;
}
merge = false;
}
//Include the points from the user dataset that aren't duplicate points
for(int i=0; i<addUserDataset.length; i++){
if(addUserDataset[i]){
mergeDataset[ctr][0] = userDataset[i][0];
mergeDataset[ctr][1] = userDataset[i][1];
ctr++;
}
}
Arrays.sort(mergeDataset, new DateComparator());
return mergeDataset;
}
/**
* Merges the public dataset (first input) with the user dataset (second input)
* based on the merge method selected
* @param publicDataset a double[][] array with the first column containing years (yyyy)
* and the second column values of flow or water quality test to be treated as the "public dataset"
* for merge method purposes
* @param userDataset a String[][] array with the first column containing years (yyyy)
* and the second column values of flow or water quality test to be treated as the "user dataset"
* for merge method purposes
* @param mergeMethod the method to merge the two datasets, one of the following:
* "user" = overwrite public data with user data
* "public" = overwrite user data with public data
* "max" = take the maximum value for that day from both datasets
* "average" = take the average of the two values for that day from both datasets
* "min" = take the minimum value for that day from both datasets
* @return
*/
public double[][] mergeData(double[][] publicDataset, double[][] userDataset, String mergeMethod){
//Sort by dates for comparison purposes
Arrays.sort(publicDataset, new sort1_smallToLargeDoubleMath());
Arrays.sort(userDataset, new sort1_smallToLargeDoubleMath());
//Allocate an array of values to determine if userdata needs to be kept (true) or merged (false)
boolean[] useUserDataset = new boolean[userDataset.length];
for(int i=0; i<useUserDataset.length; i++){
useUserDataset[i] = true;
}
int ctr = 0;
for(int i=0; i<publicDataset.length; i++){
for(int j=0; j<userDataset.length; j++){
if(Double.compare(publicDataset[i][0],userDataset[j][0]) == 0){
useUserDataset[j] = false;
ctr++;
break;
}
}
}
double[][] mergeDataset = new double[publicDataset.length + userDataset.length - ctr][2];
boolean merge = false;
ctr = 0;
for(int i=0; i<publicDataset.length; i++){
for(int j=0; j<userDataset.length; j++){
//If the dates match, merge the datasets
if(Double.compare(publicDataset[i][0],userDataset[j][0]) == 0){
if(mergeMethod.equalsIgnoreCase("user")){
mergeDataset[ctr][0] = userDataset[j][0];
mergeDataset[ctr][1] = userDataset[j][1];
ctr++;
}else if(mergeMethod.equalsIgnoreCase("public")){
mergeDataset[ctr][0] = publicDataset[i][0];
mergeDataset[ctr][1] = publicDataset[i][1];
ctr++;
}else if(mergeMethod.equalsIgnoreCase("max")){
if(Double.compare(userDataset[i][1], publicDataset[i][1]) < 0){
mergeDataset[ctr][1] = publicDataset[i][1];
}else{
mergeDataset[ctr][1] = userDataset[j][1];
}
mergeDataset[ctr][0] = publicDataset[i][0];
ctr++;
}else if(mergeMethod.equalsIgnoreCase("average")){
mergeDataset[ctr][0] = publicDataset[i][0];
mergeDataset[ctr][1] = (userDataset[j][1] + publicDataset[j][1])/2;
ctr++;
}else if(mergeMethod.equalsIgnoreCase("min")){
if(Double.compare(userDataset[j][1], publicDataset[j][1]) < 0){
mergeDataset[ctr][1] = userDataset[j][1];
}else{
mergeDataset[ctr][1] = publicDataset[i][1];
}
mergeDataset[ctr][0] = publicDataset[i][0];
ctr++;
}
merge = true;
break;
}
}
if(!merge){
mergeDataset[ctr][0] = publicDataset[i][0];
mergeDataset[ctr][1] = publicDataset[i][1];
ctr++;
}
merge = false;
}
//Include the points from the user dataset that aren't duplicate points
for(int i=0; i<useUserDataset.length; i++){
if(useUserDataset[i]){
mergeDataset[ctr][0] = userDataset[i][0];
mergeDataset[ctr][1] = userDataset[i][1];
ctr++;
}
}
Arrays.sort(mergeDataset, new sort1_smallToLargeDoubleMath());
return mergeDataset;
}
/**
* Sorts the String[][] by the first column (dates) and then removes duplicate
* date values and returns an array of equal or lesser size than the original
* @param currentStringArray the String[][] to be sorted and removed, column1 = dates (yyyy-mm-dd), column2 = values, other columns will also be kept
* @return a String[][] of equal or lesser size than the original containing the original data sorted by the first column
*/
public String[][] removeDuplicateDates(String[][] currentStringArray){
//Sort the Data by date to remove duplicate date entries
Arrays.sort(currentStringArray, new DateComparator());
//Check and remove duplicate days as to correct statistical implication of duplicate flow values on a day
int ctr=0;
for(int i=0; i<(currentStringArray.length); i++){
if(i == 0){
ctr++;
continue;
}
if(!currentStringArray[i-1][0].equals(currentStringArray[i][0])){
ctr++;
}
}
String[][] sortedData = new String[ctr][2];
ctr=0;
for(int i=0; i<(currentStringArray.length); i++){
if(i==0){
sortedData[ctr][0] = currentStringArray[i][0];
sortedData[ctr][1] = currentStringArray[i][1];
ctr++;
continue;
}
if(!currentStringArray[i-1][0].equals(currentStringArray[i][0])){
sortedData[ctr][0] = currentStringArray[i][0];
sortedData[ctr][1] = currentStringArray[i][1];
ctr++;
}
}
return sortedData;
}/**
* Performs a Weibull plotting position ranking of the provided flow values in sortedData's second column
* @param sortedData a string[][] containing: column1 = dates (unused) column2 = flowValues to be ranked
* @return a double[][] containing: column1 = x, column2 = y coordinates of the ranking based on a duration
* curve method and Weibull plotting position
*/
public double[][] weibullPlottingPosition(String[][] sortedData){
//Sort the Data by flow to prepare for ranking
Arrays.sort(sortedData, new FlowComparator());
// Index new variables of rank, and non-exceedence probability
double total_samples = sortedData.length;
double[][] xyRanks = new double[sortedData.length + 1][2];
int g = 0, h = 0;
//Flow duration curve using "Weibul" distribution
for(int i=1; i<(sortedData.length +1); i++){
if((i != sortedData.length) && (sortedData[i-1][1].equals(sortedData[i][1]))){
//Find how many elements equal
h = i;
while((h != sortedData.length) && (sortedData[h-1][1].equals(sortedData[h][1]))){
h++;
}
//Give all the equal elements the rank of the largest equal element (max rank for tied rank scenarios)
for(g=i; g<=h; g++){
xyRanks[g-1][0] = (h / (total_samples))*100;
xyRanks[g-1][1] = Double.parseDouble(sortedData[i-1][1]);
if (g==h){
//If on the last repeated element, set the initial counter "i" that last
//rank value as to not repeat comparing already sorted and ranked values
i=h;
}
}
}else{
xyRanks[i-1][0] = (i / (total_samples))*100;
xyRanks[i-1][1] = Double.parseDouble(sortedData[i-1][1]);
}
}
xyRanks[sortedData.length][0] = 100;
return xyRanks;
}
/**
* Reduces the provided water quality data to only within the user specified season
* @param allWQdata all the WQdata for the user specified date range to be minimized by season (column1 = date (yyyy-mm-dd), column2 = value)
* @param seasonBegin the user specified begin month of the season (ex "February")
* @param seasonEnd the user specified end month of the season (ex "April")
* @return an array containing the dates and values of water quality tests that occur within the specified season
*/
public String[][] getSeasonalWQData(String[][] allWQdata, String seasonBegin, String seasonEnd){
//Convert season month into season integer
int seasonBegin_number = convertMonth(seasonBegin);
int seasonEnd_number = convertMonth(seasonEnd);
//Remove water quality tests that are outside user specified seasonal range
int ctr = 0;
if(seasonBegin_number < seasonEnd_number){
for(int i=0; i<allWQdata.length; i++){
int tempdate = Integer.parseInt(allWQdata[i][0].substring(5,7));
if((tempdate > seasonBegin_number) && (tempdate < seasonEnd_number)){
ctr++;
}
}
}else{
for(int i=0; i<allWQdata.length; i++){
int tempdate = Integer.parseInt(allWQdata[i][0].substring(5,7));
if((tempdate > seasonBegin_number) || (tempdate < seasonEnd_number)){
ctr++;
}
}
}
String[][] SeasonalWQdata = new String[ctr][2];
ctr = 0;
if(seasonBegin_number < seasonEnd_number){
for(int i=0; i<allWQdata.length; i++){
int tempdate = Integer.parseInt(allWQdata[i][0].substring(5,7));
if((tempdate > seasonBegin_number) && (tempdate < seasonEnd_number)){
SeasonalWQdata[ctr][0] = allWQdata[i][0];
SeasonalWQdata[ctr][1] = allWQdata[i][1];
ctr++;
}
}
}else{
for(int i=0; i<allWQdata.length; i++){
int tempdate = Integer.parseInt(allWQdata[i][0].substring(5,7));
if((tempdate > seasonBegin_number) || (tempdate < seasonEnd_number)){
SeasonalWQdata[ctr][0] = allWQdata[i][0];
SeasonalWQdata[ctr][1] = allWQdata[i][1];
ctr++;
}
}
}
return SeasonalWQdata;
}
/**
* Sub-function to convert month text to month integer
* @param month_String the text version of the month (Ex. January).
* @return An Integer (Ex. 1).
*/
public int convertMonth (String month_String){
int month_number = 0;
if (month_String.equalsIgnoreCase("January")){
month_number = 1;
}else if (month_String.equalsIgnoreCase("February")){
month_number = 2;
}else if (month_String.equalsIgnoreCase("March")){
month_number = 3;
}else if (month_String.equalsIgnoreCase("April")){
month_number = 4;
}else if (month_String.equalsIgnoreCase("May")){
month_number = 5;
}else if (month_String.equalsIgnoreCase("June")){
month_number = 6;
}else if (month_String.equalsIgnoreCase("July")){
month_number = 7;
}else if (month_String.equalsIgnoreCase("August")){
month_number = 8;
}else if (month_String.equalsIgnoreCase("September")){
month_number = 9;
}else if (month_String.equalsIgnoreCase("October")){
month_number = 10;
}else if (month_String.equalsIgnoreCase("November")){
month_number = 11;
}else if (month_String.equalsIgnoreCase("December")){
month_number = 12;
}
return month_number;
}
/**
* Reduces the provided data to only within the specified year
* @param allData all the data for the to be minimized by year (column1 = date (yyyy-mm-dd), column2 = value)
* @param year the specified year (ex "1981")
* @return an array (same format as allData) containing the dates and values that occur within the specified year
*/
public String[][] getYearsData(String[][] allData, String year){
//Remove data that are outside specified year
int ctr = 0;
for(int i=0; i<allData.length; i++){
String tempYear = allData[i][0].substring(0,4);
if(year.compareToIgnoreCase(tempYear) == 0){
ctr++;
}
}
String[][] SeasonalData = new String[ctr][2];
ctr = 0;
for(int i=0; i<allData.length; i++){
String tempYear = allData[i][0].substring(0,4);
if(year.compareToIgnoreCase(tempYear) == 0){
SeasonalData[ctr][0] = allData[i][0];
SeasonalData[ctr][1] = allData[i][1];
ctr++;
}
}
return SeasonalData;
}
/**
* Calculates a unique list of years contained in the flow data from STORET then proceeds to
* calculate the annual maximum flow value for each of the unique years and returns the list
* of years and flow values as a double array
* @param sortableData string[][] array containing the output of STORET_unzip_FDC.main which contains
* dates (YYYY-mm-dd) in the first column and flow values (cfs) in the second column
* @return returns a double[][] array the same size as the provided string array containing
* the first column of years and the second column of flow values
*/
public double[][] convertSTORETpeakData(String[][] sortableData){
Arrays.sort(sortableData, new DateComparator());
//Find a list of unique years for which an annual maximum flow will be calculated later
int ctr=0;
for(int i=0; i<(sortableData.length); i++){
if(i == 0){
ctr++;
continue;
}
String year1 = sortableData[i-1][0].substring(0,4);
String year2 = sortableData[i][0].substring(0,4);
if (!year1.equals(year2)){
ctr++;
}
}
String[] uniqueYears = new String[ctr];
ctr=0;
for(int i=0; i<(sortableData.length); i++){
if(i==0){
uniqueYears[ctr] = sortableData[i][0].substring(0,4);
ctr++;
continue;
}
String year1 = sortableData[i-1][0].substring(0,4);
String year2 = sortableData[i][0].substring(0,4);
if (!year1.equals(year2)){
uniqueYears[ctr] = sortableData[i][0].substring(0,4);
ctr++;
}
}
//Loop through and find the annual maximum flow value for each unique year
double[][] peakFlowData = new double[uniqueYears.length][2];
for(int i=0; i<uniqueYears.length; i++){
peakFlowData[i][0] = Double.parseDouble(uniqueYears[i]);
peakFlowData[i][1] = 0;
for(int j=0; j<sortableData.length; j++){
String currentYear = sortableData[j][0].substring(0,4);
if(uniqueYears[i].equals(currentYear)){
double flowValue = Double.parseDouble(sortableData[j][1]);
if(Double.compare(flowValue, peakFlowData[i][1]) > 0){//If current value larger than "max" change the max to the current value
peakFlowData[i][1] = flowValue;
}
}
}
}
return peakFlowData;
}
/**
* Computes the daily/monthly/yearly max/min/average/total of the daily dataset provided
* @param dailyData a string array with column1 = dates (yyyy-mm-dd format), column2 = value
* @param timeStep the desired timestep "daily", "monthly", or "yearly"
* @param method the desired method "max", "min", "average", or "total"
* @return a new string[][] with column 1 = dates (yyyy-mm-dd format for "daily" timeStep,
* yyyy-mm format for "monthly" timeStep, and yyyy format for "yearly" timeStep) and column2 = values
* @param loadestTF a flag for if analyzing loadest result data which uses a yyyymmdd date format from the
* loadest.exe result file rather than typical date format of yyyy-mm-dd
*/
public String[][] computeFlowMethod(String[][] dailyData, String timeStep, String method, boolean loadestTF){
String[][] newData = new String[0][2];
if(dailyData.length > 0){
if(timeStep.equalsIgnoreCase("daily")){
return dailyData;
}else if(timeStep.equalsIgnoreCase("monthly")){
//Compute the method on the unique monthYears
if(loadestTF){
newData = computeMethod(dailyData, method, 6);
}else{
newData = computeMethod(dailyData, method, 7);
}
}else if(timeStep.equalsIgnoreCase("yearly")){
//Compute the method on the unique monthYears
newData = computeMethod(dailyData, method, 4);
}
}
return newData;
}
/**
* Computes the method on the dailyData provided substring-ing the dates from 0-dateLimit to
* get a unique set of time periods on which to perform the method
* @param dailyData a string array with column1 = dates (yyyy-mm-dd format), column2 = value
* @param method the desired method "max", "min", "average", or "total"
* @param dateLimit an integer for the limit of the date substring (typically 4 or 7 for the
* format yyyy-mm-dd resulting in yyyy and yyyy-mm respectively)
* @return a new string[][] with column 1 = dates (yyyy-mm-dd format for "daily" timeStep,
* yyyy-mm format for "monthly" timeStep, and yyyy format for "yearly" timeStep) and column2 = values
*/
private String[][] computeMethod(String[][] dailyData, String method, int dateLimit){
DoubleMath doubleMath = new DoubleMath();
//Find the unique set of months/years for which the method is desired for
ArrayList<String> uniqueTimeStep = new ArrayList<String>();
String previousMonthYear = dailyData[0][0].substring(0,dateLimit);
uniqueTimeStep.add(previousMonthYear);
for(int i=1; i<dailyData.length; i++){
//Check if current monthYear is the same or different from the previous
String currentMonthYear = dailyData[i][0].substring(0,dateLimit);
if(!previousMonthYear.equalsIgnoreCase(currentMonthYear)){
uniqueTimeStep.add(currentMonthYear);
previousMonthYear = currentMonthYear;
}
}
//Loop through daily data, pull out each uniqueTimeStep's dataset and perform the method on that dataset
String[][] newData = new String[uniqueTimeStep.size()][2];
ArrayList<Double> currentMonthData = new ArrayList<Double>();
int ctr=0;
for(int i=0; i<dailyData.length; i++){
if(uniqueTimeStep.get(ctr).equals(dailyData[i][0].substring(0, dateLimit))){
//If current data = current month, add it to the dataset
currentMonthData.add(Double.parseDouble(dailyData[i][1]));
}else{
//If current data != current month, calculate method on the current month's dataset, save the result and reset the dataset
newData[ctr][0] = uniqueTimeStep.get(ctr);
if(method.equalsIgnoreCase("max")){
newData[ctr][1] = String.valueOf(doubleMath.max(currentMonthData));
}else if(method.equalsIgnoreCase("average")){
newData[ctr][1] = String.valueOf(doubleMath.Average(currentMonthData));
}else if(method.equalsIgnoreCase("min")){
newData[ctr][1] = String.valueOf(doubleMath.min(currentMonthData));
}else if(method.equalsIgnoreCase("total")){
newData[ctr][1] = String.valueOf(doubleMath.sum(currentMonthData));
}
//Reset the dataset and add the current data to it as the new month's data
currentMonthData.clear();
currentMonthData.add(Double.parseDouble(dailyData[i][1]));
ctr++;
}
//If on the last point calculate the method on the current dataset
if(i == dailyData.length-1){
newData[ctr][0] = uniqueTimeStep.get(ctr);
if(method.equalsIgnoreCase("max")){
newData[ctr][1] = String.valueOf(doubleMath.max(currentMonthData));
}else if(method.equalsIgnoreCase("average")){
newData[ctr][1] = String.valueOf(doubleMath.Average(currentMonthData));
}else if(method.equalsIgnoreCase("min")){
newData[ctr][1] = String.valueOf(doubleMath.min(currentMonthData));
}else if(method.equalsIgnoreCase("total")){
newData[ctr][1] = String.valueOf(doubleMath.sum(currentMonthData));
}
}
}
return newData;
}
/**
* Writes out the formatted output file as expected by eRAMS to be used for a JHighchart timeseries
* @param mainFolder
* @param data
* @param timeStep
* @param outputFileName
* @param loadestTF
* @throws IOException
*/
public void writeTimeSeries(String mainFolder, String[][] data, String timeStep, String outputFileName, boolean loadestTF) throws IOException {
//Convert Date formate for output
DoubleArray doubleArray = new DoubleArray();
String[] date = new String[0];
if(loadestTF){
date = doubleArray.changeDate2JHighChart_loadest(data, timeStep);
}else{
date = doubleArray.changeDate2JHighChart(data, timeStep);
}
// open the file writer and set path
String path = mainFolder + File.separator + outputFileName + "_graph.out";
FileWriter writer = new FileWriter(path, false);
PrintWriter print_line = new PrintWriter(writer);
// Print the needed values
for (int i = 0; i < date.length; i++){
print_line.printf("%s" + "%n", date[i] + "\t" + data[i][1]);
}
// Close the file writer
print_line.close();
writer.close();
System.out.println("Text File located at:\t" + path);
}
/**
* This function will prepare the data that is needed for the use on eRAMS
* with JHighCharts (date format: yyyy-MM-dd)
* @param data
* @param timeStep
* @return
*/
private String[] changeDate2JHighChart(String[][] data, String timeStep){
String[] date = new String[data.length];
if (timeStep.equalsIgnoreCase("daily")){
for (int i = 0; i < data.length; i++){
double d = Double.parseDouble(data[i][0].substring(8));
double m = Double.parseDouble(data[i][0].substring(5,7));
double y = Double.parseDouble(data[i][0].substring(0,4));
int year = (int) y;
int month = (int) m;
int day = (int) d;
Calendar newDate = Calendar.getInstance();
newDate.set(year, month - 1, day, 12, 0, 0);
date[i] = String.valueOf(newDate.getTimeInMillis()/1000);
}
}else if (timeStep.equalsIgnoreCase("monthly")){
for (int i = 0; i < data.length; i++){
double m = Double.parseDouble(data[i][0].substring(5,7));
double y = Double.parseDouble(data[i][0].substring(0,4));
int year = (int) y;
int month = (int) m;
Calendar newDate = Calendar.getInstance();
newDate.set(year, month - 1, 1, 12, 0, 0);
date[i] = String.valueOf(newDate.getTimeInMillis()/1000);
}
}else{
for (int i = 0; i < data.length; i++){
double y = Double.parseDouble(data[i][0].substring(0,4));
int year = (int) y;
Calendar newDate = Calendar.getInstance();
newDate.set(year, 0, 1, 12, 0, 0);
date[i] = String.valueOf(newDate.getTimeInMillis()/1000);
}
}
return date;
}
/**
* This function will prepare the data that is needed for the use on eRAMS
* with JHighCharts using LOADEST output data (date format: yyyyMMdd)
* @param data
* @param timeStep
* @return
*/
private String[] changeDate2JHighChart_loadest(String[][] data, String timeStep){
String[] date = new String[data.length];
if (timeStep.equalsIgnoreCase("daily")){
for (int i = 0; i < data.length; i++){
double y = Double.parseDouble(data[i][0].substring(0,4));
double m = Double.parseDouble(data[i][0].substring(4,6));
double d = Double.parseDouble(data[i][0].substring(6));
int year = (int) y;
int month = (int) m;
int day = (int) d;
Calendar newDate = Calendar.getInstance();
newDate.set(year, month - 1, day, 12, 0, 0);
date[i] = String.valueOf(newDate.getTimeInMillis()/1000);
}
}else if (timeStep.equalsIgnoreCase("monthly")){
for (int i = 0; i < data.length; i++){
double y = Double.parseDouble(data[i][0].substring(0,4));
double m = Double.parseDouble(data[i][0].substring(4,6));
int year = (int) y;
int month = (int) m;
Calendar newDate = Calendar.getInstance();
newDate.set(year, month - 1, 1, 12, 0, 0);
date[i] = String.valueOf(newDate.getTimeInMillis()/1000);
}
}else{
for (int i = 0; i < data.length; i++){
double y = Double.parseDouble(data[i][0].substring(0,4));
int year = (int) y;
Calendar newDate = Calendar.getInstance();
newDate.set(year, 0, 1, 12, 0, 0);
date[i] = String.valueOf(newDate.getTimeInMillis()/1000);
}
}
return date;
}
/**
* Writes out the formatted output file as expected by eRAMS to be used for a JHighchart boxplot
* @param mainFolder the location of the output file
* @param lowerQuartile
* @param upperQuartile
* @param outliers
* @param median
* @param minOfData
* @param outputFileName
* @throws IOException
*/
public void writeBoxplot(String mainFolder,
double lowerQuartile,
double upperQuartile,
ArrayList<Double> outliers,
double median,
double minOfData,
String outputFileName) throws IOException {
// open the file writer and set path
String path = mainFolder + File.separator + outputFileName + "_boxplot.out";
FileWriter writer = new FileWriter(path, false);
PrintWriter print_line = new PrintWriter(writer);
double IQR = upperQuartile - lowerQuartile;
double low = lowerQuartile - 1.5 * IQR;
double high = upperQuartile + 1.5 * IQR;
// Print the needed values
for (int i = 0; i < outliers.size(); i ++){
print_line.printf("%s" + "%n", outliers.get(i));
}
if (low < minOfData){
low = minOfData;
}
print_line.printf("%s" + "%n", low + "\t" + lowerQuartile + "\t" + median + "\t" + upperQuartile + "\t" + high);
// Close the file writer
print_line.close();
writer.close();
System.out.println("Text File located at:\t" + path);
}
/**
* Checks if the provided dates are subsequent dates, aka nextDate = date + 1day.
* This check includes December 31st to January 1st catchs, 4-year leap-year catches,
* 100-year non-leap-year catches and 400-year leap-year catches
* @param date the first date to be compared (expected format = yyyy-mm-dd)
* @param nextDate the second date to be compared (expected format = (yyyy-mm-dd)
* @return returns true if nextDate = date + 1day, false otherwise
*/
private boolean checkSubsequentDates(String date, String nextDate){
double year = Double.parseDouble(date.substring(0,4));
double month = Double.parseDouble(date.substring(5,7));
double day = Double.parseDouble(date.substring(8));
double year2 = Double.parseDouble(nextDate.substring(0,4));
double month2 = Double.parseDouble(nextDate.substring(5,7));
double day2 = Double.parseDouble(nextDate.substring(8));
boolean subsequentDates = false;
//Check if nextDate = date + 1 day
if(Double.compare(year,year2) == 0){//Check if same year
if(Double.compare(month, month2) == 0){//Check if same month
if(Double.compare(day + 1, day2) == 0){//Check if subsequent day
subsequentDates = true;
}
}else{
if((Double.compare(month + 1, month2) == 0) && //Check if subsequent month
(Double.compare(day2,1) == 0)){//Check if first day
//Check months with 31 days
if((Double.compare(day, 31) == 0) &&
((Double.compare(month, 1) == 0) ||
(Double.compare(month, 3) == 0) ||
(Double.compare(month, 5) == 0) ||
(Double.compare(month, 7) == 0) ||
(Double.compare(month, 8) == 0) ||
(Double.compare(month, 10) == 0))){
subsequentDates = true;
//Check months with 30 days
}else if((Double.compare(day, 30) == 0) &&
((Double.compare(month, 4) == 0) ||
(Double.compare(month, 6) == 0) ||
(Double.compare(month, 9) == 0) ||
(Double.compare(month, 11) == 0))){
subsequentDates = true;
//Check February for leap years (including the 100-year-not-leap-year and 400-year-leap-year)
}else if((Double.compare(month, 2) == 0)){
boolean leapYear = false;
double yearUp4 = Math.ceil(year/4);
double yearDown4 = Math.floor(year/4);
double yearUp100 = Math.ceil(year/100);
double yearDown100 = Math.floor(year/100);
double yearUp400 = Math.ceil(year/400);
double yearDown400 = Math.floor(year/400);
if(yearUp400 == yearDown400){
leapYear = true;
}else if(yearUp100 == yearDown100){
leapYear = false;
}else if(yearUp4 == yearDown4){
leapYear = true;
}
//Check non-leap years (28 day February)
if(!leapYear && (Double.compare(day, 28) == 0)){//Check if is subsequent day
subsequentDates = true;
//Check leap years (29 day February)
}else if(leapYear && (Double.compare(day, 29) == 0)){//Check if is subsequent day
subsequentDates = true;
}
}
}
}
}else{
//Check if subsequent years, months, and days from December 31st to January 1st
if((Double.compare(year + 1, year2) == 0) && //Check if subsequent years
(Double.compare(month, 12) == 0) && //and the first date is December
(Double.compare(month2, 1) == 0) && //and the second date is January
(Double.compare(day, 31) == 0) && //and the first date is the 31st
(Double.compare(day2, 1) == 0)){ //and the second date is the 1st
subsequentDates = true;
}
}
return subsequentDates;
}
}