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

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

tps_coeff_field introduced instead of calc_tps

File size: 15.0 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%
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%
[169]19%OUTPUT
20% GUI_input=list of options in the GUI series.fig needed for the function
21%
22%INPUT:
[447]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
[169]27%
[447]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
[474]32%    .OutputDirExt: directory extension for data outputs
[447]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
[457]47function ParamOut=aver_stat(Param)
[447]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
[474]51    ParamOut={'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default)
[457]52        'WholeIndexRange';'off';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
[27]53        'NbSlice';'on'; ...%nbre of slices ('off' by default)
[447]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
[27]60               ''};
61        return
62end
63
[474]64%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
[457]65%% select different modes,  RUN, parameter input, BATCH
[447]66% BATCH  case: read the xml file for batch case
[457]67if ischar(Param)
68        Param=xml2struct(Param);
69        checkrun=0;
70% RUN case: parameters introduced as the input structure Param
71else
[462]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
[478]77    hseries=guidata(Param.hseries);%handles of the GUI series
[361]78end
[462]79ParamOut=Param; %default output
[474]80OutputDir=[Param.OutputSubDir Param.OutputDirExt];
81   
[457]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);
[374]88[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[474]89%%%%%%%%%%%%
90% The cell array filecell is the list of input file names, while
91% filecell{iview,fileindex}:
[447]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
[474]96%%%%%%%%%%%%
[447]97NbSlice=1;%default
[457]98if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
[447]99    NbSlice=Param.IndexRange.NbSlice;
[27]100end
[454]101nbview=numel(i1_series);%number of input file series (lines in InputTable)
[457]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
[454]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
[43]107
[447]108%determine the file type on each line from the first input file
109ImageTypeOptions={'image','multimage','mmreader','video'};
110NcTypeOptions={'netcdf','civx','civdata'};
[27]111for iview=1:nbview
[451]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});
[447]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
[454]119    if ~isempty(j1_series{iview})
120        frame_index{iview}=j1_series{iview};
121    else
122        frame_index{iview}=i1_series{iview};
123    end
[27]124end
125
[451]126%% calibration data and timing: read the ImaDoc files
[470]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
[27]129    diff_time=max(max(diff(time)));
130    if diff_time>0
[447]131        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
[27]132    end   
133end
134
[447]135%% coordinate transform or other user defined transform
136transform_fct='';%default
[478]137if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]138    addpath(Param.FieldTransform.TransformPath)
139    transform_fct=str2func(Param.FieldTransform.TransformName);
140    rmpath(Param.FieldTransform.TransformPath)
[27]141end
[474]142
[447]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
[474]160if checkrun==1
161    return % stop here for input checks
[218]162end
[442]163
[447]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
[255]179    end
180end
[447]181
182%% MAIN LOOP ON SLICES
[27]183for i_slice=1:NbSlice
[447]184    index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
[255]185    nbfiles=0;
186    nbmissing=0;
[474]187
[447]188    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
189    for index=index_slice
190        if checkrun
[478]191            update_waitbar(hseries.Waitbar,index/(nbfield))
[447]192            stopstate=get(hseries.RUN,'BusyAction');
193        else
194            stopstate='queue';
195        end
[526]196        if isequal(stopstate,'queue')% enable STOP command
197           
[447]198        %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
199        for iview=1:nbview
[255]200            % reading input file(s)
[464]201            [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
[447]202            if ~isempty(errormsg)
203                errormsg=['error of input reading: ' errormsg];
204                break
[255]205            end
[447]206            if ~isempty(NbSlice_calib)
[454]207                Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[447]208            end
209        end
[526]210        else
211            errormsg='stop';
212        end
[447]213        %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
214        %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
215        % EDIT FROM HERE
[526]216   
[462]217        if isempty(errormsg)
[526]218            Field=Data{1}; % default input field structure
219            %% coordinate transform (or other user defined transform)
[255]220            if ~isempty(transform_fct)
[526]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});
[27]238                end
[255]239            end
240           
[526]241            %% calculate tps coefficients if needed
[583]242            if isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
[585]243                Field=tps_coeff_field(Field,check_proj_tps);
[494]244            end
[526]245
[451]246            %field projection on an object
[447]247            if Param.CheckObject
[462]248                [Field,errormsg]=proj_field(Field,Param.ProjObject);
[255]249                if ~isempty(errormsg)
[27]250                    msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
251                    return
252                end
[255]253            end
254            nbfiles=nbfiles+1;
[447]255           
256            %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
257            %update sum
[462]258            if nbfiles==1 %first field
259                time_1=[];
260                if isfield(Field,'Time')
261                    time_1=Field.Time(1);
[255]262                end
[462]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
[255]280            end
[447]281            %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
282        else
[462]283            display(errormsg)
[447]284        end
[255]285    end
[447]286    %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
287   
[27]288    for ivar=1:length(Field.ListVarName)
289        VarName=Field.ListVarName{ivar};
[447]290        DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
[27]291    end
292    if nbmissing~=0
293        msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
294    end
[462]295    if isempty(time) % time is read from files
[27]296        if isfield(Field,'Time')
297            time_end=Field.Time(1);%last time read
298            if ~isempty(time_1)
[447]299                DataOut.Time=time_1;
300                DataOut.Time_end=time_end;
[27]301            end
302        end
[457]303    else  % time from ImaDoc prevails if it exists
[565]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);
[27]312    end
[255]313   
[526]314    %writting the result file
[474]315    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),i_slice,[]);
[447]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
[27]320        else
[447]321            DataOut.A=uint8(DataOut.A);
322            imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
[27]323        end
[447]324        display([OutputFile ' written']);
[55]325    else %case of netcdf input file , determine global attributes
[447]326        errormsg=struct2nc(OutputFile,DataOut); %save result file
[27]327        if isempty(errormsg)
[447]328            display([OutputFile ' written']);
[27]329        else
330            msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
331            display(errormsg)
332        end
[255]333    end  % end averaging  loop
[447]334end
335%%%%%%%%%%%%%%%% end loop on slices %%%%%%%%%%%%%%%%
[255]336
[451]337%% open the result file with uvmat (in RUN mode)
338if checkrun
[526]339%     hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
340%     delete(hget_field)
[451]341    uvmat(OutputFile)% open the last result file with uvmat
342end
Note: See TracBrowser for help on using the repository browser.