Changeset 6076


Ignore:
Timestamp:
09/29/10 14:40:18 (15 years ago)
Author:
Mathieu Morlighem
Message:

Better runme

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/m/classes/runsteps.m

    r6072 r6076  
    88%      rs = runsteps('../Models/');               %build an empty runsteps object with a given repository
    99%      rs = runsteps('../Models/','models.AGU.'); %build an empty runsteps object with a given repository and a prefix
    10 %      rs = runsteps('../Models/','models.AGU.','models.default.Agu.'); %build an empty runsteps object with a given repository, a prefix and a default prefix, in case we are branching
    1110
    1211classdef runsteps
     
    1514                 repository='./';
    1615                 prefix    ='model.';
    17                  defaultprefix    ='';
    1816                 steps     =[];
    1917                 %}}}
     
    2119         methods
    2220                 function rs=runsteps(varargin) % {{{1
    23                          if (nargin==0),
    24                                  %default constructor
    25                          elseif (nargin==1 & ischar(varargin{1})),
    26 
    27                                  %Check repository
    28                                  if exist(varargin{1},'dir')~=7,
    29                                          error(['runsteps constructor error message: repository ' varargin{1} ' is not a directory']),
    30                                  end
    31                                  rs.repository=varargin{1};
    32 
    33                          elseif (nargin==2 & ischar(varargin{1}) & ischar(varargin{2})),
    34 
    35                                  %Check repository
    36                                  if exist(varargin{1},'dir')~=7,
    37                                          error(['runsteps constructor error message: repository ' varargin{1} ' is not a directory']),
    38                                  end
    39                                  rs.repository=varargin{1};
    40                                  rs.prefix    =varargin{2};
    41                          elseif (nargin==3 & ischar(varargin{1}) & ischar(varargin{2}) & ischar(varargin{3})),
    42 
    43                                  %Check repository
    44                                  if exist(varargin{1},'dir')~=7,
    45                                          error(['runsteps constructor error message: repository ' varargin{1} ' is not a directory']),
    46                                  end
    47                                  rs.repository=varargin{1};
    48                                  rs.prefix    =varargin{2};
    49                                  rs.defaultprefix    =varargin{3};
    50                          else
     21                         if nargin>2,
    5122                                 help runsteps
    52                                  error('runsteps constructor error message: bad usage')
     23                                 error('runsteps constructor error message: bad usage');
     24                         end
     25                         if nargin>1,
     26                                 prefix=varargin{2};
     27                                 if ~ischar(prefix),                            error('prefix is not a string'); end
     28                                 if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
     29                                 rs.prefix=prefix;
     30                         end
     31                         if nargin>0,
     32                                 repository=varargin{1};
     33                                 if ~ischar(repository),        error('repository is not a string'); end
     34                                 if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
     35                                 rs.repository=repository;
    5336                         end
    5437                 end
    5538                 %}}}
    5639                 function disp(rs) % {{{1
    57                          disp(sprintf('\n%s = \n',inputname(1)));
    5840                         disp(sprintf('   Repository: ''%s''',rs.repository));
    5941                         disp(sprintf('   Prefix:     ''%s''',rs.prefix));
     
    6446                                         disp(sprintf('   step #%i',i));
    6547                                         disp(sprintf('      id:      %i',      rs.steps(i).id));
    66                                          disp(sprintf('      message: ''%s''\n',rs.steps(i).message));
     48                                         disp(sprintf('      string: ''%s''\n',rs.steps(i).string));
    6749                                 end
    6850                         end
    6951                 end
    7052                 %}}}
    71                  function rs = addstep(rs,message),% {{{1
     53                 function rs = addstep(rs,string),% {{{1
    7254
    73                          %check message
    74                          if ~ischar(message),
    75                                  error('addstep error message: message provided should be a string');
    76                          end
     55                         %check string
     56                         if ~ischar(string),                            error('Step provided should be a string'); end
     57                         if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
    7758
    7859                         rs.steps(end+1).id=length(rs.steps)+1;
    79                          rs.steps(end).message=message;
     60                         rs.steps(end).string=string;
    8061                 end% }}}
    8162                 function bool=echo(rs,steps,varargin)% {{{1
     63                         disp('WARNING: obsolete function. Use PerformStep instead (see issm/projetcs/scripts/runme.m)');
    8264                         if length(steps)==0,
    8365                                 error('steps is empty'),
     
    8769                                 else
    8870                                         for i=1:length(rs.steps),
    89                                                  disp(sprintf('   step #%2i : %s',rs.steps(i).id,rs.steps(i).message));
     71                                                 disp(sprintf('   step #%2i : %s',rs.steps(i).id,rs.steps(i).string));
    9072                                         end
    9173                                 end
     
    9779                         end
    9880                 end%}}}
    99                  function id=GetId(rs,message) % {{{1
    100                          %GetId - get step id from message
     81                 function id=GetId(rs,string) % {{{1
     82                         %GetId - get step id from string
    10183                         %
    10284                         %   Usage:
     
    10486
    10587                         for i=1:length(rs.steps),
    106                                  if strcmp(rs.steps(i).message,message),
     88                                 if strcmp(rs.steps(i).string,string),
    10789                                         id=i;
    10890                                         return;
    10991                                 end
    11092                         end
     93                         error(['No step of flag ' string ' has been found']);
    11194
    112                          %If we are here, no step with given message has been found
    113                          error(['runsteps error messge: no step with message ''' message ''' has been found ']);
    11495                 end%}}}
    115                  function md=loadmodel(rs,step,varargin),% {{{1
    116                          %LOADMODEL - save model for a given step
     96                 function md=loadmodel(rs,string),% {{{1
     97
     98                         %Some checks
     99                         if ~ischar(string), error('argument provided is not a string'); end
     100
     101                         %Get model path
     102                         id=GetId(rs,string);
     103                         path=[rs.repository '/' rs.prefix rs.steps(id).string];
     104
     105                         %figure out if the model is there, otherwise, we have to use the default path supplied by user.
     106                         if ~exist(path,'file'), error(['File ' path ' not found']); end
     107                         md=loadmodel(path);
     108                 end%}}}
     109                 function message(rs,num) % {{{1
     110                         %MESSAGE - display string of rs with id =num
    117111                         %
    118112                         %   Usage:
    119                          %      md=loadmodel(rs,num,varargin)
    120                          %
    121                          %   Examples:
    122                          %      md=loadmodel(rs,num)
    123                          %      md=loadmodel(rs,'previous model')
    124                          %      md=loadmodel(rs,num,'2d')
    125 
    126                          %check inputs
    127                          if (nargin<2 | nargin>3),
    128                                  help loamodel
    129                                  error('loadmodel error message: bad usage');
    130                          end
    131 
    132                          %Get id
    133                          if ischar(step),
    134                                  id=GetId(rs,step);
    135                          else
    136                                  id=step;
    137                          end
    138 
    139                          %check rs length
    140                          if (id>length(rs.steps)),
    141                                  error(['runsteps error message: element with id ' num2str(num) ' not found']);
    142                          end
    143 
    144                          %get type if provided
    145                          if nargin~=3
    146                                  path=[rs.repository '/' rs.prefix num2str(id)];
    147                          elseif ischar(varargin{1}),
    148                                  path=[rs.repository '/' rs.prefix num2str(id) '.' varargin{1} ];
    149                          else
    150                                  error('loadmodel error message: third argument should be a string');
    151                          end
    152                          %figure out if the model is there, otherwise, we have to use the default path supplied by user.
    153                          if exist(path,'file'),
    154 
    155                                  %load model
    156                                  md=loadmodel(path);
    157                          else
    158                                  if isempty(rs.defaultprefix),
    159                                          error('loadmodel error message: cannot find model, and default prefix was not supplied');
    160                                  else
    161 
    162                                          if nargin~=3
    163                                                  path=[rs.repository '/' rs.defaultprefix num2str(id)];
    164                                          elseif ischar(varargin{1}),
    165                                                  path=[rs.repository '/' rs.defaultprefix num2str(id) '.' varargin{1} ];
    166                                          else
    167                                                  error('loadmodel error message: third argument should be a string');
    168                                          end
    169                                  end
    170                                  %load model
    171                                  md=loadmodel(path);
    172                          end
    173                  end%}}}
    174                  function message(rs,num) % {{{1
    175                          %MESSAGE - display message of rs with id =num
    176                          %
    177                          %   Usage:
    178                          %      message(rs,num)
     113                         %      string(rs,num)
    179114
    180115                         %Some checks
     
    186121                         end
    187122
    188                          %Print message
    189                          disp(sprintf('\n   step #%i -> %s\n',rs.steps(num).id,rs.steps(num).message));
     123                         %Print string
     124                         disp('WARNING: obsolete function. Use PerformStep instead (see issm/projetcs/scripts/runme.m');
     125                         disp(sprintf('\n   step #%i -> %s\n',rs.steps(num).id,rs.steps(num).string));
    190126                 end%}}}
    191                  function savemodel(rs,num,varargin) % {{{1
    192                          %SAVEMODEL - save model for a given step
    193                          %
    194                          %   Usage:
    195                          %      savemodel(rs,num,md)
    196                          %
    197                          %   Examples:
    198                          %      savemodel(rs,num,md)
    199                          %      savemodel(rs,num,'2d',md)
     127                 function bool=PerformStep(rs,steps,num) % {{{1
    200128
    201                          %check inputs
    202                          if nargin==3
    203                                  md=varargin{1};
    204                          elseif nargin==4
    205                                  type=varargin{1};
    206                                  md=varargin{2};
    207                          else
    208                                  help loamodel
    209                                  error('savemodel error message: bad usage');
     129                         %Some checks
     130                         if (abs(floor(num))~=num),
     131                                 error(['runsteps error message: the number provided is not a positive integer']);
    210132                         end
    211 
    212                          %check that md is a model
    213                          if ~isa(md,'model'),
    214                                  error('savemodel error message: third argument is not a model')
    215                          end
    216 
    217                          %check rs length
    218133                         if (num>length(rs.steps)),
    219134                                 error(['runsteps error message: element with id ' num2str(num) ' not found']);
    220135                         end
    221136
    222                          %before saving model, try and find a runme.m file, and save it.
    223                          if isnans(md.runmefile),
    224                                  md.runmefile=cell(0,1);
    225                                  md.runmefile{1,1}=char(textread('runme.m','%s','delimiter','\n'));
    226                          else
    227                                  md.runmefile{end+1,1}=char(textread('runme.m','%s','delimiter','\n'));
     137                         bool=false;
     138
     139                         %if steps = 0, print all steps in rs and return false
     140                         if any(steps==0),
     141                                 if num==1,
     142                                         for i=1:length(rs.steps),
     143                                                 disp(sprintf('   step #%2i : %s',rs.steps(i).id,rs.steps(i).string));
     144                                         end
     145                                 end
    228146                         end
    229147
     148                         %Ok, now if num is a member of steps, return true
     149                         if ismember(num,steps),
     150                                 disp(sprintf('\n   step #%i : %s\n',rs.steps(num).id,rs.steps(num).string));
     151                                 bool=true;
     152                         end
     153                 end%}}}
     154                 function savemodel(rs,num,md) % {{{1
     155
     156                         %check that md is a model
     157                         if ~isa(md,'model'),       error('savemodel error message: third argument is not a model'); end
     158                         if (num>length(rs.steps)), error(['runsteps error message: element with id ' num2str(num) ' not found']); end
     159
     160                         %before saving model, try and find a runme.m file, and save it.
     161                         A=dbstack;
     162                         runmefilename=A(2).file;
     163                         md.runmefile=char(textread(runmefilename,'%s','delimiter','\n'));
     164
    230165                         %save model
    231                          if nargin==3,
    232                                  name=[rs.repository '/' rs.prefix num2str(num)];
    233                          else
    234                                  name=[rs.repository '/' rs.prefix num2str(num) '.' type];
    235                          end
    236 
     166                         name=[rs.repository '/' rs.prefix rs.steps(num).string ];
    237167                         save(name,'md','-v7.3');
    238168                         disp(['model saved as: ' name]);
Note: See TracChangeset for help on using the changeset viewer.