source: trunk/src/calc_field.m @ 408

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

various bugs corrected in series and uvmat.

File size: 16.4 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                    VarAttributes{var_count+1}.Role='vector_x';
171                    VarAttributes{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                    VarAttributes{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                    VarAttributes{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                    VarAttributes{var_count+1}.Role='scalar';
185                    V=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                    VarAttributes{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                    VarAttributes{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                    VarAttributes{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            DataOut.FF=nbval==0; %put errorflag to 1 for points outside the interpolation rang
202            nbval(nbval==0)=1;
203            DataOut.ListVarName=[DataOut.ListVarName ListFields {'FF'}];
204            for ilist=1:numel(ListFields)
205                VarDimName{ilist}={'coord_y','coord_x'};
206                DataOut.(ListFields{ilist})=reshape(DataOut.(ListFields{ilist}),npy,npx);
207            end
208            DataOut.FF=reshape(DataOut.FF,npy,npx);
209            DataOut.VarDimName=[DataOut.VarDimName VarDimName {{'coord_y','coord_x'}}] ;
210            VarAttributes{length(ListFields)+1}.Role='errorflag';
211            DataOut.VarAttribute=[DataOut.VarAttribute VarAttributes];
212        end
213    end
214else
215
216    %% civx data
217    DataOut=DataIn;
218    for ilist=1:length(FieldList)
219        if ~isempty(FieldList{ilist})
220            [VarName,Value,Role,units]=feval(FieldList{ilist},DataIn);%calculate field with appropriate function named FieldList{ilist}
221            ListVarName=[ListVarName VarName];
222            ValueList=[ValueList Value];
223            RoleList=[RoleList Role];
224            units_cell=[units_cell units];
225        end
226    end
227    %erase previous data (except coordinates)
228    for ivar=nbcoord+1:length(DataOut.ListVarName)
229        VarName=DataOut.ListVarName{ivar};
230        DataOut=rmfield(DataOut,VarName);
231    end
232    DataOut.ListVarName=DataOut.ListVarName(1:nbcoord);
233    if isfield(DataOut,'VarDimName')
234        DataOut.VarDimName=DataOut.VarDimName(1:nbcoord);
235    else
236        errormsg='element .VarDimName missing in input data';
237        return
238    end
239    DataOut.VarAttribute=DataOut.VarAttribute(1:nbcoord);
240    %append new data
241    DataOut.ListVarName=[DataOut.ListVarName ListVarName];
242    for ivar=1:length(ListVarName)
243        DataOut.VarDimName{nbcoord+ivar}=DataOut.VarDimName{1};
244        DataOut.VarAttribute{nbcoord+ivar}.Role=RoleList{ivar};
245        DataOut.VarAttribute{nbcoord+ivar}.units=units_cell{ivar};
246        DataOut.(ListVarName{ivar})=ValueList{ivar};
247    end
248end
249
250
251
252%%%%%%%%%%%%% velocity fieldn%%%%%%%%%%%%%%%%%%%%
253function [VarName,ValCell,Role,units_cell]=velocity(DataIn)
254VarName={};
255ValCell={};
256Role={};
257units_cell={};
258if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
259    units=[DataIn.CoordUnit '/' DataIn.TimeUnit];
260else
261    units='pixel';
262end
263if isfield(DataIn,'U')
264    VarName=[VarName {'U'}];
265    ValCell=[ValCell {DataIn.U}];
266    Role=[Role {'vector_x'}];
267    units_cell=[units_cell {units}];
268end
269if isfield(DataIn,'V')
270    VarName=[VarName {'V'}];
271    ValCell=[ValCell {DataIn.V}];
272    Role=[Role {'vector_y'}];
273    units_cell=[units_cell {units}];
274end
275if isfield(DataIn,'W')
276    VarName=[VarName {'W'}];
277    ValCell=[ValCell {DataIn.W}];
278    Role=[Role {'vector_z'}];
279    units_cell=[units_cell {units}];
280end
281if isfield(DataIn,'F')
282    VarName=[VarName {'F'}];
283    ValCell=[ValCell {DataIn.F}];
284    Role=[Role {'warnflag'}];
285    units_cell=[units_cell {[]}];
286end
287if isfield(DataIn,'FF')
288    VarName=[VarName,{'FF'}];
289    ValCell=[ValCell {DataIn.FF}];
290    Role=[Role {'errorflag'}];
291    units_cell=[units_cell {[]}];
292end
293
294%%%%%%%%%%%%% ima cor%%%%%%%%%%%%%%%%%%%%
295function [VarName,ValCell,Role,units]=ima_cor(DataIn)
296VarName={};
297ValCell={};
298Role={};
299units={};
300if isfield(DataIn,'C')
301    VarName{1}='C';
302    ValCell{1}=DataIn.C;
303    Role={'ancillary'};
304    units={[]};
305end
306
307%%%%%%%%%%%%% norm_vec %%%%%%%%%%%%%%%%%%%%
308function [VarName,ValCell,Role,units]=norm_vel(DataIn)
309VarName={};
310ValCell={};
311Role={};
312units={};
313if isfield(DataIn,'U') && isfield(DataIn,'V')
314    VarName{1}='norm_vel';
315    ValCell{1}=DataIn.U.*DataIn.U+ DataIn.V.*DataIn.V;
316    if isfield(DataIn,'W') && isequal(size(DataIn.W),size(DataIn.U))
317        ValCell{1}=ValCell{1}+DataIn.W.*DataIn.W;
318    end
319    ValCell{1}=sqrt(ValCell{1});
320    Role{1}='scalar';
321    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
322        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
323    else
324        units={'pixel'};
325    end
326end
327
328
329
330%%%%%%%%%%%%% vorticity%%%%%%%%%%%%%%%%%%%%
331function [VarName,ValCell,Role,units]=vort(DataIn)
332VarName={};
333ValCell={};
334Role={};
335units={};
336if isfield(DataIn,'DjUi')
337    VarName{1}='vort';
338    ValCell{1}=DataIn.DjUi(:,1,2)-DataIn.DjUi(:,2,1);  %vorticity
339    siz=size(ValCell{1});
340    ValCell{1}=reshape(ValCell{1},siz(1),1);
341    Role{1}='scalar';
342    if isfield(DataIn,'TimeUnit')
343        units={[DataIn.TimeUnit '-1']};
344    else
345        units={[]};
346    end
347end
348
349%%%%%%%%%%%%% divergence%%%%%%%%%%%%%%%%%%%%
350function [VarName,ValCell,Role,units]=div(DataIn)
351VarName={};
352ValCell={};
353Role={};
354units={};
355if isfield(DataIn,'DjUi')
356    VarName{1}='div';
357    ValCell{1}=DataIn.DjUi(:,1,1)+DataIn.DjUi(:,2,2); %DUDX+DVDY
358    siz=size(ValCell{1});
359    ValCell{1}=reshape(ValCell{1},siz(1),1);
360    Role{1}='scalar';
361    if isfield(DataIn,'TimeUnit')
362        units={[DataIn.TimeUnit '-1']};
363    else
364        units={[]};
365    end
366end
367
368%%%%%%%%%%%%% strain %%%%%%%%%%%%%%%%%%%%
369function [VarName,ValCell,Role,units]=strain(DataIn)
370VarName={};
371ValCell={};
372Role={};
373units={};
374if isfield(DataIn,'DjUi')
375    VarName{1}='strain';
376    ValCell{1}=DataIn.DjUi(:,1,2)+DataIn.DjUi(:,2,1);%DVDX+DUDY
377    siz=size(ValCell{1});
378    ValCell{1}=reshape(ValCell{1},siz(1),1);
379    Role{1}='scalar';
380    if isfield(DataIn,'TimeUnit')
381        units={[DataIn.TimeUnit '-1']};
382    else
383        units={[]};
384    end
385end
386
387%%%%%%%%%%%%% u %%%%%%%%%%%%%%%%%%%%
388function [VarName,ValCell,Role,units]=u(DataIn)
389VarName={};
390ValCell={};
391Role={};
392units={};
393if isfield(DataIn,'U')
394    VarName{1}='U';
395    ValCell{1}=DataIn.U;
396    Role{1}='scalar';
397    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
398        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
399    else
400        units={'pixel'};
401    end
402end
403
404%%%%%%%%%%%%% v %%%%%%%%%%%%%%%%%%%%
405function [VarName,ValCell,Role,units]=v(DataIn)
406VarName={};
407ValCell={};
408Role={};
409units={};
410if isfield(DataIn,'V')
411    VarName{1}='V';
412    ValCell{1}=DataIn.V;
413    Role{1}='scalar';
414    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
415        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
416    else
417        units={'pixel'};
418    end
419end
420
421%%%%%%%%%%%%% w %%%%%%%%%%%%%%%%%%%%
422function [VarName,ValCell,Role,units]=w(DataIn)
423VarName={};
424ValCell={};
425Role={};
426units={};
427if isfield(DataIn,'W')
428    VarName{1}='W';
429    ValCell{1}=DataIn.W;
430    Role{1}='scalar';%will remain unchanged by projection
431    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
432        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
433    else
434        units={'pixel'};
435    end
436end
437
438%%%%%%%%%%%%% w_normal %%%%%%%%%%%%%%%%%%%%
439function [VarName,ValCell,Role,units]=w_normal(DataIn)
440VarName={};
441ValCell={};
442Role={};
443units={};
444if isfield(DataIn,'W')
445    VarName{1}='W';
446    ValCell{1}=DataIn.W;
447    Role{1}='vector_z';%will behave like a vector component  by projection
448    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
449        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
450    else
451        units={'pixel'};
452    end
453end
454
455%%%%%%%%%%%%% error %%%%%%%%%%%%%%%%%%%%
456function [VarName,ValCell,Role,units]=error(DataIn)
457VarName={};
458ValCell={};
459Role={};
460units={};
461if isfield(DataIn,'E')
462    VarName{1}='E';
463    ValCell{1}=DataIn.E;
464    Role{1}='ancillary'; %TODO CHECK units in actual fields
465    if isfield(DataIn,'CoordUnit') && isfield(DataIn,'TimeUnit')
466        units={[DataIn.CoordUnit '/' DataIn.TimeUnit]};
467    else
468        units={'pixel'};
469    end
470end
471
Note: See TracBrowser for help on using the repository browser.