/* * GridStatistics1_1.java * * Created on 18 February 2002, 14:22 */ package uk.ac.leeds.ccg.cluster.grids; /** * @author andyt * * Description: * 1. This class is for GridStatistics that are not constantly updated but * calculated on request. Having calculated the stats it would be inexpensive * to convert into a GridStats object that is constantly updated. * 2. Sparseness is updated in this class. However there is scope for another * class that does not. This could be desirable if the grid values are stored in * a fixed data structure (ie. one that does not check on sparseness to optimise * storage by changing data structure eg. from double[] to Hashtable()). */ public class GridStatistics1_1 extends AbstractGridStatistics { /** * Indicates if the above field values are definitely correct. This is generally only true if the underlying data has not changed. */ protected boolean isUpToDate=false; /** Creates a new instance of GridStatistics1_0 */ public GridStatistics1_1(AbstractGrid2DSquareCellDouble grid) { setGrid(grid); } /** * Returns a description. */ public String toString() { if (getIsUpToDate()) { return "GridStatistics1_1(isUpToDate(true),sparseness("+getSparseness()+"),sum("+getSum()+"),min("+getMin()+"),minCount("+getMinCount()+"),max("+getMax()+"),maxCount("+getMaxCount()+"))"; } else { return "GridStatistics1_1(isUpToDate(false),sparseness("+this.sparseness+"),sum("+this.sum+"),min("+this.min+"),minCount("+this.minCount+"),max("+this.max+"),maxCount("+this.maxCount+"))"; } } /** * Returns true iff stats are upto date */ public boolean getIsUpToDate() {return this.isUpToDate;} /** * Sets isUptoDate. */ public void setIsUpToDate(boolean isUpToDate) {this.isUpToDate=isUpToDate;} /** * Returns sparseness. */ public double getSparseness() { if (getIsUpToDate()) { return this.sparseness; } else { doStats(); } return this.sparseness; } /** * Sets sparseness. */ public void setSparseness(double sparseness) {this.sparseness=sparseness;} /** * Returns sum. */ public double getSum() { if (getIsUpToDate()) { return this.sum; } else { doStats(); } return this.sum; } /** * Ignores call to set sum. */ public void setSum(double sum) {} /** * Ignores call to set min. */ public void setMin(double min) {} /** * Returns the minimum value in the Grid. */ public double getMin() { if (getIsUpToDate()) { return this.min; } else { doStats(); } return this.min; } /** * Returns minCount. */ public int getMinCount() { if (getIsUpToDate()) { return this.minCount; } else { doStats(); } return this.minCount; } /** * Ignores call to set minCount. */ public void setMinCount(int minCount) {} /** * Ignores Call to doMin(). */ public void doMin(double d1) {} /** * Ignores call to set max. */ public void setMax(double max) {} /** * Returns the maximum value in the Grid. */ public double getMax() { if (getIsUpToDate()) { return this.max; } else { doStats(); } return this.max; } /** * Returns maxCount. */ public int getMaxCount() { if (getIsUpToDate()) { return this.maxCount; } else { doStats(); } return this.maxCount; } /** * Ignores Call to set maxCount. */ public void setMaxCount(int maxCount) {} /** * Ignores Call to doMax(). */ public void doMax(double d1) {} /** * Calculates statistics for the grid */ public void doStats() { AbstractGrid2DSquareCellDouble grid = getGrid(); int nrows = grid.getNrows(); int ncols = grid.getNcols(); double sparseness = 1.0; double cellAsProportionOfGrid = 1 / (double) (nrows * ncols); double sum = 0.0; double max = Double.NEGATIVE_INFINITY; int maxCount = nrows * ncols; double min = Double.POSITIVE_INFINITY; int minCount = maxCount; double noDataValue = grid.getNoDataValue(); double d1; for (int i = 0; i < nrows; i++) { for (int j = 0; j < ncols; j++) { d1 = grid.getCell(i,j); if (d1 != noDataValue) { sparseness -= cellAsProportionOfGrid; sum += d1; if (d1 > max) {maxCount = 1; max = d1;} if (d1 == max) {maxCount++;} if (d1 < min) {minCount = 1;min = d1;} if (d1 == min) {minCount++;} } } } this.sparseness = sparseness; this.sum = sum; this.max = max; this.maxCount = maxCount; this.min = min; this.minCount = minCount; setIsUpToDate(true); } /** * Updates sum min minCount recalculateMin max maxCount recalculateMax from * inputGridStatistics if they are up-to-date else sets isUpToDate to false. */ public void updateGridStatisticsFrom(AbstractGridStatistics inputGridStatistics) { if (inputGridStatistics instanceof GridStatistics1_0) { this.sum=inputGridStatistics.getSum(); this.min=inputGridStatistics.getMin(); this.minCount=inputGridStatistics.getMinCount(); this.max=inputGridStatistics.getMax(); this.maxCount=inputGridStatistics.getMaxCount(); setIsUpToDate(true); } else { GridStatistics1_1 inGridStatistics = (GridStatistics1_1) inputGridStatistics; if (inGridStatistics.getIsUpToDate()) { this.sum=inputGridStatistics.getSum(); this.min=inputGridStatistics.getMin(); this.minCount=inputGridStatistics.getMinCount(); this.max=inputGridStatistics.getMax(); this.maxCount=inputGridStatistics.getMaxCount(); setIsUpToDate(true); } else { setIsUpToDate(false); } } } }