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