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