0001 function y = convert_img_units(img,arg1,arg2)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 if ischar(img) && strcmp(img,'UNIT_TEST'); y = do_unit_test; return; end
0020
0021 if nargin < 2
0022 error('EIDORS:ConvertUnits:Input', ...
0023 'Not enough input arguments.');
0024 end
0025
0026
0027 if isnumeric(img)
0028
0029 if nargin < 3
0030 error('EIDORS:ConvertUnits:Input', ...
0031 'Two units are required to convert numeric values.');
0032 else
0033 x = img;
0034 cur_unit = arg1;
0035 new_unit = arg2;
0036 y = map_data(x, cur_unit, new_unit);
0037 return
0038 end
0039 end
0040
0041
0042 if any(isfield(img,{'elem_data','node_data'}))
0043 if nargin == 3
0044 cur_unit = arg1;
0045 new_unit = arg2;
0046 else
0047 try
0048 cur_unit = img.current_params;
0049 new_unit = arg1;
0050 catch
0051 error(['Don''t know what to convert. '...
0052 'Need either 3 arguments or img.current_params.']);
0053 end
0054 end
0055 else
0056 if nargin ==3
0057 cur_unit = arg1;
0058 img.data_mapper = cur_unit;
0059 new_unit = arg2;
0060 else
0061 new_unit = arg1;
0062 end
0063 img = data_mapper(img);
0064 cur_unit = img.current_params;
0065
0066
0067
0068 end
0069
0070
0071 [x do_nodes] = get_data(img);
0072
0073 y = map_data(x, cur_unit, new_unit);
0074
0075
0076 if do_nodes
0077 img.node_data = y;
0078 else
0079 img.elem_data = y;
0080 end
0081 img.current_params = new_unit;
0082 y = img;
0083
0084 end
0085
0086 function x = map_data(x, cur_unit, new_unit)
0087
0088 if isempty(cur_unit) || isempty(new_unit)
0089 error('Unit string must not be empty.');
0090 end
0091
0092 if strcmp(cur_unit, new_unit)
0093 return
0094 end
0095
0096 try
0097 f = str2func(sprintf('%s2%s',cur_unit,new_unit));
0098 x = feval(f,x);
0099 x = fix_and_test(x);
0100 catch err
0101 if isempty(err.identifier)
0102 if strcmp(err.message(end-(30:-1:0)), ...
0103 'no function and no method found')
0104 err.identifier='MATLAB:UndefinedFunction';
0105 end
0106 end
0107
0108 if strcmp(err.identifier,'MATLAB:UndefinedFunction')
0109 cur_pre = regexp(cur_unit,'^(.*?)_','match');
0110 if ~isempty(cur_pre) && ismember(cur_pre{1}, {'log_', 'log10_'});
0111 l = length(cur_pre{1});
0112 x = reverse(cur_pre{1}, x);
0113 x = map_data(x, cur_unit(l+1:end), new_unit);
0114 x = fix_and_test(x);
0115 return
0116 end
0117 new_pre = regexp(new_unit,'^(.*?)_','match');
0118 if ~isempty(new_pre) && ismember(new_pre{1}, {'log_', 'log10_', 'abs_'});
0119 l = length(new_pre{1});
0120 x = map_data(x, cur_unit, new_unit(l+1:end));
0121 x = apply(new_pre{1}, x);
0122 x = fix_and_test(x);
0123 return
0124 end
0125
0126
0127 error('EIDORS:ConversionNotSupported', errorstr(cur_unit, new_unit));
0128 else
0129 rethrow(err);
0130 end
0131 end
0132
0133 end
0134
0135 function x = fix_and_test(x)
0136 x(x == +inf) = +realmax;
0137 x(x == -inf) = -realmax;
0138 err_if_inf_or_nan(x, 'map_data-post');
0139 end
0140
0141 function err_if_inf_or_nan(x, str)
0142 if any(isnan(x) | isinf(x))
0143 error(sprintf('bad %s (%d NaN, %d Inf of %d)', ...
0144 str, ...
0145 length(find(isnan(x))), ...
0146 length(find(isinf(x))), ...
0147 length(x)));
0148 end
0149
0150 end
0151
0152 function x = conductivity2resistivity(x)
0153 x = 1./x;
0154 end
0155
0156 function x = resistivity2conductivity(x)
0157 x = 1./x;
0158 end
0159
0160 function erstr = errorstr(cur_unit, new_unit)
0161 erstr = sprintf( ['Unit conversion from %s to %s is not supported.\n'...
0162 'Please write a function %s2%s.'], cur_unit, new_unit, cur_unit, new_unit);
0163 end
0164
0165 function x = reverse(str, x);
0166 switch str
0167 case 'log10_'
0168 if any(x >= log10(realmax)-eps) warning('loss of precision -> inf'); end
0169 x = 10.^x;
0170 case 'log_'
0171 if any(x >= log(realmax)-eps) warning('loss of precision -> inf'); end
0172 x = exp(x);
0173 otherwise
0174 error('Cannot reverse %s', str);
0175 end
0176 end
0177
0178
0179 function x = apply(str, x)
0180 switch str
0181 case 'log10_'
0182 if any(x <= 0 + eps) warning('loss of precision -> -inf'); end
0183 x = log10(x);
0184 case 'log_'
0185 if any(x <= 0 + eps) warning('loss of precision -> -inf'); end
0186 x = log(x);
0187 case 'abs_'
0188 x = abs(x);
0189 otherwise
0190 error('Cannot apply %s', str);
0191 end
0192 end
0193
0194 function [x, do_nodes] = get_data(img)
0195 do_nodes = 0;
0196 try
0197 x = img.elem_data;
0198 catch
0199 try
0200 x = img.node_data;
0201 do_nodes = 1;
0202 catch
0203 error('No elem_data or node_data found');
0204 end
0205 end
0206 end
0207
0208
0209 function pass = do_unit_test
0210 pass = 1;
0211 d = 1;
0212 while (d ~= 1) && (d ~= 0)
0213 d = rand(1);
0214 end
0215 disp('TEST: convert_img_units()');
0216 elem_types = {'conductivity', 'log_conductivity', 'log10_conductivity', ...
0217 'resistivity', 'log_resistivity', 'log10_resistivity'};
0218 expected = [d log(d) log10(d) 1./d log(1./d) log10(1./d); ...
0219 exp(d) d log10(exp(d)) 1./exp(d) log(1./exp(d)) log10(1./exp(d)); ...
0220 10.^d log(10.^d ) d 1./10.^d log(1./10.^d ) log10(1./10.^d ); ...
0221 1./d log(1./d ) log10(1./d) d log(d) log10(d); ...
0222 1./exp(d) log(1./exp(d)) log10(1./exp(d)) exp(d) d log10(exp(d)); ...
0223 1./10.^d log(1./10.^d) log10(1./10.^d) 10.^d log(10.^d) d ];
0224 for i = 1:length(elem_types)
0225 for j = 1:length(elem_types)
0226 pass = test_map_data(d, elem_types{i}, elem_types{j}, expected(i,j), pass);
0227 end
0228 end
0229
0230 if pass
0231 disp('TEST: overall PASS');
0232 else
0233 disp('TEST: overall FAIL');
0234 end
0235 end
0236
0237
0238 function pass = test_map_data(data, in, out, expected, pass)
0239 fprintf('TEST: convert_img_units(%s -> %s)\n', in, out);
0240 if convert_img_units(data, in, out) ~= expected
0241 pass = 0;
0242 fprintf('TEST: FAIL convert_img_units(%s -> %s)\n', in, out);
0243 end
0244 end