source: trunk/src/series/aver_stat.m @ 583

Last change on this file since 583 was 583, checked in by sommeria, 12 years ago

various bugs corrected, check_peaklocking added (to upgrade)

File size: 15.0 KB
Line 
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% This function is used in four modes by the GUI series:
8%           1) config GUI: with no input argument, the function determine the suitable GUI configuration
9%           2) interactive input: the function is used to interactively introduce input parameters, and then stops
10%           3) RUN: the function itself runs, when an appropriate input  structure Param has been introduced.
11%           4) BATCH: the function itself proceeds in BATCH mode, using an xml file 'Param' as input.
12%
13% This function is used in four modes by the GUI series:
14%           1) config GUI: with no input argument, the function determine the suitable GUI configuration
15%           2) interactive input: the function is used to interactively introduce input parameters, and then stops
16%           3) RUN: the function itself runs, when an appropriate input  structure Param has been introduced.
17%           4) BATCH: the function itself proceeds in BATCH mode, using an xml file 'Param' as input.
18%
19%OUTPUT
20% GUI_input=list of options in the GUI series.fig needed for the function
21%
22%INPUT:
23% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
24% In batch mode, Param is the name of the corresponding xml file containing the same information
25% In the absence of input (as activated when the current Action is selected
26% in series), the function ouput GUI_input set the activation of the needed GUI elements
27%
28% Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param)
29%    .InputTable: cell of input file names, (several lines for multiple input)
30%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
31%    .OutputSubDir: name of the subdirectory for data outputs
32%    .OutputDirExt: directory extension for data outputs
33%    .Action: .ActionName: name of the current activated function
34%             .ActionPath:   path of the current activated function
35%    .IndexRange: set the file or frame indices on which the action must be performed
36%    .FieldTransform: .TransformName: name of the selected transform function
37%                     .TransformPath:   path  of the selected transform function
38%                     .TransformHandle: corresponding function handle
39%    .InputFields: sub structure describing the input fields withfields
40%              .FieldName: name of the field
41%              .VelType: velocity type
42%              .FieldName_1: name of the second field in case of two input series
43%              .VelType_1: velocity type of the second field in case of two input series
44%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
47function ParamOut=aver_stat(Param)
48
49%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
50if ~exist('Param','var') % case with no input parameter
51    ParamOut={'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default)
52        'WholeIndexRange';'off';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
53        'NbSlice';'on'; ...%nbre of slices ('off' by default)
54        'VelType';'two';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
55        'FieldName';'two';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
56        'FieldTransform'; 'on';...%can use a transform function
57        'ProjObject';'on';...%can use projection object(option 'off'/'on',
58        'Mask';'off';...%can use mask option   (option 'off'/'on', 'off' by default)
59        'OutputDirExt';'.stat';...%set the output dir extension
60               ''};
61        return
62end
63
64%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
65%% select different modes,  RUN, parameter input, BATCH
66% BATCH  case: read the xml file for batch case
67if ischar(Param)
68        Param=xml2struct(Param);
69        checkrun=0;
70% RUN case: parameters introduced as the input structure Param
71else
72    if isfield(Param,'Specific')&& strcmp(Param.Specific,'?')
73        checkrun=1;% will only search interactive input parameters (preparation of BATCH mode)
74    else
75        checkrun=2; % indicate the RUN option is used
76    end
77    hseries=guidata(Param.hseries);%handles of the GUI series
78end
79ParamOut=Param; %default output
80OutputDir=[Param.OutputSubDir Param.OutputDirExt];
81   
82%% root input file(s) and type
83RootPath=Param.InputTable(:,1);
84RootFile=Param.InputTable(:,3);
85SubDir=Param.InputTable(:,2);
86NomType=Param.InputTable(:,4);
87FileExt=Param.InputTable(:,5);
88[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
89%%%%%%%%%%%%
90% The cell array filecell is the list of input file names, while
91% filecell{iview,fileindex}:
92%        iview: line in the table corresponding to a given file series
93%        fileindex: file index within  the file series,
94% 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
95% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
96%%%%%%%%%%%%
97NbSlice=1;%default
98if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
99    NbSlice=Param.IndexRange.NbSlice;
100end
101nbview=numel(i1_series);%number of input file series (lines in InputTable)
102nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
103nbfield_i=size(i1_series{1},2); %nb of fields for the i index
104nbfield=nbfield_j*nbfield_i; %total number of fields
105nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
106nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
107
108%determine the file type on each line from the first input file
109ImageTypeOptions={'image','multimage','mmreader','video'};
110NcTypeOptions={'netcdf','civx','civdata'};
111for iview=1:nbview
112    if ~exist(filecell{iview,1}','file')
113        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
114        return
115    end
116    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
117    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
118    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
119    if ~isempty(j1_series{iview})
120        frame_index{iview}=j1_series{iview};
121    else
122        frame_index{iview}=i1_series{iview};
123    end
124end
125
126%% calibration data and timing: read the ImaDoc files
127[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
128if size(time,1)>1
129    diff_time=max(max(diff(time)));
130    if diff_time>0
131        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
132    end   
133end
134
135%% coordinate transform or other user defined transform
136transform_fct='';%default
137if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
138    addpath(Param.FieldTransform.TransformPath)
139    transform_fct=str2func(Param.FieldTransform.TransformName);
140    rmpath(Param.FieldTransform.TransformPath)
141end
142
143%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
144 % EDIT FROM HERE
145
146%% check the validity of  input file types
147if CheckImage{1}
148    FileExtOut='.png'; % write result as .png images for image inputs
149elseif CheckNc{1}
150    FileExtOut='.nc';% write result as .nc files for netcdf inputs
151else
152    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
153    return
154end
155if nbview==2 && ~isequal(CheckImage{1},CheckImage{2})
156        msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
157    return
158end
159NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series
160if checkrun==1
161    return % stop here for input checks
162end
163
164%% Set field names and velocity types
165InputFields{1}=[];%default (case of images)
166if isfield(Param,'InputFields')
167    InputFields{1}=Param.InputFields;
168end
169if nbview==2
170    InputFields{2}=[];%default (case of images)
171    if isfield(Param,'InputFields')
172        InputFields{2}=Param.InputFields{1};%default
173        if isfield(Param.InputFields,'FieldName_1')
174            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
175            if isfield(Param.InputFields,'VelType_1')
176                InputFields{2}.VelType=Param.InputFields.VelType_1;
177            end
178        end
179    end
180end
181
182%% MAIN LOOP ON SLICES
183for i_slice=1:NbSlice
184    index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
185    nbfiles=0;
186    nbmissing=0;
187
188    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
189    for index=index_slice
190        if checkrun
191            update_waitbar(hseries.Waitbar,index/(nbfield))
192            stopstate=get(hseries.RUN,'BusyAction');
193        else
194            stopstate='queue';
195        end
196        if isequal(stopstate,'queue')% enable STOP command
197           
198        %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
199        for iview=1:nbview
200            % reading input file(s)
201            [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
202            if ~isempty(errormsg)
203                errormsg=['error of input reading: ' errormsg];
204                break
205            end
206            if ~isempty(NbSlice_calib)
207                Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
208            end
209        end
210        else
211            errormsg='stop';
212        end
213        %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
214        %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
215        % EDIT FROM HERE
216   
217        if isempty(errormsg)
218            Field=Data{1}; % default input field structure
219            %% coordinate transform (or other user defined transform)
220            if ~isempty(transform_fct)
221                switch nargin(transform_fct)
222                    case 4
223                        if length(Data)==2
224                            Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
225                        else
226                            Field=transform_fct(Data{1},XmlData{1});
227                        end
228                    case 3
229                        if length(Data)==2
230                            Field=transform_fct(Data{1},XmlData{1},Data{2});
231                        else
232                            Field=transform_fct(Data{1},XmlData{1});
233                        end
234                    case 2
235                        Field=transform_fct(Data{1},XmlData{1});
236                    case 1
237                        Field=transform_fct(Data{1});
238                end
239            end
240           
241            %% calculate tps coefficients if needed
242            if isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
243                Field=calc_tps(Field,check_proj_tps);
244            end
245
246            %field projection on an object
247            if Param.CheckObject
248                [Field,errormsg]=proj_field(Field,Param.ProjObject);
249                if ~isempty(errormsg)
250                    msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
251                    return
252                end
253            end
254            nbfiles=nbfiles+1;
255           
256            %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
257            %update sum
258            if nbfiles==1 %first field
259                time_1=[];
260                if isfield(Field,'Time')
261                    time_1=Field.Time(1);
262                end
263                DataOut=Field;%default
264                for ivar=1:length(Field.ListVarName)
265                    VarName=Field.ListVarName{ivar};
266                    DataOut.(VarName)=double(DataOut.(VarName));
267                end
268            else   %current field
269                for ivar=1:length(Field.ListVarName)
270                    VarName=Field.ListVarName{ivar};
271                    sizmean=size(DataOut.(VarName));
272                    siz=size(Field.(VarName));
273                    if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
274                        msgbox_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'])
275                        return
276                    else
277                        DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
278                    end
279                end
280            end
281            %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
282        else
283            display(errormsg)
284        end
285    end
286    %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
287   
288    for ivar=1:length(Field.ListVarName)
289        VarName=Field.ListVarName{ivar};
290        DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
291    end
292    if nbmissing~=0
293        msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
294    end
295    if isempty(time) % time is read from files
296        if isfield(Field,'Time')
297            time_end=Field.Time(1);%last time read
298            if ~isempty(time_1)
299                DataOut.Time=time_1;
300                DataOut.Time_end=time_end;
301            end
302        end
303    else  % time from ImaDoc prevails if it exists
304%         j1=1;%default
305%         if ~isempty(j1_series{1})
306%             j1=j1_series{1};
307%         end
308        %DataOut.Time=time(1,i1_series{1}(1),j1);
309        %DataOut.Time_end=time(end,i1_series{end}(end),j1_series{end}(end));
310        DataOut.Time=time(1);
311        DataOut.Time_end=time(end);
312    end
313   
314    %writting the result file
315    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),i_slice,[]);
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        display([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            display([OutputFile ' written']);
329        else
330            msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
331            display(errormsg)
332        end
333    end  % end averaging  loop
334end
335%%%%%%%%%%%%%%%% end loop on slices %%%%%%%%%%%%%%%%
336
337%% open the result file with uvmat (in RUN mode)
338if checkrun
339%     hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
340%     delete(hget_field)
341    uvmat(OutputFile)% open the last result file with uvmat
342end
Note: See TracBrowser for help on using the repository browser.