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

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

bugs corrected. One step further for using compiled fcts and cluster with series

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