source: trunk/src/read_civxdata.m @ 719

Last change on this file since 719 was 581, checked in by sommeria, 12 years ago

clean the transform field functions

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