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