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

Last change on this file since 20680 was 20680, checked in by Mathieu Morlighem, 9 years ago

CHG: make color an option of organizer (default is red)

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