%% Path to where float data is stored 
%dataDirPath = '/Volumes/Chem/ARGO_PROCESSING/DATA/PH_DIAGNOSTICS/DATA/'; %Mac
dataDirPath = '\\atlas\Chem\ARGO_PROCESSING\DATA\PH_DIAGNOSTICS\DATA\'; %PC 

%Path to Where output figures will be stored
fpath = '\\atlas\Chem\DuraFET\GDF Deployment QC\'; %PC
fpath_jpeg ='\\atlas\Chem\DuraFET\GDF Deployment QC\jpeg_copies\'; %PC

%fpath ='/Volumes/Chem/DuraFET/GDF Deployment QC/';  %Mac
%fpath_jpeg = '/Volumes/Chem/DuraFET/GDF Deployment QC/jpeg_copies/'; %Mac

%% WMO Float numbers with GDF sensors deployed
WMO_floatNums = ["5906441" "5906507" "5906529" "5906548" "5906549" "5906550" "5906560" "5906561" "5906576"];

varNames = ["SDN",      "Station",	"Lon_degE",	"Lat_degN",	"QF_LatLon", "Pressure_dbar", "QF_Pressure_dbar", "Temperature_degC", "QF_Temperature_degC"];
varNames = [varNames, "Salinity_pss", "QF_Salinity_pss", "InternalTemp_degC", "QF_InternalTemp_degC", "Internal_Humidity_percent"	"QF_Internal_Humidity_percent"];
varNames = [varNames, "System_Volts_V",	"QF_System_Volts_V",	"System_Amps_Amps",	"QF_System_Amps_Amps",	"Backup_Batt_V",	"QF_Backup_Batt_V",	"Vrs_V",	"QF_Vrs_V"];
varNames = [varNames, "std_Vrs_V",	"QF_std_Vrs_V",	"Vk_V",	    "QF_Vk_V",	"std_Vk_V",	"QF_std_Vk_V",	"Ik_Amps",	"QF_Ik_Amps",	"Ib_Amps",	"QF_Ib_Amps", "pH_tot",	"QF_pH_tot"];
varNames = [varNames,  "DF_SN",     "QF_DF_SN", "Stem_version"	"QF_Stem_version", "K2",        "QF_K2",        "K0",	    "QF_K0",    "MSC_Version"	"QF_MSC_Version"];

%% loop through the WMO float numbers to parse the data
for WMO_float=1:length(WMO_floatNums)
    fullPath(WMO_float) = strcat(dataDirPath, WMO_floatNums(WMO_float), '_DURA.TXT');
    dataTemp = get_FloatViz_data(fullPath(WMO_float));
    dataTable{WMO_float} = array2table(dataTemp.data, 'VariableNames', varNames);
    dataTable{WMO_float}.dt = datetime(dataTable{WMO_float}.SDN, 'ConvertFrom', 'datenum');
    dataTable{WMO_float}.elapsed_time =dataTable{WMO_float}.('dt') - dataTable{WMO_float}.('dt')(1);

%loop through the parsed data to take means of each profile ('Station') for
%each float
     for profile=1:max(dataTable{WMO_float}.Station)
     indexes = find(dataTable{WMO_float}.Station == profile);
     profile_date(profile) = mean(dataTable{WMO_float}.dt(indexes));
     stdDevVrsMean(profile) = mean(dataTable{WMO_float}.std_Vrs_V(indexes));
     stdDevVkMean(profile) = mean(dataTable{WMO_float}.std_Vk_V(indexes));
     end
     
%% Plot the Mean Standard Deviaton of VRS and VK against profile date
    f1 = figure('unit', 'inch', 'position',[1 1 16 9]);
    plot(profile_date,stdDevVkMean,LineWidth = 6);
    hold on; 
    plot(profile_date,stdDevVkMean,'o', 'MarkerSize',10,'MarkerFaceColor',"#0072BD",'Color',"#0072BD");
    hold on;
    ax = gca; 
    ax.FontSize = 16;
    hold on;
    plot(profile_date,stdDevVrsMean,LineWidth =6,Color= [0.9290 0.6940 0.1250]);
    hold on;
    plot(profile_date,stdDevVrsMean,'o', 'MarkerSize',10,'MarkerFaceColor',[0.9290 0.6940 0.1250],'Color',[0.9290 0.6940 0.1250]);
    ax = gca; 
    ax.FontSize = 16;
    title("Mean Standard Deviation of VK and VRS over time Float: " +WMO_floatNums{WMO_float},'Fontsize',24)
    xlabel("Profile Date",'FontSize',20)
    ylabel("Mean Standard Deviaiton (V)", 'FontSize',20)
    legend("VK STD",'',"VRS STD")
    saveas(f1, [fpath,WMO_floatNums{WMO_float},'_std_trends.fig']);
    saveas(f1,[fpath_jpeg,WMO_floatNums{WMO_float},'_std_trends.jpeg'])
    close(f1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plot VRS, VK, VRS STD, and VK STD profiles for each float
    f2 = figure ('unit', 'inch', 'position',[1 1 16 9]);
    t = tiledlayout(2,3,'TileSpacing','compact');
    
    % VRS vs Depth
    nexttile;        
     hold on; box on;
     ax = gca; 
     ax.FontSize = 16;
     for k = 1:max(dataTable{WMO_float}.Station)
         indexes =find(dataTable{WMO_float}.Station == k);
         plot(dataTable{WMO_float}.Vrs_V(indexes),dataTable{WMO_float}.Pressure_dbar(indexes),LineWidth=3);
         %scatter(dataTable{WMO_float}.Vrs_V(indexes), dataTable{WMO_float}.Pressure_dbar(indexes), 36, dataTable{WMO_float}.Station(indexes),'filled','o');
         xlabel("VRS (Volts)",'FontSize',18);
         axis ij;
         clim([1,max(dataTable{WMO_float}.Station)])
         colormap(parula(max(dataTable{WMO_float}.Station)))
         colororder(parula)
     end
    
    % VK vs Depth
    nexttile;       
     hold on; box on;
     ax = gca; 
     ax.FontSize = 16;
     for k = 1:max(dataTable{WMO_float}.Station)
         indexes =find(dataTable{WMO_float}.Station == k);
         %scatter(dataTable{WMO_float}.Vk_V, dataTable{WMO_float}.Pressure_dbar,36,dataTable{WMO_float}.Station,'filled','o');
         plot(dataTable{WMO_float}.Vk_V(indexes),dataTable{WMO_float}.Pressure_dbar(indexes),LineWidth=3);
         xlabel("VK (Volts)", 'FontSize',18);
         axis ij;
         clim([1,max(dataTable{WMO_float}.Station)])
         colormap(parula(max(dataTable{WMO_float}.Station)))
         colororder(parula)
     end
    % IK vs Depth
    nexttile; 
     hold on; box on;
     ax = gca; 
     ax.FontSize = 16;
      for k = 1:max(dataTable{WMO_float}.Station)
         indexes =find(dataTable{WMO_float}.Station == k);
         %plot(dataTable{WMO_float}.Ik_Amps(indexes),dataTable{WMO_float}.Pressure_dbar(indexes),LineWidth=3);
         scatter(dataTable{WMO_float}.Ik_Amps,dataTable{WMO_float}.Pressure_dbar,36,dataTable{WMO_float}.Station,'filled','o');
         axis ij;
         xlabel('IK Amps','FontSize',18);
         clim([1,max(dataTable{WMO_float}.Station)]);
         colormap(parula(max(dataTable{WMO_float}.Station)));
         colororder(parula);
      end

    % VRS STD vs Depth
    nexttile         
     hold on; box on;
     ax = gca; 
     ax.FontSize = 16;
      for k = 1:max(dataTable{WMO_float}.Station)
         indexes =find(dataTable{WMO_float}.Station == k);
         %scatter(dataTable{WMO_float}.std_Vrs_V, dataTable{WMO_float}.Pressure_dbar,36,dataTable{WMO_float}.Station,'filled','D');
         plot(dataTable{WMO_float}.std_Vrs_V(indexes),dataTable{WMO_float}.Pressure_dbar(indexes),'--',LineWidth=3);
         xlabel("VRS Standard Deviation",'FontSize',18);
         axis ij;
         clim([1,max(dataTable{WMO_float}.Station)])
         colormap(parula(max(dataTable{WMO_float}.Station)))
         colororder(parula)
     end
    
    % VK Std vs Depth
    nexttile;         
     hold on; box on;
     ax = gca; 
     ax.FontSize = 16;
       for k = 1:max(dataTable{WMO_float}.Station)
         indexes =find(dataTable{WMO_float}.Station == k);
         %scatter(dataTable{WMO_float}.std_Vk_V, dataTable{WMO_float}.Pressure_dbar,36,dataTable{WMO_float}.Station,'filled','o')
         plot(dataTable{WMO_float}.std_Vk_V(indexes),dataTable{WMO_float}.Pressure_dbar(indexes),'--',LineWidth=3);
         xlabel("VK Standard Deviation",'FontSize',18);
         axis ij;
         clim([1,max(dataTable{WMO_float}.Station)])
         colormap(parula(max(dataTable{WMO_float}.Station)))
         colororder(parula)
       end
    
    % IB vs Depth
    nexttile; 
     hold on; box on;
     ax = gca; 
     ax.FontSize = 16;
        for k = 1:max(dataTable{WMO_float}.Station)
         indexes =find(dataTable{WMO_float}.Station == k);
         scatter(dataTable{WMO_float}.Ib_Amps,dataTable{WMO_float}.Pressure_dbar,36,dataTable{WMO_float}.Station,'filled','o');
         %plot(dataTable{WMO_float}.Ib_Amps(indexes),dataTable{WMO_float}.Pressure_dbar(indexes),LineWidth=3);
         xlabel('IB Amps','FontSize',18);
         axis ij;
         clim([1,max(dataTable{WMO_float}.Station)])
         colormap(parula(max(dataTable{WMO_float}.Station)))
         colororder(parula)
        end
    
    ylabel(t,"Pressure (dBar)",'Fontsize',30); 
    title(t,"Float: " + WMO_floatNums{WMO_float},'Fontsize',32,'Fontweight','bold');
    cb = colorbar;
    cb.Layout.Tile ='east';
    cb.Label.String = 'Profile';
    
    saveas(f2, [fpath,WMO_floatNums{WMO_float},'_profiles.fig']);
    saveas(f2, [fpath_jpeg,WMO_floatNums{WMO_float},'_profiles.jpeg'])
    close(f2);

end

%% -----------------------------------------------------------------------------------
% stray pieces of code not being used

% STEP THROUGH CYCLES

 % casts = unique(dataTable{1}.Station); % dura file data
 % for ct = 1:size(casts,1) % step through cycles
 %    t1 = dataTable{1}.Station == casts(ct); % dura file data
 %    tmp = dataTable{1}(t1,:);
 %    rtmp = size(tmp,1);
 % 
 % 
 % end


 % %initalize figure location
 % left = .1;
 % bottom = .6;
 % width = 0.2;
 % height = 0.2;
 % leftIncr = .1;
 % bottomIncr = -.1;

 % figure('Units', 'normalized', 'Position', [left bottom width height]);
 % plot(stdDevVrsMean);
 % left = left + leftIncr;
 % bottom = bottom + bottomIncr;
 % figure('Units', 'normalized','Position', [left bottom width height]);
 % plot(stdDevVkMean);


