package edu.purdue.cs307.team16;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.json.JSONObject;

import ucar.ma2.Array;
import ucar.ma2.Index;
import ucar.nc2.NetcdfFile;

public class ResearcherMapReduceAnalysis extends MapReduceAnalysis<double[], double[]> {
	
	public ResearcherMapReduceAnalysis() {
		super();
	}
	
	protected double[] mapAnalyze(NetcdfFile nexradNetCDF) {
		Array dataArray;
		Index dataIndex;
		double[] retArray = null;
		
		// SHAPE is 3, 360, 324

		try {
			dataArray = nexradNetCDF.findVariable("Reflectivity").read();
	    	dataIndex = dataArray.getIndex();
	    	// get shape
	    	int[] shape = dataArray.getShape();	
	    	
	    	retArray = new double[shape[2]];
	    	for(int i = 0; i < shape[2]; i++) {
	        	
	        	retArray[i] = dataArray.getDouble(dataIndex.set(0,0,i));

	    	}
	    	
			System.out.println("Array from map: " + Arrays.toString(retArray));
	    	
	    	
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(ExceptionUtils.getStackTrace(e));
			System.exit(1);
		}
		
		return retArray;
	
	}

	double[] runningSumsArray = null;
	int numberOfDataPoints = 0;
	
	protected double[] reduceAnalyze(double[] input) {
		double[] averageArray = new double[input.length];
		
		if(runningSumsArray == null) runningSumsArray = new double[input.length];
	
		numberOfDataPoints++;

		for(int i = 0; i < input.length; i++) {
			runningSumsArray[i] += input[i];
			averageArray[i] = runningSumsArray[i]/numberOfDataPoints;
		}

		return averageArray;
	}


	protected void outputFileWriter(double[] reduceOutput, String outputDir) {
		String outputFile = outputDir + "/jsonOutputFile";
		JSONObject jsonObj = new JSONObject();
		FileWriter fileWriter;
		
		jsonObj.put("Average Array", Arrays.toString(reduceOutput));
		
		try {
			fileWriter = new FileWriter(outputFile);
			fileWriter.write(jsonObj.toString() + "\n");
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
	}

}

/*
 // example of scalar average
public class ResearcherMapReduceAnalysis extends MapReduceAnalysis<Double, Double> {
	
	public ResearcherMapReduceAnalysis() {
		super();
	}
	
	protected Double mapAnalyze(NetcdfFile nexradNetCDF) {
		
		Array dataArray;
		Index dataIndex;
		byte dataByte = -1;
		
		// SHAPE is 3, 360, 324

		try {
			dataArray = nexradNetCDF.findVariable("Reflectivity").read();
	

			dataIndex = dataArray.getIndex();
			dataIndex.set(0,0,0);
			dataByte = dataArray.getByte(dataIndex);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(ExceptionUtils.getStackTrace(e));
			System.exit(1);
		}
		
		return (double) dataByte;
	
	}
	
	public Double runningSum = 0.0;
	public int numberOfDataPoints = 0;
	Double average = 0.0;

	protected Double reduceAnalyze(Double input) {
		
		runningSum += input;
		numberOfDataPoints++;
		average = runningSum/numberOfDataPoints;
		return average;
	}
	

	protected void outputFileWriter(Double reduceOutput, String outputDir) {
		String outputFile = outputDir + "/jsonOutputFile";
		JSONObject jsonObj = new JSONObject();
		FileWriter fileWriter;
		
		jsonObj.put("Average", reduceOutput.toString());
		
		try {
			fileWriter = new FileWriter(outputFile);
			fileWriter.write(jsonObj.toString() + "\n");
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
	}

}
*/

@@ -268,16 +268,42 @@
 cdfFile;
+%0Aimport ucar.nc2.Variable;
 %0A%0Apublic
@@ -604,33 +604,39 @@
 %0A%09%09try %7B%0A%09%09%09
-dataArray
+Variable netcdf
  = nexradNet
@@ -659,32 +659,90 @@
 (%22Reflectivity%22)
+;%0A%09%09%09if(netcdf == null) return null;%0A%09%09%09dataArray = netcdf
 .read();%0A%09    %09d
@@ -1404,55 +1404,86 @@
 %7B%0A%09%09
-double%5B%5D averageArray = new double%5Binput.length
+int length;%0A%09%09if(input == null && numberOfDataPoints == 0) return new double%5B1
 %5D;%0A%09
@@ -1561,19 +1561,101 @@
 ngth%5D;%0A%09
-%0A%09%09
+%09double%5B%5D averageArray = new double%5BrunningSumsArray.length%5D;%0A%09%09%0A%09%09if(input != null) 
 numberOf
@@ -1672,54 +1672,204 @@
 ++;%0A
+%09%09
 %0A%09%09
-for(int i = 0; i %3C input.length; i++) %7B%0A%09%09%09
+if(input == null %7C%7C input.length %3E runningSumsArray.length) length = runningSumsArray.length;%0A%09%09else length = input.length;%0A%09%09%0A%09%09%0A%09%09%0A%0A%09%09for(int i = 0; i %3C length; i++) %7B%0A%09%09%09if(input != null) 
 runn

Make the analysis more robust to deal with missing data