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

Last change on this file since 21291 was 21291, checked in by Mathieu Morlighem, 8 years ago

CHG: fixed camhpc

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