/********************************************************************
 *  Copyright (C) 2014 by Federico Marulli and Alfonso Veropalumbo  *
 *  federico.marulli3@unibo.it                                      *
 *                                                                  *
 *  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, write to the Free      *
 *  Software Foundation, Inc.,                                      *
 *  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.       *
 ********************************************************************/

/**
 *  @file Headers/Lib/Data1D_collection.h
 *
 *  @brief The class Data1D_collection
 *
 *  This file defines the interface of the class Data1D
 *
 *  @authors Federico Marulli, Alfonso Veropalumbo
 *
 *  @authors federico.marulli3@unbo.it, alfonso.veropalumbo@unibo.it
 */

#ifndef __DATA1DC__
#define __DATA1DC__

#include "Data.h"
#include "Data1D.h"

namespace cosmobl {

  namespace data {
  
    /**
     *  @class Data1D_collection Data1D_collection.h
     *  "Headers/Lib/Data1D_collection.h"
     *
     *  @brief The class Data1D_collection
     *
     *  This is the base class used to manage collections of 1D data
     */
    class Data1D_collection : public Data
    {
    protected:

      /**
       *  @name Data input
       */
      ///@{
	
      /// The number of datasets
      int m_ndataset;

      /// vector containing the number of data in each dataset
      vector<int> m_xsize;

      /// vector containing indipendent variables
      vector<vector<double>> m_x;

      /// vector containing matrix-to-vector indexes
      vector<vector<int>> m_index;

      ///@}
      
    public:

      /**
       *  @name Constructors/destructors
       */
      ///@{

      /**
       *  @brief default constructor
       *  @return object of class Data1D
       */
      Data1D_collection () : Data() {set_dataType(DataType::_1D_data_collection_);}

      /**
       *  @brief constructor of class Data1D_collection
       *  @param input_file file with datasets
       *  @param skip_header number of lines to be skipped 
       *  @return object of class Data1D_collection
       */
      Data1D_collection(const string input_file, const int skip_header=0);

      /**
       *  @brief constructor of class Data1D_collection
       *  @param input_files files with datasets
       *  @param skip_header number of lines to be skipped 
       *  @return object of class Data1D_collection
       */
      Data1D_collection(const vector<string> input_files, const int skip_header=0);

      /**
       *  @brief constructor of class Data1D_collection
       *  @param xx vector containing the independent variable xx 
       *  for each dataset
       *  @param data vector containing the data for each dataset
       *  @return object of class Data1D_collection
       */
      Data1D_collection(const vector<vector<double>> xx, const vector<vector<double>> data);

      /**
       *  @brief constructor of class Data1D_collection
       *  @param xx vector containing the independent variable xx 
       *  for each dataset
       *  @param data vector containing the data for each dataset
       *  @param covariance vector containing the data covariance
       *  @return object of class Data1D_collection
       */
      Data1D_collection (const vector<vector<double>> xx, const vector<vector<double>> data, const vector<vector<double>> covariance);

      /**
       *  @brief constructor of class Data1D_collection
       *  @param xx vector containing the independent variable xx 
       *  for each dataset
       *  @param data vector containing the data for each dataset
       *  @param error vector containing the data error
       *  @return object of class Data1D_collection
       */
      Data1D_collection (const vector<vector<double>> xx, const vector<double> data, const vector<double> error);

      /**
       *  @brief constructor of class Data1D_collection
       *  @param xx vector containing the independent variable xx 
       *  for each dataset
       *  @param data vector containing the data for each dataset
       *  @param covariance vector containing the data covariance
       *  @return object of class Data1D_collection
       */
      Data1D_collection (const vector<vector<double>> xx, const vector<double> data, const vector<vector<double>> covariance);

      /**
       *  @brief default destructor
       *  @return none
       */
      virtual ~Data1D_collection () = default;

      /**
       *  @brief static factory used to construct objects of class
       *  Data1D
       *  @return a shared pointer to an object of class Data
       */
      shared_ptr<Data> as_factory () {return move(unique_ptr<Data1D_collection>(this));}

      ///@}

      
      /**
       *  @name Member functions used to get the private members
       */
      ///@{
    
      int xsize (const int i) const
      { return m_xsize[i]; }

      /**
       *  @brief get value of x at index j in the i-th dataset
       *  @param i the i-th dataset
       *  @param j index
       *  @return the value of the x vector at position j in the i-th dataset
       */
      double xx (const int i, const int j) const { return m_x[i][j]; }  

      /**
       *  @brief get x values
       *  @param x [out] the x values
       *  @return the value of the m_x vector for the i-th dataset
       */
      void xx (vector<vector<double> > x) const { x = m_x; }

      /**
       *  @brief get the independet variable, to be used 
       *  in model computation
       *  @param i index (not used)
       *  @param j index (not used)
       *  @return the independent variable
       */
      vector<vector<double>> IndipendentVariable(const int i=-1, const int j=-1) const {(void)i; (void)j; return m_x;}

      /**
       *  @brief get data at index i,j
       *  @param i index
       *  @param j index
       *  @return the value of the m_data vector at position i,j
       */
      double data (const int i, const int j) const {return m_data[m_index[i][j]];} 

      /**
       *  @brief get data
       *  @param [out] data vector containing the dataset
       *  @return none
       */
      void data(vector<vector<double>> &data) const;

      /**
       *  @brief get value of f(x) error at index i,j
       *  @param i index
       *  @param j index
       *  @return the value of the m_error vector at position i,j
       */
      double error (const int i, const int j) const { return m_error[m_index[i][j]]; } 

      /**
       *  @brief get standard deviation
       *  @param [out] error vector containing the error
       *  @return none
       */
      void error(vector<vector<double>> &error) const;

      ///@}
      
      /**
       *  @name Member functions used to set the private members
       */
      ///@{

      /**
       *  @brief set interval variable m_x in the i-th dataset
       *  @param i index to the i-th dataset
       *  @param x vector containing x points
       *  @return none
       */
      void set_xx (const int i, const vector<double> x) { m_x[i] = x; } 

      ///@}
      
      /**
       *  @name Member functions to compute data properties
       */
      ///@{

      /**
       * @brief function that returns total number of datasets
       * @return total number of dataset
       */
      int ndataset () const { return m_ndataset; }  
      
      /**
       *  @brief read the data
       *  @param input_file input data file
       *  @param skip_nlines the header lines to be skipped
       *  @return none
       */
      void read (const string input_file, const int skip_nlines=0) override;
      
      /**
       *  @brief read the data
       *  @param input_files input data files
       *  @param skip_nlines the header lines to be skipped
       *  @return none
       */
      void read (const vector<string> input_files, const int skip_nlines=0) override;

      /**
       *  @brief write the data
       *  @param dir output directory
       *  @param file output file
       *  @param header text with the variable names to be written at
       *  the first line of the output file
       *  @param precision the float precision
       *  @param rank cpu index (for MPI usage)
       *  @return none
       */
      void write (const string dir, const string file, const string header, const int precision=10, const int rank=0) const override;
      
      /**
       *  @brief write the data
       *  @param dir output directory
       *  @param files output files
       *  @param header text with the variable names to be written at
       *  the first line of the output file
       *  @param precision the float precision
       *  @param rank cpu index (for MPI usage)
       *  @return none
       */
      void write (const string dir, const vector<string> files, const string header, const int precision=10, const int rank=0) const override;

      /**
       *  @brief write the interval variable m_covariance on a file,
       *  @param dir the output directory
       *  @param file the output file
       *  @param precision the float precision
       *  @return none
       */
      void write_covariance (const string dir, const string file, const int precision=10) const override;

      ///@}
      
      /**
       *  @name Member functions for data cut
       */

      ///@{
     
      /**
       * @brief cut the data, for Data1D_collection
       * @param dataset the i-th dataset
       * @param xmin minumum value for the independet variable x
       * @param xmax maximum value for the independent variable x
       * @return pointer to an object of type Data1D
       */
      shared_ptr<Data> cut(const int dataset, const double xmin, const double xmax) const override;

      /**
       * @brief cut the data, for Data1D_collection type
       * @param xmin vector containing minumum values for the independet variable x
       * @param xmax vector containing maximum values for the independent variable x
       * @return pointer to an object of type Data1D_collection
       */
      shared_ptr<Data> cut(const vector<double> xmin, const vector<double> xmax) const override;

      ///@}

    };
   
  }
}

#endif
