r194 - trunk/coser-business/src/main/java/fr/ifremer/coser/services
Author: chatellier Date: 2010-11-09 14:45:50 +0000 (Tue, 09 Nov 2010) New Revision: 194 Log: Ajout du debug du calcul de la densit?\195?\169 (pas fini) Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-09 13:06:36 UTC (rev 193) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-09 14:45:50 UTC (rev 194) @@ -1460,6 +1460,142 @@ * @return density as matrix */ public MatrixND getDensity(Project project, Selection selection) { - return null; + + // map species > "year, all density as double" + Map<String, Map<String, Set<Double>>> densityPerSpeciesAndYear = new HashMap<String, Map<String, Set<Double>>>(); + Set<String> lineNames = new HashSet<String>(); + Set<String> columns = new HashSet<String>(); + + // load map strataname > stratasurface + Map<String, Double> startaAndSurface = new HashMap<String, Double>(); + double totalSourface = 0; + Iterator<String[]> itStrata = selection.getStrata().iterator(); + itStrata.next(); // skip header + while (itStrata.hasNext()) { + String[] tuple = itStrata.next(); + String stataName = tuple[Strata.INDEX_STRATUM]; + String stringSurface = tuple[Strata.INDEX_SURFACE]; + try { + Double surface = Double.valueOf(stringSurface); + totalSourface += surface; + startaAndSurface.put(stataName, surface); + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't convert surface '" + stringSurface + "' as double"); + } + } + } + + // load map traitname > stratename + Map<String, String> haulAndStratas = new HashMap<String, String>(); + Map<String, Double> haulAndSweptSurface = new HashMap<String, Double>(); + Iterator<String[]> itHaul = selection.getHaul().iterator(); + itHaul.next(); // skip header + while (itHaul.hasNext()) { + String[] tuple = itHaul.next(); + String haulKey = tuple[Haul.INDEX_YEAR] + ";" + tuple[Haul.INDEX_HAUL]; + String strataName = tuple[Haul.INDEX_STRATUM]; + haulAndStratas.put(haulKey, strataName); + + String sweptSurfaceAsString = tuple[Haul.INDEX_SWEPT_SURFACE]; + try { + Double sweptSurface = Double.valueOf(sweptSurfaceAsString); + haulAndSweptSurface.put(haulKey, sweptSurface); + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't convert swept surface '" + sweptSurfaceAsString + "' as double"); + } + } + } + + // on conserve egalement les surfaces associé a chaque species/year + // pour post traiement + Map<String, Double> surfacesParStrates = new HashMap<String, Double>(); + + // calcul de la densité + Iterator<String[]> itData = selection.getCatch().iterator(); + itData.next(); // skip header + + while (itData.hasNext()) { + String[] tuple = itData.next(); + + String year = tuple[Catch.INDEX_YEAR]; + String haul = tuple[Catch.INDEX_HAUL]; + String species = tuple[Catch.INDEX_SPECIES]; + String numberAsString = tuple[Catch.INDEX_NUMBER]; + + lineNames.add(species); + columns.add(year); + + // compute density + try { + Double number = Double.valueOf(numberAsString); + Double sweptSurface = haulAndSweptSurface.get(year + ";" + haul); + Double density = number / sweptSurface; + + // count for species and year + Map<String, Set<Double>> specyDensityPerYear = densityPerSpeciesAndYear.get(species); + if (specyDensityPerYear == null) { + specyDensityPerYear = new HashMap<String, Set<Double>>(); + Set<Double> densities = new HashSet<Double>(); + densities.add(density); + specyDensityPerYear.put(year, densities); + densityPerSpeciesAndYear.put(species, specyDensityPerYear); + } + else { + Set<Double> specyYearDensity = specyDensityPerYear.get(year); + if (specyYearDensity == null) { + Set<Double> densities = new HashSet<Double>(); + densities.add(density); + specyDensityPerYear.put(year, densities); + } + else { + specyYearDensity.add(density); + } + } + + // surfaceparstrate + String strata = haulAndStratas.get(haul); + Double surface = startaAndSurface.get(strata); + surfacesParStrates.put(year + ";" + species, surface); + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't convert number '" + numberAsString + "' as double"); + } + } + } + + // convert to matrix + List<String> lineNames2 = new ArrayList<String>(lineNames); + Collections.sort(lineNames2); + List<String> columnsNames2 = new ArrayList<String>(columns); + Collections.sort(columnsNames2); + MatrixND matrix = MatrixFactory.getInstance().create("density", new List<?>[] { + lineNames2 , columnsNames2}); + + for (Map.Entry<String, Map<String, Set<Double>>> dynMatrixEntry : densityPerSpeciesAndYear.entrySet()) { + for (Map.Entry<String, Set<Double>> haulCountForYearEntry : dynMatrixEntry.getValue().entrySet()) { + + // mean density + double totalDensity = 0; + double densityCount = 0; + for (Double singleDensity : haulCountForYearEntry.getValue()) { + totalDensity += singleDensity; + densityCount++; + } + double meanDensity = totalDensity / densityCount; + + // surfaceparstrate + double surfaceparstrate = surfacesParStrates.get(haulCountForYearEntry.getKey() + ";" + dynMatrixEntry.getKey()); + + //TODO echatellier 20101109 finir + //matrix.setValue(dynMatrixEntry.getKey(), haulCountForYearEntry.getKey(), occurence); + } + } + + return matrix; } }
participants (1)
-
chatellier@users.labs.libre-entreprise.org