//# FlagAgentTimeFreqCrop.h: This file contains the interface definition of the FlagAgentTimeFreqCrop class.
//#
//#  CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
//#  Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
//#  Copyright (C) European Southern Observatory, 2011, All rights reserved.
//#
//#  This library is free software; you can redistribute it and/or
//#  modify it under the terms of the GNU Lesser General Public
//#  License as published by the Free software Foundation; either
//#  version 2.1 of the License, or (at your option) any later version.
//#
//#  This library 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
//#  Lesser General Public License for more details.
//#
//#  You should have received a copy of the GNU Lesser General Public
//#  License along with this library; if not, write to the Free Software
//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
//#  MA 02111-1307  USA
//# $Id: $

#ifndef FlagAgentTimeFreqCrop_H_
#define FlagAgentTimeFreqCrop_H_

#include <flagging/Flagging/FlagAgentBase.h>

namespace casa { //# NAMESPACE CASA - BEGIN

class FlagAgentTimeFreqCrop : public FlagAgentBase {

public:

	FlagAgentTimeFreqCrop(FlagDataHandler *dh, casacore::Record config, casacore::Bool writePrivateFlagCube = false, casacore::Bool flag = true);
	~FlagAgentTimeFreqCrop();

protected:

	// Compute flags for a given (time,freq) map
	bool computeAntennaPairFlags(const vi::VisBuffer2 &visBuffer, VisMapper &visibilities,FlagMapper &flags,casacore::Int antenna1,casacore::Int antenna2,vector<casacore::uInt> &rows);

	// Parse configuration parameters
	void setAgentParameters(casacore::Record config);

private:

  /// casacore::Input parameters ///

  // Flag threshold in time.
  // (flag all data-points further than N-stddev from the fit).
  casacore::Double time_cutoff_p;
  // Flag threshold in frequency.
  // (flag all data-points further than N-stddev from the fit).
  casacore::Double freq_cutoff_p;
  // Maximum number of pieces to allow in the piecewise-polynomial fits (1-9)
  casacore::Int maxNPieces_p;
  // Fitting function for the time direction  ('line' or 'poly')
  casacore::String timeFitType_p;
  // Fitting function for the frequency direction  ('line' or 'poly')
  casacore::String freqFitType_p;
  // Choose the directions along which to perform flagging
  // ('time', 'freq', 'timefreq', 'freqtime')
  casacore::String flagDimension_p;
  // Half width of sliding window to use with 'usewindowstats' 
  // (1,2,3 for 3-point, 5-point or 7-point window sizes)
  casacore::Int halfWin_p;
  // Use sliding-window statistics to find additional flags
  // ('none', 'sum', 'std', 'both' )
  casacore::String winStats_p;
  // Read original flags or not. 
  // Note : original flags are those in the casacore::MS (and not those generated by agents)
  // true/false 
  //  casacore::Bool usePreFlags_p;
  
  
  /////// TFCROP functions
  
  // Average the data, fit a piecewise polynomial, divide it out, flag outliers.
  void fitBaseAndFlag(casacore::String fittype, casacore::String direction, VisMapper &visibilities,FlagMapper &flags);
  
  // Calculate Mean, Variance, Stddev while accounting for flags
  casacore::Float calcMean(casacore::Vector<casacore::Float> &vect, casacore::Vector<casacore::Bool> &flag);
  casacore::Float calcVar(casacore::Vector<casacore::Float> &vect, casacore::Vector<casacore::Bool> &flag, casacore::Vector<casacore::Float> &fit);
  casacore::Float calcStd(casacore::Vector<casacore::Float> &vect, casacore::Vector<casacore::Bool> &flag, casacore::Vector<casacore::Float> &fit);
  casacore::Float calcStd(casacore::Vector<casacore::Float> &vect, casacore::Vector<casacore::Bool> &flag, casacore::Float mean);
  
  // Fit a piece-wise polynomial according to the supplied specs
  void fitPiecewisePoly(casacore::Vector<casacore::Float> &data,casacore::Vector<casacore::Bool> &flag, casacore::Vector<casacore::Float> &fit, casacore::uInt maxnpieces, casacore::uInt maxdeg);
  
  // Fit a polynomial of specified degree to a range of data points
  void polyFit(casacore::Vector<casacore::Float> &data,casacore::Vector<casacore::Bool> &flag, casacore::Vector<casacore::Float> &fit, casacore::uInt lim1, casacore::uInt lim2,casacore::uInt deg);
  
  // Fit a line to a range of data points
  void lineFit(casacore::Vector<casacore::Float> &data,casacore::Vector<casacore::Bool> &flag, casacore::Vector<casacore::Float> &fit, casacore::uInt lim1, casacore::uInt lim2);
  


};


} //# NAMESPACE CASA - END

#endif /* FlagAgentTimeFreqCrop_H_ */