function [ error ] = ConvertLogFiles(path,filename)


InterpPer = .1;


time_reference = datenum('1970','yyyy');



disp([path filename '.csv']);

%Load data file
M = csvread([path filename '.csv']);
M = M(2:end,:);  %Remove Header.

%Split out power converter data
idx = find(M(:,1) == 2);
if(~isempty(idx))
  PC_Time = M(idx,2)/86400+time_reference;  %Convert time to matlab datenum
  PC_RPM = M(idx,3);
  PC_RPM_StdDev = M(idx,4);
  PC_RPM_TargetStdDev = M(idx,5);
  PC_Voltage = M(idx,6);
  PC_Power = M(idx,7);
  PC_BattCurr = M(idx,8);
  PC_LoadCurr = M(idx,9);
  PC_Torque = M(idx,10); 
  PC_BridgeDC = M(idx,11);
  PC_LoadDC = M(idx,12);
  PC_DiffPress = M(idx,13);
  PC_Scale = M(idx,14);
  PC_Retract = M(idx,15);
  PC_TargVolt = M(idx,16);
  PC_Status = M(idx,17);
else
  PC_Time = NaN;
  PC_RPM = NaN;
  PC_RPM_StdDev = NaN;
  PC_RPM_TargetStdDev = NaN;
  PC_Voltage = NaN;
  PC_Power = NaN;
  PC_BattCurr = NaN;
  PC_LoadCurr = NaN;
  PC_Torque = NaN; 
  PC_BridgeDC = NaN;
  PC_LoadDC = NaN;
  PC_DiffPress = NaN;
  PC_Scale = NaN;
  PC_Retract = NaN;
  PC_TargVolt = NaN;
  PC_Status = NaN;
end

%Split out Battery Controller Data
idx = find(M(:,1) == 0);

if(~isempty(idx))
  BC_Time = M(idx,2)/86400+time_reference;  %Convert time to matlab datenum
  BC_Voltage = M(idx,18);
  BC_Ips = M(idx,19);
  BC_VBalance = M(idx,20);
  BC_VStopCharge = M(idx,21);
  BC_GroundFault = M(idx,22);
  BC_Spare = M(idx,23);
  BC_Status = M(idx,24);
else
  BC_Time = NaN;
  BC_Voltage = NaN;
  BC_Ips = NaN;
  BC_VBalance = NaN;
  BC_VStopCharge = NaN;
  BC_GroundFault = NaN;
  BC_Spare = NaN;
  BC_Status = NaN;
end


%Split out CrossBow Data
idx = find(M(:,1) == 3);
if(~isempty(idx))
  XB_Time = M(idx,2)/86400+time_reference; %Convert time to matlab datenum
  XB_Lat = M(idx,37);
  XB_Lon = M(idx,38);
else
  XB_Time = NaN;
  XB_Lat = NaN;
  XB_Lon = NaN;
end
%Split out Spring Controller Data
idx = find(M(:,1) == 1);

if(~isempty(idx))
  SC_Time = M(idx,2)/86400+time_reference;   %Convert time to matlab datenum
  SC_LoadCell = M(idx,41);
  SC_Range = M(idx,42);
  SC_UpperPressure = M(idx,43);
  SC_LowerPressure = M(idx,44);
  SC_Status = M(idx,45);
  SC_CTD_epoch = M(idx,46);
  SC_CTD_Conductivity = M(idx,47);
  SC_CTD_Temp = M(idx,48);
else
  SC_Time = NaN;
  SC_LoadCell = NaN;
  SC_Range = NaN;
  SC_UpperPressure = NaN;
  SC_LowerPressure = NaN;
  SC_Status = NaN;
  SC_CTD_epoch = NaN;
  SC_CTD_Conductivity = NaN;
  SC_CTD_Temp = NaN;
end

[path filename '_raw.mat']

save('-mat7-binary', [path filename '_raw.mat'], ...
                                         'PC_Time','PC_RPM','PC_RPM_StdDev','PC_RPM_TargetStdDev','PC_Voltage','PC_Power','PC_BattCurr','PC_LoadCurr','PC_Torque','PC_BridgeDC','PC_LoadDC','PC_DiffPress','PC_Scale','PC_Retract','PC_TargVolt','PC_Status', ...
                                         'BC_Time','BC_Voltage','BC_Ips','BC_VBalance','BC_VStopCharge','BC_GroundFault','BC_Spare','BC_Status', ...
                                         'XB_Time','XB_Lat','XB_Lon', ...
                                         'SC_Time','SC_LoadCell','SC_Range','SC_UpperPressure','SC_LowerPressure','SC_Status','SC_CTD_epoch','SC_CTD_Conductivity','SC_CTD_Temp');



%Interpolate all data to 10hz.
if(~isnan(PC_Time(1)) && ~isnan(PC_Time(end))) disp(['PC Time = ' datestr(PC_Time(1)) '  ->   ' datestr(PC_Time(end))]); end
if(~isnan(SC_Time(1)) && ~isnan(SC_Time(end))) disp(['SC Time = ' datestr(SC_Time(1)) '  ->   ' datestr(SC_Time(end))]); end
if(~isnan(BC_Time(1)) && ~isnan(BC_Time(end))) disp(['BC Time = ' datestr(BC_Time(1)) '  ->   ' datestr(BC_Time(end))]); end
if(~isnan(XB_Time(1)) && ~isnan(XB_Time(end))) disp(['XB Time = ' datestr(XB_Time(1)) '  ->   ' datestr(XB_Time(end))]); end

StartDateNum = min([PC_Time(1) BC_Time(1) XB_Time(1) SC_Time(1)]);
StopDateNum = max([PC_Time(end) BC_Time(end) XB_Time(end) SC_Time(end)]);
Time_DateNum = StartDateNum:InterpPer/(86400):StopDateNum;

%Time_DateNum

if(~isnan(Time_DateNum(1)) && ~isnan(Time_DateNum(end)))  disp(['Interpolated Time = ' datestr(Time_DateNum(1)) '  ->   ' datestr(Time_DateNum(end))]);  end

%Interpolate Power Converter Data
disp('About to interp PC data');
if(~isnan(PC_Time))
  PC_RPM = interp1(PC_Time,PC_RPM,Time_DateNum); 
  PC_RPM_StdDev       = interp1(PC_Time,PC_RPM_StdDev,Time_DateNum);
  PC_RPM_TargetStdDev = interp1(PC_Time,PC_RPM_TargetStdDev,Time_DateNum);
  PC_Voltage = interp1(PC_Time,PC_Voltage,Time_DateNum);
  PC_Power = interp1(PC_Time,PC_Power,Time_DateNum);
  PC_BattCurr = interp1(PC_Time,PC_BattCurr,Time_DateNum);
  PC_LoadCurr = interp1(PC_Time,PC_LoadCurr,Time_DateNum);
  PC_Torque = interp1(PC_Time,PC_Torque,Time_DateNum);
  PC_BridgeDC= interp1(PC_Time,PC_BridgeDC,Time_DateNum);
  PC_LoadDC = interp1(PC_Time,PC_LoadDC,Time_DateNum);
  PC_DiffPress = interp1(PC_Time,PC_DiffPress,Time_DateNum);
  PC_Scale = interp1(PC_Time,PC_Scale,Time_DateNum);
  PC_Retract = interp1(PC_Time,PC_Retract,Time_DateNum);
  PC_TargVolt = interp1(PC_Time,PC_TargVolt,Time_DateNum);
  PC_Status = interp1(PC_Time,PC_Status,Time_DateNum);
else
  PC_RPM = NaN(size(Time_DateNum));
  PC_RPM_StdDev       = NaN(size(Time_DateNum));
  PC_RPM_TargetStdDev = NaN(size(Time_DateNum));
  PC_Voltage = NaN(size(Time_DateNum));
  PC_Power = NaN(size(Time_DateNum));
  PC_BattCurr = NaN(size(Time_DateNum));
  PC_LoadCurr = NaN(size(Time_DateNum));
  PC_Torque = NaN(size(Time_DateNum));
  PC_BridgeDC= NaN(size(Time_DateNum));
  PC_LoadDC = NaN(size(Time_DateNum));
  PC_DiffPress = NaN(size(Time_DateNum));
  PC_Scale = NaN(size(Time_DateNum));
  PC_Retract = NaN(size(Time_DateNum));
  PC_TargVolt = NaN(size(Time_DateNum));
  PC_Status = NaN(size(Time_DateNum));
end

disp('About to interp BC data');
%Interpolate Battery Controller Data
if(~isnan(BC_Time))
  BC_Voltage = interp1(BC_Time,BC_Voltage,Time_DateNum);
  BC_Ips = interp1(BC_Time,BC_Ips,Time_DateNum);
  BC_VBalance = interp1(BC_Time,BC_VBalance,Time_DateNum);
  BC_VStopCharge = interp1(BC_Time,BC_VStopCharge,Time_DateNum);
  BC_GroundFault = interp1(BC_Time,BC_GroundFault,Time_DateNum);
  BC_Spare = interp1(BC_Time,BC_Spare,Time_DateNum);
  BC_Status = interp1(BC_Time,BC_Status,Time_DateNum);
else
  BC_Voltage = NaN(size(Time_DateNum));
  BC_Ips = NaN(size(Time_DateNum));
  BC_VBalance = NaN(size(Time_DateNum));
  BC_VStopCharge = NaN(size(Time_DateNum));
  BC_GroundFault = NaN(size(Time_DateNum));
  BC_Spare = NaN(size(Time_DateNum));
  BC_Status = NaN(size(Time_DateNum));
end

disp('About to interp XB data');
%Interpolate CrossBow Data
if(~isnan(XB_Time))
  XB_Lat = interp1(XB_Time,XB_Lat,Time_DateNum);
  XB_Lon = interp1(XB_Time,XB_Lon,Time_DateNum);
else
  XB_Lat = NaN(size(Time_DateNum));
  XB_Lon = NaN(size(Time_DateNum));
end

disp('About to interp SC data');
%Interpolate Spring Controller Data
if(~isnan(SC_Time))
  SC_LoadCell = interp1(SC_Time,SC_LoadCell,Time_DateNum);
  SC_Range = interp1(SC_Time,SC_Range,Time_DateNum);
  SC_UpperPressure = interp1(SC_Time,SC_UpperPressure,Time_DateNum);
  SC_LowerPressure = interp1(SC_Time,SC_LowerPressure,Time_DateNum);
  SC_Status = interp1(SC_Time,SC_Status,Time_DateNum);
  SC_CTD_epoch = interp1(SC_Time,SC_CTD_epoch,Time_DateNum);
  SC_CTD_Conductivity = interp1(SC_Time,SC_CTD_Conductivity,Time_DateNum);
  SC_CTD_Temp = interp1(SC_Time,SC_CTD_Temp,Time_DateNum);
else
  SC_LoadCell = NaN(size(Time_DateNum));
  SC_Range = NaN(size(Time_DateNum));
  SC_UpperPressure = NaN(size(Time_DateNum));
  SC_LowerPressure = NaN(size(Time_DateNum));
  SC_Status = NaN(size(Time_DateNum));
  SC_CTD_epoch = NaN(size(Time_DateNum));
  SC_CTD_Conductivity = NaN(size(Time_DateNum));
  SC_CTD_Temp = NaN(size(Time_DateNum));
end

[path filename 'mat']

save('-mat7-binary',[path filename '.mat'],'Time_DateNum',  ...
                                    'PC_RPM','PC_RPM_StdDev','PC_RPM_TargetStdDev','PC_Voltage','PC_Power','PC_BattCurr','PC_LoadCurr','PC_Torque','PC_BridgeDC','PC_LoadDC','PC_DiffPress','PC_Scale','PC_Retract','PC_TargVolt','PC_Status', ...
                                    'BC_Voltage','BC_Ips','BC_VBalance','BC_VStopCharge','BC_GroundFault','BC_Spare','BC_Status', ...
                                    'XB_Lat','XB_Lon', ...
                                    'SC_LoadCell','SC_Range','SC_UpperPressure','SC_LowerPressure','SC_Status','SC_CTD_epoch','SC_CTD_Conductivity','SC_CTD_Temp');
disp('interp .mat saved');

end

