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

Last change on this file since 453 was 451, checked in by sommeria, 12 years ago

bugs repaired in aver_stat and check_data_file

sub_background largely modified. Tests needed
other functions not yet translated

File size: 16.8 KB
Line 
1%'aver_stat': calculate field average, used with series.fig
2%------------------------------------------------------------------------
3% function GUI_input=aver_stat(Param)
4%
5%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
6%OUTPUT
7% GUI_input=list of options in the GUI series.fig needed for the function
8%
9%INPUT:
10% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
11% In batch mode, Param is the name of the corresponding xml file containing the same information
12% In the absence of input (as activated when the current Action is selected
13% in series), the function ouput GUI_input set the activation of the needed GUI elements
14%
15% Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param)
16%    .InputTable: cell of input file names, (several lines for multiple input)
17%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
18%    .OutputSubDir: name of the subdirectory for data outputs
19%    .OutputDir: directory for data outputs, including path
20%    .Action: .ActionName: name of the current activated function
21%             .ActionPath:   path of the current activated function
22%    .IndexRange: set the file or frame indices on which the action must be performed
23%    .FieldTransform: .TransformName: name of the selected transform function
24%                     .TransformPath:   path  of the selected transform function
25%                     .TransformHandle: corresponding function handle
26%    .InputFields: sub structure describing the input fields withfields
27%              .FieldName: name of the field
28%              .VelType: velocity type
29%              .FieldName_1: name of the second field in case of two input series
30%              .VelType_1: velocity type of the second field in case of two input series
31%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33
34function GUI_input=aver_stat(Param)
35
36%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
37if ~exist('Param','var') % case with no input parameter
38    GUI_input={'NbViewMax';2;...% max nbre of input file series (default='' , no limitation)
39        'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default)
40        'NbSlice';'on'; ...%nbre of slices ('off' by default)
41        'VelType';'two';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
42        'FieldName';'two';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
43        'FieldTransform'; 'on';...%can use a transform function
44        'ProjObject';'on';...%can use projection object(option 'off'/'on',
45        'Mask';'off';...%can use mask option   (option 'off'/'on', 'off' by default)
46        'OutputDirExt';'.stat';...%set the output dir extension
47               ''};
48        return
49end
50
51%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
52%% get input parameters, file names and indices
53% BATCH  case: read the xml file for batch case
54if ischar(Param) && ~isempty(find(regexp(Param,'.xml$')))
55    Param=xml2struct(Param);
56    checkrun=0;
57% RUN case: parameters introduced as the input structure Param 
58else
59    hseries=guidata(Param.hseries);%handles of the GUI series
60    WaitbarPos=get(hseries.waitbar_frame,'Position');%position of the waitbar on the GUI series
61    checkrun=1; % indicate the RUN option is used
62end
63% get the set of input file names (cell array filecell), and the lists of
64% input file or frame indices i1_series,i2_series,j1_series,j2_series
65[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
66% filecell{iview,fileindex}: cell array representing the list of file names
67%        iview: line in the table corresponding to a given file series
68%        fileindex: file index within  the file series,
69% 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
70% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
71% set of frame indices used for movie or multimage input
72if ~isempty(j1_series)
73    frame_index=j1_series;
74else
75    frame_index=i1_series;
76end
77
78%% root input file(s) and type
79RootPath=Param.InputTable(:,1);
80RootFile=Param.InputTable(:,3);
81SubDir=Param.InputTable(:,2);
82NomType=Param.InputTable(:,4);
83FileExt=Param.InputTable(:,5);
84
85% numbers of slices and file indices
86NbSlice=1;%default
87if isfield(Param.IndexRange,'NbSlice')
88    NbSlice=Param.IndexRange.NbSlice;
89end
90nbview=size(i1_series,1);%number of input file series (lines in InputTable)
91nbfield_j=size(i1_series,2); %nb of consecutive fields at each level(burst
92nbfield=nbfield_j*size(i1_series,3); %total number of files or frames
93nbfield_i=floor(nbfield/NbSlice);%total number of i indexes (adjusted to an integer number of slices)
94nbfield=nbfield_i*nbfield_j; %total number of fields after adjustement
95
96%determine the file type on each line from the first input file
97ImageTypeOptions={'image','multimage','mmreader','video'};
98NcTypeOptions={'netcdf','civx','civdata'};
99   
100for iview=1:nbview
101    if ~exist(filecell{iview,1}','file')
102        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
103        return
104    end
105    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
106    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
107    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
108end
109
110%% calibration data and timing: read the ImaDoc files
111mode=''; %default
112timecell={};
113itime=0;
114NbSlice_calib={};
115for iview=1:nbview%Loop on views
116    XmlData{iview}=[];%default
117    filebase{iview}=fullfile(RootPath{iview},RootFile{iview});
118    if exist([filebase{iview} '.xml'],'file')
119        [XmlData{iview},error]=imadoc2struct([filebase{iview} '.xml']);
120        if isfield(XmlData{iview},'Time')
121            itime=itime+1;
122            timecell{itime}=XmlData{iview}.Time;
123        end
124        if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord')
125            NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1);%nbre of slices for Zindex in phys transform
126            if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
127                msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
128            end
129        end
130    elseif exist([filebase{iview} '.civ'],'file')
131        [error,time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([filebase{iview} '.civ']);
132        itime=itime+1;
133        timecell{itime}=time;
134        XmlData{iview}.Time=time;
135        GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
136        GeometryCalib.Tx=0;
137        GeometryCalib.Ty=0;
138        GeometryCalib.Tz=1;
139        GeometryCalib.dpx=1;
140        GeometryCalib.dpy=1;
141        GeometryCalib.sx=1;
142        GeometryCalib.Cx=0;
143        GeometryCalib.Cy=0;
144        GeometryCalib.f=1;
145        GeometryCalib.kappa1=0;
146        GeometryCalib.CoordUnit='cm';
147        XmlData{iview}.GeometryCalib=GeometryCalib;
148        if error==1
149            msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
150        end
151    end
152end
153
154%% check coincidence in time for several input file series
155multitime=0;
156if isempty(timecell)
157    time=[];
158elseif length(timecell)==1
159    time=timecell{1};
160elseif length(timecell)>1
161    multitime=1;
162    for icell=1:length(timecell)
163        if ~isequal(size(timecell{icell}),size(timecell{1}))
164            msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used')
165            time=timecell{1};
166            multitime=0;
167            break
168        end
169    end
170end
171if multitime
172    for icell=1:length(timecell)
173        time(icell,:,:)=timecell{icell};
174    end
175    diff_time=max(max(diff(time)));
176    if diff_time>0
177        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
178    end   
179end
180if size(time,2) < i2_series(1,end) || size(time,3) < j2_series(1,end)% time array absent or too short in ImaDoc xml file'
181    time=[];
182end
183
184%% coordinate transform or other user defined transform
185transform_fct='';%default
186if isfield(Param,'FieldTransform')&&isfield(Param.FieldTransform,'TransformHandle')
187    transform_fct=Param.FieldTransform.TransformHandle;
188end
189%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
190 % EDIT FROM HERE
191
192%% check the validity of  input file types
193if CheckImage{1}
194    FileExtOut='.png'; % write result as .png images for image inputs
195elseif CheckNc{1}
196    FileExtOut='.nc';% write result as .nc files for netcdf inputs
197else
198    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
199    return
200end
201if nbview==2 && ~isequal(CheckImage{1},CheckImage{2})
202        msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
203    return
204end
205NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series
206if NbSlice~=nbfield_j
207    answer=msgbox_uvmat('INPUT_Y-N',['will not average slice by slice: for so cancel and set NbSlice= ' num2str(nbfield_j)]);
208    if ~strcmp(answer,'Yes')
209        return
210    end
211end
212
213%% Set field names and velocity types
214InputFields{1}=[];%default (case of images)
215if isfield(Param,'InputFields')
216    InputFields{1}=Param.InputFields;
217end
218if nbview==2
219    InputFields{2}=[];%default (case of images)
220    if isfield(Param,'InputFields')
221        InputFields{2}=Param.InputFields{1};%default
222        if isfield(Param.InputFields,'FieldName_1')
223            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
224            if isfield(Param.InputFields,'VelType_1')
225                InputFields{2}.VelType=Param.InputFields.VelType_1;
226            end
227        end
228    end
229end
230
231%% Initiate output fields
232%initiate the output structure as a copy of the first input one (reproduce fields)
233[DataOut,ParamOut,errormsg] = read_field(filecell{1,1},FileType{1},InputFields{1},1);
234if ~isempty(errormsg)
235    msgbox_uvmat('ERROR',['error reading ' filecell{1,1} ': ' errormsg])
236    return
237end
238time_1=[];
239if isfield(DataOut,'Time')
240    time_1=DataOut.Time(1);
241end
242if CheckNc{iview}
243    if isempty(strcmp('Conventions',DataOut.ListGlobalAttribute))
244        DataOut.ListGlobalAttribute=['Conventions' DataOut.ListGlobalAttribute];
245    end
246    DataOut.Conventions='uvmat';
247    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {Param.Action}];
248    ActionKey='Action';
249    while isfield(DataOut,ActionKey)
250        ActionKey=[ActionKey '_1'];
251    end
252    DataOut.(ActionKey)=Param.Action;
253    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {ActionKey}];
254    if isfield(DataOut,'Time')
255        DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Time','Time_end'}];
256    end
257end
258
259%% MAIN LOOP ON SLICES
260%%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
261for i_slice=1:NbSlice
262    index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
263    nbfiles=0;
264    nbmissing=0;
265   
266   %initiate result fields
267   for ivar=1:length(DataOut.ListVarName)
268       DataOut.(DataOut.ListVarName{ivar})=0; % initialise all fields to zero
269   end
270
271    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
272    for index=index_slice
273        if checkrun
274            update_waitbar(hseries.waitbar_frame,WaitbarPos,index/(nbfield))
275            stopstate=get(hseries.RUN,'BusyAction');
276        else
277            stopstate='queue';
278        end
279       
280        %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
281        for iview=1:nbview
282            % reading input file(s)
283            [Data{iview},ParamOut,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index(iview,index));
284            if ~isempty(errormsg)
285                errormsg=['error of input reading: ' errormsg];
286                break
287            end
288            if ~isempty(NbSlice_calib)
289                Data{iview}.ZIndex=mod(i1_series(iview,index)-1,NbSlice_calib{1})+1;%Zindex for phys transform
290            end
291        end
292        Field=[]; % initiate the current input field structure
293        %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
294        %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
295        % EDIT FROM HERE
296
297        if isempty(errormsg)     
298            % coordinate transform (or other user defined transform)
299            if ~isempty(transform_fct)
300                if nbview==2
301                    [Data{1},Data{2}]=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
302                    if isempty(Data{2})
303                        Data(2)=[];
304                    end
305                else
306                    Data{1}=transform_fct(Data{1},XmlData{1});
307                end
308            end
309           
310            % field calculation (vort, div...)
311            if strcmp(FileType{1},'civx')||strcmp(FileType{1},'civ')
312                Data{1}=calc_field(InputFields{1}.FieldName,Data{1});%calculate field (vort..)
313            end
314           
315            % field substration (for two input file series)
316            if length(Data)==2
317                if strcmp(FileType{2},'civx')||strcmp(FileType{2},'civ')
318                    Data{2}=calc_field(InputFields{2}.FieldName,Data{2});%calculate field (vort..)
319                end
320                [Field,errormsg]=sub_field(Data{1},Data{2}); %substract the two fields
321                if ~isempty(errormsg)
322                    msgbox_uvmat('ERROR',['error in aver_stat/sub_field:' errormsg])
323                    return
324                end
325            else
326                Field=Data{1};
327            end
328           
329            %field projection on an object
330            if Param.CheckObject
331                [Field,errormsg]=proj_field(Field,ProjObject);
332                if ~isempty(errormsg)
333                    msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
334                    return
335                end
336            end
337            nbfiles=nbfiles+1;
338           
339            %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
340            %update sum
341            for ivar=1:length(Field.ListVarName)
342                VarName=Field.ListVarName{ivar};
343                sizmean=size(DataOut.(VarName));
344                siz=size(Field.(VarName));
345                if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
346                    msgbox_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'])
347                    return
348                else
349                    DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
350                end
351            end
352            %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
353        else
354            display(errormsg) 
355        end
356    end
357    %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
358   
359    for ivar=1:length(Field.ListVarName)
360        VarName=Field.ListVarName{ivar};
361        DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
362    end
363    if nbmissing~=0
364        msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
365    end
366    if isempty(time) % time read from files  prevails
367        if isfield(Field,'Time')
368            time_end=Field.Time(1);%last time read
369            if ~isempty(time_1)
370                DataOut.Time=time_1;
371                DataOut.Time_end=time_end;
372            end
373        end
374    else  % time from ImaDoc prevails
375        DataOut.Time=time(1,i1_series(1,1),j1_series(1,1));
376        DataOut.Time_end=time(end,i1_series(end,end),j1_series(end,end));
377    end
378   
379    %writing the result file
380    OutputFile=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series(1,1),i1_series(1,end),i_slice,[]);
381    if CheckImage{1} %case of images
382        if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16))
383            DataOut.A=uint16(DataOut.A);
384            imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images
385        else
386            DataOut.A=uint8(DataOut.A);
387            imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
388        end
389        display([OutputFile ' written']);
390    else %case of netcdf input file , determine global attributes
391        errormsg=struct2nc(OutputFile,DataOut); %save result file
392        if isempty(errormsg)
393            display([OutputFile ' written']);
394        else
395            msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
396            display(errormsg)
397        end
398    end  % end averaging  loop
399end
400%%%%%%%%%%%%%%%% end loop on slices %%%%%%%%%%%%%%%%
401
402%% open the result file with uvmat (in RUN mode)
403if checkrun
404    hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
405    delete(hget_field)
406    uvmat(OutputFile)% open the last result file with uvmat
407end
Note: See TracBrowser for help on using the repository browser.