Commit a686b60e authored by patrick.lambert's avatar patrick.lambert
Browse files

simplication of DiadromousFishGroup

parent 954173e9
......@@ -27,74 +27,135 @@ import java.io.IOException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import environment.RiverBasin;
import environment.Time;
import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
import fr.cemagref.simaqualife.pilot.Pilot;
import miscellaneous.TreeMapForCentile;
import species.DiadromousFish;
import species.DiadromousFishGroup;
/**
*
*/
@Deprecated
public class ExportSpeciesRange extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
private String fileNameOutput = "range";
/**
* centile to calculate the nothernmost range of species distribution
*
* @unit
*/
public double centileForRange = 0.95;
private transient BufferedWriter bW;
private transient String sep=";";
private transient String sep = ";";
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver()))
.toXML(new ExportSpeciesRange()));
System.out.println((new XStream(new DomDriver())).toXML(new ExportSpeciesRange()));
}
/* (non-Javadoc)
* @see fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess#initTransientParameters(fr.cemagref.simaqualife.pilot.Pilot)
/*
* (non-Javadoc)
*
* @see
* fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess#initTransientParameters(fr.cemagref.simaqualife.
* pilot.Pilot)
*/
@Override
public void initTransientParameters(Pilot pilot) {
super.initTransientParameters(pilot);
sep=";";
sep = ";";
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see fr.cemagref.simaqualife.kernel.processes.Process#doProcess(java.lang.Object)
*/
@Override
public void doProcess(DiadromousFishGroup group) {
if (bW==null){
if (fileNameOutput != null){
new File(group.getOutputPath()+fileNameOutput).getParentFile().mkdirs();
if (bW == null) {
if (fileNameOutput != null) {
new File(group.getOutputPath() + fileNameOutput).getParentFile().mkdirs();
try {
bW = new BufferedWriter(new FileWriter(new File(group.getOutputPath()+
fileNameOutput +group.getSimulationId()+ ".csv")));
bW = new BufferedWriter(
new FileWriter(new File(group.getOutputPath() + fileNameOutput + group.getSimulationId() + ".csv")));
bW.write("timestep"+sep+"year"+sep+"season"+sep+"medianRange"
+sep+"southRange"+sep+"northRange"+sep+"centileRange"+"\n");
bW.write("timestep" + sep + "year" + sep + "season" + sep + "medianRange" + sep + "southRange" + sep
+ "northRange" + sep + "centileRange" + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
Time time = group.getEnvironment().getTime();
bW.write(group.getPilot().getCurrentTime()+sep+time.getYear(group.getPilot()));
bW.write(sep+time.getSeason(group.getPilot()));
for (double value : group.getRangeDistributionWithLat()){
bW.write(sep+value);
bW.write(group.getPilot().getCurrentTime() + sep + time.getYear(group.getPilot()));
bW.write(sep + time.getSeason(group.getPilot()));
for (double value : getRangeSpawnerDistributionWithLat(group)) {
bW.write(sep + value);
}
bW.write("\n");
if (group.getPilot().getCurrentTime()== group.getPilot().getSimBegin()+group.getPilot().getSimDuration()-1){
if (group.getPilot().getCurrentTime() == group.getPilot().getSimBegin() + group.getPilot().getSimDuration() - 1) {
bW.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private Double[] getRangeSpawnerDistributionWithLat(DiadromousFishGroup group) {
TreeMapForCentile latitudeEffective = new TreeMapForCentile();
double southernLatitude = 90.;
double northernLatitude = 0.;
// loop over riverBasin
for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) {
long effective = (long) (riverBasin.getLastFemaleSpawnerNumber() + riverBasin.getLastMaleSpawnerNumber());
if (effective > 0) {
latitudeEffective.putWithAdding(riverBasin.getLatitude(), effective);
if (riverBasin.getLatitude() < southernLatitude)
southernLatitude = riverBasin.getLatitude();
if (riverBasin.getLatitude() > northernLatitude)
northernLatitude = riverBasin.getLatitude();
}
}
// compute result
// (y, y-low, y-high) data item
Double[] rangeDistribution = new Double[4];
if (!latitudeEffective.isEmpty()) {
// median
rangeDistribution[0] = latitudeEffective.calculateMedian();
// southern
rangeDistribution[1] = southernLatitude;
// northern
rangeDistribution[2] = northernLatitude;
// centile
rangeDistribution[3] = latitudeEffective.calculateCentile(centileForRange);
} else {
// median
rangeDistribution[0] = (southernLatitude + northernLatitude) / 2.;
// southern
rangeDistribution[1] = rangeDistribution[0];
// northern
rangeDistribution[2] = rangeDistribution[0];
// centile
rangeDistribution[3] = rangeDistribution[0];
}
return rangeDistribution;
}
}
......@@ -14,8 +14,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.openide.util.lookup.ServiceProvider;
......@@ -31,9 +29,7 @@ import fr.cemagref.simaqualife.kernel.AquaNismsGroup;
import fr.cemagref.simaqualife.kernel.Processes;
import fr.cemagref.simaqualife.pilot.Pilot;
import miscellaneous.Duo;
import miscellaneous.TreeMapForCentile;
import species.DiadromousFish.Gender;
import species.DiadromousFish.Stage;
/**
*
......@@ -58,15 +54,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
*/
private NutrientRoutine nutrientRoutine;
public String fileNameInputForInitialObservation = "data/input/reality/Obs1900.csv";
/**
* centile to calcucale the range of species distribution
*
* @unit
*/
public double centileForRange = 0.95;
/**
* file with the calibated parameters (from baysian approach)
*
......@@ -179,23 +166,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
*/
private transient List<Duo<Double, Double>> parameterSets;
// =================================================
// calibration
// =================================================
@Deprecated
private Double targetedAgeForFemaleSpawnerForFirstTime = 5.5;
@Deprecated
private Double targetedAgeForMaleSpawnerForFirstTime = 4.5;
/**
* map of observedoccurence in 1900
*
* @unit
*/
@Deprecated
private transient Map<String, Integer> obs1900;
public static void main(String[] args) {
DiadromousFishGroup diadromousFishGroup = new DiadromousFishGroup(new Pilot(), null, null);
......@@ -479,44 +449,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
e.printStackTrace();
}
}
// ------------------------------------------------------------------
// read occurence data file
// -----------------------------------------------------------
FileReader reader;
Scanner scanner;
obs1900 = new HashMap<String, Integer>();
if (!fileNameInputForInitialObservation.isEmpty()) {
try {
reader = new FileReader(fileNameInputForInitialObservation);
// Parsing the file
scanner = new Scanner(reader);
scanner.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!!
scanner.useDelimiter(Pattern.compile("[;\r]"));
scanner.nextLine(); // to skip the file first line of headers
while (scanner.hasNextLine()) {
String[] fields = scanner.nextLine().split(",");
// System.out.println(Arrays.toString(fields));
if (fields.length > 2) {
if (!fields[2].isEmpty())
obs1900.put(fields[1], Integer.valueOf(fields[2]));
}
}
reader.close();
scanner.close();
} catch (IOException ex) {
Logger.getLogger(DiadromousFishGroup.class.getName()).log(Level.SEVERE, null, ex);
}
}
// defaut values
if (targetedAgeForFemaleSpawnerForFirstTime == null)
targetedAgeForFemaleSpawnerForFirstTime = 5.5;
if (targetedAgeForMaleSpawnerForFirstTime == null)
targetedAgeForMaleSpawnerForFirstTime = 4.5;
// this.nutrientRoutine.getNutrientFluxesCollection().setBasinNames(this.getEnvironment().getRiverBasinNames());
this.nutrientRoutine.createNutrienFluxesCollections(this.getEnvironment().getRiverBasinNames());
......@@ -614,6 +546,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@Deprecated
public double getStandardDeviationOfMatureFishLength() {
double standardDeviationOfMatureFishLength = 0.;
double sumOfSquareLength = 0.;
......@@ -692,264 +625,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
// ================================================================
// statictis for calibration
// ================================================================
@Deprecated
public double computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) {
double sum = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() > 0.) {
double val = riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() - TARGET;
sum += val * val;
}
}
// System.out.println("sum female: " + sum);
return sum;
}
@Deprecated
@Observable(description = "Female spawners For First Time Summary Statistic")
public double computeFemaleSpawnerForFirstTimeSummaryStatistic() {
return computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime);
}
@Deprecated
@Observable(description = "mean age at first reproduction for female")
public double getMeanAgeOfFirstReprodutionForFemale() {
double sum = 0;
double nb = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() > 0.) {
nb++;
sum += riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero();
}
}
return sum / nb;
}
@Deprecated
@Observable(description = "mean length of first reprodution for female")
public double getMeanLengthOfFirstReprodutionForFemale() {
double sum = 0;
double nb = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.FEMALE).getMeanWithoutZero() > 0.) {
nb++;
sum += riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.FEMALE).getMeanWithoutZero();
}
}
return sum / nb;
}
@Deprecated
public double computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) {
double sum = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero() > 0.) {
double val = riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero() - TARGET;
sum += val * val;
}
}
// System.out.println("sum male: " + sum);
return sum;
}
@Deprecated
@Observable(description = "Male spawners For First Time Summary Statistic")
public double computeMaleSpawnerForFirstTimeSummaryStatistic() {
return computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime);
}
@Deprecated
@Observable(description = "mean age of first reprodution for male")
public double getMeanAgeOfFirstReprodutionForMale() {
double sum = 0;
double nb = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero() > 0.) {
nb++;
sum += riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero();
}
}
return sum / nb;
}
@Observable(description = "mean length of first reprodution for male")
public double getMeanLengthOfFirstReprodutionForMale() {
double sum = 0;
double nb = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
if (riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.MALE).getMeanWithoutZero() > 0.) {
nb++;
sum += riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.MALE).getMeanWithoutZero();
}
}
return sum / nb;
}
@Deprecated
@Observable(description = "Likelihood Summary stat")
public double computeLikelihood() {
int obsVal;
double sumLogWherePres = 0.;
double sumLogWhereAbs = 0.;
final double[] probOfNonNulRecruitmentDuringLastYears = getEnvironment().getProbOfNonNulRecruitmentDuringLastYears();
final String[] finalStatesNames = getEnvironment().getRiverBasinNames();
for (int i = 0; i < finalStatesNames.length; i++) {
if (obs1900.containsKey(finalStatesNames[i])) {
obsVal = obs1900.get(finalStatesNames[i]);
if (obsVal == 0) {
sumLogWhereAbs += Math.log(1 - probOfNonNulRecruitmentDuringLastYears[i]);
} else {
sumLogWherePres += Math.log(probOfNonNulRecruitmentDuringLastYears[i]);
}
}
}
return sumLogWhereAbs + sumLogWherePres;
}
// ========================================================
// observer to explore fish distribution
// ========================================================
@Deprecated
@Observable(description = "Higher Populated Latitude")
public double getHigherPopulatedLatitude() {
double latitude = 0.0;
RiverBasin[] basins = getEnvironment().getRiverBasins();
int[] finalStates = getEnvironment().getFinalStates();
for (int i = 0; i < finalStates.length; i++) {
if ((finalStates[i] == 1) && (basins[i].getLatitude() > latitude)) {
latitude = basins[i].getLatitude();
}
}
return latitude;
}
@Deprecated
@Observable(description = "Number of colonized basins")
public double getNbColonizedBasins() {
int nb = 0;
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
nb++;
}
}
}
return nb;
}
@Deprecated
@Observable(description = "Northern colonized basins")
public double getNorthernBasins() {
int northernBasin = Integer.MAX_VALUE;
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
northernBasin = Math.min(northernBasin, getEnvironment().getAssociatedRiverBasin(seaBasin).getId());
}
}
}
return northernBasin;
}
@Deprecated
@Observable(description = "Southern colonized basins")
public double getSouthernBasins() {
int southernBasin = Integer.MIN_VALUE;
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
southernBasin = Math.max(southernBasin, getEnvironment().getAssociatedRiverBasin(seaBasin).getId());
}
}
}
return southernBasin;
}
@Deprecated
@Observable(description = "Range distribution with latitude")
public Double[] getRangeDistributionWithLat() {
// TODO keep the extreme latitudes from the catchment
double southernBasin = 35.;
double northernBasin = 60.;
RiverBasin riverBasin;
TreeMapForCentile latitudeEffective = new TreeMapForCentile();
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
riverBasin = getEnvironment().getAssociatedRiverBasin(seaBasin);
long effective = 0;
for (DiadromousFish fish : seaBasin.getFishs(this)) {
effective += fish.getAmount();
}
southernBasin = Math.max(southernBasin, riverBasin.getLatitude());
latitudeEffective.putWithAdding(riverBasin.getLatitude(), effective);
northernBasin = Math.min(northernBasin, riverBasin.getLatitude());
}
}
}
Double[] rangeDistribution = new Double[4];
rangeDistribution[0] = (latitudeEffective.isEmpty() ? (southernBasin + northernBasin) / 2.
: latitudeEffective.calculateMedian());
rangeDistribution[1] = Math.min(southernBasin, northernBasin);
rangeDistribution[2] = Math.max(southernBasin, northernBasin);
rangeDistribution[3] = latitudeEffective.calculateCentile(centileForRange);
return rangeDistribution;
}
@Deprecated
@Observable(description = "Range distribution")
public double[] getRangeDistribution() {
double southernBasin = 0;
double nbBasin = getEnvironment().getNbRiverBasin();
double northernBasin = nbBasin;
Basin riverBasin;
TreeMapForCentile latitudeEffective = new TreeMapForCentile();
for (Basin seaBasin : getEnvironment().getSeaBasins()) {
if (seaBasin.getFishs(this) != null) {
if (!seaBasin.getFishs(this).isEmpty()) {
riverBasin = getEnvironment().getAssociatedRiverBasin(seaBasin);
long effective = 0;
for (DiadromousFish fish : seaBasin.getFishs(this)) {
effective += fish.getAmount();
}
latitudeEffective.putWithAdding(riverBasin.getId(), effective);
southernBasin = Math.max(southernBasin, riverBasin.getId());
northernBasin = Math.min(northernBasin, riverBasin.getId());
}
}
}
southernBasin = nbBasin - southernBasin;
northernBasin = nbBasin - northernBasin;
double[] rangeDistribution = new double[3];
rangeDistribution[0] = (latitudeEffective.isEmpty() ? (southernBasin + northernBasin) / 2.
: nbBasin - latitudeEffective.calculateMedian());
rangeDistribution[1] = Math.min(southernBasin, northernBasin);
rangeDistribution[2] = Math.max(southernBasin, northernBasin);
return rangeDistribution;
}
// ========================================================
// observer to explore fish abundance
// ========================================================
......@@ -970,63 +645,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
/**
* @return sum of spawner effectives in all the river basins
*/
@Observable(description = "Number of spawners in river basins")
@Deprecated
public double getSpawnerEffective() {
long eff = 0;
for (RiverBasin basin : this.getEnvironment().getRiverBasins()) {
if (basin.getFishs(this) != null) {
for (DiadromousFish fish : basin.getFishs(this)) {
if (fish.getStage() == Stage.MATURE)
eff += fish.getAmount();
}
}
}
return eff;
}
/**
* @return sum of male spawner effectives in all basins
*/
@Observable(description = "Number of male spawners in all basins")
@Deprecated
public double getMaleSpawnerEffective() {
long eff = 0;
for (Basin basin : this.getEnvironment().getBasins()) {
if (basin.getFishs(this) != null) {
for (DiadromousFish fish : basin.getFishs(this)) {
if (fish.getStage() == Stage.MATURE && fish.getGender() == Gender.MALE)
eff += fish.getAmount();
}
}
}
return eff;
}
/**
* @return sum of female spawner effectives in all basins
*/
@Observable(description = "Number of female spawners in all basins")
@Deprecated
public double getFemaleSpawnerEffective() {
long eff = 0;
for (Basin basin : this.getEnvironment().getBasins()) {
if (basin.getFishs(this) != null) {
for (DiadromousFish fish : basin.getFishs(this)) {
if (fish.getStage() == Stage.MATURE && fish.getGender() == Gender.FEMALE)
eff += fish.getAmount();
}
}
}
return eff;
}