source: trunk/src/read_civxdata.m @ 19

Last change on this file since 19 was 19, checked in by gostiaux, 11 years ago

the private files have been moved down to the root folder

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