function fsi_slicez(action)

switch nargin
case 0
   LocalInitFig
case 1
   figH     = findobj('Tag','Main');
   UserData = get(figH, 'UserData');
   % Get the data
   xColumn  = strmatch('lon', UserData.columns);
   yColumn  = strmatch('lat', UserData.columns);
   zColumn  = strmatch('depth', UserData.columns);
   
   if isstr(action)
      switch action
      case 'grid'
         
         % Read grid spacing from the runfsi.properties file
         p    = readProperties('runfsi.properties');
         xInc = sscanf(getProperty(p, 'xinc'), '%f');
         yInc = sscanf(getProperty(p, 'yinc'), '%f');
         zInc = sscanf(getProperty(p, 'zinc'), '%f');
         
         % Get the user selected variable to graph
         bh         = findobj('Tag', 'variableName');
         dataColumn = get(bh, 'Value');
         
         [p f e] = fileparts(UserData.filename);
         filename = [p filesep f '_' UserData.columns{dataColumn}];
         % Get the slice points
         figZ = findobj('Tag', 'fsi_slicez');
         if exist([filename '.mat']) ~= 2
            
            
            % Grid the variable
            [XI, YI, ZI, VI] = grid3d(UserData.data(:, xColumn), ...
               UserData.data(:, yColumn), ...
               UserData.data(:, zColumn), ...
               UserData.data(:, dataColumn), ...
               xInc, yInc, zInc);
            
            
            
            save(filename, 'XI','YI','ZI','VI');
            
         end
         set(figZ, 'UserData', filename);
      end
      
   else
      figZ = findobj('Tag', 'fsi_slicez');
      filename = get(figZ, 'UserData');
      if isempty(filename)
         fsi_slicez('grid'); % Grid the data
         fsi_slicez(action); % Call the function again after the data is gridded
      else
         set(0, 'CurrentFigure', figZ);
         load(filename)
         slice(XI,YI,ZI,VI,[],[], action);
         good = near(ZI(1,1,:), action);
         vMax = max(max(VI(:,:,good)));
         vMin = min(min(VI(:,:,good)));
         try 
            set(gca, 'CLim', [vMin vMax])
         catch
            fprintf(1, 'FSI_SLICEZ warning: No valid data found at %s m depth\n', num2str(action));
         end
         
         view(2)
         hold on
         plot3(UserData.data(:, xColumn), ...
            UserData.data(:, yColumn), ...
            UserData.data(:, zColumn), 'k.', 'Markersize', 1)
         hold off
         
         xlabel('\bf{Longitude}');
         ylabel('\bf{Latitude}');
         shading interp 
         %colorbar2
         
         
         % Make ArcView Products
         ButtonName=questdlg('Do you wish to make ArcView data products?', ...
            'ArcView Products?', 'Yes','No','No');
         
         
         switch ButtonName,
         case 'Yes', 
            
            filename_ = [filename '_' num2str(round(action))];
            
            fprintf(1, 'Writing ArcView ASCII Raster data to %s.txt ...', filename_ );
            writeArcRaster([filename_ '.asc'], ...
               XI(:,:,good), ...
               YI(:,:,good), ...
               VI(:,:,good));
            fprintf(1, 'completed\n');
            
            fprintf(1, 'Writing ArcView jpeg to %s.jpg and %s.jgw ...', filename_, filename_ );
            arcimage([filename_ ]);
            fprintf(1, 'completed\n');
         case 'No',
            % Do nothing
            
         end % switch
         
         
      end
      
      
   end
   
   
end



%%%%%%%%%%%%%%%%%%%%%
function LocalInitFig
% Creates the main figure

ScreenSize = get(0, 'ScreenSize');
figH = findobj('Tag', 'Main');
UserData = get(figH, 'UserData');
zColumn  = strmatch('depth', UserData.columns);
p    = readProperties('runfsi.properties');
zInc = sscanf(getProperty(p, 'zinc'), '%f');

badH = findobj('Tag', 'fsi_slicez');
if ~isempty(badH)
   delete(badH)
end

fH = figure('FileName','fsi_slicez.m', ...
   'Tag','fsi_slicez', ...
   'ToolBar','none');

set(fH, 'UserData', []);

d = [min(UserData.data(:, zColumn)):zInc:max(UserData.data(:, zColumn))];
% Create labelS
labelS = sprintf('''>&%3.1f'',',abs(d));     % turn d into an evaluable string of depths
labelS(end) = 32;                            % change last character from comma to blank
labelS = ['''&Depth'',',labelS];             % add &Depth to the start of the string
labelS = eval(['str2mat(' labelS ')']);      % Change to a character matrix
% create callback strings  
% turn d into evaluable string of calls to viewadcp
callS = sprintf('''fsi_slicez(%3.1f);'',',d);  
callS(end) = 32;                             % change last character from comma to blank
callS = [''''',',callS];                     % &Depth callback must be empty ''
callS = eval(['str2mat(' callS ')']);        % change to a character matrix
makemenu(fH,labelS,callS);                   % create menu
fsi_slicez(d(1));                           % call fsi_select at the shallowest depth
