[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)
|
---|
[20681] | 21 | repository ='';
|
---|
| 22 | prefix ='';
|
---|
| 23 | color ='';
|
---|
[13946] | 24 | steps =[];
|
---|
| 25 | requestedsteps=[0];
|
---|
| 26 | %}}}
|
---|
| 27 | end
|
---|
| 28 | methods
|
---|
[20211] | 29 | function org=organizer(varargin) % {{{
|
---|
[8171] | 30 |
|
---|
[13946] | 31 | %process options
|
---|
| 32 | options=pairoptions(varargin{:});
|
---|
[8171] | 33 |
|
---|
[13946] | 34 | %Get prefix
|
---|
[20680] | 35 | prefix=getfieldvalue(options,'prefix','model_');
|
---|
[13946] | 36 | if ~ischar(prefix), error('prefix is not a string'); end
|
---|
| 37 | if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
|
---|
| 38 | org.prefix=prefix;
|
---|
[8171] | 39 |
|
---|
[13946] | 40 | %Get repository
|
---|
| 41 | repository=getfieldvalue(options,'repository','./');
|
---|
| 42 | if ~ischar(repository), error('repository is not a string'); end
|
---|
| 43 | if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
|
---|
| 44 | org.repository=repository;
|
---|
[8171] | 45 |
|
---|
[20680] | 46 | %Color
|
---|
| 47 | org.color=getfieldvalue(options,'color','41;37;2m');
|
---|
| 48 |
|
---|
[13946] | 49 | %Get steps
|
---|
| 50 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
---|
[8171] | 51 |
|
---|
[13946] | 52 | end
|
---|
| 53 | %}}}
|
---|
| 54 | function disp(org) % {{{
|
---|
| 55 | disp(sprintf(' Repository: ''%s''',org.repository));
|
---|
[20210] | 56 | disp(sprintf(' Prefix: ''%s''\n',org.prefix));
|
---|
[20680] | 57 | disp(sprintf(' Color: ''%s''\n',org.color));
|
---|
[13946] | 58 | if isempty(org.steps)
|
---|
| 59 | disp(' no step');
|
---|
| 60 | else
|
---|
| 61 | for i=1:length(org.steps),
|
---|
| 62 | disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
|
---|
| 63 | end
|
---|
| 64 | end
|
---|
| 65 | end
|
---|
| 66 | %}}}
|
---|
| 67 | function md=load(org,string),% {{{
|
---|
[12648] | 68 |
|
---|
[13946] | 69 | %Get model path
|
---|
| 70 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 71 | path=[org.repository '/' org.prefix string];
|
---|
[12648] | 72 |
|
---|
[13946] | 73 | %figure out if the model is there
|
---|
| 74 | if exist(path,'file'),
|
---|
[17259] | 75 | path=path;
|
---|
| 76 | elseif exist([path '.mat'],'file'),
|
---|
| 77 | path=[path '.mat'];
|
---|
[13946] | 78 | else
|
---|
| 79 | error(['Could not find ' path ]);
|
---|
| 80 | end
|
---|
[17259] | 81 |
|
---|
| 82 | struc=load(path,'-mat');
|
---|
| 83 | name=char(fieldnames(struc));
|
---|
| 84 | md=struc.(name);
|
---|
| 85 | if nargout,
|
---|
| 86 | varargout{1}=md;
|
---|
| 87 | end
|
---|
[13946] | 88 | end%}}}
|
---|
| 89 | function md=loadmodel(org,string),% {{{
|
---|
[6989] | 90 |
|
---|
[13946] | 91 | %Get model path
|
---|
| 92 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 93 | path=[org.repository '/' org.prefix string];
|
---|
[6989] | 94 |
|
---|
[13946] | 95 | %figure out if the model is there, otherwise, we have to use the default path supplied by user.
|
---|
| 96 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
| 97 | md=loadmodel(path);
|
---|
| 98 | return;
|
---|
| 99 | end
|
---|
[6989] | 100 |
|
---|
[20210] | 101 | %If we are here, the data has not been found.
|
---|
| 102 | error(['Could not find ' path ]);
|
---|
[13946] | 103 | end%}}}
|
---|
[15786] | 104 | function loaddata(org,string),% {{{
|
---|
[14278] | 105 |
|
---|
| 106 | %Get model path
|
---|
| 107 | if ~ischar(string), error('argument provided is not a string'); end
|
---|
| 108 | path=[org.repository '/' org.prefix string];
|
---|
| 109 |
|
---|
| 110 | %figure out if the data is there, otherwise, we have to use the default path supplied by user.
|
---|
| 111 | if exist(path,'file') | exist([path '.mat'],'file'),
|
---|
| 112 | evalin('caller',['load -mat ' path]);
|
---|
| 113 | return;
|
---|
| 114 | end
|
---|
| 115 |
|
---|
[20210] | 116 | %If we are here, the data has not been found.
|
---|
| 117 | error(['Could not find ' path ]);
|
---|
[14278] | 118 | end%}}}
|
---|
[20210] | 119 | function bool=perform(org,varargin) % {{{
|
---|
[13646] | 120 |
|
---|
[13946] | 121 | bool=false;
|
---|
[20210] | 122 |
|
---|
| 123 | %group,string are the variable arguments length:
|
---|
| 124 | if nargin==2,
|
---|
| 125 | string=varargin{1};
|
---|
| 126 | elseif nargin==3,
|
---|
| 127 | string=sprintf('%s.%s',varargin{1},varargin{2});
|
---|
| 128 | end
|
---|
[6989] | 129 |
|
---|
[13946] | 130 | %Some checks
|
---|
| 131 | if ~ischar(string), error('Step provided should be a string'); end
|
---|
| 132 | if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
|
---|
| 133 | if (org.currentstep>0 & ismember({string},{org.steps.string}))
|
---|
| 134 | error(['Step ' string ' already present. Change name']);
|
---|
| 135 | end
|
---|
[6989] | 136 |
|
---|
[13946] | 137 | %Add step
|
---|
| 138 | org.steps(end+1).id=length(org.steps)+1;
|
---|
| 139 | org.steps(end).string=string;
|
---|
| 140 | org.currentstep=org.currentstep+1;
|
---|
[6989] | 141 |
|
---|
[13946] | 142 | %if requestedsteps = 0, print all steps in org
|
---|
| 143 | if any(org.requestedsteps==0),
|
---|
| 144 | if org.currentstep==1,
|
---|
| 145 | disp(sprintf(' prefix: %s',org.prefix));
|
---|
| 146 | end
|
---|
| 147 | disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 148 | end
|
---|
[6989] | 149 |
|
---|
[13946] | 150 | %Ok, now if currentstep is a member of steps, return true
|
---|
| 151 | if ismember(org.currentstep,org.requestedsteps),
|
---|
[20609] | 152 | if usejava('desktop'),
|
---|
| 153 | disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
|
---|
| 154 | else
|
---|
| 155 | %Print on a red background
|
---|
[20680] | 156 | fprintf(['\n\033[' org.color ' step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
|
---|
[20609] | 157 | end
|
---|
[13946] | 158 | bool=true;
|
---|
| 159 | end
|
---|
| 160 | end%}}}
|
---|
| 161 | function savemodel(org,md) % {{{
|
---|
[6989] | 162 |
|
---|
[13946] | 163 | %check
|
---|
| 164 | if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 165 | if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
|
---|
[8151] | 166 |
|
---|
[13946] | 167 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 168 | disp(['saving model as: ' name]);
|
---|
[13646] | 169 |
|
---|
[13946] | 170 | %check that md is a model
|
---|
[20130] | 171 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
---|
[13946] | 172 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
[6989] | 173 |
|
---|
[13946] | 174 | %save model
|
---|
| 175 | save(name,'md','-v7.3');
|
---|
| 176 | end%}}}
|
---|
[14278] | 177 | function savedata(org,varargin) % {{{
|
---|
| 178 |
|
---|
| 179 | %check
|
---|
| 180 | if (org.currentstep==0), error('Cannot save data because organizer (org) is empty! Make sure you did not skip any perform call'); end
|
---|
| 181 | if (org.currentstep>length(org.steps)), error('Cannot save data because organizer (org) is not up to date!'); end
|
---|
| 182 |
|
---|
| 183 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
---|
| 184 | disp(['saving data in: ' name]);
|
---|
| 185 |
|
---|
| 186 | %check that md is a model
|
---|
| 187 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
---|
| 188 |
|
---|
| 189 | %list of variable names:
|
---|
| 190 | variables='';
|
---|
| 191 | for i=2:nargin,
|
---|
| 192 | variables=[variables ',' '''' inputname(i) ''''];
|
---|
| 193 | eval([inputname(i) '= varargin{' num2str(i-1) '};']);
|
---|
| 194 | end
|
---|
| 195 | eval(['save(''' name '''' variables ',''-v7.3'');']);
|
---|
| 196 | end%}}}
|
---|
[6989] | 197 | end
|
---|
| 198 | end
|
---|