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

Last change on this file since 41 was 41, checked in by sommeria, 14 years ago

-relabel_i_j: improve the question dlg box for image transform
-aver_stat,time_series,merge_proj: include modification of field transform and call of set_object.
-series: introduce a browser for set_object
-ima2vol: create volume images (png images of a slice concacened along y) for PIV 3D
-set_object: suppress TITLE, introduce Tooltip helps for parameters
-civ: fixed bug on BATCH sequences

File size: 18.1 KB
Line 
1function GUI_input=aver_stat(num_i1,num_i2,num_j1,num_j2,Series)
2%----------------------------------------------------------------------
3% --- make average on a series of files
4%----------------------------------------------------------------------
5%INPUT:
6%num_i1: series of first indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
7%num_i2: series of second indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
8%num_j1: series of first indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ )
9%num_j2: series of second indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ)
10%OTHER INPUTS given by the structure Series
11%  Series.Time:
12%  Series.GeometryCalib:%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
13if ~exist('num_i1','var')
14    GUI_input={'RootPath';'two';...%nbre of possible input series (options 'on'/'two'/'many', default:'one')
15        'SubDir';'on';... % subdirectory of derived files (PIV fields), ('on' by default)
16        'RootFile';'on';... %root input file name ('on' by default)
17        'FileExt';'on';... %input file extension ('on' by default)
18        'NomType';'on';...%type of file indexing ('on' by default)
19        'NbSlice';'on'; ...%nbre of slices ('off' by default)
20        'VelTypeMenu';'two';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
21        'FieldMenu';'two';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
22        'CoordType'; 'on';...%can use a transform function
23        'GetObject';'on';...%can use projection object(option 'off'/'one'/'two',
24        %'GetMask';'on'...%can use mask option   
25        %'PARAMETER'; %options: name of the user defined parameter',repeat a line for each parameter
26               ''};
27        return
28end
29
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31hseries=guidata(Series.hseries);%handles of the GUI series
32WaitbarPos=get(hseries.waitbar_frame,'Position');
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35%projection object
36test_object=get(hseries.GetObject,'Value');
37if test_object%isfield(Series,'sethandles')
38    hset_object=findobj(allchild(0),'Name','set_object');
39    ProjObject=read_set_object(guidata(hset_object));
40    %answeryes=questdlg({['field series projected on ' Series.ProjObject.Style]});
41    answeryes=msgbox_uvmat('INPUT_Y-N',['field series projected on ' ProjObject.Style ' before averaging']);
42    if ~isequal(answeryes,'Yes')
43        return
44    end
45end
46
47%root input file and type
48if ~iscell(Series.RootPath)% case of a single input field series
49    num_i1={num_i1};num_j1={num_j1};num_i2={num_i2};num_j2={num_j2};
50    RootPath={Series.RootPath};
51    RootFile={Series.RootFile};
52    SubDir={Series.SubDir};
53    FileExt={Series.FileExt};
54    NomType={Series.NomType};
55else
56    RootPath=Series.RootPath;
57    RootFile=Series.RootFile;
58    SubDir=Series.SubDir;
59    NomType=Series.NomType;
60    FileExt=Series.FileExt;
61end   
62ext=FileExt{1};
63form=imformats(ext([2:end]));%test valid Matlab image formats
64testima=0;
65if ~isempty(form)||isequal(lower(ext),'.avi')||isequal(lower(ext),'.vol')
66    testima(1)=1;
67end
68if length(FileExt)>=2
69    ext_1=FileExt{2};
70    form=imformats(ext_1([2:end]));%test valid Matlab image formats
71    if ~isempty(form)||isequal(lower(ext_1),'.avi')||isequal(lower(ext_1),'.vol')
72        testima(2)=1;
73    end
74    if testima(2)~=testima(1)
75        msgbox_uvmat('ERROR','images and netcdf files cannot be compared')
76        return
77    end
78end
79
80%Number of input series: this function  accepts two input file series at most (then it operates on the difference of fields)
81nbview=length(RootPath);
82if nbview>2 
83    RootPath=RootPath(1:2);
84    set(hseries.RootPath,'String',RootPath)
85    SubDir=SubDir(1:2);
86    set(hseries.SubDir,'String',SubDir)
87    RootFile=RootFile(1:2);
88    set(hseries.RootFile,'String',RootFile)
89    NomType=NomType(1:2);
90    FileExt=FileExt(1:2);
91    set(hseries.FileExt,'String',FileExt)
92    nbview=2;
93end
94hhh=which('mmreader');
95for iview=1:nbview
96    test_movie(iview)=0;
97    if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
98        if isequal(lower(FileExt{iview}),'.avi')
99            MovieObject{iview}=mmreader(fullfile(RootPath{iview},[RootFile{iview} FileExt{iview}]));
100            test_movie(iview)=1;
101        end
102    end
103end
104
105% number of slices
106NbSlice=str2num(get(hseries.NbSlice,'String'));
107if isempty(NbSlice)
108    NbSlice=1;
109end
110NbSlice_name=num2str(NbSlice);
111
112% Field and velocity type (the same for the two views)
113Field_str=get(hseries.FieldMenu,'String');
114FieldName=[]; %default
115testfield=get(hseries.FieldMenu,'Visible');
116if isequal(testfield,'on')
117    val=get(hseries.FieldMenu,'Value');
118    FieldName=Field_str(val);%the same set of fields for all views
119    if isequal(FieldName,{'get_field...'})
120        hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
121        if length(hget_field)>1
122            delete(hget_field(2:end))
123        elseif isempty(hget_field)
124           filename=...
125                 name_generator(fullfile(RootPath{1},RootFile{1}),num_i1{1}(1),num_j1{1}(1),FileExt{1},NomType{1},1,num_i2{1}(1),num_j2{1}(1),SubDir{1});
126           get_field(filename);
127           return
128        end
129        %hhget_field=guidata(hget_field);%handles of GUI elements in get_field
130        SubField=read_get_field(hget_field); %read the names of the variables to plot in the get_field GUI
131    end
132end
133%detect whether the two files are 'images' or 'netcdf'
134testima=0;
135testvol=0;
136testcivx=0;
137testnc=0;
138FileExt=get(hseries.FileExt,'String');
139% test_movie=0;
140for iview=1:nbview
141     ext=FileExt{iview};
142     form=imformats(ext([2:end]));
143     if isequal(lower(ext),'.vol')
144         testvol=testvol+1;
145     elseif ~isempty(form)||isequal(lower(ext),'.avi')% if the extension corresponds to an image format recognized by Matlab
146         testima=testima+1;
147     elseif isequal(ext,'.nc')
148         testnc=testnc+1;
149     end
150end
151if testvol
152    msgbox_uvmat('ERROR','volume images not implemented yet')
153    return
154end
155if testnc~=nbview && testima~=nbview && testvol~=nbview
156    msgbox_uvmat('ERROR','compare two image series or two netcdf files with the same fields as input')
157    return
158end
159if ~isequal(FieldName,{'get_field...'})
160    if isequal(FieldName,{''}) && ~testima
161        msgbox_uvmat('ERROR','an input field needs to be selected')
162        return
163    end
164    testcivx=testnc;
165end
166
167if testcivx
168    VelType_str=get(hseries.VelTypeMenu,'String');
169    VelType_val=get(hseries.VelTypeMenu,'Value');
170    VelType{1}=VelType_str{VelType_val};
171    if nbview==2
172        VelType_str=get(hseries.VelTypeMenu_1,'String');
173        VelType_val=get(hseries.VelTypeMenu_1,'Value');
174        VelType{2}=VelType_str{VelType_val};
175    end
176end
177
178%Calibration data and timing: read the ImaDoc files
179mode=''; %default
180timecell={};
181itime=0;
182NbSlice_calib={};
183for iview=1:nbview%Loop on views
184    XmlData{iview}=[];%default
185    filebase{iview}=fullfile(RootPath{iview},RootFile{iview});
186    if exist([filebase{iview} '.xml'],'file')
187        [XmlData{iview},error]=imadoc2struct([filebase{iview} '.xml']);
188        if isfield(XmlData{iview},'Time')
189            itime=itime+1;
190            timecell{itime}=XmlData{iview}.Time;
191        end
192        if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord')
193            NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1);%nbre of slices for Zindex in phys transform
194            if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
195                msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
196            end
197        end
198    elseif exist([filebase{iview} '.civ'],'file')
199        [error,time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([filebase{iview} '.civ']);
200        itime=itime+1;
201        timecell{itime}=time;
202        XmlData{iview}.Time=time;
203        GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
204        GeometryCalib.Tx=0;
205        GeometryCalib.Ty=0;
206        GeometryCalib.Tz=1;
207        GeometryCalib.dpx=1;
208        GeometryCalib.dpy=1;
209        GeometryCalib.sx=1;
210        GeometryCalib.Cx=0;
211        GeometryCalib.Cy=0;
212        GeometryCalib.f=1;
213        GeometryCalib.kappa1=0;
214        GeometryCalib.CoordUnit='cm';
215        XmlData{iview}.GeometryCalib=GeometryCalib;
216        if error==1
217            msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
218        end
219    end
220end
221
222%check coincidence in time
223multitime=0;
224if length(timecell)==0
225    time=[];
226elseif length(timecell)==1
227    time=timecell{1};
228elseif length(timecell)>1
229    multitime=1;
230    for icell=1:length(timecell)
231        if ~isequal(size(timecell{icell}),size(timecell{1}))
232            msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used')
233            time=timecell{1};
234            multitime=0;
235            break
236        end
237    end
238end
239if multitime
240    for icell=1:length(timecell)
241        time(icell,:,:)=timecell{icell};
242    end
243    diff_time=max(max(diff(time)));
244    if diff_time>0
245        msgbox_uvmat('WARNING',['times of series differ by more than ' num2str(diff_time)])
246    end   
247end
248if size(time,2) < num_i2{1}(end) || size(time,3) < num_j2{1}(end)% ime array absent or too short in ImaDoc xml file'
249    time=[];
250end
251
252% Root name of output files (TO GENERALISE FOR TWO INPUT SERIES)
253subdir_result='aver_stat';
254if ~exist(fullfile(RootPath{1},subdir_result),'dir')
255    dircur=pwd; %record current working directory
256    cd(RootPath{1})% goes to the iamge directory
257    [m1,m2,m3]=mkdir(subdir_result);
258    if ~isequal(m2,'')
259         msgbox_uvmat('CONFIRMATION',m2);%error message for directory creation
260    end
261    cd(dircur) %back to the initial working directory
262end
263filebase_out=filebase{1};
264NomTypeOut=nomtype2pair(NomType{1},num_i2{end}(end)-num_i1{1}(1),num_j2{end}(end)-num_j1{1}(1));
265
266% coordinate transform or other user defined transform
267transform_fct=[];%default
268if isfield(Series,'transform_fct')
269    transform_fct=Series.transform_fct;
270end
271
272%slice loop
273siz=size(num_i1{1});
274lengthtot=siz(1)*siz(2);
275nbfield=floor(lengthtot/(siz(1)*NbSlice));%total number of i indexes (adjusted to an integer number of slices)
276nbfield_slice=nbfield*siz(1);% number of fields per slice
277
278for i_slice=1:NbSlice
279   S=0; %initiate the image sum S
280   nbfiles=0;
281   nbmissing=0;
282    %averaging loop
283   for ifile=i_slice:NbSlice:lengthtot
284        stopstate=get(hseries.RUN,'BusyAction');
285        if isequal(stopstate,'queue') % enable STOP command
286             update_waitbar(hseries.waitbar,WaitbarPos,ifile/lengthtot)
287             for iview=1:nbview
288                [filename]=...
289                           name_generator(filebase{iview},num_i1{iview}(ifile),num_j1{iview}(ifile),FileExt{iview},NomType{iview},1,num_i2{iview}(ifile),num_j2{iview}(ifile),SubDir{iview});
290                if testima
291                    Data{iview}.ListVarName={'A'};
292                    Data{iview}.AName='image';
293                    if test_movie(iview)
294                        Data{iview}.A=read(MovieObject{iview},num_i1{iview}(ifile));
295                    else
296                        Data{iview}.A=read_image(filename,NomType{iview},num_i1{iview}(ifile));% read the image, num2 is the counter for avi files
297                    end
298                    Atype{iview}=class(Data{iview}.A);
299                    Data{iview}.A=double(Data{iview}.A);
300                elseif testcivx
301                    [Data{iview},VelTypeOut]=read_civxdata(filename,FieldName,VelType);
302                else
303                    [Data{iview},var_detect]=nc2struct(filename,SubField.ListVarName); %read the corresponding input data               
304                    Data{iview}.VarAttribute=SubField.VarAttribute;
305                end
306                if isfield(Data{iview},'Txt')
307                    msgbox_uvmat('ERROR',['error of input reading: ' Data{iview}.Txt])
308                    return
309                end
310             end   
311             % coordinate transform (or other user defined transform)
312             if ~isempty(transform_fct)
313                 % z index
314                if ~isempty(NbSlice_calib)
315                    Data{iview}.ZIndex=mod(num_i1{iview}(ifile)-1,NbSlice_calib{1})+1;%Zindex for phys transform
316                end
317                if nbview==2
318                    [Data{1},Data{2}]=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
319                    if isempty(Data{2})
320                        Data(2)=[];
321                    end
322                else
323                    Data{1}=transform_fct(Data{1},XmlData);
324                end
325             end     
326            if testcivx
327                    Data{iview}=calc_field(FieldName,Data{iview});%calculate field (vort..)
328            end
329            if length(Data)==2
330                [Field,errormsg]=sub_field(Data{1},Data{2}); %substract the two fields
331                if ~isempty(errormsg)
332                    msgbox_uvmat('ERROR',['error in aver_stat/sub_field:' errormsg])
333                    return
334                end
335            else
336                Field=Data{1};
337            end
338            if test_object
339                [Field,errormsg]=proj_field(Field,ProjObject);
340                 if ~isempty(errormsg)
341                    msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
342                    return
343                end
344             end                                                       
345                nbfiles=nbfiles+1;
346                if nbfiles==1 %first field
347                    time_1=[];
348                    if isfield(Field,'Time')
349                        time_1=Field.Time(1);
350                    end
351                    DataMean=Field;%default
352                else
353                    for ivar=1:length(Field.ListVarName)
354                        VarName=Field.ListVarName{ivar};
355                        eval(['sizmean=size(DataMean.' VarName ');']);
356                        eval(['siz=size(Field.' VarName ');']);
357                        if ~isequal(siz,sizmean)
358                            msgbox_uvmat('WARNING',['unequal size of input field ' VarName ', need to interpolate on a grid'])
359                            nbmissing=nbmissing+1;
360                            break
361                        else
362                            eval(['DataMean.' VarName '=DataMean.' VarName '+ Field.' VarName ';']); % update the sum
363                        end
364                    end
365                end
366%             else
367%                 nbmissing=nbmissing+1;
368%             end
369        end
370    end %end averaging loop
371    for ivar=1:length(Field.ListVarName)
372        VarName=Field.ListVarName{ivar};
373        eval(['DataMean.' VarName '=DataMean.' VarName '/nbfiles;']); % normalize the mean
374    end
375    if nbmissing~=0
376        msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
377    end
378    if isempty(time) % time read from files  prevails
379        time_end=[];
380        if isfield(Field,'Time')
381            time_end=Field.Time(1);%last time read
382            if ~isempty(time_1)
383                DataMean.Time=time_1;
384                DataMean.Time_end=time_end;
385            end
386        end
387    else  % time from ImaDoc prevails
388        DataMean.Time=time(1,num_i1{1}(1),num_j1{1}(1));
389        DataMean.Time_end=time(end,num_i1{end}(end),num_j1{end}(end));
390    end
391   
392    %writing the result file
393   if testima   
394%        if NbSlice==1
395        [filemean]=name_generator(filebase_out,num_i1{1}(1),num_j1{1}(1),'.png',NomTypeOut,1,num_i2{end}(end),num_j2{end}(end),subdir_result);
396%        else % label the file number by the slice # for simplicity
397%           [filemean]=name_generator(filebase_out,i_slice,1,'.png','_i');
398%        end
399        if exist(filemean,'file')
400            backupfile=filemean;
401            testexist=2;
402            while testexist==2
403                backupfile=[backupfile(1:end-4) '~.png'];
404                testexist=exist(backupfile,'file');
405            end
406            [success,message]=copyfile(filemean,backupfile);%make backup
407            if ~isequal(success,1)
408                msgbox_uvmat('ERROR',['previous file result ' filemean ' already exists, problem in backup'])
409                return
410            end
411        end
412        if isequal(Atype{1},'uint16')
413            imwrite(uint16(DataMean.A),filemean,'BitDepth',16);
414        else
415            imwrite(uint8(DataMean.A),filemean,'BitDepth',8);
416        end
417        display([filemean ' written']);
418    else %determine global attributes
419        DataMean.ListGlobalAttribute=[DataMean.ListGlobalAttribute {Series.Action}];
420        ActionKey='Action';
421        while isfield(DataMean,ActionKey)
422            ActionKey=[ActionKey '_1'];
423        end
424        eval(['DataMean.' ActionKey '=Series.Action;'])
425        DataMean.ListGlobalAttribute=[DataMean.ListGlobalAttribute {ActionKey}];
426        if isfield(DataMean,'Time')
427            DataMean.ListGlobalAttribute=[DataMean.ListGlobalAttribute {'Time','Time_end'}];
428        end 
429%         if NbSlice==1
430          filemean=name_generator(filebase_out,num_i1{1}(1),num_j1{1}(1),'.nc',NomTypeOut,1,num_i2{end}(end),num_j2{end}(end),subdir_result);
431%         else % label the file number by the slice # for simplicity
432%           [filemean]=name_generator(filebase_out,i_slice,1,'.nc','_i');
433%         end
434        if exist(filemean,'file')
435            backupfile=filemean;
436            testexist=2;
437            while testexist==2
438                backupfile=[backupfile(1:end-3) '~.nc'];
439                testexist=exist(backupfile,'file');
440            end
441            [success,message]=copyfile(filemean,backupfile);%make backup
442            if ~isequal(success,1)
443                msgbox_uvmat('ERROR',['previous file result ' filemean ' already exists, problem in backup'])
444                display(['previous file result ' filemean ' already exists, problem in backup'])
445                return
446            end
447        end
448        errormsg=struct2nc(filemean,DataMean); %save result file
449        if isempty(errormsg)
450            display([filemean ' written']);
451        else
452            msgbox_uvmat('ERROR',['error in writting result file: ' errormsg])
453            display(errormsg)
454        end
455   end
456end
457hget_field=findobj(allchild(0),'name','get_field');%find the get_field... GUI
458delete(hget_field)
459uvmat(filemean)
Note: See TracBrowser for help on using the repository browser.