%% Edit AIMMS
% Edits an AIMMS model by manipulating the .ams file
% Currently only works for Parameter objects.
% para is a cell array {Name, Value; ...} where Name is the AIMMS Parameter identifier and Value is the new value to set (can be an array).
% if no solution filename is given, uses the newest .sol file in the project folder
% if nargout == 1, returns props.header (includes objectiveValue)
% if nargout > 1, returns props.quality and data.[AIMMS variables] with values
%%
function data = exec_read_AIMMS_1var(projectpath, modelname, solname)

if nargin < 3 % if solname not given
    d = dir([projectpath '*.sol']); % find all .sol files in project directory
    [~,dx]=sort([d.datenum],'descend'); % sort by age
    solname = d(dx==1).name(1:end-4); % take the newest (remove the .sol)
end

[var_names, con_names] = read_lis([projectpath 'log\' modelname '.lis']); % read variable and constraint names from .lis file

for i=size(var_names,1):-1:1
    if strcmpi('Total_carbon',var_names(i))
        var_ind = i;
        break
    end
end
header = read_sol([projectpath solname '.sol']); % read data from .sol file (xml)
[vars, cons] = read_sol_fast([projectpath solname '.sol']); % read data from .sol file (xml)
values = vars{1,4};
%props = v2struct; % pack into props
strdata = values{var_ind};
data.L1 = str2double(strdata(2:end-1));
data.L2 = str2double(header{2,2});
fprintf('Solution read from %s (Objective: %s).\n',solname,data.L2); % report number of replacements


% size(vars)
% for i=1:size(vars,1)
%     eval(['data.' var_names{i,1} ' = ' num2str(vars{i,2}) ';']); % allocate values to variables under the data struct
% end