source: trunk/src/calc_field.m @ 411

Last change on this file since 411 was 411, checked in by sommeria, 12 years ago

bugs corrected in uvmat: fixed x/y and calc_field for the new PIV data

File size: 16.5 KB
Line 
1
2
3%'calc_field': defines fields (velocity, vort, div...) from civx data and calculate them
4%---------------------------------------------------------------------
5% [DataOut,errormsg]=calc_field(FieldList,DataIn,Coord_interp)
6%
7% OUTPUT:
8% Scal: matlab vector representing the scalar values (length nbvec defined by var_read)
9%      if no input, Scal=list of programmed scalar names (to put in menus)
10%      if only the field name is put as input, vec_A=type of scalar, which can be:
11%                   'discrete': related to the individual velocity vectors, not interpolated by patch
12%                   'vel': scalar calculated solely from velocity components
13%                   'der': needs spatial derivatives
14%                   'var': the scalar name directly corresponds to a field name in the netcdf files
15% error: error flag
16%      error = 0; OK
17%      error = 1; the prescribed scalar cannot be read or calculated from available fields
18%
19% INPUT:
20% FieldList: cell array of strings representing the name(s) of the field(s) to calculate
21% DataIn: structure representing the field, as defined in check_field_srtructure.m
22% Coord_interp(:,nb_coord) optional set of coordinates to interpolate the field (use with thin plate shell)
23%
24% FUNCTION related
25% varname_generator.m: determines the field names to read in the netcdf
26% file, depending on the scalar
27
28function [DataOut,errormsg]=calc_field(FieldList,DataIn,Coord_interp)
29
30%list of defined scalars to display in menus (in addition to 'ima_cor').
31% a type is associated to each scalar:
32%              'discrete': related to the individual velocity vectors, not interpolated by patch
33%              'vel': calculated from velocity components, continuous field (interpolated with velocity)
34%              'der': needs spatial derivatives
35%              'var': the scalar name corresponds to a field name in the netcdf files
36% a specific variable name for civ1 and civ2 fields are also associated, if
37% the scalar is calculated from other fields, as explicited below
38
39%% list of field options implemented
40FieldOptions={'velocity';...%image correlation corresponding to a vel vector
41    'ima_cor';...%image correlation corresponding to a vel vector
42    'norm_vel';...%norm of the velocity
43    'vort';...%vorticity
44    'div';...%divergence
45    'strain';...%rate of strain
46    'u';... %u velocity component
47    'v';... %v velocity component
48    'w';... %w velocity component
49    'w_normal';... %w velocity component normal to the plane
50    'error'}; %error associated to a vector (for stereo or patch)
51errormsg=[]; %default error message
52if ~exist('FieldList','var')
53    DataOut=FieldOptions;% gives the list of possible field inputs in the absence of input
54    return
55end
56
57%% check input
58if ~exist('DataIn','var')
59    DataIn=[];
60end
61if ischar(FieldList)
62    FieldList={FieldList};%convert a string input to a cell with one string element
63end
64check_grid=0;
65check_der=0;
66check_calc=ones(size(FieldList));
67for ilist=1:length(FieldList)
68    switch FieldList{ilist}
69        case {'u','v'}
70            check_grid=1;% needs a regular grid
71        case{'vort','div','strain'}% needs spatial derivatives spatial derivatives
72            check_der=1;
73        case {'velocity','norm_vel','ima_cor'};
74        otherwise
75            check_calc(ilist)=0;
76    end
77end
78FieldList=FieldList(check_calc==1);
79% if isempty(FieldList)
80%     DataOut=DataIn;
81%     return
82% end
83if isfield(DataIn,'Z')&& isequal(size(DataIn.Z),size(DataIn.X))
84    nbcoord=3;
85else
86    nbcoord=2;
87end
88ListVarName={};
89ValueList={};
90RoleList={};
91units_cell={};
92
93%% interpolation with new civ data
94if isfield(DataIn,'SubRange') && isfield(DataIn,'Coord_tps') && (exist('Coord_interp','var') || check_grid ||check_der)
95    %reproduce global attributes
96    DataOut.ListGlobalAttribute=DataIn.ListGlobalAttribute;
97    for ilist=1:numel(DataOut.ListGlobalAttribute)
98        DataOut.(DataOut.ListGlobalAttribute{ilist})=DataIn.(DataIn.ListGlobalAttribute{ilist});
99    end
100
101    %create a default grid if needed
102    if  ~exist('Coord_interp','var')
103            XMax=max(max(DataIn.SubRange(1,:,:)));% extrema of the coordinates
104            YMax=max(max(DataIn.SubRange(2,:,:)));
105            XMin=min(min(DataIn.SubRange(1,:,:)));
106            YMin=min(min(DataIn.SubRange(2,:,:)));
107        if ~isfield(DataIn,'Mesh')
108            DataIn.Mesh=sqrt(2*(XMax-XMin)*(YMax-YMin)/numel(DataIn.Coord_tps));
109            % adjust the mesh to a value 1, 2 , 5 *10^n
110            ord=10^(floor(log10(DataIn.Mesh)));%order of magnitude
111            if DataIn.Mesh/ord>=5
112                DataIn.Mesh=5*ord;
113            elseif DataIn.Mesh/ord>=2
114                DataIn.Mesh=2*ord;
115            else
116                DataIn.Mesh=ord;
117            end
118        end
119        coord_x=XMin:DataIn.Mesh:XMax;
120        coord_y=YMin:DataIn.Mesh:YMax;
121        npx=length(coord_x);
122        npy=length(coord_y);
123        DataOut.coord_x=[XMin XMax];
124        DataOut.coord_y=[YMin YMax];
125        [XI,YI]=meshgrid(coord_x,coord_y);
126        XI=reshape(XI,[],1);
127        YI=reshape(YI,[],1);
128        Coord_interp=[XI YI];
129    end
130   
131    %initialise output
132    nb_sites=size(Coord_interp,1);
133    nb_coord=size(Coord_interp,2);
134    nbval=zeros(nb_sites,1);
135    NbSubDomain=size(DataIn.SubRange,3);
136    DataOut.ListVarName={'coord_y','coord_x'};
137    DataOut.VarDimName={{'coord_y'},{'coord_x'}};
138    DataOut.VarAttribute{1}=[];
139    DataOut.VarAttribute{2}=[];
140    for ilist=1:length(FieldList)
141        switch FieldList{ilist}
142            case 'velocity'
143                DataOut.U=zeros(nb_sites,1);
144                DataOut.V=zeros(nb_sites,1);
145            otherwise
146                DataOut.(FieldList{ilist})=zeros(nb_sites,1);
147        end
148    end
149   
150    % interpolate data in each subdomain
151    for isub=1:NbSubDomain
152        nbvec_sub=DataIn.NbSites(isub);
153        check_range=(Coord_interp >=ones(nb_sites,1)*DataIn.SubRange(:,1,isub)' & Coord_interp<=ones(nb_sites,1)*DataIn.SubRange(:,2,isub)');
154        ind_sel=find(sum(check_range,2)==nb_coord);
155        %rho smoothing parameter
156        %                 epoints = Coord_interp(ind_sel) ;% coordinates of interpolation sites
157        %                 ctrs=DataIn.Coord_tps(1:nbvec_sub,:,isub);%(=initial points) ctrs
158        nbval(ind_sel)=nbval(ind_sel)+1;% records the number of values for eacn interpolation point (in case of subdomain overlap)
159        if check_grid
160            EM = tps_eval(Coord_interp(ind_sel,:),DataIn.Coord_tps(1:nbvec_sub,:,isub));%kernels for calculating the velocity from tps 'sources'
161        end
162        if check_der
163            [EMDX,EMDY] = tps_eval_dxy(Coord_interp(ind_sel,:),DataIn.Coord_tps(1:nbvec_sub,:,isub));%kernels for calculating the spatial derivatives from tps 'sources'
164        end
165        var_count=0;
166        for ilist=1:length(FieldList)
167            switch FieldList{ilist}
168                case 'velocity'
169                    ListFields={'U', 'V'};
170                    VarAttribute{var_count+1}.Role='vector_x';
171                    VarAttribute{var_count+2}.Role='vector_y';
172                    DataOut.U(ind_sel)=DataOut.U(ind_sel)+EM *DataIn.U_tps(1:nbvec_sub+3,isub);
173                    DataOut.V(ind_sel)=DataOut.V(ind_sel)+EM *DataIn.V_tps(1:nbvec_sub+3,isub);
174                case 'u'
175                    ListFields={'u'};
176                    VarAttribute{var_count+1}.Role='scalar';
177                    DataOut.u(ind_sel)=DataOut.u(ind_sel)+EM *DataIn.U_tps(1:nbvec_sub+3,isub);
178                case 'v'
179                    ListFields={'v'};
180                    VarAttribute{var_count+1}.Role='scalar';
181                    DataOut.v(ind_sel)=DataOut.v(ind_sel)+EM *DataIn.V_tps(1:nbvec_sub+3,isub);
182                case 'norm_vel'
183                    ListFields={'norm_vel'};
184                    VarAttribute{var_count+1}.Role='scalar';
185                    U=DataOut.U(ind_sel)+EM *DataIn.U_tps(1:nbvec_sub+3,isub);
186                    V=DataOut.V(ind_sel)+EM *DataIn.V_tps(1:nbvec_sub+3,isub);
187                    DataOut.norm_vel(ind_sel)=sqrt(U.*U+V.*V);
188                case 'vort'
189                    ListFields={'vort'};
190                    VarAttribute{var_count+1}.Role='scalar';
191                    DataOut.vort(ind_sel)=DataOut.vort(ind_sel)-EMDY *DataIn.U_tps(1:nbvec_sub+3,isub)+EMDX *DataIn.V_tps(1:nbvec_sub+3,isub);
192                case 'div'
193                    ListFields={'div'};
194                    VarAttribute{var_count+1}.Role='scalar';
195                    DataOut.div(ind_sel)=DataOut.div(ind_sel)+EMDX*DataIn.U_tps(1:nbvec_sub+3,isub)+EMDY *DataIn.V_tps(1:nbvec_sub+3,isub);
196                case 'strain'
197                    ListFields={'strain'};
198                    VarAttribute{var_count+1}.Role='scalar';
199                    DataOut.strain(ind_sel)=DataOut.strain(ind_sel)+EMDY*DataIn.U_tps(1:nbvec_sub+3,isub)+EMDX *DataIn.V_tps(1:nbvec_sub+3,isub);
200            end
201        end
202    end
203    DataOut.FF=nbval==0; %put errorflag to 1 for points outside the interpolation rang
204    nbval(nbval==0)=1;
205    if isempty(find(strcmp('FF',DataOut.ListVarName),1))% if FF is not already listed
206        DataOut.ListVarName=[DataOut.ListVarName {'FF'}];
207        DataOut.VarDimName=[DataOut.VarDimName {{'coord_y','coord_x'}}];
208        DataOut.VarAttribute{length(DataOut.ListVarName)}.Role='errorflag';
209    end
210    DataOut.ListVarName=[DataOut.ListVarName ListFields];
211    for ifield=1:numel(ListFields)
212        VarDimName{ifield}={'coord_y','coord_x'};
213        DataOut.(ListFields{ifield})=reshape(DataOut.(ListFields{ifield}),npy,npx);
214    end
215    DataOut.FF=reshape(DataOut.FF,npy,npx);
216    DataOut.VarDimName=[DataOut.VarDimName VarDimName];     
217    DataOut.VarAttribute=[DataOut.VarAttribute VarAttribute];
218else
219
220    %% civx data
221    DataOut=DataIn;
222    for ilist=1:length(FieldList)
223        if ~isempty(FieldList{ilist})
224            [VarName,Value,Role,units]=feval(FieldList{ilist},DataIn);%calculate field with appropriate function named FieldList{ilist}
225            ListVarName=[ListVarName VarName];
226            ValueList=[ValueList Value];
227            RoleList=[RoleList Role];
228            units_cell=[units_cell units];
229        end
230    end
231    %erase previous data (except coordinates)
232    for ivar=nbcoord+1:length(DataOut.ListVarName)
233        VarName=DataOut.ListVarName{ivar};
234        DataOut=rmfield(DataOut,VarName);
235    end
236    DataOut.ListVarName=DataOut.ListVarName(1:nbcoord);
237    if isfield(DataOut,'VarDimName')
238        DataOut.VarDimName=DataOut.VarDimName(1:nbcoord);
239    else
240        errormsg='element .VarDimName missing in input data';
241        return
242    end
243    DataOut.VarAttribute=DataOut.VarAttribute(1:nbcoord);
244    %append new data
245    DataOut.ListVarName=[DataOut.ListVarName ListVarName];
246    for ivar=1:length(ListVarName)
247        DataOut.VarDimName{nbcoord+ivar}=DataOut.VarDimName{1};
248        DataOut.VarAttribute{nbcoord+ivar}.Role=RoleList{ivar};
249        DataOut.VarAttribute{nbcoord+ivar}.units=units_cell{ivar};
250        DataOut.(ListVarName{ivar})=ValueList{ivar};
251    end
252end
253
254
255
256%%%%%%%%%%%%% velocity fieldn%%%%%%%%%%%%%%%%%%%%
257function [VarName,ValCell,Role,units_cell]=velocity(DataIn)
258VarName={};
259ValCell={};
260Role={};
261units_cell={};
262if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
263    units=[DataIn.CoordUnit '/' DataIn.TimeUnit];
264else
265    units='pixel';
266end
267if isfield(DataIn,'U')
268    VarName=[VarName {'U'}];
269    ValCell=[ValCell {DataIn.U}];
270    Role=[Role {'vector_x'}];
271    units_cell=[units_cell {units}];
272end
273if isfield(DataIn,'V')
274    VarName=[VarName {'V'}];
275    ValCell=[ValCell {DataIn.V}];
276    Role=[Role {'vector_y'}];
277    units_cell=[units_cell {units}];
278end
279if isfield(DataIn,'W')
280    VarName=[VarName {'W'}];
281    ValCell=[ValCell {DataIn.W}];
282    Role=[Role {'vector_z'}];
283    units_cell=[units_cell {units}];
284end
285if isfield(DataIn,'F')
286    VarName=[VarName {'F'}];
287    ValCell=[ValCell {DataIn.F}];
288    Role=[Role {'warnflag'}];
289    units_cell=[units_cell {[]}];
290end
291if isfield(DataIn,'FF')
292    VarName=[VarName,{'FF'}];
293    ValCell=[ValCell {DataIn.FF}];
294    Role=[Role {'errorflag'}];
295    units_cell=[units_cell {[]}];
296end
297
298%%%%%%%%%%%%% ima cor%%%%%%%%%%%%%%%%%%%%
299function [VarName,ValCell,Role,units]=ima_cor(DataIn)
300VarName={};
301ValCell={};
302Role={};
303units={};
304if isfield(DataIn,'C')
305    VarName{1}='C';
306    ValCell{1}=DataIn.C;
307    Role={'ancillary'};
308    units={[]};
309end
310
311%%%%%%%%%%%%% norm_vec %%%%%%%%%%%%%%%%%%%%
312function [VarName,ValCell,Role,units]=norm_vel(DataIn)
313VarName={};
314ValCell={};
315Role={};
316units={};
317if isfield(DataIn,'U') && isfield(DataIn,'V')
318    VarName{1}='norm_vel';
319    ValCell{1}=DataIn.U.*DataIn.U+ DataIn.V.*DataIn.V;
320    if isfield(DataIn,'W') && isequal(size(DataIn.W),size(DataIn.U))
321        ValCell{1}=ValCell{1}+DataIn.W.*DataIn.W;
322    end
323    ValCell{1}=sqrt(ValCell{1});
324    Role{1}='scalar';
325    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
326        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
327    else
328        units={'pixel'};
329    end
330end
331
332
333
334%%%%%%%%%%%%% vorticity%%%%%%%%%%%%%%%%%%%%
335function [VarName,ValCell,Role,units]=vort(DataIn)
336VarName={};
337ValCell={};
338Role={};
339units={};
340if isfield(DataIn,'DjUi')
341    VarName{1}='vort';
342    ValCell{1}=DataIn.DjUi(:,1,2)-DataIn.DjUi(:,2,1);  %vorticity
343    siz=size(ValCell{1});
344    ValCell{1}=reshape(ValCell{1},siz(1),1);
345    Role{1}='scalar';
346    if isfield(DataIn,'TimeUnit')
347        units={[DataIn.TimeUnit '-1']};
348    else
349        units={[]};
350    end
351end
352
353%%%%%%%%%%%%% divergence%%%%%%%%%%%%%%%%%%%%
354function [VarName,ValCell,Role,units]=div(DataIn)
355VarName={};
356ValCell={};
357Role={};
358units={};
359if isfield(DataIn,'DjUi')
360    VarName{1}='div';
361    ValCell{1}=DataIn.DjUi(:,1,1)+DataIn.DjUi(:,2,2); %DUDX+DVDY
362    siz=size(ValCell{1});
363    ValCell{1}=reshape(ValCell{1},siz(1),1);
364    Role{1}='scalar';
365    if isfield(DataIn,'TimeUnit')
366        units={[DataIn.TimeUnit '-1']};
367    else
368        units={[]};
369    end
370end
371
372%%%%%%%%%%%%% strain %%%%%%%%%%%%%%%%%%%%
373function [VarName,ValCell,Role,units]=strain(DataIn)
374VarName={};
375ValCell={};
376Role={};
377units={};
378if isfield(DataIn,'DjUi')
379    VarName{1}='strain';
380    ValCell{1}=DataIn.DjUi(:,1,2)+DataIn.DjUi(:,2,1);%DVDX+DUDY
381    siz=size(ValCell{1});
382    ValCell{1}=reshape(ValCell{1},siz(1),1);
383    Role{1}='scalar';
384    if isfield(DataIn,'TimeUnit')
385        units={[DataIn.TimeUnit '-1']};
386    else
387        units={[]};
388    end
389end
390
391%%%%%%%%%%%%% u %%%%%%%%%%%%%%%%%%%%
392function [VarName,ValCell,Role,units]=u(DataIn)
393VarName={};
394ValCell={};
395Role={};
396units={};
397if isfield(DataIn,'U')
398    VarName{1}='U';
399    ValCell{1}=DataIn.U;
400    Role{1}='scalar';
401    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
402        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
403    else
404        units={'pixel'};
405    end
406end
407
408%%%%%%%%%%%%% v %%%%%%%%%%%%%%%%%%%%
409function [VarName,ValCell,Role,units]=v(DataIn)
410VarName={};
411ValCell={};
412Role={};
413units={};
414if isfield(DataIn,'V')
415    VarName{1}='V';
416    ValCell{1}=DataIn.V;
417    Role{1}='scalar';
418    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
419        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
420    else
421        units={'pixel'};
422    end
423end
424
425%%%%%%%%%%%%% w %%%%%%%%%%%%%%%%%%%%
426function [VarName,ValCell,Role,units]=w(DataIn)
427VarName={};
428ValCell={};
429Role={};
430units={};
431if isfield(DataIn,'W')
432    VarName{1}='W';
433    ValCell{1}=DataIn.W;
434    Role{1}='scalar';%will remain unchanged by projection
435    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
436        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
437    else
438        units={'pixel'};
439    end
440end
441
442%%%%%%%%%%%%% w_normal %%%%%%%%%%%%%%%%%%%%
443function [VarName,ValCell,Role,units]=w_normal(DataIn)
444VarName={};
445ValCell={};
446Role={};
447units={};
448if isfield(DataIn,'W')
449    VarName{1}='W';
450    ValCell{1}=DataIn.W;
451    Role{1}='vector_z';%will behave like a vector component  by projection
452    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
453        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
454    else
455        units={'pixel'};
456    end
457end
458
459%%%%%%%%%%%%% error %%%%%%%%%%%%%%%%%%%%
460function [VarName,ValCell,Role,units]=error(DataIn)
461VarName={};
462ValCell={};
463Role={};
464units={};
465if isfield(DataIn,'E')
466    VarName{1}='E';
467    ValCell{1}=DataIn.E;
468    Role{1}='ancillary'; %TODO CHECK units in actual fields
469    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
470        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
471    else
472        units={'pixel'};
473    end
474end
475
Note: See TracBrowser for help on using the repository browser.