1 | %'aver_stat': calculate field average over a time series |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % function ParamOut=aver_stat(Param) |
---|
4 | % |
---|
5 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
6 | % |
---|
7 | %OUTPUT |
---|
8 | % ParamOut: sets options in the GUI series.fig needed for the function |
---|
9 | % |
---|
10 | %INPUT: |
---|
11 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
12 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
13 | % when Param.Action.RUN=0 (as activated when the current Action is selected |
---|
14 | % in series), the function ouput paramOut set the activation of the needed GUI elements |
---|
15 | % |
---|
16 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
---|
17 | % see the current structure Param) |
---|
18 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
19 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
20 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
21 | % .OutputDirExt: directory extension for data outputs |
---|
22 | % .Action: .ActionName: name of the current activated function |
---|
23 | % .ActionPath: path of the current activated function |
---|
24 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct |
---|
25 | % .RUN =0 for GUI input, =1 for function activation |
---|
26 | % .RunMode='local','background', 'cluster': type of function use |
---|
27 | % |
---|
28 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
29 | % .FieldTransform: .TransformName: name of the selected transform function |
---|
30 | % .TransformPath: path of the selected transform function |
---|
31 | % .InputFields: sub structure describing the input fields withfields |
---|
32 | % .FieldName: name(s) of the field |
---|
33 | % .VelType: velocity type |
---|
34 | % .FieldName_1: name of the second field in case of two input series |
---|
35 | % .VelType_1: velocity type of the second field in case of two input series |
---|
36 | % .Coord_y: name of y coordinate variable |
---|
37 | % .Coord_x: name of x coordinate variable |
---|
38 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
40 | |
---|
41 | function ParamOut=aver_stat(Param) |
---|
42 | |
---|
43 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName |
---|
44 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
45 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
46 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
47 | ParamOut.NbSlice='on'; %nbre of slices ('off' by default) |
---|
48 | ParamOut.VelType='two';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
49 | ParamOut.FieldName='two';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
50 | ParamOut.FieldTransform = 'on';%can use a transform function |
---|
51 | ParamOut.ProjObject='on';%can use projection object(option 'off'/'on', |
---|
52 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default) |
---|
53 | ParamOut.OutputDirExt='.stat';%set the output dir extension |
---|
54 | ParamOut.OutputFileMode='NbSlice';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
---|
55 | filecell=get_file_series(Param);%check existence of the first input file |
---|
56 | if ~exist(filecell{1,1},'file') |
---|
57 | msgbox_uvmat('WARNING','the first input file does not exist') |
---|
58 | end |
---|
59 | return |
---|
60 | end |
---|
61 | |
---|
62 | %%%%%%%%%%%% STANDARD PART %%%%%%%%%%%% |
---|
63 | ParamOut=[];%default output |
---|
64 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
65 | checkrun=1; |
---|
66 | if ischar(Param) |
---|
67 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
68 | checkrun=0; |
---|
69 | end |
---|
70 | hseries=findobj(allchild(0),'Tag','series'); |
---|
71 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
72 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
73 | |
---|
74 | %% define the directory for result file (with path=RootPath{1}) |
---|
75 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
76 | |
---|
77 | %% root input file(s) name, type and index series |
---|
78 | RootPath=Param.InputTable(:,1); |
---|
79 | RootFile=Param.InputTable(:,3); |
---|
80 | SubDir=Param.InputTable(:,2); |
---|
81 | NomType=Param.InputTable(:,4); |
---|
82 | FileExt=Param.InputTable(:,5); |
---|
83 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
84 | %%%%%%%%%%%% |
---|
85 | % The cell array filecell is the list of input file names, while |
---|
86 | % filecell{iview,fileindex}: |
---|
87 | % iview: line in the table corresponding to a given file series |
---|
88 | % fileindex: file index within the file series, |
---|
89 | % i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j |
---|
90 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
91 | %%%%%%%%%%%% |
---|
92 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
93 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
94 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
95 | NbField=NbField_j*NbField_i; %total number of fields |
---|
96 | |
---|
97 | %% determine the file type on each line from the first input file |
---|
98 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
99 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
100 | for iview=1:NbView |
---|
101 | if ~exist(filecell{iview,1}','file') |
---|
102 | msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist']) |
---|
103 | return |
---|
104 | end |
---|
105 | [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1}); |
---|
106 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
107 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
108 | if ~isempty(j1_series{iview}) |
---|
109 | frame_index{iview}=j1_series{iview}; |
---|
110 | else |
---|
111 | frame_index{iview}=i1_series{iview}; |
---|
112 | end |
---|
113 | end |
---|
114 | |
---|
115 | %% calibration data and timing: read the ImaDoc files |
---|
116 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
117 | if size(time,1)>1 |
---|
118 | diff_time=max(max(diff(time))); |
---|
119 | if diff_time>0 |
---|
120 | msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)]) |
---|
121 | end |
---|
122 | end |
---|
123 | |
---|
124 | %% coordinate transform or other user defined transform |
---|
125 | transform_fct='';%default |
---|
126 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
127 | addpath(Param.FieldTransform.TransformPath) |
---|
128 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
129 | rmpath(Param.FieldTransform.TransformPath) |
---|
130 | end |
---|
131 | |
---|
132 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
133 | % EDIT FROM HERE |
---|
134 | |
---|
135 | %% check the validity of input file types |
---|
136 | if CheckImage{1} |
---|
137 | FileExtOut='.png'; % write result as .png images for image inputs |
---|
138 | elseif CheckNc{1} |
---|
139 | FileExtOut='.nc';% write result as .nc files for netcdf inputs |
---|
140 | else |
---|
141 | msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}]) |
---|
142 | return |
---|
143 | end |
---|
144 | if NbView==2 && ~isequal(CheckImage{1},CheckImage{2}) |
---|
145 | msgbox_uvmat('ERROR','input must be two image series or two netcdf file series') |
---|
146 | return |
---|
147 | end |
---|
148 | |
---|
149 | %% settings for the output file |
---|
150 | NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file |
---|
151 | first_i=i1_series{1}(1); |
---|
152 | last_i=i1_series{1}(end); |
---|
153 | if isempty(j1_series{1})% if there is no second index j |
---|
154 | first_j=1;last_j=1; |
---|
155 | else |
---|
156 | first_j=j1_series{1}(1); |
---|
157 | last_j=j1_series{1}(end); |
---|
158 | end |
---|
159 | |
---|
160 | %% Set field names and velocity types |
---|
161 | InputFields{1}=[];%default (case of images) |
---|
162 | if isfield(Param,'InputFields') |
---|
163 | InputFields{1}=Param.InputFields; |
---|
164 | end |
---|
165 | if NbView==2 |
---|
166 | InputFields{2}=[];%default (case of images) |
---|
167 | if isfield(Param,'InputFields') |
---|
168 | InputFields{2}=Param.InputFields{1};%default |
---|
169 | if isfield(Param.InputFields,'FieldName_1') |
---|
170 | InputFields{2}.FieldName=Param.InputFields.FieldName_1; |
---|
171 | if isfield(Param.InputFields,'VelType_1') |
---|
172 | InputFields{2}.VelType=Param.InputFields.VelType_1; |
---|
173 | end |
---|
174 | end |
---|
175 | end |
---|
176 | end |
---|
177 | |
---|
178 | % for i_slice=1:NbSlice |
---|
179 | % index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice |
---|
180 | nbfiles=0; |
---|
181 | nbmissing=0; |
---|
182 | |
---|
183 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
184 | for index=1:NbField |
---|
185 | update_waitbar(WaitbarHandle,index/NbField) |
---|
186 | if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
187 | disp('program stopped by user') |
---|
188 | break |
---|
189 | end |
---|
190 | |
---|
191 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
192 | for iview=1:NbView |
---|
193 | % reading input file(s) |
---|
194 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index)); |
---|
195 | if ~isempty(errormsg) |
---|
196 | errormsg=['error of input reading: ' errormsg]; |
---|
197 | break |
---|
198 | end |
---|
199 | if ~isempty(NbSlice_calib) |
---|
200 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
201 | end |
---|
202 | end |
---|
203 | %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
204 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
205 | % EDIT FROM HERE |
---|
206 | |
---|
207 | if isempty(errormsg) |
---|
208 | Field=Data{1}; % default input field structure |
---|
209 | %% coordinate transform (or other user defined transform) |
---|
210 | if ~isempty(transform_fct) |
---|
211 | switch nargin(transform_fct) |
---|
212 | case 4 |
---|
213 | if length(Data)==2 |
---|
214 | Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2}); |
---|
215 | else |
---|
216 | Field=transform_fct(Data{1},XmlData{1}); |
---|
217 | end |
---|
218 | case 3 |
---|
219 | if length(Data)==2 |
---|
220 | Field=transform_fct(Data{1},XmlData{1},Data{2}); |
---|
221 | else |
---|
222 | Field=transform_fct(Data{1},XmlData{1}); |
---|
223 | end |
---|
224 | case 2 |
---|
225 | Field=transform_fct(Data{1},XmlData{1}); |
---|
226 | case 1 |
---|
227 | Field=transform_fct(Data{1}); |
---|
228 | end |
---|
229 | end |
---|
230 | |
---|
231 | %% calculate tps coefficients if needed |
---|
232 | if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps') |
---|
233 | Field=tps_coeff_field(Field,check_proj_tps); |
---|
234 | end |
---|
235 | |
---|
236 | %field projection on an object |
---|
237 | if Param.CheckObject |
---|
238 | [Field,errormsg]=proj_field(Field,Param.ProjObject); |
---|
239 | if ~isempty(errormsg) |
---|
240 | msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg]) |
---|
241 | return |
---|
242 | end |
---|
243 | end |
---|
244 | nbfiles=nbfiles+1; |
---|
245 | |
---|
246 | %%%%%%%%%%%% MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
247 | if nbfiles==1 %first field |
---|
248 | time_1=[]; |
---|
249 | if isfield(Field,'Time') |
---|
250 | time_1=Field.Time(1); |
---|
251 | end |
---|
252 | DataOut=Field;%default |
---|
253 | DataOut.Conventions='uvmat'; %suppress Conventions='uvmat/civdata' for civ input files |
---|
254 | for ivar=1:length(Field.ListVarName) |
---|
255 | VarName=Field.ListVarName{ivar}; |
---|
256 | DataOut.(VarName)=double(DataOut.(VarName)); |
---|
257 | end |
---|
258 | else %current field |
---|
259 | for ivar=1:length(Field.ListVarName) |
---|
260 | VarName=Field.ListVarName{ivar}; |
---|
261 | sizmean=size(DataOut.(VarName)); |
---|
262 | siz=size(Field.(VarName)); |
---|
263 | if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean) |
---|
264 | disp_uvmat('ERROR',['unequal size of input field ' VarName ', need to project on a grid'],checkrun) |
---|
265 | return |
---|
266 | else |
---|
267 | DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum |
---|
268 | end |
---|
269 | end |
---|
270 | end |
---|
271 | %%%%%%%%%%%% END MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
272 | else |
---|
273 | disp(errormsg) |
---|
274 | end |
---|
275 | end |
---|
276 | %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%% |
---|
277 | |
---|
278 | for ivar=1:length(Field.ListVarName) |
---|
279 | VarName=Field.ListVarName{ivar}; |
---|
280 | DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean |
---|
281 | end |
---|
282 | if nbmissing~=0 |
---|
283 | msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted']) |
---|
284 | end |
---|
285 | if isempty(time) % time is read from files |
---|
286 | if isfield(Field,'Time') |
---|
287 | time_end=Field.Time(1);%last time read |
---|
288 | if ~isempty(time_1) |
---|
289 | DataOut.Time=time_1; |
---|
290 | DataOut.Time_end=time_end; |
---|
291 | end |
---|
292 | end |
---|
293 | else % time from ImaDoc prevails if it exists |
---|
294 | DataOut.Time=time(1); |
---|
295 | DataOut.Time_end=time(end); |
---|
296 | end |
---|
297 | |
---|
298 | %% writing the result file |
---|
299 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j); |
---|
300 | if CheckImage{1} %case of images |
---|
301 | if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16)) |
---|
302 | DataOut.A=uint16(DataOut.A); |
---|
303 | imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images |
---|
304 | else |
---|
305 | DataOut.A=uint8(DataOut.A); |
---|
306 | imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images |
---|
307 | end |
---|
308 | disp([OutputFile ' written']); |
---|
309 | else %case of netcdf input file , determine global attributes |
---|
310 | errormsg=struct2nc(OutputFile,DataOut); %save result file |
---|
311 | if isempty(errormsg) |
---|
312 | disp([OutputFile ' written']); |
---|
313 | else |
---|
314 | disp(['error in writting result file: ' errormsg]) |
---|
315 | end |
---|
316 | end % end averaging loop |
---|
317 | |
---|
318 | %% open the result file with uvmat (in RUN mode) |
---|
319 | if checkrun |
---|
320 | uvmat(OutputFile)% open the last result file with uvmat |
---|
321 | end |
---|