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

Last change on this file since 24883 was 24883, checked in by Eric.Larour, 5 years ago

CHG: new reset routine to be used when you want organizer to span several files.

File size: 7.3 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 bool=perform(org,varargin) % {{{
159
160 bool=false;
161
162 %group,string are the variable arguments length:
163 if nargin==2,
164 string=varargin{1};
165 elseif nargin==3,
166 string=sprintf('%s.%s',varargin{1},varargin{2});
167 end
168
169 %Some checks
170 if ~ischar(string), error('Step provided should be a string'); end
171 if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
172 if (org.currentstep>0 & ismember({string},{org.steps.string}))
173 error(['Step ' string ' already present. Change name']);
174 end
175
176 %Add step
177 org.steps(end+1).id=length(org.steps)+1;
178 org.steps(end).string=string;
179 org.currentstep=org.currentstep+1;
180
181 %if requestedsteps = 0, print all steps in org
182 if any(org.requestedsteps==0),
183 if org.currentstep==1,
184 disp(sprintf(' prefix: %s',org.prefix));
185 end
186 disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
187 end
188
189 %Ok, now if currentstep is a member of steps, return true
190 if ismember(org.currentstep,org.requestedsteps),
191 if usejava('desktop'),
192 disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
193 else
194 %Print on a red background
195 fprintf(['\n\033[' org.color 'm step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
196 end
197 bool=true;
198 end
199 end%}}}
200 function savemodel(org,md) % {{{
201
202 %check
203 if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
204 if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
205
206 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
207 disp(['saving model as: ' name]);
208
209 %Skip if requested
210 if org.skipio,
211 disp(['WARNING: Skipping saving ' name]);
212 return;
213 end
214
215 %check that md is a model
216 if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
217 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
218
219 %save model
220 save(name,'md','-v7.3');
221 end%}}}
222 function savedata(org,varargin) % {{{
223
224 %check
225 if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
226 if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
227
228 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
229 disp(['saving data in: ' name]);
230
231 %Skip if requested
232 if org.skipio,
233 disp(['WARNING: Skipping saving ' name]);
234 return;
235 end
236
237 %check that md is a model
238 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
239
240 %list of variable names:
241 variables='';
242 for i=2:nargin,
243 variables=[variables ',' '''' inputname(i) ''''];
244 eval([inputname(i) '= varargin{' num2str(i-1) '};']);
245 end
246 eval(['save(''' name '''' variables ',''-v7.3'');']);
247 end%}}}
248 end
249end
Note: See TracBrowser for help on using the repository browser.