[8]  1  %'read_civxdata': reads civx data from netcdf files


 2  %


 3  % OUTPUT:


 4  % nb_coord,nb_dim,


 5  % Civ: =0 or 1, indicates whether the data is civ (A SUPPRIMER ?)


 6  % CivStage: =0, ??? A UTILISER POUR REMPLACER civ


 7  % =1, civ1 has been performed only


 8  % =2, fix1 has been performed


 9  % =3, pacth1 has been performed


 10  % =4, civ2 has been performed


 11  % =5, fix2 has been performed


 12  % =6, pacth2 has been performed


 13  % time: absolute time


 14  % Field


 15  % .Txt: (char string) error message if any


 16  % .NbDim: number of dimensions (=0 by default)


 17  % .NbCoord: number of vector components


 18  % .CoordType: expresses the type of coordinate ('px' for image, 'sig' for instruments, or 'phys')


 19  % .dt: time interval for the corresponding image pair


[93]  20  % .CivStage: =0,


 21  % =1, civ1 has been performed only


 22  % =2, fix1 has been performed


 23  % =3, pacth1 has been performed


 24  % =4, civ2 has been performed


 25  % =5, fix2 has been performed


 26  % =6, pacth2 has been performed


[8]  27  % .X, .Y, .Z: set of vector coordinates


 28  % .U,.V,.W: corresponding set of vector components


 29  % .F: warning flags


 30  % .FF: error flag, =0 for good vectors


 31  % .C: scalar associated with velocity (used for vector colors)


 32  % .CoordType


 33  % .DijU; matrix of spatial derivatives (DijU(1,1,:)=DUDX,


 34  % DijU(1,2,:)=DUDY, Dij(2,1,:)=DVDX, DijU(2,2,:)=DVDY


 35  % .A, .AX, .AY: additional scalar


 36  % dt:time interval of the image pair red from a single file, or vector with


 37  % pixcmx,pixcmy: scaling factors (from the first file)


 38  % vel_type_out: string representing the selected velocity type (civ1,civ2,filter1...)


 39  %


 40  % INPUT:


 41  % filename: file name (string).


 42  % FieldNames =cell of field names to get, which can contain the strings:


 43  % 'ima_cor': image correlation, vec_c or vec2_C


 44  % 'vort','div','strain': requires velocity derivatives DUDX...


 45  % 'error': error estimate (vec_E or vec2_E)


 46  %


 47  % VelType : character string indicating the types of velocity fields to read ('civ1','civ2'...)


 48  % if vel_type=[] or'*', a priority choice, given by vel_type_out{1,2}, is done depending


 49  % if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)


 50 


 51 


 52  % FUNCTIONS called:


 53  % 'varcivx_generator':, sets the names of vaiables to read in the netcdf file


 54  % 'nc2struct': reads a netcdf file


 55 


 56  function [Field,VelTypeOut]=read_civxdata(filename,FieldNames,VelType)


 57 


 58  if ~exist('VelType','var')


 59  VelType=[];


 60  end


 61  if isequal(VelType,'*')


 62  VelType=[];


 63  end


 64  if ~exist('FieldNames','var')


 65  FieldNames=[]; %default


 66  end


 67  VelTypeOut=VelType;%default


 68  [var,role,units,vel_type_out_cell]=varcivx_generator(FieldNames,VelType);%determine the names of constants and variables to read


 69  [Field,vardetect,ichoice]=nc2struct(filename,var);


 70  if isfield(Field,'Txt')


 71  return % error in file reading


 72  end


 73  if isequal(vardetect,0)


[93]  74  Field.Txt=[reshape(FieldNames,1,numel(FieldNames)) ' not accessible in ' filename '/' VelType];


[8]  75  return


 76  end


 77  var_ind=find(vardetect);


 78  for ivar=1:length(var_ind)


 79  Field.VarAttribute{ivar}.Role=role{var_ind(ivar)};


 80  % Field.VarAttribute{ivar}.units=units{var_ind(ivar)};% not necessary: set with calc_field


 81  end


 82  VelTypeOut=VelType;


 83  if ~isempty(ichoice)


 84  VelTypeOut=vel_type_out_cell{ichoice};


 85  end


 86 


 87  %adjust for Djui:


 88  if isfield(Field,'DjUi')


 89  Field.ListVarName(end2:end)=[];


 90  Field.ListVarName{end}='DjUi';


 91  Field.VarDimIndex(end2:end)=[];


 92  Field.VarAttribute(end2:end)=[];


 93  end


 94 


 95  %determine the appropriate constant for time and dt for the PIV pair


 96  test_civ1=isequal(VelTypeOut,'civ1')isequal(VelTypeOut,'interp1')isequal(VelTypeOut,'filter1');


 97  test_civ2=isequal(VelTypeOut,'civ2')isequal(VelTypeOut,'interp2')isequal(VelTypeOut,'filter2');


 98  if test_civ1


[127]  99  Field.Time=double(Field.absolut_time_T0);


 100  Field.dt=double(Field.dt);


[8]  101  elseif test_civ2


[127]  102  Field.Time=double(Field.absolut_time_T0_2);


 103  Field.dt=double(Field.dt2);


[8]  104  else


 105  Field.Txt='the input file is not civx';


 106  display(Field.Txt)


 107  end


 108 


 109  % CivStage


 110  if isfield(Field,'patch2')&& isequal(Field.patch2,1)


 111  Field.CivStage=6;


 112  elseif isfield(Field,'fix2')&& isequal(Field.fix2,1)


 113  Field.CivStage=5;


 114  elseif isfield(Field,'civ2')&& isequal(Field.civ2,1)


 115  Field.CivStage=4;


 116  elseif isfield(Field,'patch')&& isequal(Field.patch,1)


 117  Field.CivStage=3;


 118  elseif isfield(Field,'fix')&& isequal(Field.fix,1)


 119  Field.CivStage=2;


 120  else


 121  Field.CivStage=1;


 122  end


 123 


 124  %update list of global attributes


 125  List=Field.ListGlobalAttribute;


 126  ind_remove=[];


 127  for ilist=1:length(List)


 128  switch(List{ilist})


 129  case {'patch2','fix2','civ2','patch','fix','dt2','absolut_time_T0','absolut_time_T0_2'}


 130  ind_remove=[ind_remove ilist];


 131  Field=rmfield(Field,List{ilist});


 132  end


 133  end


 134  List(ind_remove)=[];


 135  Field.ListGlobalAttribute=[List {'Time','CivStage','CoordUnit'}];


 136 


 137  % rescale to pixel coordiantes


 138  if isfield(Field,'pixcmx')


[127]  139  Field.pixcmx=double(Field.pixcmx);


 140  Field.pixcmy=double(Field.pixcmy);


[8]  141  Field.U=Field.U*Field.pixcmx;


 142  Field.V=Field.V*Field.pixcmy;


 143  Field.X=Field.X*Field.pixcmx;


 144  Field.Y=Field.Y*Field.pixcmy;


 145  end


 146  if ~isequal(Field.dt,0)


 147  Field.U=Field.U*Field.dt;%translate in px displacement


 148  Field.V=Field.V*Field.dt;


 149  if isfield(Field,'DjUi')


 150  Field.DjUi(:,1,1)=Field.dt*Field.DjUi(:,1,1);


 151  Field.DjUi(:,2,2)=Field.dt*Field.DjUi(:,2,2);


 152  Field.DjUi(:,1,2)=(Field.pixcmy/Field.pixcmx)*Field.dt*Field.DjUi(:,1,2);


 153  Field.DjUi(:,2,1)=(Field.pixcmx/Field.pixcmy)*Field.dt*Field.DjUi(:,2,1);


 154  end


 155  end


 156  Field.CoordType='px';% TODO: abandon, use COORdUnit instead ? (to adapt 'px' and 'phys')


 157  Field.CoordUnit='pixel';


 158 


 159 


 160  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


 161  % [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)


 162  %INPUT:


 163  % FieldNames =cell of field names to get, which can contain the strings:


 164  % 'ima_cor': image correlation, vec_c or vec2_C


 165  % 'vort','div','strain': requires velocity derivatives DUDX...


 166  % 'error': error estimate (vec_E or vec2_E)


 167  %


 168  % vel_type: character string indicating the types of velocity fields to read ('civ1','civ2'...)


 169  % if vel_type=[] or'*', a priority choice, given by vel_type_out{1,2}, is done depending


 170  % if vel_type='filter'; a structured field is sought (filter2 in priority, then filter1)


 171 


 172  function [var,role,units,vel_type_out]=varcivx_generator(FieldNames,vel_type)


 173 


 174  %default input values


 175  if ~exist('vel_type','var'),vel_type=[];end;


 176  if iscell(vel_type),vel_type=vel_type{1}; end;%transform cell to string if needed


 177  % if ~exist('display','var'),display=[];end;


 178  if ~exist('FieldNames','var'),FieldNames={'ima_cor'};end;%default scalar


 179  if ischar(FieldNames), FieldNames={FieldNames}; end;


 180 


 181  %select the priority order for automatic vel_type selection


 182  testder=0;


 183  for ilist=1:length(FieldNames)


 184  if ~isempty(FieldNames{ilist})


 185  switch FieldNames{ilist}


 186  case {'vort','div','strain'}


 187  testder=1;


 188  end


 189  end


 190  end


 191  if isempty(vel_type)  isequal(vel_type,'*') %undefined velocity type (civ1,civ2...)


 192  if testder


 193  vel_type_out{1}='filter2'; %priority to filter2 for scalar reading, filter1 as second


 194  vel_type_out{2}='filter1';


 195  else


 196  vel_type_out{1}='civ2'; %priority to civ2 for vector reading, civ1 as second priority


 197  vel_type_out{2}='civ1';


 198  end


 199  elseif isequal(vel_type,'filter')


 200  vel_type_out{1}='filter2'; %priority to filter2 for scalar reading, filter1 as second


 201  vel_type_out{2}='filter1';


 202  if ~testder


 203  vel_type_out{3}='civ1';%civ1 as third priority if derivatives are not needed


 204  end


 205  elseif testder


 206  test_civ1=isequal(vel_type,'civ1')isequal(vel_type,'interp1')isequal(vel_type,'filter1');


 207  if test_civ1


 208  vel_type_out{1}='filter1'; %switch to filter for reading spatial derivatives


 209  else


 210  vel_type_out{1}='filter2';


 211  end


 212  else


 213  vel_type_out{1}=vel_type;%imposed velocity field


 214  end


 215  vel_type_out=vel_type_out';


 216 


 217  %determine names of netcdf variables to read


 218  var={'X','Y','Z','U','V','W','C','F','FF'};


 219  role={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','ancillary','warnflag','errorflag'};


 220  units={'pixel','pixel','pixel','pixel','pixel','pixel',[],[],[]};


 221  if testder


 222  var=[var {'DjUi(:,1,1)','DjUi(:,1,2)','DjUi(:,2,1)','DjUi(:,2,2)'}];


 223  role=[role {'tensor','tensor','tensor','tensor'}];


 224  units=[units {'pixel','pixel','pixel','pixel'}];


 225  end


 226  for ilist=1:length(vel_type_out)


 227  var=[var;varname1(vel_type_out{ilist},FieldNames)];


 228  end


 229 


 230 


 231  %


 232  %determine var names to read


 233  %


 234  function varin=varname1(vel_type,FieldNames)


 235 


 236  testder=0;


 237  C1='';


 238  C2='';


 239  for ilist=1:length(FieldNames)


 240  if ~isempty(FieldNames{ilist})


 241  switch FieldNames{ilist}


 242  case 'ima_cor' %image correlation corresponding to a vel vector


 243  C1='vec_C';


 244  C2='vec2_C';


 245  case 'error'


 246  C1='vec_E';


 247  C2='vec2_E';


 248  case {'vort','div','strain'}


 249  testder=1;


 250  end


 251  end


 252  end


 253  switch vel_type


 254  case 'civ1'


 255  varin={'vec_X','vec_Y','vec_Z','vec_U','vec_V','vec_W',C1,'vec_F','vec_FixFlag'};


 256  case 'interp1'


 257  varin={'vec_patch_X','vec_patch_Y','','vec_patch0_U','vec_patch0_V','','','',''};


 258  case 'filter1'


 259  varin={'vec_patch_X','vec_patch_Y','','vec_patch_U','vec_patch_V','','','',''};


 260  case 'civ2'


 261  varin={'vec2_X','vec2_Y','vec2_Z','vec2_U','vec2_V','vec2_W',C2,'vec2_F','vec2_FixFlag'};


 262  case 'interp2'


 263  varin={'vec2_patch_X','vec2_patch_Y','vec2_patch_Z','vec2_patch0_U','vec2_patch0_V','vec2_patch0_W','','',''};


 264  case 'filter2'


 265  varin={'vec2_patch_X','vec2_patch_Y','vec2_patch_Z','vec2_patch_U','vec2_patch_V','vec2_patch0_W','','',''};


 266  end


 267  if testder


 268  switch vel_type


 269  case 'filter1'


 270  varin=[varin {'vec_patch_DUDX','vec_patch_DVDX','vec_patch_DUDY','vec_patch_DVDY'}];


 271  case 'filter2'


 272  varin=[varin {'vec2_patch_DUDX','vec2_patch_DVDX','vec2_patch_DUDY','vec2_patch_DVDY'}];


 273  end


 274  end 
