[497] | 1 | %'read_field': read the fields from files in different formats (netcdf files, images, video) |
---|
[181] | 2 | %-------------------------------------------------------------------------- |
---|
[450] | 3 | % function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num) |
---|
[181] | 4 | % |
---|
| 5 | % OUTPUT: |
---|
| 6 | % Field: matlab structure representing the field |
---|
| 7 | % ParamOut: structure representing parameters: |
---|
| 8 | % .FieldName; field name |
---|
| 9 | % .VelType |
---|
[1045] | 10 | % .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2 (fix1).... |
---|
[181] | 11 | % .Npx,.Npy: for images, nbre of pixels in x and y |
---|
| 12 | % errormsg: error message, ='' by default |
---|
| 13 | % |
---|
| 14 | %INPUT |
---|
[466] | 15 | % FileName: name of the input file |
---|
[784] | 16 | % FileType: type of file, as determined by the function get_file_info.m |
---|
[450] | 17 | % ParamIn: movie object or Matlab structure of input parameters |
---|
[445] | 18 | % .FieldName: name (char string) of the input field (for Civx data) |
---|
| 19 | % .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...) |
---|
[181] | 20 | % .ColorVar: variable used for vector color |
---|
| 21 | % .Npx, .Npy: nbre of pixels along x and y (used for .vol input files) |
---|
[1045] | 22 | % .TimeDimName: name of the dimension considered as 'time', selected index value then set by input 'num' |
---|
[466] | 23 | % num: frame number for movies |
---|
[497] | 24 | % |
---|
| 25 | % see also read_image.m,read_civxdata.m,read_civdata.m, |
---|
[466] | 26 | |
---|
[809] | 27 | %======================================================================= |
---|
[1107] | 28 | % Copyright 2008-2022, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[809] | 29 | % http://www.legi.grenoble-inp.fr |
---|
| 30 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
| 31 | % |
---|
| 32 | % This file is part of the toolbox UVMAT. |
---|
| 33 | % |
---|
| 34 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 35 | % it under the terms of the GNU General Public License as published |
---|
| 36 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 37 | % or (at your option) any later version. |
---|
| 38 | % |
---|
| 39 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 40 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 41 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 42 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 43 | %======================================================================= |
---|
| 44 | |
---|
[450] | 45 | function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num) |
---|
[748] | 46 | %% default output and check input |
---|
[181] | 47 | Field=[]; |
---|
[354] | 48 | if ~exist('num','var') |
---|
| 49 | num=1; |
---|
| 50 | end |
---|
[1033] | 51 | if isempty(num) |
---|
| 52 | num=1; |
---|
| 53 | end |
---|
[354] | 54 | if ~exist('ParamIn','var') |
---|
| 55 | ParamIn=[]; |
---|
| 56 | end |
---|
| 57 | ParamOut=ParamIn;%default |
---|
[181] | 58 | errormsg=''; |
---|
[1022] | 59 | if isempty(regexp(FileName,'^http://'))&& ~exist(FileName,'file') |
---|
[747] | 60 | errormsg=['input file ' FileName ' does not exist']; |
---|
[635] | 61 | return |
---|
| 62 | end |
---|
[334] | 63 | A=[]; |
---|
[575] | 64 | InputField={}; |
---|
| 65 | check_colorvar=0; |
---|
[526] | 66 | if isstruct(ParamIn) |
---|
[575] | 67 | if isfield(ParamIn,'FieldName') |
---|
| 68 | if ischar(ParamIn.FieldName) |
---|
[576] | 69 | InputField={ParamIn.FieldName}; |
---|
[575] | 70 | else |
---|
| 71 | InputField= ParamIn.FieldName; |
---|
| 72 | end |
---|
[526] | 73 | end |
---|
[654] | 74 | check_colorvar=zeros(size(InputField)); |
---|
[684] | 75 | if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar) |
---|
[526] | 76 | InputField=[ParamIn.FieldName {ParamIn.ColorVar}]; |
---|
[654] | 77 | check_colorvar(numel(InputField))=1; |
---|
[526] | 78 | end |
---|
[521] | 79 | end |
---|
[575] | 80 | |
---|
[334] | 81 | %% distingush different input file types |
---|
[527] | 82 | switch FileType |
---|
[748] | 83 | case 'civdata'% new format for civ results |
---|
[527] | 84 | [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType); |
---|
[907] | 85 | if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end |
---|
[527] | 86 | ParamOut.CivStage=Field.CivStage; |
---|
[958] | 87 | case 'pivdata_fluidimage' |
---|
| 88 | [Field,ParamOut.VelType,errormsg]=read_pivdata_fluidimage(FileName,InputField,ParamIn.VelType); |
---|
| 89 | ParamOut.CivStage=Field.CivStage; |
---|
[748] | 90 | case 'civx'% old (obsolete) format for civ results |
---|
[527] | 91 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
| 92 | [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
| 93 | if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end |
---|
| 94 | ParamOut.CivStage=Field.CivStage; |
---|
[1095] | 95 | case {'netcdf','mat'}% general netcdf file (not recognized as civ) |
---|
[1045] | 96 | ListVarName={}; |
---|
[675] | 97 | Role={}; |
---|
| 98 | ProjModeRequest={}; |
---|
[1041] | 99 | % scan the list InputField |
---|
[1045] | 100 | Operator=cell(1,numel(InputField)); |
---|
| 101 | InputVar=cell(1,numel(InputField)); |
---|
[527] | 102 | for ilist=1:numel(InputField) |
---|
[748] | 103 | % look for input variables to read |
---|
[527] | 104 | r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
[654] | 105 | if isempty(r)% no operator used |
---|
[1045] | 106 | ListVarName=[ListVarName InputField(ilist)];%append the variable name |
---|
| 107 | if check_colorvar(ilist)% case of field used for vector color |
---|
| 108 | Role{numel(ListVarName)}='ancillary';% not projected with interpolation |
---|
| 109 | ProjModeRequest{numel(ListVarName)}=''; |
---|
| 110 | else |
---|
| 111 | Role{numel(ListVarName)}='scalar'; |
---|
| 112 | ProjModeRequest{numel(ListVarName)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
[681] | 113 | end |
---|
[1045] | 114 | Operator{numel(ListVarName)}=''; |
---|
| 115 | else % an operator 'vec' or 'norm' is used |
---|
| 116 | ListVarName=[ListVarName {r.UName}]; % append the variable in the list if not previously listed |
---|
| 117 | if strcmp(r.Operator,'norm') |
---|
| 118 | if check_colorvar(ilist) |
---|
| 119 | Role=[Role {'ancillary'}]; |
---|
| 120 | else |
---|
| 121 | Role=[Role {'scalar'}]; |
---|
[907] | 122 | end |
---|
[654] | 123 | else |
---|
[1045] | 124 | Role=[Role {'vector_x'}]; |
---|
[654] | 125 | end |
---|
[1045] | 126 | ListVarName=[ListVarName {r.VName}];% append the variable in the list if not previously listed |
---|
| 127 | Role=[Role {'vector_y'}]; |
---|
| 128 | Operator{numel(ListVarName)-1}=r.Operator; |
---|
| 129 | Operator{numel(ListVarName)}=''; |
---|
[667] | 130 | if ~check_colorvar(ilist) && strcmp(r.Operator,'norm') |
---|
[1045] | 131 | ProjModeRequest{numel(ListVarName)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
| 132 | ProjModeRequest{numel(ListVarName)-1}='interp_lin';%scalar field (requires interpolation for plot) |
---|
[667] | 133 | else |
---|
[1045] | 134 | ProjModeRequest{numel(ListVarName)}=''; |
---|
| 135 | ProjModeRequest{numel(ListVarName)-1}=''; |
---|
[667] | 136 | end |
---|
[493] | 137 | end |
---|
[527] | 138 | end |
---|
[748] | 139 | if ~isfield(ParamIn,'Coord_z') |
---|
| 140 | ParamIn.Coord_z=[]; |
---|
| 141 | end |
---|
| 142 | NbCoord=~isempty(ParamIn.Coord_x)+~isempty(ParamIn.Coord_y)+~isempty(ParamIn.Coord_z); |
---|
[648] | 143 | if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array |
---|
[1045] | 144 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]); |
---|
[747] | 145 | elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time in a multidimensional array |
---|
[1045] | 146 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeVarName',ParamIn.TimeVarName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]); |
---|
[907] | 147 | if numel(num)~=1 |
---|
[1001] | 148 | NbCoord=NbCoord+1;% adds time coordinate, except if a single time has been selected |
---|
[907] | 149 | end |
---|
[648] | 150 | else |
---|
[1045] | 151 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVarName]); |
---|
[648] | 152 | end |
---|
[747] | 153 | if ~isempty(errormsg) |
---|
[534] | 154 | return |
---|
| 155 | end |
---|
[1001] | 156 | CheckStructured=1; |
---|
[1045] | 157 | %scan all the variables |
---|
| 158 | NbCoord=0; |
---|
| 159 | if ~isempty(ParamIn.Coord_x) |
---|
| 160 | index_Coord_x=find(strcmp(ParamIn.Coord_x,Field.ListVarName)); |
---|
| 161 | Field.VarAttribute{index_Coord_x}.Role='coord_x';% |
---|
| 162 | NbCoord=NbCoord+1; |
---|
[675] | 163 | end |
---|
[1045] | 164 | if ~isempty(ParamIn.Coord_y) |
---|
| 165 | if ischar(ParamIn.Coord_y) |
---|
| 166 | index_Coord_y=find(strcmp(ParamIn.Coord_y,Field.ListVarName)); |
---|
| 167 | Field.VarAttribute{index_Coord_y}.Role='coord_y';% |
---|
| 168 | NbCoord=NbCoord+1; |
---|
| 169 | else |
---|
| 170 | for icoord_y=1:numel(ParamIn.Coord_y) |
---|
| 171 | index_Coord_y=find(strcmp(ParamIn.Coord_y{icoord_y},Field.ListVarName)); |
---|
| 172 | Field.VarAttribute{index_Coord_y}.Role='coord_y';% |
---|
| 173 | NbCoord=NbCoord+1; |
---|
[1001] | 174 | end |
---|
| 175 | end |
---|
[1002] | 176 | end |
---|
[1045] | 177 | NbDim=1; |
---|
| 178 | if ~isempty(ParamIn.Coord_z) |
---|
| 179 | index_Coord_z=find(strcmp(ParamIn.Coord_z,Field.ListVarName)); |
---|
| 180 | Field.VarAttribute{index_Coord_z}.Role='coord_z';% |
---|
| 181 | NbCoord=NbCoord+1; |
---|
| 182 | NbDim=3; |
---|
| 183 | elseif ~isempty(ParamIn.FieldName) |
---|
| 184 | NbDim=2; |
---|
| 185 | end |
---|
[684] | 186 | NormName=''; |
---|
| 187 | UName=''; |
---|
| 188 | VName=''; |
---|
[1045] | 189 | if numel(Field.ListVarName)>NbCoord % if there are variables beyond coord (exclude 1 D plots) |
---|
| 190 | VarAttribute=cell(1,numel(ListVarName)); |
---|
| 191 | for ilist=1:numel(ListVarName) |
---|
| 192 | index_var=find(strcmp(ListVarName{ilist},Field.ListVarName),1); |
---|
| 193 | VarDimName{ilist}=Field.VarDimName{index_var}; |
---|
| 194 | DimOrder=[]; |
---|
| 195 | if NbDim ==2 |
---|
| 196 | DimOrder=[find(strcmp(ParamIn.Coord_y,VarDimName{ilist})) find(strcmp(ParamIn.Coord_x,VarDimName{ilist}))]; |
---|
| 197 | elseif NbDim ==3 |
---|
| 198 | DimOrder=[find(strcmp(ParamIn.Coord_z,VarDimName{ilist}))... |
---|
| 199 | find(strcmp(ParamIn.Coord_y,VarDimName{ilist}))... |
---|
| 200 | find(strcmp(ParamIn.Coord_x,VarDimName{ilist}))]; |
---|
[907] | 201 | end |
---|
[1045] | 202 | if ~isempty(DimOrder) |
---|
| 203 | Field.(ListVarName{ilist})=permute(Field.(ListVarName{ilist}),DimOrder); |
---|
| 204 | VarDimName{ilist}=VarDimName{ilist}(DimOrder); |
---|
[907] | 205 | end |
---|
[1045] | 206 | if numel(Field.VarAttribute)>=index_var |
---|
| 207 | VarAttribute{ilist}=Field.VarAttribute{index_var};% read var attributes from input if exist |
---|
[684] | 208 | end |
---|
[681] | 209 | end |
---|
[1045] | 210 | check_remove=false(1,numel(Field.ListVarName)); |
---|
| 211 | for ilist=1:numel(ListVarName) |
---|
| 212 | VarAttribute{ilist}.Role=Role{ilist}; |
---|
| 213 | VarAttribute{ilist}.ProjModeRequest=ProjModeRequest{ilist}; |
---|
| 214 | if isfield(ParamIn,'FieldName') |
---|
| 215 | VarAttribute{ilist}.Operator=Operator{ilist}; |
---|
[907] | 216 | end |
---|
[1045] | 217 | if strcmp(Operator{ilist},'norm') |
---|
| 218 | UName=ListVarName{ilist}; |
---|
| 219 | VName=ListVarName{ilist+1}; |
---|
| 220 | ListVarName{ilist}='norm'; |
---|
| 221 | Field.norm=Field.(UName).*Field.(UName)+Field.(VName).*Field.(VName); |
---|
| 222 | Field.norm=sqrt(Field.norm); |
---|
| 223 | check_remove(ilist+1)=true; |
---|
| 224 | VarAttribute{ilist}.Operator=''; |
---|
| 225 | end |
---|
[769] | 226 | end |
---|
[1045] | 227 | ListVarName(check_remove)=[]; |
---|
| 228 | VarDimName(check_remove)=[]; |
---|
| 229 | VarAttribute(check_remove)=[]; |
---|
| 230 | Field.ListVarName=[Field.ListVarName(1:NbCoord) ListVarName];% complement the list of vqriables, which may be listed twice |
---|
| 231 | Field.VarDimName=[Field.VarDimName(1:NbCoord) VarDimName]; |
---|
| 232 | Field.VarAttribute=[Field.VarAttribute(1:NbCoord) VarAttribute]; |
---|
[1095] | 233 | end |
---|
[527] | 234 | case 'video' |
---|
| 235 | if strcmp(class(ParamIn),'VideoReader') |
---|
| 236 | A=read(ParamIn,num); |
---|
| 237 | else |
---|
| 238 | ParamOut=VideoReader(FileName); |
---|
| 239 | A=read(ParamOut,num); |
---|
| 240 | end |
---|
| 241 | case 'mmreader' |
---|
| 242 | if strcmp(class(ParamIn),'mmreader') |
---|
| 243 | A=read(ParamIn,num); |
---|
| 244 | else |
---|
| 245 | ParamOut=mmreader(FileName); |
---|
| 246 | A=read(ParamOut,num); |
---|
| 247 | end |
---|
| 248 | case 'vol' |
---|
| 249 | A=imread(FileName); |
---|
| 250 | Npz=size(A,1)/ParamIn.Npy; |
---|
| 251 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
| 252 | A=permute(A,[3 2 1]); |
---|
| 253 | case 'multimage' |
---|
[907] | 254 | % warning 'off' |
---|
[527] | 255 | A=imread(FileName,num); |
---|
| 256 | case 'image' |
---|
| 257 | A=imread(FileName); |
---|
[784] | 258 | case 'rdvision' |
---|
[1058] | 259 | [A,FileInfo,timestamps,errormsg]=read_rdvision(FileName,num); |
---|
[1007] | 260 | case 'image_DaVis' |
---|
| 261 | Input=readimx(FileName); |
---|
[1033] | 262 | if numel(Input.Frames)==1 |
---|
| 263 | num=1; |
---|
| 264 | end |
---|
[1007] | 265 | A=Input.Frames{num}.Components{1}.Planes{1}'; |
---|
[1033] | 266 | for ilist=1:numel(Input.Frames{1}.Attributes) |
---|
| 267 | if strcmp(Input.Frames{1}.Attributes{ilist}.Name,'AcqTimeSeries') |
---|
[1045] | 268 | timestamps=str2num(Input.Frames{1}.Attributes{ilist}.Value(1:end-3))/1000000; |
---|
| 269 | break |
---|
[1033] | 270 | end |
---|
| 271 | end |
---|
[980] | 272 | case 'cine_phantom' |
---|
| 273 | [A,FileInfo] = read_cine_phantom(FileName,num ); |
---|
[784] | 274 | otherwise |
---|
| 275 | errormsg=[ FileType ': invalid input file type for uvmat']; |
---|
[1045] | 276 | |
---|
[527] | 277 | end |
---|
[445] | 278 | |
---|
[334] | 279 | %% case of image |
---|
| 280 | if ~isempty(A) |
---|
[1032] | 281 | if strcmp(FileType,'rdvision')||strcmp(FileType,'image_DaVis') |
---|
[784] | 282 | Field.Time=timestamps; |
---|
| 283 | end |
---|
[452] | 284 | if isstruct(ParamOut) |
---|
[580] | 285 | ParamOut.FieldName='image'; |
---|
[452] | 286 | end |
---|
[182] | 287 | Npz=1;%default |
---|
[181] | 288 | npxy=size(A); |
---|
| 289 | Field.NbDim=2;%default |
---|
| 290 | Field.AName='image'; |
---|
[782] | 291 | Field.ListVarName={'Coord_y','Coord_x','A'}; % |
---|
[1024] | 292 | Field.VarAttribute{1}.Unit='pixel'; |
---|
| 293 | Field.VarAttribute{2}.Unit='pixel'; |
---|
[1045] | 294 | Field.VarAttribute{1}.Role='coord_y'; |
---|
| 295 | Field.VarAttribute{2}.Role='coord_x'; |
---|
[1024] | 296 | Field.VarAttribute{3}.Role='scalar'; |
---|
[181] | 297 | if ndims(A)==3 |
---|
| 298 | if Npz==1;%color |
---|
[782] | 299 | Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x','rgb'}}; % |
---|
| 300 | Field.Coord_y=[npxy(1)-0.5 0.5]; |
---|
| 301 | Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 302 | if isstruct(ParamOut) |
---|
[580] | 303 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 304 | ParamOut.Npy=npxy(1); |
---|
[452] | 305 | end |
---|
[181] | 306 | else |
---|
| 307 | Field.NbDim=3; |
---|
| 308 | Field.ListVarName=['AZ' Field.ListVarName]; |
---|
[782] | 309 | Field.VarDimName={'AZ','Coord_y','Coord_x',{'AZ','Coord_y','Coord_x'}}; |
---|
[181] | 310 | Field.AZ=[npxy(1)-0.5 0.5]; |
---|
[782] | 311 | Field.Coord_y=[npxy(2)-0.5 0.5]; |
---|
| 312 | Field.Coord_x=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 313 | if isstruct(ParamOut) |
---|
[580] | 314 | ParamOut.Npx=npxy(3);% display image size on the interface |
---|
| 315 | ParamOut.Npy=npxy(2); |
---|
| 316 | end |
---|
[181] | 317 | end |
---|
| 318 | else |
---|
[782] | 319 | Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}}; % |
---|
| 320 | Field.Coord_y=[npxy(1)-0.5 0.5]; |
---|
| 321 | Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[181] | 322 | end |
---|
| 323 | Field.A=A; |
---|
| 324 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
| 325 | end |
---|
| 326 | |
---|
| 327 | |
---|
[334] | 328 | |
---|