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_type.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 | % num: frame number for movies |
---|
23 | % |
---|
24 | % see also read_image.m,read_civxdata.m,read_civdata.m, |
---|
25 | |
---|
26 | function [Field,ParamOut,errormsg] = read_field(FileName,FileType,ParamIn,num) |
---|
27 | Field=[]; |
---|
28 | if ~exist('num','var') |
---|
29 | num=1; |
---|
30 | end |
---|
31 | if ~exist('ParamIn','var') |
---|
32 | ParamIn=[]; |
---|
33 | end |
---|
34 | ParamOut=ParamIn;%default |
---|
35 | errormsg=''; |
---|
36 | if ~exist(FileName,'file') |
---|
37 | erromsg=['input file ' FileName ' does not exist']; |
---|
38 | return |
---|
39 | end |
---|
40 | A=[]; |
---|
41 | InputField={}; |
---|
42 | check_colorvar=0; |
---|
43 | if isstruct(ParamIn) |
---|
44 | if isfield(ParamIn,'FieldName') |
---|
45 | if ischar(ParamIn.FieldName) |
---|
46 | InputField={ParamIn.FieldName}; |
---|
47 | else |
---|
48 | InputField= ParamIn.FieldName; |
---|
49 | end |
---|
50 | end |
---|
51 | check_colorvar=zeros(size(InputField)); |
---|
52 | if isfield(ParamIn,'ColorVar') |
---|
53 | InputField=[ParamIn.FieldName {ParamIn.ColorVar}]; |
---|
54 | check_colorvar(numel(InputField))=1; |
---|
55 | end |
---|
56 | end |
---|
57 | |
---|
58 | %% distingush different input file types |
---|
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={}; |
---|
71 | Role={}; |
---|
72 | ProjModeRequest={}; |
---|
73 | for ilist=1:numel(InputField) |
---|
74 | r=regexp(InputField{ilist},'(?<Operator>(^vec|^norm))\((?<UName>.+),(?<VName>.+)\)$','names'); |
---|
75 | if isempty(r)% no operator used |
---|
76 | ListVar=[ListVar InputField(ilist)]; |
---|
77 | if check_colorvar(ilist) |
---|
78 | Role{numel(ListVar)}='ancillary';% not projected with interpolation |
---|
79 | ProjModeRequest{numel(ListVar)}=''; |
---|
80 | else |
---|
81 | Role{numel(ListVar)}='scalar'; |
---|
82 | ProjModeRequest{numel(ListVar)}='interp_lin';%scalar field (requires interpolation for plot) |
---|
83 | end |
---|
84 | else % an operator 'vec' or 'norm' is used |
---|
85 | if ~check_colorvar(ilist) && strcmp(r.Operator,'norm') |
---|
86 | ProjModeRequestVar='interp_lin';%scalar field (requires interpolation for plot) |
---|
87 | else |
---|
88 | ProjModeRequestVar=''; |
---|
89 | end |
---|
90 | ind_var_U=find(strcmp(r.UName,ListVar));%check previous listing of variable r.UName |
---|
91 | ind_var_V=find(strcmp(r.VName,ListVar));%check previous listing of variable r.VName |
---|
92 | if isempty(ind_var_U) |
---|
93 | ListVar=[ListVar r.UName]; % append the variable in the list if not previously listed |
---|
94 | Role=[Role {'vector_x'}]; |
---|
95 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
96 | end |
---|
97 | if isempty(ind_var_V) |
---|
98 | ListVar=[ListVar r.VName];% append the variable in the list if not previously listed |
---|
99 | Role=[Role {'vector_y'}]; |
---|
100 | ProjModeRequest=[ProjModeRequest {ProjModeRequestVar}]; |
---|
101 | end |
---|
102 | end |
---|
103 | end |
---|
104 | if isfield(ParamIn,'TimeDimName')% case of reading of a single time index in a multidimensional array |
---|
105 | [Field,var_detect,ichoice]=nc2struct(FileName,'TimeDimName',ParamIn.TimeDimName,num,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]); |
---|
106 | else |
---|
107 | [Field,var_detect,ichoice]=nc2struct(FileName,[ParamIn.Coord_x (ParamIn.Coord_y) ListVar]); |
---|
108 | end |
---|
109 | if isfield(Field,'Txt') |
---|
110 | errormsg=Field.Txt; |
---|
111 | return |
---|
112 | end |
---|
113 | for ilist=3:numel(Field.VarDimName) |
---|
114 | if isequal(Field.VarDimName{1},Field.VarDimName{ilist}) |
---|
115 | Field.VarAttribute{1}.Role='coord_x';%unstructured coordinates |
---|
116 | Field.VarAttribute{2}.Role='coord_y'; |
---|
117 | break |
---|
118 | end |
---|
119 | end |
---|
120 | for ivar=1:numel(ListVar) |
---|
121 | Field.VarAttribute{ivar+2}.Role=Role{ivar}; |
---|
122 | if isfield(ParamIn,'FieldName') |
---|
123 | Field.VarAttribute{ivar+2}.FieldName=ParamIn.FieldName; |
---|
124 | end |
---|
125 | Field.VarAttribute{ivar+2}.ProjModeRequest=ProjModeRequest{ivar}; |
---|
126 | end |
---|
127 | |
---|
128 | case 'video' |
---|
129 | if strcmp(class(ParamIn),'VideoReader') |
---|
130 | A=read(ParamIn,num); |
---|
131 | else |
---|
132 | ParamOut=VideoReader(FileName); |
---|
133 | A=read(ParamOut,num); |
---|
134 | end |
---|
135 | case 'mmreader' |
---|
136 | if strcmp(class(ParamIn),'mmreader') |
---|
137 | A=read(ParamIn,num); |
---|
138 | else |
---|
139 | ParamOut=mmreader(FileName); |
---|
140 | A=read(ParamOut,num); |
---|
141 | end |
---|
142 | case 'vol' |
---|
143 | A=imread(FileName); |
---|
144 | Npz=size(A,1)/ParamIn.Npy; |
---|
145 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
146 | A=permute(A,[3 2 1]); |
---|
147 | case 'multimage' |
---|
148 | warning 'off' |
---|
149 | A=imread(FileName,num); |
---|
150 | case 'image' |
---|
151 | A=imread(FileName); |
---|
152 | end |
---|
153 | if ~isempty(errormsg) |
---|
154 | errormsg=[FileType ' input: ' errormsg]; |
---|
155 | return |
---|
156 | end |
---|
157 | |
---|
158 | %% case of image |
---|
159 | if ~isempty(A) |
---|
160 | if isstruct(ParamOut) |
---|
161 | ParamOut.FieldName='image'; |
---|
162 | end |
---|
163 | Npz=1;%default |
---|
164 | npxy=size(A); |
---|
165 | % Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
166 | % Rangy=[npxy(1)-0.5 0.5]; % |
---|
167 | Field.NbDim=2;%default |
---|
168 | Field.AName='image'; |
---|
169 | Field.ListVarName={'AY','AX','A'}; % |
---|
170 | if ndims(A)==3 |
---|
171 | if Npz==1;%color |
---|
172 | Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; % |
---|
173 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
174 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
175 | if isstruct(ParamOut) |
---|
176 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
177 | ParamOut.Npy=npxy(1); |
---|
178 | end |
---|
179 | Field.VarAttribute{3}.Mesh=1; |
---|
180 | else |
---|
181 | Field.NbDim=3; |
---|
182 | Field.ListVarName=['AZ' Field.ListVarName]; |
---|
183 | Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}}; |
---|
184 | Field.AZ=[npxy(1)-0.5 0.5]; |
---|
185 | Field.AY=[npxy(2)-0.5 0.5]; |
---|
186 | Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers |
---|
187 | if isstruct(ParamOut) |
---|
188 | ParamOut.Npx=npxy(3);% display image size on the interface |
---|
189 | ParamOut.Npy=npxy(2); |
---|
190 | end |
---|
191 | Field.VarAttribute{4}.Mesh=1; |
---|
192 | end |
---|
193 | else |
---|
194 | Field.VarDimName={'AY','AX',{'AY','AX'}}; % |
---|
195 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
196 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
197 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
198 | ParamOut.Npy=npxy(1); |
---|
199 | Field.VarAttribute{3}.Mesh=1; |
---|
200 | end |
---|
201 | Field.A=A; |
---|
202 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
203 | end |
---|
204 | |
---|
205 | |
---|
206 | |
---|