%%newdir = '../../DecodedResults/';

%cd newdir;
%% START 
clear;
pbfiles = dir('*.mat');

timeDT = [];
groundFault = [];
mtimeDT = [];
mgroundFault = [];
sdgroundFault = [];
xbtime = [];
xbroll = [];
xbpitch = [];
xbyaw = [];
pctime = [];
pcpres = [];

%Now go through all of them. 
for i = 1:length(pbfiles) 
    if strfind(pbfiles(i).name,'_raw.mat') >0
    else continue;
    end
%     if strfind(pbfiles(i).name,'2016.03.15') > 0
%     elseif strfind(pbfiles(i).name,'2016.03.16') > 0
%     elseif strfind(pbfiles(i).name,'2016.03.17') > 0
%     elseif strfind(pbfiles(i).name,'2016.03.18') > 0
%     else 
%         continue;
%     end
    if pbfiles(i).datenum < datenum('28-Oct-2018')
        continue;
    end
    
    disp(['Loading ' pbfiles(i).name]);
    
    load(pbfiles(i).name);
    timeDT = [timeDT;BC_Time];
    groundFault = [groundFault;BC_GroundFault];
    
    mtimeDT = [mtimeDT;mean(BC_Time)];
    mgroundFault = [mgroundFault;mean(BC_GroundFault)];
    sdgroundFault = [sdgroundFault;std(BC_GroundFault)];
    
    xbtime = [xbtime; XB_Time];
    xbroll = [xbroll; XB_RollAng];
    xbpitch = [xbpitch; XB_PitchAng];
    xbyaw = [xbyaw; XB_YawAng];
    
    pctime = [pctime; PC_Time];
    pcpres = [pcpres; PC_DiffPress];
    
    clear Time_DateNum;
    clear BC_GroundFault;
    
end
%% choose what to plot comment for regular 
%groundFault = mgroundFault;
%timeDT = mtimeDT;

% %% First fix the zeros to numbers that 
idx = find(groundFault == 0);
groundFault(idx) = 0.2372;
% 
% % strip NaN
idx = ~isnan(groundFault);
% 
% % adjust to PDT
timeDT = timeDT - datenum(0,0,0,7,0,0);
xbtime = xbtime - datenum(0,0,0,7,0,0);
pctime = pctime - datenum(0,0,0,7,0,0);
timeRf = timeDT;
% %%convert the gf numbers to faults. 
Rf = 20*120./(20./max(groundFault(idx)).*groundFault(idx)) - 120;
timeRf = timeDT(idx);

%% moving avg
mRf = movmean(Rf,30001);

mpctime = movmean(pctime,3001);
mpcpres = movmean(pcpres,3001);
%% plotting
semilogy(datetime(timeRf,'ConvertFrom','datenum'),Rf,'.',...
    datetime(timeRf,'ConvertFrom','datenum'),mRf);
datetick('x','mm/dd-HHAM');
ylabel('Rf (k{\Omega})');
grid on


% plot(datetime(pctime,'ConvertFrom','datenum'), pcpres ,'.',...
%     datetime(mpctime,'ConvertFrom','datenum'), mpcpres,'-');
% ylabel('Pressure (PSIG)');
% xlabel('Time (UTC)')
% title('Entire Deployment Comp Pressure Measurements');

%figure; 
%plot(timeDT,groundFault)

