Par input.m

From Pnb
Jump to navigation Jump to search
%PAR_INPUT.M
%11/4/99 code pulled from goplot4.m
%parfile,gp,dp
%called by goac9_tiho Added by Erik S 6/6/2010

function [par,parnames]=par_input(varargin)
%assign command line parameters to local variables
disp('in par_input')
narg=length(varargin);
varargin;
nrow=narg/2;
if nrow==round(nrow),
    for iarg=1:2:length(varargin),
        eval([varargin{iarg} '= varargin{iarg+1};']);
    end
else
    display('par_input: command line pairs not correctly formed');
    return
end
%local parameters, if passed a parfile
%parfile
[lp,ll]=read_struc(parfile);
if isempty(lp),
    disp(['par_input: local par structure is empty, check ' parfile]);
end
%does the local file give a defaults file
if isfield(lp,'defaults'),
    
   switch lp.defaults
        case ''
            disp('par_input: default par structure is empty, use lp structure');
            dp=lp;
            dl=ll;
        case 'program'
            %don't look for a defaults file, the dp array is passed to this function.
            if ~exist(dp),
                disp('par_input: default par structure is empty, not passed to par_input');
                dp=[];
                dl=[];
            else
                dl=fieldnames(dp);
            end
        otherwise
            %load defaults file if specified, determine format by ext.
           dp=[];
            if findstr(lp.defaults,'.par'),%only look for .par
                %lpdefaultsis_beforereadstruc = lp.defaults
                [dp,dl]=read_struc(lp.defaults);
               
           % return
            elseif findstr(lp.defaults,'.mat'),  %only look for .mat
                load(lp.defaults);
            else                                 %update .mat if .par is newer
                m=dir([lp.defaults,'.mat']);
                if isempty(m),
                    m(1).date=0;
                end
                p=dir([lp.defaults,'.par']);
                if isempty(m) | datenum(p.date) > datenum(m.date),
                    [dp,dl]=read_struc([lp.defaults,'.par']);
                    save(lp.defaults,'dp');
                else
                    load(lp.defaults);
                end
            end
            if isempty(dp),
                disp(['par_input: default par  structure is empty, ' lp.defaults , ...
                    ' not found']);
            end
   end
end
if ~exist('dp') | isempty(dp),
    if exist('def_file'),
        lp.defaults=def_file
        %load defaults file if specified, determine format by ext.
        dp=[];
        if findstr(lp.defaults,'.par'),      %only look for .par
            [dp,dl]=read_struc(lp.defaults);
        elseif findstr(lp.defaults,'.mat'),  %only look for .mat
            load(lp.defaults);
        else                                 %update .mat if .par is newer
            m=dir([lp.defaults,'.mat']);
            if isempty(m),
                m.date=0;
            end
            p=dir([lp.defaults,'.par']);
         
            if isempty(m) | datenum(p.date) > datenum(m.date),
                [dp,dl]=read_struc([lp.defaults,'.par']);
                save(lp.defaults,'dp');
            else
                load(lp.defaults);
            end
        end
        if isempty(dp),
            disp(['par_input: default par structure is empty, ' lp.defaults , ...
                ' not found']);
        end
    end
end

if ~exist('dp') | isempty(dp),
    disp(['par_input: default par structure is empty, using local values']);
    dp=lp;
    dl=ll;
end
%allows local par file to override defalut file for use of local
%pars
%_ | '' | 'y' | 'n'
if isfield(lp,'local_pars'),
    if ~isempty(lp.local_pars),
        dp.local_pars=lp.local_pars;
    end
end
%default to use loacl pars if not specified in default par file
%_ | '' | 'y' | 'n'
if isfield(dp,'local_pars'),
    if isempty(dp.local_pars),
        dp.local_pars='y';
    end
else
    dp.local_pars='y';
end
if dp.local_pars=='y',
   % disp('here')
    %update dp.defaults with local values, y|n
    for iname=1:size(ll,1),
        eval(['dp.' parse(ll(iname,:),1) '=lp.' ll(iname,:) ';']);
    end
    dl=fieldnames(dp);
end
%global parameters: overwrite,add
if exist('gp'),
    gl=char(fieldnames(gp));
    for iname=1:size(gl,1),
        eval(['dp.' parse(gl(iname,:),1) '=gp.' gl(iname,:) ';']);
    end
end
if isempty(dp),
    disp('par_input: returning empty par structure');
    par=[]
    parnames=[];
else
    [par,parnames]=sortstruct(dp);
    parnames=char(parnames);
end

return

par_input