Commit 03b203c3 authored by Lambert Patrick's avatar Lambert Patrick
Browse files

write biomass and effective fluxes

parent 55dd75d1
......@@ -214,7 +214,9 @@
<processesEachStep>
<environment.InformTime>
<synchronisationMode>ASYNCHRONOUS</synchronisationMode>
<consoleDisplay>true</consoleDisplay>
</environment.InformTime>
<species.PlopProcess>
<synchronisationMode>ASYNCHRONOUS</synchronisationMode>
<temporisation>0</temporisation>
......@@ -273,18 +275,12 @@
<mortalityRateInOffshore>0.4</mortalityRateInOffshore>
</species.Survive>
<species.WriteEffectivesFluxes>
<species.WriteEffectiveAndBiomassFluxes>
<synchronisationMode>ASYNCHRONOUS</synchronisationMode>
<exportSeason>SPRING</exportSeason>
<fileNameOutput>effectiveFluxes</fileNameOutput>
</species.WriteEffectivesFluxes>
<species.WriteBiomassFluxes>
<synchronisationMode>ASYNCHRONOUS</synchronisationMode>
<exportSeason>SPRING</exportSeason>
<fileNameOutput>BiomassFluxessBeforeReproduction</fileNameOutput>
</species.WriteBiomassFluxes>
<fileNameOutput>effectiveBiomassFluxesBeforeReproduction</fileNameOutput>
</species.WriteEffectiveAndBiomassFluxes>
<species.ReproduceAndSurviveAfterReproductionWithDiagnose>
<synchronisationMode>ASYNCHRONOUS</synchronisationMode>
<reproductionSeason>SPRING</reproductionSeason>
......
......@@ -116,11 +116,11 @@ public class NutrientRoutine {
private Map<String,Double> compoJuvenile;
private transient NutrientFluxesCollection nutrientFluxesCollection;
private transient NutrientImportFluxesCollection nutrientImportFluxesCollection;
public NutrientRoutine() {
this.nutrientFluxesCollection = new NutrientFluxesCollection();
this.nutrientImportFluxesCollection = new NutrientImportFluxesCollection();
}
/**
......@@ -155,17 +155,17 @@ public class NutrientRoutine {
this.juvenileFeatures = juvenileFeatures;
this.compoJuvenile = compoJuvenile;
this.nutrientFluxesCollection = new NutrientFluxesCollection();
this.nutrientImportFluxesCollection = new NutrientImportFluxesCollection();
}
public void createNutrienFluxesCollection(String[] basinNames) {
nutrientFluxesCollection = new NutrientFluxesCollection();
nutrientFluxesCollection.setBasinNames(basinNames);
nutrientImportFluxesCollection = new NutrientImportFluxesCollection();
nutrientImportFluxesCollection.setBasinNames(basinNames);
}
public NutrientFluxesCollection getNutrientFluxesCollection() {
return nutrientFluxesCollection;
public NutrientImportFluxesCollection getNutrientFluxesCollection() {
return nutrientImportFluxesCollection;
}
/**
......@@ -546,9 +546,10 @@ public class NutrientRoutine {
}
public class NutrientFluxesCollection {
public class NutrientImportFluxesCollection {
/*
* collection of import fluxes (from sea to rivers)
* <key> Year
* <value>
* <key> Nutrient
......@@ -558,21 +559,37 @@ public class NutrientRoutine {
* <key> destination basin name
* <value> flux
*/
//
private Map<Long, Map <String, Map<String, Map<String, Double>>>> fluxesCollection ;
private Map<Long, Map <String, Map<String, Map<String, Double>>>> importFluxesCollection ;
/*
* collection of export fluxes (from river to sea)
* <key> Year
* <value>
* <key> Nutrient
* <value>
* <key> origin basin name
* <value> flux
*/
private Map<Long, Map <String, Map<String, Double>>> exportFluxesCollection ;
private transient String[] basinNames;
NutrientFluxesCollection() {
NutrientImportFluxesCollection() {
fluxesCollection = new HashMap <Long, Map <String, Map<String, Map<String, Double>>>> ();
importFluxesCollection = new HashMap <Long, Map <String, Map<String, Map<String, Double>>>> ();
exportFluxesCollection = new HashMap <Long, Map <String, Map<String, Double>>> ();
}
public Map<Long, Map<String, Map<String, Map<String, Double>>>> getFluxesCollection() {
return fluxesCollection;
public Map<Long, Map<String, Map<String, Map<String, Double>>>> getImportFluxesCollection() {
return importFluxesCollection;
}
public Map<Long, Map<String, Map<String, Double>>> getExportFluxesCollection() {
return exportFluxesCollection;
}
public void setBasinNames(String[] basinNames) {
this.basinNames = basinNames;
}
......@@ -580,7 +597,7 @@ public class NutrientRoutine {
public void put(Long year,String nutrient, String originBasinName, String destinationBasinName, Double flux ) {
if(!fluxesCollection.containsKey(year)) {
if(!importFluxesCollection.containsKey(year)) {
Map <String, Map <String, Map <String, Double>>> nutrientsMap = new TreeMap <String, Map <String, Map <String, Double>>>();
......@@ -600,15 +617,69 @@ public class NutrientRoutine {
nutrientsMap.put(nutrientOfInterest, originsMap);
}
fluxesCollection.put(year,nutrientsMap);
importFluxesCollection.put(year,nutrientsMap);
}
double previousFlux = fluxesCollection.get(year).get(nutrient).get(originBasinName).get(destinationBasinName);
fluxesCollection.get(year).get(nutrient).get(originBasinName).put(destinationBasinName, previousFlux + flux);
double previousFlux = importFluxesCollection.get(year).get(nutrient).get(originBasinName).get(destinationBasinName);
importFluxesCollection.get(year).get(nutrient).get(originBasinName).put(destinationBasinName, previousFlux + flux);
}
}
public class NutrientExportFluxesCollection {
/*
* <key> Year
* <value>
* <key> Nutrient
* <value>
* <key> origin basin name
* <value> flux
*/
//
private Map<Long, Map<String, Map<String, Double>>> exportFluxesCollection ;
private transient String[] basinNames;
NutrientExportFluxesCollection() {
exportFluxesCollection = new HashMap <Long, Map<String, Map<String, Double>>> ();
}
public Map<Long, Map<String, Map<String, Double>>> getFluxesCollection() {
return exportFluxesCollection;
}
public void setBasinNames(String[] basinNames) {
this.basinNames = basinNames;
}
public void put(Long year,String nutrient, String originBasinName, Double flux ) {
if(!exportFluxesCollection.containsKey(year)) {
Map <String, Map <String, Double>> nutrientsMap = new TreeMap <String, Map <String, Double>>();
for (String nutrientOfInterest : nutrientsOfInterest) {
Map <String, Double> originsMap = new TreeMap <String, Double>();
for (String originName : basinNames) {
originsMap.put(originName, 0.);
}
nutrientsMap.put(nutrientOfInterest, originsMap);
}
exportFluxesCollection.put(year,nutrientsMap);
}
double previousFlux = exportFluxesCollection.get(year).get(nutrient).get(originBasinName);
exportFluxesCollection.get(year).get(nutrient).put(originBasinName, previousFlux + flux);
}
}
}
/**
* patrick.lambert
* @author Patrick Lambert
* @copyright Copyright (c) 2018, Irstea
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package species;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import environment.RiverBasin;
import environment.Time;
import environment.Time.Season;
import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
import fr.cemagref.simaqualife.pilot.Pilot;
import species.DiadromousFish.Stage;
/**
*
*/
public class WriteEffectiveAndBiomassFluxes extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
private Season exportSeason = Season.SPRING;
private String fileNameOutput = "effectiveBiomassFluxes";
private transient BufferedWriter bW;
private transient String sep=";";
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver()))
.toXML(new WriteEffectiveAndBiomassFluxes()));
}
/* (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=";";
}
@Override
public void doProcess(DiadromousFishGroup group) {
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.write("year"+sep+"type"+ sep+"originBasin" ); //create the field of the column
for (String birthBasinName : group.getEnvironment().getRiverBasinNames()) {
bW.write(sep + birthBasinName); // write each basin name in the file
}
bW.write("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (Time.getSeason(pilot) == exportSeason & Time.getYear(pilot) >= group.getMinYearToWrite()) {
//Create the map to get the biomass in each migration basin and birth basin
Map<String, Map<String, Long>> spawnerEffectivePerDestination = new HashMap<String, Map<String, Long>>(group.getEnvironment().getRiverBasinNames().length);
//Create the map to get the abundance in each migration and birth birth basin
Map<String, Map<String, Double>> spawnerBiomassPerDestination = new HashMap<String, Map<String, Double>>(group.getEnvironment().getRiverBasinNames().length);
// initialise maps with 0
for (String destinationName: group.getEnvironment().getRiverBasinNames()) {
Map<String, Long> spawnerEffectivePerOrigin = new HashMap<String, Long>(group.getEnvironment().getRiverBasinNames().length);
Map<String, Double> spawnerBiomassPerOrigin = new HashMap<String, Double>(group.getEnvironment().getRiverBasinNames().length);
for (String originName : group.getEnvironment().getRiverBasinNames()){
spawnerEffectivePerOrigin.put(originName, 0L);
spawnerBiomassPerOrigin.put(originName, 0.);
}
spawnerEffectivePerDestination.put(destinationName,spawnerEffectivePerOrigin );
spawnerBiomassPerDestination.put(destinationName,spawnerBiomassPerOrigin );
}
for (RiverBasin destinationBasin: group.getEnvironment().getRiverBasins()) {
//compute the cumulative effective and biomass per birth basin
if (destinationBasin.getFishs(group) != null) {
for (DiadromousFish fish : destinationBasin.getFishs(group)) {
if (fish.getStage() == Stage.MATURE) {
String originBasinName = fish.getBirthBasin().getName();
spawnerEffectivePerDestination.get(destinationBasin.getName()).
put(originBasinName, spawnerEffectivePerDestination.get(destinationBasin.getName()).get(originBasinName) + fish.getAmount() );
double biomass = group.getNutrientRoutine().getWeight(fish) * fish.getAmount();
spawnerBiomassPerDestination.get(destinationBasin.getName()).
put(originBasinName, spawnerBiomassPerDestination.get(destinationBasin.getName()).get(originBasinName) + biomass );
}
}
}
}
try {
// write effective
for (String originBasinName: group.getEnvironment().getRiverBasinNames()) {
bW.write(Time.getYear(pilot)+ sep + "effective" + sep + originBasinName );
for (String destinationBasinName : group.getEnvironment().getRiverBasinNames()) {
bW.write(sep+spawnerEffectivePerDestination.get(destinationBasinName).get(originBasinName));
}
// write an end-of-line
bW.write("\n");
}
// write biomass
for (String originBasinName: group.getEnvironment().getRiverBasinNames()) {
bW.write(Time.getYear(pilot)+ sep + "biomass"+ sep + originBasinName );
for (String destinationBasinName : group.getEnvironment().getRiverBasinNames()) {
bW.write(sep+spawnerBiomassPerDestination.get(destinationBasinName).get(originBasinName));
}
// write an end-of-line
bW.write("\n");
}
if (group.getPilot().getCurrentTime()== group.getPilot().getSimBegin()+group.getPilot().getSimDuration()-1) {
bW.flush();
bW.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
......@@ -47,7 +47,7 @@ public class WriteImportNutrientFluxes extends AquaNismsGroupProcess<DiadromousF
bW.write("\n");
Map<Long, Map <String, Map<String, Map<String, Double>>>> fluxesCollection = group.getNutrientRoutine().getNutrientFluxesCollection().getFluxesCollection();
Map<Long, Map <String, Map<String, Map<String, Double>>>> fluxesCollection = group.getNutrientRoutine().getNutrientFluxesCollection().getImportFluxesCollection();
// to iterate on sorted years
List<Long> years = new ArrayList<Long>(fluxesCollection.keySet());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment