source: trunk/src/read_civxdata.m @ 136

Last change on this file since 136 was 127, checked in by sommeria, 14 years ago

merge_proj.m: cleaning
sub_field.m: ?
dataview.m: modifs not finished
name-generator: bug repair for new nom_type
nc2struct: cleaning
uvmat: cleaning
read_civxdata: transform all variables to double
civ: bug repair for new nom_type, not finished

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