-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_unit_conversion.m
245 lines (220 loc) · 8.96 KB
/
l_unit_conversion.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
function wlog=l_unit_conversion(wlog,varargin)
% Function converts units of measurements of curves and parameters in
% a log structure
%
% Written by: E. R.: January 10, 2001
% Last updated: December 7, 2005: Handle additional field "units".
%
% wlog=l_unit_conversion(wlog,varargin)
% INPUT
% wlog log structure
% varargin cell arrays with at least two elements. The first element is a string representing
% an existing unit of measurement, the second is the desired unit of measurement.
% If additional elements are given they represent curve mnemonics which should be
% changed. If no curves are found that have these units of measurements and/or
% these mnmonics, an alert is printed (see keyword 'alert').
% Possible pairs of units of measurement are (in alphabetical order) and
% vice versa:
% {'fraction','%'}
% {'g/cm3','ppg'}, {'g/cm3','kg/m3'}
% {'m','ft'} (this also converts '1/m' to '1/ft')
% {'m/s','ft/s')
% {'us/ft','us/m'}
% {'s','ms'}
% It is not an error if the two units are the same (e.g {'m','m'})
%
% 'alert' Print an alert. Possible values are 0 (false) and 1 (true).
% This keyword has an effect only on those conversions following it. Hence,
% it should be the first argument after the log structure
% Default: {'alert',1}
% OUTPUT
% wlog log structure with new units of measurement
%
% EXAMPLES
% % Change 'ft' to 'm' and '%' to 'fraction'
% wlog=l_unit_conversion(wlog,{'ft','m'},{'%','fraction'})
% % Change 'ft' to 'm' and and change '%' to 'fraction' (but only for brine
% saturation); do not print alert messages
% wlog=l_unit_conversion(wlog,{'alert',0},{'ft','m'},{'%','fraction','Sbrine'})
global S4M
for kk=1:length(wlog)
for ll=1:length(varargin)
units=varargin{ll};
if strcmpi(units{1},'alert')
S4M.alert=units{2};
end
if ~strcmpi(units{1},units{2}) & ~strcmpi(units{1},'alert') % Perform conversion only if the two units are different
if size(units) < 2
disp(units)
error(' Input arguments: old and new units must be represented as a two-element cell')
end
switch units{1}
%===========================================================
case 'fraction'
if strcmp(units{2},'%') % fraction ==> %
[wlog,ier]=unit_conversion(wlog,units,'new=old*100');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'ft'
if strcmp(units{2},'m') % ft ==> m
[wlog,ier]=unit_conversion(wlog,units,'new=old*0.3048');
[wlog,ier]=unit_conversion(wlog,{'1/ft','1/m'},'new=old/0.3048');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'g/cm3'
if strcmp(units{2},'ppg') % g/cm3 ==> ppg
[wlog,ier]=unit_conversion(wlog,units,'new=old*8.35');
elseif strcmp(units{2},'kg/m3') % g/cm3 ==> kg/m3
[wlog,ier]=unit_conversion(wlog,units,'new=old*1000');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'kg/m3'
if strcmp(units{2},'ppg') % kg/m3 ==> ppg
[wlog,ier]=unit_conversion(wlog,units,'new=old*0.00835');
elseif strcmp(units{2},'g/cm3') % kg/m3 ==> g/cm3
[wlog,ier]=unit_conversion(wlog,units,'new=old/1000');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'm'
if strcmp(units{2},'ft') % m ==> ft
[wlog,ier]=unit_conversion(wlog,units,'new=old/0.3048');
[wlog,ier]=unit_conversion(wlog,{'1/m','1/ft'},'new=old*0.3048');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case '%'
if strcmp(units{2},'fraction') % % ==> fraction
[wlog,ier]=unit_conversion(wlog,units,'new=old/100');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'us/ft'
if strcmp(units{2},'us/m') % us/ft ==> us/m
[wlog,ier]=unit_conversion(wlog,units,'new=old/0.3048');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'us/m'
if strcmp(units{2},'us/ft') % us/m ==> us/ft
[wlog,ier]=unit_conversion(wlog,units,'new=old*0.3048');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'ft/s'
if strcmp(units{2},'m/s') % ft/s ==> m/s
[wlog,ier]=unit_conversion(wlog,units,'new=old*0.3048');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'm/s'
if strcmp(units{2},'ft/s') % m/s ==> ft/s
[wlog,ier]=unit_conversion(wlog,units,'new=old/0.3048');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 'ms'
if strcmp(units{2},'s') % ms ==> s
[wlog,ier]=unit_conversion(wlog,units,'new=old/1000');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
case 's'
if strcmp(units{2},'ms') % s ==> ms
[wlog,ier]=unit_conversion(wlog,units,'new=old*1000');
else
error([' No conversion option from "',units{1},'" to "',units{2},'" found'])
end
%===========================================================
otherwise
error([' No conversion option from "',units{1},'" available'])
end % End of "switch' block
%===========================================================
end % End of if statement checking if the two units are the same
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [wlog,ier]=unit_conversion(wlog,units,expression)
% Function converts values of one or more curves with the same units of measurement
% using the relationship in input argument "expression"
% INPUT
% wlog log structure
% units cell array with at least two elements (strings) representing original and
% new units of measurement; additional elements (if given) represent curve
% mnemonics
% expression matlab expression of the form 'new=function(old)'
% Examples: 'new=old*0.3048'
% 'new=10*log(old)'
% OUTPUT
% wlog log structure with the updated curve(s)
% ier error code: no error ==> ier = 0
% error ==> ier = 1
% error occur if no curve with specified units of measurement are found or
% if curve mnemonics are specified ("units" has more than 2 elements)
% but are not fund in wlog
% An ALERT message is printed if ier == 1 ang global variable S4M.alert == 1
global S4M
ier=0;
% Check log curves
index=find(ismember(wlog.curve_info(:,2),units{1}));
if isempty(index)
if S4M.alert
disp([' Alert from "l_unit_conversion": no curve with units "',units{1},'" found'])
end
ier=1;
elseif length(units) > 2 % Are there specific curve mnemonics for which to convert units
idx=find(ismember(lower(wlog.curve_info(index,1)),lower(units(3:end))));
if isempty(idx)
if S4M.alert
disp([' Alert from "l_unit_conversion": no specified curve with units "',units{1},'" found'])
disp([' curves with these units: ', ...
cell2str(wlog.curve_info(index,1),', ')])
end
ier=1;
else
index=index(idx);
end
end
if ier == 0
old=wlog.curves(:,index);
eval([expression,';']);
wlog.curves(:,index)=new;
alert([' Log curve(s) ',cell2str(wlog.curve_info(index,1),', '),' changed to units "',units{2},'"'])
wlog.curve_info(index,2)=units(2);
end
% Check parameters for units that need to be converted
if isfield(wlog,'parameter_info')
params=wlog.parameter_info(:,1);
for ii=1:length(params)
temp=param_info(wlog,params{ii});
if strcmp(temp{2},units{1})
old=getfield(wlog,params{ii});
eval([expression,';']);
wlog=add_parameter(wlog,new,{params{ii},units{2},temp{3}});
alert([' Parameter "',params{ii},'" changed'])
end
end
end
% Handle change in the units for depth
if ~isempty(index) & index(1) == 1
wlog.first=wlog.curves(1,1);
wlog.last=wlog.curves(end,1);
old=wlog.step;
eval([expression,';']);
wlog.step=new;
wlog.units=units{2};
end