function writePbeDefinitionForProjection(caseNameBase, approxName, ...
    caseList, nDim)
% write pbe defintion files for projections
%
% This file is strongly dependent on generateProjections.

%% basic procedure
% Write pbe definition file that contains:
%   1) a call to the base case
%   2) load projection data
%   3) overwriting the pbe definition: decomposition, seed population, 
%        growth rate

% get my location
myPath = fileparts(which(mfilename));

for iCase = 1:length(caseList)
    % construct file names
    fileNameProjectionData = [myPath filesep 'database' filesep ...
        caseNameBase '_' num2str(nDim) '_' ...
        approxName '_' ...
          caseList{iCase} '.matx'];
    % PBE definition file
    functionName  = [caseNameBase '_' num2str(nDim) '_' approxName '_' ...
          caseList{iCase}];
    fileNamePbeDef = [myPath filesep 'database' filesep ...
         functionName '.m'];
     
    % open file for writing  
    fileHandle = fopen(fileNamePbeDef, 'w');
    if fileHandle < 0
        error('elk:caseStudy:generalError', 'file open did not work');
    end
    
    % write lines
    fprintf(fileHandle, ['function pbeDef = ' functionName  '()\n']);
    fprintf(fileHandle, '%% automatically generated by writePbeDefinitionForProjection.m\n\n');
    
    fprintf(fileHandle, ['dataApprox = load(''' fileNameProjectionData ''', ''-mat'');\n']);
    fprintf(fileHandle, 'dataApprox = dataApprox.dataApprox;\n');
    fprintf(fileHandle, ['pbeDef = ' caseNameBase ';\n\n']);
    
    fprintf(fileHandle, 'pbeDef.dec = dataApprox.dec;\n\n');
    
    fprintf(fileHandle, 'pbeDef.seeding.positionMatrix = mapFromProperEmbedding(projectVectorDec(...\n');
    fprintf(fileHandle, '    pbeDef.seeding.positionMatrix, pbeDef.dec),pbeDef.dec);\n\n');
    
    fprintf(fileHandle, 'pbeDef.growthRate = @(H, P, y, t)(...\n');
    fprintf(fileHandle, '    pbeDef.growthRate(...\n');
    fprintf(fileHandle, '      mapToProperEmbedding(H, pbeDef.dec, [], P(1,:)), ...\n');
    fprintf(fileHandle, '      P,y,t));\n\n');
    
    fprintf(fileHandle, 'pbeDef.optionStruct.odeSolver = @ode45;\n');
    
    % close file
    fclose(fileHandle);
end

%% update database
tmp = obtainData('init');
