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
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
[592]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
[27]61end
62
[474]63%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
[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%%%%%%%%%%%%
[447]90NbSlice=1;%default
[457]91if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
[447]92    NbSlice=Param.IndexRange.NbSlice;
[27]93end
[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
[454]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
[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
173%% MAIN LOOP ON SLICES
[27]174for i_slice=1:NbSlice
[447]175    index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
[255]176    nbfiles=0;
177    nbmissing=0;
[474]178
[447]179    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
180    for index=index_slice
[592]181          if checkrun
182                stopstate=get(Param.RUNHandle,'BusyAction');
183                update_waitbar(Param.WaitbarHandle,index/nbfield)
184          else
185                stopstate='queue';
186          end
[526]187        if isequal(stopstate,'queue')% enable STOP command
188           
[447]189        %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
190        for iview=1:nbview
[255]191            % reading input file(s)
[464]192            [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
[447]193            if ~isempty(errormsg)
194                errormsg=['error of input reading: ' errormsg];
195                break
[255]196            end
[447]197            if ~isempty(NbSlice_calib)
[454]198                Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[447]199            end
200        end
[526]201        else
202            errormsg='stop';
203        end
[447]204        %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
205        %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
206        % EDIT FROM HERE
[526]207   
[462]208        if isempty(errormsg)
[526]209            Field=Data{1}; % default input field structure
210            %% coordinate transform (or other user defined transform)
[255]211            if ~isempty(transform_fct)
[526]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});
[27]229                end
[255]230            end
231           
[526]232            %% calculate tps coefficients if needed
[592]233            if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
[585]234                Field=tps_coeff_field(Field,check_proj_tps);
[494]235            end
[526]236
[451]237            %field projection on an object
[447]238            if Param.CheckObject
[462]239                [Field,errormsg]=proj_field(Field,Param.ProjObject);
[255]240                if ~isempty(errormsg)
[27]241                    msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
242                    return
243                end
[255]244            end
245            nbfiles=nbfiles+1;
[447]246           
247            %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
248            %update sum
[462]249            if nbfiles==1 %first field
250                time_1=[];
251                if isfield(Field,'Time')
252                    time_1=Field.Time(1);
[255]253                end
[462]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
[255]271            end
[447]272            %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
273        else
[462]274            display(errormsg)
[447]275        end
[255]276    end
[447]277    %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
278   
[27]279    for ivar=1:length(Field.ListVarName)
280        VarName=Field.ListVarName{ivar};
[447]281        DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
[27]282    end
283    if nbmissing~=0
284        msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
285    end
[462]286    if isempty(time) % time is read from files
[27]287        if isfield(Field,'Time')
288            time_end=Field.Time(1);%last time read
289            if ~isempty(time_1)
[447]290                DataOut.Time=time_1;
291                DataOut.Time_end=time_end;
[27]292            end
293        end
[457]294    else  % 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
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);
[27]303    end
[255]304   
[526]305    %writting the result file
[474]306    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(1),i1_series{1}(end),i_slice,[]);
[447]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
[27]311        else
[447]312            DataOut.A=uint8(DataOut.A);
313            imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
[27]314        end
[447]315        display([OutputFile ' written']);
[55]316    else %case of netcdf input file , determine global attributes
[447]317        errormsg=struct2nc(OutputFile,DataOut); %save result file
[27]318        if isempty(errormsg)
[447]319            display([OutputFile ' written']);
[27]320        else
321            msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
322            display(errormsg)
323        end
[255]324    end  % end averaging  loop
[447]325end
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.