function fh = visFlag(raw_tot, raw_filt, bin_tot_good, bin_tot_suspect, bin_filt_good, bin_filt_bad, ...
                      varname, varindex, raw_bad, fooflow, spd_var, autoscale, figh, raw_fdom, bin_fdom)
  if nargin < 9; raw_bad = []; end
  if nargin < 10; fooflow = []; end
  if nargin < 11; spd_var = 'spd'; end
  if nargin < 12; autoscale = false; end
  if nargin < 13; figh = fig(52); end
  if nargin < 14; raw_fdom = []; end
  if nargin < 15; bin_fdom = []; end
  if isempty(figh); fh = fig(52); else; fh = figh; end
  hold('on');
  
  
  % Define constants
  ColorSet = lines(5);
  % . raw | o bin 
  sha_raw = '.';
  sha_bin = 'o';
  % blue total | red fitlered | yellow bad (switch buffer + QCed)
  col_tot = ColorSet(1,:);
  col_filt = ColorSet(2,:);
  col_switch = ColorSet(3, :);
  col_flag = ColorSet(4, :);
  %   col_target = ColorSet(5, :);
  
  % check if data is loaded
  if isempty(raw_filt) && isempty(bin_tot_good) && isempty(bin_filt_good)
    error('No data loaded, check previous step')
  end
  
  sc = [];
  leg = {};
  xmin = [];
  xmax = [];
  axis_nb = 1;
  
  % plot flow right y axis
  if ~strcmp(varname,'par')
    % yyaxis('left');
    if ~isempty(fooflow)
      % convert datenum to datetime
      if ~isdatetime(fooflow.dt)
        fooflow.dt = datetime(fooflow.dt, 'ConvertFrom', 'datenum');
      end
      if ~any(strcmp(fooflow.Properties.VariableNames, spd_var))
        spd_var = fooflow.Properties.VariableNames{contains(fooflow.Properties.VariableNames, 'spd') & ...
          ~contains(fooflow.Properties.VariableNames, 'avg')};
      end
      if any(~isnan(fooflow.(spd_var)))
        plot(fooflow.dt, fooflow.(spd_var),'Color',[0 0.8 0]); popo=gca; 
        % sc(end+1) = plot(fooflow.dt, fooflow.(spd_var),'Color',[0 0.8 0]); popo=gca; 
        ylim([0 max(fooflow.(spd_var))+4]); ylabel('Flow rate (lpm)'); popo.YColor = [0 0.8 0]; % green
        ylabel('flow rate (lpm)');
        leg{end+1} = 'flow';
      end
    end
  end

  if ~isempty(raw_fdom)
    % convert datenum to datetime
    if ~isdatetime(raw_fdom.dt)
      raw_fdom.dt = datetime(raw_fdom.dt, 'ConvertFrom', 'datenum');
    end
    if isempty(leg)
      scatter(raw_fdom.dt, raw_fdom.fdom, sha_raw, 'Color', [0	205	205]/255, 'MarkerEdgeColor', [0	205	205]/255)
      ylabel('FDOM (volts)')
    else
      addaxis(raw_fdom.dt, raw_fdom.fdom, sha_raw, 'Color', [0	205	205]/255, 'MarkerEdgeColor', [0	205	205]/255);
      addaxislabel(size(leg,2)+1,'FDOM (volts)');
    end
    leg{end+1} = 'fCDOM (raw)';
  end

  if ~isempty(bin_fdom)
    % convert datenum to datetime
    if ~isdatetime(bin_fdom.dt)
      bin_fdom.dt = datetime(bin_fdom.dt, 'ConvertFrom', 'datenum');
    end
    if isempty(leg)
      scatter(bin_fdom.dt, bin_fdom.fdom, sha_bin, 'Color', [0	205	205]/255, 'MarkerEdgeColor', [0	205	205]/255)
      ylabel('FDOM (volts)')
    else
      addaxis(bin_fdom.dt, bin_fdom.fdom, sha_bin, 'Color', [0	205	205]/255, 'MarkerFaceColor', [0	205	205]/255, 'MarkerEdgeColor', 'k');
      addaxislabel(size(leg,2)+1,'FDOM (volts)');
    end
    leg{end+1} = 'fCDOM (binned)';
  end
  
  yyaxis('right');
  % Plot raw data
  if ~isempty(raw_bad)
    % convert datenum to datetime
    if ~isdatetime(raw_bad.dt)
      raw_bad.dt = datetime(raw_bad.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(raw_bad.dt)]);
    xmax = max([xmax; max(raw_bad.dt)]);
    sc(end+1) = scatter(raw_bad.dt, raw_bad.(varname)(:,varindex), sha_raw, 'MarkerEdgeColor', col_switch, 'MarkerEdgeAlpha', 0.5);
    leg{end+1} = 'raw bad';
  end
  if ~isempty(raw_tot)
    ax2 = gca;
    ax2.YColor = col_tot;
    % convert datenum to datetime
    if ~isdatetime(raw_tot.dt)
      raw_tot.dt = datetime(raw_tot.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(raw_tot.dt)]);
    xmax = max([xmax; max(raw_tot.dt)]);
    sc(end+1) = scatter(raw_tot.dt, raw_tot.(varname)(:,varindex), sha_raw, 'MarkerEdgeColor', col_tot, 'MarkerEdgeAlpha', 0.5);
    leg{end+1} = 'raw total';
  end
  if ~isempty(raw_filt)
    % convert datenum to datetime
    if ~isdatetime(raw_filt.dt)
      raw_filt.dt = datetime(raw_filt.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(raw_filt.dt)]);
    xmax = max([xmax; max(raw_filt.dt)]);
    sc(end+1) = scatter(raw_filt.dt, raw_filt.(varname)(:,varindex), sha_raw, 'MarkerEdgeColor', col_filt, 'MarkerEdgeAlpha', 0.5);
    leg{end+1} = 'raw filtered';
  end
  
  % Plot tot binned data
  if ~isempty(bin_tot_good)
    ax2 = gca;
    ax2.YColor = col_tot;
    % convert datenum to datetime
    if ~isdatetime(bin_tot_good.dt)
      bin_tot_good.dt = datetime(bin_tot_good.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(bin_tot_good.dt)]);
    xmax = max([xmax; max(bin_tot_good.dt)]);
    sc(end+1) = plot(bin_tot_good.dt, bin_tot_good.(varname)(:,varindex), sha_bin, 'MarkerEdgeColor', 'k', 'MarkerFaceColor', col_tot);
    leg{end+1} = 'binned total';
  end
  if ~isempty(bin_tot_suspect)
    % convert datenum to datetime
    if ~isdatetime(bin_tot_suspect.dt)
      bin_tot_suspect.dt = datetime(bin_tot_suspect.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(bin_tot_suspect.dt)]);
    xmax = max([xmax; max(bin_tot_suspect.dt)]);
    sc(end+1) = plot(bin_tot_suspect.dt, bin_tot_suspect.(varname)(:,varindex), sha_bin, 'MarkerEdgeColor', col_flag, 'MarkerFaceColor', col_flag);
    leg{end+1} = 'binned total suspect';
  end
  
  % Plot filt binned data
  if ~isempty(bin_filt_good)
    % convert datenum to datetime
    if ~isdatetime(bin_filt_good.dt)
      bin_filt_good.dt = datetime(bin_filt_good.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(bin_filt_good.dt)]);
    xmax = max([xmax; max(bin_filt_good.dt)]);
    sc(end+1) = plot(bin_filt_good.dt, bin_filt_good.(varname)(:,varindex), sha_bin, 'MarkerEdgeColor', 'k', 'MarkerFaceColor', col_filt);
    leg{end+1} = 'binned filtered';
  end
  if ~isempty(bin_filt_bad)
    % convert datenum to datetime
    if ~isdatetime(bin_filt_bad.dt)
      bin_filt_bad.dt = datetime(bin_filt_bad.dt, 'ConvertFrom', 'datenum');
    end
    xmin = min([xmin; min(bin_filt_bad.dt)]);
    xmax = max([xmax; max(bin_filt_bad.dt)]);
    sc(end+1) = plot(bin_filt_bad.dt, bin_filt_bad.(varname)(:,varindex), sha_bin, 'MarkerEdgeColor', col_flag, 'MarkerFaceColor', col_flag);
    leg{end+1} = 'binned filtered bad';
  end
  

  % set y limit to binned data in priority
  if ~autoscale
    if isempty(bin_tot_good) && ~isempty(bin_filt_good) && ~isempty(raw_filt)
      foo_scale = [bin_filt_good.(varname)(:,varindex); raw_filt.(varname)(:,varindex)];
    elseif isempty(bin_tot_good) && ~isempty(bin_filt_good)
      foo_scale = bin_filt_good.(varname)(:,varindex);
    elseif ~isempty(bin_tot_good) && isempty(bin_filt_good)
      foo_scale = bin_tot_good.(varname)(:,varindex);
    elseif ~isempty(bin_tot_good) && ~isempty(bin_filt_good) && ~isempty(raw_filt)
      foo_scale = [bin_tot_good.(varname)(:,varindex); bin_filt_good.(varname)(:,varindex); raw_filt.(varname)(:,varindex)];
    elseif ~isempty(bin_tot_good) && ~isempty(bin_filt_good)
      foo_scale = [bin_tot_good.(varname)(:,varindex); bin_filt_good.(varname)(:,varindex)];
    end
    ylim([min(foo_scale) max(foo_scale)]);
  end
  xlim([xmin xmax])

  ylabel(varname);
  
  if ~isempty(leg)
    legend(sc, leg, 'FontSize', 13, 'AutoUpdate','off')
  end
  
  % datetick2_doy();
  % set(datacursormode(fh),'UpdateFcn',@data_cursor_display_date);
end