[6989] | 1 | %ORGANIZER class definition
|
---|
| 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
|
---|
| 7 | % trunkprefix:prefix of previous run with a different prefix. Used to branch.
|
---|
| 8 | %
|
---|
[6989] | 9 | % Usage:
|
---|
| 10 | % org = organizer(varargin)
|
---|
| 11 | %
|
---|
| 12 | % Examples:
|
---|
[8171] | 13 | % org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
|
---|
[6989] | 14 |
|
---|
[14228] | 15 | classdef organizer < handle
|
---|
[6989] | 16 | properties (SetAccess=private)
|
---|
[13946] | 17 | % {{{
|
---|
| 18 | currentstep =0;
|
---|
| 19 | end
|
---|
[6989] | 20 | properties (SetAccess=public)
|
---|
[13946] | 21 | repository ='./';
|
---|
| 22 | prefix ='model.';
|
---|
| 23 | trunkprefix ='';
|
---|
| 24 | steps =[];
|
---|
| 25 | requestedsteps=[0];
|
---|
[15701] | 26 | upload_server='';
|
---|
| 27 | upload_path='';
|
---|
| 28 | upload_login='';
|
---|
| 29 | upload_port=0;
|
---|
| 30 | download=0;
|
---|
[13946] | 31 | %}}}
|
---|
| 32 | end
|
---|
| 33 | methods
|
---|
| 34 | function org=organizer(varargin) % {{{
|
---|
[8171] | 35 |
|
---|
[13946] | 36 | %process options
|
---|
| 37 | options=pairoptions(varargin{:});
|
---|
[8171] | 38 |
|
---|
[13946] | 39 | %Get prefix
|
---|
| 40 | prefix=getfieldvalue(options,'prefix','model.');
|
---|
| 41 | if ~ischar(prefix), error('prefix is not a string'); end
|
---|
| 42 | if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
|
---|
| 43 | org.prefix=prefix;
|
---|
[8171] | 44 |
|
---|
[13946] | 45 | %Get repository
|
---|
| 46 | repository=getfieldvalue(options,'repository','./');
|
---|
| 47 | if ~ischar(repository), error('repository is not a string'); end
|
---|
| 48 | if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
|
---|
| 49 | org.repository=repository;
|
---|
[8171] | 50 |
|
---|
[13946] | 51 | %Get steps
|
---|
| 52 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
[8171] | 53 |
|
---|
[13946] | 54 | %Get trunk prefix (only if provided by user)
|
---|
| 55 | if exist(options,'trunkprefix'),
|
---|
| 56 | trunkprefix=getfieldvalue(options,'trunkprefix','');
|
---|
| 57 | if ~ischar(trunkprefix), error('trunkprefix is not a string'); end
|
---|
| 58 | if ~strcmp(regexprep(trunkprefix,'\s+',''),trunkprefix), error('trunkprefix should not have any white space'); end
|
---|
| 59 | org.trunkprefix=trunkprefix;
|
---|
| 60 | end
|
---|
[15701] | 61 |
|
---|
| 62 | %Get upload information, if provided
|
---|
| 63 | org.upload_server=getfieldvalue(options,'upload_server','');
|
---|
| 64 | org.upload_path=getfieldvalue(options,'upload_path','');
|
---|
| 65 | org.upload_login=getfieldvalue(options,'upload_login','');
|
---|
| 66 | org.upload_port=getfieldvalue(options,'upload_port',0);
|
---|
| 67 | org.download=getfieldvalue(options,'download',0);
|
---|
| 68 |
|
---|
[13946] | 69 | end
|
---|
| 70 | %}}}
|
---|
| 71 | function disp(org) % {{{
|
---|
| 72 | disp(sprintf(' Repository: ''%s''',org.repository));
|
---|
| 73 | disp(sprintf(' Prefix: ''%s''',org.prefix));
|
---|
| 74 | if isempty(org.steps)
|
---|
| 75 | disp(' no step');
|
---|
| 76 | else
|
---|
| 77 | for i=1:length(org.steps),
|
---|
| 78 | disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
|
---|
| 79 | end
|
---|
| 80 | end
|
---|
[15701] | 81 | if isempty(org.upload_server),
|
---|
| 82 | disp(' no upload info');
|
---|
| 83 | else
|
---|
| 84 | disp(sprintf(' upload_server: ''%s''',org.upload_server));
|
---|
| 85 | disp(sprintf(' upload_login: ''%s''',org.upload_login));
|
---|
| 86 | disp(sprintf(' upload_path: ''%s''',org.upload_path));
|
---|
| 87 | disp(sprintf(' upload_port: ''%i''',org.upload_port));
|
---|
| 88 | end
|
---|
[13946] | 89 | end
|
---|
| 90 | %}}}
|
---|
| 91 | function md=load(org,string),% {{{
|
---|
[12648] | 92 |
|
---|
[13946] | 93 | %Get model path
|
---|
| 94 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 95 | path=[org.repository '/' org.prefix string];
|
---|
[12648] | 96 |
|
---|
[13946] | 97 | %figure out if the model is there
|
---|
| 98 | if exist(path,'file'),
|
---|
| 99 | struc=load(path,'-mat');
|
---|
| 100 | name=char(fieldnames(struc));
|
---|
| 101 | md=struc.(name);
|
---|
| 102 | if nargout,
|
---|
| 103 | varargout{1}=md;
|
---|
| 104 | end
|
---|
| 105 | else
|
---|
| 106 | error(['Could not find ' path ]);
|
---|
| 107 | end
|
---|
| 108 | end%}}}
|
---|
| 109 | function md=loadmodel(org,string),% {{{
|
---|
[6989] | 110 |
|
---|
[13946] | 111 | %Get model path
|
---|
| 112 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 113 | path=[org.repository '/' org.prefix string];
|
---|
[6989] | 114 |
|
---|
[13946] | 115 | %figure out if the model is there, otherwise, we have to use the default path supplied by user.
|
---|
| 116 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
| 117 | md=loadmodel(path);
|
---|
| 118 | return;
|
---|
| 119 | end
|
---|
[6989] | 120 |
|
---|
[13946] | 121 | %If we are here, the model has not been found. Try trunk prefix if provided
|
---|
| 122 | if ~isempty(org.trunkprefix),
|
---|
| 123 | path2=[org.repository '/' org.trunkprefix string];
|
---|
| 124 | if ~exist(path2,'file'),
|
---|
| 125 | error(['Could find neither ' path ', nor ' path2]);
|
---|
| 126 | else
|
---|
| 127 | disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
|
---|
| 128 | md=loadmodel(path2);
|
---|
| 129 | return;
|
---|
| 130 | end
|
---|
| 131 | else
|
---|
| 132 | error(['Could not find ' path ]);
|
---|
| 133 | end
|
---|
| 134 | end%}}}
|
---|
[15786] | 135 | function loaddata(org,string),% {{{
|
---|
[14278] | 136 |
|
---|
| 137 | %Get model path
|
---|
| 138 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 139 | path=[org.repository '/' org.prefix string];
|
---|
| 140 |
|
---|
| 141 | %figure out if the data is there, otherwise, we have to use the default path supplied by user.
|
---|
| 142 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
| 143 | evalin('caller',['load -mat ' path]);
|
---|
| 144 | return;
|
---|
| 145 | end
|
---|
| 146 |
|
---|
| 147 | %If we are here, the data has not been found. Try trunk prefix if provided
|
---|
| 148 | if ~isempty(org.trunkprefix),
|
---|
| 149 | path2=[org.repository '/' org.trunkprefix string];
|
---|
| 150 | if ~exist(path2,'file'),
|
---|
| 151 | error(['Could find neither ' path ', nor ' path2]);
|
---|
| 152 | else
|
---|
| 153 | disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
|
---|
| 154 | evalin('caller',['load -mat ' path2]);
|
---|
| 155 | return;
|
---|
| 156 | end
|
---|
| 157 | else
|
---|
| 158 | error(['Could not find ' path ]);
|
---|
| 159 | end
|
---|
| 160 | end%}}}
|
---|
[13946] | 161 | function bool=perform(org,string) % {{{
|
---|
[13646] | 162 |
|
---|
[13946] | 163 | bool=false;
|
---|
[6989] | 164 |
|
---|
[13946] | 165 | %Some checks
|
---|
| 166 | if ~ischar(string), error('Step provided should be a string'); end
|
---|
| 167 | if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
|
---|
| 168 | if (org.currentstep>0 & ismember({string},{org.steps.string}))
|
---|
| 169 | error(['Step ' string ' already present. Change name']);
|
---|
| 170 | end
|
---|
[6989] | 171 |
|
---|
[13946] | 172 | %Add step
|
---|
| 173 | org.steps(end+1).id=length(org.steps)+1;
|
---|
| 174 | org.steps(end).string=string;
|
---|
| 175 | org.currentstep=org.currentstep+1;
|
---|
[6989] | 176 |
|
---|
[13946] | 177 | %if requestedsteps = 0, print all steps in org
|
---|
| 178 | if any(org.requestedsteps==0),
|
---|
| 179 | if org.currentstep==1,
|
---|
| 180 | disp(sprintf(' prefix: %s',org.prefix));
|
---|
| 181 | end
|
---|
| 182 | disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 183 | end
|
---|
[6989] | 184 |
|
---|
[13946] | 185 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 186 | if ismember(org.currentstep,org.requestedsteps),
|
---|
| 187 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 188 | bool=true;
|
---|
| 189 | end
|
---|
[6989] | 190 |
|
---|
[15701] | 191 | %But if download is requested, we are downloading and skipping the step:
|
---|
| 192 | if ismember(org.currentstep,org.requestedsteps) & org.download,
|
---|
| 193 | %load the model if it exists, and download
|
---|
| 194 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 195 | if exist(name,'file'),
|
---|
| 196 | md=loadmodel(name);
|
---|
| 197 | if isa(md,'model'),
|
---|
| 198 | if ~isempty(md.settings.upload_filename),
|
---|
| 199 | disp(sprintf(' downloading model'));
|
---|
| 200 | md=download(md);
|
---|
| 201 | save(name,'md','-v7.3');
|
---|
| 202 | end
|
---|
| 203 | end
|
---|
| 204 | end
|
---|
| 205 |
|
---|
| 206 | %reset bool to false, so we stick with only downloading
|
---|
| 207 | bool=false;
|
---|
| 208 | end
|
---|
| 209 |
|
---|
[13946] | 210 | end%}}}
|
---|
| 211 | function savemodel(org,md) % {{{
|
---|
[6989] | 212 |
|
---|
[13946] | 213 | %check
|
---|
| 214 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 215 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
[8151] | 216 |
|
---|
[13946] | 217 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 218 | disp(['saving model as: ' name]);
|
---|
[13646] | 219 |
|
---|
[13946] | 220 | %check that md is a model
|
---|
| 221 | if ~isa(md,'model'), warning('second argument is not a model'); end
|
---|
| 222 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 223 |
|
---|
[13946] | 224 | %save model
|
---|
| 225 | save(name,'md','-v7.3');
|
---|
| 226 | end%}}}
|
---|
[14278] | 227 | function savedata(org,varargin) % {{{
|
---|
| 228 |
|
---|
| 229 | %check
|
---|
| 230 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 231 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 232 |
|
---|
| 233 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 234 | disp(['saving data in: ' name]);
|
---|
| 235 |
|
---|
| 236 | %check that md is a model
|
---|
| 237 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 238 |
|
---|
| 239 | %list of variable names:
|
---|
| 240 | variables='';
|
---|
| 241 | for i=2:nargin,
|
---|
| 242 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 243 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 244 | end
|
---|
| 245 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 246 | end%}}}
|
---|
[6989] | 247 | end
|
---|
| 248 | end
|
---|