source: issm/trunk-jpl/src/m/classes/organizer.m@ 20210

Last change on this file since 20210 was 20210, checked in by Eric.Larour, 9 years ago

CHG: simplifying, adding some capabilities (provide a basin name for each step, so you can duplicate
steps if they deal with separate basins).

File size: 5.9 KB
Line 
1%XORGANIZER 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%
8% Usage:
9% org = xorganizer(varargin)
10%
11% Examples:
12% org = xorganizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
13
14classdef xorganizer < handle
15 properties (SetAccess=private)
16 % {{{
17 currentstep =0;
18 end
19 properties (SetAccess=public)
20 repository ='./';
21 prefix ='model.';
22 steps =[];
23 requestedsteps=[0];
24 %}}}
25 end
26 methods
27 function org=xorganizer(varargin) % {{{
28
29 %process options
30 options=pairoptions(varargin{:});
31
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;
37
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;
43
44 %Get steps
45 org.requestedsteps=getfieldvalue(options,'steps',0);
46
47 end
48 %}}}
49 function disp(org) % {{{
50 disp(sprintf(' Repository: ''%s''',org.repository));
51 disp(sprintf(' Prefix: ''%s''\n',org.prefix));
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),% {{{
62
63 %Get model path
64 if ~ischar(string), error('argument provided is not a string'); end
65 path=[org.repository '/' org.prefix string];
66
67 %figure out if the model is there
68 if exist(path,'file'),
69 path=path;
70 elseif exist([path '.mat'],'file'),
71 path=[path '.mat'];
72 else
73 error(['Could not find ' path ]);
74 end
75
76 struc=load(path,'-mat');
77 name=char(fieldnames(struc));
78 md=struc.(name);
79 if nargout,
80 varargout{1}=md;
81 end
82 end%}}}
83 function md=loadmodel(org,string),% {{{
84
85 %Get model path
86 if ~ischar(string), error('argument provided is not a string'); end
87 path=[org.repository '/' org.prefix string];
88
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
94
95 %If we are here, the data has not been found.
96 error(['Could not find ' path ]);
97 end%}}}
98 function loaddata(org,string),% {{{
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
110 %If we are here, the data has not been found.
111 error(['Could not find ' path ]);
112 end%}}}
113 function bool=perform(org,varargin) % {{{
114
115 bool=false;
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
123
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
130
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;
135
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
143
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) % {{{
151
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
155
156 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
157 disp(['saving model as: ' name]);
158
159 %check that md is a model
160 if ~isa(md,'model') & ~isa(md,'sealevelmodel'), warning('second argument is not a model'); end
161 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
162
163 %save model
164 save(name,'md','-v7.3');
165 end%}}}
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%}}}
186 end
187end
Note: See TracBrowser for help on using the repository browser.