[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%}}}
|
---|
[20210] | 158 | function bool=perform(org,varargin) % {{{
|
---|
[13646] | 159 |
|
---|
[13946] | 160 | bool=false;
|
---|
[20210] | 161 |
|
---|
| 162 | %group,string are the variable arguments length:
|
---|
| 163 | if nargin==2,
|
---|
| 164 | string=varargin{1};
|
---|
| 165 | elseif nargin==3,
|
---|
| 166 | string=sprintf('%s.%s',varargin{1},varargin{2});
|
---|
| 167 | end
|
---|
[6989] | 168 |
|
---|
[13946] | 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
|
---|
[6989] | 175 |
|
---|
[13946] | 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;
|
---|
[6989] | 180 |
|
---|
[13946] | 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
|
---|
[6989] | 188 |
|
---|
[13946] | 189 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 190 | if ismember(org.currentstep,org.requestedsteps),
|
---|
[20609] | 191 | if usejava('desktop'),
|
---|
| 192 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 193 | else
|
---|
| 194 | %Print on a red background
|
---|
[20699] | 195 | fprintf(['\n\033[' org.color 'm step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
|
---|
[20609] | 196 | end
|
---|
[13946] | 197 | bool=true;
|
---|
| 198 | end
|
---|
| 199 | end%}}}
|
---|
| 200 | function savemodel(org,md) % {{{
|
---|
[6989] | 201 |
|
---|
[13946] | 202 | %check
|
---|
| 203 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 204 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
[8151] | 205 |
|
---|
[13946] | 206 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 207 | disp(['saving model as: ' name]);
|
---|
[13646] | 208 |
|
---|
[23776] | 209 | %Skip if requested
|
---|
| 210 | if org.skipio,
|
---|
| 211 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 212 | return;
|
---|
| 213 | end
|
---|
| 214 |
|
---|
[13946] | 215 | %check that md is a model
|
---|
[20130] | 216 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
---|
[13946] | 217 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 218 |
|
---|
[13946] | 219 | %save model
|
---|
| 220 | save(name,'md','-v7.3');
|
---|
| 221 | end%}}}
|
---|
[14278] | 222 | function savedata(org,varargin) % {{{
|
---|
| 223 |
|
---|
| 224 | %check
|
---|
| 225 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 226 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 227 |
|
---|
| 228 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 229 | disp(['saving data in: ' name]);
|
---|
| 230 |
|
---|
[23776] | 231 | %Skip if requested
|
---|
| 232 | if org.skipio,
|
---|
| 233 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 234 | return;
|
---|
| 235 | end
|
---|
| 236 |
|
---|
[14278] | 237 | %check that md is a model
|
---|
| 238 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 239 |
|
---|
| 240 | %list of variable names:
|
---|
| 241 | variables='';
|
---|
| 242 | for i=2:nargin,
|
---|
| 243 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 244 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 245 | end
|
---|
| 246 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 247 | end%}}}
|
---|
[6989] | 248 | end
|
---|
| 249 | end
|
---|