source: trunk/src/calc_field.m @ 546

Last change on this file since 546 was 546, checked in by sommeria, 8 years ago

bugs corrected for tps projection on a grid + pb of colorbar display solved

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