function retstr = Cl36_input_v3(in)

% This is the wrapper script for the v3 Cl-36 exposure age calculator.
% Copyright: Greg Balco, Berkeley Geochronology Center
% February, 2019
% Not licensed for use or distribution. 
%
% List of things to do:
%   LSDn scaling grids -- Nat?
%   Ca, K elemental production rates calibration
%   Muon cross-sections calibration
%   Figure out what to use for Pf0
%   Enter some calibration data for above
%   Figure out what to do with LOI - Irene?
%   Make the return_profile_plots code function without redundant scaling calls
% 

% Load constants

load Cl36_consts;

% Get in correct directory and acquire path to v3 stuff

if consts.isLocal == 0
    % Case running on Linux web server
    cd /var/www/html/math/Cl36/v3
    path(path,'/var/www/html/math/v3/')
else
    % Case on GB's Mac, no cd but declare globals for diagnostics
    global ages;
    path(path,'/Users/balcs/sw/calcs_v3/v3/core/');
end

% Define version

versions.wrapper = 'Cl36_input_v3 - 0.1-dev';


% ------------------------- VALIDATE INPUT DATA --------------------

% Things to validate are as follows. 
% 1. in.text_block - input text
% 2. in.reportType - 'HTML' or 'XML'
% 3. in.plotFlag - 'yes' or 'no'
% 4. in.summaryFlag - 'yes' or 'no'

% Validate and parse main text block

d = validate_v3_Cl36(in.text_block);

% Return errors from validation as appropriate

if d.error
    if strcmp(in.reportType,'XML')
        retstr = dump_error_XML(d.message);
    else
        retstr = dump_error_HTML(d.message);
    end
    if consts.isLocal == 1
        disp(d.message);
    end
    return;
end

% Validate report type field
if isfield(in,'reportType')
    % Case field present
    if ~(strcmp(in.reportType,'XML') || strcmp(in.reportType,'HTML'))
        message = ('Unrecognized value for report type field');
        retstr = dump_error_XML(message);
        if consts.isLocal == 1
            % Dump to command window
            disp(message);
        end
        return;
    end
else
    % Otherwise default to HTML
    in.reportType = 'HTML';
end

% Field to determine whether plotting is needed for XML output

internalPlotFlag = 'no'; % default no plotting

if isfield(in,'plotFlag')
    if strcmp(in.plotFlag,'yes') || strcmp(in.plotFlag,'no')
        % Pass; set internal flag accordingly
        internalPlotFlag = in.plotFlag;
    else
        message = ('Unrecognized value for XML plot field');
        retstr = dump_error_XML(message);
        if consts.isLocal == 1
            % Dump to command window
            disp(message);
        end
        return;
    end
end

% Field to determine whether summary stats and camelplot are wanted

internalSummaryFlag = 'no'; % default no summary

if isfield(in,'summary')
    if strcmp(in.summary,'yes')
        % Pass; set internal flag accordingly
        internalSummaryFlag = 'yes'; 
    elseif strcmp(in.summary,'no')
        % do nothing, already set
    else
        message = ('Unrecognized value for summary switch field');
        retstr = dump_error_XML(message);
        if consts.isLocal == 1
            % Dump to command window
            disp(message);
        end
        return;
    end
end     

% ---------------- DONE VALIDATING INPUT DATA ----------------------

% ------------ CALCULATE EXPOSURE AGES -----------------------------

% Now we should have a properly validated input data set. 
% Pass this to the age calculation script. 

% remember d is the validated input data block. 

control.cal = 0; 
% This isn't doing anything at the moment. 

% Now calculate ages. 

ages = get_Cl36_ages_v3(d,control,consts); % Note pass consts. 

% List what is in here

versions.get_age = ages.version_ages;
versions.muons = 'Exponential approximation per Balco (2019)';
versions.validate = d.version_validate;
versions.consts = consts.version;
versions.params = ages.profile{1}.version_params;
versions.profile = ages.profile{1}.version_profile;

versions.Rcfile = consts.RCfname;
versions.Sfile = consts.Sfname;

% --------------- DONE CALCULATING EXPOSURE AGES ---------------------

% ------------------- WRITE TO LOG -----------------------------------

% Get IP address
this_ip = getenv('REMOTE_ADDR');

if consts.isLocal == 0 
    % Write to the log file. 
    for a = 1:ages.numcls
        temp_log_string = [char(ages.s.sample_name{ages.n.index(a)}) ' ' sprintf('%0.4f',ages.s.lat(ages.n.index(a))) ' ' sprintf('%0.4f',ages.s.long(ages.n.index(a)))];
        what_to_log = [this_ip ' ' in.reportType ' ' temp_log_string];
        write_to_log_v3('Cl36_input_v3.m',what_to_log);
    end
end
    
% ------------------ DONE WRITING TO LOG -----------------------------

% --------- MAKE NUCLIDE-NUCLIDE PLOTS (for qtz pairs) ---------------

% Note this only executes if internalPlotFlag is 'yes'. 

if strcmp(internalPlotFlag,'yes')
    
    % -------------- MAKE PROFILE PLOTS ---------------------------

    profile_plots = return_profile_plots(ages,consts);

    % ----------------- DONE MAKING PROFILE PLOTS -----------------
    
else
    % This is case internalPlotFlag is 'no'; set dummy variables for plots
    % that would have been made
    profile_plots = {};
end

% --------------------  DONE MAKING PLOTS OR NOT ------------------------

% ----------------- SUMMARIZE DATA AND MAKE CAMELPLOT IF NEEDED ----------

if strcmp(internalSummaryFlag,'yes')
    % Calculate stats for each nuclide separately as well as all nuclides,
    % all samples. This also makes camelplots. 
    % NOTE must make sure summarize_ages recognizes 'Cl36'. 
    
    if strcmp(internalPlotFlag,'yes')
        summary = summarize_ages(ages,1);
    else
        summary = summarize_ages(ages,0);
    end
else
    summary = [];    
end

% ------------------- DONE WITH SUMMARY AND CAMELPLOT --------------------

% -------------------- RETURN HTML OR XML ----------------------------


if strcmp(in.reportType,'XML')
    retstr = Cl36agesToXML(ages,versions,profile_plots,summary);
else
    retstr = Cl36agesToHTML(ages,versions,profile_plots,summary);
end


% -------------------------------- DONE ------------------------------








