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