[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 | %}}}
|
---|
| 57 | function disp(org) % {{{
|
---|
| 58 | disp(sprintf(' Repository: ''%s''',org.repository));
|
---|
[20210] | 59 | disp(sprintf(' Prefix: ''%s''\n',org.prefix));
|
---|
[20680] | 60 | disp(sprintf(' Color: ''%s''\n',org.color));
|
---|
[23776] | 61 | disp(sprintf(' skipio: %i\n',org.skipio));
|
---|
[13946] | 62 | if isempty(org.steps)
|
---|
| 63 | disp(' no step');
|
---|
| 64 | else
|
---|
| 65 | for i=1:length(org.steps),
|
---|
| 66 | disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
|
---|
| 67 | end
|
---|
| 68 | end
|
---|
| 69 | end
|
---|
| 70 | %}}}
|
---|
| 71 | function md=load(org,string),% {{{
|
---|
[12648] | 72 |
|
---|
[13946] | 73 | %Get model path
|
---|
| 74 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 75 | path=[org.repository '/' org.prefix string];
|
---|
[12648] | 76 |
|
---|
[23776] | 77 | %Skip if requested
|
---|
| 78 | if org.skipio,
|
---|
| 79 | disp(['WARNING: Skipping loading ' path]);
|
---|
| 80 | md = evalin('base', 'md');
|
---|
| 81 | return;
|
---|
| 82 | end
|
---|
| 83 |
|
---|
[13946] | 84 | %figure out if the model is there
|
---|
| 85 | if exist(path,'file'),
|
---|
[17259] | 86 | path=path;
|
---|
| 87 | elseif exist([path '.mat'],'file'),
|
---|
| 88 | path=[path '.mat'];
|
---|
[13946] | 89 | else
|
---|
| 90 | error(['Could not find ' path ]);
|
---|
| 91 | end
|
---|
[17259] | 92 |
|
---|
| 93 | struc=load(path,'-mat');
|
---|
| 94 | name=char(fieldnames(struc));
|
---|
| 95 | md=struc.(name);
|
---|
| 96 | if nargout,
|
---|
| 97 | varargout{1}=md;
|
---|
| 98 | end
|
---|
[13946] | 99 | end%}}}
|
---|
| 100 | function md=loadmodel(org,string),% {{{
|
---|
[6989] | 101 |
|
---|
[13946] | 102 | %Get model path
|
---|
| 103 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 104 | path=[org.repository '/' org.prefix string];
|
---|
[6989] | 105 |
|
---|
[23776] | 106 | %Skip if requested
|
---|
| 107 | if org.skipio,
|
---|
| 108 | disp(['WARNING: Skipping loading ' path]);
|
---|
| 109 | md = evalin('base', 'md');
|
---|
| 110 | return;
|
---|
| 111 | end
|
---|
| 112 |
|
---|
[13946] | 113 | %figure out if the model is there, otherwise, we have to use the default path supplied by user.
|
---|
| 114 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
| 115 | md=loadmodel(path);
|
---|
| 116 | return;
|
---|
| 117 | end
|
---|
[6989] | 118 |
|
---|
[20210] | 119 | %If we are here, the data has not been found.
|
---|
| 120 | error(['Could not find ' path ]);
|
---|
[13946] | 121 | end%}}}
|
---|
[15786] | 122 | function loaddata(org,string),% {{{
|
---|
[14278] | 123 |
|
---|
| 124 | %Get model path
|
---|
| 125 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 126 | path=[org.repository '/' org.prefix string];
|
---|
| 127 |
|
---|
| 128 | %figure out if the data is there, otherwise, we have to use the default path supplied by user.
|
---|
[21291] | 129 | if exist(path,'file'),
|
---|
| 130 | path=path;
|
---|
| 131 | elseif exist([path '.mat'],'file'),
|
---|
| 132 | path=[path '.mat'];
|
---|
| 133 | else
|
---|
| 134 | error(['Could not find ' path ]);
|
---|
| 135 | end
|
---|
| 136 | if exist(path,'file')
|
---|
[14278] | 137 | evalin('caller',['load -mat ' path]);
|
---|
| 138 | return;
|
---|
| 139 | end
|
---|
| 140 |
|
---|
[20210] | 141 | %If we are here, the data has not been found.
|
---|
| 142 | error(['Could not find ' path ]);
|
---|
[14278] | 143 | end%}}}
|
---|
[20210] | 144 | function bool=perform(org,varargin) % {{{
|
---|
[13646] | 145 |
|
---|
[13946] | 146 | bool=false;
|
---|
[20210] | 147 |
|
---|
| 148 | %group,string are the variable arguments length:
|
---|
| 149 | if nargin==2,
|
---|
| 150 | string=varargin{1};
|
---|
| 151 | elseif nargin==3,
|
---|
| 152 | string=sprintf('%s.%s',varargin{1},varargin{2});
|
---|
| 153 | end
|
---|
[6989] | 154 |
|
---|
[13946] | 155 | %Some checks
|
---|
| 156 | if ~ischar(string), error('Step provided should be a string'); end
|
---|
| 157 | if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
|
---|
| 158 | if (org.currentstep>0 & ismember({string},{org.steps.string}))
|
---|
| 159 | error(['Step ' string ' already present. Change name']);
|
---|
| 160 | end
|
---|
[6989] | 161 |
|
---|
[13946] | 162 | %Add step
|
---|
| 163 | org.steps(end+1).id=length(org.steps)+1;
|
---|
| 164 | org.steps(end).string=string;
|
---|
| 165 | org.currentstep=org.currentstep+1;
|
---|
[6989] | 166 |
|
---|
[13946] | 167 | %if requestedsteps = 0, print all steps in org
|
---|
| 168 | if any(org.requestedsteps==0),
|
---|
| 169 | if org.currentstep==1,
|
---|
| 170 | disp(sprintf(' prefix: %s',org.prefix));
|
---|
| 171 | end
|
---|
| 172 | disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 173 | end
|
---|
[6989] | 174 |
|
---|
[13946] | 175 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 176 | if ismember(org.currentstep,org.requestedsteps),
|
---|
[20609] | 177 | if usejava('desktop'),
|
---|
| 178 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 179 | else
|
---|
| 180 | %Print on a red background
|
---|
[20699] | 181 | fprintf(['\n\033[' org.color 'm step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
|
---|
[20609] | 182 | end
|
---|
[13946] | 183 | bool=true;
|
---|
| 184 | end
|
---|
| 185 | end%}}}
|
---|
| 186 | function savemodel(org,md) % {{{
|
---|
[6989] | 187 |
|
---|
[13946] | 188 | %check
|
---|
| 189 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 190 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
[8151] | 191 |
|
---|
[13946] | 192 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 193 | disp(['saving model as: ' name]);
|
---|
[13646] | 194 |
|
---|
[23776] | 195 | %Skip if requested
|
---|
| 196 | if org.skipio,
|
---|
| 197 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 198 | return;
|
---|
| 199 | end
|
---|
| 200 |
|
---|
[13946] | 201 | %check that md is a model
|
---|
[20130] | 202 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
---|
[13946] | 203 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 204 |
|
---|
[13946] | 205 | %save model
|
---|
| 206 | save(name,'md','-v7.3');
|
---|
| 207 | end%}}}
|
---|
[14278] | 208 | function savedata(org,varargin) % {{{
|
---|
| 209 |
|
---|
| 210 | %check
|
---|
| 211 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 212 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 213 |
|
---|
| 214 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 215 | disp(['saving data in: ' name]);
|
---|
| 216 |
|
---|
[23776] | 217 | %Skip if requested
|
---|
| 218 | if org.skipio,
|
---|
| 219 | disp(['WARNING: Skipping saving ' name]);
|
---|
| 220 | return;
|
---|
| 221 | end
|
---|
| 222 |
|
---|
[14278] | 223 | %check that md is a model
|
---|
| 224 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 225 |
|
---|
| 226 | %list of variable names:
|
---|
| 227 | variables='';
|
---|
| 228 | for i=2:nargin,
|
---|
| 229 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 230 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 231 | end
|
---|
| 232 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 233 | end%}}}
|
---|
[6989] | 234 | end
|
---|
| 235 | end
|
---|