[20211] | 1 | %ORGANIZER class definition
|
---|
[6989] | 2 | %
|
---|
[8171] | 3 | % Supported options:
|
---|
| 4 | % repository: directory where all models will be saved
|
---|
| 5 | % prefix: prefix for saved model names
|
---|
| 6 | % steps: requested steps
|
---|
[20923] | 7 | % color: color of step title (default is '41;37')
|
---|
[8171] | 8 | %
|
---|
[6989] | 9 | % Usage:
|
---|
[20211] | 10 | % org = organizer(varargin)
|
---|
[6989] | 11 | %
|
---|
| 12 | % Examples:
|
---|
[20211] | 13 | % org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
|
---|
[6989] | 14 |
|
---|
[20211] | 15 | classdef organizer < handle
|
---|
[23776] | 16 | properties (SetAccess=private)
|
---|
[13946] | 17 | % {{{
|
---|
| 18 | currentstep =0;
|
---|
| 19 | end
|
---|
[6989] | 20 | properties (SetAccess=public)
|
---|
[20681] | 21 | repository ='';
|
---|
| 22 | prefix ='';
|
---|
| 23 | color ='';
|
---|
[13946] | 24 | steps =[];
|
---|
[23776] | 25 | skipio = false;
|
---|
[13946] | 26 | requestedsteps=[0];
|
---|
| 27 | %}}}
|
---|
| 28 | end
|
---|
| 29 | methods
|
---|
[20211] | 30 | function org=organizer(varargin) % {{{
|
---|
[8171] | 31 |
|
---|
[13946] | 32 | %process options
|
---|
| 33 | options=pairoptions(varargin{:});
|
---|
[8171] | 34 |
|
---|
[13946] | 35 | %Get prefix
|
---|
[20680] | 36 | prefix=getfieldvalue(options,'prefix','model_');
|
---|
[13946] | 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;
|
---|
[8171] | 40 |
|
---|
[13946] | 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;
|
---|
[8171] | 46 |
|
---|
[20680] | 47 | %Color
|
---|
[20923] | 48 | org.color=getfieldvalue(options,'color','41;37');
|
---|
[20680] | 49 |
|
---|
[13946] | 50 | %Get steps
|
---|
| 51 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
[8171] | 52 |
|
---|
[23776] | 53 | %Skip io?
|
---|
| 54 | org.skipio=getfieldvalue(options,'skipio',0);
|
---|
[13946] | 55 | end
|
---|
| 56 | %}}}
|
---|
[24883] | 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 | %}}}
|
---|
[13946] | 71 | function disp(org) % {{{
|
---|
| 72 | disp(sprintf(' Repository: ''%s''',org.repository));
|
---|
[20210] | 73 | disp(sprintf(' Prefix: ''%s''\n',org.prefix));
|
---|
[20680] | 74 | disp(sprintf(' Color: ''%s''\n',org.color));
|
---|
[23776] | 75 | disp(sprintf(' skipio: %i\n',org.skipio));
|
---|
[13946] | 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),% {{{
|
---|
[12648] | 86 |
|
---|
[13946] | 87 | %Get model path
|
---|
| 88 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 89 | path=[org.repository '/' org.prefix string];
|
---|
[12648] | 90 |
|
---|
[23776] | 91 | %Skip if requested
|
---|
| 92 | if org.skipio,
|
---|
| 93 | disp(['WARNING: Skipping loading ' path]);
|
---|
| 94 | md = evalin('base', 'md');
|
---|
| 95 | return;
|
---|
| 96 | end
|
---|
| 97 |
|
---|
[13946] | 98 | %figure out if the model is there
|
---|
| 99 | if exist(path,'file'),
|
---|
[17259] | 100 | path=path;
|
---|
| 101 | elseif exist([path '.mat'],'file'),
|
---|
| 102 | path=[path '.mat'];
|
---|
[13946] | 103 | else
|
---|
| 104 | error(['Could not find ' path ]);
|
---|
| 105 | end
|
---|
[17259] | 106 |
|
---|
| 107 | struc=load(path,'-mat');
|
---|
| 108 | name=char(fieldnames(struc));
|
---|
| 109 | md=struc.(name);
|
---|
| 110 | if nargout,
|
---|
| 111 | varargout{1}=md;
|
---|
| 112 | end
|
---|
[13946] | 113 | end%}}}
|
---|
| 114 | function md=loadmodel(org,string),% {{{
|
---|
[6989] | 115 |
|
---|
[13946] | 116 | %Get model path
|
---|
| 117 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 118 | path=[org.repository '/' org.prefix string];
|
---|
[6989] | 119 |
|
---|
[23776] | 120 | %Skip if requested
|
---|
| 121 | if org.skipio,
|
---|
| 122 | disp(['WARNING: Skipping loading ' path]);
|
---|
| 123 | md = evalin('base', 'md');
|
---|
| 124 | return;
|
---|
| 125 | end
|
---|
| 126 |
|
---|
[13946] | 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
|
---|
[6989] | 132 |
|
---|
[20210] | 133 | %If we are here, the data has not been found.
|
---|
| 134 | error(['Could not find ' path ]);
|
---|
[13946] | 135 | end%}}}
|
---|
[15786] | 136 | function loaddata(org,string),% {{{
|
---|
[14278] | 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.
|
---|
[21291] | 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')
|
---|
[14278] | 151 | evalin('caller',['load -mat ' path]);
|
---|
| 152 | return;
|
---|
| 153 | end
|
---|
| 154 |
|
---|
[20210] | 155 | %If we are here, the data has not been found.
|
---|
| 156 | error(['Could not find ' path ]);
|
---|
[14278] | 157 | end%}}}
|
---|
[25758] | 158 | function loaddatanoprefix(org,string),% {{{
|
---|
| 159 |
|
---|
| 160 | %Get model path
|
---|
| 161 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 162 | path=[org.repository '/' string];
|
---|
| 163 |
|
---|
| 164 | %figure out if the data is there, otherwise, we have to use the default path supplied by user.
|
---|
| 165 | if exist(path,'file'),
|
---|
| 166 | path=path;
|
---|
| 167 | elseif exist([path '.mat'],'file'),
|
---|
| 168 | path=[path '.mat'];
|
---|
| 169 | else
|
---|
| 170 | error(['Could not find ' path ]);
|
---|
| 171 | end
|
---|
| 172 | if exist(path,'file')
|
---|
| 173 | evalin('caller',['load -mat ' path]);
|
---|
| 174 | return;
|
---|
| 175 | end
|
---|
| 176 |
|
---|
| 177 | %If we are here, the data has not been found.
|
---|
| 178 | error(['Could not find ' path ]);
|
---|
| 179 | end%}}}
|
---|
[20210] | 180 | function bool=perform(org,varargin) % {{{
|
---|
[13646] | 181 |
|
---|
[13946] | 182 | bool=false;
|
---|
[20210] | 183 |
|
---|
| 184 | %group,string are the variable arguments length:
|
---|
| 185 | if nargin==2,
|
---|
| 186 | string=varargin{1};
|
---|
| 187 | elseif nargin==3,
|
---|
| 188 | string=sprintf('%s.%s',varargin{1},varargin{2});
|
---|
| 189 | end
|
---|
[6989] | 190 |
|
---|
[13946] | 191 | %Some checks
|
---|
| 192 | if ~ischar(string), error('Step provided should be a string'); end
|
---|
| 193 | if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
|
---|
| 194 | if (org.currentstep>0 & ismember({string},{org.steps.string}))
|
---|
| 195 | error(['Step ' string ' already present. Change name']);
|
---|
| 196 | end
|
---|
[6989] | 197 |
|
---|
[13946] | 198 | %Add step
|
---|
| 199 | org.steps(end+1).id=length(org.steps)+1;
|
---|
| 200 | org.steps(end).string=string;
|
---|
| 201 | org.currentstep=org.currentstep+1;
|
---|
[6989] | 202 |
|
---|
[13946] | 203 | %if requestedsteps = 0, print all steps in org
|
---|
| 204 | if any(org.requestedsteps==0),
|
---|
| 205 | if org.currentstep==1,
|
---|
| 206 | disp(sprintf(' prefix: %s',org.prefix));
|
---|
| 207 | end
|
---|
| 208 | disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 209 | end
|
---|
[6989] | 210 |
|
---|
[13946] | 211 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 212 | if ismember(org.currentstep,org.requestedsteps),
|
---|
[20609] | 213 | if usejava('desktop'),
|
---|
| 214 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 215 | else
|
---|
| 216 | %Print on a red background
|
---|
[20699] | 217 | fprintf(['\n\033[' org.color 'm step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
|
---|
[20609] | 218 | end
|
---|
[13946] | 219 | bool=true;
|
---|
[25120] | 220 |
|
---|
| 221 | %last check: is this step locked?
|
---|
| 222 | string=org.steps(org.currentstep).string;
|
---|
| 223 | if length(string)>7,
|
---|
| 224 | if strcmpi(string(end-5:end),'Locked'),
|
---|
| 225 | error('organizer: you are trying to run a locked step! Unlock it first!');
|
---|
| 226 | end
|
---|
| 227 | end
|
---|
[13946] | 228 | end
|
---|
| 229 | end%}}}
|
---|
| 230 | function savemodel(org,md) % {{{
|
---|
[6989] | 231 |
|
---|
[13946] | 232 | %check
|
---|
| 233 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 234 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
[8151] | 235 |
|
---|
[13946] | 236 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 237 | disp(['saving model as: ' name]);
|
---|
[13646] | 238 |
|
---|
[23776] | 239 | %Skip if requested
|
---|
| 240 | if org.skipio,
|
---|
| 241 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 242 | return;
|
---|
| 243 | end
|
---|
| 244 |
|
---|
[13946] | 245 | %check that md is a model
|
---|
[20130] | 246 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
---|
[13946] | 247 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 248 |
|
---|
[13946] | 249 | %save model
|
---|
| 250 | save(name,'md','-v7.3');
|
---|
| 251 | end%}}}
|
---|
[14278] | 252 | function savedata(org,varargin) % {{{
|
---|
| 253 |
|
---|
| 254 | %check
|
---|
| 255 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 256 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 257 |
|
---|
| 258 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 259 | disp(['saving data in: ' name]);
|
---|
| 260 |
|
---|
[23776] | 261 | %Skip if requested
|
---|
| 262 | if org.skipio,
|
---|
| 263 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 264 | return;
|
---|
| 265 | end
|
---|
| 266 |
|
---|
[14278] | 267 | %check that md is a model
|
---|
| 268 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 269 |
|
---|
| 270 | %list of variable names:
|
---|
| 271 | variables='';
|
---|
| 272 | for i=2:nargin,
|
---|
| 273 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 274 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 275 | end
|
---|
| 276 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 277 | end%}}}
|
---|
[25758] | 278 | function savedatanoprefix(org,varargin) % {{{
|
---|
| 279 |
|
---|
| 280 | %check
|
---|
| 281 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 282 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 283 |
|
---|
| 284 | name=[org.repository '/' org.steps(org.currentstep).string ];
|
---|
| 285 | disp(['saving data in: ' name]);
|
---|
| 286 |
|
---|
| 287 | %Skip if requested
|
---|
| 288 | if org.skipio,
|
---|
| 289 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 290 | return;
|
---|
| 291 | end
|
---|
| 292 |
|
---|
| 293 | %check that md is a model
|
---|
| 294 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 295 |
|
---|
| 296 | %list of variable names:
|
---|
| 297 | variables='';
|
---|
| 298 | for i=2:nargin,
|
---|
| 299 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 300 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 301 | end
|
---|
| 302 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 303 | end%}}}
|
---|
[6989] | 304 | end
|
---|
| 305 | end
|
---|