| 1 | %ORGANIZER class definition
|
|---|
| 2 | %
|
|---|
| 3 | % Supported options:
|
|---|
| 4 | % repository: directory where all models will be saved
|
|---|
| 5 | % prefix: prefix for saved model names
|
|---|
| 6 | % steps: requested steps
|
|---|
| 7 | % color: color of step title (default is '41;37;2m')
|
|---|
| 8 | %
|
|---|
| 9 | % Usage:
|
|---|
| 10 | % org = organizer(varargin)
|
|---|
| 11 | %
|
|---|
| 12 | % Examples:
|
|---|
| 13 | % org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
|
|---|
| 14 |
|
|---|
| 15 | classdef organizer < handle
|
|---|
| 16 | properties (SetAccess=private)
|
|---|
| 17 | % {{{
|
|---|
| 18 | currentstep =0;
|
|---|
| 19 | end
|
|---|
| 20 | properties (SetAccess=public)
|
|---|
| 21 | repository ='';
|
|---|
| 22 | prefix ='';
|
|---|
| 23 | color ='';
|
|---|
| 24 | steps =[];
|
|---|
| 25 | requestedsteps=[0];
|
|---|
| 26 | %}}}
|
|---|
| 27 | end
|
|---|
| 28 | methods
|
|---|
| 29 | function org=organizer(varargin) % {{{
|
|---|
| 30 |
|
|---|
| 31 | %process options
|
|---|
| 32 | options=pairoptions(varargin{:});
|
|---|
| 33 |
|
|---|
| 34 | %Get prefix
|
|---|
| 35 | prefix=getfieldvalue(options,'prefix','model_');
|
|---|
| 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;
|
|---|
| 39 |
|
|---|
| 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;
|
|---|
| 45 |
|
|---|
| 46 | %Color
|
|---|
| 47 | org.color=getfieldvalue(options,'color','41;37;2m');
|
|---|
| 48 |
|
|---|
| 49 | %Get steps
|
|---|
| 50 | org.requestedsteps=getfieldvalue(options,'steps',0);
|
|---|
| 51 |
|
|---|
| 52 | end
|
|---|
| 53 | %}}}
|
|---|
| 54 | function disp(org) % {{{
|
|---|
| 55 | disp(sprintf(' Repository: ''%s''',org.repository));
|
|---|
| 56 | disp(sprintf(' Prefix: ''%s''\n',org.prefix));
|
|---|
| 57 | disp(sprintf(' Color: ''%s''\n',org.color));
|
|---|
| 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),% {{{
|
|---|
| 68 |
|
|---|
| 69 | %Get model path
|
|---|
| 70 | if ~ischar(string), error('argument provided is not a string'); end
|
|---|
| 71 | path=[org.repository '/' org.prefix string];
|
|---|
| 72 |
|
|---|
| 73 | %figure out if the model is there
|
|---|
| 74 | if exist(path,'file'),
|
|---|
| 75 | path=path;
|
|---|
| 76 | elseif exist([path '.mat'],'file'),
|
|---|
| 77 | path=[path '.mat'];
|
|---|
| 78 | else
|
|---|
| 79 | error(['Could not find ' path ]);
|
|---|
| 80 | end
|
|---|
| 81 |
|
|---|
| 82 | struc=load(path,'-mat');
|
|---|
| 83 | name=char(fieldnames(struc));
|
|---|
| 84 | md=struc.(name);
|
|---|
| 85 | if nargout,
|
|---|
| 86 | varargout{1}=md;
|
|---|
| 87 | end
|
|---|
| 88 | end%}}}
|
|---|
| 89 | function md=loadmodel(org,string),% {{{
|
|---|
| 90 |
|
|---|
| 91 | %Get model path
|
|---|
| 92 | if ~ischar(string), error('argument provided is not a string'); end
|
|---|
| 93 | path=[org.repository '/' org.prefix string];
|
|---|
| 94 |
|
|---|
| 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
|
|---|
| 100 |
|
|---|
| 101 | %If we are here, the data has not been found.
|
|---|
| 102 | error(['Could not find ' path ]);
|
|---|
| 103 | end%}}}
|
|---|
| 104 | function loaddata(org,string),% {{{
|
|---|
| 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 |
|
|---|
| 116 | %If we are here, the data has not been found.
|
|---|
| 117 | error(['Could not find ' path ]);
|
|---|
| 118 | end%}}}
|
|---|
| 119 | function bool=perform(org,varargin) % {{{
|
|---|
| 120 |
|
|---|
| 121 | bool=false;
|
|---|
| 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
|
|---|
| 129 |
|
|---|
| 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
|
|---|
| 136 |
|
|---|
| 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;
|
|---|
| 141 |
|
|---|
| 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
|
|---|
| 149 |
|
|---|
| 150 | %Ok, now if currentstep is a member of steps, return true
|
|---|
| 151 | if ismember(org.currentstep,org.requestedsteps),
|
|---|
| 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
|
|---|
| 156 | fprintf(['\n\033[' org.color ' step #' num2str(org.steps(org.currentstep).id) ': ' org.steps(org.currentstep).string ' \033[0m\n\n']);
|
|---|
| 157 | end
|
|---|
| 158 | bool=true;
|
|---|
| 159 | end
|
|---|
| 160 | end%}}}
|
|---|
| 161 | function savemodel(org,md) % {{{
|
|---|
| 162 |
|
|---|
| 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
|
|---|
| 166 |
|
|---|
| 167 | name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
|
|---|
| 168 | disp(['saving model as: ' name]);
|
|---|
| 169 |
|
|---|
| 170 | %check that md is a model
|
|---|
| 171 | if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
|
|---|
| 172 | if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
|
|---|
| 173 |
|
|---|
| 174 | %save model
|
|---|
| 175 | save(name,'md','-v7.3');
|
|---|
| 176 | end%}}}
|
|---|
| 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%}}}
|
|---|
| 197 | end
|
|---|
| 198 | end
|
|---|