From: <de...@us...> - 2012-12-20 10:14:58
|
Revision: 8158 http://fudaa.svn.sourceforge.net/fudaa/?rev=8158&view=rev Author: deniger Date: 2012-12-20 10:14:50 +0000 (Thu, 20 Dec 2012) Log Message: ----------- Modified Paths: -------------- trunk/framework/ctulu-gis/src/main/java/org/fudaa/ctulu/gis/GisZoneCollectionAsListPointAdapter.java Added Paths: ----------- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/ trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java Modified: trunk/framework/ctulu-gis/src/main/java/org/fudaa/ctulu/gis/GisZoneCollectionAsListPointAdapter.java =================================================================== --- trunk/framework/ctulu-gis/src/main/java/org/fudaa/ctulu/gis/GisZoneCollectionAsListPointAdapter.java 2012-12-19 16:33:55 UTC (rev 8157) +++ trunk/framework/ctulu-gis/src/main/java/org/fudaa/ctulu/gis/GisZoneCollectionAsListPointAdapter.java 2012-12-20 10:14:50 UTC (rev 8158) @@ -5,13 +5,14 @@ import com.vividsolutions.jts.geom.Coordinate; import org.fudaa.ctulu.CtuluLibArray; +import org.fudaa.ctulu.interpolation.SupportLocationI; /** * Adapte une collection en un fournisseur de points. * * @author Frederic Deniger */ -public class GisZoneCollectionAsListPointAdapter { +public class GisZoneCollectionAsListPointAdapter implements SupportLocationI { private final GISZoneCollection points; private final int[] nbPointsBySemis; @@ -26,7 +27,8 @@ nbPts = CtuluLibArray.getSum(nbPointsBySemis); } - public int getNbPoints() { + @Override + public int getPtsNb() { return nbPts; } @@ -53,11 +55,13 @@ return null; } - public double getX(int idxPt) { + @Override + public double getPtX(int idxPt) { return getCoordinate(idxPt).x; } - public double getY(int idxPt) { + @Override + public double getPtY(int idxPt) { return getCoordinate(idxPt).y; } } Copied: trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java (from rev 7771, trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoDoubleArrayBinaryFileSaver.java) =================================================================== --- trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java (rev 0) +++ trunk/framework/dodico-common/src/main/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaver.java 2012-12-20 10:14:50 UTC (rev 8158) @@ -0,0 +1,189 @@ +package org.fudaa.dodico.fortran; + +import com.memoire.fu.FuEmptyArrays; +import java.io.File; +import java.io.FileOutputStream; + +import org.fudaa.ctulu.fileformat.FortranLib; +import org.fudaa.dodico.fichiers.NativeBinarySystem; + +import com.memoire.fu.FuLog; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import org.fudaa.ctulu.CtuluLibArray; +import org.fudaa.ctulu.CtuluLibFile; + +/** + * Permet d'\xE9crire facilement des tableaux d'entier, bool\xE9ens dans un fichier binaire. + * @author Frederic Deniger + */ +public class DodicoIntegerArrayBinaryFileSaver { + + public DodicoIntegerArrayBinaryFileSaver() { + } + + public boolean saveSimpleArray(File file, int[] in) { + int[][] toSave = new int[1][]; + toSave[0] = in; + return save(file, toSave); + } + + public boolean saveSimpleArray(OutputStream file, int[] in) { + int[][] toSave = new int[1][]; + toSave[0] = in; + return save(file, toSave); + } + + public int[] loadSimpleArray(File file) { + int[][] res = load(file); + return CtuluLibArray.isEmpty(res) ? FuEmptyArrays.INT0 : res[0]; + } + + public int[] loadSimpleArray(InputStream file) { + int[][] res = load(file); + return CtuluLibArray.isEmpty(res) ? FuEmptyArrays.INT0 : res[0]; + } + + private static int[] toInt(boolean[] in) { + if (in == null) { + return FuEmptyArrays.INT0; + } + int[] out = new int[in.length]; + for (int i = 0; i < out.length; i++) { + out[i] = in[i] ? 1 : 0; + } + return out; + } + + private static boolean[] toBoolean(int[][] in) { + if (in == null || in.length == 0 || in[0].length == 0) { + return FuEmptyArrays.BOOLEAN0; + } + final int[] values = in[0]; + boolean[] out = new boolean[values.length]; + for (int i = 0; i < out.length; i++) { + out[i] = values[i] == 1 ? true : false; + } + return out; + } + + public boolean saveBoolean(File file, boolean[] in) { + int[][] toSave = new int[1][]; + toSave[0] = toInt(in); + return save(file, toSave); + } + + public boolean saveBoolean(OutputStream file, boolean[] in) { + int[][] toSave = new int[1][]; + toSave[0] = toInt(in); + return save(file, toSave); + } + + public boolean[] loadBoolean(File file) { + int[][] res = load(file); + return toBoolean(res); + } + + public boolean[] loadBoolean(InputStream file) { + int[][] res = load(file); + return toBoolean(res); + } + + public int[][] load(File file) { + FileInputStream input = null; + int[][] res = null; + try { + input = new FileInputStream(file); + res = load(input); + } catch (Exception e) { + FuLog.error(e); + } finally { + CtuluLibFile.close(input); + } + return res == null ? new int[0][0] : res; + } + + public int[][] load(InputStream in) { + FortranBinaryInputStream input = null; + if (in == null) { + return new int[0][0]; + } + boolean ok = true; + int[][] mapOfValues = null; + try { + try { + input = new FortranBinaryInputStream(in, true, NativeBinarySystem.X86); + input.readRecord(); + int nbValues = input.readInteger(); + if (nbValues >= 0) { + mapOfValues = new int[nbValues][]; + for (int i = 0; i < nbValues; i++) { + input.readRecord(); + int nbItem = input.readInteger(); + if (nbItem < 0) { + ok = false; + break; + } + mapOfValues[i] = new int[nbItem]; + for (int j = 0; j < nbItem; j++) { + mapOfValues[i][j] = input.readInteger(); + } + } + } else { + ok = false; + } + } catch (Exception e) { + FuLog.error(e); + } + + } catch (Exception e) { + FuLog.error(e); + } + if (ok) { + return mapOfValues; + } + return new int[0][0]; + } + + public boolean save(File file, int[][] mapOfValues) { + boolean ok = false; + FileOutputStream out = null; + try { + out = new FileOutputStream(file); + ok = save(out, mapOfValues); + } catch (Exception exception) { + ok = false; + FuLog.error(exception); + } finally { + CtuluLibFile.close(out); + } + return ok; + } + + public boolean save(OutputStream out, int[][] mapOfValues) { + FortranBinaryOutputStream output = null; + boolean ok = true; + try { + output = new FortranBinaryOutputStream(out, true, NativeBinarySystem.X86); + int nbValues = mapOfValues.length; + output.writeInteger(nbValues); + output.writeRecord(); + for (int i = 0; i < nbValues; i++) { + int[] values = mapOfValues[i]; + output.writeInteger(values.length); + for (int j = 0; j < values.length; j++) { + output.writeInteger(values[j]); + } + output.writeRecord(); + } + } catch (Exception e) { + ok = false; + FuLog.error(e); + } finally { + FortranLib.close(output); + } + return ok; + + } +} Added: trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java =================================================================== --- trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java (rev 0) +++ trunk/framework/dodico-common/src/test/java/org/fudaa/dodico/fortran/DodicoIntegerArrayBinaryFileSaverTest.java 2012-12-20 10:14:50 UTC (rev 8158) @@ -0,0 +1,70 @@ +/* + GPL 2 + */ +package org.fudaa.dodico.fortran; + +import java.io.File; +import org.fudaa.dodico.common.TestIO; + +/** + * + * @author Frederic Deniger + */ +public class DodicoIntegerArrayBinaryFileSaverTest extends TestIO { + + public DodicoIntegerArrayBinaryFileSaverTest() { + super(null); + } + + public void testReadWriteIntArrayArray() { + int[][] values = new int[10][]; + int k = 5; + for (int i = 0; i < values.length; i++) { + values[i] = new int[k++]; + for (int j = 0; j < values[i].length; j++) { + values[i][j] = i * j; + } + } + File tempFile = createTempFile(); + DodicoIntegerArrayBinaryFileSaver saver = new DodicoIntegerArrayBinaryFileSaver(); + saver.save(tempFile, values); + int[][] load = saver.load(tempFile); + assertEquals(values.length, load.length); + for (int i = 0; i < values.length; i++) { + assertEquals(values[i].length, load[i].length); + for (int j = 0; j < values[i].length; j++) { + assertEquals(values[i][j], load[i][j]); + } + } + } + + public void testReadWriteIntArray() { + int[] values = new int[10]; + for (int i = 0; i < values.length; i++) { + values[i] = i * 2; + } + File tempFile = createTempFile(); + DodicoIntegerArrayBinaryFileSaver saver = new DodicoIntegerArrayBinaryFileSaver(); + saver.saveSimpleArray(tempFile, values); + int[] load = saver.loadSimpleArray(tempFile); + assertEquals(values.length, load.length); + for (int i = 0; i < values.length; i++) { + assertEquals(values[i], load[i]); + } + } + + public void testReadWriteBooleanArray() { + boolean[] values = new boolean[11]; + for (int i = 0; i < values.length; i++) { + values[i] = i % 2 == 0; + } + File tempFile = createTempFile(); + DodicoIntegerArrayBinaryFileSaver saver = new DodicoIntegerArrayBinaryFileSaver(); + saver.saveBoolean(tempFile, values); + boolean[] load = saver.loadBoolean(tempFile); + assertEquals(values.length, load.length); + for (int i = 0; i < values.length; i++) { + assertEquals(values[i], load[i]); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |