%adapted to bookstore problem of XPWu
%bookstore temoplate is stored in C:sens along with utility libray and
%temproray files
%function to run lqns at one set of parameter values

function [h]=modelCalc(par, controlled)

varnames=cell(1,6);
varnames{1,1} = ' par1';   %' nusers';
varnames{1,2} = ' par2';   %' DirDemand';
varnames{1,3} = ' par3';   %' WSDemand'; 
varnames{1,4} = ' par4';   %' appDph1';
varnames{1,5} = ' par5';   %appDph2';
varnames{1,6} = ' par6';   %DBopD';,
varnames{1,7} = ' par7';   %mApp';,
varnames{1,8} = ' par8';   %wsThreads';,
varnames{1,9} = ' par9';   %think';,

%set controlled variable
par(1) = controlled(1);

%replace the strings 'pari' by the values, in template
callstring = 'awk';
for i=1:9
    if(par(i)>0)
        callstring = [callstring ' -v ' varnames{1,i} '=' sprintf('"%f"',par(i)) ];
    end;
end;

% calls to set up a spex model (which sets the value of vairables in the LQ
%   model). For documentation of spex, see
%   www.sce.carleton.ca/rads/lqns/lqn-documentation/
% the utility files are located in a fixed directory for ease of invocation,
%   in this example it is c:\sens\LQWeb\
% templateLQWeb.xlqn is a spex file defining the model, the variables, and
%   the results to be reported
% the values are inserted by awk (a PC version is included, it must have a
%   system path) to give aModelin.xlqn, 
% a system command solves the model, putting the result into a single line
%   of aModelin.res
% the vector of results is extracted by awk into the file runspex.res,
%   which is then read in

callstring = [callstring ' -f c:\sens\LQWeb\inscript.awk c:\sens\LQWeb\templateLQWeb.xlqn >c:\sens\LQWeb\aModelin.xlqn'];
[status,result]=system('erase c:\sens\LQWeb\aModelin.xlqn');
[status,result]=system('erase c:\sens\LQWeb\aModelin.res');
%[status,result]=system('erase c:\documents and settings\cmw\my documents\research\mypapers\08\calib08\matlabtrial\aModelin.res');
[status,result]=system('erase c:\sens\LQWeb\runspex.res');
%callstring
[status,result]=system(callstring);
[status,result]=system('spex c:\sens\LQWeb\aModelin.xlqn') ;
%status
[status,result]=system('copy .\aModelin.res c:\sens\LQWeb\aModelin.res') ;
%[status,result]=system('awk -f c:\sens\outscript.awk c:\documents and settings\cmw\my documents\research\mypapers\08\calib08\matlabtrial\aModelin.res >c:\sens\runspex.res');
[status,result]=system('awk -f c:\sens\LQWeb\outscript.awk c:\sens\LQWeb\aModelin.res >c:\sens\LQWeb\runspex.res');
[h]=textread('c:\sens\LQWeb\runspex.res','%f');