function eps_to_plot(filename)
% 
% FILE: eps_to_plot.m
%
%	This is an example MATLAB script which uses mexeps() function
%       to read netCDF, classic EPIC or ASCII data file; and make
%       plots of time vs. each variable at each depth.
%
%       The input filename can be one of following file names:
%
%	(1) single netCDF file
%       (2) single Classic EPIC file
%       (3) ASCII data file table
%       (4) pointer file contains netCDf or Classic EPIC file
%
%       Example data files are provided. To run this script, try 
%       these comands:
%
%	1. Read single netCDF file (1-D time series data file)
%
%		>  eps_to_plot('w5n155w.cdf')
%
%	2. Read single Classic EPIC file (1-D time series data file)
%
%		>  eps_to_plot('time110w.epic')
%
%	3. Read pointer file contains netCDF files (2-D and 1-D 
%          data files: t5n155w.cdf and w5n155w.cdf)
%
%		>  eps_to_plot('pointer.file')
%
%	4. Read ASCII data file. (The data file table is ascii.table,
%          the data file name is 0n156e.met.)
%
%		> eps_to_plot('ascii.table')
%
%-----------------------------------------------------------------
%Willa Zhu                           Phone:    206-526-6208
%NOAA/PMEL/OCRD                      FAX:      206-526-6744
%7600 Sand Point Way NE              OMNET:    TAO.PMEL
%Seattle, WA 98115                   Internet: willa@pmel.noaa.gov
%-----------------------------------------------------------------

% reads input data file -----------------------------------------
% The infile can be a pointer file contains a list of data files
% or a single netCDF or classic EPIC file.

infile=filename;
currentfile = mexeps('openr',infile);	%opens pointer file and
				        %first data file.
while(currentfile ~= ' ')
  varlist = mexeps('varlist');		%get variable code list
  for I = 1:length(varlist)
     varshape = mexeps('varshape',varlist(I)); %get variable shape
     x_axis = mexeps('getaxis',varlist(I),1);  %get x-axis value
     y_axis = mexeps('getaxis',varlist(I),2);  %get y-axis value
     z_axis = mexeps('getaxis',varlist(I),3);  %get z-axis value
     t_axis = mexeps('gettaxis',varlist(I));   %get t-axis value

% plot time vs. variable at each depth -------------------------

  % convert time array for plotting: basetime is first time
  % (i.e. first time is 0) and time units is "day".  
     t_day = mexeps('totarray',t_axis,'day');
     basetime = mexeps('totstr',t_axis(1),'YYYY-MM-DD hh:mm:ss');

  %get variable info then define sname_varcode to be variable name
     [sname lname frmt units] = mexeps('inqvar',varlist(I));   
     if(varlist(I) > 0) 
       var_name = [sname '_' int2str(varlist(I))];
     else
       var_name = sname;
     end

  %get variable value at each depth ---------------------------
 
     for J = 1:length(z_axis)
        rng = [1 1; 1 1; J J; 1 length(t_axis)]; % set axis range
        var = mexeps('getvar',varlist(I),rng);   %get variable

     % convert all bad number 1e+35 to NaN
        ind = find(var >= 1e+35);
        if(~isempty(ind))
	   var(ind) = NaN*ones(size(ind));
	end

     % plot time vs. var.
        plot(t_day,var);
	xlabel(['Time (day since ' basetime ')']); 
	ylabel([var_name '(' units ')']);
	title(['Lon = ' num2str(x_axis(1)) '   Lat = ' num2str(y_axis(1)) ...
	       '   Depth = ' num2str(z_axis(J))]);  
	fprintf('Hit Return to continue ...\n');
	pause
     end

  %get slice of variable with all depth in column and time in row 
    if(length(z_axis) > 1) 
     range = [1 1; 1 1; 1 length(z_axis); 1 length(t_axis)];
     var_slice = mexeps('getslice',varlist(I), range, 4, 3);

     % convert all bad number 1e+35 to NaN
     ind = find(var_slice >= 1e+35);
        if(~isempty(ind))
	   var_slice(ind) = NaN*ones(size(ind));
	end

     % make plot var vs. time with for at all depth 
     plot(t_day,var_slice);
     xlabel(['Time (day since ' basetime ')']); 
     ylabel([var_name '(' units ')']);
     title(['Lon = ' num2str(x_axis(1)) '   Lat = ' num2str(y_axis(1)) ...
       '   Depth = ' num2str(z_axis(1)) ' m to ' ... 
                     num2str(z_axis(length(z_axis))) 'm']);  
     fprintf('Hit Return to continue ...\n');
     pause
    end
  end

  % move to the next data file in the pointer file
  currentfile = mexeps('setnextr');
end

mexeps('close');	% close pointer file
