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

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

bugs corrected to get an advancement bar with status

File size: 13.7 KB
RevLine 
[573]1%'aver_stat': calculate field average over a time series
[169]2%------------------------------------------------------------------------
[457]3% function ParamOut=aver_stat(Param)
[169]4%
[447]5%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
[457]6%
[169]7%OUTPUT
[596]8% ParamOut: sets options in the GUI series.fig needed for the function
[169]9%
10%INPUT:
[447]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
[596]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
[169]15%
[596]16% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
17% see the current structure Param)
[447]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
[474]21%    .OutputDirExt: directory extension for data outputs
[447]22%    .Action: .ActionName: name of the current activated function
23%             .ActionPath:   path of the current activated function
[596]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%             
[447]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
[596]32%              .FieldName: name(s) of the field
[447]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
[596]36%              .Coord_y: name of y coordinate variable
37%              .Coord_x: name of x coordinate variable
[447]38%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
[457]41function ParamOut=aver_stat(Param)
[447]42
43%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
[592]44if isstruct(Param) && isequal(Param.Action.RUN,0)
[605]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)
[592]53    ParamOut.OutputDirExt='.stat';%set the output dir extension
[605]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    filecell=get_file_series(Param);%check existence of the first input file
56    if ~exist(filecell{1,1},'file')
57        msgbox_uvmat('WARNING','the first input file does not exist')
58    end
59    return
[27]60end
61
[474]62%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
[594]63ParamOut=[];
[592]64%% read input parameters from an xml file if input is a file name (batch mode)
65checkrun=1;
[457]66if ischar(Param)
[592]67    Param=xml2struct(Param);% read Param as input file (batch case)
68    checkrun=0;
[361]69end
[592]70
71
[462]72ParamOut=Param; %default output
[474]73OutputDir=[Param.OutputSubDir Param.OutputDirExt];
74   
[457]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);
[374]81[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[474]82%%%%%%%%%%%%
83% The cell array filecell is the list of input file names, while
84% filecell{iview,fileindex}:
[447]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
[474]89%%%%%%%%%%%%
[595]90% NbSlice=1;%default
91% if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
92%     NbSlice=Param.IndexRange.NbSlice;
93% end
[454]94nbview=numel(i1_series);%number of input file series (lines in InputTable)
[457]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
[595]98%nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
99%nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
[43]100
[447]101%determine the file type on each line from the first input file
102ImageTypeOptions={'image','multimage','mmreader','video'};
103NcTypeOptions={'netcdf','civx','civdata'};
[27]104for iview=1:nbview
[451]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});
[447]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
[454]112    if ~isempty(j1_series{iview})
113        frame_index{iview}=j1_series{iview};
114    else
115        frame_index{iview}=i1_series{iview};
116    end
[27]117end
118
[451]119%% calibration data and timing: read the ImaDoc files
[470]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
[27]122    diff_time=max(max(diff(time)));
123    if diff_time>0
[447]124        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
[27]125    end   
126end
127
[447]128%% coordinate transform or other user defined transform
129transform_fct='';%default
[478]130if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]131    addpath(Param.FieldTransform.TransformPath)
132    transform_fct=str2func(Param.FieldTransform.TransformName);
133    rmpath(Param.FieldTransform.TransformPath)
[27]134end
[474]135
[447]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
[442]153
[592]154
[447]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
[255]170    end
171end
[447]172
[595]173% for i_slice=1:NbSlice
[598]174% index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
175nbfiles=0;
176nbmissing=0;
[474]177
[598]178%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
179for index=1:nbfield
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
[447]196        end
[598]197        if ~isempty(NbSlice_calib)
198            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[526]199        end
[598]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
[526]217                        Field=transform_fct(Data{1},XmlData{1});
[598]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});
[255]229            end
[598]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
[494]243            end
[598]244        end
245        nbfiles=nbfiles+1;
[526]246
[598]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'])
[27]266                    return
[598]267                else
268                    DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
[27]269                end
[255]270            end
[447]271        end
[598]272        %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
273    else
274        display(errormsg)
[255]275    end
[598]276end
277%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
278
279for ivar=1:length(Field.ListVarName)
280    VarName=Field.ListVarName{ivar};
281    DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
282end
283if nbmissing~=0
284    msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
285end
286if 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
[27]293    end
[598]294else  % time from ImaDoc prevails if it exists
[565]295%         j1=1;%default
296%         if ~isempty(j1_series{1})
297%             j1=j1_series{1};
298%         end
[598]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);
303end
304
305%writting the result file
306OutputFile=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));
307if 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
[27]314    end
[598]315    display([OutputFile ' written']);
316else %case of netcdf input file , determine global attributes
317    errormsg=struct2nc(OutputFile,DataOut); %save result file
318    if isempty(errormsg)
[447]319        display([OutputFile ' written']);
[598]320    else
321        msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
322        display(errormsg)
323    end
324end  % end averaging  loop
[595]325% end
[447]326%%%%%%%%%%%%%%%% end loop on slices %%%%%%%%%%%%%%%%
[255]327
[451]328%% open the result file with uvmat (in RUN mode)
329if checkrun
[526]330%     hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
331%     delete(hget_field)
[451]332    uvmat(OutputFile)% open the last result file with uvmat
333end
Note: See TracBrowser for help on using the repository browser.