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