[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
|
---|
[20680] | 7 | % color: color of step title (default is '41;37;2m')
|
---|
[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
|
---|
[6989] | 16 | properties (SetAccess=private)
|
---|
[13946] | 17 | % {{{
|
---|
| 18 | currentstep =0;
|
---|
| 19 | end
|
---|
[6989] | 20 | properties (SetAccess=public)
|
---|
[13946] | 21 | repository ='./';
|
---|
[20680] | 22 | prefix ='model_';
|
---|
[13946] | 23 | steps =[];
|
---|
| 24 | requestedsteps=[0];
|
---|
| 25 | %}}}
|
---|
| 26 | end
|
---|
| 27 | methods
|
---|
[20211] | 28 | function org=organizer(varargin) % {{{
|
---|
[8171] | 29 |
|
---|
[13946] | 30 | %process options
|
---|
| 31 | options=pairoptions(varargin{:});
|
---|
[8171] | 32 |
|
---|
[13946] | 33 | %Get prefix
|
---|
[20680] | 34 | prefix=getfieldvalue(options,'prefix','model_');
|
---|
[13946] | 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;
|
---|
[8171] | 38 |
|
---|
[13946] | 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;
|
---|
[8171] | 44 |
|
---|
[20680] | 45 | %Color
|
---|
| 46 | org.color=getfieldvalue(options,'color','41;37;2m');
|
---|
| 47 |
|
---|
[13946] | 48 | %Get steps
|
---|
| 49 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
[8171] | 50 |
|
---|
[13946] | 51 | end
|
---|
| 52 | %}}}
|
---|
| 53 | function disp(org) % {{{
|
---|
| 54 | disp(sprintf(' Repository: ''%s''',org.repository));
|
---|
[20210] | 55 | disp(sprintf(' Prefix: ''%s''\n',org.prefix));
|
---|
[20680] | 56 | disp(sprintf(' Color: ''%s''\n',org.color));
|
---|
[13946] | 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),% {{{
|
---|
[12648] | 67 |
|
---|
[13946] | 68 | %Get model path
|
---|
| 69 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 70 | path=[org.repository '/' org.prefix string];
|
---|
[12648] | 71 |
|
---|
[13946] | 72 | %figure out if the model is there
|
---|
| 73 | if exist(path,'file'),
|
---|
[17259] | 74 | path=path;
|
---|
| 75 | elseif exist([path '.mat'],'file'),
|
---|
| 76 | path=[path '.mat'];
|
---|
[13946] | 77 | else
|
---|
| 78 | error(['Could not find ' path ]);
|
---|
| 79 | end
|
---|
[17259] | 80 |
|
---|
| 81 | struc=load(path,'-mat');
|
---|
| 82 | name=char(fieldnames(struc));
|
---|
| 83 | md=struc.(name);
|
---|
| 84 | if nargout,
|
---|
| 85 | varargout{1}=md;
|
---|
| 86 | end
|
---|
[13946] | 87 | end%}}}
|
---|
| 88 | function md=loadmodel(org,string),% {{{
|
---|
[6989] | 89 |
|
---|
[13946] | 90 | %Get model path
|
---|
| 91 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 92 | path=[org.repository '/' org.prefix string];
|
---|
[6989] | 93 |
|
---|
[13946] | 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
|
---|
[6989] | 99 |
|
---|
[20210] | 100 | %If we are here, the data has not been found.
|
---|
| 101 | error(['Could not find ' path ]);
|
---|
[13946] | 102 | end%}}}
|
---|
[15786] | 103 | function loaddata(org,string),% {{{
|
---|
[14278] | 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 |
|
---|
[20210] | 115 | %If we are here, the data has not been found.
|
---|
| 116 | error(['Could not find ' path ]);
|
---|
[14278] | 117 | end%}}}
|
---|
[20210] | 118 | function bool=perform(org,varargin) % {{{
|
---|
[13646] | 119 |
|
---|
[13946] | 120 | bool=false;
|
---|
[20210] | 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
|
---|
[6989] | 128 |
|
---|
[13946] | 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
|
---|
[6989] | 135 |
|
---|
[13946] | 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;
|
---|
[6989] | 140 |
|
---|
[13946] | 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
|
---|
[6989] | 148 |
|
---|
[13946] | 149 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 150 | if ismember(org.currentstep,org.requestedsteps),
|
---|
[20609] | 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
|
---|
[20680] | 155 | fprintf(['\n\033[' org.color ' step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
|
---|
[20609] | 156 | end
|
---|
[13946] | 157 | bool=true;
|
---|
| 158 | end
|
---|
| 159 | end%}}}
|
---|
| 160 | function savemodel(org,md) % {{{
|
---|
[6989] | 161 |
|
---|
[13946] | 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
|
---|
[8151] | 165 |
|
---|
[13946] | 166 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 167 | disp(['saving model as: ' name]);
|
---|
[13646] | 168 |
|
---|
[13946] | 169 | %check that md is a model
|
---|
[20130] | 170 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
---|
[13946] | 171 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 172 |
|
---|
[13946] | 173 | %save model
|
---|
| 174 | save(name,'md','-v7.3');
|
---|
| 175 | end%}}}
|
---|
[14278] | 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%}}}
|
---|
[6989] | 196 | end
|
---|
| 197 | end
|
---|