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 | % trunkprefix:prefix of previous run with a different prefix. Used to branch.
|
---|
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 |
|
---|
15 | classdef organizer < handle
|
---|
16 | properties (SetAccess=private)
|
---|
17 | % {{{
|
---|
18 | currentstep =0;
|
---|
19 | end
|
---|
20 | properties (SetAccess=public)
|
---|
21 | repository ='./';
|
---|
22 | prefix ='model.';
|
---|
23 | trunkprefix ='';
|
---|
24 | steps =[];
|
---|
25 | requestedsteps=[0];
|
---|
26 | upload_server='';
|
---|
27 | upload_path='';
|
---|
28 | upload_login='';
|
---|
29 | upload_port=0;
|
---|
30 | download=0;
|
---|
31 | %}}}
|
---|
32 | end
|
---|
33 | methods
|
---|
34 | function org=organizer(varargin) % {{{
|
---|
35 |
|
---|
36 | %process options
|
---|
37 | options=pairoptions(varargin{:});
|
---|
38 |
|
---|
39 | %Get prefix
|
---|
40 | prefix=getfieldvalue(options,'prefix','model.');
|
---|
41 | if ~ischar(prefix), error('prefix is not a string'); end
|
---|
42 | if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
|
---|
43 | org.prefix=prefix;
|
---|
44 |
|
---|
45 | %Get repository
|
---|
46 | repository=getfieldvalue(options,'repository','./');
|
---|
47 | if ~ischar(repository), error('repository is not a string'); end
|
---|
48 | if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
|
---|
49 | org.repository=repository;
|
---|
50 |
|
---|
51 | %Get steps
|
---|
52 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
53 |
|
---|
54 | %Get trunk prefix (only if provided by user)
|
---|
55 | if exist(options,'trunkprefix'),
|
---|
56 | trunkprefix=getfieldvalue(options,'trunkprefix','');
|
---|
57 | if ~ischar(trunkprefix), error('trunkprefix is not a string'); end
|
---|
58 | if ~strcmp(regexprep(trunkprefix,'\s+',''),trunkprefix), error('trunkprefix should not have any white space'); end
|
---|
59 | org.trunkprefix=trunkprefix;
|
---|
60 | end
|
---|
61 |
|
---|
62 | %Get upload information, if provided
|
---|
63 | org.upload_server=getfieldvalue(options,'upload_server','');
|
---|
64 | org.upload_path=getfieldvalue(options,'upload_path','');
|
---|
65 | org.upload_login=getfieldvalue(options,'upload_login','');
|
---|
66 | org.upload_port=getfieldvalue(options,'upload_port',0);
|
---|
67 | org.download=getfieldvalue(options,'download',0);
|
---|
68 |
|
---|
69 | end
|
---|
70 | %}}}
|
---|
71 | function disp(org) % {{{
|
---|
72 | disp(sprintf(' Repository: ''%s''',org.repository));
|
---|
73 | disp(sprintf(' Prefix: ''%s''',org.prefix));
|
---|
74 | if isempty(org.steps)
|
---|
75 | disp(' no step');
|
---|
76 | else
|
---|
77 | for i=1:length(org.steps),
|
---|
78 | disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
|
---|
79 | end
|
---|
80 | end
|
---|
81 | if isempty(org.upload_server),
|
---|
82 | disp(' no upload info');
|
---|
83 | else
|
---|
84 | disp(sprintf(' upload_server: ''%s''',org.upload_server));
|
---|
85 | disp(sprintf(' upload_login: ''%s''',org.upload_login));
|
---|
86 | disp(sprintf(' upload_path: ''%s''',org.upload_path));
|
---|
87 | disp(sprintf(' upload_port: ''%i''',org.upload_port));
|
---|
88 | end
|
---|
89 | end
|
---|
90 | %}}}
|
---|
91 | function md=load(org,string),% {{{
|
---|
92 |
|
---|
93 | %Get model path
|
---|
94 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
95 | path=[org.repository '/' org.prefix string];
|
---|
96 |
|
---|
97 | %figure out if the model is there
|
---|
98 | if exist(path,'file'),
|
---|
99 | path=path;
|
---|
100 | elseif exist([path '.mat'],'file'),
|
---|
101 | path=[path '.mat'];
|
---|
102 | else
|
---|
103 | error(['Could not find ' path ]);
|
---|
104 | end
|
---|
105 |
|
---|
106 | struc=load(path,'-mat');
|
---|
107 | name=char(fieldnames(struc));
|
---|
108 | md=struc.(name);
|
---|
109 | if nargout,
|
---|
110 | varargout{1}=md;
|
---|
111 | end
|
---|
112 | end%}}}
|
---|
113 | function md=loadmodel(org,string),% {{{
|
---|
114 |
|
---|
115 | %Get model path
|
---|
116 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
117 | path=[org.repository '/' org.prefix string];
|
---|
118 |
|
---|
119 | %figure out if the model is there, otherwise, we have to use the default path supplied by user.
|
---|
120 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
121 | md=loadmodel(path);
|
---|
122 | return;
|
---|
123 | end
|
---|
124 |
|
---|
125 | %If we are here, the model has not been found. Try trunk prefix if provided
|
---|
126 | if ~isempty(org.trunkprefix),
|
---|
127 | path2=[org.repository '/' org.trunkprefix string];
|
---|
128 | if ~exist(path2,'file'),
|
---|
129 | error(['Could find neither ' path ', nor ' path2]);
|
---|
130 | else
|
---|
131 | disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
|
---|
132 | md=loadmodel(path2);
|
---|
133 | return;
|
---|
134 | end
|
---|
135 | else
|
---|
136 | error(['Could not find ' path ]);
|
---|
137 | end
|
---|
138 | end%}}}
|
---|
139 | function loaddata(org,string),% {{{
|
---|
140 |
|
---|
141 | %Get model path
|
---|
142 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
143 | path=[org.repository '/' org.prefix string];
|
---|
144 |
|
---|
145 | %figure out if the data is there, otherwise, we have to use the default path supplied by user.
|
---|
146 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
147 | evalin('caller',['load -mat ' path]);
|
---|
148 | return;
|
---|
149 | end
|
---|
150 |
|
---|
151 | %If we are here, the data has not been found. Try trunk prefix if provided
|
---|
152 | if ~isempty(org.trunkprefix),
|
---|
153 | path2=[org.repository '/' org.trunkprefix string];
|
---|
154 | if ~exist(path2,'file'),
|
---|
155 | error(['Could find neither ' path ', nor ' path2]);
|
---|
156 | else
|
---|
157 | disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
|
---|
158 | evalin('caller',['load -mat ' path2]);
|
---|
159 | return;
|
---|
160 | end
|
---|
161 | else
|
---|
162 | error(['Could not find ' path ]);
|
---|
163 | end
|
---|
164 | end%}}}
|
---|
165 | function bool=perform(org,string) % {{{
|
---|
166 |
|
---|
167 | bool=false;
|
---|
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 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
192 | bool=true;
|
---|
193 | end
|
---|
194 |
|
---|
195 | %But if download is requested, we are downloading and skipping the step:
|
---|
196 | if ismember(org.currentstep,org.requestedsteps) & org.download,
|
---|
197 | %load the model if it exists, and download
|
---|
198 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
199 | if exist(name,'file'),
|
---|
200 | md=loadmodel(name);
|
---|
201 | if isa(md,'model'),
|
---|
202 | if ~isempty(md.settings.upload_filename),
|
---|
203 | disp(sprintf(' downloading model'));
|
---|
204 | md=download(md);
|
---|
205 | save(name,'md','-v7.3');
|
---|
206 | end
|
---|
207 | end
|
---|
208 | end
|
---|
209 |
|
---|
210 | %reset bool to false, so we stick with only downloading
|
---|
211 | bool=false;
|
---|
212 | end
|
---|
213 |
|
---|
214 | end%}}}
|
---|
215 | function savemodel(org,md) % {{{
|
---|
216 |
|
---|
217 | %check
|
---|
218 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
219 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
220 |
|
---|
221 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
222 | disp(['saving model as: ' name]);
|
---|
223 |
|
---|
224 | %check that md is a model
|
---|
225 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
---|
226 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
227 |
|
---|
228 | %save model
|
---|
229 | save(name,'md','-v7.3');
|
---|
230 | end%}}}
|
---|
231 | function savedata(org,varargin) % {{{
|
---|
232 |
|
---|
233 | %check
|
---|
234 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
235 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
236 |
|
---|
237 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
238 | disp(['saving data in: ' name]);
|
---|
239 |
|
---|
240 | %check that md is a model
|
---|
241 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
242 |
|
---|
243 | %list of variable names:
|
---|
244 | variables='';
|
---|
245 | for i=2:nargin,
|
---|
246 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
247 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
248 | end
|
---|
249 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
250 | end%}}}
|
---|
251 | end
|
---|
252 | end
|
---|