/* * Grid2DSquareCellDoubleFile.java * Created on 5 November 2001, 17:00 */ package uk.ac.leeds.ccg.cluster.grids; import uk.ac.leeds.ccg.raster.*; import uk.ac.leeds.ccg.geotools.*; import java.io.*; import java.lang.*; import java.util.*; /** * Last updated on 17 December 2001 * @author andyt * @version 1.0 */ public class Grid2DSquareCellDoubleFile extends AbstractGrid2DSquareCellDouble { /** * This is a potential dummy/substitute for noDataValue. * (Time can be saved in some constructors file initialisation if noDataValue=noData=0.0d.) */ private final double noData = 0.0d; /** * The directory in which the data is stored. */ private String dataDirectory; /** * The file containing all the grid values in row major order. */ private File gridFile; /** * The RandomAccessFile for accessing gridFile. */ private RandomAccessFile gridRandomAccessFile; /** * Default constructor. * Constructs a grid containing noDataValues. */ public Grid2DSquareCellDoubleFile() { this(System.getProperty("java.io.tmpdir")); } /* * @param dataDirectory - the system directory path where the data will be stored as a file */ public Grid2DSquareCellDoubleFile(String dataDirectory) { setDataDirectory(dataDirectory); String gridFilename = new String(dataDirectory+"g"+System.currentTimeMillis()); File grid = new File(gridFilename); try { if (grid.exists()) { boolean gridCreated = false; while (!gridCreated) { gridFilename = System.getProperty("java.io.tmpdir")+"g"+System.currentTimeMillis(); grid = new File(gridFilename); if (!grid.exists()) { grid.createNewFile(); gridCreated = true; } } } else { grid.createNewFile(); } // Set to delete on exiting the Java Virtual Machine grid.deleteOnExit(); Grid2DSquareCellDoubleFile dummy = new Grid2DSquareCellDoubleFile(grid,1,1); initNcols(dummy.getNcols()); initNrows(dummy.getNrows()); setXllcorner(dummy.getXllcorner()); setYllcorner(dummy.getYllcorner()); setCellsize(dummy.getCellsize()); initNoDataValue(dummy.getNoDataValue()); initGridStatistics(dummy.getGridStatistics()); setFile(dummy.getFile()); setRandomAccessFile(dummy.getRandomAccessFile()); //dummy.clear(); } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" creating "+grid+" in Grid2DSquareCellDoubleFile(dataDirectory("+dataDirectory+"))"); System.exit(0); } } /** * @param gridFile - the file in which the data is to be stored * @param nrows see nrows * @param ncols see ncols */ public Grid2DSquareCellDoubleFile(File gridFile,int nrows,int ncols) { this(gridFile,nrows,ncols,0.0d,0.0d,1.0d,Double.NaN); } /** * @param xllcorner see xllcorner * @param yllcorner see yllcorner * @param cellsize see cellsize * @param noDataValue see noDataValue */ public Grid2DSquareCellDoubleFile(File gridFile,int nrows,int ncols,double xllcorner,double yllcorner,double cellsize,double noDataValue) { this(gridFile,nrows,ncols,xllcorner,yllcorner,cellsize,noDataValue,true); } /** * @param keepGridStatisticsUpToDate - used to define which AbstractGridStatistic object to instantiate */ public Grid2DSquareCellDoubleFile(File gridFile,int nrows,int ncols,double xllcorner,double yllcorner,double cellsize,double noDataValue,boolean keepGridStatisticsUpToDate) { if (DEBUG > 0) {System.out.println("Grid2DSquareCellDoubleFile(File("+gridFile+"),nrows("+nrows+"),ncols("+ncols+"),xllcorner("+xllcorner+"),yllcorner("+yllcorner+"),cellsize("+cellsize+"),noDataValue("+noDataValue+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+")) {");} setDataDirectory(gridFile.getAbsolutePath()); setFile(gridFile); AbstractGridStatistics gridStatistics; if (keepGridStatisticsUpToDate) { gridStatistics = new GridStatistics1_0(this); } else { gridStatistics = new GridStatistics1_1(this); } initGridStatistics(gridStatistics); RandomAccessFile gridRandomAccessFile; try { gridRandomAccessFile = new RandomAccessFile(gridFile,"rw"); setRandomAccessFile(gridRandomAccessFile); initNrows(nrows); initNcols(ncols); setXllcorner(xllcorner); setYllcorner(yllcorner); setCellsize(cellsize); initNoDataValue(noDataValue); try { if (noDataValue == noData) { // There is a fast way to initialise the file! gridRandomAccessFile.seek(((nrows * ncols) - 1) * 8); gridRandomAccessFile.writeDouble(noData); } else { gridRandomAccessFile.seek(0); for (int i = 0; i < nrows; i ++) { for (int j = 0; j < ncols; j ++) { //initCell(i,j,noDataValue); gridRandomAccessFile.writeDouble(noDataValue); // May as well just populate with noDataValues as field variables do not change! } } } } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" initialising grid values in Grid2DSquareCellDoubleFile(File("+gridFile+"),nrows("+nrows+"),ncols("+ncols+"),xllcorner("+xllcorner+"),yllcorner("+yllcorner+"),cellsize("+cellsize+"),noDataValue("+noDataValue+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+"))"); System.exit(0); } } catch (java.io.FileNotFoundException fnfe1) { System.out.println(""+fnfe1+" creating RandomAccessFile in Grid2DSquareCellDoubleFile(File("+gridFile+"),nrows("+nrows+"),ncols("+ncols+"),xllcorner("+xllcorner+"),yllcorner("+yllcorner+"),cellsize("+cellsize+"),noDataValue("+noDataValue+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+"))"); System.exit(0); } if (DEBUG > 1) {System.out.println(toString());} if (DEBUG > 0) {System.out.println("}");} } /** * Constructs from an AbstractGrid2DSquareCellDouble. * This creates a complete and independent copy. * @param input - the AbstractGrid2DSquareCellDoubleFile1_0 object to construct from * @param grid - the file for storing the constructed grid data which is not the same as input.getFile(). */ public Grid2DSquareCellDoubleFile(AbstractGrid2DSquareCellDouble inputGrid) { Grid2DSquareCellDoubleFile dummy; String dataDirectory; if (inputGrid instanceof Grid2DSquareCellDoubleFile) { Grid2DSquareCellDoubleFile inGrid = (Grid2DSquareCellDoubleFile) inputGrid; dataDirectory = inGrid.getDataDirectory(); dummy = new Grid2DSquareCellDoubleFile(inputGrid,dataDirectory); } else { dataDirectory = System.getProperty("java.io.tmpdir"); dummy = new Grid2DSquareCellDoubleFile(inputGrid,dataDirectory); } initNcols(dummy.getNcols()); initNrows(dummy.getNrows()); setXllcorner(dummy.getXllcorner()); setYllcorner(dummy.getYllcorner()); setCellsize(dummy.getCellsize()); initNoDataValue(dummy.getNoDataValue()); initGridStatistics(dummy.getGridStatistics()); setDataDirectory(dataDirectory); setFile(dummy.getFile()); setRandomAccessFile(dummy.getRandomAccessFile()); getFile().deleteOnExit(); } public Grid2DSquareCellDoubleFile(AbstractGrid2DSquareCellDouble inputGrid,String dataDirectory) { String gridFilename = new String(dataDirectory+"g"+System.currentTimeMillis()); File gridFile = new File(gridFilename); try { if (gridFile.exists()) { boolean gridCreated = false; while (!gridCreated) { gridFilename = System.getProperty("java.io.tmpdir")+"g"+System.currentTimeMillis(); gridFile = new File(gridFilename); if (!gridFile.exists()) { gridFile.createNewFile(); gridCreated = true; } } } else { gridFile.createNewFile(); } // Set to delete on exiting the Java Virtual Machine gridFile.deleteOnExit(); Grid2DSquareCellDoubleFile dummy = new Grid2DSquareCellDoubleFile(inputGrid,gridFile); initNcols(dummy.getNcols()); initNrows(dummy.getNrows()); setXllcorner(dummy.getXllcorner()); setYllcorner(dummy.getYllcorner()); setCellsize(dummy.getCellsize()); initNoDataValue(dummy.getNoDataValue()); initGridStatistics(dummy.getGridStatistics()); setDataDirectory(dataDirectory); setFile(dummy.getFile()); setRandomAccessFile(dummy.getRandomAccessFile()); } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" creating "+gridFile+" in Grid2DSquareCellDoubleFile(AbstractGrid2DSquareCellDouble("+inputGrid.toString()+"),dataDirectory("+dataDirectory+"))"); System.exit(0); } } public Grid2DSquareCellDoubleFile(AbstractGrid2DSquareCellDouble inputGrid,File gridFile) { this(inputGrid,gridFile,0,0,(inputGrid.getNrows()-1),(inputGrid.getNcols()-1)); } public Grid2DSquareCellDoubleFile(AbstractGrid2DSquareCellDouble inputGrid,File gridFile,int startRowIndex,int startColIndex,int endRowIndex,int endColIndex) { // The default is to set it so the AbstractGridStatistics object instantiated is of the same type as that for inputGrid Grid2DSquareCellDoubleFile dummy; AbstractGridStatistics inputGridStatistics = inputGrid.getGridStatistics(); if (inputGridStatistics instanceof GridStatistics1_0) { dummy = new Grid2DSquareCellDoubleFile(inputGrid,gridFile,0,0,(inputGrid.getNrows()-1),(inputGrid.getNcols()-1),true); } else { dummy = new Grid2DSquareCellDoubleFile(inputGrid,gridFile,0,0,(inputGrid.getNrows()-1),(inputGrid.getNcols()-1),false); } initNcols(dummy.getNcols()); initNrows(dummy.getNrows()); setXllcorner(dummy.getXllcorner()); setYllcorner(dummy.getYllcorner()); setCellsize(dummy.getCellsize()); initNoDataValue(dummy.getNoDataValue()); initGridStatistics(dummy.getGridStatistics()); setDataDirectory(gridFile.getPath()); setFile(dummy.getFile()); setRandomAccessFile(dummy.getRandomAccessFile()); } public Grid2DSquareCellDoubleFile(AbstractGrid2DSquareCellDouble inputGrid,File gridFile,int startRowIndex,int startColIndex,int endRowIndex,int endColIndex,boolean keepGridStatisticsUpToDate) { if (DEBUG > 0) {System.out.println("Grid2DSquareCellDouble(AbstractGrid2DSquareCellDouble("+inputGrid.toString()+"),gridFile("+gridFile+"),startRowIndex("+startRowIndex+"),startColIndex("+startColIndex+"),endRowIndex("+endRowIndex+"),endColIndex("+endColIndex+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+")) {");} int inputGridNrows = inputGrid.getNrows(); int inputGridNcols = inputGrid.getNcols(); double cellsize = inputGrid.getCellsize(); int outputGridNrows = (endRowIndex - startRowIndex + 1); int outputGridNcols = (endColIndex - startColIndex + 1); initNrows(outputGridNrows); initNcols(outputGridNcols); setXllcorner(inputGrid.getXllcorner() + (double) startColIndex * cellsize); setYllcorner(inputGrid.getYllcorner() + (double) (endRowIndex - inputGridNrows + 1) * cellsize); setCellsize(cellsize); double noDataValue = inputGrid.getNoDataValue(); initNoDataValue(noDataValue); AbstractGridStatistics inputGridStatistics = inputGrid.getGridStatistics(); AbstractGridStatistics outputGridStatistics; if (keepGridStatisticsUpToDate) { outputGridStatistics = new GridStatistics1_0(this); } else { outputGridStatistics = new GridStatistics1_1(this); } initGridStatistics(outputGridStatistics); double inputGridSparseness = inputGridStatistics.getSparseness(); setFile(gridFile); try { setRandomAccessFile(new RandomAccessFile(gridFile,"rw")); } catch (java.io.FileNotFoundException fnfe1) { System.out.println(""+fnfe1+" creating RandomAccessFile in Grid2DSquareCellDouble(AbstractGrid2DSquareCellDouble("+inputGrid.toString()+"),gridFile("+gridFile+"),startRowIndex("+startRowIndex+"),startColIndex("+startColIndex+"),endRowIndex("+endRowIndex+"),endColIndex("+endColIndex+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+"))"); System.exit(0); } if (inputGrid instanceof Grid2DSquareCellDoubleFile) { Grid2DSquareCellDoubleFile dummy; Grid2DSquareCellDoubleFile inGrid = (Grid2DSquareCellDoubleFile) inputGrid; File inputGridFile=inGrid.getFile(); if (startRowIndex<=0 && startColIndex<=0 && endRowIndex>=(inputGridNrows - 1) && endColIndex>=(inputGridNcols - 1)) { if (startRowIndex==0 && startColIndex==0 && endRowIndex==(inputGridNrows - 1) && endColIndex==(inputGridNcols - 1)) { // Copy inputGridFile into gridFile try { // This is an optimisation for SunOS so the file is copied directly by the operating system. // NB. For other platforms similar optimisations can be inserted here. String os = new String(System.getProperty("os.name")); if (os.equalsIgnoreCase("SunOS")) { Runtime runtime = java.lang.Runtime.getRuntime(); runtime.exec("cp "+inputGridFile+" "+gridFile); } else { BufferedInputStream bis=new BufferedInputStream(new FileInputStream(inputGridFile)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(gridFile)); for (int i = 0; i < inputGridFile.length(); i ++) { bos.write(bis.read()); } bos.flush(); bos.close(); bis.close(); } } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" in Grid2DSquareCellDouble(AbstractGrid2DSquareCellDouble("+inputGrid.toString()+"),gridFile("+gridFile+"),startRowIndex("+startRowIndex+"),startColIndex("+startColIndex+"),endRowIndex("+endRowIndex+"),endColIndex("+endColIndex+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+"))"); System.exit(0); } outputGridStatistics.updateGridStatisticsFrom(inputGridStatistics); } else { dummy = new Grid2DSquareCellDoubleFile(inputGridFile,new GeoRectangle(getXllcorner(),getYllcorner(),getWidth(),getHeight()),cellsize,noDataValue,startRowIndex,startColIndex,endRowIndex,endColIndex,gridFile,keepGridStatisticsUpToDate); initNcols(dummy.getNcols()); initNrows(dummy.getNrows()); setXllcorner(dummy.getXllcorner()); setYllcorner(dummy.getYllcorner()); setCellsize(dummy.getCellsize()); initNoDataValue(dummy.getNoDataValue()); initGridStatistics(dummy.getGridStatistics()); setDataDirectory(gridFile.getPath()); setFile(dummy.getFile()); setRandomAccessFile(dummy.getRandomAccessFile()); //double outputGridSparseness = ((inputGridSparseness * inputGridNrows * inputGridNcols) + ((outputGridNrows * outputGridNcols) - (inputGridNrows * inputGridNcols))) / (outputGridNrows * outputGridNcols); //outputGridStatistics.setSparseness(outputGridSparseness); } } else { dummy = new Grid2DSquareCellDoubleFile(inputGridFile,new GeoRectangle(getXllcorner(),getYllcorner(),getWidth(),getHeight()),cellsize,noDataValue,startRowIndex,startColIndex,endRowIndex,endColIndex,gridFile,true); initNcols(dummy.getNcols()); initNrows(dummy.getNrows()); setXllcorner(dummy.getXllcorner()); setYllcorner(dummy.getYllcorner()); setCellsize(dummy.getCellsize()); initNoDataValue(dummy.getNoDataValue()); initGridStatistics(dummy.getGridStatistics()); setDataDirectory(gridFile.getPath()); setFile(dummy.getFile()); setRandomAccessFile(dummy.getRandomAccessFile()); } } else { // inputGrid instanceof Grid2DSquareCellDouble Grid2DSquareCellDouble inGrid = (Grid2DSquareCellDouble) inputGrid; for (int i = startRowIndex; i <= endRowIndex; i ++) { for (int j = startColIndex; i <= endColIndex; i ++) { initCell((i - startRowIndex),(j - startColIndex),inGrid.getCell(i,j)); } } } if (DEBUG > 1) {System.out.println(toString());} if (DEBUG > 0) {System.out.println("}");} } /** * Constructs from a binary file representation of a grid where the values are written in row major order as double values. * The constructed grid has endColIndex-startColIndex+1 columns and endRowIndex-startRowIndex+1 rows. * The values in the constructed grid File are initialized with noDataValue and replaced with coincident cells from inputGrid. * @param inputGrid - the binary file representaion of the grid to construct from * @param gr1 - a uk.ac.leeds.ccg.egotools.GeoRectangle which specifies the dimensions (bounds) of inputGrid * @param cellsize - the width/height of a grid cell of the constructed grid * @param noDataValue - the value to use as the noDataValue of the grids * @param grid - the file for storing the constructed grid data which is not the same as inputGrid */ public Grid2DSquareCellDoubleFile(File inputGridFile,GeoRectangle gr1,double cellsize,double noDataValue,int startRowIndex,int startColIndex,int endRowIndex,int endColIndex,File gridFile) { this(inputGridFile,gr1,cellsize,noDataValue,startRowIndex,startColIndex,endRowIndex,endColIndex,gridFile,true); } public Grid2DSquareCellDoubleFile(File inputGridFile,GeoRectangle gr1,double cellsize,double noDataValue,int startRowIndex,int startColIndex,int endRowIndex,int endColIndex,File gridFile,boolean keepGridStatisticsUpToDate) { if (DEBUG>0) {System.out.println("Grid2DSquareCellDoubleFile(inputGridFile("+inputGridFile+"),GeoRectangle("+gr1.toString()+"),cellsize("+cellsize+"),noDataValue("+noDataValue+"),startRowIndex("+startRowIndex+"),startColIndex("+startColIndex+"),endRowIndex("+endRowIndex+"),endColIndex("+endColIndex+"),gridFile("+gridFile+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+")) {");} RandomAccessFile inputGridRandomAccessFile; RandomAccessFile gridRandomAccessFile; setDataDirectory(gridFile.getPath()); setFile(gridFile); AbstractGridStatistics outputGridStatistics; if (keepGridStatisticsUpToDate) { outputGridStatistics = new GridStatistics1_0(this); } else { outputGridStatistics = new GridStatistics1_1(this); } initGridStatistics(outputGridStatistics); try { gridRandomAccessFile = new RandomAccessFile(gridFile,"rw"); inputGridRandomAccessFile = new RandomAccessFile(inputGridFile,"rw"); setRandomAccessFile(gridRandomAccessFile); int inputGridNcols = (int) Math.floor(gr1.getWidth() / cellsize); int inputGridNrows = (int) Math.floor(gr1.getHeight() / cellsize); int ncols = endColIndex - startColIndex + 1; int nrows = endRowIndex - startRowIndex + 1; initNcols(ncols); initNrows(nrows); setXllcorner(gr1.getX()+(((double) startColIndex) * cellsize)); setYllcorner(gr1.getY()+(((double) (endRowIndex - inputGridNrows + 1)) * cellsize)); setCellsize(cellsize); setNoDataValue(noDataValue); // Initilise file // Replace coincident values from input if (!(startColIndex >= inputGridNcols || endColIndex < 0 || startRowIndex >= inputGridNrows || endRowIndex < 0)) { // Only need do something if there are coincident values. int caseType=0; // There are 16 cases to deal with. if (startColIndex <= 0 && startRowIndex <= 0 && endRowIndex >= inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 1;} if (startColIndex <= 0 && startRowIndex <= 0 && endRowIndex >= inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 2;} if (startColIndex <= 0 && startRowIndex <= 0 && endRowIndex < inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 3;} if (startColIndex <= 0 && startRowIndex <= 0 && endRowIndex < inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 4;} if (startColIndex > 0 && startRowIndex <= 0 && endRowIndex >= inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 5;} if (startColIndex > 0 && startRowIndex <= 0 && endRowIndex >= inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 6;} if (startColIndex > 0 && startRowIndex <= 0 && endRowIndex < inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 7;} if (startColIndex > 0 && startRowIndex <= 0 && endRowIndex < inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 8;} if (startColIndex <= 0 && startRowIndex > 0 && endRowIndex >= inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 9;} if (startColIndex <= 0 && startRowIndex > 0 && endRowIndex >= inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 10;} if (startColIndex <= 0 && startRowIndex > 0 && endRowIndex < inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 11;} if (startColIndex <= 0 && startRowIndex > 0 && endRowIndex < inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 12;} if (startColIndex > 0 && startRowIndex > 0 && endRowIndex >= inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 13;} if (startColIndex > 0 && startRowIndex > 0 && endRowIndex >= inputGridNrows - 1 && endColIndex < inputGridNcols -1 ) {caseType = 14;} if (startColIndex > 0 && startRowIndex > 0 && endRowIndex < inputGridNrows - 1 && endColIndex >= inputGridNcols -1 ) {caseType = 15;} if (startColIndex > 0 && startRowIndex > 0 && endRowIndex < inputGridNrows - 1 && endColIndex < inputGridNcols - 1) {caseType = 16;} //System.out.print("caseType "+caseType); switch (caseType) { case 1 : inputGridRandomAccessFile.seek(0); for (int i = 0; i < inputGridNrows; i ++) { for (int j = 0; j 1) {System.out.println(toString());} if (DEBUG > 0) {System.out.println("}");} } /** * Constructs from a binary file representation of a grid. * This constructor uses the binary file passed in. * @param nrows - the number of rows in the grid * @param ncols - the number of cols in the grid * @param xllcorner - the lower left x coordinate of the grid * @param yllcorner - the lower left y coordinate of the grid * @param cellsize - the cell size of the constructed grid * @param noDataValue - the noDataValue of the grids. * @param grid - File containing the binary representation of a grid. * @param sum - the sum of the grid. * @param min - the min of the grid. * @param minCount - the minCount of the grid. * @param max - the max of the grid. * @param maxCount - the maxCount of the grid. */ public Grid2DSquareCellDoubleFile(File gridFile,int ncols,int nrows,double xllcorner,double yllcorner,double cellsize,double noDataValue,double sum,double min,int minCount,double max,int maxCount) { this(gridFile,ncols,nrows,xllcorner,yllcorner,cellsize,noDataValue,sum,min,minCount,max,maxCount,true); } public Grid2DSquareCellDoubleFile(File gridFile,int ncols,int nrows,double xllcorner,double yllcorner,double cellsize,double noDataValue,double sum,double min,int minCount,double max,int maxCount,boolean keepGridStatisticsUpToDate) { if (DEBUG > 0) {System.out.println("Grid2DSquareCellDoubleFile(gridFile("+gridFile+")ncols("+ncols+"),nrows("+nrows+"),xllcorner("+xllcorner+"),yllcorner("+yllcorner+"),noDataValue("+noDataValue+"),sum("+sum+"),min("+min+"),minCount("+minCount+"),max("+max+"),maxCount("+maxCount+"),keepGridStatisticsUptoDate("+keepGridStatisticsUpToDate+")) {");} AbstractGridStatistics gridStatistics; if (keepGridStatisticsUpToDate) { gridStatistics = new GridStatistics1_0(this); } else { gridStatistics = new GridStatistics1_1(this); } initGridStatistics(gridStatistics); setDataDirectory(gridFile.getPath()); setFile(gridFile); try { setRandomAccessFile(new RandomAccessFile(gridFile,"rw")); } catch (java.io.FileNotFoundException fnfe1) { System.out.println(""+fnfe1+" in Grid2DSquareCellDoubleFile(gridFile("+gridFile+")ncols("+ncols+"),nrows("+nrows+"),xllcorner("+xllcorner+"),yllcorner("+yllcorner+"),noDataValue("+noDataValue+"),sum("+sum+"),min("+min+"),minCount("+minCount+"),max("+max+"),maxCount("+maxCount+"),keepGridStatisticsUptoDate("+keepGridStatisticsUpToDate+"))"); System.exit(0); } initNrows(nrows); initNcols(ncols); setXllcorner(xllcorner); setYllcorner(yllcorner); setCellsize(cellsize); initNoDataValue(noDataValue); if (keepGridStatisticsUpToDate) { gridStatistics.doStats(); } // Describe grid if (DEBUG > 1) {System.out.println(toString());} if (DEBUG > 0) {System.out.println("}");} } /** * Constructs from an ArcInfo gridascii file. * @param asciiFile - the ArcInfo format gridascii file to construct from * @param gridFile - the file for storing the constructed grid data which is not the same as asciiFile. */ public Grid2DSquareCellDoubleFile(File asciiFile,File gridFile) { this(asciiFile,gridFile,true); } public Grid2DSquareCellDoubleFile(File asciiFile,File gridFile,boolean keepGridStatisticsUpToDate) { if (DEBUG > 0) {System.out.println("Grid2DSquareCellDoubleFile(asciiFile("+asciiFile+"),gridFile("+gridFile+")) {");} AbstractGridStatistics outputGridStatistics; if (keepGridStatisticsUpToDate) { outputGridStatistics = new GridStatistics1_0(this); } else { outputGridStatistics = new GridStatistics1_1(this); } setDataDirectory(gridFile.getPath()); initGridStatistics(outputGridStatistics); try { // Set up asciiFile reader. StreamTokenizer st = (new StreamTokenizer(new BufferedReader(new InputStreamReader(new FileInputStream(asciiFile))))); st.wordChars('_','_'); st.parseNumbers(); st.eolIsSignificant(false); st.lowerCaseMode(true); // Set up grid writer. setFile(gridFile); try { setRandomAccessFile(new RandomAccessFile(gridFile,"rw")); } catch (java.io.FileNotFoundException fnfe1) { System.out.println(""+fnfe1+" when setting up RandomAccessFile in Grid2DSquareCellDoubleFile(asciiFile("+asciiFile+"),gridFile("+gridFile+"))"); System.exit(0); } // Read header and set parameters from asciiFile. int type = st.nextToken(); st.nextToken(); int ncols = (int)st.nval; initNcols(ncols); st.nextToken(); st.nextToken(); int nrows = (int)st.nval; initNrows(nrows); st.nextToken(); boolean b1 = true; if (st.sval.equalsIgnoreCase("xllcenter") || st.sval.equalsIgnoreCase("xllcentre")) { b1 = false; } st.nextToken(); double xllcorner = st.nval; st.nextToken(); boolean b2 = true; if (st.sval.equalsIgnoreCase("yllcenter") || st.sval.equalsIgnoreCase("yllcentre")) { b2 = false; } st.nextToken(); double yllcorner = st.nval; st.nextToken(); st.nextToken(); cellsize = st.nval; // Correct for xllcorner and yllcorner if need be. if (!b1) { xllcorner -= (cellsize / 2.0d); } if (!b2) { yllcorner -= (cellsize / 2.0d); } setXllcorner(xllcorner); setYllcorner(yllcorner); // Try to get noDataValue as it's optional. type=st.nextToken(); double noDataValue = Double.NaN; if(type == StreamTokenizer.TT_NUMBER) { // Put it back to be read as data and set noDataValue default. // Warning - If there is no noDataValue and grid values are non-numeric this will break! st.pushBack(); if (DEBUG > 2) {System.out.println("No noDataValue detected. Using default...");} } else { type = st.nextToken(); noDataValue = st.nval; } initNoDataValue(noDataValue); // Initialise cell values. double d1 = noDataValue; for (int i = 0; i < nrows; i ++) { for (int j = 0; j < ncols; j ++) { st.nextToken(); d1 = st.nval; type = st.nextToken(); if (type != StreamTokenizer.TT_NUMBER && type != StreamTokenizer.TT_EOF) { // Either an exponent term number or end of file marker or grid value is non-numeric or something else. // This assumes it is an exponent number of the form 0.1E-5; where, E means *(times by ten raised to the power of *). st.nextToken(); d1 = d1 * Math.pow(10.0,st.nval); } else { st.pushBack(); } initCell(i,j,d1); } } if (DEBUG > 1) {System.out.println(toString());} if (DEBUG > 0) {System.out.println("}");} } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+"in Grid2DSquareCellDoubleFile(asciiFile("+asciiFile+"),gridFile("+gridFile+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+")) {"); } } /** * Constructs from a uk.ac.leeds.ccg.raster.Raster * @param raster - the uk.ac.leeds.ccg.raster.Raster to construct from * @param gridFile - the file for storing the constructed grid data */ public Grid2DSquareCellDoubleFile(Raster raster,File gridFile) { this(raster,gridFile,true); } public Grid2DSquareCellDoubleFile(Raster raster,File gridFile,boolean keepGridStatisticsUpToDate) { if (DEBUG > 0) {System.out.println("Grid2DSquareCellDoubleFile(Raster(raster),gridFile("+gridFile+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+")) {");} AbstractGridStatistics outputGridStatistics; if (keepGridStatisticsUpToDate) { outputGridStatistics = new GridStatistics1_0(this); } else { outputGridStatistics = new GridStatistics1_1(this); } initGridStatistics(outputGridStatistics); int ncols = raster.getWidth(); int nrows = raster.getHeight(); initNrows(nrows); initNcols(ncols); setXllcorner(raster.getOriginx()); setYllcorner(raster.getOriginy()); setCellsize(raster.getCellSize()); initNoDataValue(raster.getMissingValueCode()); //initNoDataValue(raster.getNoDataValue()); setDataDirectory(gridFile.getPath()); setFile(gridFile); try { setRandomAccessFile(new RandomAccessFile(gridFile,"rw")); } catch (java.io.FileNotFoundException fnfe1) { System.out.println(""+fnfe1+" creating RandomAccessFile in Grid2DSquareCellDoubleFile(Raster(raster),gridFile("+gridFile+"),keepGridStatisticsUpToDate("+keepGridStatisticsUpToDate+"))"); System.exit(0); } for (int i = 0; i < nrows; i ++) { for (int j = 0; j < ncols; j ++) { initCell(i,j,raster.getCell(i,j)); } } if (DEBUG > 1) {System.out.println(toString());} if (DEBUG > 0) {System.out.println("}");} } /** * Basic Methods: */ /** * Returns the data type */ public int getDataType() {return 1;} /** * Returns dataDirectory. */ public String getDataDirectory() {return this.dataDirectory;} /** * Sets dataDirectory to the String passed in */ public final void setDataDirectory(String dataDirectory) {this.dataDirectory = dataDirectory;} /** * Returns gridFile. */ public final File getFile() {return this.gridFile;} /** * Sets gridFile. */ private final void setFile(File gridFile) {this.gridFile=gridFile;} /** * Returns gridRandomAccessFile. */ public final RandomAccessFile getRandomAccessFile() {return gridRandomAccessFile;} /** * Sets gridRandomAccessFile. */ private final void setRandomAccessFile(RandomAccessFile gridRandomAccessFile) {this.gridRandomAccessFile=gridRandomAccessFile;} /** * Returns a description of the grid. */ public String toString() { AbstractGridStatistics gridStatistics = getGridStatistics(); return ("File("+getFile()+"),ncols("+getNcols()+"),nrows("+getNrows()+"),xllcorner("+getXllcorner()+"),yllcorner("+getYllcorner()+"),cellsize("+getCellsize()+"),noDataValue("+getNoDataValue()+"),gridStatistics("+gridStatistics.toString()+"))"); } /** * Returns the value at row i, column j else returns noDataValue. */ public double getCell(int i,int j) { if (inGrid(i,j)) { try { RandomAccessFile gridRandomAccessFile = getRandomAccessFile(); gridRandomAccessFile.seek(((i * getNcols()) + j) * 8); //gridRandomAccessFile.seek(getCellID(i,j) * 8); return gridRandomAccessFile.readDouble(); } catch (java.io.IOException ioe1) { if (ioe1 instanceof java.io.FileNotFoundException) { // Otherwise trying to seek to a position or read a value that does not exist so return noDataValue! System.out.println(""+ioe1+" in getCell("+i+","+j+")"); System.exit(0); } } } return getNoDataValue(); } /** * Returns the value at row i column j and sets the value at row i column j to d1. */ public double setCell(int i,int j,double d1) { double noDataValue = getNoDataValue(); if (inGrid(i,j)) { AbstractGridStatistics gridStatistics = getGridStatistics(); boolean keepGridStatisticsUpToDate = false; if (gridStatistics instanceof GridStatistics1_0) {keepGridStatisticsUpToDate = true;} try { RandomAccessFile gridRandomAccessFile = getRandomAccessFile(); int nrows = getNrows(); int ncols = getNcols(); double cellAsProportionOfGrid = (1.0d/(double)(nrows*ncols)); gridRandomAccessFile.seek(((i * ncols) + j) * 8); //gridRandomAccessFile.seek(getCellID(i,j) * 8); if (d1 == noDataValue) {gridStatistics.setSparseness(gridStatistics.getSparseness() + cellAsProportionOfGrid);} double d2 = gridRandomAccessFile.readDouble(); if (d2 == noDataValue) {gridStatistics.setSparseness(gridStatistics.getSparseness() - cellAsProportionOfGrid);} if (keepGridStatisticsUpToDate) { gridStatistics.doMin(d1); if (d2 == gridStatistics.getMin()) {gridStatistics.setMinCount(gridStatistics.getMinCount() - 1);} gridStatistics.doMax(d1); if (d2 == gridStatistics.getMax()) {gridStatistics.setMaxCount(gridStatistics.getMaxCount() - 1);} if (d1 != noDataValue) {gridStatistics.setSum(gridStatistics.getSum() + d1);} if (d2 != noDataValue) {gridStatistics.setSum(gridStatistics.getSum() - d2);} } gridRandomAccessFile.seek(((i * ncols) + j) * 8); //gridRandomAccessFile.seek(getCellID(i,j) * 8); gridRandomAccessFile.writeDouble(d1); return d2; } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" in setCell("+i+","+j+","+d1+")"); ioe1.printStackTrace(); System.exit(0); } } return noDataValue; } /** * Initilises the value at row i column j to d1. */ protected void initCell(int i, int j, double d1) { if (inGrid(i,j)) { AbstractGridStatistics gridStatistics = getGridStatistics(); boolean keepGridStatisticsUpToDate = false; if (gridStatistics instanceof GridStatistics1_0) {keepGridStatisticsUpToDate = true;} try { RandomAccessFile gridRandomAccessFile = getRandomAccessFile(); int ncols = getNcols(); gridRandomAccessFile.seek(((i * ncols) + j) * 8); if (d1 != getNoDataValue()) { gridStatistics.setSparseness(gridStatistics.getSparseness() - (1.0d / (double) (getNrows() * ncols))); if (keepGridStatisticsUpToDate) { gridStatistics.doMin(d1); gridStatistics.doMax(d1); gridStatistics.setSum(gridStatistics.getSum() + d1); } } gridRandomAccessFile.writeDouble(d1); } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" in initCell("+i+","+j+","+d1+")"); System.exit(0); } } } /** * Sets a new value for noDataValue and replaces existing noDataValues with * the numeric value */ public void setNoDataValue(double newNoDataValue) { AbstractGridStatistics gridStatistics = getGridStatistics(); boolean keepGridStatisticsUpToDate = false; if (gridStatistics instanceof GridStatistics1_0) { keepGridStatisticsUpToDate = true; } double noDataValue = getNoDataValue(); if (newNoDataValue != noDataValue) { int ncols = getNcols(); int nrows = getNrows(); double d1 = noDataValue; double cellasProportionOfGrid = 1 / (double) (ncols * nrows); double sparseness = 1.0d; double sum = 0.0d; double min = Double.POSITIVE_INFINITY; int minCount = nrows * ncols; double max = Double.NEGATIVE_INFINITY; int maxCount = minCount; Hashtable hashtable = new Hashtable(); for (int i = 0 ; i < nrows ; i++) { for (int j = 0 ; j < ncols ; j++) { d1 = getCell(i,j); if (d1 == newNoDataValue) { sparseness -= cellasProportionOfGrid; } else { if (keepGridStatisticsUpToDate) { sum += d1; if (d1 < min) {min = d1; minCount = 1;} if (d1 == min) {minCount += 1;} if (d1 > max) {max = d1; maxCount = 1;} if (d1 == max) {maxCount += 1;} } } } } gridStatistics.setSparseness(sparseness); initNoDataValue(newNoDataValue); if (keepGridStatisticsUpToDate) { GridStatistics1_0 gridStats = (GridStatistics1_0) gridStatistics; gridStats.setSum(sum); gridStats.setMinCount(minCount); gridStats.setRecalculateMin(false); gridStats.setMin(min); gridStats.setMaxCount(maxCount); gridStats.setMax(max); gridStats.setRecalculateMax(false); } } } }