/* * #%L * IsisFish data * %% * Copyright (C) 2006 - 2011 Ifremer, CodeLutin * %% * 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 2 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 * . * #L% */ package rules; import static org.nuiton.i18n.I18n._; import java.util.HashSet; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.math.matrix.MatrixIterator; import org.nuiton.math.matrix.MatrixND; import scripts.ResultName; import scripts.RuleUtil; import scripts.SiMatrix; import fr.ifremer.isisfish.datastore.ResultStorage; import fr.ifremer.isisfish.entities.EffortDescription; import fr.ifremer.isisfish.entities.Metier; import fr.ifremer.isisfish.entities.Population; import fr.ifremer.isisfish.entities.Species; import fr.ifremer.isisfish.entities.Strategy; import fr.ifremer.isisfish.entities.StrategyMonthInfo; import fr.ifremer.isisfish.entities.TargetSpecies; import fr.ifremer.isisfish.rule.AbstractRule; import fr.ifremer.isisfish.simulator.MetierMonitor; import fr.ifremer.isisfish.simulator.PopulationMonitor; import fr.ifremer.isisfish.simulator.SimulationContext; import fr.ifremer.isisfish.types.TimeStep; import fr.ifremer.isisfish.types.Month; import fr.ifremer.isisfish.util.Doc; /** * * * Created: 16 octobre 2012 * * @author anonymous * @version $Revision: 1.3 $ * * Last update: $Date: 161012 $ * by : $Author: Stephanie $ */ public class CapturesLongLinersNetters extends AbstractRule { /** to use log facility, just put in your code: log.info("..."); */ static private Log log = LogFactory.getLog(CapturesLongLinersNetters.class); @Doc("Affected species") public Species param_species = null; //pour le merlu @Doc("Debin date") public TimeStep param_beginStep = new TimeStep(0); //2001 @Doc("End date") public TimeStep param_endStep = new TimeStep(112); //2010 // deux fichiers 112 lignes et 65 colonnes : captures en Nombre public String param_nomfichier_capturesLiners = "C:/StephAncienPortableDisqueD/SimulationsISIS/INPUTS_Sauvegarde/Inputs_LangoustineMerluEnLongeurGdG/CapturesLinersSpanishNetters/CatchNumberHakeGdGLiners20012010Moisgpe2cm.csv";//in row : time ; in columns : age or length group protected File CatchHakeGdGLiners; protected MatrixND matrixCatchHakeGdGLiners; public String param_nomfichier_capturesNetters = "C:/StephAncienPortableDisqueD/SimulationsISIS/INPUTS_Sauvegarde/Inputs_LangoustineMerluEnLongeurGdG/CapturesLinersSpanishNetters/CatchNumberHakeGdGNetters20012010Moisgpe2cm.csv";//in row : time ; in columns : age or length group protected File CatchHakeGdGNetters; protected MatrixND matrixCatchHakeGdGNetters; boolean affectation = false; protected String[] necessaryResult = { // put here all necessary result for this rule // example: // ResultName.MATRIX_BIOMASS, // ResultName.MATRIX_NET_VALUE_OF_LANDINGS_PER_STRATEGY_MET, }; /** * @return the necessaryResult */ @Override public String[] getNecessaryResult() { return this.necessaryResult; } /** * Permet d'afficher a l'utilisateur une aide sur la regle. * * @return L'aide ou la description de la regle */ @Override public String getDescription() { return _("Remove catches in number of French and Spanih linners and Spanish gillnetters from abundance, the former in zone_merlu_reproduction and the latter in zone_merlu_recrutemenr - period 2001-2010"); } /** * Appele au demarrage de la simulation, cette methode permet d'initialiser * des valeurs * * @param context La simulation pour lequel on utilise cette regle */ @Override public void init(SimulationContext context) throws Exception { if (param_nomfichier_capturesLiners==null || "".equals(param_nomfichier_capturesLiners)){ CatchHakeGdGLiners = FileUtil.getFile(".*.csv", "fichier csv separateur ';'"); } else { CatchHakeGdGLiners = new File(param_nomfichier_capturesLiners); } if (param_nomfichier_capturesNetters==null || "".equals(param_nomfichier_capturesNetters)){ CatchHakeGdGNetters = FileUtil.getFile(".*.csv", "fichier csv separateur ';'"); } else { CatchHakeGdGNetters = new File(param_nomfichier_capturesNetters); } TopiaContext db = context.getParam().getRegion().getStorage().beginTransaction(); /////*** specify dimention of the matrix containning catch /////*** numbers of columns : could be equal to your number of classes in ISIS but may also be different if your had only aggregated data int nbGroup = 65 ; /////*** enter number of rows (years*12) (lines of the catch file) int nbTimeStep = 120; //de 2001 a 2010 int[] dimMatrix = {nbTimeStep,nbGroup}; matrixCatchHakeGdGLiners = MatrixFactory.getInstance().create(dimMatrix); matrixCatchHakeGdGLiners.importCSV(new FileReader(CatchHakeGdGLiners),new int []{0,0}); matrixCatchHakeGdGLiners=matrixCatchHakeGdGLiners.reduce(); log.info("MatrixCapturesNombreLiners : " + matrixCatchHakeGdGLiners); matrixCatchHakeGdGNetters = MatrixFactory.getInstance().create(dimMatrix); matrixCatchHakeGdGNetters.importCSV(new FileReader(CatchHakeGdGNetters),new int []{0,0}); matrixCatchHakeGdGNetters=matrixCatchHakeGdGNetters.reduce(); log.info("MatrixCapturesNombreNetters : " + matrixCatchHakeGdGNetters); db.closeContext(); } /** * La condition qui doit etre vrai pour faire les actions. * * @param context la simulation pour lequel on utilise cette regle * @param step le pas de temps courant * @param metier le metier concern���� * @return vrai si on souhaite que les actions soit faites */ @Override public boolean condition(SimulationContext context, TimeStep step, Metier metier) throws Exception { boolean result = false; if (step.before(param_beginStep)) { result = false; } else if (step.after(param_endStep)) { result = false; } else { result = true; } return result; } /** * Si la condition est vrai alors cette action est executee avant le pas * de temps de la simulation. * * @param context la simulation pour lequel on utilise cette regle * @param step le pas de temps courant * @param metier le metier concern���� */ @Override public void preAction(SimulationContext context, TimeStep step, Metier metier) throws Exception { affectation = false; } /** * Si la condition est vrai alors cette action est execut����e apres le pas * de temps de la simulation. * * @param context La simulation pour lequel on utilise cette regle * @param step le pas de temps courant * @param metier le metier concern���� */ @Override public void postAction(SimulationContext context, TimeStep step, Metier metier) throws Exception { //ne faire cette postAction que pour le premier metier ResultStorage resultmanager = context.getSimulationStorage() .getResultStorage(); if (!affectation) { PopulationMonitor popMon = context.getPopulationMonitor(); Population pop =param_species.getPopulation(); MatrixND abondance = popMon.getN(pop); //on soustrait les captures des longliners et nettersSpanish for (MatrixIterator i = abondance.iterator(); i .next();) { Object[] coord = i.getSemanticsCoordinates(); Zone zone = (Zone) coord[1]; if (zone.getName().equals("zone_merlu_recrutement")) { int valAbondance = abondance.getValue(coord[0],coord[1]); int valCapture = matrixCatchHakeNetters.getValue(step.getStep(),coord[0]); abondance.setValue(coord[0],coord[1],valAbondance-valCapture); } else if (zone.getName().equals("zone_merlu_reproduction")) { int valAbondance = abondance.getValue(coord[0],coord[1]); int valCapture = matrixCatchHakeLiners.getValue(step.getStep(),coord[0]); abondance.setValue(coord[0],coord[1],valAbondance-valCapture); } } } affectation = true; } }