source: trunk/src/read_civxdata.m @ 528

Last change on this file since 528 was 527, checked in by sommeria, 12 years ago

various bugs corrected

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