%	BatchDiego.m


%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION. 

clear all;
%==================================================================
TRStatus=1;
[DefFilePath] = SetDefPath('r','*.RAW*');
[NNetFiles,RAWFileMat,NoUse,BatchFilePath]=ReadFileNames([],DefFilePath,'Choose NetStation file:');
if NNetFiles==0; return; end
[BatchFile,BatchPath]=SepFilePath(BatchFilePath);
SetDefPath('w',BatchPath);


[UseOverAllGainFileStatus]=IfEmptyInputBo('Do you want to use one special gain and one special zero file ?',[],[],1,1);

if UseOverAllGainFileStatus
	DefFilePath=SetDefPath(1,'*.Gain');
	[GainFile,GainPath,GainFilePath]=ReadFilePath([],DefFilePath,'Please choose the over all gain file:');
	SetDefPath(2,GainPath);
	DefFilePath=SetDefPath(1,'*.Zero');
	[ZeroFile,ZeroPath,ZeroFilePath]=ReadFilePath([],DefFilePath','Please choose the over all zero file:');
	if GainFile==0; GainFile=[]; GainPath=[]; GainFilePath=[]; end
	if ZeroFile==0; ZeroFile=[]; ZeroPath=[]; ZeroFilePath=[]; end
else
	GainFilePath=[];
	ZeroFilePath=[];
end

%===============FilterNetFile====================================

%Filter 50 Hz LowPass
ch_LowPassFilt=0;
ch_HighPassFilt=0;
SampRate=[];
if ch_LowPassFilt | ch_HighPassFilt
	for FileInd=1:NNetFiles
		[NoUse,NoUse,NetFilePath]=GetFileNameOfMat(RAWFileMat,FileInd);
		[NoUse,NoUse,NoUse,NoUse, ...
		NoUse, NoUse, NoUse, NoUse, NoUse, NoUse, NoUse, ...
		SampRateTmp,NChan]= ReadRAWHeader(NetFilePath);
		if ~isempty(SampRate)
			 if SampRateTmp~=SampRate
				error('It is not possible to filter in batch mode because of different sampling rates in different files !')
			end
		end
		SampRate=SampRateTmp;
	end
end

HighDefFreqsMat=[SampRate 1 0.01];
HighDefRipMat=[3 45];
LowDefFreqsMat=[SampRate 50 60]; 
LowDefRipMat=[3 45];
ch_ChooseChannelInd=0;
ChanStatusVec=ones(NChan,1);
PlotStatus=0;
ch_AddSubZeros=0;
NZPoints=[];

[ch_LowPassFilt,LowB,LowA,ch_HighPassFilt,HighB,HighA,LowFreqsMat,HighFreqsMat] = ...
GetHighLowFiltCoeff(SampRate,HighDefFreqsMat,HighDefRipMat,LowDefFreqsMat,LowDefRipMat,...
ch_LowPassFilt,ch_HighPassFilt);

%================================================================
%===============Transform data file format=======================
[TAWFileMat,TRStatus]=TransRawTaw(RAWFileMat,TRStatus,20);

%=============================================================
%[HistFileMat,TRStatus]=ExpDin2Hist(FileMat,TRStatus,MaxMemory,NoEventVec);
TrigStatusMat=ones(4,59);
TrigStatusMat(3,[1 2 59])=zeros(1,3);
[HistFileMat]=ExpDin2Hist(TAWFileMat,TRStatus,10,TrigStatusMat);

%DeleteFileMat(RAWFileMat);
%===============FilterNetFile====================================
[FilterTAWFileMat]=FilterNetFile(TAWFileMat,ch_LowPassFilt,LowB,LowA,...
ch_HighPassFilt,HighB,HighA,LowFreqsMat,HighFreqsMat,...
ch_ChooseChannelInd,ChanStatusVec,PlotStatus,ch_AddSubZeros,NZPoints,TRStatus);

DeleteFileMat(TAWFileMat);

%[EGISFileMat,NetFileMat]=TransNetGeoHistTmp(NetFileMat,TRStatus,PreTrig,PostTrig,ReTrig,GainFilePath,ZeroFilePath,...
%OldZeroVersion,NGainZeroString,SpecCondVec,NStep,NDefTrials,SimpleTrig,DSample,DMeanSampleStatus)
[FilterEGISFileMat,NonUse,CondFileMat]=TransNetGeoHistTmp(FilterTAWFileMat,TRStatus,49,100,1,GainFilePath,ZeroFilePath,0,[],[],[],56)

%=============================================================
%[OutFilePath]=MergeEgisFiles(InFileMat,OutFilePath,MergeAddStatus)
[MergeFile,MergePath]=SepFilePath(FilterEGISFileMat(1,:));
[MergeFilePath]=MergeEgisFiles(FilterEGISFileMat,[MergePath,MergeFile(1:5),'.E1'],2);

%=============================================================
[MergeConFilePath]=MergeMatFiles(CondFileMat,[MergeFilePath,'.CON'],[],[],[],1,1,0);

%===============CalcAutoEditMat without avg. re.f and without flat====================================
%[AEMFilePathMat]=CalcAutoEditMat(FileMat,TimeStatusVec,ch_CalcAvgRef,ch_UseSpecChan,...
%ch_CalcBaseline,BaseStartPoint,BaseEndPoint,ch_ChooseTimeInd,LFlatWin,ch_CalcFlaMat,ch_UseAWE);
[AEMFilePathMat]=CalcAutoEditMat(MergeFilePath,[],0,[],1,1,49,1,8,0,0);

%====== Search for bad sensors ==================================
%FindBadChan(AEMFilePathMat,AWELimesFac,BatchStatus);
FindBadChan(AEMFilePathMat,3,1);

%====== Search for bad trials ==================================
% [AWEFileMat]=FindBadChanTrial(FilePathMat,AWELimesFac,BatchStatus);
FindBadChanTrial(AEMFilePathMat,2.5,1);
 
%===============CalcAutoEditMat using avg. ref. and flat====================================
%[AEMFilePathMat]=CalcAutoEditMat(FileMat,TimeStatusVec,ch_CalcAvgRef,ch_UseSpecChan,...
%ch_CalcBaseline,BaseStartPoint,BaseEndPoint,ch_ChooseTimeInd,LFlatWin,ch_CalcFlaMat,ch_UseAWE);

[AEMFilePathMat]=CalcAutoEditMat(MergeFilePath,[],1,1,1,1,124,0,8,1,1);

