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

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

steps further to use series with cluster. Some bsolete functions removed

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%%%%%%%%%%%%
91% NbSlice=1;%default
92% if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
93%     NbSlice=Param.IndexRange.NbSlice;
94% end
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
99%nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
100%nbfield=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
175% for 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=1:nbfield
182    %for index=index_slice
183          if checkrun
184                stopstate=get(Param.RUNHandle,'BusyAction');
185                update_waitbar(Param.WaitbarHandle,index/nbfield)
186          else
187                stopstate='queue';
188          end
189        if isequal(stopstate,'queue')% enable STOP command
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        else
204            errormsg='stop';
205        end
206        %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
207        %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
208        % EDIT FROM HERE
209   
210        if isempty(errormsg)
211            Field=Data{1}; % default input field structure
212            %% coordinate transform (or other user defined transform)
213            if ~isempty(transform_fct)
214                switch nargin(transform_fct)
215                    case 4
216                        if length(Data)==2
217                            Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
218                        else
219                            Field=transform_fct(Data{1},XmlData{1});
220                        end
221                    case 3
222                        if length(Data)==2
223                            Field=transform_fct(Data{1},XmlData{1},Data{2});
224                        else
225                            Field=transform_fct(Data{1},XmlData{1});
226                        end
227                    case 2
228                        Field=transform_fct(Data{1},XmlData{1});
229                    case 1
230                        Field=transform_fct(Data{1});
231                end
232            end
233           
234            %% calculate tps coefficients if needed
235            if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
236                Field=tps_coeff_field(Field,check_proj_tps);
237            end
238
239            %field projection on an object
240            if Param.CheckObject
241                [Field,errormsg]=proj_field(Field,Param.ProjObject);
242                if ~isempty(errormsg)
243                    msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
244                    return
245                end
246            end
247            nbfiles=nbfiles+1;
248           
249            %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
250            %update sum
251            if nbfiles==1 %first field
252                time_1=[];
253                if isfield(Field,'Time')
254                    time_1=Field.Time(1);
255                end
256                DataOut=Field;%default
257                for ivar=1:length(Field.ListVarName)
258                    VarName=Field.ListVarName{ivar};
259                    DataOut.(VarName)=double(DataOut.(VarName));
260                end
261            else   %current field
262                for ivar=1:length(Field.ListVarName)
263                    VarName=Field.ListVarName{ivar};
264                    sizmean=size(DataOut.(VarName));
265                    siz=size(Field.(VarName));
266                    if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
267                        msgbox_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'])
268                        return
269                    else
270                        DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
271                    end
272                end
273            end
274            %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
275        else
276            display(errormsg)
277        end
278    end
279    %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
280   
281    for ivar=1:length(Field.ListVarName)
282        VarName=Field.ListVarName{ivar};
283        DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
284    end
285    if nbmissing~=0
286        msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
287    end
288    if isempty(time) % time is read from files
289        if isfield(Field,'Time')
290            time_end=Field.Time(1);%last time read
291            if ~isempty(time_1)
292                DataOut.Time=time_1;
293                DataOut.Time_end=time_end;
294            end
295        end
296    else  % time from ImaDoc prevails if it exists
297%         j1=1;%default
298%         if ~isempty(j1_series{1})
299%             j1=j1_series{1};
300%         end
301        %DataOut.Time=time(1,i1_series{1}(1),j1);
302        %DataOut.Time_end=time(end,i1_series{end}(end),j1_series{end}(end));
303        DataOut.Time=time(1);
304        DataOut.Time_end=time(end);
305    end
306   
307    %writting the result file
308    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),j1_series{1}(1),j1_series{1}(end));
309    if CheckImage{1} %case of images
310        if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16))
311            DataOut.A=uint16(DataOut.A);
312            imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images
313        else
314            DataOut.A=uint8(DataOut.A);
315            imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
316        end
317        display([OutputFile ' written']);
318    else %case of netcdf input file , determine global attributes
319        errormsg=struct2nc(OutputFile,DataOut); %save result file
320        if isempty(errormsg)
321            display([OutputFile ' written']);
322        else
323            msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
324            display(errormsg)
325        end
326    end  % end averaging  loop
327% end
328%%%%%%%%%%%%%%%% end loop on slices %%%%%%%%%%%%%%%%
329
330%% open the result file with uvmat (in RUN mode)
331if checkrun
332%     hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
333%     delete(hget_field)
334    uvmat(OutputFile)% open the last result file with uvmat
335end
Note: See TracBrowser for help on using the repository browser.