/*
 * #%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
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */
package exports;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Writer;

import org.nuiton.math.matrix.*;

import scripts.ResultName;

import fr.ifremer.isisfish.entities.*;
import fr.ifremer.isisfish.export.Export;
import fr.ifremer.isisfish.types.TimeStep;
import fr.ifremer.isisfish.datastore.SimulationStorage;

/**
 * Biomasses.java
 *
 * Created: 23 novembre 2006
 *
 * @author anonymous <anonymous@labs.libre-entreprise.org>
 * @version $Revision:  $
 *
 * Last update: $Date: 28/08/2012$
 * by : $Author:lgasche $
 */
public class Biomasse_at_age_Indiseas implements Export {

    /** to use log facility, just put in your code: log.info("..."); */
    static private Log log = LogFactory.getLog(Biomasse_at_age_Indiseas.class);

    protected String [] necessaryResult = {
            ResultName.MATRIX_BIOMASS_BEGIN_MONTH
    };

    @Override
    public String[] getNecessaryResult() {
        return this.necessaryResult;
    }

    @Override
    public String getExportFilename() {
        return "Biomasse_at_age_Indiseas";
    }

    @Override
    public String getExtensionFilename() {
        return ".csv";
    }

    @Override
    public String getDescription() {
        return "Exporte les biomasses aux ages 1 fois par an";
    }

    @Override
    public void export(SimulationStorage simulation, Writer out) throws Exception {
    	TimeStep lastStep = simulation.getResultStorage().getLastStep();
        for (Population pop : simulation.getParameter().getPopulations()) {
            for (TimeStep step = new TimeStep(0); !step.after(lastStep); step = step
                    .next()) {
				MatrixND mat = simulation.getResultStorage().getMatrix(step, pop, ResultName.MATRIX_BIOMASS_BEGIN_MONTH);
				if( mat != null){
				int mois = 0;
				if(pop.getName().equals("Veined Squid")) mois = 6;
	               if(step.getMonth().getMonthNumber()==mois){
	                    MatrixND mat2 = mat.sumOverDim(1).reduceDims(1);
					for (MatrixIterator i=mat2.iterator(); i.hasNext();) {
		               	i.next();
		               	Object [] sems = i.getSemanticsCoordinates();
		               	PopulationGroup group = (PopulationGroup)sems[1];
	                   
	                    	out.write(pop.getName() +";"+ step.getStep() + ";"+ group.getId()+";"+ i.getValue() +"\n");
					}
                	}
            }}
        }
    }
}
