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 | % .FieldList: menu of possible fields |
---|
10 | % .VelType |
---|
11 | % .CivStage: stage of civx processing (=0, not Civx, =1 (civ1), =2 (fix1).... |
---|
12 | % .Npx,.Npy: for images, nbre of pixels in x and y |
---|
13 | % errormsg: error message, ='' by default |
---|
14 | % |
---|
15 | %INPUT |
---|
16 | % FileName: name of the input file |
---|
17 | % FileType: type of file, as determined by the function get_file_type.m |
---|
18 | % ParamIn: movie object or Matlab structure of input parameters |
---|
19 | % .FieldName: name (char string) of the input field (for Civx data) |
---|
20 | % .VelType: char string giving the type of velocity data ('civ1', 'filter1', 'civ2'...) |
---|
21 | % .ColorVar: variable used for vector color |
---|
22 | % .Npx, .Npy: nbre of pixels along x and y (used for .vol input files) |
---|
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 | Field=[]; |
---|
29 | if ~exist('num','var') |
---|
30 | num=1; |
---|
31 | end |
---|
32 | if ~exist('ParamIn','var') |
---|
33 | ParamIn=[]; |
---|
34 | end |
---|
35 | ParamOut=ParamIn;%default |
---|
36 | errormsg=''; |
---|
37 | % if isfield(ParamIn,'VelType') |
---|
38 | % VelType=ParamIn.VelType; |
---|
39 | % end |
---|
40 | A=[]; |
---|
41 | %% distingush different input file types |
---|
42 | try |
---|
43 | switch FileType |
---|
44 | case {'civx','civdata','netcdf'} %read the first nc field |
---|
45 | % ParamOut.FieldName=ParamIn.FieldName; |
---|
46 | GUIName='get_field'; %default name of the GUI get_field |
---|
47 | if isfield(ParamIn,'GUIName') |
---|
48 | GUIName=ParamIn.GUIName; |
---|
49 | end |
---|
50 | CivStage=0; |
---|
51 | if ~strcmp(ParamIn.FieldName,'get_field...')% if get_field is not requested, look for Civx data |
---|
52 | FieldList=calc_field;%list of possible fields for Civx data |
---|
53 | ParamOut.ColorVar='';%default |
---|
54 | if ischar(ParamIn.FieldName) |
---|
55 | FieldName=ParamIn.FieldName; |
---|
56 | else |
---|
57 | FieldName=ParamIn.FieldName{1}; |
---|
58 | end |
---|
59 | field_index=strcmp(FieldName,FieldList);%look for ParamIn.FieldName in the list of possible fields for Civx data |
---|
60 | if isempty(find(field_index,1))% ParamIn.FieldName is not in the list, check whether Civx data exist |
---|
61 | Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ','CivStage'); |
---|
62 | % case of new civdata conventions |
---|
63 | if isequal(Data.Conventions,'uvmat/civdata') |
---|
64 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
65 | ParamOut.ColorVar='ima_cor'; |
---|
66 | InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}]; |
---|
67 | [Field,ParamOut.VelType,errormsg]=read_civdata(FileName,InputField,ParamIn.VelType,Data.CivStage); |
---|
68 | if ~isempty(errormsg),errormsg=['read_civdata:' errormsg];return,end |
---|
69 | CivStage=Field.CivStage; |
---|
70 | ParamOut.CivStage=Field.CivStage; |
---|
71 | %case of old civx conventions |
---|
72 | elseif ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0) |
---|
73 | ParamOut.FieldName='velocity';%Civx data found, set .FieldName='velocity' by default |
---|
74 | ParamOut.ColorVar='ima_cor'; |
---|
75 | InputField=[{ParamOut.FieldName} {ParamOut.ColorVar}]; |
---|
76 | [Field,ParamOut.VelType]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
77 | if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end |
---|
78 | CivStage=Field.CivStage; |
---|
79 | ParamOut.CivStage=Field.CivStage; |
---|
80 | % not cvix file, fields will be chosen through the GUI get_field |
---|
81 | else |
---|
82 | ParamOut.FieldName='get_field...'; |
---|
83 | hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI |
---|
84 | if ~isempty(hget_field) |
---|
85 | delete(hget_field)%delete get_field for reinitialisation |
---|
86 | end |
---|
87 | end |
---|
88 | else |
---|
89 | InputField=ParamOut.FieldName; |
---|
90 | if ischar(InputField) |
---|
91 | InputField={InputField}; |
---|
92 | end |
---|
93 | if isfield(ParamIn,'ColorVar') |
---|
94 | ParamOut.ColorVar=ParamIn.ColorVar; |
---|
95 | InputField=[InputField {ParamOut.ColorVar}]; |
---|
96 | end |
---|
97 | [Field,ParamOut.VelType,errormsg]=read_civxdata(FileName,InputField,ParamIn.VelType); |
---|
98 | if ~isempty(errormsg),errormsg=['read_civxdata:' errormsg];return,end |
---|
99 | CivStage=Field.CivStage; |
---|
100 | ParamOut.CivStage=Field.CivStage; |
---|
101 | end |
---|
102 | ParamOut.FieldList=[{'image'};FieldList;{'get_field...'}]; |
---|
103 | end |
---|
104 | if CivStage==0% read the field names on the interface get_field. |
---|
105 | hget_field=findobj(allchild(0),'Name',GUIName);%find the get_field... GUI |
---|
106 | if isempty(hget_field)% open the GUI get_field if it is not found |
---|
107 | hget_field= get_field(FileName);%open the get_field GUI |
---|
108 | set(hget_field,'Name',GUIName)%update the name of get_field (e.g. get_field_1) |
---|
109 | end |
---|
110 | hhget_field=guidata(hget_field); |
---|
111 | %% update the get_field GUI |
---|
112 | set(hhget_field.inputfile,'String',FileName) |
---|
113 | set(hhget_field.list_fig,'Value',1) |
---|
114 | if exist('num','var')&&~isnan(num) |
---|
115 | set(hhget_field.TimeIndexValue,'String',num2str(num)) |
---|
116 | end |
---|
117 | funct_list=get(hhget_field.ACTION,'UserData'); |
---|
118 | funct_index=get(hhget_field.ACTION,'Value'); |
---|
119 | funct=funct_list{funct_index};%select the current action in get_field, e;g. PLOT |
---|
120 | Field=funct(hget_field); %%activate the current action selected in get_field, e;g.read the names of the variables to plot |
---|
121 | Tabchar={''};%default |
---|
122 | Tabcell=[]; |
---|
123 | set(hhget_field.inputfile,'String',FileName) |
---|
124 | if isfield(Field,'ListGlobalAttribute')&& ~isempty(Field.ListGlobalAttribute) |
---|
125 | for iline=1:length(Field.ListGlobalAttribute) |
---|
126 | Tabcell{iline,1}=Field.ListGlobalAttribute{iline}; |
---|
127 | if isfield(Field, Field.ListGlobalAttribute{iline}) |
---|
128 | val=Field.(Field.ListGlobalAttribute{iline}); |
---|
129 | if ischar(val); |
---|
130 | Tabcell{iline,2}=val; |
---|
131 | else |
---|
132 | Tabcell{iline,2}=num2str(val); |
---|
133 | end |
---|
134 | end |
---|
135 | end |
---|
136 | if ~isempty(Tabcell) |
---|
137 | Tabchar=cell2tab(Tabcell,'='); |
---|
138 | Tabchar=[{''};Tabchar]; |
---|
139 | end |
---|
140 | end |
---|
141 | ParamOut.CivStage=0; |
---|
142 | ParamOut.VelType=[]; |
---|
143 | if isfield(Field,'TimeIndex') |
---|
144 | ParamOut.TimeIndex=Field.TimeIndex; |
---|
145 | end |
---|
146 | if isfield(Field,'TimeValue') |
---|
147 | ParamOut.TimeValue=Field.TimeValue; |
---|
148 | end |
---|
149 | ParamOut.FieldList={'get_field...'}; |
---|
150 | end |
---|
151 | case 'video' |
---|
152 | if strcmp(class(ParamIn),'VideoReader') |
---|
153 | A=read(ParamIn,num); |
---|
154 | else |
---|
155 | ParamOut=VideoReader(FileName); |
---|
156 | A=read(ParamOut,num); |
---|
157 | end |
---|
158 | case 'mmreader' |
---|
159 | if strcmp(class(ParamIn),'mmreader') |
---|
160 | A=read(ParamIn,num); |
---|
161 | else |
---|
162 | ParamOut=mmreader(FileName); |
---|
163 | A=read(ParamOut,num); |
---|
164 | end |
---|
165 | case 'vol' |
---|
166 | A=imread(FileName); |
---|
167 | Npz=size(A,1)/ParamIn.Npy; |
---|
168 | A=reshape(A',ParamIn.Npx,ParamIn.Npy,Npz); |
---|
169 | A=permute(A,[3 2 1]); |
---|
170 | case 'multimage' |
---|
171 | warning 'off' |
---|
172 | A=imread(FileName,num); |
---|
173 | case 'image' |
---|
174 | A=imread(FileName); |
---|
175 | end |
---|
176 | catch ME |
---|
177 | errormsg=[FileType ' input: ' ME.message]; |
---|
178 | return |
---|
179 | end |
---|
180 | |
---|
181 | %% case of image |
---|
182 | if ~isempty(A) |
---|
183 | if isstruct(ParamOut) |
---|
184 | ParamOut.FieldName='image'; |
---|
185 | ParamOut.FieldList={'image'}; |
---|
186 | end |
---|
187 | Npz=1;%default |
---|
188 | npxy=size(A); |
---|
189 | % Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
190 | % Rangy=[npxy(1)-0.5 0.5]; % |
---|
191 | Field.NbDim=2;%default |
---|
192 | Field.AName='image'; |
---|
193 | Field.ListVarName={'AY','AX','A'}; % |
---|
194 | if ndims(A)==3 |
---|
195 | if Npz==1;%color |
---|
196 | Field.VarDimName={'AY','AX',{'AY','AX','rgb'}}; % |
---|
197 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
198 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
199 | if isstruct(ParamOut) |
---|
200 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
201 | ParamOut.Npy=npxy(1); |
---|
202 | end |
---|
203 | Field.VarAttribute{3}.Mesh=1; |
---|
204 | else |
---|
205 | Field.NbDim=3; |
---|
206 | Field.ListVarName=['AZ' Field.ListVarName]; |
---|
207 | Field.VarDimName={'AZ','AY','AX',{'AZ','AY','AX'}}; |
---|
208 | Field.AZ=[npxy(1)-0.5 0.5]; |
---|
209 | Field.AY=[npxy(2)-0.5 0.5]; |
---|
210 | Field.AX=[0.5 npxy(3)-0.5]; % coordinates of the first and last pixel centers |
---|
211 | if isstruct(ParamOut) |
---|
212 | ParamOut.Npx=npxy(3);% display image size on the interface |
---|
213 | ParamOut.Npy=npxy(2); |
---|
214 | end |
---|
215 | Field.VarAttribute{4}.Mesh=1; |
---|
216 | end |
---|
217 | else |
---|
218 | Field.VarDimName={'AY','AX',{'AY','AX'}}; % |
---|
219 | Field.AY=[npxy(1)-0.5 0.5]; |
---|
220 | Field.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers |
---|
221 | ParamOut.Npx=npxy(2);% display image size on the interface |
---|
222 | ParamOut.Npy=npxy(1); |
---|
223 | Field.VarAttribute{3}.Mesh=1; |
---|
224 | end |
---|
225 | Field.A=A; |
---|
226 | Field.CoordUnit='pixel'; %used for mouse_motion |
---|
227 | end |
---|
228 | |
---|
229 | |
---|
230 | |
---|