source: issm/trunk-jpl/src/m/classes/organizer.m@ 25758

Last change on this file since 25758 was 25758, checked in by jdquinn, 4 years ago

CHG: Missing changes from merge from Eric’s branch

File size: 9.1 KB
Line 
1%ORGANIZER class definition
2%
3% Supported options:
4% repository: directory where all models will be saved
5% prefix: prefix for saved model names
6% steps: requested steps
7% color: color of step title (default is '41;37')
8%
9% Usage:
10% org = organizer(varargin)
11%
12% Examples:
13% org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
14
15classdef organizer < handle
16 properties (SetAccess=private)
17 % {{{
18 currentstep =0;
19 end
20 properties (SetAccess=public)
21 repository ='';
22 prefix ='';
23 color ='';
24 steps =[];
25 skipio = false;
26 requestedsteps=[0];
27 %}}}
28 end
29 methods
30 function org=organizer(varargin) % {{{
31
32 %process options
33 options=pairoptions(varargin{:});
34
35 %Get prefix
36 prefix=getfieldvalue(options,'prefix','model_');
37 if ~ischar(prefix), error('prefix is not a string'); end
38 if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
39 org.prefix=prefix;
40
41 %Get repository
42 repository=getfieldvalue(options,'repository','./');
43 if ~ischar(repository), error('repository is not a string'); end
44 if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
45 org.repository=repository;
46
47 %Color
48 org.color=getfieldvalue(options,'color','41;37');
49
50 %Get steps
51 org.requestedsteps=getfieldvalue(options,'steps',0);
52
53 %Skip io?
54 org.skipio=getfieldvalue(options,'skipio',0);
55 end
56 %}}}
57 function reset(org,varargin) % {{{
58
59 %process options
60 options=pairoptions(varargin{:});
61
62 %Zero out some fields:
63 org.currentstep =0;
64 org.steps=[];
65
66 %get requested step:
67 org.requestedsteps=getfieldvalue(options,'steps',0);
68
69 end
70 %}}}
71 function disp(org) % {{{
72 disp(sprintf(' Repository: ''%s''',org.repository));
73 disp(sprintf(' Prefix: ''%s''\n',org.prefix));
74 disp(sprintf(' Color: ''%s''\n',org.color));
75 disp(sprintf(' skipio: %i\n',org.skipio));
76 if isempty(org.steps)
77 disp(' no step');
78 else
79 for i=1:length(org.steps),
80 disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
81 end
82 end
83 end
84 %}}}
85 function md=load(org,string),% {{{
86
87 %Get model path
88 if ~ischar(string), error('argument provided is not a string'); end
89 path=[org.repository '/' org.prefix string];
90
91 %Skip if requested
92 if org.skipio,
93 disp(['WARNING: Skipping loading ' path]);
94 md = evalin('base', 'md');
95 return;
96 end
97
98 %figure out if the model is there
99 if exist(path,'file'),
100 path=path;
101 elseif exist([path '.mat'],'file'),
102 path=[path '.mat'];
103 else
104 error(['Could not find ' path ]);
105 end
106
107 struc=load(path,'-mat');
108 name=char(fieldnames(struc));
109 md=struc.(name);
110 if nargout,
111 varargout{1}=md;
112 end
113 end%}}}
114 function md=loadmodel(org,string),% {{{
115
116 %Get model path
117 if ~ischar(string), error('argument provided is not a string'); end
118 path=[org.repository '/' org.prefix string];
119
120 %Skip if requested
121 if org.skipio,
122 disp(['WARNING: Skipping loading ' path]);
123 md = evalin('base', 'md');
124 return;
125 end
126
127 %figure out if the model is there, otherwise, we have to use the default path supplied by user.
128 if exist(path,'file') | exist([path '.mat'],'file'),
129 md=loadmodel(path);
130 return;
131 end
132
133 %If we are here, the data has not been found.
134 error(['Could not find ' path ]);
135 end%}}}
136 function loaddata(org,string),% {{{
137
138 %Get model path
139 if ~ischar(string), error('argument provided is not a string'); end
140 path=[org.repository '/' org.prefix string];
141
142 %figure out if the data is there, otherwise, we have to use the default path supplied by user.
143 if exist(path,'file'),
144 path=path;
145 elseif exist([path '.mat'],'file'),
146 path=[path '.mat'];
147 else
148 error(['Could not find ' path ]);
149 end
150 if exist(path,'file')
151 evalin('caller',['load -mat ' path]);
152 return;
153 end
154
155 %If we are here, the data has not been found.
156 error(['Could not find ' path ]);
157 end%}}}
158 function loaddatanoprefix(org,string),% {{{
159
160 %Get model path
161 if ~ischar(string), error('argument provided is not a string'); end
162 path=[org.repository '/' string];
163
164 %figure out if the data is there, otherwise, we have to use the default path supplied by user.
165 if exist(path,'file'),
166 path=path;
167 elseif exist([path '.mat'],'file'),
168 path=[path '.mat'];
169 else
170 error(['Could not find ' path ]);
171 end
172 if exist(path,'file')
173 evalin('caller',['load -mat ' path]);
174 return;
175 end
176
177 %If we are here, the data has not been found.
178 error(['Could not find ' path ]);
179 end%}}}
180 function bool=perform(org,varargin) % {{{
181
182 bool=false;
183
184 %group,string are the variable arguments length:
185 if nargin==2,
186 string=varargin{1};
187 elseif nargin==3,
188 string=sprintf('%s.%s',varargin{1},varargin{2});
189 end
190
191 %Some checks
192 if ~ischar(string), error('Step provided should be a string'); end
193 if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
194 if (org.currentstep>0 & ismember({string},{org.steps.string}))
195 error(['Step ' string ' already present. Change name']);
196 end
197
198 %Add step
199 org.steps(end+1).id=length(org.steps)+1;
200 org.steps(end).string=string;
201 org.currentstep=org.currentstep+1;
202
203 %if requestedsteps = 0, print all steps in org
204 if any(org.requestedsteps==0),
205 if org.currentstep==1,
206 disp(sprintf(' prefix: %s',org.prefix));
207 end
208 disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
209 end
210
211 %Ok, now if currentstep is a member of steps, return true
212 if ismember(org.currentstep,org.requestedsteps),
213 if usejava('desktop'),
214 disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
215 else
216 %Print on a red background
217 fprintf(['\n\033[' org.color 'm step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
218 end
219 bool=true;
220
221 %last check: is this step locked?
222 string=org.steps(org.currentstep).string;
223 if length(string)>7,
224 if strcmpi(string(end-5:end),'Locked'),
225 error('organizer: you are trying to run a locked step! Unlock it first!');
226 end
227 end
228 end
229 end%}}}
230 function savemodel(org,md) % {{{
231
232 %check
233 if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
234 if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
235
236 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
237 disp(['saving model as: ' name]);
238
239 %Skip if requested
240 if org.skipio,
241 disp(['WARNING: Skipping saving ' name]);
242 return;
243 end
244
245 %check that md is a model
246 if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
247 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
248
249 %save model
250 save(name,'md','-v7.3');
251 end%}}}
252 function savedata(org,varargin) % {{{
253
254 %check
255 if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
256 if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
257
258 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
259 disp(['saving data in: ' name]);
260
261 %Skip if requested
262 if org.skipio,
263 disp(['WARNING: Skipping saving ' name]);
264 return;
265 end
266
267 %check that md is a model
268 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
269
270 %list of variable names:
271 variables='';
272 for i=2:nargin,
273 variables=[variables ',' '''' inputname(i) ''''];
274 eval([inputname(i) '= varargin{' num2str(i-1) '};']);
275 end
276 eval(['save(''' name '''' variables ',''-v7.3'');']);
277 end%}}}
278 function savedatanoprefix(org,varargin) % {{{
279
280 %check
281 if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
282 if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
283
284 name=[org.repository '/' org.steps(org.currentstep).string ];
285 disp(['saving data in: ' name]);
286
287 %Skip if requested
288 if org.skipio,
289 disp(['WARNING: Skipping saving ' name]);
290 return;
291 end
292
293 %check that md is a model
294 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
295
296 %list of variable names:
297 variables='';
298 for i=2:nargin,
299 variables=[variables ',' '''' inputname(i) ''''];
300 eval([inputname(i) '= varargin{' num2str(i-1) '};']);
301 end
302 eval(['save(''' name '''' variables ',''-v7.3'');']);
303 end%}}}
304 end
305end
Note: See TracBrowser for help on using the repository browser.