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)% function activated from the GUI series but not RUN |
---|
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 | % check the existence of the first file in the series |
---|
56 | first_j=[]; |
---|
57 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
58 | last_j=[]; |
---|
59 | if isfield(Param.IndexRange,'last_j'); last_j=Param.IndexRange.last_j; end |
---|
60 | PairString=''; |
---|
61 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
62 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
63 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
64 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
65 | if ~exist(FirstFileName,'file') |
---|
66 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
67 | else |
---|
68 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString); |
---|
69 | LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
70 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
71 | if ~exist(FirstFileName,'file') |
---|
72 | msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist']) |
---|
73 | end |
---|
74 | end |
---|
75 | return |
---|
76 | end |
---|
77 | |
---|
78 | %%%%%%%%%%%% STANDARD PART %%%%%%%%%%%% |
---|
79 | ParamOut=[];%default output |
---|
80 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
81 | checkrun=1; |
---|
82 | if ischar(Param) |
---|
83 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
84 | checkrun=0; |
---|
85 | end |
---|
86 | hseries=findobj(allchild(0),'Tag','series'); |
---|
87 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
88 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
89 | |
---|
90 | %% define the directory for result file (with path=RootPath{1}) |
---|
91 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
92 | |
---|
93 | %% root input file(s) name, type and index series |
---|
94 | RootPath=Param.InputTable(:,1); |
---|
95 | RootFile=Param.InputTable(:,3); |
---|
96 | SubDir=Param.InputTable(:,2); |
---|
97 | NomType=Param.InputTable(:,4); |
---|
98 | FileExt=Param.InputTable(:,5); |
---|
99 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
100 | %%%%%%%%%%%% |
---|
101 | % The cell array filecell is the list of input file names, while |
---|
102 | % filecell{iview,fileindex}: |
---|
103 | % iview: line in the table corresponding to a given file series |
---|
104 | % fileindex: file index within the file series, |
---|
105 | % 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 |
---|
106 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
107 | %%%%%%%%%%%% |
---|
108 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
109 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
110 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
111 | NbField=NbField_j*NbField_i; %total number of fields |
---|
112 | |
---|
113 | %% determine the file type on each line from the first input file |
---|
114 | ImageTypeOptions={'image','multimage','mmreader','video'}; |
---|
115 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
116 | for iview=1:NbView |
---|
117 | if ~exist(filecell{iview,1}','file') |
---|
118 | msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist']) |
---|
119 | return |
---|
120 | end |
---|
121 | [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1}); |
---|
122 | CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images |
---|
123 | CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files |
---|
124 | if ~isempty(j1_series{iview}) |
---|
125 | frame_index{iview}=j1_series{iview}; |
---|
126 | else |
---|
127 | frame_index{iview}=i1_series{iview}; |
---|
128 | end |
---|
129 | end |
---|
130 | |
---|
131 | %% calibration data and timing: read the ImaDoc files |
---|
132 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
133 | if size(time,1)>1 |
---|
134 | diff_time=max(max(diff(time))); |
---|
135 | if diff_time>0 |
---|
136 | msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)]) |
---|
137 | end |
---|
138 | end |
---|
139 | |
---|
140 | %% coordinate transform or other user defined transform |
---|
141 | transform_fct='';%default |
---|
142 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
143 | addpath(Param.FieldTransform.TransformPath) |
---|
144 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
145 | rmpath(Param.FieldTransform.TransformPath) |
---|
146 | end |
---|
147 | |
---|
148 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
149 | % EDIT FROM HERE |
---|
150 | |
---|
151 | %% check the validity of input file types |
---|
152 | if CheckImage{1} |
---|
153 | FileExtOut='.png'; % write result as .png images for image inputs |
---|
154 | elseif CheckNc{1} |
---|
155 | FileExtOut='.nc';% write result as .nc files for netcdf inputs |
---|
156 | else |
---|
157 | msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}]) |
---|
158 | return |
---|
159 | end |
---|
160 | if NbView==2 && ~isequal(CheckImage{1},CheckImage{2}) |
---|
161 | msgbox_uvmat('ERROR','input must be two image series or two netcdf file series') |
---|
162 | return |
---|
163 | end |
---|
164 | |
---|
165 | %% settings for the output file |
---|
166 | NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file |
---|
167 | first_i=i1_series{1}(1); |
---|
168 | last_i=i1_series{1}(end); |
---|
169 | if isempty(j1_series{1})% if there is no second index j |
---|
170 | first_j=1;last_j=1; |
---|
171 | else |
---|
172 | first_j=j1_series{1}(1); |
---|
173 | last_j=j1_series{1}(end); |
---|
174 | end |
---|
175 | |
---|
176 | %% Set field names and velocity types |
---|
177 | InputFields{1}=[];%default (case of images) |
---|
178 | if isfield(Param,'InputFields') |
---|
179 | InputFields{1}=Param.InputFields; |
---|
180 | end |
---|
181 | if NbView==2 |
---|
182 | InputFields{2}=[];%default (case of images) |
---|
183 | if isfield(Param,'InputFields') |
---|
184 | InputFields{2}=Param.InputFields{1};%default |
---|
185 | if isfield(Param.InputFields,'FieldName_1') |
---|
186 | InputFields{2}.FieldName=Param.InputFields.FieldName_1; |
---|
187 | if isfield(Param.InputFields,'VelType_1') |
---|
188 | InputFields{2}.VelType=Param.InputFields.VelType_1; |
---|
189 | end |
---|
190 | end |
---|
191 | end |
---|
192 | end |
---|
193 | |
---|
194 | % for i_slice=1:NbSlice |
---|
195 | % index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice |
---|
196 | nbfiles=0; |
---|
197 | nbmissing=0; |
---|
198 | |
---|
199 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
200 | for index=1:NbField |
---|
201 | update_waitbar(WaitbarHandle,index/NbField) |
---|
202 | if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
203 | disp('program stopped by user') |
---|
204 | break |
---|
205 | end |
---|
206 | |
---|
207 | %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
208 | for iview=1:NbView |
---|
209 | % reading input file(s) |
---|
210 | [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index)); |
---|
211 | if ~isempty(errormsg) |
---|
212 | errormsg=['error of input reading: ' errormsg]; |
---|
213 | break |
---|
214 | end |
---|
215 | if ~isempty(NbSlice_calib) |
---|
216 | Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform |
---|
217 | end |
---|
218 | end |
---|
219 | %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%% |
---|
220 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
221 | % EDIT FROM HERE |
---|
222 | |
---|
223 | if isempty(errormsg) |
---|
224 | Field=Data{1}; % default input field structure |
---|
225 | %% coordinate transform (or other user defined transform) |
---|
226 | if ~isempty(transform_fct) |
---|
227 | switch nargin(transform_fct) |
---|
228 | case 4 |
---|
229 | if length(Data)==2 |
---|
230 | Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2}); |
---|
231 | else |
---|
232 | Field=transform_fct(Data{1},XmlData{1}); |
---|
233 | end |
---|
234 | case 3 |
---|
235 | if length(Data)==2 |
---|
236 | Field=transform_fct(Data{1},XmlData{1},Data{2}); |
---|
237 | else |
---|
238 | Field=transform_fct(Data{1},XmlData{1}); |
---|
239 | end |
---|
240 | case 2 |
---|
241 | Field=transform_fct(Data{1},XmlData{1}); |
---|
242 | case 1 |
---|
243 | Field=transform_fct(Data{1}); |
---|
244 | end |
---|
245 | end |
---|
246 | |
---|
247 | %% calculate tps coefficients if needed |
---|
248 | if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps') |
---|
249 | Field=tps_coeff_field(Field,check_proj_tps); |
---|
250 | end |
---|
251 | |
---|
252 | %field projection on an object |
---|
253 | if Param.CheckObject |
---|
254 | [Field,errormsg]=proj_field(Field,Param.ProjObject); |
---|
255 | if ~isempty(errormsg) |
---|
256 | msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg]) |
---|
257 | return |
---|
258 | end |
---|
259 | end |
---|
260 | nbfiles=nbfiles+1; |
---|
261 | |
---|
262 | %%%%%%%%%%%% MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
263 | if nbfiles==1 %first field |
---|
264 | time_1=[]; |
---|
265 | if isfield(Field,'Time') |
---|
266 | time_1=Field.Time(1); |
---|
267 | end |
---|
268 | DataOut=Field;%default |
---|
269 | DataOut.Conventions='uvmat'; %suppress Conventions='uvmat/civdata' for civ input files |
---|
270 | for ivar=1:length(Field.ListVarName) |
---|
271 | VarName=Field.ListVarName{ivar}; |
---|
272 | DataOut.(VarName)=double(DataOut.(VarName)); |
---|
273 | end |
---|
274 | else %current field |
---|
275 | for ivar=1:length(Field.ListVarName) |
---|
276 | VarName=Field.ListVarName{ivar}; |
---|
277 | sizmean=size(DataOut.(VarName)); |
---|
278 | siz=size(Field.(VarName)); |
---|
279 | if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean) |
---|
280 | disp_uvmat('ERROR',['unequal size of input field ' VarName ', need to project on a grid'],checkrun) |
---|
281 | return |
---|
282 | else |
---|
283 | DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum |
---|
284 | end |
---|
285 | end |
---|
286 | end |
---|
287 | %%%%%%%%%%%% END MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
288 | else |
---|
289 | disp(errormsg) |
---|
290 | end |
---|
291 | end |
---|
292 | %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%% |
---|
293 | |
---|
294 | for ivar=1:length(Field.ListVarName) |
---|
295 | VarName=Field.ListVarName{ivar}; |
---|
296 | DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean |
---|
297 | end |
---|
298 | if nbmissing~=0 |
---|
299 | msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted']) |
---|
300 | end |
---|
301 | if isempty(time) % time is read from files |
---|
302 | if isfield(Field,'Time') |
---|
303 | time_end=Field.Time(1);%last time read |
---|
304 | if ~isempty(time_1) |
---|
305 | DataOut.Time=time_1; |
---|
306 | DataOut.Time_end=time_end; |
---|
307 | end |
---|
308 | end |
---|
309 | else % time from ImaDoc prevails if it exists |
---|
310 | DataOut.Time=time(1); |
---|
311 | DataOut.Time_end=time(end); |
---|
312 | end |
---|
313 | |
---|
314 | %% writing the result file |
---|
315 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j); |
---|
316 | if CheckImage{1} %case of images |
---|
317 | if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16)) |
---|
318 | DataOut.A=uint16(DataOut.A); |
---|
319 | imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images |
---|
320 | else |
---|
321 | DataOut.A=uint8(DataOut.A); |
---|
322 | imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images |
---|
323 | end |
---|
324 | disp([OutputFile ' written']); |
---|
325 | else %case of netcdf input file , determine global attributes |
---|
326 | errormsg=struct2nc(OutputFile,DataOut); %save result file |
---|
327 | if isempty(errormsg) |
---|
328 | disp([OutputFile ' written']); |
---|
329 | else |
---|
330 | disp(['error in writting result file: ' errormsg]) |
---|
331 | end |
---|
332 | end % end averaging loop |
---|
333 | |
---|
334 | %% open the result file with uvmat (in RUN mode) |
---|
335 | if checkrun |
---|
336 | uvmat(OutputFile)% open the last result file with uvmat |
---|
337 | end |
---|