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

Last change on this file since 23776 was 23776, checked in by Mathieu Morlighem, 6 years ago

NEW: option to skip io to speed up runme for testing

File size: 7.0 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 disp(org) % {{{
58 disp(sprintf(' Repository: ''%s''',org.repository));
59 disp(sprintf(' Prefix: ''%s''\n',org.prefix));
60 disp(sprintf(' Color: ''%s''\n',org.color));
61 disp(sprintf(' skipio: %i\n',org.skipio));
62 if isempty(org.steps)
63 disp(' no step');
64 else
65 for i=1:length(org.steps),
66 disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
67 end
68 end
69 end
70 %}}}
71 function md=load(org,string),% {{{
72
73 %Get model path
74 if ~ischar(string), error('argument provided is not a string'); end
75 path=[org.repository '/' org.prefix string];
76
77 %Skip if requested
78 if org.skipio,
79 disp(['WARNING: Skipping loading ' path]);
80 md = evalin('base', 'md');
81 return;
82 end
83
84 %figure out if the model is there
85 if exist(path,'file'),
86 path=path;
87 elseif exist([path '.mat'],'file'),
88 path=[path '.mat'];
89 else
90 error(['Could not find ' path ]);
91 end
92
93 struc=load(path,'-mat');
94 name=char(fieldnames(struc));
95 md=struc.(name);
96 if nargout,
97 varargout{1}=md;
98 end
99 end%}}}
100 function md=loadmodel(org,string),% {{{
101
102 %Get model path
103 if ~ischar(string), error('argument provided is not a string'); end
104 path=[org.repository '/' org.prefix string];
105
106 %Skip if requested
107 if org.skipio,
108 disp(['WARNING: Skipping loading ' path]);
109 md = evalin('base', 'md');
110 return;
111 end
112
113 %figure out if the model is there, otherwise, we have to use the default path supplied by user.
114 if exist(path,'file') | exist([path '.mat'],'file'),
115 md=loadmodel(path);
116 return;
117 end
118
119 %If we are here, the data has not been found.
120 error(['Could not find ' path ]);
121 end%}}}
122 function loaddata(org,string),% {{{
123
124 %Get model path
125 if ~ischar(string), error('argument provided is not a string'); end
126 path=[org.repository '/' org.prefix string];
127
128 %figure out if the data is there, otherwise, we have to use the default path supplied by user.
129 if exist(path,'file'),
130 path=path;
131 elseif exist([path '.mat'],'file'),
132 path=[path '.mat'];
133 else
134 error(['Could not find ' path ]);
135 end
136 if exist(path,'file')
137 evalin('caller',['load -mat ' path]);
138 return;
139 end
140
141 %If we are here, the data has not been found.
142 error(['Could not find ' path ]);
143 end%}}}
144 function bool=perform(org,varargin) % {{{
145
146 bool=false;
147
148 %group,string are the variable arguments length:
149 if nargin==2,
150 string=varargin{1};
151 elseif nargin==3,
152 string=sprintf('%s.%s',varargin{1},varargin{2});
153 end
154
155 %Some checks
156 if ~ischar(string), error('Step provided should be a string'); end
157 if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
158 if (org.currentstep>0 & ismember({string},{org.steps.string}))
159 error(['Step ' string ' already present. Change name']);
160 end
161
162 %Add step
163 org.steps(end+1).id=length(org.steps)+1;
164 org.steps(end).string=string;
165 org.currentstep=org.currentstep+1;
166
167 %if requestedsteps = 0, print all steps in org
168 if any(org.requestedsteps==0),
169 if org.currentstep==1,
170 disp(sprintf(' prefix: %s',org.prefix));
171 end
172 disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
173 end
174
175 %Ok, now if currentstep is a member of steps, return true
176 if ismember(org.currentstep,org.requestedsteps),
177 if usejava('desktop'),
178 disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
179 else
180 %Print on a red background
181 fprintf(['\n\033[' org.color 'm step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
182 end
183 bool=true;
184 end
185 end%}}}
186 function savemodel(org,md) % {{{
187
188 %check
189 if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
190 if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
191
192 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
193 disp(['saving model as: ' name]);
194
195 %Skip if requested
196 if org.skipio,
197 disp(['WARNING: Skipping saving ' name]);
198 return;
199 end
200
201 %check that md is a model
202 if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
203 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
204
205 %save model
206 save(name,'md','-v7.3');
207 end%}}}
208 function savedata(org,varargin) % {{{
209
210 %check
211 if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
212 if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
213
214 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
215 disp(['saving data in: ' name]);
216
217 %Skip if requested
218 if org.skipio,
219 disp(['WARNING: Skipping saving ' name]);
220 return;
221 end
222
223 %check that md is a model
224 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
225
226 %list of variable names:
227 variables='';
228 for i=2:nargin,
229 variables=[variables ',' '''' inputname(i) ''''];
230 eval([inputname(i) '= varargin{' num2str(i-1) '};']);
231 end
232 eval(['save(''' name '''' variables ',''-v7.3'');']);
233 end%}}}
234 end
235end
Note: See TracBrowser for help on using the repository browser.