/** * * Copyright (C) 2005 Andy Turner, CCG, University of Leeds, UK * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package uk.ac.leeds.ccg.andyt.projects.geomorphometrics; import java.io.File; import java.io.IOException; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellAbstract; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellAbstractFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellChunkAbstractFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDouble; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunk64CellMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkAbstractFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkArrayFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkJAIFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkRAFFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunk64CellMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkAbstractFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkJAIFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkRAFFactory; import uk.ac.leeds.ccg.andyt.grids.core.GridStatistics0; import uk.ac.leeds.ccg.andyt.grids.core.GridStatistics1; import uk.ac.leeds.ccg.andyt.grids.core.GridStatisticsAbstract; import uk.ac.leeds.ccg.andyt.grids.exchange.ESRIAsciiGridExporter; import uk.ac.leeds.ccg.andyt.grids.exchange.ESRIAsciiGridImporter; import uk.ac.leeds.ccg.andyt.grids.exchange.ImageExporter; import uk.ac.leeds.ccg.andyt.grids.process.Grid2DSquareCellProcessor; import uk.ac.leeds.ccg.andyt.grids.process.Grid2DSquareCellProcessorDEM; import uk.ac.leeds.ccg.andyt.grids.utilities.FileCreator; import uk.ac.leeds.ccg.andyt.grids.utilities.Utilities; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellInt; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkArrayFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntFactory; public class DataLoader extends Grid2DSquareCellProcessor { private long time; //** Creates a new LoadData */ public DataLoader() { this( FileCreator.createNewFile() ); } /** * Creates a new LoadData using specified workspace. * WARNING: Files in the specified workspace may get overwritten. */ public DataLoader( File workspace ) { super( workspace ); } public static void main( String args[] ) { File workspace = new File( "C:/work/Projects/geomorphometrics/workspace/" ); DataLoader dataLoader = new DataLoader( workspace ); dataLoader.run(); } public void run() { try { //String filename = "catchment1.asc"; //String filename = "geom_rast.asc"; String filename = "crete_w0.asc"; String dataDirectory = "C:/Work/src/andyt/java/projects/geomorphometrics/data/"; String filenameWithoutExtension = filename.substring(0,filename.length() - 4); time = System.currentTimeMillis(); boolean handleOutOfMemoryError = true; int chunkNrows = 512; //64, 128, 256, 512, 1024, 2048 int chunkNcols = 512; //64, 128, 256, 512, 1024, 2048 //this.grid2DSquareCellDoubleFactory.setChunkNrows( chunkNrows ); //this.grid2DSquareCellDoubleFactory.setChunkNcols( chunkNcols ); //this.grid2DSquareCellDoubleFactory.setHandleOutOfMemoryError( handleOutOfMemoryError ); this.grid2DSquareCellIntFactory.setChunkNrows( chunkNrows ); this.grid2DSquareCellIntFactory.setChunkNcols( chunkNcols ); this.grid2DSquareCellIntFactory.setHandleOutOfMemoryError( handleOutOfMemoryError ); Grid2DSquareCellInt grid2DSquareCellInt = null; Grid2DSquareCellDouble grid2DSquareCellDouble = null; boolean load1 = true; if ( load1 ) { File file = new File( dataDirectory + filename ); grid2DSquareCellInt = ( Grid2DSquareCellInt ) grid2DSquareCellIntFactory.create( file ); boolean swapToFileCache = true; grid2DSquareCellInt.writeToFile( swapToFileCache, handleOutOfMemoryError ); } boolean load1double = true; if ( load1double ) { File file = new File( dataDirectory + filename ); grid2DSquareCellDouble = ( Grid2DSquareCellDouble ) grid2DSquareCellDoubleFactory.create( file ); boolean swapToFileCache = true; grid2DSquareCellDouble.writeToFile( swapToFileCache, handleOutOfMemoryError ); } // boolean load2 = true; // if ( load2 ) { // String directoryName = new String( // dataDirectory + "/" + filenameWithoutExtension + // grid2DSquareCellIntFactory.getClass().getName() + // "_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); // File directory = new File( directoryName ); // this.grid2DSquareCellIntFactory.setDirectory( directory ); // grid2DSquareCellInt = ( Grid2DSquareCellInt ) this.grid2DSquareCellIntFactory.create(); // } // boolean write1 = true; // if ( write1 ) { // System.out.println( grid2DSquareCellInt.toString( handleOutOfMemoryError ) ); // new ESRIAsciiGridExporter().toAsciiFile( // grid2DSquareCellInt, // handleOutOfMemoryError ); // String type = "PNG"; // //File file = new File ( this.getWorkspace( handleOutOfMemoryError ), filenameWithoutExtension + "." + type ); // File file = new File ( dataDirectory, filenameWithoutExtension + "." + type ); // new ImageExporter().toGreyScaleImage( // grid2DSquareCellInt, // file, // type, // handleOutOfMemoryError ); // } boolean write1double = true; if ( write1double ) { System.out.println( grid2DSquareCellDouble.toString( handleOutOfMemoryError ) ); new ESRIAsciiGridExporter().toAsciiFile( grid2DSquareCellDouble, handleOutOfMemoryError ); String type = "PNG"; //File file = new File ( this.getWorkspace( handleOutOfMemoryError ), filenameWithoutExtension + "." + type ); File file = new File ( dataDirectory, filenameWithoutExtension + "." + type ); new ImageExporter().toGreyScaleImage( grid2DSquareCellDouble, file, type, handleOutOfMemoryError ); } System.out.println("Time taken " + Utilities.time( System.currentTimeMillis() - time ) ); } catch ( Error e ) { e.printStackTrace(); } catch ( Exception e ) { e.printStackTrace(); } } // private void loadGSHHSMask( GridStatisticsAbstract gridStatistics, Grid2DSquareCellChunkAbstractFactory grid2DSquareCellChunkFactory, int chunkNrows, int chunkNcols, boolean handleOutOfMemoryError ) { // //for ( int row = 0; row < 5; row ++ ) { // int row = 0; // for ( int col = 3; col < 9; col ++ ) { // File eSRIAsciigridFile = new File( "C:/cygwin/home/geoagdt/gshhs/gshhs_1.3/mask/Ascii/m_" + row + "_" + col + ".asc" ); // File outputDirectory = new File( "C:/cygwin/home/geoagdt/gshhs/gshhs_1.3/mask/Grid2DSquareCellInt/m_" + row + "_" + col + "_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); // //File outputDirectory = new File( "C:/Work/Projects/DesertLinks/Data/gshhs/Grid2DSquareCellInt/m_" + row + "_" + col + "_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); // ESRIAsciiGridImporter eSRIAsciigridImporter = new ESRIAsciiGridImporter( eSRIAsciigridFile ); // Grid2DSquareCellInt mask = eSRIAsciigridImporter.constructGrid2DSquareCellInt( gridStatistics, outputDirectory, ( Grid2DSquareCellIntChunkAbstractFactory ) grid2DSquareCellChunkFactory, chunkNrows, chunkNcols ); // System.out.println( mask.toString( handleOutOfMemoryErrorFalse ) ); // // Cache // mask.writeToFile( true, false ); // mask = null; // eSRIAsciigridFile = null; // outputDirectory = null; // eSRIAsciigridImporter = null; // System.gc(); // } // row = 3; // for ( int col = 4; col < 6; col ++ ) { // File eSRIAsciigridFile = new File( "C:/cygwin/home/geoagdt/gshhs/gshhs_1.3/mask/Ascii/m_" + row + "_" + col + ".asc" ); // File outputDirectory = new File( "C:/cygwin/home/geoagdt/gshhs/gshhs_1.3/mask/Grid2DSquareCellInt/m_" + row + "_" + col + "_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); // //File outputDirectory = new File( "C:/Work/Projects/DesertLinks/Data/gshhs/Grid2DSquareCellInt/m_" + row + "_" + col + "_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); // ESRIAsciiGridImporter eSRIAsciigridImporter = new ESRIAsciiGridImporter( eSRIAsciigridFile ); // Grid2DSquareCellInt mask = eSRIAsciigridImporter.constructGrid2DSquareCellInt( new GridStatistics1(), outputDirectory, ( Grid2DSquareCellIntChunkAbstractFactory ) grid2DSquareCellChunkFactory, chunkNrows, chunkNcols ); // System.out.println( mask.toString( handleOutOfMemoryErrorFalse ) ); // // Cache // mask.writeToFile( true, false ); // mask = null; // System.gc(); // } // } }