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