source: trunk/src/calc_field.m @ 236

Last change on this file since 236 was 236, checked in by sommeria, 13 years ago

correct Matlab PIV, remove call to image tool box. Improve menu of uvmat VelType? (replacement of buttons)

File size: 9.7 KB
Line 
1%'calc_field': defines fields (velocity, vort, div...) from civx data and calculate them
2%---------------------------------------------------------------------
3% [DataOut,errormsg]=calc_field(FieldName,DataIn)
4%
5% OUTPUT:
6% Scal: matlab vector representing the scalar values (length nbvec defined by var_read)
7%      if no input, Scal=list of programmed scalar names (to put in menus)
8%      if only the field name is put as input, vec_A=type of scalar, which can be:
9%                   'discrete': related to the individual velocity vectors, not interpolated by patch
10%                   'vel': scalar calculated solely from velocity components
11%                   'der': needs spatial derivatives
12%                   'var': the scalar name directly corresponds to a field name in the netcdf files
13% error: error flag
14%      error = 0; OK
15%      error = 1; the prescribed scalar cannot be read or calculated from available fields
16%
17% INPUT:
18% FieldName: string representing the name of the field
19% DataIn: structure representing the field, as defined in check_field_srtructure.m
20%
21% FUNCTION related
22% varname_generator.m: determines the field names to read in the netcdf
23% file, depending on the scalar
24
25function [DataOut,errormsg]=calc_field(FieldName,DataIn)
26
27%list of defined scalars to display in menus (in addition to 'ima_cor').
28% a type is associated to each scalar:
29%              'discrete': related to the individual velocity vectors, not interpolated by patch
30%              'vel': calculated from velocity components, continuous field (interpolated with velocity)
31%              'der': needs spatial derivatives
32%              'var': the scalar name corresponds to a field name in the netcdf files
33% a specific variable name for civ1 and civ2 fields are also associated, if
34% the scalar is calculated from other fields, as explicited below
35
36list_field={'velocity';...%image correlation corresponding to a vel vector
37    'ima_cor';...%image correlation corresponding to a vel vector
38    'norm_vel';...%norm of the velocity
39    'vort';...%vorticity
40    'div';...%divergence
41    'strain';...%rate of strain
42    'u';... %u velocity component
43    'v';... %v velocity component
44    'w';... %w velocity component
45    'w_normal';... %w velocity component normal to the plane
46    'error'}; %error associated to a vector (for stereo or patch)
47errormsg=[]; %default error message
48if ~exist('FieldName','var')
49    DataOut=list_field;% gives the list of possible fields in the absence of input
50else
51    if ~exist('DataIn','var')
52        DataIn=[];
53    end
54    if ischar(FieldName)
55        FieldName={FieldName};%convert a string input to a cell with one string element
56    end
57    if isfield(DataIn,'Z')&& isequal(size(DataIn.Z),size(DataIn.X))
58        nbcoord=3;
59    else
60        nbcoord=2;
61    end
62    DataOut=DataIn; %reproduce global attribute
63    ListVarName={};
64    ValueList={};
65    RoleList={};
66    units_cell={};
67    for ilist=1:length(FieldName)
68        if ~isempty(FieldName{ilist})
69            [VarName,Value,Role,units]=feval(FieldName{ilist},DataIn);%calculate field with appropriate function named FieldName{ilist}
70            ListVarName=[ListVarName VarName];
71            ValueList=[ValueList Value];
72            RoleList=[RoleList Role];
73            units_cell=[units_cell units];
74        end
75    end
76    %erase previous data (except coordinates)
77    for ivar=nbcoord+1:length(DataOut.ListVarName)
78        VarName=DataOut.ListVarName{ivar};
79        DataOut=rmfield(DataOut,VarName);
80    end
81    DataOut.ListVarName=DataOut.ListVarName(1:nbcoord);
82    if isfield(DataOut,'VarDimName')
83        DataOut.VarDimName=DataOut.VarDimName(1:nbcoord);
84    else
85        errormsg='element .VarDimName missing in input data';
86        return
87    end
88    DataOut.VarAttribute=DataOut.VarAttribute(1:nbcoord);
89    %append new data
90    DataOut.ListVarName=[DataOut.ListVarName ListVarName];
91    for ivar=1:length(ListVarName)
92        DataOut.VarDimName{nbcoord+ivar}=DataOut.VarDimName{1};
93        DataOut.VarAttribute{nbcoord+ivar}.Role=RoleList{ivar};
94        DataOut.VarAttribute{nbcoord+ivar}.units=units_cell{ivar};
95        eval(['DataOut.' ListVarName{ivar} '=ValueList{ivar};'])
96    end
97end
98
99%%%%%%%%%%%%% velocity fieldn%%%%%%%%%%%%%%%%%%%%
100function [VarName,ValCell,Role,units_cell]=velocity(DataIn)
101VarName={};
102ValCell={};
103Role={};
104units_cell={};
105if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
106    units=[DataIn.CoordUnit '/' DataIn.TimeUnit];
107else
108    units='pixel';
109end
110if isfield(DataIn,'U')
111    VarName=[VarName {'U'}];
112    ValCell=[ValCell {DataIn.U}];
113    Role=[Role {'vector_x'}];
114    units_cell=[units_cell {units}];
115end
116if isfield(DataIn,'V')
117    VarName=[VarName {'V'}];
118    ValCell=[ValCell {DataIn.V}];
119    Role=[Role {'vector_y'}];
120    units_cell=[units_cell {units}];
121end
122if isfield(DataIn,'W')
123    VarName=[VarName {'W'}];
124    ValCell=[ValCell {DataIn.W}];
125    Role=[Role {'vector_z'}];
126    units_cell=[units_cell {units}];
127end
128if isfield(DataIn,'F')
129    VarName=[VarName {'F'}];
130    ValCell=[ValCell {DataIn.F}];
131    Role=[Role {'warnflag'}];
132    units_cell=[units_cell {[]}];
133end
134if isfield(DataIn,'FF')
135    VarName=[VarName,{'FF'}];
136    ValCell=[ValCell {DataIn.FF}];
137    Role=[Role {'errorflag'}];
138    units_cell=[units_cell {[]}];
139end
140
141%%%%%%%%%%%%% ima cor%%%%%%%%%%%%%%%%%%%%
142function [VarName,ValCell,Role,units]=ima_cor(DataIn)
143VarName={};
144ValCell={};
145Role={};
146units={};
147if isfield(DataIn,'C')
148    VarName{1}='C';
149    ValCell{1}=DataIn.C;
150    Role={'ancillary'};
151    units={[]};
152end
153
154%%%%%%%%%%%%% norm_vec %%%%%%%%%%%%%%%%%%%%
155function [VarName,ValCell,Role,units]=norm_vel(DataIn)
156VarName={};
157ValCell={};
158Role={};
159units={};
160if isfield(DataIn,'U') && isfield(DataIn,'V')
161    VarName{1}='norm_vel';
162    ValCell{1}=DataIn.U.*DataIn.U+ DataIn.V.*DataIn.V;
163    if isfield(DataIn,'W') && isequal(size(DataIn.W),size(DataIn.U))
164        ValCell{1}=ValCell{1}+DataIn.W.*DataIn.W;
165    end
166    ValCell{1}=sqrt(ValCell{1});
167    Role{1}='scalar';
168    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
169        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
170    else
171        units={'pixel'};
172    end
173end
174
175
176
177%%%%%%%%%%%%% vorticity%%%%%%%%%%%%%%%%%%%%
178function [VarName,ValCell,Role,units]=vort(DataIn)
179VarName={};
180ValCell={};
181Role={};
182units={};
183if isfield(DataIn,'DjUi')
184    VarName{1}='vort';
185    ValCell{1}=DataIn.DjUi(:,1,2)-DataIn.DjUi(:,2,1);  %vorticity
186    siz=size(ValCell{1});
187    ValCell{1}=reshape(ValCell{1},siz(1),1);
188    Role{1}='scalar';
189    if isfield(DataIn,'TimeUnit')
190        units={[DataIn.TimeUnit '-1']};
191    else
192        units={[]};
193    end
194end
195
196%%%%%%%%%%%%% divergence%%%%%%%%%%%%%%%%%%%%
197function [VarName,ValCell,Role,units]=div(DataIn)
198VarName={};
199ValCell={};
200Role={};
201units={};
202if isfield(DataIn,'DjUi')
203    VarName{1}='div';
204    ValCell{1}=DataIn.DjUi(:,1,1)+DataIn.DjUi(:,2,2); %DUDX+DVDY
205    siz=size(ValCell{1});
206    ValCell{1}=reshape(ValCell{1},siz(1),1);
207    Role{1}='scalar';
208    if isfield(DataIn,'TimeUnit')
209        units={[DataIn.TimeUnit '-1']};
210    else
211        units={[]};
212    end
213end
214
215%%%%%%%%%%%%% strain %%%%%%%%%%%%%%%%%%%%
216function [VarName,ValCell,Role,units]=strain(DataIn)
217VarName={};
218ValCell={};
219Role={};
220units={};
221if isfield(DataIn,'DjUi')
222    VarName{1}='strain';
223    ValCell{1}=DataIn.DjUi(:,1,2)+DataIn.DjUi(:,2,1);%DVDX+DUDY
224    siz=size(ValCell{1});
225    ValCell{1}=reshape(ValCell{1},siz(1),1);
226    Role{1}='scalar';
227    if isfield(DataIn,'TimeUnit')
228        units={[DataIn.TimeUnit '-1']};
229    else
230        units={[]};
231    end
232end
233
234%%%%%%%%%%%%% u %%%%%%%%%%%%%%%%%%%%
235function [VarName,ValCell,Role,units]=u(DataIn)
236VarName={};
237ValCell={};
238Role={};
239units={};
240if isfield(DataIn,'U')
241    VarName{1}='U';
242    ValCell{1}=DataIn.U;
243    Role{1}='scalar';
244    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
245        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
246    else
247        units={'pixel'};
248    end
249end
250
251%%%%%%%%%%%%% v %%%%%%%%%%%%%%%%%%%%
252function [VarName,ValCell,Role,units]=v(DataIn)
253VarName={};
254ValCell={};
255Role={};
256units={};
257if isfield(DataIn,'V')
258    VarName{1}='V';
259    ValCell{1}=DataIn.V;
260    Role{1}='scalar';
261    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
262        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
263    else
264        units={'pixel'};
265    end
266end
267
268%%%%%%%%%%%%% w %%%%%%%%%%%%%%%%%%%%
269function [VarName,ValCell,Role,units]=w(DataIn)
270VarName={};
271ValCell={};
272Role={};
273units={};
274if isfield(DataIn,'W')
275    VarName{1}='W';
276    ValCell{1}=DataIn.W;
277    Role{1}='scalar';%will remain unchanged by projection
278    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
279        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
280    else
281        units={'pixel'};
282    end
283end
284
285%%%%%%%%%%%%% w_normal %%%%%%%%%%%%%%%%%%%%
286function [VarName,ValCell,Role,units]=w_normal(DataIn)
287VarName={};
288ValCell={};
289Role={};
290units={};
291if isfield(DataIn,'W')
292    VarName{1}='W';
293    ValCell{1}=DataIn.W;
294    Role{1}='vector_z';%will behave like a vector component  by projection
295    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
296        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
297    else
298        units={'pixel'};
299    end
300end
301
302%%%%%%%%%%%%% error %%%%%%%%%%%%%%%%%%%%
303function [VarName,ValCell,Role,units]=error(DataIn)
304VarName={};
305ValCell={};
306Role={};
307units={};
308if isfield(DataIn,'E')
309    VarName{1}='E';
310    ValCell{1}=DataIn.E;
311    Role{1}='ancillary'; %TODO CHECK units in actual fields
312    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
313        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
314    else
315        units={'pixel'};
316    end
317end
318
Note: See TracBrowser for help on using the repository browser.