[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 |
---|
[497] | 16 | % FileType: type of file, as determined by the function get_file_type.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) |
---|
[466] | 22 | % num: frame number for movies |
---|
[497] | 23 | % |
---|
| 24 | % see also read_image.m,read_civxdata.m,read_civdata.m, |
---|
[466] | 25 | |
---|
[450] | 26 | function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num) |
---|
[181] | 27 | Field=[]; |
---|
[354] | 28 | if ~exist('num','var') |
---|
| 29 | num=1; |
---|
| 30 | end |
---|
| 31 | if ~exist('ParamIn','var') |
---|
| 32 | ParamIn=[]; |
---|
| 33 | end |
---|
| 34 | ParamOut=ParamIn;%default |
---|
[181] | 35 | errormsg=''; |
---|
[635] | 36 | if ~exist(FileName,'file') |
---|
| 37 | erromsg=['input file ' FileName ' does not exist']; |
---|
| 38 | return |
---|
| 39 | end |
---|
[334] | 40 | A=[]; |
---|
[575] | 41 | InputField={}; |
---|
| 42 | check_colorvar=0; |
---|
[526] | 43 | if isstruct(ParamIn) |
---|
[575] | 44 | if isfield(ParamIn,'FieldName') |
---|
| 45 | if ischar(ParamIn.FieldName) |
---|
[576] | 46 | InputField={ParamIn.FieldName}; |
---|
[575] | 47 | else |
---|
| 48 | InputField= ParamIn.FieldName; |
---|
| 49 | end |
---|
[526] | 50 | end |
---|
[654] | 51 | check_colorvar=zeros(size(InputField)); |
---|
[684] | 52 | if isfield(ParamIn,'ColorVar')&&~isempty(ParamIn.ColorVar) |
---|
[526] | 53 | InputField=[ParamIn.FieldName {ParamIn.ColorVar}]; |
---|
[654] | 54 | check_colorvar(numel(InputField))=1; |
---|
[526] | 55 | end |
---|
[521] | 56 | end |
---|
[575] | 57 | |
---|
[334] | 58 | %% distingush different input file types |
---|
[527] | 59 | switch FileType |
---|
| 60 | case 'civdata' |
---|
| 61 | [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType); |
---|
| 62 | if ~isempty(errormsg),errormsg=['read_civdata / ' errormsg];return,end |
---|
| 63 | ParamOut.CivStage=Field.CivStage; |
---|
| 64 | case 'civx' |
---|
| 65 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
| 66 | [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
| 67 | if ~isempty(errormsg),errormsg=['read_civxdata / ' errormsg];return,end |
---|
| 68 | ParamOut.CivStage=Field.CivStage; |
---|
| 69 | case 'netcdf' |
---|
| 70 | ListVar={}; |
---|
[675] | 71 | Role={}; |
---|
| 72 | ProjModeRequest={}; |
---|
[684] | 73 | ListInputField={}; |
---|
| 74 | ListOperator={}; |
---|
[681] | 75 | checkU=0; |
---|
| 76 | checkV=0; |
---|
[527] | 77 | for ilist=1:numel(InputField) |
---|
| 78 | r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
[654] | 79 | if isempty(r)% no operator used |
---|
[681] | 80 | if isempty(find(strcmp(InputField{ilist},ListVar))) |
---|
[684] | 81 | ListVar=[ListVar InputField(ilist)];%append the variable name if not already in the list |
---|
| 82 | ListInputField=[ListInputField InputField(ilist)]; |
---|
| 83 | ListOperator=[ListOperator {''}]; |
---|
[681] | 84 | end |
---|
[654] | 85 | if check_colorvar(ilist) |
---|
[667] | 86 | Role{numel(ListVar)}='ancillary';% not projected with interpolation |
---|
| 87 | ProjModeRequest{numel(ListVar)}=''; |
---|
[654] | 88 | else |
---|
[667] | 89 | Role{numel(ListVar)}='scalar'; |
---|
| 90 | ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
[654] | 91 | end |
---|
| 92 | else % an operator 'vec' or 'norm' is used |
---|
[684] | 93 | %Operator=r.Operator; |
---|
[667] | 94 | if ~check_colorvar(ilist) && strcmp(r.Operator,'norm') |
---|
[675] | 95 | ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot) |
---|
[667] | 96 | else |
---|
[675] | 97 | ProjModeRequestVar=''; |
---|
[667] | 98 | end |
---|
[675] | 99 | ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName |
---|
| 100 | ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName |
---|
| 101 | if isempty(ind_var_U) |
---|
[684] | 102 | ListVar=[ListVar {r.UName}]; % append the variable in the list if not previously listed |
---|
[675] | 103 | Role=[Role {'vector_x'}]; |
---|
| 104 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
[684] | 105 | ListInputField=[ListInputField InputField(ilist)]; |
---|
| 106 | %ListOperator=[ListOperator {[r.Operator '_U']}]; |
---|
[681] | 107 | else |
---|
| 108 | checkU=1; |
---|
[675] | 109 | end |
---|
| 110 | if isempty(ind_var_V) |
---|
[684] | 111 | ListVar=[ListVar {r.VName}];% append the variable in the list if not previously listed |
---|
[675] | 112 | Role=[Role {'vector_y'}]; |
---|
| 113 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
[684] | 114 | ListInputField=[ListInputField {''}]; |
---|
| 115 | %ListOperator=[ListOperator {[r.Operator '_V']}]; |
---|
[681] | 116 | else |
---|
| 117 | checkV=1; |
---|
[675] | 118 | end |
---|
[493] | 119 | end |
---|
[527] | 120 | end |
---|
[648] | 121 | if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array |
---|
| 122 | [Field,var_detect,ichoice]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]); |
---|
| 123 | else |
---|
[667] | 124 | [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]); |
---|
[648] | 125 | end |
---|
[534] | 126 | if isfield(Field,'Txt') |
---|
| 127 | errormsg=Field.Txt; |
---|
| 128 | return |
---|
| 129 | end |
---|
[675] | 130 | for ilist=3:numel(Field.VarDimName) |
---|
| 131 | if isequal(Field.VarDimName{1},Field.VarDimName{ilist}) |
---|
| 132 | Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates |
---|
| 133 | Field.VarAttribute{2}.Role='coord_y'; |
---|
| 134 | break |
---|
| 135 | end |
---|
| 136 | end |
---|
[684] | 137 | NormName=''; |
---|
| 138 | UName=''; |
---|
| 139 | VName=''; |
---|
| 140 | for ilist=1:numel(ListVar) |
---|
| 141 | Field.VarAttribute{ilist+2}.Role=Role{ilist}; |
---|
| 142 | Field.VarAttribute{ilist+2}.ProjModeRequest=ProjModeRequest{ilist}; |
---|
[648] | 143 | if isfield(ParamIn,'FieldName') |
---|
[684] | 144 | Field.VarAttribute{ilist+2}.FieldName=ListInputField{ilist}; |
---|
[648] | 145 | end |
---|
[684] | 146 | r=regexp(ListInputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
| 147 | if ~isempty(r)&& strcmp(r.Operator,'norm') |
---|
| 148 | NormName='norm'; |
---|
| 149 | if ~isempty(find(strcmp(ListVar,'norm'))) |
---|
| 150 | NormName='norm_1'; |
---|
| 151 | end |
---|
| 152 | Field.ListVarName=[Field.ListVarName {NormName}]; |
---|
| 153 | ilistmax=numel(Field.ListVarName); |
---|
| 154 | Field.VarDimName{ilistmax}=Field.VarDimName{ilist+2}; |
---|
| 155 | Field.VarAttribute{ilistmax}.Role='scalar'; |
---|
| 156 | Field.(NormName)=Field.(r.UName).*Field.(r.UName)+Field.(r.VName).*Field.(r.VName); |
---|
| 157 | Field.(NormName)=sqrt(Field.(NormName)); |
---|
| 158 | UName=r.UName; |
---|
| 159 | VName=r.VName; |
---|
[681] | 160 | end |
---|
| 161 | end |
---|
[684] | 162 | if ~isempty(NormName)% remove U and V if norm has been calculated and U and V are not needed as variables |
---|
| 163 | ind_var_U=find(strcmp(UName,ListVar));%check previous listing of variable r.UName |
---|
| 164 | ind_var_V=find(strcmp(VName,ListVar));%check previous listing of variable r.VName |
---|
| 165 | if ~checkU && ~checkV |
---|
| 166 | Field.ListVarName([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 167 | Field.VarDimName([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 168 | Field.VarAttribute([ind_var_U+2 ind_var_V+2])=[]; |
---|
| 169 | elseif ~checkU |
---|
| 170 | Field.ListVarName(ind_var_U+2)=[]; |
---|
| 171 | Field.VarDimName(ind_var_U+2)=[]; |
---|
| 172 | Field.VarAttribute(ind_var_U+2 )=[]; |
---|
| 173 | elseif ~checkV |
---|
| 174 | Field.ListVarName(ind_var_V+2)=[]; |
---|
| 175 | Field.VarDimName(ind_var_V+2)=[]; |
---|
| 176 | Field.VarAttribute(ind_var_V+2 )=[]; |
---|
| 177 | end |
---|
| 178 | end |
---|
[527] | 179 | case 'video' |
---|
| 180 | if strcmp(class(ParamIn),'VideoReader') |
---|
| 181 | A=read(ParamIn,num); |
---|
| 182 | else |
---|
| 183 | ParamOut=VideoReader(FileName); |
---|
| 184 | A=read(ParamOut,num); |
---|
| 185 | end |
---|
| 186 | case 'mmreader' |
---|
| 187 | if strcmp(class(ParamIn),'mmreader') |
---|
| 188 | A=read(ParamIn,num); |
---|
| 189 | else |
---|
| 190 | ParamOut=mmreader(FileName); |
---|
| 191 | A=read(ParamOut,num); |
---|
| 192 | end |
---|
| 193 | case 'vol' |
---|
| 194 | A=imread(FileName); |
---|
| 195 | Npz=size(A,1)/ParamIn.Npy; |
---|
| 196 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
| 197 | A=permute(A,[3 2 1]); |
---|
| 198 | case 'multimage' |
---|
| 199 | warning 'off' |
---|
| 200 | A=imread(FileName,num); |
---|
| 201 | case 'image' |
---|
| 202 | A=imread(FileName); |
---|
| 203 | end |
---|
| 204 | if ~isempty(errormsg) |
---|
| 205 | errormsg=[FileType ' input: ' errormsg]; |
---|
[445] | 206 | return |
---|
[334] | 207 | end |
---|
[445] | 208 | |
---|
[334] | 209 | %% case of image |
---|
| 210 | if ~isempty(A) |
---|
[452] | 211 | if isstruct(ParamOut) |
---|
[580] | 212 | ParamOut.FieldName='image'; |
---|
[452] | 213 | end |
---|
[182] | 214 | Npz=1;%default |
---|
[181] | 215 | npxy=size(A); |
---|
[580] | 216 | % Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
| 217 | % Rangy=[npxy(1)-0.5 0.5]; % |
---|
[181] | 218 | Field.NbDim=2;%default |
---|
| 219 | Field.AName='image'; |
---|
| 220 | Field.ListVarName={'AY','AX','A'}; % |
---|
| 221 | if ndims(A)==3 |
---|
| 222 | if Npz==1;%color |
---|
| 223 | Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; % |
---|
| 224 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
| 225 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 226 | if isstruct(ParamOut) |
---|
[580] | 227 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 228 | ParamOut.Npy=npxy(1); |
---|
[452] | 229 | end |
---|
[186] | 230 | Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 231 | else |
---|
| 232 | Field.NbDim=3; |
---|
| 233 | Field.ListVarName=['AZ' Field.ListVarName]; |
---|
| 234 | Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}}; |
---|
| 235 | Field.AZ=[npxy(1)-0.5 0.5]; |
---|
| 236 | Field.AY=[npxy(2)-0.5 0.5]; |
---|
| 237 | Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers |
---|
[452] | 238 | if isstruct(ParamOut) |
---|
[580] | 239 | ParamOut.Npx=npxy(3);% display image size on the interface |
---|
| 240 | ParamOut.Npy=npxy(2); |
---|
| 241 | end |
---|
[186] | 242 | Field.VarAttribute{4}.Mesh=1; |
---|
[181] | 243 | end |
---|
| 244 | else |
---|
| 245 | Field.VarDimName={'AY','AX',{'AY','AX'}}; % |
---|
| 246 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
| 247 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
[182] | 248 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
| 249 | ParamOut.Npy=npxy(1); |
---|
[186] | 250 | Field.VarAttribute{3}.Mesh=1; |
---|
[181] | 251 | end |
---|
| 252 | Field.A=A; |
---|
| 253 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
| 254 | end |
---|
| 255 | |
---|
| 256 | |
---|
[334] | 257 | |
---|