function fsi_isosurf(action)

switch nargin
case 0
   LocalInitFig
case 1
   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 data
      figH     = findobj('Tag','Main');
      UserData = get(figH, 'UserData');
      xColumn  = strmatch('lon', UserData.columns);
      yColumn  = strmatch('lat', UserData.columns);
      zColumn  = strmatch('depth', UserData.columns);
      
      % Get the user selected variable to graph
      fh         = findobj('Tag', 'fsi_isosurf');
      eh         = findobj(fh, 'Tag', 'isoValue');
      isoValue   = sscanf(get(eh, 'String'), '%f');
      if isempty(isoValue)
         error('Invalid value entered...try again');
      end
      
      bh         = findobj('Tag', 'variableName');
      dataColumn = get(bh, 'Value');
      
     
      [p f e] = fileparts(UserData.filename);
      filename = [p filesep f '_' UserData.columns{dataColumn}];
      
      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');
         
      else
         load(filename)
      end
            
      % Create the plot
      figure;
      p = patch(isosurface(XI, YI, ZI, VI, isoValue));
      isonormals(XI, YI, ZI, VI, p)
      set(p, 'FaceColor', 'red', 'EdgeColor', 'none');
      %daspect([1 1 1])
      view(3)
      camlight 
      lighting phong
      set(gca, 'ZDir', 'reverse')
      grid('on')
      
      fh2 = findobj('Tag', 'fsi_isosurf');      % Get this panel
      delete(fh2)
      
   case 'cancel'
      figH = findobj('tag', 'fsi_isosurf');
      delete(figH)
   end
   
end



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

ScreenSize = get(0, 'ScreenSize');
figH = findobj('Tag', 'Main');
UserData = get(figH, 'UserData');

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

% Get the user selected variable to graph
      bh         = findobj('Tag', 'variableName');
      dataColumn = get(bh, 'Value');
      variableName = UserData.columns{dataColumn};
      vMin = min(UserData.data(:, dataColumn));
      vMax = max(UserData.data(:, dataColumn));


h0 = figure('Color',[0.501960784313725 0 0.250980392156863], ...
	'FileName','fsi_isosurf.m', ...
	'PaperPosition',[18 180 576 432], ...
	'PaperUnits','points', ...
	'Position',[162 402 358 156], ...
	'Tag','fsi_isosurf', ...
   'ToolBar','none');

h1 = uicontrol('Parent',h0, ...
	'Units','points', ...
	'BackgroundColor',[0 1 1], ...
	'FontSize',14, ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[11.25 66.75 96 16.5], ...
	'String',['Enter "' variableName '"'], ...
	'Style','text', ...
   'Tag','StaticText1');

h1 = uicontrol('Parent',h0, ...
	'Units','points', ...
	'BackgroundColor',[1 1 1], ...
	'ListboxTop',0, ...
	'Position',[111 66.75 98.25 18], ...
	'Style','edit', ...
   'Tag','isoValue');  % Tag = isoValue

h1 = uicontrol('Parent',h0, ...
	'Units','points', ...
	'BackgroundColor',[0.501960784313725 1 0.501960784313725], ...
	'FontSize',10, ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[10.5 91.5 181.5 12], ...
	'String',['Minimum  = ' num2str(vMin)], ...
	'Style','text', ...
   'Tag','StaticText2');

h1 = uicontrol('Parent',h0, ...
	'Units','points', ...
	'BackgroundColor',[0.501960784313725 1 0.501960784313725], ...
	'FontSize',10, ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[11.25 45.75 181.5 12], ...
	'String',['Maximum = ' num2str(vMax)], ...
	'Style','text', ...
   'Tag','StaticText2');

h1 = uicontrol('Parent',h0, ...
	'Units','points', ...
	'BackgroundColor',[0.752941176470588 0.701960784313725 0.6039215686274511], ...
	'ListboxTop',0, ...
	'Position',[13.5 9 51.75 24], ...
   'String','Apply', ...
   'CallBack', 'fsi_isosurf grid', ...
   'Tag','isoApply');

h1 = uicontrol('Parent',h0, ...
	'Units','points', ...
	'BackgroundColor',[0.752941176470588 0.701960784313725 0.6039215686274511], ...
	'ListboxTop',0, ...
	'Position',[75.75 9 51.75 24], ...
   'String','Cancel', ...
   'CallBack', 'fsi_isosurf cancel', ...
	'Tag','isoCancel');
