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

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

bug on waitbar corrected for background mode

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