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

Last change on this file since 12648 was 12648, checked in by Mathieu Morlighem, 13 years ago

can now save and load matlab objects that are not structures

File size: 5.4 KB
Line 
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% trunkprefix:prefix of previous run with a different prefix. Used to branch.
8%
9% Usage:
10% org = organizer(varargin)
11%
12%
13% Examples:
14% org = organizer('repository','Models/','prefix','AGU2015','steps',0); %build an empty organizer object with a given repository
15
16classdef organizer
17 properties (SetAccess=private)
18 % {{{
19 currentstep =0;
20 end
21 properties (SetAccess=public)
22 repository ='./';
23 prefix ='model.';
24 trunkprefix ='';
25 steps =[];
26 requestedsteps=[0];
27 %}}}
28 end
29 methods
30 function org=organizer(varargin) % {{{
31
32 %process options
33 options=pairoptions(varargin{:});
34
35 %Get prefix
36 prefix=getfieldvalue(options,'prefix','model.');
37 if ~ischar(prefix), error('prefix is not a string'); end
38 if ~strcmp(regexprep(prefix,'\s+',''),prefix), error('prefix should not have any white space'); end
39 org.prefix=prefix;
40
41 %Get repository
42 repository=getfieldvalue(options,'repository','./');
43 if ~ischar(repository), error('repository is not a string'); end
44 if exist(repository,'dir')~=7, error(['Directory ' repository ' not found']), end
45 org.repository=repository;
46
47 %Get repository
48 org.requestedsteps=getfieldvalue(options,'steps',0);
49
50 %Get trunk prefix (only if provided by user)
51 if exist(options,'trunkprefix'),
52 trunkprefix=getfieldvalue(options,'trunkprefix','');
53 if ~ischar(trunkprefix), error('trunkprefix is not a string'); end
54 if ~strcmp(regexprep(trunkprefix,'\s+',''),trunkprefix), error('trunkprefix should not have any white space'); end
55 org.trunkprefix=trunkprefix;
56 end
57 end
58 %}}}
59 function disp(org) % {{{
60 disp(sprintf(' Repository: ''%s''',org.repository));
61 disp(sprintf(' Prefix: ''%s''',org.prefix));
62 if isempty(org.steps)
63 disp(' no step');
64 else
65 for i=1:length(org.steps),
66 disp(sprintf(' step #%2i: ''%s''',org.steps(i).id,org.steps(i).string));
67 end
68 end
69 end
70 %}}}
71 function md=load(org,string),% {{{
72
73 %Get model path
74 if ~ischar(string), error('argument provided is not a string'); end
75 path=[org.repository '/' org.prefix string];
76
77 %figure out if the model is there
78 if exist(path,'file'),
79 struc=load(path,'-mat');
80 name=char(fieldnames(struc));
81 md=struc.(name);
82 if nargout,
83 varargout{1}=md;
84 end
85 else
86 error(['Could not find ' path ]);
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 model has not been found. Try trunk prefix if provided
102 if ~isempty(org.trunkprefix),
103 path2=[org.repository '/' org.trunkprefix string];
104 if ~exist(path2,'file'),
105 error(['Could neither find ' path ', nor ' path2]);
106 else
107 disp(['--> Branching ' org.prefix ' from trunk ' org.trunkprefix]);
108 md=loadmodel(path2);
109 return;
110 end
111 else
112 error(['Could not find ' path ]);
113 end
114 end%}}}
115 function bool=perform(org,string) % {{{
116
117 bool=false;
118
119 %Some checks
120 if ~ischar(string), error('Step provided should be a string'); end
121 if ~strcmp(regexprep(string,'\s+',''),string), error('Step provided should not have any white space'); end
122 if (org.currentstep>0 & ismember({string},{org.steps.string}))
123 error(['Step ' string ' already present. Change name']);
124 end
125
126 %Add step
127 org.steps(end+1).id=length(org.steps)+1;
128 org.steps(end).string=string;
129 org.currentstep=org.currentstep+1;
130
131 %if requestedsteps = 0, print all steps in org
132 if any(org.requestedsteps==0),
133 if org.currentstep==1,
134 disp(sprintf(' prefix: %s',org.prefix));
135 end
136 disp(sprintf(' step #%2i : %s',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
137 end
138
139 %Ok, now if currentstep is a member of steps, return true
140 if ismember(org.currentstep,org.requestedsteps),
141 disp(sprintf('\n step #%i : %s\n',org.steps(org.currentstep).id,org.steps(org.currentstep).string));
142 bool=true;
143 end
144
145 %assign org back to calling workspace
146 assignin('caller',inputname(1),org);
147
148 end%}}}
149 function savemodel(org,md) % {{{
150
151 %check
152 if (org.currentstep==0), error('Cannot save model because organizer (org) is empty! Make sure you did not skip any perform call'); end
153 if (org.currentstep>length(org.steps)), error('Cannot save model because organizer (org) is not up to date!'); end
154
155 name=[org.repository '/' org.prefix org.steps(org.currentstep).string ];
156 disp(['saving model as: ' name]);
157
158 %check that md is a model
159 if ~isa(md,'model'), warning('third argument is not a model'); end
160 if (org.currentstep>length(org.steps)), error(['organizer error message: element with id ' num2str(org.currentstep) ' not found']); end
161
162 %save model
163 save(name,'md','-v7.3');
164 end%}}}
165 end
166end
Note: See TracBrowser for help on using the repository browser.