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

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

add aver_synchro (field analysis at different frequencies) and turb_stat

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