/*! \file utilities.h
		\author Vishnu Krishnamachari
		\date June 2006 - May 2008
		\brief Defines some utility functions.

		Defines some utility functions that are used by the methods of the classes introduced in this package.
*/
#ifndef __UTILITIES_H__
#define __UTILITIES_H__

#include "focus3d_parameters.h"
#include "precompile.h"

		//!A simple function for making a directory if it already does not exist.
		/*! 
				\param s - name of the directory.
				\return result - a bool - true if successful false otherwise
		*/
bool my_mkdir(string s);
//---------------------------

		//!Calculates the Simpson 1/3 weights for a three-dimensional grid.
		/*!
			\param Nx - number of points along dimension 1
			\param Ny - number of points along dimension 2
			\param Nz - number of points along dimension 3
			\param Dx - grid spacing along dimension 1
			\param Dy - grid spacing along dimension 2
			\param Dz - grid spacing along dimension 3
			\return output - Array<double,3>: a three dimensional blitz++ array
		*/
Array<double,3> calculate_simpson_13_weights(int Nx,int Ny,int Nz,double Dx,double Dy,double Dz);
//---------------------------

		//!Calculates the Simpson 1/3 weights for a two-dimensional grid.
		/*!
			\param Ntheta - number of points along dimension 1
			\param Nphi - number of points along dimension 2
			\param Dtheta - grid spacing along dimension 1
			\param Dphi - grid spacing along dimension 2
			\return output - Array<double,2> A(theta,phi): a two dimensional blitz++ array
		*/
Array<double,2> calculate_simpson_13_weights(int Ntheta,int Nphi,double Dtheta,double Dphi);
//---------------------------
		//!Prints read file error to cerr.
		/*!
			\param s - std::string to be sent to cerr 
		*/
void file_read_error(string s);
//---------------------------

		//!Calculates the angular weights.
		/*!
			\param ampl_coeffs - blitz array of polynomial coefficients of amplitude fit
			\param phase_coeffs - blitz array of polynomial coefficients of phase fit
			\param param - focus3d_parameters object containing Ntheta and Nphi information.
			\return result - a 2D complex array of size (param.Ntheta,param.Nphi) of angular weights  
		*/
Array<complex<double>,2> make_angular_weights( Array<double,1> ampl_coeffs,
    Array<double,1> phase_coeffs, focus3d_parameters param);
//---------------------------

		//!load polynomial coefficients.
		/*!
			\param filename - std::string with filename containing the polynomial coefficients
			\param poly_fit_order - order of the fit polynomial
			\return result - a one dimensional array of the polynomial coefficients 
			
			Note:
				- The polynomial coefficients file is a file containing the coefficients of the polynomial fit one in each line. If the polynomial fit is a third order polynomial, then the fit function \f$ f(x) \f$ is
				\f{eqnarray*}
				f(x) &=& P(0)*x^3 + P(1)*x^2 + P(2)*x + P(3) 
				\f}
				and the coefficients P(0), ..., P(3) are stored in the file one in each line in that order.
		*/
Array<double,1> load_polynomial_coeffs(string filename, int poly_fit_order);
//---------------------------

		//!Calculates angular weights.
		/*!
			\param ampl_coeff_file - std::string with filename containing the polynomial coefficients for amplitude fit
			\param phase_coeff_file - std::string with filename containing the polynomial coefficients for amplitude fit
			\param param - focus3d_parameters object with information about Ntheta and Nphi
			\return result - a 2D complex array of size (param.Ntheta,param.Nphi) of angular weights 
			
			Note:
				- The polynomial coefficients file is a file containing the coefficients of the polynomial fit one in each line. If the polynomial fit is a third order polynomial, then the fit function \f$ f(x) \f$ is
				\f{eqnarray*}
				f(x) &=& P(0)*x^3 + P(1)*x^2 + P(2)*x + P(3) 
				\f}
				and the coefficients P(0), ..., P(3) are stored in the file one in each line in that order.
		*/
Array<complex<double>,2> make_angular_weights( string ampl_coeff_file,
    string phase_coeff_file, focus3d_parameters param);
//---------------------------

		//!Determine the order of the polynomial fit.
		/*!
			\param filename - std::string with filename containing the polynomial coefficients
			\return result - an integer with the order of the polynomial fit 
			
			Note:
				- The polynomial coefficients file is a file containing the coefficients of the polynomial fit one in each line. If the polynomial fit is a third order polynomial, then the fit function \f$ f(x) \f$ is
				\f{eqnarray*}
				f(x) &=& P(0)*x^3 + P(1)*x^2 + P(2)*x + P(3) 
				\f}
				and the coefficients P(0), ..., P(3) are stored in the file one in each line in that order.
		*/
int determine_poly_fit_order(string filename);
//---------------------------

		//!Interpolate amplitude and phase data of G(\f$ \theta \f$) from a file.
		/*!
			\param filename - std::string with filename containing three colums corresponding to \f$ \theta \f$, \f$ |G(\theta)| \f$, and angle(\f$ G(\theta) \f$)
			\param theta - A double array with the theta values at which the interpolated  G(\f$ \theta \f$) is required
			\param[out] output - a complex double array which is populated with interpolated G(\f$ \theta \f$). Note the memory to this array should be allocated before calling the function. The length of this array is equal to that of the second parameter.
		*/
void interpolate_data(string filename, Array<double,1> theta, Array<complex<double>,1>& output);
//----------------------------

int get_number_of_data_points(string filename);
#endif
