function runfsi(action)
% This is the machine-generated representation of a Handle Graphics object
% and its children.  Note that handle values may change when these objects
% are re-created. This may cause problems with any callbacks written to
% depend on the value of the handle at the time the object was saved.
% This problem is solved by saving the output as a FIG-file.
%
% To reopen this object, just type the name of the M-file at the MATLAB
% prompt. The M-file and its associated MAT-file must be on your path.
% 
% NOTE: certain newer features in MATLAB may not have been saved in this
% M-file due to limitations of this format, which has been superseded by
% FIG-files.  Figures which have been annotated using the plot editor tools
% are incompatible with the M-file/MAT-file format, and should be saved as
% FIG-files.

switch nargin
case 0
   figH = findobj('Tag', 'Main');
   if isempty(figH)
      LocalInitFig;
   else
      figure(figH);
   end
   
   
case 1
   figH = findobj('Tag','Main');
   UserData = get(figH,'UserData');
   switch action
   case 'load'
      [infile inpath] = uigetfile('*.*','Select FSI data file',0,0);
      if infile == 0
         return                            % Return if CANCEL is selected
      end
      filename = [inpath infile];
      
      [p f ext] = fileparts(filename);
      
      switch lower(ext)
      case '.mat'
         load(filename)
      otherwise
         try 
            UserData = readTowFish(filename);
            save([p filesep f], 'UserData');
         catch
            fprintf(1, 'Unable to load %s\n', filename)
         end
         
      end
      
      bh3 = findobj(figH, 'Tag', '3dVisButton');
      bh2 = findobj(figH, 'Tag', '2dVisButton');
      enable = 'off';
      if strcmp(lower(UserData.tag), 'fsi')
         enable = 'on';
         set(figH, 'UserData', UserData);
         th = findobj(figH, 'Tag', 'filenameText');
         set(th, 'String', filename)
      end
      set(bh3, 'Enable', enable);
      set(bh2, 'Enable', enable);
      
      % See if the vis-3d box is open, if so update the string
      bh     = findobj('Tag', 'variableName');
      if ~isempty(bh)
         String = strvcat(UserData.columns);
         set(bh, 'String', String, 'Value', 1);
      end
      
      
   case 'plot3d'
      fsi_vis3d;
   case 'plot2d'
   case 'exit'
      fig3d = findobj('tag', 'fsi_vis3d');
      if ~isempty(fig3d)
         delete(fig3d)
      end
      delete(figH)
      
      figISO = findobj('tag', 'fsi_isosurf');
      if ~isempty(figISO)
         delete(figISO)
      end
      
      figZ = findobj('Tag', 'fsi_slicez');
      if ~isempty(figZ)
         delete(figZ)
      end
      
      figXY = findobj('Tag', 'fsi_slicexy');
      if ~isempty(figXY)
         delete(figXY)
      end
      
   end
end

%%%%%%%%%%%%%%%%%%%%%%%
function LocalInitFig

UserData.tag = '';
UserData.filename = '';
ScreenSize = get(0, 'ScreenSize');

h0 = figure('Units','points', ...
   'Color',[0.250980392156863 0 0.250980392156863], ...
   'DeleteFcn','runfsi exit', ...
   'FileName','runfsi.m', ...
   'PaperPosition',[18 180 576 432], ...
   'PaperUnits','points', ...
   'Position',[1 ScreenSize(4)-425 394.5 188.25], ... % [366 206.25 394.5 188.25]
   'Tag','Main', ...
   'ToolBar','none', ...
   'UserData',UserData);

h1 = uicontrol('Parent',h0, ...
   'Units','points', ...
   'BackgroundColor',[0.501960784313725 1 1], ...
   'FontSize',14, ...
   'ListboxTop',0, ...
   'Position',[21.75 155.25 150 18.75], ...
   'String','FSI Data Visualization', ...
   'Style','text', ...
   'Tag','titleText');

h1 = uicontrol('Parent',h0, ...
   'Units','points', ...
   'BackgroundColor',[0.752941176470588 0.701960784313725 0.6039215686274511], ...
   'Callback','runfsi load', ...
   'ListboxTop',0, ...
   'Position',[21.75 124.125 84.75 22.5], ...
   'String','Select File', ...
   'Tag','selectFileButton', ...
   'TooltipString','Select a data file to visualize');

h1 = uicontrol('Parent',h0, ...
   'Units','points', ...
   'BackgroundColor',[0.752941176470588 0.701960784313725 0.6039215686274511], ...
   'Callback','runfsi plot3d', ...
   'Enable', 'off', ...
   'ListboxTop',0, ...
   'Position',[21.75 90 84.75 22.5], ...
   'String','3D visualiation', ...
   'Tag','3dVisButton', ...
   'TooltipString','Create slice or isocontour plots');

h1 = uicontrol('Parent',h0, ...
   'Units','points', ...
   'BackgroundColor',[0.752941176470588 0.701960784313725 0.6039215686274511], ...
   'Enable', 'off', ...
   'Callback','runfsi plot2d', ...
   'ListboxTop',0, ...
   'Position',[21.75 55.5 84.75 22.5], ...
   'String','2D visualiation', ...
   'Tag','2dVisButton', ...
   'TooltipString','Create a Depth-Distance plot');

h1 = uicontrol('Parent',h0, ...
   'Units','points', ...
   'BackgroundColor',[0.752941176470588 0.701960784313725 0.6039215686274511], ...
   'Callback','runfsi exit', ...
   'ListboxTop',0, ...
   'Position',[21.75 20.25 84.75 22.5], ...
   'String','Exit', ...
   'Tag','exitButton', ...
   'TooltipString','Exit');

h1 = uicontrol('Parent',h0, ...
   'Units','points', ...
   'BackgroundColor',[1 1 1], ...
   'FontSize',12, ...
   'ListboxTop',0, ...
   'Position',[117 124.125 271.5 22.5], ...
   'String','', ...
   'Style','text', ...
   'Tag','filenameText');
