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

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

adpat uvmat to reading data from rdvision

File size: 14.6 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    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
124    FileType{iview}=FileInfo{iview}.FileType;
125    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
126    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
127    if ~isempty(j1_series{iview})
128        frame_index{iview}=j1_series{iview};
129    else
130        frame_index{iview}=i1_series{iview};
131    end
132end
133
134%% calibration data and timing: read the ImaDoc files
135[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
136if size(time,1)>1
137    diff_time=max(max(diff(time)));
138    if diff_time>0
139        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
140    end   
141end
142
143%% coordinate transform or other user defined transform
144transform_fct='';%default
145if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
146    addpath(Param.FieldTransform.TransformPath)
147    transform_fct=str2func(Param.FieldTransform.TransformName);
148    rmpath(Param.FieldTransform.TransformPath)
149end
150
151%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
152 % EDIT FROM HERE
153
154%% check the validity of  input file types
155if CheckImage{1}
156    FileExtOut='.png'; % write result as .png images for image inputs
157elseif CheckNc{1}
158    FileExtOut='.nc';% write result as .nc files for netcdf inputs
159else
160    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
161    return
162end
163if NbView==2 && ~isequal(CheckImage{1},CheckImage{2})
164    msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
165    return
166end
167
168%% settings for the output file
169NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
170first_i=i1_series{1}(1);
171last_i=i1_series{1}(end);
172if isempty(j1_series{1})% if there is no second index j
173    first_j=1;last_j=1;
174else
175    first_j=j1_series{1}(1);
176    last_j=j1_series{1}(end);
177end
178
179%% Set field names and velocity types
180InputFields{1}=[];%default (case of images)
181if isfield(Param,'InputFields')
182    InputFields{1}=Param.InputFields;
183end
184if NbView==2
185    InputFields{2}=[];%default (case of images)
186    if isfield(Param,'InputFields')
187        InputFields{2}=Param.InputFields{1};%default
188        if isfield(Param.InputFields,'FieldName_1')
189            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
190            if isfield(Param.InputFields,'VelType_1')
191                InputFields{2}.VelType=Param.InputFields.VelType_1;
192            end
193        end
194    end
195end
196
197% for i_slice=1:NbSlice
198% index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
199nbfiles=0;
200nbmissing=0;
201
202%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
203for index=1:NbField
204    update_waitbar(WaitbarHandle,index/NbField)
205    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
206        disp('program stopped by user')
207        break
208    end
209   
210    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
211    for iview=1:NbView
212        % reading input file(s)
213        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
214        if ~isempty(errormsg)
215            errormsg=['error of input reading: ' errormsg];
216            break
217        end
218        if ~isempty(NbSlice_calib)
219            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
220        end
221    end
222    %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
223    %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
224    % EDIT FROM HERE
225   
226    if isempty(errormsg)
227        Field=Data{1}; % default input field structure
228        %% coordinate transform (or other user defined transform)
229        if ~isempty(transform_fct)
230            switch nargin(transform_fct)
231                case 4
232                    if length(Data)==2
233                        Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
234                    else
235                        Field=transform_fct(Data{1},XmlData{1});
236                    end
237                case 3
238                    if length(Data)==2
239                        Field=transform_fct(Data{1},XmlData{1},Data{2});
240                    else
241                        Field=transform_fct(Data{1},XmlData{1});
242                    end
243                case 2
244                    Field=transform_fct(Data{1},XmlData{1});
245                case 1
246                    Field=transform_fct(Data{1});
247            end
248        end
249       
250        %% calculate tps coefficients if needed
251        if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
252            Field=tps_coeff_field(Field,check_proj_tps);
253        end
254       
255        %field projection on an object
256        if Param.CheckObject
257            [Field,errormsg]=proj_field(Field,Param.ProjObject);
258            if ~isempty(errormsg)
259                msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
260                return
261            end
262        end
263        nbfiles=nbfiles+1;
264       
265        %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
266        if nbfiles==1 %first field
267            time_1=[];
268            if isfield(Field,'Time')
269                time_1=Field.Time(1);
270            end
271            DataOut=Field;%default
272            DataOut.Conventions='uvmat'; %suppress Conventions='uvmat/civdata' for civ input files
273            for ivar=1:length(Field.ListVarName)
274                VarName=Field.ListVarName{ivar};
275                DataOut.(VarName)=double(DataOut.(VarName));
276            end
277        else   %current field
278            for ivar=1:length(Field.ListVarName)
279                VarName=Field.ListVarName{ivar};
280                sizmean=size(DataOut.(VarName));
281                siz=size(Field.(VarName));
282                if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
283                    disp_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'],checkrun)
284                    return
285                else
286                    DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
287                end
288            end
289        end
290        %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
291    else
292        disp(errormsg)
293    end
294end
295%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
296
297for ivar=1:length(Field.ListVarName)
298    VarName=Field.ListVarName{ivar};
299    DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
300end
301if nbmissing~=0
302    msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
303end
304if isempty(time) % time is read from files
305    if isfield(Field,'Time')
306        time_end=Field.Time(1);%last time read
307        if ~isempty(time_1)
308            DataOut.Time=time_1;
309            DataOut.Time_end=time_end;
310        end
311    end
312else  % time from ImaDoc prevails if it exists
313    DataOut.Time=time(1);
314    DataOut.Time_end=time(end);
315end
316
317%% writing the result file
318OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j);
319if CheckImage{1} %case of images
320    if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16))
321        DataOut.A=uint16(DataOut.A);
322        imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images
323    else
324        DataOut.A=uint8(DataOut.A);
325        imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
326    end
327    disp([OutputFile ' written']);
328else %case of netcdf input file , determine global attributes
329    errormsg=struct2nc(OutputFile,DataOut); %save result file
330    if isempty(errormsg)
331        disp([OutputFile ' written']);
332    else
333        disp(['error in writting result file: ' errormsg])
334    end
335end  % end averaging  loop
336
337%% open the result file with uvmat (in RUN mode)
338if checkrun
339    uvmat(OutputFile)% open the last result file with uvmat
340end
Note: See TracBrowser for help on using the repository browser.