function slices_plot(l1b,c,p,thres,slices,color,dateline)
%
% function slices_plot(l1b,c,p,thres,slices,color,dateline)
%
% plots the outlines (at a response threshold of thres) of the slices
% in slices(1..8) of the slices at pulse c in frame p with color
% Set dateline=1 for longitude in the range 0..360 otherwise -180 to 180 used
% Slice info is from l1b struct read from QuikSCAT L1B file.
%  l1b.slice_lon, l1b.slice_lat l1b.cell_lon, l1b.cell_lat
%  l1b.antenna_azimuth l1b.orbit_time l1b.sigma0_mode_flag

% written 25 Jun 2020 by D Long at BYU

% cell (egg) center location
cell_lon=l1b.cell_lon(c,p);
cell_lon(cell_lon>180)=cell_lon(cell_lon>180)-360;
cell_lat=l1b.cell_lat(c,p);

% compute slice centers
slon=mod(cell_lon+l1b.slice_lon(:,c,p)/cosd(cell_lat),360);
slon(slon<-180)=slon(slon<-180)+360;
slon(slon>180)=slon(slon>180)-360;
slat=cell_lat+l1b.slice_lat(:,c,p);

% and plot slice centers
plot(adjLon(slon,dateline),slat,[color,'.'])
hold on;

% get full slice responses by interpolation of the BYU slice response
ant_angle=l1b.antenna_azimuth(c,p);
orbit_time=6061*double(l1b.orbit_time(p))/double(max(l1b.orbit_time)-min(l1b.orbit_time)); 
polarization=mod(fix(double(l1b.sigma0_mode_flag(c,p))/4),2);
[pulse_del_lat,pulse_del_lon,resp]=interpolate_slice_response(ant_angle,orbit_time,polarization,cell_lat);

myfigure(2)
% compute slice contour and plot
for n=1:length(slices);
  s=slices(n);
  plon=pulse_del_lon+cell_lon;
  plon(plon<-180)=plon(plon<-180)+360;
  plon(plon>180)=plon(plon>180)-360;
  plat=pulse_del_lat+cell_lat;
  
  if 1 % recenter slice locations; this help accounts for orbit and attitude variations
    ind=find(resp(:,:,s)>=-6);
    plon=plon-mean(plon(ind))+slon(s);
    plat=plat-mean(plat(ind))+slat(s);
  end
  
  % plot contour of slice response function
  contour(adjLon(plon,dateline),plat,resp(:,:,s),[thres,thres],color)
end

function outlon=adjLon(inlon,flag)
%
% function outlon=adjLon(inlon,flag)
%
% returns longitude values in range flag==0: -180..180 flag==1: 0..360

% written 25 Jun 2020 by D Long at BYU

outlon=inlon;
if flag==1
  outlon(outlon<0)=outlon(outlon<0)+360.0;
else
  outlon(outlon>180)=outlon(outlon>180)-360.0;
end