function rs = webpeaks(h,outfile)
%WEBPEAKS sample application of surf peaks.
% RS = WEBPEAKS(H) creates a surf peaks plot and
% returns HTML output in string RS.  Handle H is the
% structure created by matweb.  It contains variables
% from the HTML input form in peaksplot.html.
%

%   Author(s): Y. Habibzai 01-01-98
%   Copyright (c) 1998 by The MathWorks, Inc.
%   $Revision: 1.9 $   $Date: 1998/07/20 15:58:24 $


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 1	Initialize the return string
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rs = char('');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 2	Set directory for storage of graphic files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Change working dir to temp dir on ACS machine
% NOTE: We won't make use of the Matweb variable MLDIR
tempdir = '/var/local/httpd/htdocs/matweb/tmp';
% Uncomment below when running script from a stand-alone test file
%tempdir = '/afs/athena/course/6/6.555/matweb/tmp';
%tempdir = '/var/tmp/dyt'; % on EECS-ATH-15
cd(tempdir);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 3	Get HTML form input variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Get unique identifier (to form file name)
mlid = getfield(h, 'mlid');

% Get the colors.
rcolor = str2double(h.red);
gcolor = str2double(h.green);
bcolor = str2double(h.blue);

% Get perspective.
azimuth = str2double(h.AZ);
elevation = str2double(h.EL);
 
% Get the shading value
if (isfield(h, 'shading'))
   shdng = getfield(h, 'shading');
   if strcmp(shdng, 'Interpolated');  
      shdng = 'interp';
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 4	Perform MATLAB computations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Cleanup jpegs older than 1 hour.
wscleanup('ml*peaks.jpeg', 1);

% Create the graphic.
fhandle = figure('visible','off');
z = peaks;
surf(z);
pos = get(gcf, 'position');
pos(3) = 380;
pos(4) = 310;

l = light('color', [rcolor gcolor bcolor]);
shading(lower(shdng));
lighting phong;
set(gcf, 'Position', pos, 'PaperPosition', [.25 .25 4 3]);
view(azimuth, elevation);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 5	Put output variables in an output structure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Render jpeg and write to file.
drawnow;
s.GraphFileName = sprintf('%speaks.jpeg', mlid);
wsprintjpeg(fhandle, s.GraphFileName);
close all;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STEP 6	Call HTMLREP with output struct and output HTML
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Put name of graphic file into HTML template file.
templatefile = '/afs/athena/course/6/6.555/matweb/dev/webpeaks2.html';
if (nargin == 1)
   rs = htmlrep(s, templatefile);
elseif (nargin == 2)
   rs = htmlrep(s, templatefile, outfile);
end
     


