source: trunk/src/read_civxdata.m @ 180

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

rationalisation of uvmat, introduction of the new function read_field, links with get_field, several bug repairs

File size: 10.6 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]=read_civxdata(filename,FieldNames,VelType)
50
51if ~exist('VelType','var')
52    VelType=[];
53end
54if isequal(VelType,'*')
55    VelType=[];
56end
57if ~exist('FieldNames','var')
58    FieldNames=[]; %default
59end
60VelTypeOut=VelType;%default
61[var,role,units,vel_type_out_cell]=varcivx_generator(FieldNames,VelType);%determine the names of constants and variables to read
62[Field,vardetect,ichoice]=nc2struct(filename,var);
63% if isfield(Field,'Txt')
64%     return % error in file reading
65% end
66if vardetect(1)==0
67     Field.Txt=[ 'requested field not available in ' filename '/' VelType];
68end
69var_ind=find(vardetect);
70for ivar=1:length(var_ind)
71    Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};
72%     Field.VarAttribute{ivar}.units=units{var_ind(ivar)};% not necessary: set with calc_field
73end
74VelTypeOut=VelType;
75if ~isempty(ichoice)
76    VelTypeOut=vel_type_out_cell{ichoice};
77end
78
79%adjust for Djui:
80if isfield(Field,'DjUi')
81    Field.ListVarName(end-2:end)=[];
82    Field.ListVarName{end}='DjUi';
83    Field.VarDimName(end-2:end)=[];
84    Field.VarAttribute(end-2:end)=[];
85end
86
87%renaiming for standard conventions
88Field.NbCoord=Field.nb_coord;
89Field.NbDim=Field.nb_dim;
90
91% CivStage
92if isfield(Field,'patch2')&& isequal(Field.patch2,1)
93    Field.CivStage=6;
94elseif isfield(Field,'fix2')&& isequal(Field.fix2,1)
95    Field.CivStage=5;
96elseif isfield(Field,'civ2')&& isequal(Field.civ2,1)
97    Field.CivStage=4;
98elseif isfield(Field,'patch')&& isequal(Field.patch,1)
99    Field.CivStage=3;
100elseif isfield(Field,'fix')&& isequal(Field.fix,1)
101    Field.CivStage=2;
102else
103    Field.CivStage=1;
104end
105
106%determine the appropriate constant for time and dt for the PIV pair
107test_civ1=isequal(VelTypeOut,'civ1')||isequal(VelTypeOut,'interp1')||isequal(VelTypeOut,'filter1');
108test_civ2=isequal(VelTypeOut,'civ2')||isequal(VelTypeOut,'interp2')||isequal(VelTypeOut,'filter2');
109Field.Time=0; %default
110if test_civ1
111    if isfield(Field,'absolut_time_T0')
112        Field.Time=double(Field.absolut_time_T0);
113        Field.dt=double(Field.dt);
114    else
115       Field.Txt='the input file is not civx';
116       Field.CivStage=0;
117       Field.dt=0;
118    end
119elseif test_civ2
120    Field.Time=double(Field.absolut_time_T0_2);
121    Field.dt=double(Field.dt2);
122else
123    Field.Txt='the input file is not civx';
124    Field.CivStage=0;
125    Field.dt=0;
126end
127
128
129
130%% rescale fields to pixel coordinates
131if isfield(Field,'pixcmx')
132    Field.pixcmx=double(Field.pixcmx);
133    Field.pixcmy=double(Field.pixcmy);
134    Field.U=Field.U*Field.pixcmx;
135    Field.V=Field.V*Field.pixcmy;
136    Field.X=Field.X*Field.pixcmx;
137    Field.Y=Field.Y*Field.pixcmy;
138end
139if ~isequal(Field.dt,0)
140    Field.U=Field.U*Field.dt;%translate in px displacement
141    Field.V=Field.V*Field.dt;
142    if isfield(Field,'DjUi')
143       Field.DjUi(:,1,1)=Field.dt*Field.DjUi(:,1,1);
144       Field.DjUi(:,2,2)=Field.dt*Field.DjUi(:,2,2);
145       Field.DjUi(:,1,2)=(Field.pixcmy/Field.pixcmx)*Field.dt*Field.DjUi(:,1,2);
146       Field.DjUi(:,2,1)=(Field.pixcmx/Field.pixcmy)*Field.dt*Field.DjUi(:,2,1);
147    end
148end
149
150%% update list of global attributes
151List=Field.ListGlobalAttribute;
152ind_remove=[];
153for ilist=1:length(List)
154    switch(List{ilist})
155        case {'patch2','fix2','civ2','patch','fix','dt2','absolut_time_T0','absolut_time_T0_2','nb_coord','nb_dim','pixcmx','pixcmy'}
156            ind_remove=[ind_remove ilist];
157            Field=rmfield(Field,List{ilist});
158    end
159end
160List(ind_remove)=[];
161Field.ListGlobalAttribute=[{'NbCoord'},{'NbDim'} List {'Time','CivStage','CoordUnit'}];
162Field.CoordUnit='pixel';
163
164
165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166% [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
167%INPUT:
168% FieldNames =cell of field names to get, which can contain the strings:
169%             'ima_cor': image correlation, vec_c or vec2_C
170%             'vort','div','strain': requires velocity derivatives DUDX...
171%             'error': error estimate (vec_E or vec2_E)
172%             
173% vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)
174%            if vel_type=[] or'*', a  priority choice, given by vel_type_out{1,2}, is done depending
175%            if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)
176
177function [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)
178
179%default input values
180if ~exist('vel_type','var'),vel_type=[];end;
181if iscell(vel_type),vel_type=vel_type{1}; end;%transform cell to string if needed
182% if ~exist('display','var'),display=[];end;
183if ~exist('FieldNames','var'),FieldNames={'ima_cor'};end;%default scalar
184if ischar(FieldNames), FieldNames={FieldNames}; end;
185
186%select the priority order for automatic vel_type selection
187testder=0;
188for ilist=1:length(FieldNames)
189    if ~isempty(FieldNames{ilist})
190    switch FieldNames{ilist}
191        case {'vort','div','strain'}
192            testder=1;
193    end
194    end
195end     
196if isempty(vel_type) || isequal(vel_type,'*') %undefined velocity type (civ1,civ2...)
197    if testder
198         vel_type_out{1}='filter2'; %priority to filter2 for scalar reading, filter1 as second
199        vel_type_out{2}='filter1';
200    else
201        vel_type_out{1}='civ2'; %priority to civ2 for vector reading, civ1 as second priority     
202        vel_type_out{2}='civ1';
203    end
204elseif isequal(vel_type,'filter')
205        vel_type_out{1}='filter2'; %priority to filter2 for scalar reading, filter1 as second
206        vel_type_out{2}='filter1';
207        if ~testder
208            vel_type_out{3}='civ1';%civ1 as third priority if derivatives are not needed
209        end
210elseif testder
211    test_civ1=isequal(vel_type,'civ1')||isequal(vel_type,'interp1')||isequal(vel_type,'filter1');
212    if test_civ1
213        vel_type_out{1}='filter1'; %switch to filter for reading spatial derivatives
214    else
215        vel_type_out{1}='filter2';
216    end
217else   
218    vel_type_out{1}=vel_type;%imposed velocity field
219end
220vel_type_out=vel_type_out';
221
222%determine names of netcdf variables to read
223var={'X','Y','Z','U','V','W','C','F','FF'};
224role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};
225units={'pixel','pixel','pixel','pixel','pixel','pixel',[],[],[]};
226if testder
227    var=[var {'DjUi(:,1,1)','DjUi(:,1,2)','DjUi(:,2,1)','DjUi(:,2,2)'}];
228    role=[role {'tensor','tensor','tensor','tensor'}];
229    units=[units {'pixel','pixel','pixel','pixel'}];
230end
231for ilist=1:length(vel_type_out)
232    var=[var;varname1(vel_type_out{ilist},FieldNames)];
233end
234
235 
236%-------------------------
237%determine  var names to read
238%--------------------------------------
239function varin=varname1(vel_type,FieldNames)
240
241testder=0;
242C1='';
243C2='';
244for ilist=1:length(FieldNames)
245    if ~isempty(FieldNames{ilist})
246    switch FieldNames{ilist}
247        case 'ima_cor' %image correlation corresponding to a vel vector
248            C1='vec_C';
249            C2='vec2_C';
250        case 'error'
251            C1='vec_E';
252            C2='vec2_E';
253        case {'vort','div','strain'}
254            testder=1;
255    end
256    end
257end     
258switch vel_type
259    case 'civ1'
260        varin={'vec_X','vec_Y','vec_Z','vec_U','vec_V','vec_W',C1,'vec_F','vec_FixFlag'};
261    case 'interp1'
262        varin={'vec_patch_X','vec_patch_Y','','vec_patch0_U','vec_patch0_V','','','',''};
263    case 'filter1'
264        varin={'vec_patch_X','vec_patch_Y','','vec_patch_U','vec_patch_V','','','',''};
265    case 'civ2'
266        varin={'vec2_X','vec2_Y','vec2_Z','vec2_U','vec2_V','vec2_W',C2,'vec2_F','vec2_FixFlag'};
267    case 'interp2'
268        varin={'vec2_patch_X','vec2_patch_Y','vec2_patch_Z','vec2_patch0_U','vec2_patch0_V','vec2_patch0_W','','',''};
269    case 'filter2'
270        varin={'vec2_patch_X','vec2_patch_Y','vec2_patch_Z','vec2_patch_U','vec2_patch_V','vec2_patch0_W','','',''};
271end
272if testder
273     switch vel_type
274        case 'filter1'
275            varin=[varin {'vec_patch_DUDX','vec_patch_DVDX','vec_patch_DUDY','vec_patch_DVDY'}];
276        case 'filter2'
277            varin=[varin {'vec2_patch_DUDX','vec2_patch_DVDX','vec2_patch_DUDY','vec2_patch_DVDY'}];
278    end   
279end
Note: See TracBrowser for help on using the repository browser.