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

Last change on this file since 592 was 592, checked in by sommeria, 11 years ago

commit the series fcts with the new form

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