source: trunk/src/read_civxdata.m @ 257

Last change on this file since 257 was 247, checked in by sommeria, 14 years ago

thin plate shell corrected for spatial derivatives

File size: 11.0 KB
Line 
1%'read_civxdata': reads civx data from netcdf files
2%------------------------------------------------------------------
3%
4% function [Field,VelTypeOut]=read_civxdata(filename,FieldNames,VelType)
5%
6% OUTPUT:
7% Field: structure representing the selected field, containing
8%            .Txt: (char string) error message if any
9%            .ListGlobalAttribute: list of global attributes containing:
10%                    .NbCoord: number of vector components
11%                    .NbDim: number of dimensions (=2 or 3)
12%                    .dt: time interval for the corresponding image pair
13%                    .Time: absolute time (average of the initial image pair)
14%                    .CivStage: =0,
15%                       =1, civ1 has been performed only
16%                       =2, fix1 has been performed
17%                       =3, pacth1 has been performed
18%                       =4, civ2 has been performed
19%                       =5, fix2 has been performed
20%                       =6, pacth2 has been performed
21%                     .CoordUnit: 'pixel'
22%             .ListVarName: {'X'  'Y'  'U'  'V'  'F'  'FF'}
23%                   .X, .Y, .Z: set of vector coordinates
24%                    .U,.V,.W: corresponding set of vector components
25%                    .F: warning flags
26%                    .FF: error flag, =0 for good vectors
27%                     .C: scalar associated with velocity (used for vector colors)
28%                    .DijU; matrix of spatial derivatives (DijU(1,1,:)=DUDX,
29%                        DijU(1,2,:)=DUDY, Dij(2,1,:)=DVDX, DijU(2,2,:)=DVDY
30%
31% VelTypeOut: velocity type corresponding to the selected field: ='civ1','interp1','interp2','civ2'....
32%
33% INPUT:
34% filename: file name (string).
35% FieldNames =cell of field names to get, which can contain the strings:
36%             'ima_cor': image correlation, vec_c or vec2_C
37%             'vort','div','strain': requires velocity derivatives DUDX...
38%             'error': error estimate (vec_E or vec2_E)
39%             
40% VelType : character string indicating the types of velocity fields to read ('civ1','civ2'...)
41%            if vel_type=[] or'*', a  priority choice, given by vel_type_out{1,2}, is done depending
42%            if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)
43
44
45% FUNCTIONS called:
46% 'varcivx_generator':, sets the names of vaiables to read in the netcdf file
47% 'nc2struct': reads a netcdf file
48
49function [Field,VelTypeOut,errormsg]=read_civxdata(filename,FieldNames,VelType)
50errormsg='';
51DataTest=nc2struct(filename,'ListGlobalAttribute','Conventions');
52if isfield(DataTest,'Txt')
53    errormsg=DataTest.Txt;   
54elseif isequal(DataTest.Conventions,'uvmat/civdata')%test for new civ format
55     [Field,VelTypeOut]=read_civdata(filename,FieldNames,VelType);
56     return
57end
58   
59%% default input
60if ~exist('VelType','var')
61    VelType=[];
62end
63if isequal(VelType,'*')
64    VelType=[];
65end
66if ~exist('FieldNames','var')
67    FieldNames=[]; %default
68end
69
70%% reading data
71VelTypeOut=VelType;%default
72[var,role,units,vel_type_out_cell]=varcivx_generator(FieldNames,VelType);%determine the names of constants and variables to read
73[Field,vardetect,ichoice]=nc2struct(filename,var);%read the variables in the netcdf file
74if isfield(Field,'Txt')
75    errormsg=Field.Txt;
76    return
77end
78if vardetect(1)==0
79     errormsg=[ 'requested field not available in ' filename '/' VelType];
80     return
81end
82var_ind=find(vardetect);
83for ivar=1:length(var_ind)
84    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
85    Field.VarAttribute{ivar}.Mesh=0.1;%typical mesh for histograms O.1 pixel
86end
87VelTypeOut=VelType;
88if ~isempty(ichoice)
89    VelTypeOut=vel_type_out_cell{ichoice};
90end
91
92%% adjust for Djui:
93if isfield(Field,'DjUi')
94    Field.ListVarName(end-2:end)=[];
95    Field.ListVarName{end}='DjUi';
96    Field.VarDimName(end-2:end)=[];
97    Field.VarAttribute(end-2:end)=[];
98end
99
100%% renaming for standard conventions
101Field.NbCoord=Field.nb_coord;
102Field.NbDim=Field.nb_dim;
103
104%% CivStage
105if isfield(Field,'patch2')&& isequal(Field.patch2,1)
106    Field.CivStage=6;
107elseif isfield(Field,'fix2')&& isequal(Field.fix2,1)
108    Field.CivStage=5;
109elseif isfield(Field,'civ2')&& isequal(Field.civ2,1)
110    Field.CivStage=4;
111elseif isfield(Field,'patch')&& isequal(Field.patch,1)
112    Field.CivStage=3;
113elseif isfield(Field,'fix')&& isequal(Field.fix,1)
114    Field.CivStage=2;
115else
116    Field.CivStage=1;
117end
118
119%determine the appropriate constant for time and dt for the PIV pair
120test_civ1=isequal(VelTypeOut,'civ1')||isequal(VelTypeOut,'interp1')||isequal(VelTypeOut,'filter1');
121test_civ2=isequal(VelTypeOut,'civ2')||isequal(VelTypeOut,'interp2')||isequal(VelTypeOut,'filter2');
122Field.Time=0; %default
123Field.TimeUnit='s';
124if test_civ1
125    if isfield(Field,'absolut_time_T0')
126        Field.Time=double(Field.absolut_time_T0);
127        Field.dt=double(Field.dt);
128    else
129       errormsg='the input file is not civx';
130       Field.CivStage=0;
131       Field.dt=0;
132    end
133elseif test_civ2
134    Field.Time=double(Field.absolut_time_T0_2);
135    Field.dt=double(Field.dt2);
136else
137    errormsg='the input file is not civx';
138    Field.CivStage=0;
139    Field.dt=0;
140end
141
142
143
144%% rescale fields to pixel coordinates
145if isfield(Field,'pixcmx')
146    Field.pixcmx=double(Field.pixcmx);
147    Field.pixcmy=double(Field.pixcmy);
148    Field.U=Field.U*Field.pixcmx;
149    Field.V=Field.V*Field.pixcmy;
150    Field.X=Field.X*Field.pixcmx;
151    Field.Y=Field.Y*Field.pixcmy;
152end
153if ~isequal(Field.dt,0)
154    Field.U=Field.U*Field.dt;%translate in px displacement
155    Field.V=Field.V*Field.dt;
156    if isfield(Field,'DjUi')
157       Field.DjUi(:,1,1)=Field.dt*Field.DjUi(:,1,1);
158       Field.DjUi(:,2,2)=Field.dt*Field.DjUi(:,2,2);
159       Field.DjUi(:,1,2)=(Field.pixcmy/Field.pixcmx)*Field.dt*Field.DjUi(:,1,2);
160       Field.DjUi(:,2,1)=(Field.pixcmx/Field.pixcmy)*Field.dt*Field.DjUi(:,2,1);
161    end
162end
163
164%% update list of global attributes
165List=Field.ListGlobalAttribute;
166ind_remove=[];
167for ilist=1:length(List)
168    switch(List{ilist})
169        case {'patch2','fix2','civ2','patch','fix','dt2','absolut_time_T0','absolut_time_T0_2','nb_coord','nb_dim','pixcmx','pixcmy'}
170            ind_remove=[ind_remove ilist];
171            Field=rmfield(Field,List{ilist});
172    end
173end
174List(ind_remove)=[];
175Field.ListGlobalAttribute=[{'NbCoord'},{'NbDim'} List {'Time','TimeUnit','CivStage','CoordUnit'}];
176Field.CoordUnit='pixel';
177
178
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
181%INPUT:
182% FieldNames =cell of field names to get, which can contain the strings:
183%             'ima_cor': image correlation, vec_c or vec2_C
184%             'vort','div','strain': requires velocity derivatives DUDX...
185%             'error': error estimate (vec_E or vec2_E)
186%             
187% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
188%            if vel_type=[] or'*', a  priority choice, given by vel_type_out{1,2}, is done depending
189%            if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)
190
191function [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
192
193%% default input values
194if ~exist('vel_type','var'),vel_type=[];end;
195if iscell(vel_type),vel_type=vel_type{1}; end;%transform cell to string if needed
196if ~exist('FieldNames','var'),FieldNames={'ima_cor'};end;%default scalar
197if ischar(FieldNames), FieldNames={FieldNames}; end;
198
199%% select the priority order for automatic vel_type selection
200testder=0;
201for ilist=1:length(FieldNames)
202    if ~isempty(FieldNames{ilist})
203    switch FieldNames{ilist}
204        case {'vort','div','strain'}
205            testder=1;
206    end
207    end
208end     
209if isempty(vel_type) || isequal(vel_type,'*') %undefined velocity type (civ1,civ2...)
210    if testder
211         vel_type_out{1}='filter2'; %priority to filter2 for scalar reading, filter1 as second
212        vel_type_out{2}='filter1';
213    else
214        vel_type_out{1}='civ2'; %priority to civ2 for vector reading, civ1 as second priority     
215        vel_type_out{2}='civ1';
216    end
217elseif isequal(vel_type,'filter')
218        vel_type_out{1}='filter2'; %priority to filter2 for scalar reading, filter1 as second
219        vel_type_out{2}='filter1';
220        if ~testder
221            vel_type_out{3}='civ1';%civ1 as third priority if derivatives are not needed
222        end
223elseif testder
224    test_civ1=isequal(vel_type,'civ1')||isequal(vel_type,'interp1')||isequal(vel_type,'filter1');
225    if test_civ1
226        vel_type_out{1}='filter1'; %switch to filter for reading spatial derivatives
227    else
228        vel_type_out{1}='filter2';
229    end
230else   
231    vel_type_out{1}=vel_type;%imposed velocity field
232end
233vel_type_out=vel_type_out';
234
235%% determine names of netcdf variables to read
236var={'X','Y','Z','U','V','W','C','F','FF'};
237role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};
238units={'pixel','pixel','pixel','pixel','pixel','pixel',[],[],[]};
239if testder
240    var=[var {'DjUi(:,1,1)','DjUi(:,1,2)','DjUi(:,2,1)','DjUi(:,2,2)'}];
241    role=[role {'tensor','tensor','tensor','tensor'}];
242    units=[units {'pixel','pixel','pixel','pixel'}];
243end
244for ilist=1:length(vel_type_out)
245    var=[var;varname1(vel_type_out{ilist},FieldNames)];
246end
247
248%------------------------------------------------------------------------ 
249%--- determine  var names to read
250function varin=varname1(vel_type,FieldNames)
251%------------------------------------------------------------------------
252testder=0;
253C1='';
254C2='';
255for ilist=1:length(FieldNames)
256    if ~isempty(FieldNames{ilist})
257    switch FieldNames{ilist}
258        case 'ima_cor' %image correlation corresponding to a vel vector
259            C1='vec_C';
260            C2='vec2_C';
261        case 'error'
262            C1='vec_E';
263            C2='vec2_E';
264        case {'vort','div','strain'}
265            testder=1;
266    end
267    end
268end     
269switch vel_type
270    case 'civ1'
271        varin={'vec_X','vec_Y','vec_Z','vec_U','vec_V','vec_W',C1,'vec_F','vec_FixFlag'};
272    case 'interp1'
273        varin={'vec_patch_X','vec_patch_Y','','vec_patch0_U','vec_patch0_V','','','',''};
274    case 'filter1'
275        varin={'vec_patch_X','vec_patch_Y','','vec_patch_U','vec_patch_V','','','',''};
276    case 'civ2'
277        varin={'vec2_X','vec2_Y','vec2_Z','vec2_U','vec2_V','vec2_W',C2,'vec2_F','vec2_FixFlag'};
278    case 'interp2'
279        varin={'vec2_patch_X','vec2_patch_Y','vec2_patch_Z','vec2_patch0_U','vec2_patch0_V','vec2_patch0_W','','',''};
280    case 'filter2'
281        varin={'vec2_patch_X','vec2_patch_Y','vec2_patch_Z','vec2_patch_U','vec2_patch_V','vec2_patch0_W','','',''};
282end
283if testder
284     switch vel_type
285        case 'filter1'
286            varin=[varin {'vec_patch_DUDX','vec_patch_DVDX','vec_patch_DUDY','vec_patch_DVDY'}];
287        case 'filter2'
288            varin=[varin {'vec2_patch_DUDX','vec2_patch_DVDX','vec2_patch_DUDY','vec2_patch_DVDY'}];
289    end   
290end
Note: See TracBrowser for help on using the repository browser.