source: trunk/src/read_civxdata.m @ 159

Last change on this file since 159 was 159, checked in by sommeria, 10 years ago

bug in civ corrected: civ2 was not lauched for Windows system
various bugs corrections and cleaning

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