function out = Cl36agesToHTML(ages,versions,profile_plots,summary)

% This ingests the age results structure from get_l36_ages_v3 and spits out an
% HTML string that is the results web page.  Various arguments. 
%
% Greg Balco - Berkeley Geochronology Center - February, 2019.
% Development, not licensed for use or distribution. 

if nargin < 4; summary = []; end

load Cl36_consts; % Needed for list of scaling methods?

dstring = [];

nl = char(10);

twidth = '1000';
snwidth = '100';

% Write header

out = ['<!DOCTYPE html>' nl '<html>' nl '<head>' nl '<title>Exposure age calculator v3 results</title>' nl ...       
    '<style>' nl '<!--table {}' nl '.title2{font-family:Arial;font-size:12.0pt;}' nl ...
    '.title{font-family:Arial;font-size:10.0pt;}' nl '.standard{font-family:Arial;font-size:8.0pt;}' nl ... 
    '-->' nl  '</style>' nl '</head>'];

out = [out nl nl '<body><center>'];

% Title table
out = [out nl '<table width=' twidth '>' nl '<tr>' nl '<td class="title2" colspan="2">' nl ...
    '<hr><i><b><p>Online exposure age calculator v3, Cl-36 results</p></b></i><p><i><small>This is the first try at this project. Expect errors.</i></small></p><hr>' nl '</td>' nl '</tr>' ...	
    '</table>'];

% Version table

out = [out nl nl '<table width=' twidth '>' nl '<tr>' nl '<td class=title width=200 valign=top>' ];
out = [out nl 'Version info:' nl '</td>' nl '<td class=standard>'];
fns = fieldnames(versions);
for a = 1:length(fns)
    thisstr = [nl fns{a} ': ' eval(['versions.' fns{a}]) nl '<br>' nl];
    out = [out thisstr];
end
out = [out nl '</td>' nl '</tr>'];
out = [out nl '<tr><td colspan=2><hr></td></tr>' nl '</table>'];

% Age tables. Loop through each scaling scheme. Within each scaling scheme,
% display all nuclide results for each sample sequentially.  

% Build table header line

ths1 = ['<tr class=standard><td width=' snwidth '><b>Sample name</b></td><td><b>Nuclide</b></td>'];
ths2 = ['<tr class=standard><td><br><hr></td><td><br><hr></td>'];
ths3 = '<tr class=standard><td></td><td></td>';
for a = 1:length(consts.sschemes)
    ths1 = [ths1 '<td colspan=3 align=center><b>' consts.sschemes{a} '</b></td>'];
    ths2 = [ths2 '<td colspan=3 align=center>' consts.sspropernames{a} '<br><hr></td>'];    
    ths3 = [ths3 '<td align=right><b>Age (yr)</b><br><hr></td>'];
    ths3 = [ths3 '<td align=right><b>Interr (yr)</b><br><hr></td>'];
    ths3 = [ths3 '<td align=right><b>Exterr (yr)</b><br><hr></td>'];
end
ths1 = [ths1 '</tr>'];
ths2 = [ths2 '</tr>'];
ths3 = [ths3 '</tr>'];

ncols = 2 + 3.*length(consts.sschemes);

% Start building table...
out = [out nl nl '<table width=' twidth '>' nl '<tr>' nl '<td class=title colspan=' int2str(ncols) '>' nl '<b>Exposure age results:</b>' nl '</td>' nl '</tr>' nl];
out = [out nl '<tr><td colspan=' int2str(ncols) '></td></tr>'];

out = [out nl ths1 nl ths2 nl ths3 nl]; % end with newline


% Now loop through ages. For each sample in ages, loop through different
% nuclide measurements and write a table row for all scaling schemes for each. 

for a = 1:ages.numsamples
    nindices = find(ages.n.index == a);
    for b = 1:length(nindices)
        thisi = nindices(b);
        % Begin table row
        out = [out '<tr class=standard>' nl '<td>' ages.s.sample_name{a} '</td>'];
        out = [out nl '<td>Cl-36</td>'];
        % Loop through all scaling methods...
        for c = 1:length(consts.sschemes)
            tfield = ['t_' consts.sschemes{c}];
            ifield = ['delt_int_' consts.sschemes{c}];
            efield = ['delt_ext_' consts.sschemes{c}];
            if isfield(ages.n,tfield)
                % Exists age for this scaling scheme
                out = [out nl '<td align=right>' sprintf('%0.0f',eval(['ages.n.' tfield '(thisi)'])) '</td>'];
                if isfield(ages.n,ifield)
                    out = [out nl '<td align=right>' sprintf('%0.0f',eval(['ages.n.' ifield '(thisi)'])) '</td>'];
                else
                    out = [out nl '<td align=center> -- </td>'];
                end
                if isfield(ages.n,efield)
                    out = [out nl '<td align=right>' sprintf('%0.0f',eval(['ages.n.' efield '(thisi)'])) '</td>'];
                else
                    out = [out nl '<td align=center> -- </td>'];
                end
            else
                out = [out nl '<td align=center> -- </td><td align=center> -- </td><td align=center> -- </td>'];
            end
        end
    end
    % After each sample, write table row with diagnostics
    if isempty(ages.flags{a})
        thisdtext = 'None';
    else
        thisdtext = ages.flags{a};
    end
    out = [out nl '<tr height=3><td class=standard valign=top>Diagnostics: </td><td colspan=' int2str(ncols-1) ' class=standard>' thisdtext '</td></tr>'];
end

% Close table
out = [out nl '<tr><td colspan=' int2str(ncols) '><hr></td></tr>'];
out = [out nl '</table>'];


% If summary data included, make another table with summary data

if ~isempty(summary)
    
    % Start table
    out = [out nl nl '<table width=' twidth '>'];
    out = [out nl '<tr><td class=title>Summary statistics:</td></tr>'];
    

    for sf = {'St','Lm','LSDn'}
            eval(['thiss = summary.all.' char(sf) ';']);           
            if ~isempty(thiss)
                idstr = ['<b>' char(sf) ' scaling, all nuclides</b></br>'];
                out = [out nl '<tr><td class=standard><p>' idstr '</p><blockquote>' thiss.text '</blockquote></td></tr>'];
            end
    end
    out = [out nl '<tr><td><hr></td><tr></table>'];
    
    % Add camelplots
    if isfield(summary,'camels')
        out = [out nl nl '<table width=' twidth '>']; 
        for a = 1:length(summary.camels)
            out = [out nl '<tr>' nl '<td class=standard>' nl '<center><img src="' consts.plotURL summary.camels{a} '.png">'];
            out = [out nl '<br>'];
            out = [out '<p><a href="' consts.plotURL summary.camels{a} '.ps">Postscript file</a> -- '];
            out = [out '<a href="' consts.plotURL summary.camels{a} '.gmt">GMT script</a></p></center>'];
            out = [out nl '</center></td>' nl '</tr>'];
            out = [out nl '<tr><td><hr></td></tr>'];
        end
    end
    
    % Close table
    %out = [out nl '<tr><td><hr></td><tr>']
    out = [out nl '</table>'];
    
end;


% Display production profile plots
% This is another table

% Start table

out = [out nl nl '<table width=' twidth '>'];

for a = 1:length(profile_plots)
    out = [out nl '<tr>' nl '<td class=standard>' nl '<center><img src="' consts.plotURL profile_plots{a} '.png">'];
    out = [out nl '</td>' nl '<td class=standard valign=middle>'];
    out = [out '<a href="' consts.plotURL profile_plots{a} '.ps">Postscript file</a><br>'];
    out = [out '<a href="' consts.plotURL profile_plots{a} '.gmt">GMT script</a></center>'];
    out = [out nl '</td>' nl '</tr>'];
    out = [out nl '<tr><td colspan=2><hr></td></tr>'];
end

% Close table

out = [out nl '</table>'];

% Read back elemental data

out = [out nl nl readback_elemental_data(ages,'yes') nl nl];

% Read back returned Cl-36 parameter data

for a = 1:length(ages.profile)
    this = ages.profile{a}.params;
    sp = ['<table class=standard><tr><td colspan=2 align=center>Parameters readback, sample ' ages.s.sample_name{a} '</td></tr>'];
    fns = fieldnames(this);
    for b = 1:length(fns)
        this_item = getfield(this,fns{b});
        if length(this_item) == 1
            if iscell(this_item)
                sp = [sp '<tr><td>' fns{b} '</td><td>' num2str(this_item{1}) '</td></tr>'];
            else
                sp = [sp '<tr><td>' fns{b} '</td><td>' num2str(getfield(this,fns{b})) '</td></tr>'];
            end
        end
    end
    sp = [sp '</table>'];
    out = [out sp nl nl];
end


% End HTML
out = [out nl '</center></body>' nl '</html>'];



