[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 | %======================================================================= |
---|
| 28 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
---|
| 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=''; |
---|
[635] | 56 | if ~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); |
---|
| 82 | if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end |
---|
[822] | 83 | % if ~isempty(strcmp('C',ParamIn.FieldName))% if C image correlation is requested as field (not color visu) |
---|
| 84 | % ScalarIndex=strcmp('C',Field.ListVarName); |
---|
| 85 | % Field.VarAttribute{ScalarIndex}.Role='scalar';%put role as 'scalar' instead of ancillary |
---|
| 86 | % end |
---|
[527] | 87 | ParamOut.CivStage=Field.CivStage; |
---|
[748] | 88 | case 'civx'% old (obsolete) format for civ results |
---|
[527] | 89 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
| 90 | [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
| 91 | if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end |
---|
| 92 | ParamOut.CivStage=Field.CivStage; |
---|
[748] | 93 | case 'netcdf'% general netcdf file (not recognized as civ) |
---|
[527] | 94 | ListVar={}; |
---|
[675] | 95 | Role={}; |
---|
| 96 | ProjModeRequest={}; |
---|
[684] | 97 | ListInputField={}; |
---|
| 98 | ListOperator={}; |
---|
[681] | 99 | checkU=0; |
---|
| 100 | checkV=0; |
---|
[527] | 101 | for ilist=1:numel(InputField) |
---|
[748] | 102 | % look for input variables to read |
---|
[527] | 103 | r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
[654] | 104 | if isempty(r)% no operator used |
---|
[681] | 105 | if isempty(find(strcmp(InputField{ilist},ListVar))) |
---|
[684] | 106 | ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list |
---|
| 107 | ListInputField=[ListInputField InputField(ilist)]; |
---|
| 108 | ListOperator=[ListOperator {''}]; |
---|
[681] | 109 | end |
---|
[654] | 110 | if check_colorvar(ilist) |
---|
[667] | 111 | Role{numel(ListVar)}='ancillary';% not projected with interpolation |
---|
| 112 | ProjModeRequest{numel(ListVar)}=''; |
---|
[654] | 113 | else |
---|
[667] | 114 | Role{numel(ListVar)}='scalar'; |
---|
| 115 | ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
[654] | 116 | end |
---|
| 117 | else % an operator 'vec' or 'norm' is used |
---|
[667] | 118 | if ~check_colorvar(ilist) && strcmp(r.Operator,'norm') |
---|
[675] | 119 | ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot) |
---|
[667] | 120 | else |
---|
[675] | 121 | ProjModeRequestVar=''; |
---|
[667] | 122 | end |
---|
[675] | 123 | ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName |
---|
| 124 | ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName |
---|
| 125 | if isempty(ind_var_U) |
---|
[684] | 126 | ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed |
---|
[675] | 127 | Role=[Role {'vector_x'}]; |
---|
| 128 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
[684] | 129 | ListInputField=[ListInputField InputField(ilist)]; |
---|
| 130 | %ListOperator=[ListOperator {[r.Operator '_U']}]; |
---|
[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}]; |
---|
[684] | 138 | ListInputField=[ListInputField {''}]; |
---|
| 139 | %ListOperator=[ListOperator {[r.Operator '_V']}]; |
---|
[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]); |
---|
| 153 | NbCoord=NbCoord+1;% adds time coordinate |
---|
[648] | 154 | else |
---|
[748] | 155 | [Field,var_detect,ichoice,errormsg]=nc2struct(FileName,[ParamIn.Coord_x ParamIn.Coord_y ParamIn.Coord_z ListVar]); |
---|
[648] | 156 | end |
---|
[747] | 157 | if ~isempty(errormsg) |
---|
[534] | 158 | return |
---|
| 159 | end |
---|
[748] | 160 | %scan all the variables beyond the two first ones, ParamIn.Coord_x and ParamIn.Coord_y. |
---|
| 161 | for ilist=NbCoord+1:numel(Field.VarDimName) |
---|
| 162 | 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] | 163 | Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates |
---|
| 164 | Field.VarAttribute{2}.Role='coord_y'; |
---|
[748] | 165 | if NbCoord>=3 |
---|
| 166 | Field.VarAttribute{3}.Role='coord_z'; |
---|
| 167 | end |
---|
[675] | 168 | break |
---|
| 169 | end |
---|
| 170 | end |
---|
[684] | 171 | NormName=''; |
---|
| 172 | UName=''; |
---|
| 173 | VName=''; |
---|
| 174 | for ilist=1:numel(ListVar) |
---|
[748] | 175 | Field.VarAttribute{ilist+NbCoord}.Role=Role{ilist}; |
---|
| 176 | Field.VarAttribute{ilist+NbCoord}.ProjModeRequest=ProjModeRequest{ilist}; |
---|
[648] | 177 | if isfield(ParamIn,'FieldName') |
---|
[748] | 178 | Field.VarAttribute{ilist+NbCoord}.FieldName=ListInputField{ilist}; |
---|
[648] | 179 | end |
---|
[684] | 180 | r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
| 181 | if ~isempty(r)&& strcmp(r.Operator,'norm') |
---|
| 182 | NormName='norm'; |
---|
| 183 | if ~isempty(find(strcmp(ListVar,'norm'))) |
---|
| 184 | NormName='norm_1'; |
---|
| 185 | end |
---|
| 186 | Field.ListVarName=[Field.ListVarName {NormName}]; |
---|
| 187 | ilistmax=numel(Field.ListVarName); |
---|
| 188 | Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2}; |
---|
| 189 | Field.VarAttribute{ilistmax}.Role='scalar'; |
---|
| 190 | Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName); |
---|
| 191 | Field.(NormName)=sqrt(Field.(NormName)); |
---|
| 192 | UName=r.UName; |
---|
| 193 | VName=r.VName; |
---|
[681] | 194 | end |
---|
| 195 | end |
---|
[684] | 196 | if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables |
---|
| 197 | ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName |
---|
| 198 | ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName |
---|
[769] | 199 | if ~checkU && ~checkV |
---|
| 200 | Field.ListVarName([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 201 | Field.VarDimName([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 202 | Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 203 | elseif ~checkU |
---|
| 204 | Field.ListVarName(ind_var_U+2)=[]; |
---|
| 205 | Field.VarDimName(ind_var_U+2)=[]; |
---|
| 206 | Field.VarAttribute(ind_var_U+2 )=[]; |
---|
| 207 | elseif ~checkV |
---|
| 208 | Field.ListVarName(ind_var_V+2)=[]; |
---|
| 209 | Field.VarDimName(ind_var_V+2)=[]; |
---|
| 210 | Field.VarAttribute(ind_var_V+2 )=[]; |
---|
| 211 | end |
---|
[684] | 212 | end |
---|
[527] | 213 | case 'video' |
---|
| 214 | if strcmp(class(ParamIn),'VideoReader') |
---|
| 215 | A=read(ParamIn,num); |
---|
| 216 | else |
---|
| 217 | ParamOut=VideoReader(FileName); |
---|
| 218 | A=read(ParamOut,num); |
---|
| 219 | end |
---|
| 220 | case 'mmreader' |
---|
| 221 | if strcmp(class(ParamIn),'mmreader') |
---|
| 222 | A=read(ParamIn,num); |
---|
| 223 | else |
---|
| 224 | ParamOut=mmreader(FileName); |
---|
| 225 | A=read(ParamOut,num); |
---|
| 226 | end |
---|
| 227 | case 'vol' |
---|
| 228 | A=imread(FileName); |
---|
| 229 | Npz=size(A,1)/ParamIn.Npy; |
---|
| 230 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
| 231 | A=permute(A,[3 2 1]); |
---|
| 232 | case 'multimage' |
---|
[784] | 233 | % warning 'off' |
---|
[527] | 234 | A=imread(FileName,num); |
---|
| 235 | case 'image' |
---|
| 236 | A=imread(FileName); |
---|
[784] | 237 | case 'rdvision' |
---|
[785] | 238 | [A,FileInfo,timestamps]=read_rdvision(FileName,num); |
---|
[784] | 239 | otherwise |
---|
| 240 | errormsg=[ FileType ': invalid input file type for uvmat']; |
---|
[527] | 241 | end |
---|
[445] | 242 | |
---|
[334] | 243 | %% case of image |
---|
| 244 | if ~isempty(A) |
---|
[784] | 245 | if strcmp(FileType,'rdvision') |
---|
| 246 | Field.Time=timestamps; |
---|
| 247 | end |
---|
[452] | 248 | if isstruct(ParamOut) |
---|
[580] | 249 | ParamOut.FieldName='image'; |
---|
[452] | 250 | end |
---|
[182] | 251 | Npz=1;%default |
---|
[181] | 252 | npxy=size(A); |
---|
[580] | 253 | % Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
| 254 | % Rangy=[npxy(1)-0.5 0.5]; % |
---|
[181] | 255 | Field.NbDim=2;%default |
---|
| 256 | Field.AName='image'; |
---|
[782] | 257 | Field.ListVarName={'Coord_y','Coord_x','A'}; % |
---|
[181] | 258 | if ndims(A)==3 |
---|
| 259 | if Npz==1;%color |
---|
[782] | 260 | Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x','rgb'}}; % |
---|
| 261 | Field.Coord_y=[npxy(1)-0.5 0.5]; |
---|
| 262 | Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 263 | if isstruct(ParamOut) |
---|
[580] | 264 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 265 | ParamOut.Npy=npxy(1); |
---|
[452] | 266 | end |
---|
[186] | 267 | Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 268 | else |
---|
| 269 | Field.NbDim=3; |
---|
| 270 | Field.ListVarName=['AZ' Field.ListVarName]; |
---|
[782] | 271 | Field.VarDimName={'AZ','Coord_y','Coord_x',{'AZ','Coord_y','Coord_x'}}; |
---|
[181] | 272 | Field.AZ=[npxy(1)-0.5 0.5]; |
---|
[782] | 273 | Field.Coord_y=[npxy(2)-0.5 0.5]; |
---|
| 274 | Field.Coord_x=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 275 | if isstruct(ParamOut) |
---|
[580] | 276 | ParamOut.Npx=npxy(3);% display image size on the interface |
---|
| 277 | ParamOut.Npy=npxy(2); |
---|
| 278 | end |
---|
[186] | 279 | Field.VarAttribute{4}.Mesh=1; |
---|
[181] | 280 | end |
---|
| 281 | else |
---|
[782] | 282 | Field.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}}; % |
---|
| 283 | Field.Coord_y=[npxy(1)-0.5 0.5]; |
---|
| 284 | Field.Coord_x=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[182] | 285 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 286 | ParamOut.Npy=npxy(1); |
---|
[186] | 287 | Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 288 | end |
---|
| 289 | Field.A=A; |
---|
| 290 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
[784] | 291 | |
---|
[181] | 292 | end |
---|
| 293 | |
---|
| 294 | |
---|
[334] | 295 | |
---|