classdef Glide < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                matlab.ui.Figure
        Toolbar                 matlab.ui.container.Toolbar
        previousprofile         matlab.ui.container.toolbar.PushTool
        nextprofile             matlab.ui.container.toolbar.PushTool
        TabGroup                matlab.ui.container.TabGroup
        Home                    matlab.ui.container.Tab
        GridLayout3             matlab.ui.container.GridLayout
        missionDropDown         matlab.ui.control.DropDown
        GLIDEAPPLabel           matlab.ui.control.Label
        NewMissionSetupButton   matlab.ui.control.Button
        UpdateButton            matlab.ui.control.Button
        Image                   matlab.ui.control.Image
        ProfileDataTab          matlab.ui.container.Tab
        GridLayout11            matlab.ui.container.GridLayout
        GridLayout16            matlab.ui.container.GridLayout
        YaxisDropDown           matlab.ui.control.DropDown
        YaxisDropDownLabel      matlab.ui.control.Label
        XaxisDropDown           matlab.ui.control.DropDown
        XaxisDropDownLabel      matlab.ui.control.Label
        UpdatePlotsButton       matlab.ui.control.StateButton
        GridLayout13            matlab.ui.container.GridLayout
        nprofsEditFieldLabel    matlab.ui.control.Label
        nprofsEditField         matlab.ui.control.NumericEditField
        profileLabel            matlab.ui.control.Label
        profEditField           matlab.ui.control.NumericEditField
        TabGroup2               matlab.ui.container.TabGroup
        BOGTab                  matlab.ui.container.Tab
        GridLayout10            matlab.ui.container.GridLayout
        YearsSpinner            matlab.ui.control.Spinner
        yearsCheckBox           matlab.ui.control.CheckBox
        MonthsSpinner           matlab.ui.control.Spinner
        monthsCheckBox          matlab.ui.control.CheckBox
        RadiusSpinner           matlab.ui.control.Spinner
        radiuskmCheckBox        matlab.ui.control.CheckBox
        LoadBOGButton           matlab.ui.control.Button
        compareCheckBox         matlab.ui.control.CheckBox
        FilterBOGButton         matlab.ui.control.Button
        SprayTab                matlab.ui.container.Tab
        GridLayout14            matlab.ui.container.GridLayout
        MinDepthSpinner         matlab.ui.control.Spinner
        MinDepthCheckBox        matlab.ui.control.CheckBox
        LoadIndexButton         matlab.ui.control.Button
        YearsSpinner_Spray      matlab.ui.control.Spinner
        yearsCheckBox_Spray     matlab.ui.control.CheckBox
        MonthsSpinner_Spray     matlab.ui.control.Spinner
        monthsCheckBox_Spray    matlab.ui.control.CheckBox
        RadiusSpinner_Spray     matlab.ui.control.Spinner
        radiuskmCheckBox_Spray  matlab.ui.control.CheckBox
        compareCheckBox_Spray   matlab.ui.control.CheckBox
        FilterSprayButton       matlab.ui.control.Button
        MapInfo                 matlab.ui.control.TextArea
        ProfileMap              matlab.ui.container.Panel
        pHin                    matlab.ui.control.UIAxes
        deltapH                 matlab.ui.control.UIAxes
        PAR                     matlab.ui.control.UIAxes
        PropertyProperty        matlab.ui.control.UIAxes
        doxy                    matlab.ui.control.UIAxes
        psal                    matlab.ui.control.UIAxes
        Vk_std                  matlab.ui.control.UIAxes
        Vrse_std                matlab.ui.control.UIAxes
        Ib                      matlab.ui.control.UIAxes
        Ik                      matlab.ui.control.UIAxes
        Vk                      matlab.ui.control.UIAxes
        Vrse                    matlab.ui.control.UIAxes
        temp                    matlab.ui.control.UIAxes
        Chla                    matlab.ui.control.UIAxes
        DielCyclesTab           matlab.ui.container.Tab
        GridLayout5             matlab.ui.container.GridLayout
        GridLayout7             matlab.ui.container.GridLayout
        ProfilesLabel           matlab.ui.control.Label
        dielend                 matlab.ui.control.Spinner
        dielstart               matlab.ui.control.Spinner
        GridLayout6             matlab.ui.container.GridLayout
        DepthsLabel             matlab.ui.control.Label
        depth3                  matlab.ui.control.Spinner
        depth2                  matlab.ui.control.Spinner
        depth1                  matlab.ui.control.Spinner
        DIC                     matlab.ui.control.UIAxes
        O2Anompcolor            matlab.ui.control.UIAxes
        DICpcolor               matlab.ui.control.UIAxes
        O2AnomDiel              matlab.ui.control.UIAxes
        LargeMapTab             matlab.ui.container.Tab
        Panel                   matlab.ui.container.Panel
        RemarksTab              matlab.ui.container.Tab
        GridLayout4             matlab.ui.container.GridLayout
        GridLayout9             matlab.ui.container.GridLayout
        AddRemarkButton         matlab.ui.control.Button
        ProfSelect              matlab.ui.control.Spinner
        DropDown                matlab.ui.control.DropDown
        RemarksEditField        matlab.ui.control.EditField
        TextArea                matlab.ui.control.TextArea
    end

    
    properties (Access = private)
        mission % Current mission ID
        s % Spray science data
        eng % Spray engineering data
        filepath % path to open sat file
        n % Current profile
        n_sdn % Total number of profiles
        nprofs % nprofs to display on figures
        MainMap % Main map object
        SubMap1 % Profile data map
        B % BOG data
        BOG2Plot % Filtered BOG data to plot
        GliderIndex % Glider Index file
        Spray2Plot % Spray profiles to compare
        sc % struct of profiles with data to compare
        basepath % Path to Spray Data folder in project libraray
        remarksfilepath % Path to remarks file
    end
    
    methods (Access = private)
        
        function LoadData(app)
            app.mission = app.missionDropDown.Value;
    
            try
            app.filepath = fullfile(app.basepath,char(app.mission), 'sat_files', [char(app.mission), '_sat.mat']);
           
            catch
                disp({'no active mission',[app.filepath],[app.mission]})
            end
            
            load(app.filepath,'s','eng');
            app.s = s;
            app.eng = eng;
            app.n_sdn = length(app.s.sdn);
            app.n = app.n_sdn; % Set n initially equal to n_sdn

        end
        
        function MapFig(app)
            close('all')

            % Importing location info
            mosslat = 36.80594819607352;
            mosslon = -121.786377002975;
            CalCOFI67 = ...
            [50 36.7869600000000 -122.056330000000
            55 36.6202900000000 -122.414820000000
            60 36.4536300000000 -122.772540000000
            65 36.2869600000000 -123.129480000000
            70 36.1202900000000 -123.485650000000
            75 35.9536300000000 -123.841060000000
            80 35.7869600000000 -124.195710000000
            85 35.6202900000000 -124.549620000000
            90 35.4536300000000 -124.902780000000
            95 35.2869600000000 -125.255200000000
            100 35.1202900000000 -125.606890000000];
            CalCOFI67Description = strsplit(num2str(CalCOFI67(:,1)')); 
            
            % M1
            M1Lat = 36.75901;
            M1Lon = -122.02019;
            
            % Plotting location history
            if app.n == 0 || app.n == 1
                SprayLatHist = [];
                SprayLonHist = [];
            elseif app.n > 1 && app.n <= 10
                histprofs2plot = app.n - 1;
                % Spray location history
                SprayLatHist = app.s.lat(app.n-histprofs2plot:app.n-1);
                SprayLonHist = app.s.lon(app.n-histprofs2plot:app.n-1);
            elseif app.n > 10
                % Spray location history
                histprofs2plot = 10;
                SprayLatHist = app.s.lat(app.n-histprofs2plot:app.n-1);
                SprayLonHist = app.s.lon(app.n-histprofs2plot:app.n-1);
            end

            % % if app.n >= 10
            % %     histprofs2plot = 10;
            % % elseif app.n < 10 && app.n ~= 1
            % %     histprofs2plot = app.n - 1;
            % % elseif app.n == 1
            % %     histprofs2plot = 1;
            % % end
            % % 
            % % % Spray location history
            % % SprayLatHist = app.s.lat(app.n-histprofs2plot:app.n-1);
            % % SprayLonHist = app.s.lon(app.n-histprofs2plot:app.n-1);

            % Most recent location
            SprayLat = app.s.lat(app.n);
            SprayLon = app.s.lon(app.n);

            % Information to display next to map
            date = datetime(datevec(app.s.sdn_(2,:))); 
            date.TimeZone = "UTC";
            date.TimeZone = "America/Los_Angeles"; % change timezone to PST
            app.MapInfo.Value = {
                ['Mission: ' app.s.depID]
                ['Start Date: ' datestr(min(date(:))) ' PST']
                ['Last Profile: ' datestr(date(end)) ' PST']
                ['Current Profile: ', num2str(app.n)]
                ['Total Profiles: ' num2str(length(app.s.lat))]
                ['Current location: ' num2str(app.s.lat(app.n)) ', '...
                num2str(app.s.lon(app.n))]
                ['Max Depth: ' num2str(max(app.s.depth(:,app.n))) ' m']...
                };
            
            % Hold map axes
            hold(app.MainMap,'on')
            hold(app.SubMap1,'on')
            
            % Clear map axes
            cla(app.MainMap)
            cla(app.SubMap1)
            
            % Adding data to maps
            geoplot(app.MainMap, CalCOFI67(:,2), CalCOFI67(:,3), 's', 'MarkerFaceColor',...
                '#FFFF00', 'MarkerEdgeColor', 'none', 'DisplayName',...
                'CalCOFI Line 67', 'MarkerSize', 10);
            geoplot(app.SubMap1, CalCOFI67(:,2), CalCOFI67(:,3), 's', 'MarkerFaceColor',...
                '#FFFF00', 'MarkerEdgeColor', 'none', 'DisplayName',...
                'CalCOFI Line 67', 'MarkerSize', 10);
            
            % Adding CalCOFI station names to maps
            textoffset = -0.01; % for location display
            for i = 1:length(CalCOFI67Description)
                text(app.MainMap, CalCOFI67(i,2) + textoffset, CalCOFI67(i,3) + textoffset,...
                    CalCOFI67Description{i}, 'Color', '#FFFF00', 'FontSize',...
                    10, 'HorizontalAlignment', 'right');
                text(app.SubMap1, CalCOFI67(i,2) + textoffset, CalCOFI67(i,3) + textoffset,...
                    CalCOFI67Description{i}, 'Color', '#FFFF00', 'FontSize',...
                    10, 'HorizontalAlignment', 'right');
            end
            
            % Adding more data to maps
            geoplot(app.MainMap, M1Lat, M1Lon, 'p', 'Color', '#00FFFF', 'MarkerFaceColor',...
                '#00FFFF', 'DisplayName', 'M1 Mooring', 'MarkerSize', 10);
            geoplot(app.SubMap1, M1Lat, M1Lon, 'p', 'Color', '#00FFFF', 'MarkerFaceColor',...
                '#00FFFF', 'DisplayName', 'M1 Mooring', 'MarkerSize', 10);
            geoplot(app.MainMap, mosslat, mosslon, "^", 'Color', '#FF00FF',...
                'MarkerFaceColor', '#FF00FF', 'DisplayName', 'MBARI', 'MarkerSize', 10);
            geoplot(app.SubMap1, mosslat, mosslon, "^", 'Color', '#FF00FF',...
                'MarkerFaceColor', '#FF00FF', 'DisplayName', 'MBARI', 'MarkerSize', 10);
            geoplot(app.MainMap, SprayLat, SprayLon, 'o', 'MarkerFaceColor', '#FFA500', 'MarkerEdgeColor',...
                '#FFA500', 'DisplayName', 'Spray 034 Location', 'MarkerSize', 10);
            geoplot(app.SubMap1, SprayLat, SprayLon, 'o', 'MarkerFaceColor', '#FFA500', 'MarkerEdgeColor',...
                '#FFA500', 'DisplayName', 'Spray 034 Location', 'MarkerSize', 10);
            if ~isempty(SprayLatHist)
            geoplot(app.MainMap, SprayLatHist, SprayLonHist, '.', 'Color', '#D3D3D3',...
                'DisplayName', 'Spray 034 Location History', 'MarkerSize', 10);
            geoplot(app.SubMap1, SprayLatHist, SprayLonHist, '.', 'Color', '#D3D3D3',...
                'DisplayName', 'Spray 034 Location History', 'MarkerSize', 10);
            end
            % Map titles
            title(app.MainMap, ['Profile: ' num2str(app.n)])
            
            % title(app.SubMap1, ['Profile: ' num2str(app.n)])
            hold(app.MainMap, 'off');
            hold(app.SubMap1, 'off');
            
            % Set map zoom bounds
            ZoomRadius = 25;
            offset_km = ZoomRadius / 111;
            lat_min = SprayLat - offset_km;
            lat_max = SprayLat + offset_km;
            lon_min = SprayLon - offset_km / cosd(SprayLat);
            lon_max = SprayLon + offset_km / cosd(SprayLat);
            geolimits(app.MainMap, [lat_min lat_max], [lon_min lon_max]);
            geolimits(app.SubMap1, [lat_min lat_max], [lon_min lon_max]);
            app.MainMap.MapCenter = [SprayLat SprayLon];
            app.SubMap1.MapCenter = [SprayLat SprayLon];

            % Remove lat/lon labels
            % app.SubMap1
            
            % Normal condition legend
            legend(app.MainMap,{'Calcofi Stations','M1 Mooring','MBARI',['spray ', app.mission(4:6)], 'spray location history'})
            
            % Add BOG data to map and change legend
            if app.compareCheckBox.Value == 1
                [~, ii] = unique(app.BOG2Plot.DEC_LAT,"first","rows");
                BOG2PlotLatLon = app.BOG2Plot(ii,{'DEC_LAT','DEC_LONG','DATE_TIME'});
                hold(app.SubMap1,"on")
                geoplot(app.SubMap1,BOG2PlotLatLon.DEC_LAT,BOG2PlotLatLon.DEC_LONG,'>')
                legend(app.MainMap,{'Calcofi Stations','M1 Mooring','MBARI',['spray ', app.mission(4:6)], 'spray location history','BOG data'})
            end
            
            % Add spray data to map and change legend
            if app.compareCheckBox_Spray.Value == 1
                hold(app.SubMap1,"on")
                geoplot(app.SubMap1,app.sc.lat,app.sc.lon,'<')
                legend(app.MainMap,{'Calcofi Stations','M1 Mooring','MBARI',['spray ', app.mission(4:6)], 'spray location history','Spraydata'})
            end
            if app.compareCheckBox.Value == 1 && app.compareCheckBox_Spray.Value == 1
                legend(app.MainMap,{'Calcofi Stations','M1 Mooring','MBARI',['spray ', app.mission(4:6)], 'spray location history','BOG data','Spraydata'})
            end
        end
        
        function ProfileFigure(app,var,uifig)
            fname = 'avenir';
            fsize = 16;
            newcol = rgb('crimson'); % New profile color settings
            newlw = 3; % New line size
            profs2plot = size(app.s.pres,2)-app.nprofsEditField.Value:size(app.s.pres,2); % prof idxs
            preslimits = [0 round(max(max(app.s.pres(:,profs2plot))),1)+10]; % pres axis lims
            app.s.deltapH = app.s.pHin - app.s.pHin_canb; % Define deltapH
            app.s.deltapH_QC = app.s.pHin_QC; % QC field

            try
                var2plot = app.s.(var);
            catch
                var2plot = NaN;
            end
            try
                var2plot(app.s.([var '_QC'])~=0) = NaN;
            catch
                var2plot = app.s.(var);
            end
            figure('Visible','off')
            clf
            if app.nprofs >= 2
                plot(app.(uifig),var2plot(:,app.n-app.nprofs:app.n-1), app.s.pres(:,app.n-app.nprofs:app.n-1));
                hold(app.(uifig),"on")
                plot(app.(uifig),var2plot(:,app.n), app.s.pres(:,app.n),'Color', newcol,'Linewidth',newlw);
                hold(app.(uifig),"off")
            else
                plot(app.(uifig),var2plot(:,app.n), app.s.pres(:,app.n),'Color', newcol,'Linewidth',newlw);
            end

            % Check compare checkbox value and add another layer if 1
            bogvars = {'tc','psal','doxy','pHin'};
            if app.compareCheckBox.Value == 1 && ismember(var,bogvars)  
                hold(app.(uifig), "on")
                plot(app.(uifig), app.BOG2Plot.(var), app.BOG2Plot.pres, '.', 'Color', 'green');
                hold(app.(uifig), "off")
            end

            % Check compare checkbox value and add another layer if 1
            sprayvars = {'tc','psal','doxy','pH25atm'};
            if app.compareCheckBox_Spray.Value == 1 && ismember(var,sprayvars)  
                hold(app.(uifig), "on")
                plot(app.(uifig), app.sc.(var), app.sc.pres, '.', 'Color', 'blue',LineWidth=.1);
                hold(app.(uifig), "off")
            end

            app.(uifig).YDir = "reverse";
            app.(uifig).FontSize = fsize;
            app.(uifig).FontName = fname;
            xlim(app.(uifig),[-inf inf])   
            ylim(app.(uifig),preslimits);
            yticks(app.(uifig),'auto')
            xticks(app.(uifig),'auto')
            hold(app.(uifig),"off")
        end
        
        function UpdateFigs(app)
            close('all')
            ProfileFigure(app,'tc','temp')
            ProfileFigure(app,'psal','psal')
            ProfileFigure(app,'doxy','doxy')
            ProfileFigure(app,'opt','Chla')
            ProfileFigure(app,'pHin','pHin')
            ProfileFigure(app,'deltapH','deltapH')
            ProfileFigure(app,'PAR','PAR')
            ProfileFigure(app,'vrs','Vrse')
            ProfileFigure(app,'vrs_std','Vrse_std')
            ProfileFigure(app,'vk','Vk')
            ProfileFigure(app,'vk_std','Vk_std')
            ProfileFigure(app,'Ik','Ik')
            ProfileFigure(app,'Ib','Ib')
    end
        
    function O2Anom(app)
        try
            cla(app.O2AnomDiel)
            if ~isempty(app.dielstart.Value) || ~isempty(app.dielend.Value)
                idiel = app.dielstart.Value:app.dielend.Value;
            else
                idiel = app.n-30:app.n;
            end
            % Get vars
            app.s.o2satconc = reshape(calcO2sat(app.s.tc(:), app.s.psal(:)), size(app.s.pres));
            app.s.o2anom = app.s.doxy - app.s.o2satconc;
            
            date = datetime(datevec(app.s.sdn(idiel))); 
            date.TimeZone = "UTC"; % Set timezone for the datetime var type
            date.TimeZone = "America/Los_Angeles"; % change timezone to local
            
            %
            depthavg = mean(app.s.depth(:,idiel),2);
            % depthmax = max(depthavg);
            iupper1 = find(depthavg > app.depth1.Value-1 & depthavg < app.depth1.Value+1, 1);
            iupper2 = find(depthavg > app.depth2.Value-1 & depthavg < app.depth2.Value+1, 1);
            iupper3 = find(depthavg > app.depth3.Value-1 & depthavg < app.depth3.Value+1, 1);
            
            % plotting
            title(app.O2AnomDiel, [datestr(date(1)), ' to ', datestr(date(end))])
            subtitle(app.O2AnomDiel,['Profiles: ', num2str(idiel(1)), ' - ', num2str(idiel(end)), ', n = ', num2str(length(app.s.o2anom(1,idiel)))])
            ylabel(app.O2AnomDiel, 'O_2 Sat Anom [\mumol/kg]')
            % xlabel(app.O2AnomDiel, 'Time Local (PST)')
            
            % Define the colors
            colors = [1 0 0;   % Red
                      0 1 0;   % Green
                      0 0 1];  % Blue
            % Loop to plot with specified colors
            iupper = [iupper1, iupper2, iupper3];
            for i = 1:length(iupper)
                meano2anom = mean(app.s.o2anom(iupper(i), idiel), 1);
                plot(app.O2AnomDiel, date, meano2anom, 'o', 'MarkerFaceColor', colors(i, :), 'MarkerEdgeColor', colors(i, :))
                hold(app.O2AnomDiel, "on")
            end

            % % % % for iupper = [iupper1, iupper2, iupper3]
            % % % %     meano2anom = mean(app.s.o2anom(iupper,idiel),1);
            % % % %     plot(app.O2AnomDiel,date,meano2anom,'o','MarkerFaceColor', 'k', 'MarkerEdgeColor', 'k')
            % % % %     hold(app.O2AnomDiel,"on")
            % % % % end
            % title(app.O2AnomDiel,'O2 Anom Diel')
            % xticks
            datetickstart = date(1);
            datetickstart.Hour = 0;
            datetickstart.Minute = 0;
            datetickstart.Second = 0;
            datetickend = date(end);
            datetickend.Hour = 0;
            datetickend.Minute = 0;
            datetickend.Second = 0;
            
            if numel(datetickstart:.25:datetickend+1) < 15
                xticks(app.O2AnomDiel,datetickstart:.25:datetickend+1)
            else
                xticks(app.O2AnomDiel,datetickstart:.5:datetickend+1)
            end
            xtickformat(app.O2AnomDiel,'HH:mm')
            xtickangle(app.O2AnomDiel,45)
            % xlabel(tl,'Local time (hours:minutes)')
            legend(app.O2AnomDiel,num2str(depthavg(iupper1)),num2str(depthavg(iupper2)),num2str(depthavg(iupper3)), Location='southwest')
            grid(app.O2AnomDiel,"on")


            %
            iupper = 1:10;
            pcolor(app.O2Anompcolor,date,app.s.depth(iupper,idiel(1)),app.s.o2anom(iupper,idiel))
            shading(app.O2Anompcolor,"interp");
            % xticklabels(app.O2Anompcolor,[])
            cb1 = colorbar(app.O2Anompcolor);
            ylabel(cb1,'O_2 anom')
            ylabel(app.O2Anompcolor,'depth')
            if numel(datetickstart:.25:datetickend+1) < 15
                xticks(app.O2Anompcolor,datetickstart:.25:datetickend+1)
            else
                xticks(app.O2Anompcolor,datetickstart:.5:datetickend+1)
            end
            xtickformat(app.O2Anompcolor,'HH:mm')
            xtickangle(app.O2Anompcolor,45)
            set(app.O2Anompcolor,'YDIR','reverse')
            title(app.O2Anompcolor,'')
            xlabel(app.O2Anompcolor, 'Time Local (PST)')
        catch
            text(app.O2AnomDiel,.5,.5,'NO DIEL DATA DETECTED')
        end
    end
        
    function plotDIC(app)
        try
            cla(app.DIC)
            % dielmax = app.n;
            % dielstart = 300; % input
            % dielend = 350; % input
            if ~isempty(app.dielstart.Value) || ~isempty(app.dielend.Value)
                idiel = app.dielstart.Value:app.dielend.Value;
            else
                idiel = app.n-30:app.n;
            end
            % Get vars
            
            date = datetime(datevec(app.s.sdn(idiel))); 
            date.TimeZone = "UTC"; % Set timezone for the datetime var type
            date.TimeZone = "America/Los_Angeles"; % change timezone to local
            
            %
            depthavg = mean(app.s.depth(:,idiel),2);
            % depthmax = max(depthavg);
            iupper1 = find(depthavg > app.depth1.Value-1 & depthavg < app.depth1.Value+1, 1);
            iupper2 = find(depthavg > app.depth2.Value-1 & depthavg < app.depth2.Value+1, 1);
            iupper3 = find(depthavg > app.depth3.Value-1 & depthavg < app.depth3.Value+1, 1);
            
            % plotting
            % title(app.DIC, [datestr(date(1)), ' to ', datestr(date(end))])
            % subtitle(app.DIC,['Profiles: ', num2str(idiel(1)), ' - ', num2str(idiel(end)), ', n = ', num2str(length(app.s.o2anom(1,idiel)))])
            ylabel(app.DIC, 'DIC [\mumol/kg]')
            % xlabel(app.DIC, 'Time Local (PST)')
            % Define the colors
            colors = [1 0 0;   % Red
                      0 1 0;   % Green
                      0 0 1];  % Blue
            iupper = [iupper1, iupper2, iupper3];
            for i = 1:length(iupper)
                meanDIC = mean(app.s.dic_canb(iupper(i),idiel),1);
                plot(app.DIC,date,meanDIC,'o', 'MarkerFaceColor', colors(i, :), 'MarkerEdgeColor', colors(i, :))
                hold(app.DIC,"on")
            end

            % title(app.O2AnomDiel,'O2 Anom Diel')
            % xticks
            datetickstart = date(1);
            datetickstart.Hour = 0;
            datetickstart.Minute = 0;
            datetickstart.Second = 0;
            datetickend = date(end);
            datetickend.Hour = 0;
            datetickend.Minute = 0;
            datetickend.Second = 0;
            
            if numel(datetickstart:.25:datetickend+1) < 15
                xticks(app.DIC,datetickstart:.25:datetickend+1)
            else
                xticks(app.DIC,datetickstart:.5:datetickend+1)
            end
            xtickformat(app.DIC,'HH:mm')
            xtickangle(app.DIC,45)
            % xlabel(tl,'Local time (hours:minutes)')
            legend(app.DIC,num2str(depthavg(iupper1)),num2str(depthavg(iupper2)),num2str(depthavg(iupper3)), Location='southwest')
            grid(app.DIC,"on")


            iupper = 1:10;
            pcolor(app.DICpcolor,date,app.s.depth(iupper,idiel(1)),app.s.dic_canb(iupper,idiel))
            shading(app.DICpcolor,"interp");
            % xticklabels(app.O2Anompcolor,[])
            cb1 = colorbar(app.DICpcolor);
            ylabel(cb1,'DIC')
            ylabel(app.DICpcolor,'depth')
            if numel(datetickstart:.25:datetickend+1) < 15
                xticks(app.DICpcolor,datetickstart:.25:datetickend+1)
            else
                xticks(app.DICpcolor,datetickstart:.5:datetickend+1)
            end
            xtickformat(app.DICpcolor,'HH:mm')
            xtickangle(app.DICpcolor,45)
            set(app.DICpcolor,'YDIR','reverse')
            title(app.DICpcolor,'')
            xlabel(app.DICpcolor,'Time Local (PST)')

        catch
            text(app.DIC,.5,.5,'NO DIEL DATA DETECTED')
        end
    end
        
        
        function createRemark(app)
            remark = [char(app.DropDown.Value), ', ',...
                num2str(app.ProfSelect.Value), ', ', char(app.RemarksEditField.Value)];

            try
                fileId = fopen(app.remarksfilepath, 'a');
                fprintf(fileId, '%s\n', remark);
                fclose(fileId);
            catch
                disp('No remarks folder available')
            end
        end
        
        function readRemarkfile(app) % should save this filepath to app
            try
                % Read the file in the txtarea
                fileID = fopen(app.remarksfilepath, 'r');
                fileContent = fread(fileID, '*char')';
                fclose(fileID);
                
                % Display content in TextArea
                app.TextArea.Value = '';
                app.TextArea.Value = fileContent;
            catch
                app.TextArea.Value = 'No remarks folder available';
            end
        end
        
        function PropertyPropertyPlot(app,xvar,yvar)
            
            xvar2plot = app.s.(char(xvar))(:,app.n);
            yvar2plot = app.s.(char(yvar))(:,app.n);
            if length(xvar2plot) == length(yvar2plot)
                scatter(app.PropertyProperty,xvar2plot,yvar2plot)
            else
                text(app.PropertyProperty,1,1,'data length does not match')
            end
            xlabel(app.PropertyProperty,char(xvar))
            ylabel(app.PropertyProperty,char(yvar))
            title(app.PropertyProperty,[char(yvar), ' vs. ' char(xvar)])
            if ismember((char(yvar)),'depth')
                app.PropertyProperty.YDir = "reverse";
            end
            fname = 'avenir';
            fsize = 16;
            app.PropertyProperty.FontSize = fsize;
            app.PropertyProperty.FontName = fname;
            xlim(app.PropertyProperty,'auto')   
            ylim(app.PropertyProperty,'auto');
            yticks(app.PropertyProperty,'auto')
            xticks(app.PropertyProperty,'auto')
            hold(app.PropertyProperty,"off")
        end
        
        function UpdateAll(app)
            LoadData(app)
            MapFig(app)
            UpdateFigs(app)
            O2Anom(app)
            plotDIC(app)
        end
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            % Root directory for spray data
            app.basepath = fullfile('\\atlas.shore.mbari.org','ProjectLibrary','901805_Coastal_Biogeochemical_Sensing','Spray_Data');
            basemapName = 'WorldOceanBaseMap';
            url = 'https://services.arcgisonline.com/arcgis/rest/services/Ocean/World_Ocean_Base/MapServer/tile/{z}/{y}/{x}';
            
            % Add custom basemap with bathy data
            try
                removeCustomBasemap(basemapName); % Need to remove and add again each time for the setup to work properly
                addCustomBasemap(basemapName, url, 'Attribution', '© Esri');
            catch
                addCustomBasemap(basemapName, url, 'Attribution', '© Esri');
            end
            % Set up the map axes
            app.MainMap = geoaxes(app.Panel,"Basemap","WorldOceanBaseMap",Position=[0,0,1,1]);
            % Create the geoaxes with initial properties
            app.SubMap1 = geoaxes(app.ProfileMap, "Basemap", "WorldOceanBase",Position=[0, 0, 1, 1]);
            app.SubMap1.LatitudeLabel.String = '';
            app.SubMap1.LongitudeLabel.String = '';
            
            % Get mission IDs from directory
            missionIDs = GetMissionIDs(app.basepath);
            app.missionDropDown.Items = missionIDs;

            % Update all maps and figures
            UpdateAll(app)
            
            % Set up remarks filepath
            app.remarksfilepath = fullfile(app.basepath,app.mission,'deployment_remarks','remarks.txt');
            readRemarkfile(app)

            % Disable NewMissionSetup for first release
            app.NewMissionSetupButton.Enable = 'off';
        end

        % Callback function: previousprofile
        function previousprofileClicked(app, event)
            % Add previous profile button and update figures
            if app.n > 1
                app.n = app.n - 1;
            else
                app.n = 1;
            end
            UpdateFigs(app)
            MapFig(app)
        end

        % Callback function: nextprofile
        function nextprofileClicked(app, event)
            % Add next profile button and update figures
            if app.n < app.n_sdn
                app.n = app.n + 1;
            else
                app.n = app.n_sdn;
            end
            UpdateFigs(app)
            MapFig(app)
        end

        % Value changed function: profEditField
        function profEditFieldValueChanged(app, event)
            % Cannot view more profiles then exist
            if app.profEditField.Value <= app.n_sdn
                app.n = app.profEditField.Value;
            end
            UpdateFigs(app)
            MapFig(app)
        end

        % Value changed function: nprofsEditField
        function nprofsEditFieldValueChanged(app, event)
            % Make sure nprofs to plot is never 0
            app.nprofs = app.nprofsEditField.Value;
            if app.n - app.nprofs > 1
                UpdateFigs(app)
                MapFig(app)
            else
                app.nprofs = 1;
                UpdateFigs(app)
                MapFig(app)
            end
        end

        % Value changed function: depth1
        function depth1ValueChanged(app, event)
            % Diel cycles depth 1
            app.depth1.Limits = [0 inf];
            O2Anom(app)
            plotDIC(app)
        end

        % Value changed function: depth2
        function depth2ValueChanged(app, event)
            % Diel cycles depth 2
            app.depth2.Limits = [0 inf];
            O2Anom(app)
            plotDIC(app)
        end

        % Value changed function: depth3
        function depth3ValueChanged(app, event)
            % Diel cycles depth 3
            app.depth3.Limits = [0 inf];
            O2Anom(app)
            plotDIC(app)
        end

        % Value changed function: dielstart
        function dielstartValueChanged(app, event)
            % Limits are number of profiles
            app.dielstart.Limits = [0 app.n_sdn];
            O2Anom(app)
            plotDIC(app)
        end

        % Value changed function: dielend
        function dielendValueChanged(app, event)
            % Limits are number of profiles
            app.dielend.Limits = [0 app.n_sdn];
            O2Anom(app)
            plotDIC(app)
        end

        % Button pushed function: AddRemarkButton
        function AddRemarkButtonPushed(app, event)
            % Create new remark and read the updated file
            createRemark(app);
            readRemarkfile(app)
        end

        % Button pushed function: FilterBOGButton
        function FilterBOGButtonPushed(app, event)
            % Add BOG cruise data to plots according to filter criteria
            app.BOG2Plot = filterBOG(app.B, app.s, app.n, app.RadiusSpinner.Value, app.MonthsSpinner.Value, app.YearsSpinner.Value, app.radiuskmCheckBox.Value, app.monthsCheckBox.Value, app.yearsCheckBox.Value);
            UpdateFigs(app);
            MapFig(app);
        end

        % Button pushed function: LoadBOGButton
        function LoadBOGButtonPushed(app, event)
            % Load BOG data if accessible. If not disable the button and
            % turn it red.
            try
                app.B = loadBOG(); % change filepath for deployed version
                app.LoadBOGButton.BackgroundColor = [0, 1, 0]; % Green
                app.FilterBOGButton.Enable = "on";
            catch
                app.LoadBOGButton.BackgroundColor = [1, 0, 0]; % Red
                app.LoadBOGButton.Enable = "off";
                app.FilterBOGButton.Enable = "off";
            end
        end

        % Value changed function: UpdatePlotsButton
        function UpdatePlotsButtonValueChanged(app, event)
            % Create plot based on user input
            PropertyPropertyPlot(app,app.XaxisDropDown.Value,app.YaxisDropDown.Value)
            
        end

        % Button pushed function: LoadIndexButton
        function LoadIndexButtonPushed(app, event)
            % Load GliderIndex if accessible. If not disable button and
            % turn it red.
            try
                load('C:\Users\bwerb\Documents\SprayDashBoard\GlideAppPackage\testdata\GliderIndex_v1.mat','Glidex'); % change filepath for deployed version
                app.GliderIndex = Glidex;
                app.LoadIndexButton.BackgroundColor = [0, 1, 0]; % Green
                app.FilterSprayButton.Enable = "on";
            catch
                app.LoadIndexButton.BackgroundColor = [1, 0, 0]; % Red
                app.LoadIndexButton.Enable = "off";
                app.FilterSprayButton.Enable = "off";
            end
        end

        % Button pushed function: FilterSprayButton
        function FilterSprayButtonPushed(app, event)
            % Add Spray data from other missions according to user filters
            app.Spray2Plot = filterSpray(app.GliderIndex, app.s, app.mission,app.n, app.RadiusSpinner_Spray.Value, app.MonthsSpinner_Spray.Value, app.YearsSpinner_Spray.Value, app.MinDepthSpinner.Value,app.radiuskmCheckBox_Spray.Value, app.monthsCheckBox_Spray.Value, app.yearsCheckBox_Spray.Value,app.MinDepthCheckBox.Value);
            app.sc = SpraySearch(app.Spray2Plot);
            UpdateFigs(app);
            MapFig(app);
        end

        % Button pushed function: NewMissionSetupButton
        function NewMissionSetupButtonPushed(app, event)
            % Open new mission setup application
            NewMissionSetup % disabled for first release
        end

        % Button pushed function: UpdateButton
        function UpdateButtonPushed(app, event)
            % Update all figures
            UpdateAll(app)
        end

        % Value changed function: missionDropDown
        function missionDropDownValueChanged(app, event)
            % Update all figures
            UpdateAll(app)
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Get the file path for locating images
            pathToMLAPP = fileparts(mfilename('fullpath'));

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 1310 1082];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.Scrollable = 'on';

            % Create Toolbar
            app.Toolbar = uitoolbar(app.UIFigure);

            % Create previousprofile
            app.previousprofile = uipushtool(app.Toolbar);
            app.previousprofile.ClickedCallback = createCallbackFcn(app, @previousprofileClicked, true);
            app.previousprofile.Icon = fullfile(pathToMLAPP, 'images', 'Arrow-left-512.png');

            % Create nextprofile
            app.nextprofile = uipushtool(app.Toolbar);
            app.nextprofile.ClickedCallback = createCallbackFcn(app, @nextprofileClicked, true);
            app.nextprofile.Icon = fullfile(pathToMLAPP, 'images', 'Arrow-right-512.png');

            % Create TabGroup
            app.TabGroup = uitabgroup(app.UIFigure);
            app.TabGroup.Position = [1 -457 1310 1545];

            % Create Home
            app.Home = uitab(app.TabGroup);
            app.Home.Title = 'Home';

            % Create GridLayout3
            app.GridLayout3 = uigridlayout(app.Home);
            app.GridLayout3.ColumnWidth = {'1x', '1x', '1x'};
            app.GridLayout3.RowHeight = {'1x', '1x', '1x', '1x'};

            % Create Image
            app.Image = uiimage(app.GridLayout3);
            app.Image.ScaleMethod = 'fill';
            app.Image.Layout.Row = [1 4];
            app.Image.Layout.Column = [1 3];
            app.Image.ImageSource = fullfile(pathToMLAPP, 'images', 'Spray_near_surface_1_1920_800a.jpg');

            % Create UpdateButton
            app.UpdateButton = uibutton(app.GridLayout3, 'push');
            app.UpdateButton.ButtonPushedFcn = createCallbackFcn(app, @UpdateButtonPushed, true);
            app.UpdateButton.Layout.Row = 4;
            app.UpdateButton.Layout.Column = 3;
            app.UpdateButton.Text = 'Update';

            % Create NewMissionSetupButton
            app.NewMissionSetupButton = uibutton(app.GridLayout3, 'push');
            app.NewMissionSetupButton.ButtonPushedFcn = createCallbackFcn(app, @NewMissionSetupButtonPushed, true);
            app.NewMissionSetupButton.Layout.Row = 4;
            app.NewMissionSetupButton.Layout.Column = 1;
            app.NewMissionSetupButton.Text = 'New Mission Setup';

            % Create GLIDEAPPLabel
            app.GLIDEAPPLabel = uilabel(app.GridLayout3);
            app.GLIDEAPPLabel.HorizontalAlignment = 'center';
            app.GLIDEAPPLabel.FontName = 'Papyrus';
            app.GLIDEAPPLabel.FontSize = 48;
            app.GLIDEAPPLabel.FontWeight = 'bold';
            app.GLIDEAPPLabel.FontColor = [1 1 1];
            app.GLIDEAPPLabel.Layout.Row = 1;
            app.GLIDEAPPLabel.Layout.Column = [1 3];
            app.GLIDEAPPLabel.Text = 'GLIDEAPP';

            % Create missionDropDown
            app.missionDropDown = uidropdown(app.GridLayout3);
            app.missionDropDown.Items = {};
            app.missionDropDown.ValueChangedFcn = createCallbackFcn(app, @missionDropDownValueChanged, true);
            app.missionDropDown.Layout.Row = 4;
            app.missionDropDown.Layout.Column = 2;
            app.missionDropDown.Value = {};

            % Create ProfileDataTab
            app.ProfileDataTab = uitab(app.TabGroup);
            app.ProfileDataTab.Title = 'Profile Data';

            % Create GridLayout11
            app.GridLayout11 = uigridlayout(app.ProfileDataTab);
            app.GridLayout11.ColumnWidth = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
            app.GridLayout11.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};

            % Create Chla
            app.Chla = uiaxes(app.GridLayout11);
            title(app.Chla, 'Chlorophyll A')
            xlabel(app.Chla, '[unit]')
            app.Chla.XTick = [];
            app.Chla.YTick = [];
            app.Chla.ZTick = [];
            app.Chla.Box = 'on';
            app.Chla.XGrid = 'on';
            app.Chla.YGrid = 'on';
            app.Chla.Layout.Row = [4 6];
            app.Chla.Layout.Column = [7 8];

            % Create temp
            app.temp = uiaxes(app.GridLayout11);
            title(app.temp, 'Temperature')
            xlabel(app.temp, '^o C')
            app.temp.XTick = [];
            app.temp.YTick = [];
            app.temp.ZTick = [];
            app.temp.Box = 'on';
            app.temp.XGrid = 'on';
            app.temp.YGrid = 'on';
            app.temp.Layout.Row = [4 6];
            app.temp.Layout.Column = [1 2];

            % Create Vrse
            app.Vrse = uiaxes(app.GridLayout11);
            title(app.Vrse, 'Vrse')
            app.Vrse.XTick = [];
            app.Vrse.YTick = [];
            app.Vrse.ZTick = [];
            app.Vrse.Box = 'on';
            app.Vrse.Layout.Row = [10 12];
            app.Vrse.Layout.Column = [1 2];

            % Create Vk
            app.Vk = uiaxes(app.GridLayout11);
            title(app.Vk, 'Vk')
            app.Vk.XTick = [];
            app.Vk.YTick = [];
            app.Vk.ZTick = [];
            app.Vk.Box = 'on';
            app.Vk.Layout.Row = [10 12];
            app.Vk.Layout.Column = [3 4];

            % Create Ik
            app.Ik = uiaxes(app.GridLayout11);
            title(app.Ik, 'Ik')
            app.Ik.XTick = [];
            app.Ik.YTick = [];
            app.Ik.ZTick = [];
            app.Ik.Box = 'on';
            app.Ik.Layout.Row = [10 12];
            app.Ik.Layout.Column = [5 6];

            % Create Ib
            app.Ib = uiaxes(app.GridLayout11);
            title(app.Ib, 'Ib')
            app.Ib.XTick = [];
            app.Ib.YTick = [];
            app.Ib.ZTick = [];
            app.Ib.Box = 'on';
            app.Ib.Layout.Row = [10 12];
            app.Ib.Layout.Column = [7 8];

            % Create Vrse_std
            app.Vrse_std = uiaxes(app.GridLayout11);
            title(app.Vrse_std, 'Vrse std')
            app.Vrse_std.XTick = [];
            app.Vrse_std.YTick = [];
            app.Vrse_std.ZTick = [];
            app.Vrse_std.Box = 'on';
            app.Vrse_std.Layout.Row = [13 15];
            app.Vrse_std.Layout.Column = [1 2];

            % Create Vk_std
            app.Vk_std = uiaxes(app.GridLayout11);
            title(app.Vk_std, 'Vk std')
            app.Vk_std.XTick = [];
            app.Vk_std.YTick = [];
            app.Vk_std.ZTick = [];
            app.Vk_std.Box = 'on';
            app.Vk_std.Layout.Row = [13 15];
            app.Vk_std.Layout.Column = [3 4];

            % Create psal
            app.psal = uiaxes(app.GridLayout11);
            title(app.psal, 'Salinity')
            xlabel(app.psal, 'PSU')
            app.psal.XTick = [];
            app.psal.YTick = [];
            app.psal.ZTick = [];
            app.psal.Box = 'on';
            app.psal.XGrid = 'on';
            app.psal.YGrid = 'on';
            app.psal.Layout.Row = [4 6];
            app.psal.Layout.Column = [3 4];

            % Create doxy
            app.doxy = uiaxes(app.GridLayout11);
            title(app.doxy, 'Dissolved Oxygen')
            xlabel(app.doxy, '\mumolkg^-1')
            app.doxy.XTick = [];
            app.doxy.YTick = [];
            app.doxy.ZTick = [];
            app.doxy.Box = 'on';
            app.doxy.XGrid = 'on';
            app.doxy.YGrid = 'on';
            app.doxy.Layout.Row = [4 6];
            app.doxy.Layout.Column = [5 6];

            % Create PropertyProperty
            app.PropertyProperty = uiaxes(app.GridLayout11);
            title(app.PropertyProperty, 'Property Property')
            app.PropertyProperty.XTick = [];
            app.PropertyProperty.YTick = [];
            app.PropertyProperty.ZTick = [];
            app.PropertyProperty.Box = 'on';
            app.PropertyProperty.XGrid = 'on';
            app.PropertyProperty.YGrid = 'on';
            app.PropertyProperty.Layout.Row = [7 9];
            app.PropertyProperty.Layout.Column = [7 8];

            % Create PAR
            app.PAR = uiaxes(app.GridLayout11);
            title(app.PAR, 'PAR')
            app.PAR.XTick = [];
            app.PAR.YTick = [];
            app.PAR.ZTick = [];
            app.PAR.Box = 'on';
            app.PAR.XGrid = 'on';
            app.PAR.YGrid = 'on';
            app.PAR.Layout.Row = [7 9];
            app.PAR.Layout.Column = [5 6];

            % Create deltapH
            app.deltapH = uiaxes(app.GridLayout11);
            title(app.deltapH, 'pHin - CanyonB pHin')
            app.deltapH.XTick = [];
            app.deltapH.YTick = [];
            app.deltapH.ZTick = [];
            app.deltapH.Box = 'on';
            app.deltapH.XGrid = 'on';
            app.deltapH.YGrid = 'on';
            app.deltapH.Layout.Row = [7 9];
            app.deltapH.Layout.Column = [3 4];

            % Create pHin
            app.pHin = uiaxes(app.GridLayout11);
            title(app.pHin, 'pHin')
            app.pHin.XTick = [];
            app.pHin.YTick = [];
            app.pHin.ZTick = [];
            app.pHin.Box = 'on';
            app.pHin.XGrid = 'on';
            app.pHin.YGrid = 'on';
            app.pHin.Layout.Row = [7 9];
            app.pHin.Layout.Column = [1 2];

            % Create ProfileMap
            app.ProfileMap = uipanel(app.GridLayout11);
            app.ProfileMap.BorderType = 'none';
            app.ProfileMap.BorderWidth = 0;
            app.ProfileMap.Layout.Row = [1 3];
            app.ProfileMap.Layout.Column = [1 4];

            % Create MapInfo
            app.MapInfo = uitextarea(app.GridLayout11);
            app.MapInfo.Editable = 'off';
            app.MapInfo.Layout.Row = [1 3];
            app.MapInfo.Layout.Column = 5;

            % Create TabGroup2
            app.TabGroup2 = uitabgroup(app.GridLayout11);
            app.TabGroup2.Layout.Row = [1 3];
            app.TabGroup2.Layout.Column = [6 7];

            % Create BOGTab
            app.BOGTab = uitab(app.TabGroup2);
            app.BOGTab.Title = 'BOG';

            % Create GridLayout10
            app.GridLayout10 = uigridlayout(app.BOGTab);
            app.GridLayout10.RowHeight = {'1x', '1x', '1x', '1x', '1x'};

            % Create FilterBOGButton
            app.FilterBOGButton = uibutton(app.GridLayout10, 'push');
            app.FilterBOGButton.ButtonPushedFcn = createCallbackFcn(app, @FilterBOGButtonPushed, true);
            app.FilterBOGButton.Enable = 'off';
            app.FilterBOGButton.Layout.Row = 1;
            app.FilterBOGButton.Layout.Column = [1 2];
            app.FilterBOGButton.Text = 'Filter BOG';

            % Create compareCheckBox
            app.compareCheckBox = uicheckbox(app.GridLayout10);
            app.compareCheckBox.Text = 'compare';
            app.compareCheckBox.Layout.Row = 2;
            app.compareCheckBox.Layout.Column = 1;

            % Create LoadBOGButton
            app.LoadBOGButton = uibutton(app.GridLayout10, 'push');
            app.LoadBOGButton.ButtonPushedFcn = createCallbackFcn(app, @LoadBOGButtonPushed, true);
            app.LoadBOGButton.Layout.Row = 2;
            app.LoadBOGButton.Layout.Column = 2;
            app.LoadBOGButton.Text = 'LoadBOG';

            % Create radiuskmCheckBox
            app.radiuskmCheckBox = uicheckbox(app.GridLayout10);
            app.radiuskmCheckBox.Text = 'radius (km)';
            app.radiuskmCheckBox.Layout.Row = 3;
            app.radiuskmCheckBox.Layout.Column = 1;

            % Create RadiusSpinner
            app.RadiusSpinner = uispinner(app.GridLayout10);
            app.RadiusSpinner.Layout.Row = 3;
            app.RadiusSpinner.Layout.Column = 2;

            % Create monthsCheckBox
            app.monthsCheckBox = uicheckbox(app.GridLayout10);
            app.monthsCheckBox.Text = 'months';
            app.monthsCheckBox.Layout.Row = 4;
            app.monthsCheckBox.Layout.Column = 1;

            % Create MonthsSpinner
            app.MonthsSpinner = uispinner(app.GridLayout10);
            app.MonthsSpinner.Layout.Row = 4;
            app.MonthsSpinner.Layout.Column = 2;

            % Create yearsCheckBox
            app.yearsCheckBox = uicheckbox(app.GridLayout10);
            app.yearsCheckBox.Text = 'years';
            app.yearsCheckBox.Layout.Row = 5;
            app.yearsCheckBox.Layout.Column = 1;

            % Create YearsSpinner
            app.YearsSpinner = uispinner(app.GridLayout10);
            app.YearsSpinner.Layout.Row = 5;
            app.YearsSpinner.Layout.Column = 2;

            % Create SprayTab
            app.SprayTab = uitab(app.TabGroup2);
            app.SprayTab.Title = 'Spray';

            % Create GridLayout14
            app.GridLayout14 = uigridlayout(app.SprayTab);
            app.GridLayout14.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x'};

            % Create FilterSprayButton
            app.FilterSprayButton = uibutton(app.GridLayout14, 'push');
            app.FilterSprayButton.ButtonPushedFcn = createCallbackFcn(app, @FilterSprayButtonPushed, true);
            app.FilterSprayButton.Enable = 'off';
            app.FilterSprayButton.Layout.Row = 1;
            app.FilterSprayButton.Layout.Column = [1 2];
            app.FilterSprayButton.Text = 'Filter Spray';

            % Create compareCheckBox_Spray
            app.compareCheckBox_Spray = uicheckbox(app.GridLayout14);
            app.compareCheckBox_Spray.Text = 'compare';
            app.compareCheckBox_Spray.Layout.Row = 2;
            app.compareCheckBox_Spray.Layout.Column = 1;

            % Create radiuskmCheckBox_Spray
            app.radiuskmCheckBox_Spray = uicheckbox(app.GridLayout14);
            app.radiuskmCheckBox_Spray.Text = 'radius (km)';
            app.radiuskmCheckBox_Spray.Layout.Row = 4;
            app.radiuskmCheckBox_Spray.Layout.Column = 1;

            % Create RadiusSpinner_Spray
            app.RadiusSpinner_Spray = uispinner(app.GridLayout14);
            app.RadiusSpinner_Spray.Layout.Row = 4;
            app.RadiusSpinner_Spray.Layout.Column = 2;

            % Create monthsCheckBox_Spray
            app.monthsCheckBox_Spray = uicheckbox(app.GridLayout14);
            app.monthsCheckBox_Spray.Text = 'months';
            app.monthsCheckBox_Spray.Layout.Row = 5;
            app.monthsCheckBox_Spray.Layout.Column = 1;

            % Create MonthsSpinner_Spray
            app.MonthsSpinner_Spray = uispinner(app.GridLayout14);
            app.MonthsSpinner_Spray.Layout.Row = 5;
            app.MonthsSpinner_Spray.Layout.Column = 2;

            % Create yearsCheckBox_Spray
            app.yearsCheckBox_Spray = uicheckbox(app.GridLayout14);
            app.yearsCheckBox_Spray.Text = 'years';
            app.yearsCheckBox_Spray.Layout.Row = 6;
            app.yearsCheckBox_Spray.Layout.Column = 1;

            % Create YearsSpinner_Spray
            app.YearsSpinner_Spray = uispinner(app.GridLayout14);
            app.YearsSpinner_Spray.Layout.Row = 6;
            app.YearsSpinner_Spray.Layout.Column = 2;

            % Create LoadIndexButton
            app.LoadIndexButton = uibutton(app.GridLayout14, 'push');
            app.LoadIndexButton.ButtonPushedFcn = createCallbackFcn(app, @LoadIndexButtonPushed, true);
            app.LoadIndexButton.Enable = 'off';
            app.LoadIndexButton.Layout.Row = 2;
            app.LoadIndexButton.Layout.Column = 2;
            app.LoadIndexButton.Text = 'Load Index';

            % Create MinDepthCheckBox
            app.MinDepthCheckBox = uicheckbox(app.GridLayout14);
            app.MinDepthCheckBox.Text = 'min depth (m)';
            app.MinDepthCheckBox.Layout.Row = 3;
            app.MinDepthCheckBox.Layout.Column = 1;

            % Create MinDepthSpinner
            app.MinDepthSpinner = uispinner(app.GridLayout14);
            app.MinDepthSpinner.Step = 50;
            app.MinDepthSpinner.Limits = [0 1000];
            app.MinDepthSpinner.Layout.Row = 3;
            app.MinDepthSpinner.Layout.Column = 2;

            % Create GridLayout13
            app.GridLayout13 = uigridlayout(app.GridLayout11);
            app.GridLayout13.Layout.Row = 2;
            app.GridLayout13.Layout.Column = 8;

            % Create profEditField
            app.profEditField = uieditfield(app.GridLayout13, 'numeric');
            app.profEditField.Limits = [1 Inf];
            app.profEditField.ValueChangedFcn = createCallbackFcn(app, @profEditFieldValueChanged, true);
            app.profEditField.HorizontalAlignment = 'center';
            app.profEditField.Layout.Row = 1;
            app.profEditField.Layout.Column = 2;
            app.profEditField.Value = 1;

            % Create profileLabel
            app.profileLabel = uilabel(app.GridLayout13);
            app.profileLabel.HorizontalAlignment = 'right';
            app.profileLabel.Layout.Row = 1;
            app.profileLabel.Layout.Column = 1;
            app.profileLabel.Text = 'profile';

            % Create nprofsEditField
            app.nprofsEditField = uieditfield(app.GridLayout13, 'numeric');
            app.nprofsEditField.Limits = [1 Inf];
            app.nprofsEditField.ValueChangedFcn = createCallbackFcn(app, @nprofsEditFieldValueChanged, true);
            app.nprofsEditField.HorizontalAlignment = 'center';
            app.nprofsEditField.Layout.Row = 2;
            app.nprofsEditField.Layout.Column = 2;
            app.nprofsEditField.Value = 1;

            % Create nprofsEditFieldLabel
            app.nprofsEditFieldLabel = uilabel(app.GridLayout13);
            app.nprofsEditFieldLabel.HorizontalAlignment = 'right';
            app.nprofsEditFieldLabel.Layout.Row = 2;
            app.nprofsEditFieldLabel.Layout.Column = 1;
            app.nprofsEditFieldLabel.Text = 'nprofs';

            % Create UpdatePlotsButton
            app.UpdatePlotsButton = uibutton(app.GridLayout11, 'state');
            app.UpdatePlotsButton.ValueChangedFcn = createCallbackFcn(app, @UpdatePlotsButtonValueChanged, true);
            app.UpdatePlotsButton.Text = 'Update Plots';
            app.UpdatePlotsButton.Layout.Row = 1;
            app.UpdatePlotsButton.Layout.Column = 8;

            % Create GridLayout16
            app.GridLayout16 = uigridlayout(app.GridLayout11);
            app.GridLayout16.Layout.Row = 3;
            app.GridLayout16.Layout.Column = 8;

            % Create XaxisDropDownLabel
            app.XaxisDropDownLabel = uilabel(app.GridLayout16);
            app.XaxisDropDownLabel.HorizontalAlignment = 'right';
            app.XaxisDropDownLabel.Layout.Row = 1;
            app.XaxisDropDownLabel.Layout.Column = 1;
            app.XaxisDropDownLabel.Text = 'X axis';

            % Create XaxisDropDown
            app.XaxisDropDown = uidropdown(app.GridLayout16);
            app.XaxisDropDown.Items = {'tc', 'psal', 'doxy', 'opt', 'pH25atm', 'deltapH', 'PAR', 'depth', 'pHin'};
            app.XaxisDropDown.Layout.Row = 1;
            app.XaxisDropDown.Layout.Column = 2;
            app.XaxisDropDown.Value = 'tc';

            % Create YaxisDropDownLabel
            app.YaxisDropDownLabel = uilabel(app.GridLayout16);
            app.YaxisDropDownLabel.HorizontalAlignment = 'right';
            app.YaxisDropDownLabel.Layout.Row = 2;
            app.YaxisDropDownLabel.Layout.Column = 1;
            app.YaxisDropDownLabel.Text = 'Y axis';

            % Create YaxisDropDown
            app.YaxisDropDown = uidropdown(app.GridLayout16);
            app.YaxisDropDown.Items = {'tc', 'psal', 'doxy', 'opt', 'pH25atm', 'deltapH', 'PAR', 'depth', 'pHin'};
            app.YaxisDropDown.Layout.Row = 2;
            app.YaxisDropDown.Layout.Column = 2;
            app.YaxisDropDown.Value = 'tc';

            % Create DielCyclesTab
            app.DielCyclesTab = uitab(app.TabGroup);
            app.DielCyclesTab.Title = 'Diel Cycles';

            % Create GridLayout5
            app.GridLayout5 = uigridlayout(app.DielCyclesTab);
            app.GridLayout5.ColumnWidth = {'6x', '1x'};
            app.GridLayout5.RowHeight = {'1x', '1x', '1x', '1x'};

            % Create O2AnomDiel
            app.O2AnomDiel = uiaxes(app.GridLayout5);
            zlabel(app.O2AnomDiel, 'Z')
            app.O2AnomDiel.Layout.Row = 1;
            app.O2AnomDiel.Layout.Column = 1;

            % Create DICpcolor
            app.DICpcolor = uiaxes(app.GridLayout5);
            zlabel(app.DICpcolor, 'Z')
            app.DICpcolor.Layout.Row = 4;
            app.DICpcolor.Layout.Column = 1;

            % Create O2Anompcolor
            app.O2Anompcolor = uiaxes(app.GridLayout5);
            zlabel(app.O2Anompcolor, 'Z')
            app.O2Anompcolor.Layout.Row = 3;
            app.O2Anompcolor.Layout.Column = 1;

            % Create DIC
            app.DIC = uiaxes(app.GridLayout5);
            zlabel(app.DIC, 'Z')
            app.DIC.Layout.Row = 2;
            app.DIC.Layout.Column = 1;

            % Create GridLayout6
            app.GridLayout6 = uigridlayout(app.GridLayout5);
            app.GridLayout6.ColumnWidth = {'1x'};
            app.GridLayout6.RowHeight = {'1x', '1x', '1x', '1x'};
            app.GridLayout6.Layout.Row = [1 2];
            app.GridLayout6.Layout.Column = 2;

            % Create depth1
            app.depth1 = uispinner(app.GridLayout6);
            app.depth1.Limits = [0 1000];
            app.depth1.ValueChangedFcn = createCallbackFcn(app, @depth1ValueChanged, true);
            app.depth1.Layout.Row = 2;
            app.depth1.Layout.Column = 1;
            app.depth1.Value = 1;

            % Create depth2
            app.depth2 = uispinner(app.GridLayout6);
            app.depth2.Limits = [0 1000];
            app.depth2.ValueChangedFcn = createCallbackFcn(app, @depth2ValueChanged, true);
            app.depth2.Layout.Row = 3;
            app.depth2.Layout.Column = 1;
            app.depth2.Value = 2;

            % Create depth3
            app.depth3 = uispinner(app.GridLayout6);
            app.depth3.Limits = [0 1000];
            app.depth3.ValueChangedFcn = createCallbackFcn(app, @depth3ValueChanged, true);
            app.depth3.Layout.Row = 4;
            app.depth3.Layout.Column = 1;
            app.depth3.Value = 3;

            % Create DepthsLabel
            app.DepthsLabel = uilabel(app.GridLayout6);
            app.DepthsLabel.BackgroundColor = [1 1 1];
            app.DepthsLabel.HorizontalAlignment = 'center';
            app.DepthsLabel.FontWeight = 'bold';
            app.DepthsLabel.Layout.Row = 1;
            app.DepthsLabel.Layout.Column = 1;
            app.DepthsLabel.Text = 'Depths';

            % Create GridLayout7
            app.GridLayout7 = uigridlayout(app.GridLayout5);
            app.GridLayout7.ColumnWidth = {'1x'};
            app.GridLayout7.RowHeight = {'1x', '1x', '1x'};
            app.GridLayout7.Layout.Row = [3 4];
            app.GridLayout7.Layout.Column = 2;

            % Create dielstart
            app.dielstart = uispinner(app.GridLayout7);
            app.dielstart.AllowEmpty = 'on';
            app.dielstart.ValueChangedFcn = createCallbackFcn(app, @dielstartValueChanged, true);
            app.dielstart.Layout.Row = 2;
            app.dielstart.Layout.Column = 1;
            app.dielstart.Value = [];

            % Create dielend
            app.dielend = uispinner(app.GridLayout7);
            app.dielend.AllowEmpty = 'on';
            app.dielend.ValueChangedFcn = createCallbackFcn(app, @dielendValueChanged, true);
            app.dielend.Layout.Row = 3;
            app.dielend.Layout.Column = 1;
            app.dielend.Value = [];

            % Create ProfilesLabel
            app.ProfilesLabel = uilabel(app.GridLayout7);
            app.ProfilesLabel.BackgroundColor = [1 1 1];
            app.ProfilesLabel.HorizontalAlignment = 'center';
            app.ProfilesLabel.FontWeight = 'bold';
            app.ProfilesLabel.Layout.Row = 1;
            app.ProfilesLabel.Layout.Column = 1;
            app.ProfilesLabel.Text = 'Profiles';

            % Create LargeMapTab
            app.LargeMapTab = uitab(app.TabGroup);
            app.LargeMapTab.Title = 'Large Map';

            % Create Panel
            app.Panel = uipanel(app.LargeMapTab);
            app.Panel.BorderWidth = 0;
            app.Panel.Position = [11 452 1287 1050];

            % Create RemarksTab
            app.RemarksTab = uitab(app.TabGroup);
            app.RemarksTab.Title = 'Remarks';

            % Create GridLayout4
            app.GridLayout4 = uigridlayout(app.RemarksTab);
            app.GridLayout4.RowHeight = {'1x', '1x', '1x'};

            % Create TextArea
            app.TextArea = uitextarea(app.GridLayout4);
            app.TextArea.Layout.Row = [2 3];
            app.TextArea.Layout.Column = [1 2];

            % Create GridLayout9
            app.GridLayout9 = uigridlayout(app.GridLayout4);
            app.GridLayout9.ColumnWidth = {'1x', '1x', '1x', '1x'};
            app.GridLayout9.RowHeight = {'1x', '1x', '1x'};
            app.GridLayout9.Layout.Row = 1;
            app.GridLayout9.Layout.Column = [1 2];

            % Create RemarksEditField
            app.RemarksEditField = uieditfield(app.GridLayout9, 'text');
            app.RemarksEditField.Layout.Row = [1 2];
            app.RemarksEditField.Layout.Column = 1;

            % Create DropDown
            app.DropDown = uidropdown(app.GridLayout9);
            app.DropDown.Items = {'Questionable Profile', 'Updated Cal FIle', 'Diel Cycle Start', 'Diel Cycle End', 'Diel Cycles', 'Pies Profiles', 'Note', 'Issue Tracking'};
            app.DropDown.Layout.Row = [1 2];
            app.DropDown.Layout.Column = 2;
            app.DropDown.Value = 'Questionable Profile';

            % Create ProfSelect
            app.ProfSelect = uispinner(app.GridLayout9);
            app.ProfSelect.Layout.Row = [1 2];
            app.ProfSelect.Layout.Column = 3;

            % Create AddRemarkButton
            app.AddRemarkButton = uibutton(app.GridLayout9, 'push');
            app.AddRemarkButton.ButtonPushedFcn = createCallbackFcn(app, @AddRemarkButtonPushed, true);
            app.AddRemarkButton.Layout.Row = [1 2];
            app.AddRemarkButton.Layout.Column = 4;
            app.AddRemarkButton.Text = 'Add Remark';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Glide

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end