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