Changeset 6076
- Timestamp:
- 09/29/10 14:40:18 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk/src/m/classes/runsteps.m
r6072 r6076 8 8 % rs = runsteps('../Models/'); %build an empty runsteps object with a given repository 9 9 % 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 branching11 10 12 11 classdef runsteps … … 15 14 repository='./'; 16 15 prefix ='model.'; 17 defaultprefix ='';18 16 steps =[]; 19 17 %}}} … … 21 19 methods 22 20 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, 51 22 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; 53 36 end 54 37 end 55 38 %}}} 56 39 function disp(rs) % {{{1 57 disp(sprintf('\n%s = \n',inputname(1)));58 40 disp(sprintf(' Repository: ''%s''',rs.repository)); 59 41 disp(sprintf(' Prefix: ''%s''',rs.prefix)); … … 64 46 disp(sprintf(' step #%i',i)); 65 47 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)); 67 49 end 68 50 end 69 51 end 70 52 %}}} 71 function rs = addstep(rs, message),% {{{153 function rs = addstep(rs,string),% {{{1 72 54 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 77 58 78 59 rs.steps(end+1).id=length(rs.steps)+1; 79 rs.steps(end). message=message;60 rs.steps(end).string=string; 80 61 end% }}} 81 62 function bool=echo(rs,steps,varargin)% {{{1 63 disp('WARNING: obsolete function. Use PerformStep instead (see issm/projetcs/scripts/runme.m)'); 82 64 if length(steps)==0, 83 65 error('steps is empty'), … … 87 69 else 88 70 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)); 90 72 end 91 73 end … … 97 79 end 98 80 end%}}} 99 function id=GetId(rs, message) % {{{1100 %GetId - get step id from message81 function id=GetId(rs,string) % {{{1 82 %GetId - get step id from string 101 83 % 102 84 % Usage: … … 104 86 105 87 for i=1:length(rs.steps), 106 if strcmp(rs.steps(i). message,message),88 if strcmp(rs.steps(i).string,string), 107 89 id=i; 108 90 return; 109 91 end 110 92 end 93 error(['No step of flag ' string ' has been found']); 111 94 112 %If we are here, no step with given message has been found113 error(['runsteps error messge: no step with message ''' message ''' has been found ']);114 95 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 117 111 % 118 112 % 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) 179 114 180 115 %Some checks … … 186 121 end 187 122 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)); 190 126 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 200 128 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']); 210 132 end 211 212 %check that md is a model213 if ~isa(md,'model'),214 error('savemodel error message: third argument is not a model')215 end216 217 %check rs length218 133 if (num>length(rs.steps)), 219 134 error(['runsteps error message: element with id ' num2str(num) ' not found']); 220 135 end 221 136 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 228 146 end 229 147 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 230 165 %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 ]; 237 167 save(name,'md','-v7.3'); 238 168 disp(['model saved as: ' name]);
Note:
See TracChangeset
for help on using the changeset viewer.