[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 |
---|
| 10 | % .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2 (fix1).... |
---|
| 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) |
---|
[693] | 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 | %======================================================================= |
---|
[977] | 28 | % Copyright 2008-2017, 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 |
---|
| 51 | if ~exist('ParamIn','var') |
---|
| 52 | ParamIn=[]; |
---|
| 53 | end |
---|
| 54 | ParamOut=ParamIn;%default |
---|
[181] | 55 | errormsg=''; |
---|
[1022] | 56 | if isempty(regexp(FileName,'^http://'))&& ~exist(FileName,'file') |
---|
[747] | 57 | errormsg=['input file ' FileName ' does not exist']; |
---|
[635] | 58 | return |
---|
| 59 | end |
---|
[334] | 60 | A=[]; |
---|
[575] | 61 | InputField={}; |
---|
| 62 | check_colorvar=0; |
---|
[526] | 63 | if isstruct(ParamIn) |
---|
[575] | 64 | if isfield(ParamIn,'FieldName') |
---|
| 65 | if ischar(ParamIn.FieldName) |
---|
[576] | 66 | InputField={ParamIn.FieldName}; |
---|
[575] | 67 | else |
---|
| 68 | InputField= ParamIn.FieldName; |
---|
| 69 | end |
---|
[526] | 70 | end |
---|
[654] | 71 | check_colorvar=zeros(size(InputField)); |
---|
[684] | 72 | if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar) |
---|
[526] | 73 | InputField=[ParamIn.FieldName {ParamIn.ColorVar}]; |
---|
[654] | 74 | check_colorvar(numel(InputField))=1; |
---|
[526] | 75 | end |
---|
[521] | 76 | end |
---|
[575] | 77 | |
---|
[334] | 78 | %% distingush different input file types |
---|
[527] | 79 | switch FileType |
---|
[748] | 80 | case 'civdata'% new format for civ results |
---|
[527] | 81 | [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType); |
---|
[907] | 82 | if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end |
---|
[527] | 83 | ParamOut.CivStage=Field.CivStage; |
---|
[958] | 84 | case 'pivdata_fluidimage' |
---|
| 85 | [Field,ParamOut.VelType,errormsg]=read_pivdata_fluidimage(FileName,InputField,ParamIn.VelType); |
---|
| 86 | ParamOut.CivStage=Field.CivStage; |
---|
[748] | 87 | case 'civx'% old (obsolete) format for civ results |
---|
[527] | 88 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
| 89 | [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
| 90 | if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end |
---|
| 91 | ParamOut.CivStage=Field.CivStage; |
---|
[748] | 92 | case 'netcdf'% general netcdf file (not recognized as civ) |
---|
[527] | 93 | ListVar={}; |
---|
[675] | 94 | Role={}; |
---|
| 95 | ProjModeRequest={}; |
---|
[684] | 96 | ListInputField={}; |
---|
| 97 | ListOperator={}; |
---|
[681] | 98 | checkU=0; |
---|
| 99 | checkV=0; |
---|
[527] | 100 | for ilist=1:numel(InputField) |
---|
[748] | 101 | % look for input variables to read |
---|
[527] | 102 | r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
[654] | 103 | if isempty(r)% no operator used |
---|
[681] | 104 | if isempty(find(strcmp(InputField{ilist},ListVar))) |
---|
[684] | 105 | ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list |
---|
| 106 | ListInputField=[ListInputField InputField(ilist)]; |
---|
| 107 | ListOperator=[ListOperator {''}]; |
---|
[681] | 108 | end |
---|
[654] | 109 | if check_colorvar(ilist) |
---|
[907] | 110 | if isempty(find(strcmp(InputField{ilist},ListVar))) |
---|
[667] | 111 | Role{numel(ListVar)}='ancillary';% not projected with interpolation |
---|
| 112 | ProjModeRequest{numel(ListVar)}=''; |
---|
[907] | 113 | end |
---|
[654] | 114 | else |
---|
[667] | 115 | Role{numel(ListVar)}='scalar'; |
---|
| 116 | ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
[654] | 117 | end |
---|
| 118 | else % an operator 'vec' or 'norm' is used |
---|
[667] | 119 | if ~check_colorvar(ilist) && strcmp(r.Operator,'norm') |
---|
[675] | 120 | ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot) |
---|
[667] | 121 | else |
---|
[675] | 122 | ProjModeRequestVar=''; |
---|
[667] | 123 | end |
---|
[675] | 124 | ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName |
---|
| 125 | ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName |
---|
| 126 | if isempty(ind_var_U) |
---|
[684] | 127 | ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed |
---|
[675] | 128 | Role=[Role {'vector_x'}]; |
---|
| 129 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
[684] | 130 | ListInputField=[ListInputField InputField(ilist)]; |
---|
[681] | 131 | else |
---|
| 132 | checkU=1; |
---|
[675] | 133 | end |
---|
| 134 | if isempty(ind_var_V) |
---|
[684] | 135 | ListVar=[ListVar {r.VName}];% append the variable in the list if not previously listed |
---|
[675] | 136 | Role=[Role {'vector_y'}]; |
---|
| 137 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
[867] | 138 | ListInputField=[ListInputField InputField(ilist)]; |
---|
[890] | 139 | |
---|
[681] | 140 | else |
---|
| 141 | checkV=1; |
---|
[675] | 142 | end |
---|
[493] | 143 | end |
---|
[527] | 144 | end |
---|
[748] | 145 | if ~isfield(ParamIn,'Coord_z') |
---|
| 146 | ParamIn.Coord_z=[]; |
---|
| 147 | end |
---|
| 148 | NbCoord=~isempty(ParamIn.Coord_x)+~isempty(ParamIn.Coord_y)+~isempty(ParamIn.Coord_z); |
---|
[648] | 149 | if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array |
---|
[748] | 150 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]); |
---|
[747] | 151 | elseif isfield(ParamIn,'TimeVarName')% case of reading of a single time in a multidimensional array |
---|
[748] | 152 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,'TimeVarName',ParamIn.TimeVarName,num,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]); |
---|
[907] | 153 | if numel(num)~=1 |
---|
[1001] | 154 | NbCoord=NbCoord+1;% adds time coordinate, except if a single time has been selected |
---|
[907] | 155 | end |
---|
[648] | 156 | else |
---|
[748] | 157 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]); |
---|
[648] | 158 | end |
---|
[747] | 159 | if ~isempty(errormsg) |
---|
[534] | 160 | return |
---|
| 161 | end |
---|
[1001] | 162 | CheckStructured=1; |
---|
| 163 | %scan all the variables beyond the two first NbCoord ones describing the coordinates. |
---|
[748] | 164 | for ilist=NbCoord+1:numel(Field.VarDimName) |
---|
| 165 | if isequal(Field.VarDimName{1},Field.VarDimName{ilist}) % if a variable has the same dimension as the coordinate, it denotes a field with unstructured coordinates |
---|
[675] | 166 | Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates |
---|
| 167 | Field.VarAttribute{2}.Role='coord_y'; |
---|
[748] | 168 | if NbCoord>=3 |
---|
| 169 | Field.VarAttribute{3}.Role='coord_z'; |
---|
| 170 | end |
---|
[1001] | 171 | CheckUnstructured=0; |
---|
[675] | 172 | break |
---|
| 173 | end |
---|
| 174 | end |
---|
[1001] | 175 | if CheckStructured |
---|
| 176 | for ilist=NbCoord+1:numel(Field.VarDimName) |
---|
[1002] | 177 | if numel(Field.VarDimName{ilist})==NbCoord |
---|
| 178 | rank(1)=find(strcmp(ParamIn.Coord_x,Field.VarDimName{ilist})); |
---|
| 179 | rank(2)=find(strcmp(ParamIn.Coord_y,Field.VarDimName{ilist})); |
---|
| 180 | if NbCoord==3 |
---|
| 181 | rank(3)=find(strcmp(ParamIn.Coord_z,Field.VarDimName{ilist})); |
---|
| 182 | end |
---|
| 183 | rank=rank(end:-1:1); |
---|
| 184 | VarName=Field.ListVarName{ilist}; |
---|
| 185 | Field.(VarName)=permute(Field.(VarName),rank); |
---|
| 186 | Field.VarDimName{ilist}=Field.VarDimName{ilist}(rank);% permute the order of dimensions |
---|
[1001] | 187 | end |
---|
| 188 | end |
---|
[1002] | 189 | end |
---|
[684] | 190 | NormName=''; |
---|
| 191 | UName=''; |
---|
| 192 | VName=''; |
---|
[896] | 193 | if numel(Field.ListVarName)>NbCoord % if there are variables beyond coord (1 D plots) |
---|
[907] | 194 | for ilist=1:numel(ListVar) |
---|
| 195 | Field.VarAttribute{ilist+NbCoord}.Role=Role{ilist}; |
---|
| 196 | Field.VarAttribute{ilist+NbCoord}.ProjModeRequest=ProjModeRequest{ilist}; |
---|
| 197 | if isfield(ParamIn,'FieldName') |
---|
| 198 | Field.VarAttribute{ilist+NbCoord}.FieldName=ListInputField{ilist}; |
---|
| 199 | end |
---|
| 200 | r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
| 201 | if ~isempty(r)&& strcmp(r.Operator,'norm') |
---|
| 202 | NormName='norm'; |
---|
| 203 | if ~isempty(find(strcmp(ListVar,'norm'))) |
---|
| 204 | NormName='norm_1'; |
---|
| 205 | end |
---|
| 206 | Field.ListVarName=[Field.ListVarName {NormName}]; |
---|
| 207 | ilistmax=numel(Field.ListVarName); |
---|
| 208 | Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2}; |
---|
| 209 | Field.VarAttribute{ilistmax}.Role='scalar'; |
---|
| 210 | Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName); |
---|
| 211 | Field.(NormName)=sqrt(Field.(NormName)); |
---|
| 212 | UName=r.UName; |
---|
| 213 | VName=r.VName; |
---|
| 214 | end |
---|
[648] | 215 | end |
---|
[907] | 216 | |
---|
| 217 | if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables |
---|
| 218 | ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName |
---|
| 219 | ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName |
---|
| 220 | if ~checkU && ~checkV |
---|
| 221 | Field.ListVarName([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 222 | Field.VarDimName([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 223 | Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 224 | elseif ~checkU |
---|
| 225 | Field.ListVarName(ind_var_U+2)=[]; |
---|
| 226 | Field.VarDimName(ind_var_U+2)=[]; |
---|
| 227 | Field.VarAttribute(ind_var_U+2 )=[]; |
---|
| 228 | elseif ~checkV |
---|
| 229 | Field.ListVarName(ind_var_V+2)=[]; |
---|
| 230 | Field.VarDimName(ind_var_V+2)=[]; |
---|
| 231 | Field.VarAttribute(ind_var_V+2 )=[]; |
---|
[684] | 232 | end |
---|
[681] | 233 | end |
---|
[907] | 234 | % insert coordinates as indices in case of plots vs matrix index |
---|
| 235 | if isfield(ParamIn,'CheckCoordIndex') && ParamIn.CheckCoordIndex |
---|
| 236 | Field.ListVarName=[Field.ListDimName Field.ListVarName]; |
---|
| 237 | Field.VarDimName=[Field.ListDimName Field.VarDimName]; |
---|
| 238 | for idim=1:numel(Field.ListDimName) |
---|
| 239 | CoordName=Field.ListDimName{idim}; |
---|
| 240 | Field.(CoordName)=1:Field.DimValue(idim); |
---|
| 241 | end |
---|
| 242 | Field.VarAttribute=[cell(1,numel(Field.ListDimName)) Field.VarAttribute]; |
---|
[769] | 243 | end |
---|
[1001] | 244 | |
---|
[684] | 245 | end |
---|
[527] | 246 | case 'video' |
---|
| 247 | if strcmp(class(ParamIn),'VideoReader') |
---|
| 248 | A=read(ParamIn,num); |
---|
| 249 | else |
---|
| 250 | ParamOut=VideoReader(FileName); |
---|
| 251 | A=read(ParamOut,num); |
---|
| 252 | end |
---|
| 253 | case 'mmreader' |
---|
| 254 | if strcmp(class(ParamIn),'mmreader') |
---|
| 255 | A=read(ParamIn,num); |
---|
| 256 | else |
---|
| 257 | ParamOut=mmreader(FileName); |
---|
| 258 | A=read(ParamOut,num); |
---|
| 259 | end |
---|
| 260 | case 'vol' |
---|
| 261 | A=imread(FileName); |
---|
| 262 | Npz=size(A,1)/ParamIn.Npy; |
---|
| 263 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
| 264 | A=permute(A,[3 2 1]); |
---|
| 265 | case 'multimage' |
---|
[907] | 266 | % warning 'off' |
---|
[527] | 267 | A=imread(FileName,num); |
---|
| 268 | case 'image' |
---|
| 269 | A=imread(FileName); |
---|
[784] | 270 | case 'rdvision' |
---|
[785] | 271 | [A,FileInfo,timestamps]=read_rdvision(FileName,num); |
---|
[1007] | 272 | case 'image_DaVis' |
---|
| 273 | Input=readimx(FileName); |
---|
| 274 | A=Input.Frames{num}.Components{1}.Planes{1}'; |
---|
[980] | 275 | case 'cine_phantom' |
---|
| 276 | [A,FileInfo] = read_cine_phantom(FileName,num ); |
---|
[784] | 277 | otherwise |
---|
| 278 | errormsg=[ FileType ': invalid input file type for uvmat']; |
---|
[527] | 279 | end |
---|
[445] | 280 | |
---|
[334] | 281 | %% case of image |
---|
| 282 | if ~isempty(A) |
---|
[784] | 283 | if strcmp(FileType,'rdvision') |
---|
| 284 | Field.Time=timestamps; |
---|
| 285 | end |
---|
[452] | 286 | if isstruct(ParamOut) |
---|
[580] | 287 | ParamOut.FieldName='image'; |
---|
[452] | 288 | end |
---|
[182] | 289 | Npz=1;%default |
---|
[181] | 290 | npxy=size(A); |
---|
[580] | 291 | % Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
| 292 | % Rangy=[npxy(1)-0.5 0.5]; % |
---|
[181] | 293 | Field.NbDim=2;%default |
---|
| 294 | Field.AName='image'; |
---|
[782] | 295 | Field.ListVarName={'Coord_y','Coord_x','A'}; % |
---|
[181] | 296 | if ndims(A)==3 |
---|
| 297 | if Npz==1;%color |
---|
[782] | 298 | Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x','rgb'}}; % |
---|
| 299 | Field.Coord_y=[npxy(1)-0.5 0.5]; |
---|
| 300 | Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 301 | if isstruct(ParamOut) |
---|
[580] | 302 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 303 | ParamOut.Npy=npxy(1); |
---|
[452] | 304 | end |
---|
[871] | 305 | % Field.VarAttribute{3}.Mesh=1; |
---|
[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 |
---|
[871] | 317 | % Field.VarAttribute{4}.Mesh=1; |
---|
[181] | 318 | end |
---|
| 319 | else |
---|
[782] | 320 | Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}}; % |
---|
| 321 | Field.Coord_y=[npxy(1)-0.5 0.5]; |
---|
| 322 | Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[880] | 323 | % ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 324 | % ParamOut.Npy=npxy(1); |
---|
[871] | 325 | % Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 326 | end |
---|
| 327 | Field.A=A; |
---|
| 328 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
[784] | 329 | |
---|
[181] | 330 | end |
---|
| 331 | |
---|
| 332 | |
---|
[334] | 333 | |
---|