/*
*   CLASS:      ThreeDimensionalSmooth
*
*   USAGE:      Class for smoothing a surface
*               Smoothing methods: moving average window or Savitzky-Golay filter
*
*   WRITTEN BY: Dr Michael Thomas Flanagan
*
*   DATE:       5-16 March 2012
*   AMENDED:     
*
*   DOCUMENTATION:
*   See Michael Thomas Flanagan's Java library on-line web pages:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*   http://www.ee.ucl.ac.uk/~mflanaga/java/ThreeDimensionalSmooth.html
*
*   Copyright (c) 2012  Michael Thomas Flanagan
*
*   PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/

package flanagan.analysis;

import flanagan.math.ArrayMaths;
import flanagan.math.Conv;
import flanagan.math.Fmath;
import flanagan.interpolation.TriCubicSpline;
import flanagan.math.Matrix;
import flanagan.plot.*;

import java.math.BigDecimal;
import java.math.BigInteger;

public class ThreeDimensionalSmooth{
    
    private double[] xData = null;                                  // original x data, v = f(x,y,z)
    private double[] yData = null;                                  // original y data, v = f(x,y,z)  
    private double[] zData = null;                                  // original z data, v = f(x,y,z)
    private double[][][] vData = null;                              // original v data, v = f(x,y,z)
    private BigDecimal[] xBDdata = null;                            // original arbitrary precision x data, v = f(x,y,z)
    private BigDecimal[] yBDdata = null;                            // original arbitrary precision y data, v = f(x,y,z)
    private BigDecimal[] zBDdata = null;                            // original arbitrary precision y data, v = f(x,y,z)
    private BigDecimal[][][] vBDdata = null;                        // original arbitrary precision v data, v = f(x,y,z)
    private int nPointsX = 0;                                       // number of  x data points, v = f(x,y,z)
    private int nPointsY = 0;                                       // number of  y data points, v = f(x,y,z)
    private int nPointsZ = 0;                                       // number of  z data points, v = f(x,y,z)
    private int nPoints = 0;                                        // total number of data points
    private boolean arbprec = false;                                // = true if arbitrary precision data entered
    
    private double[][][]vDataMovAv = null;                          // moving average smoothed v data, double precision, v = f(x,y,z)
    private BigDecimal[][][] vDataMovAvBD = null;                   // moving average smoothed v data, arbitrary precision, v = f(x,y,z)
    private double[][][] vDataSavGol = null;                        // Savitzky-Golay smoothed v data, v = f(x,y,z)             
    private double[][][] derivSavGol = null;                        // Savitzky-Golay smoothed derivative (d^mnv/dx^mdy^n), v = f(x,y,z)  
    private int[][] sgCoeffIndices = null;                          // indices of the fitting polynomial and Savitzky-Golay filter coefficients
    private int nSGcoeff = 0;                                       // number of indices
    private int lastMethod = 0;                                     // = 0: none called
                                                                    // = 1: moving window average
                                                                    // = 2: Savitzky-Golay filter                                                           
    private int nMethods = 2;                                       // number of methods
                                                                                                                               
    private int maWindowWidthx = 0;                                 // x width of the smoothing cube in the running average method, v = f(x,y,z)
    private int maWindowWidthy = 0;                                 // y width of the smoothing cube in the running average method, v = f(x,y,z)
    private int maWindowWidthz = 0;                                 // y width of the smoothing cube in the running average method, z = f(x,y,z)

    private int sgWindowWidthx = 0;                                 // x width of smoothing window in the Savitzky-Golay method, v = f(x,y,z)
    private int sgWindowWidthy = 0;                                 // y width of smoothing window in the Savitzky-Golay method, v = f(x,y,z)
    private int sgWindowWidthz = 0;                                 // z width of smoothing window in the Savitzky-Golay method, v = f(x,y,z)

    private int sgPolyDeg = 4;                                      // Savitzky-Golay smoothing polynomial degree
    private double[][] sgArrayC = null;                             // Savitzky-Golay C matrix
   
    private boolean calcSavGol = false;                             // = true when Savitzky-Golay smoothing performed
    private boolean calcMovAv = false;                              // = true when moving average smoothing performed
    
    private boolean nthSet = false;                                 // = true when any Savitzky-Golay derivative smoothing performed
    
    private double extentMovAv = -1.0;                              // Extent of the moving average window smoothing
    private double extentSavGol = -1.0;                             // Extent of the Savitzky-Golay smoothing
        
    private TriCubicSpline tcsSavGol = null;                        // TriCubic spline interpolation of the Savitzky-Golay smoothed data
    private TriCubicSpline tcsMovAv = null;                         // TriCubic spline interpolation of the moving averagey smoothed data
    
    private int trunc = 4;                                          // truncation value for plot title values
    
    
    // CONSTRUCTORS
    
    // Constructor - data as  double, v=f(x,y,z)
    public ThreeDimensionalSmooth(double[] x, double[] y, double[] z, double[][][] v){ 
        this.xData = x;
        this.yData = y;
        this.zData = z;
        this.vData = v;
        this.polyIndices();
        this.check();
        this.ascend();
    }
    
    // Constructor - data as  double - no x, y or z data, v=f(x,y,z)
    public ThreeDimensionalSmooth(double[][][] v){ 
        int n = v.length;
        this.vData = v;
        this.yData = new double[n];
        for(int i=0; i=this.nPointsZ)test1 = false;
            }
        }
        if(!test2){
            ArrayMaths am = new ArrayMaths(this.zData);
            am =  am.sort();
            int[] indices = am.originalIndices();
            double[] holdz = new double[this.nPointsZ];
            double[][][] holdv = new double[this.nPointsZ][this.nPointsY][this.nPointsX];
            BigDecimal[] holdzBD = new BigDecimal[this.nPointsZ];
            BigDecimal[][][] holdvBD = null;
            if(this.arbprec)holdvBD = new BigDecimal[this.nPointsZ][this.nPointsY][this.nPointsX];
            for(int i=0; i=this.nPointsY)test1 = false;
            }
        }
        if(!test2){
            ArrayMaths am = new ArrayMaths(this.yData);
            am =  am.sort();
            int[] indices = am.originalIndices();
            double[] holdy = new double[this.nPointsY];
            double[][][] holdv = new double[this.nPointsZ][this.nPointsY][this.nPointsX];
            BigDecimal[] holdyBD = new BigDecimal[this.nPointsY];
            BigDecimal[][][] holdvBD = null;
            if(this.arbprec)holdvBD = new BigDecimal[this.nPointsZ][this.nPointsY][this.nPointsX];
            for(int i=0; i=this.nPointsX)test1 = false;
            }
        }
        if(!test2){
            ArrayMaths am = new ArrayMaths(this.xData);
            am =  am.sort();
            int[] indices = am.originalIndices();
            double[] holdx = new double[this.nPointsX];
            double[][][] holdv = new double[this.nPointsZ][this.nPointsY][this.nPointsX];
            BigDecimal[] holdxBD = new BigDecimal[this.nPointsX];
            BigDecimal[][][] holdvBD = null;
            if(this.arbprec)holdvBD = new BigDecimal[this.nPointsZ][this.nPointsY][this.nPointsX];
            for(int i=0; i=wwx){
                lpx = i - wwx;
            }
            else{
                lpx = 0;               
            }
            if(i<=this.nPointsX-wwx-1){                    
                upx = i + wwx;
            }
            else{
                upx = this.nPointsX - 1;
            }
            int nw1 = upx - lpx + 1;
            
            // Set y window limits
            for(int j=0; j=wwy){
                    lpy = j - wwy;
                }
                else{
                    lpy = 0;               
                }
                if(j<=this.nPointsY-wwy-1){                    
                    upy = j + wwy;
                }
                else{
                    upy = this.nPointsY - 1;
                }
                int nw2 = upy - lpy + 1;
                
                // Set z window limits
                for(int k=0; k=wwz){
                        lpz = k - wwz;
                    }
                    else{
                        lpz = 0;               
                    }
                    if(k<=this.nPointsZ-wwy-1){                    
                        upz = k + wwz;
                    }
                    else{
                        upz = this.nPointsZ - 1;
                    }
                    int nw3 = upz - lpz + 1;
  
                    // Apply moving average window
                    if(this.arbprec){
                        BigDecimal sumbd = new BigDecimal("0.0");
                        for(int k1=lpx; k1<=upx; k1++){
                            for(int k2=lpy; k2<=upy; k2++){
                                for(int k3=lpz; k3<=upz; k3++){
                                    sumbd = sumbd.add(this.vBDdata[k3][k2][k1]);
                                }
                            }
                        }
                        String xx = (new Integer(nw1*nw2+nw3)).toString();
                        this.vDataMovAvBD[k][j][i] = sumbd.divide(new BigDecimal(xx), BigDecimal.ROUND_HALF_UP);
                        this.vDataMovAv[k][j][i] = this.vDataMovAvBD[k][j][i].doubleValue();
                    }
                    else{
                        double sum = 0.0;
                        for(int k1=lpx; k1<=upx; k1++){
                            for(int k2=lpy; k2<=upy; k2++){
                                for(int k3=lpz; k3<=upz; k3++){
                                    sum += this.vData[k3][k2][k1];
                                }
                            }
                        }
                        this.vDataMovAv[k][j][i] = sum/(nw1*nw2*nw3);
                        String xx = (new Double(this.vDataMovAv[k][j][i])).toString();
                        this.vDataMovAvBD[k][j][i] = new BigDecimal(xx);
                    }
                }
            }
        }
        
        // Set up interpolation
        this.tcsMovAv = new TriCubicSpline(this.zData, this.yData, this.xData, this.vDataMovAv);
                
        this.calcMovAv = true;
        return Conv.copy(this.vDataMovAv);
    }
    
    // Smooth with a moving average symmetric window of dimensions maWindowWidth, maWindowWidth and maWindowWidtz
    public double[][][] movingAverage(int maWindowWidth){
          return this.movingAverage(maWindowWidth, maWindowWidth, maWindowWidth);
    }
       
    // Smooth with a moving average window of dimensions maWindowWidthx and maWindowWidthy
    // Return values as BigDecimal
    public BigDecimal[][][] movingAverageAsBigDecimal(int maWindowWidthx, int maWindowWidthy, int maWindowWidthz){
        this.movingAverage(maWindowWidthx, maWindowWidthy, maWindowWidthx);
        return Conv.copy(this.vDataMovAvBD);
    }
          
    // Smooth with a moving average window of dimensions maWindowWidthx and maWindowWidthy
    // Return values as BigDecimal
    public BigDecimal[][][] movingAverageAsBigDecimal(int maWindowWidth){
        this.movingAverage(maWindowWidth, maWindowWidth, maWindowWidth);
        return Conv.copy(this.vDataMovAvBD);
    }
    
    // Adust width to an odd number of points
    private int windowLength(int width){
    
        int ww = 0;
        if(Fmath.isEven(width)){
            ww = width+1;
        }
        else{
           ww = width;
        }
        return ww;
    }

    
    // SAVITZKY-GOLAY FILTER
    
    // Smooth with a Savitzky-Golay filter with a window of dimensions sgWindowWidthx, sgWindowWidthy and sgWindowWidthz
    public double[][][] savitzkyGolay(int sgWindowWidthx, int sgWindowWidthy, int sgWindowWidthz){              
       
        this.lastMethod = 2;
        
        this.vDataSavGol = new double[this.nPointsZ][this.nPointsY][this.nPointsX];
        
        // adjust window width to an odd number of points
        this.sgWindowWidthx = this.windowLength(sgWindowWidthx);
        this.sgWindowWidthy = this.windowLength(sgWindowWidthy);
        this.sgWindowWidthz = this.windowLength(sgWindowWidthz);
        
        // Apply filter 
        this.savitzkyGolayCommon(this.sgWindowWidthx, this.sgWindowWidthy, this.sgWindowWidthz);
                       
        // Set up interpolation
        this.tcsSavGol = new TriCubicSpline(this.zData, this.yData, this.xData, Conv.copy(this.vDataSavGol));
         
                 
        this.calcSavGol = true;
        return Conv.copy(this.vDataSavGol);
    }
    
    // Smooth with a Savitzky-Golay filter with a window of dimensions sgWindowWidth x sgWindowWidthy x sgWindowWidthz
    public double[][][] savitzkyGolay(int sgWindowWidth){ 
        return this.savitzkyGolay(sgWindowWidth, sgWindowWidth, sgWindowWidth);              
    }             
   
    // Common method for smoothing with a Savitzky-Golay filter with a window of width sgWindowWidth
    private double[][] []savitzkyGolayCommon(int widthx, int widthy, int widthz){
        
        // Set filter dimensions
        int wwx = (widthx - 1)/2;
        int wwy = (widthy - 1)/2;
        int wwz = (widthz - 1)/2;
  
        // Calculate filter coefficients
        double[] coeff = (this.savitzkyGolayFilter(wwx, wwx, wwy, wwy, wwz, wwz))[0];
        
        // Padout the data to solve edge effects
        double[][][] psData = this.padData(this.vData, wwx, wwy, wwz);

        // Apply filter       
        for(int i=wwz; ithis.sgPolyDeg)throw new IllegalArgumentException("The sum of the derivative orders " + m + " plus " + n + " plus " + o + ", must be less than or equal to the polynomial degree, " + this.sgPolyDeg + ".");
 
        this.lastMethod = 2;
        double[][][][] ret = new double[2][this.nPointsZ][this.nPointsY][this.nPointsX];
        
         // adjust window width to an odd number of points
        this.sgWindowWidthx = this.windowLength(sgWindowWidthx);
        int wwx = (this.sgWindowWidthx - 1)/2;
        this.sgWindowWidthy = this.windowLength(sgWindowWidthy);
        int wwy = (this.sgWindowWidthy - 1)/2;
        this.sgWindowWidthz = this.windowLength(sgWindowWidthz);
        int wwz = (this.sgWindowWidthz - 1)/2;
        
        if(!this.calcSavGol)this.savitzkyGolay(sgWindowWidthx, sgWindowWidthy, sgWindowWidthz);
        ret[0] = this.vDataSavGol;
        
        // get filter coefficients
        int pointer = 0;
        boolean test = true;
        int len = this.sgCoeffIndices.length;
        while(test){
            if(this.sgCoeffIndices[pointer][0]==m && this.sgCoeffIndices[pointer][1]==n)test = false;
            pointer++;
            if(pointer>=len)throw new IllegalArgumentException("It should not have been possible to reach this situation, m = " + m + ", n = " + n);
        }
        double[] coeff = this.sgArrayC[pointer]; 
        
        // Padout the data to solve edge effects
        double[][][] psData = this.padData(this.vData, wwx, wwy, wwz);

        // Apply filter       
        for(int i=wwz; i=this.nPointsY)throw new IllegalArgumentException("The index, " + yIndex + ", must be less than the number of y values, " + this.nPointsY);
        if(zIndex>=this.nPointsZ)throw new IllegalArgumentException("The index, " + zIndex + ", must be less than the number of y values, " + this.nPointsZ);
 
        int flag = 0;
        double yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        double zValue = Fmath.truncate(this.zData[zIndex], this.trunc);
        this.commonPlot(flag, yIndex, zIndex, yValue, zValue);        
    }
    
    // Plot x section for a given y and z value  - Savitzky-Golay
    public void plotSavitzkyGolayX(double yValue, double zValue){
        if(!this.calcSavGol)throw new IllegalArgumentException("No Savitzky-Golay smoothing method has been called");
        int flag = 0;
        int yIndex = this.findValue(this.yData, yValue);
        yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        int zIndex = this.findValue(this.zData, zValue);
        zValue = Fmath.truncate(this.zData[zIndex], this.trunc);
        this.commonPlot(flag, yIndex, zIndex, yValue, zValue);        
    }
    
    // Plot y section for a given x and z value indices - Savitzky-Golay
    public void plotSavitzkyGolayY(int xIndex, int zIndex){
        if(!this.calcSavGol)throw new IllegalArgumentException("No Savitzky-Golay smoothing method has been called");
        if(zIndex>=this.nPointsZ)throw new IllegalArgumentException("The index, " + zIndex + ", must be less than the number of y values, " + this.nPointsZ);
        if(xIndex>=this.nPointsX)throw new IllegalArgumentException("The index, " + xIndex + ", must be less than the number of x values, " + this.nPointsX);
 
        int flag = 1;
        double zValue = Fmath.truncate(this.yData[zIndex], this.trunc);
        double xValue = Fmath.truncate(this.xData[xIndex], this.trunc);
        this.commonPlot(flag, xIndex, zIndex, xValue, zValue);        
    }
    
    // Plot y section for a given x and z value  - Savitzky-Golay
    public void plotSavitzkyGolayY(double xValue, double zValue){
        if(!this.calcSavGol)throw new IllegalArgumentException("No Savitzky-Golay smoothing method has been called");
        int flag = 1;
        int xIndex = this.findValue(this.xData, xValue);
        xValue = Fmath.truncate(this.xData[xIndex], this.trunc);
        int zIndex = this.findValue(this.zData, zValue);
        zValue = Fmath.truncate(this.zData[zIndex], this.trunc);
        this.commonPlot(flag, xIndex, zIndex, xValue, zValue);        
    }
    
   // Plot z section for a given x and y value indices - Savitzky-Golay
    public void plotSavitzkyGolayZ(int xIndex, int yIndex){
        if(!this.calcSavGol)throw new IllegalArgumentException("No Savitzky-Golay smoothing method has been called");
        if(yIndex>=this.nPointsY)throw new IllegalArgumentException("The index, " + yIndex + ", must be less than the number of y values, " + this.nPointsY);
        if(xIndex>=this.nPointsX)throw new IllegalArgumentException("The index, " + xIndex + ", must be less than the number of x values, " + this.nPointsX);
 
        int flag = 2;
        double yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        double xValue = Fmath.truncate(this.xData[xIndex], this.trunc);
        this.commonPlot(flag, xIndex, yIndex, xValue, yValue);        
    }

    // Plot z section for a given x and y value  - Savitzky-Golay
    public void plotSavitzkyGolayZ(double xValue, double yValue){
        if(!this.calcSavGol)throw new IllegalArgumentException("No Savitzky-Golay smoothing method has been called");
        int flag = 2;
        int xIndex = this.findValue(this.xData, xValue);
        xValue = Fmath.truncate(this.xData[xIndex], this.trunc);
        int yIndex = this.findValue(this.yData, yValue);
        yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        this.commonPlot(flag, xIndex, yIndex, xValue, yValue);        
    }
    
    // Plot x section for a given y and z value indices - Moving Average
    public void plotMovingAverageX(int yIndex, int zIndex){
        if(!this.calcSavGol)throw new IllegalArgumentException("No moving average smoothing method has been called");
        if(yIndex>=this.nPointsY)throw new IllegalArgumentException("The index, " + yIndex + ", must be less than the number of y values, " + this.nPointsY);
        if(zIndex>=this.nPointsZ)throw new IllegalArgumentException("The index, " + zIndex + ", must be less than the number of z values, " + this.nPointsZ);
 
        int flag = 3;
        double yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        double zValue = Fmath.truncate(this.zData[zIndex], this.trunc);
        this.commonPlot(flag, yIndex, zIndex, yValue, zValue);        
    }
    
    // Plot x section for a given y and z value  - moving average
    public void plotMovingAverageX(double yValue, double zValue){
        if(!this.calcMovAv)throw new IllegalArgumentException("No moving average smoothing method has been called");
        int flag = 3;
        int yIndex = this.findValue(this.yData, yValue);
        yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        int zIndex = this.findValue(this.zData, zValue);
        zValue = Fmath.truncate(this.zData[zIndex], this.trunc);
        this.commonPlot(flag, yIndex, zIndex, yValue, zValue);        
    }
    
    // Plot y section for a given x and z value indices - Moving Average
    public void plotMovingAverageY(int xIndex, int zIndex){
        if(!this.calcMovAv)throw new IllegalArgumentException("No moving average smoothing method has been called");
        if(zIndex>=this.nPointsY)throw new IllegalArgumentException("The index, " + zIndex + ", must be less than the number of y values, " + this.nPointsZ);
        if(xIndex>=this.nPointsX)throw new IllegalArgumentException("The index, " + xIndex + ", must be less than the number of x values, " + this.nPointsX);
 
        int flag = 4;
        double zValue = Fmath.truncate(this.yData[zIndex], this.trunc);
        double xValue = Fmath.truncate(this.yData[xIndex], this.trunc);
        this.commonPlot(flag, xIndex, zIndex, xValue, zValue);        
    }
    
    // Plot y section for a given x and z value  - moving average
    public void plotMovingAverageY(double xValue, double zValue){
        if(!this.calcMovAv)throw new IllegalArgumentException("No moving average smoothing method has been called");
        int flag = 4;
        int xIndex = this.findValue(this.xData, xValue);
        xValue = Fmath.truncate(this.xData[xIndex], this.trunc);
        int zIndex = this.findValue(this.zData, zValue);
        zValue = Fmath.truncate(this.zData[zIndex], this.trunc);
        this.commonPlot(flag, xIndex, zIndex, xValue, zValue);        
    }
    
   // Plot z section for a given x and y value indices - Moving Average
    public void plotMovingAverageZ(int xIndex, int yIndex){
        if(!this.calcMovAv)throw new IllegalArgumentException("No moving average smoothing method has been called");
        if(yIndex>=this.nPointsY)throw new IllegalArgumentException("The index, " + yIndex + ", must be less than the number of y values, " + this.nPointsY);
        if(xIndex>=this.nPointsX)throw new IllegalArgumentException("The index, " + xIndex + ", must be less than the number of x values, " + this.nPointsX);
 
        int flag = 5;
        double yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        double xValue = Fmath.truncate(this.yData[xIndex], this.trunc);
        this.commonPlot(flag, xIndex, yIndex, xValue, yValue);        
    }

    // Plot z section for a given x and y value  - moving average
    public void plotMovingAverageZ(double xValue, double yValue){
        if(!this.calcMovAv)throw new IllegalArgumentException("No moving avrage smoothing method has been called");
        int flag = 5;
        int xIndex = this.findValue(this.xData, xValue);
        xValue = Fmath.truncate(this.xData[xIndex], this.trunc);
        int yIndex = this.findValue(this.yData, yValue);
        yValue = Fmath.truncate(this.yData[yIndex], this.trunc);
        this.commonPlot(flag, xIndex, yIndex, xValue, yValue);        
    }
    
    // Find index for a value
    private int findValue(double[] array, double value){
        int len = array.length;
        boolean test = true;
        int counter = 0;
        while(test){
           if(Fmath.isEqualWithinLimits(array[counter], value, Math.abs(value)*0.001)){
               test = false;
           } 
           else{
               counter++;
               if(counter>=len)throw new IllegalArgumentException("The entered plot value, " + value + ",  must equal an entered data value");
           }
        }
        return counter;
    }
    
    // Common plot method
    private void commonPlot(int flag, int point1, int point2, double value1, double value2){

        String title0 = null;
        String title1 = null;
        String title1b = ",  Original data - circles,  Smoothed data - squares";
        String xleg = null;
        String yleg = null;
        int[] xPoints ={0, this.nPointsX/4, this.nPointsX/2, 3*this.nPointsX/4, this.nPointsX-1};
        int[] yPoints ={0, this.nPointsY/4, this.nPointsY/2, 3*this.nPointsY/4, this.nPointsY-1};
        int[] zPoints ={0, this.nPointsZ/4, this.nPointsZ/2, 3*this.nPointsZ/4, this.nPointsZ-1};
        double[][] data = new double[8][];
        double[] holdx1 = new double[5];
        double[] holdx2 = new double[5];
        double[] holdy1 = new double[5];
        double[] holdy2 = new double[5];
        double[] holdz1 = new double[5];
        double[] holdz2 = new double[5];
        
        switch(flag){
            
           case 0:  // Savitzky-Golay  v versus x
                    title0 = "Savitzky-Golay smoothing with an x by y by z window of " + this.maWindowWidthx + " x " +  + this.maWindowWidthy + " x " +  + this.maWindowWidthz + " points";
                    title1 = "Plot of v versus x values for a y value of " + value1 + " a z value of "+  value2 + title1b;
                    xleg = "x values";  // CHANGE FOR SWAPPED INDICES
                    yleg = "v values";
                    data[0] = this.xData;
                    data[1] = this.vData[point2][point1];
                    data[2] = this.xData;
                    data[3] = this.vDataSavGol[point2][point1];
                    for(int i=0; i<5;i++){
                        holdx1[i] = this.xData[xPoints[i]];
                        holdy1[i] = this.vData[point2][point1][xPoints[i]];
                        holdx2[i] = this.xData[xPoints[i]];
                        holdy2[i] = this.vDataSavGol[point2][point1][xPoints[i]];
                    }
                    break;
                
            case 1: // Savitzky-Golay  v versus y
                    title0 = "Savitzky-Golay smoothing with an x by y by z window of " + this.maWindowWidthx + " x " +  + this.maWindowWidthy + " x " +  + this.maWindowWidthz + " points";
                    title1 = "Plot of v versus y values for a x value of " + value1 + " a z value of "+  value2 + title1b;
                    xleg = "y values";
                    yleg = "v values";
                    data[0] = this.yData;
                    data[2] = this.yData;
                    data[1] = new double[this.nPointsY];
                    data[3] = new double[this.nPointsY];
                    for(int i=0; i 

Ads help maintain this website.