function dtt2mlab(testType,testPath,testList,commentList,outputPath,outputList,workPath,toolDir)
% DTT2MLAB - runs DTT template, preps Matlab output
%      Inputs
%           testType - how to run test (0-read,1-interactive,2-background)
%           testPath - directory of test files
%           testList - list of test files in testPath
%           commentList - list of comments for each test in list
%           outputPath - directory to save results
%           outputList - list of result files         
%           workPath - (optional) directory to save work files
%           toolDir - (optional) directory for dtt2mlab    
%
%  - A. Lombardi - July 13, 2012 - alexander.lombardi@ligo.org
%
% This example script opens a DTT template, runs the test, and returns results.
% This requires the python script dtt2mlab.py in the same directory.
% You build a DTT template and add the information about it to the User
% values block below. The template will be copied to the current directory
% (i.e. the directory containing this script) with a timestamp in front of
% it. This template will be opened and you can run it. After running the
% test, save it and hit enter in the Matlab terminal. This will return a
% matlab structure called "result" to the workspace and save this structure
% to a file that you specify
%
%  March 15, 2013 - K. Thorne - now a proper function
%
% Files you need in toolDir: dtt2mlab.py

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% User values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
if(nargin < 7)
    workPath = testPath;
end
if(nargin < 8)
    userDir = getenv('USERAPPS_DIR');
    if(isempty(userDir))
        userDir = '/opt/rtcds/userapps/release';
    end
    toolDir = fullfile(userDir,'cds/common/scripts/dtt2mlab');
end
pyScript = fullfile(toolDir,'dtt2mlab.py'); % where does the python script live
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define outputs from dtt2mlab.py
tempDataFile = fullfile(workPath,'temporary.dat');
tempInFile = fullfile(workPath,'temporaryin.dat');
tempParamFile =  fullfile(workPath,'temporaryparams.dat');

% Get runtime, nChan, and dttMethod
strlen=length(testList);
if testType == 0
     disp('Getting data without running tests')
end
nChan={};
dttMethod={};
for i = 1:strlen
    testFile = fullfile(testPath,testList{i});  
    if testType == 0
        dttMeth='5';
    else
        dttMeth='10';
    end    
    teststring = horzcat('python',' ',pyScript,' ',testFile,' ',workPath,' ','0',' ','0',' ',dttMeth);
    system(teststring);
%
    inputvals = importdata(tempInFile);
    delete(tempInFile);
    nChan{i}=inputvals(2);
    dttMethod{i}=inputvals(1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
strlen=length(testList);
for i = 1:strlen
   if length(commentList)==1
       comment = commentList;
   else
       comment = commentList{i};
   end
   fprintf('Looking at test %s',testList{i});
   testFile = fullfile(testPath,testList{i});
   saveFile = fullfile(outputPath,outputList{i});

   [~,timeNow] = system('tconvert now');
   timeStr = timeNow(1:10);
   testXmlName = horzcat(timeStr,'-',testList{i});
   testXmlFile = fullfile(workPath,testXmlName); 
   copyfile(testFile,testXmlFile);

   methStr = num2str(dttMethod{i});
   nchStr = num2str(nChan{i});
   switch testType
% testType = 0 - do test with python, but just read data
       case 0
           runMeth = '0';
% testType = 1 - do test with diaggui
       case 1
           filecommand=horzcat('diaggui -open ',testXmlFile,' &');
           system(filecommand);
           cont = input('Press enter after saving .xml file');
           runMeth = '0';
% testType = 2 - do test with diag using python
       case 2
           runMeth = '1'; 
       otherwise
           return
   end
% call dtt2mlab.py to run test
   teststring = horzcat('python',' ',pyScript,' ',testXmlFile,' ',workPath,' ',nchStr,' ',runMeth,' ',methStr);
   system(teststring);

% get number of channels/columns
   A = importdata(tempDataFile); 
   [rows,cols] = size(A);
% open data file
   fid = fopen(tempDataFile,'r');
   fid2 = fopen(tempParamFile,'r');
   fltstrng = '%f ';
   format=repmat(fltstrng,1,cols);
   data = textscan(fid, format);
   param = textscan(fid2, '%s','delimiter','\n');
   [h,w] = size(data);
   f=data{1};
   if dttMethod{i} == 0
      ps=[];
      for colnum = 2:nChan{i}+1
         if colnum <= w
            ps=[ps,data{colnum}];
         end
      end
      coh=[];
      for colnum = nChan{i}+2:nChan{i}*2+1
         if colnum <= w
            coh=[coh,data{colnum}];
         end
      end
      tf=[];
      for colnum = nChan{i}*2+2:2:nChan{i}*4+1
        if colnum <= w
            tf=[tf,complex(data{colnum},-data{colnum+1})];
        end
      end
      result = struct('f',f,'tf',tf,'ps',ps,'coh',coh,'param',param,'comment',comment);
   
   elseif dttMethod{i} == 1
      coh=[];
      for colnum = 2:nChan{i}+1
         if colnum <= w
            coh=[coh,data{colnum}];
         end
      end
     tf=[];
     for colnum = nChan{i}+2:2:nChan{i}*3+1
        if colnum <= w
           tf=[tf,complex(data{colnum},data{colnum+1})];
        end
     end
     result = struct('f',f,'tf',tf,'coh',coh,'param',param,'comment',comment);
   end
   save(saveFile,'result');
   if testType == 0
       delete(testXmlFile);
   end
end
delete(tempInFile);
delete(tempDataFile);
delete(tempParamFile);
if length(testList) == 1
    clearvars -except result
else
    clearvars
end