/*--------------------------------------------------------------------------
 * database.h - definitions/declarations for Sample and Database classes
 *
 *       (C) Jurgen Doornik 1994-2005
 *
 *--------------------------------------------------------------------------*/

#ifndef DATABASE_INCLUDED
#define DATABASE_INCLUDED

enum
{
    DB_DOUBLE    = 0,
    DB_DATE      = 1,
    DB_CHOICE    = 2
};

class Sample
{
    decl m_iFreq;                               // data frequency
    decl m_iYear1, m_iPeriod1;// year,period of first observation
    decl m_iYear2, m_iPeriod2; // year,period of last observation
    GetFrequency();         			        // data frequency
    GetYear1();                      // year of first observation
    GetPeriod1();                  // period of first observation
    GetYear2();                       // year of last observation
    GetPeriod2();                   // period of last observation
    GetSize();                // number of observations in sample
    GetIndex(const iYear,const iPeriod);   // index of time point
    ObsYear(obs);              // finds year of observation index
    ObsPeriod(obs);          // finds period of observation index
};

class Database : Sample
{
    decl m_mData;                                  // data matrix
    decl m_asNames;                             // variable names
    decl m_mVarsel;                         // variable selection
    decl m_mLagsel;                         // corresponding lags
    decl m_mSelgroup;                // group number of selection
    decl m_iT1sel, m_iT2sel;                   // selected sample
    decl m_iSampleSelMode;                 // mode for findsample
	decl m_sDbName;					 // name of database (if any)

	decl m_vVarType;			 // variable type 0=DB_DOUBLE,...

	Database();                                    // constructor
	Empty();				   // empties the database and sample

	Append(const mNew,         // append a matrix to the database
        const asNew, ...);	     // last argument is optional iT1
    Create(const iFreq, const iYear1,        // create a database
        const iPeriod1, const iYear2, const iPeriod2);
    Deterministic(const fCseason);      // create Constant, Trend
                              // and Seasonals (possibly centred)
    GetAll();                       // returns the whole database
    GetAllNames();              // returns all the variable names
	GetDbName();							 // get database name
	SetDbName(const sName);			         // set database name
	GetSample();                     // text with database sample
    GetVar(const sName);              // gets variable(s) by name
	GetVarByIndex(const iVar);       // gets variable(s) by index
	GetVarNameByIndex(const iVar);	     // gets name(s) by index
    GetVarIndex(const sName); // gets the database index of a var
	Grow(const cTadd); // grows sample size by cTadd observations
    Info();                           // prints database contents
    Load(const file);      				         // load data set
    LoadIn7(const file);                // load PcGive 7 data set
    LoadXls(const file);           // load Excel spreadsheet file
    LoadWks(const file);           // load Lotus spreadsheet file
    LoadFmtVar(const file);    // load ASCII formated by variable
    LoadObs(const file,         // load ASCII file by observation
        const cVar, const cObs, const iYear1,
        const iPeriod1, const iFreq, const fOffendMis);
    LoadVar(const file,            // load ASCII file by variable
        const cVar, const cObs, const iYear1,
        const iPeriod1, const iFreq, const fOffendMis);
    LoadDht(const file, const iYear1,
        const iPeriod1, const iFreq);          // Gauss data file
    LoadDta(const file, const iYear1,
        const iPeriod1, const iFreq);          // Stata data file
    Remove(const sName);   // remove a variable from the database
                           //  also removes the current selection
	RemoveObsIf(const vRemove);
    Rename(const sNewName, const sOldName);  // rename a variable
    Renew(const mNew,     // renew the observations of a variable
        const asNew, ...);	     // last argument is optional iT1
	RenewBlock(const mNew, const iVarIndex);// renews a var block
    Save(const file);   // save the data set (file type from ext)
    SaveIn7(const file);                // save PcGive 7 data set
    SaveWks(const file);           // save Excel spreadsheet file
    SaveXls(const file);           // save Lotus spreadsheet file
    SaveFmtVar(const file);    // save ASCII formated by variable
    SaveObs(const file);        // save ASCII file by observation
    SaveVar(const file);           // save ASCII file by variable
    SetVar(const vNew, const sName);  // sets variable(s) by name

	// vartype/date specific code 
	IsDated();
	SetVarTypeByIndex(const iVar, const iType);
	GetVarTypeByIndex(const iVar);
	SetDates(const vDates);
	GetDates();
	GetIndexByDates(const dDate1, const dDate2);
	GetIndexByDate(const dDate1);
	GetDateByIndex(const iT1);
	
	// variable selection
    DeSelect();                       // remove current selection
	DeSelectByIndex(const iSel);     // remove var from selection
	DeSelectByName(const sVar, const iGroup, const iLag);
    FindSelection(const sVar,    // find a variable name with lag
        const iLag);             //              in the selection
	ForceSelSample(const iYear1, const iPeriod1,
		const iYear2, const iPeriod2);//forces a selection sample
	ForceSelSampleByIndex(const iT1, const iT2);
    GetGroup(const iGroup);       // get a group selection matrix
    GetGroupLag(const iGroup,// get group with specific lag range
        const iLag1, const iLag2);
	GetGroupLagNames(const iGroup,
		const iLag1, const iLag2, const aasNames);
	GetGroupNames(const iGroup,   // get the var names of a group
		const aasNames);
    GetMaxSelLag();                 // returns maximum lag length
    GetMaxGroupLag(const iGroup); // gets max lag length in group
    GetSelEnd();           // index of last selection observation
	GetSelInfo();				 // get array with selection info
	virtual GetSampleByIndex(const iT1, const iT2);
    GetSelSample();                  // text with selected sample
    GetSelStart();        // index of first selection observation
	Select(const iGroup,    // select variables from the database
        const aSel);
	SelectByIndex(const iGroup, const iVar,
		const iLag0, const iLag1);
	SetSelInfo(const asInfo);	 // set array with selection info
	SetSelSampleMode(const iMode);
	GetSelSampleMode();
    SetSelSample(const iYear1, const iPeriod1,	  //select sample
		const iYear2, const iPeriod2);
    SetSelDates(const iYear1, const iMonth1, const iDay1,
		const iYear2, const iMonth2, const iDay2);
	SetSelSampleByIndex(const iT1, const iT2);
	SetSelSampleByDates(const dDate1, const dDate2);
		 
	// obsolescent:
    GetMaxLag();                              // use GetMaxSelLag
	SetSample(const iYear1,const iPeriod1,    // use SetSelSample
         const iYear2, const iPeriod2);
};

#endif /* DATABASE_INCLUDED */
