 function [MatlabFilenames] = ConvertDataToMatlab(path,base_filename)


%close all
%clear all

%path = 'tmp/'
%base_filename = '2018.10.04T09.43.40'


persist_filename = [path 'PersistSC_SmootherData.mat'];

InterpPer = 1/50;


time_reference = datenum(1970,1,1);


%Load data file
[path base_filename '.csv']
if(isOctave)
    M = csvread([path base_filename '.csv']);   %For octave
    M = M(2:end,:);  %Remove Header.
else
   % cmd = ['wc -l ' path base_filename '.csv'];
   % [status, cmdout] = system(cmd);
   % scanCell = textscan(cmdout,'%u %s');
   % lineCount = scanCell{1};
   % if((status ~=1) && (lineCount  > 1))
    	M = csvread([path base_filename '.csv'],1,0); %Temporarily move  beyond first few values that seem to have a messed up timestamp.
   % else
   %     exit;
   % end	
end


%Remove any lines with a timestamp of zero
idx = find(M(:,2) > 0);
M = M(idx,:);

ControllerOrder = {'PC','BC','XB','SC','TC'};  %Indicates order in csv log file, left to right.  Names here also become varaible name prefixes.
ControllerSrcID = [   2,  0,   3,   1,   4];

PC_VarNames = {'RPM',...
    'Voltage',...
    'WindingCurrent',...
    'BattCurr',...
    'Status',...
    'LoadCurr',...    
    'TargVolt',... 
    'TargCurr',... 
    'DiffPress',...
    'RPMStdDev',...
    'Scale',...
    'Retract',...    
    'Torque',...
    'BiasCurrent',...
    'ChargeCurrentLimit',...
    'DrawCurrentLimit'
    };

BC_VarNames = {'Voltage',...
    'Ips',...
    'VBalance',...
    'VStopCharge',...
    'GroundFault',...
    'Hydrogen',...
    'Status',...
    };

XB_VarNames = {'RollAng',...
    'PitchAng',...
    'YawAng',...
    'XRate',...
    'YRate',...
    'ZRate',...
    'XAccel',...
    'YAccel',...
    'ZAccel',...
    'NorthVel',...
    'EastVel',...
    'DownVel',...
    'Lat',...
    'Lon',...
    'Alt',...
    'Temp',...   
    };

SC_VarNames = {'LoadCell',...
    'Range',...
    'UpperPressure',...
    'LowerPressure',...
    'Status',...
    'CTD_epoch',...
    'CTD_Conductivity',...
    'CTD_Temp',...
    };

TC_VarNames = {'PowerTimeOut',...
    'TetherVolt',...
    'BattVolt',...
    'Pressure',...
    'Qtn1',...
    'Qtn2',...
    'Qtn3',...
    'Qtn4',...
     'Mag1',...
    'Mag2',...
    'Mag3',...
    'Status',...
    'AngRate1',...
    'AngRate2',...
    'AngRate3',...
    'vpeStatus',...
    'Accel1',...
    'Accel2',...
    'Accel3',...
    'CommsTimeOut',...
    'MaxonStatus',...  //Temporary comment out until .csv file is fixed.
    'MotorCurr',...  
    'EncPos',...  
    };



k = 3;
kk = 1;
for i = 1:length(ControllerOrder)
    cmd_str = ['NumVars = length(' ControllerOrder{i} '_VarNames);']; eval(cmd_str);
    idx = find(M(:,1) == ControllerSrcID(i));
    if(~isempty(idx))
        cmd_str = [ControllerOrder{i} '_Time = M(idx,2)/86400+time_reference;'];  eval(cmd_str);
        SavedVariables{kk} = [ControllerOrder{i} '_Time'];  kk = kk+1;
        for j = 1:NumVars
            cmd_str = ['VarName = [''' ControllerOrder{i} '_'' ' ControllerOrder{i} '_VarNames{j}];']; eval(cmd_str);
            cmd_str = [VarName ' = M(idx,' num2str(k) ');']; eval(cmd_str); 
            SavedVariables{kk} = VarName; kk = kk+1;
            k = k+1;
        end
    else
        cmd_str = [ControllerOrder{i} '_Time = NaN;'];  eval(cmd_str);
        SavedVariables{kk} = [ControllerOrder{i} '_Time'];  kk = kk+1;
        for j = 1:NumVars
            cmd_str = ['VarName = [''' ControllerOrder{i} '_'' ' ControllerOrder{i} '_VarNames{j}];']; eval(cmd_str);
            cmd_str = [VarName ' = [''' ControllerOrder{i} '_'' ' ControllerOrder{i} '_VarNames{j}];']; eval(cmd_str);
            cmd_str = [VarName ' = NaN;']; eval(cmd_str);
            SavedVariables{kk} = VarName; kk = kk+1;
            k = k+1; 
        end
    end
end

%Perform Secondary Processing
[TC_StatusFields] = ParseTC_Status(TC_Status);
SC_LoadCellUnCorrected = SC_LoadCell;
%[SC_LoadCell] = CorrectLoadCell(SC_LoadCellUnCorrected);







MatlabFilenames{1} = [base_filename '_raw.mat'];


if(isOctave)
    Savecmd_str = ['save(''-mat7-binary'',[path MatlabFilenames{1}]'];
else
    Savecmd_str = ['save([path MatlabFilenames{1}]'];
end
for i = 1:length(ControllerOrder)
   Savecmd_str = [Savecmd_str ',''' ControllerOrder{i} '*''']; 
end
Savecmd_str = [Savecmd_str ');'];
disp(Savecmd_str);
eval(Savecmd_str);

%Process CTD Data and put results in temp directory, this operates on the raw data, not the interpolated data.
%if(length(SC_Time) > 1)
%    [UniqueValues , Means] = ProcessCTD_Data([path base_filename],SC_Time,SC_CTD_epoch,SC_CTD_Temp,SC_CTD_Conductivity);
%end





%Interpolate all data to 10hz.
MinCmd_str = 'StartDateNum = min([';
MaxCmd_str = 'StopDateNum = max([';
for i = 1:length(ControllerOrder)
    MinCmd_str = [MinCmd_str ControllerOrder{i} '_Time(1) '];
    MaxCmd_str = [MaxCmd_str ControllerOrder{i} '_Time(end) '];
end
disp(MaxCmd_str);
MinCmd_str = [MinCmd_str ']);']; eval(MinCmd_str);
MaxCmd_str = [MaxCmd_str ']);']; eval(MaxCmd_str);

disp(['StartDate = ' num2str(StartDateNum)]);
disp(['StopDate = ' num2str(StopDateNum)]);


Time_DateNum = StartDateNum:InterpPer/(86400):StopDateNum;

length(Time_DateNum)
%Time_DateNum
if(exist('Time_DateNum') && length(Time_DateNum > 0))
    if(~isnan(Time_DateNum(1)) && ~isnan(Time_DateNum(end)))  disp(['Interpolated Time = ' datestr(Time_DateNum(1)) '  ->   ' datestr(Time_DateNum(end))]);  end
else
    Time_DateNum = NaN;
    for i = 1:length(ControllerOrder)
       cmd_str = [ControllerOrder{i} '_Time = NaN;']; eval(cmd_str); 
    end
end

for i = 1:length(ControllerOrder)
    cmd_str = ['NumTimeSteps = length(' ControllerOrder{i} '_Time);'];   disp(cmd_str); eval(cmd_str);
  if(NumTimeSteps > 1)
    cmd_str = ['[' ControllerOrder{i} '_Time ia ic] = unique( ' ControllerOrder{i} '_Time);'];  disp(cmd_str); eval(cmd_str);
    cmd_str = ['NumVars = length(' ControllerOrder{i} '_VarNames);']; eval(cmd_str);
    for j = 1:NumVars
            cmd_str = ['VarName = [''' ControllerOrder{i} '_'' ' ControllerOrder{i} '_VarNames{j}];']; eval(cmd_str);
            cmd_str = [VarName ' = interp1(' ControllerOrder{i} '_Time,' VarName '(ia),Time_DateNum);'];
            eval(cmd_str); 
        end
  else
      cmd_str = ['NumVars = length(' ControllerOrder{i} '_VarNames);']; eval(cmd_str);
      for j = 1:NumVars
            cmd_str = ['VarName = [''' ControllerOrder{i} '_'' ' ControllerOrder{i} '_VarNames{j}];']; eval(cmd_str);
            cmd_str = [VarName ' = NaN(size(Time_DateNum));']; eval(cmd_str); 
        end    
      
  end
    
end

%Perform Secondary Processing on interpolated data
[TC_StatusFields] = ParseTC_Status(TC_Status);
SC_LoadCellUnCorrected = SC_LoadCell;
%[SC_LoadCell] = CorrectLoadCell(SC_LoadCellUnCorrected);


%if(exist(persist_filename))
%    load(persist_filename);
%else
%    SC_Persist.SC_OffSet = -30;
%    SC_Persist.SC_Slope = .027;
%end

%if (sum(~isnan(SC_Range)) > 0)  %Make sure it's not all NaNs
%[SC_Range_Smooth, SC_Slope, SC_OffSet] =  RangeSmoother(SC_Range,SC_UpperPressure,SC_LowerPressure,SC_Persist.SC_Slope,SC_Persist.SC_OffSet);

%SC_Persist.SC_OffSet = SC_OffSet(end);
%SC_Persist.SC_Slope = SC_Slope(end);
%else
%SC_Range_Smooth = SC_Range;
%SC_Slope = SC_Range;
%SC_OffSet = SC_Range;
%end

%save(persist_filename,'SC_Persist');



MatlabFilenames{2} = [base_filename '.mat'];

if(isOctave)
    Savecmd_str = ['save(''-mat7-binary'',[path MatlabFilenames{2}]'];
else
    Savecmd_str = ['save([path MatlabFilenames{2}]'];
end
for i = 1:length(ControllerOrder)
   Savecmd_str = [Savecmd_str ',''' ControllerOrder{i} '*''']; 
end
Savecmd_str = [Savecmd_str ', ''Time_DateNum'');'];
eval(Savecmd_str);

disp('interp .mat saved');

end

