source: trunk/src/series/merge_proj.m @ 93

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

FFT: improved to deal with NaN data
merge_proj: corrected to reproduce dt if unique
uvmat.fig: tooltip corrected
plot_field: bug for isocontour corrected,
im_filter: cleaning
phys_polar: spatial derivative included (still to check)
set_obeject.fig: minor correction
struct2nc: comments improved
uvmat: button NB implemented
read_civxdata: error message improved

File size: 17.2 KB
RevLine 
[89]1function GUI_input=merge_proj(num_i1,num_i2,num_j1,num_j2,Series)
[29]2
3%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
4if ~exist('num_i1','var')
5    GUI_input={'RootPath';'two';...%nbre of possible input series (options 'on'/'two'/'many', default:'one')
6        'SubDir';'on';... % subdirectory of derived files (PIV fields), ('on' by default)
7        'RootFile';'on';... %root input file name ('on' by default)
8        'FileExt';'on';... %input file extension ('on' by default)
9        'NomType';'on';...%type of file indexing ('on' by default)
10        'NbSlice';'on'; ...%nbre of slices ('off' by default)
11        'VelTypeMenu';'one';...% menu for selecting the velocity type (civ1,..) options 'off'/'one'/'two', 'off' by default)
12        'FieldMenu';'one';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
13        'CoordType';'on';...%can use a transform function 'off' by default
14        'GetObject';'on';...%can use projection object ,'off' by default
15        %'GetMask';'on'...%can use mask option   ,'off' by default
16        %'PARAMETER'; options: name of the user defined parameter',repeat a line for each parameter
17               ''};
18    return %exit the function
19end
20
21%-------------------------------------------------
22hseries=guidata(Series.hseries);%handles of the GUI series
23WaitbarPos=get(hseries.waitbar_frame,'Position'); %positiopn of waitbar frame
24%-------------------------------------------------
25
[41]26%projection object
27test_object=get(hseries.GetObject,'Value');
28if test_object
[76]29    hset_object=findobj(allchild(0),'tag','set_object');
[41]30    ProjObject=read_set_object(guidata(hset_object));
31    if ~isfield(ProjObject,'Style')
32            msgbox_uvmat('ERROR','Undefined projection object style')
33            return
34    end
35    if ~isequal(ProjObject.Style,'plane')
36            msgbox_uvmat('ERROR','The projection object must be a plane')
37            return
38    end
39    %answeryes=questdlg({['field series projected on ' Series.ProjObject.Style]});
40    answeryes=msgbox_uvmat('INPUT_Y-N',['field series projected on ' ProjObject.Style]);
41    if ~isequal(answeryes,'Yes')
42        return
43    end
44end
45
[29]46%numbers of view fields (nbre of inputs in RootPath)
47testcell=iscell(Series.RootFile);
48if ~testcell
49    Series.RootPath={Series.RootPath};
50    Series.RootFile={Series.RootFile};
51    Series.SubDir={Series.SubDir};
52    Series.FileExt={Series.FileExt};
53    Series.NomType={Series.NomType};
54    num_i1={num_i1};
55    num_i2={num_i2};
56    num_j1={num_j1};
57    num_j2={num_j2};
58end
59nbview=length(Series.RootFile);%number of views (file series to merge)
60nbfield=size(num_i1{1},1)*size(num_i1{1},2);%number of fields in the time series
61transform=Series.CoordType; %  field transform function
62hhh=which('mmreader');
63for iview=1:nbview
64    test_movie(iview)=0;
65    if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
66        if isequal(lower(FileExt{iview}),'.avi')
[89]67            MovieObject{iview}=mmreader(fullfile(Series.RootPath{iview},[Series.RootFile{iview} Series.FileExt{iview}]));
[29]68            test_movie(iview)=1;
69        end
70    end
71end
72
73%Calibration data and timing: read the ImaDoc files
74mode=''; %default
75timecell={};
76itime=0;
77NbSlice_calib={}; %test for z index
78for iview=1:nbview%Loop on views
79    XmlData{iview}=[];%default
80    filebase{iview}=fullfile(Series.RootPath{iview},Series.RootFile{iview});
81    if exist([filebase{iview} '.xml'],'file')
82        [XmlData{iview},error]=imadoc2struct([filebase{iview} '.xml']);
83        if isfield(XmlData{iview},'Time')
84            itime=itime+1;
85            timecell{itime}=XmlData{iview}.Time;
86        end
87        if isfield(XmlData{iview},'GeometryCalib') && isfield(XmlData{iview}.GeometryCalib,'SliceCoord')
88            NbSlice_calib{iview}=size(XmlData{iview}.GeometryCalib.SliceCoord,1);
89            if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
90                msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
91            end
92        end   
93    elseif exist([filebase{iview} '.civ'],'file')
94        [error,time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([filebase{iview} '.civ']);
95        itime=itime+1;
96        timecell{itime}=time;
97        XmlData{iview}.Time=time;
98        GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
99        GeometryCalib.Tx=0;
100        GeometryCalib.Ty=0;
101        GeometryCalib.Tz=1;
102        GeometryCalib.dpx=1;
103        GeometryCalib.dpy=1;
104        GeometryCalib.sx=1;
105        GeometryCalib.Cx=0;
106        GeometryCalib.Cy=0;
107        GeometryCalib.f=1;
108        GeometryCalib.kappa1=0;
109        GeometryCalib.CoordUnit='cm';
110        XmlData{iview}.GeometryCalib=GeometryCalib;
111        if error==1
112            msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
113        end
114    end
115end
116
117%check coincidence in time
118multitime=0;
[89]119if isempty(timecell)
[29]120    time=[];
121elseif length(timecell)==1
122    time=timecell{1};
123elseif length(timecell)>1
124    multitime=1;
125    for icell=1:length(timecell)
126        if ~isequal(size(timecell{icell}),size(timecell{1}))
127            msgbox_uvmat('WARNING','inconsistent time array dimensions in ImaDoc fields, the time for the first series is used')
128            time=timecell{1};
129            multitime=0;
130            break
131        end
132    end
133end
134if multitime
135    for icell=1:length(timecell)
136        time(icell,:,:)=timecell{icell};
137    end
138    diff_time=max(max(diff(time)));
139    if diff_time>0
140        msgbox_uvmat('WARNING',['times of series differ by more than ' num2str(diff_time)])
141    end   
142end
143if size(time,2) < num_i2{1}(end) || size(time,3) < num_j2{1}(end)% ime array absent or too short in ImaDoc xml file'
144    time=[];
145end
146
[41]147% coordinate transform or other user defined transform
148transform_fct=[];%default
149if isfield(Series,'transform_fct')
150    transform_fct=Series.transform_fct;
151end
152
[29]153% Field and velocity type (the same for all views)
154Field_str=get(hseries.FieldMenu,'String');
155val=get(hseries.FieldMenu,'Value');
156FieldName=Field_str(val);%the same set of fields for all views
157VelType_str=get(hseries.VelTypeMenu,'String');
158VelType_val=get(hseries.VelTypeMenu,'Value');
159VelType=VelType_str{VelType_val}; %the same for all views
160if isequal(FieldName,'get_field...')
161    hget_field=findobj(allchild(0),'Name','get_field');%find the get_field... GUI
162    SubField=get_field('read_get_field',hObject,eventdata,hget_field); %read the names of the variables to plot in the get_field GUI
163end
164%detect whether all the files are 'images' or 'netcdf'
165testima=0;
166testvol=0;
167testcivx=0;
168testnc=0;
169FileExt=get(hseries.FileExt,'String');
170for iview=1:nbview
171     ext=FileExt{iview};
[89]172     form=imformats(ext(2:end));
[29]173     if isequal(lower(ext),'.vol')
174         testvol=testvol+1;
175     elseif ~isempty(form)||isequal(lower(ext),'.avi')% if the extension corresponds to an image format recognized by Matlab
176         testima=testima+1;
177     elseif isequal(ext,'.nc')
178         testnc=testnc+1;
179     end
180end
181if testvol
182    msgbox_uvmat('ERROR','volume images not implemented yet')
183    return
184end
185if testnc~=nbview && testima~=nbview && testvol~=nbview
186    msgbox_uvmat('ERROR','need a set of images or a set of netcdf files with the same fields as input')
187    return
188end
189if ~isequal(FieldName,'get_field...')
190    testcivx=testnc;
191end
[41]192
[29]193%name of output files and directory:
194% res_subdir=fullfile(Series.RootPath{1},[Series.SubDir{1} '_STAT']);
195ProjectDir=fileparts(fileparts(Series.RootPath{1}));% preoject directory (GERK)
196prompt={['result directory (in' ProjectDir ')']};
197RootPath=get(hseries.RootPath,'String');
198SubDir=get(hseries.SubDir,'String');
199if isequal(length(RootPath),1)
200    fulldir=RootPath{1};
[41]201    subdir='merge_proj';
[29]202    res_subdir=fullfile(fulldir,subdir);
203else
204    def={fullfile(ProjectDir,'0_RESULTS')};
205    dlgTitle='result directory';
206    lineNo=1;
207    answer=msgbox_uvmat('INPUT_TXT',dlgTitle,def);
208    fulldir=answer{1};
209    subdir=[];
210    dirlist=sort(Series.RootFile);
211    for iview=1:nbview
212        if ~isempty(subdir)
213            subdir=[subdir '-'];
214        end
215        subdir=[subdir dirlist{iview}];
216    end 
217    res_subdir=fullfile(fulldir,subdir);
218end
219ext=FileExt{1};
220if ~exist(fulldir,'dir')
221    msgbox_uvmat('ERROR',['directory ' fulldir ' needs to be created'])
222    return
223end
224if ~exist(res_subdir,'dir')
225    dircur=pwd;
226    cd(fulldir)
227    error=mkdir(subdir);
228    cd(dircur)
229end
230filebasesub=fullfile(res_subdir,Series.RootFile{1});
231filebase_merge=fullfile(res_subdir,'merged');%root name for the merged files
232
233    %MAIN LOOP
234for ifile=1:nbfield               
235    stopstate=get(hseries.RUN,'BusyAction');
236    if isequal(stopstate,'queue')% enable STOP command from the 'series' interface
237         update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield)
238         Amerge=0;
239         
240         %----------LOOP ON VIEWS----------------------
241        nbtime=0;
242        for iview=1:nbview
243            %name of the current file
244            filename=name_generator(filebase{iview},num_i1{iview}(ifile),num_j1{iview}(ifile),Series.FileExt{iview},Series.NomType{iview},1,num_i2{iview}(ifile),num_j2{iview}(ifile),SubDir{iview});
245            if ~exist(filename,'file')
246                msgbox_uvmat('ERROR',['missing input file' filename])
247                break
248            end
249
250            %reading the current file
251            if testima
252                if test_movie(iview)
253                    Field{iview}.A=read(MovieObject{iview},num_i1{iview}(ifile));
254                else
[78]255                    Field{iview}.A=imread(filename);
[29]256                end % TODO: introduce ListVarName
257                npxy=size(Field{iview}.A);
258                Field{iview}.AX=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
259                Field{iview}.AY=[npxy(1)-0.5 0.5];
260                Field{iview}.CoordType='px';
261                Field{iview}.AName='image';
262            else
263                if testcivx
264                    [Field{iview},VelTypeOut]=read_civxdata(filename,FieldName,VelType);
265                else
266                    [Field{iview},var_detect]=nc2struct(filename,SubField.ListVarName); %read the corresponding input data               
267                    Field{iview}.VarAttribute=SubField.VarAttribute;
268                end
[93]269                if isfield(Field{iview},'Txt')
270                    msgbox_uvmat('ERROR',Field{iview}.Txt)
271                    return
272                end
[29]273                if isfield(Field{iview},'Time')
274                    timeread(iview)=Field{iview}.Time;
275                    nbtime=nbtime+1;
276                end
277            end
278            % coord transform
279            % z index
280            if ~isempty(NbSlice_calib)
281                Field{iview}.ZIndex=mod(num_i1{iview}(ifile)-1,NbSlice_calib{1})+1;
282            end
[41]283            if ~isempty(transform_fct)
284                Field{iview}=transform_fct(Field{iview},XmlData{iview});%transform to phys if requested
[29]285            end
286            if testcivx
287                    Field{iview}=calc_field(FieldName,Field{iview});
288            end
289
290            %projection on object (gridded plane)
[41]291            if test_object
292                Field{iview}=proj_field(Field{iview},ProjObject);
[29]293            end
294        end   
295       
296         %----------END LOOP ON VIEWS----------------------
297         
298        %merge the nbview fields
299        MergeData=merge_field(Field);
300        if isfield(MergeData,'Txt')
301            msgbox_uvmat('ERROR',MergeData.Txt)
302            return
303        end
304       
305        % generating the name of the merged field
306        mergename=name_generator(filebase_merge,num_i1{iview}(ifile),num_j1{iview}(ifile),Series.FileExt{iview},Series.NomType{iview},1,num_i2{iview}(ifile),num_j2{iview}(ifile));
307       
308        % time:
309        time_i=0;%default
310        if isempty(time)% time from ImaDoc prevails
311            time_i=sum(timeread)/nbtime;
312        else
313            time_i=(time(iview,num_i1{iview}(ifile),num_j1{iview}(ifile))+time(iview,num_i2{iview}(ifile),num_j2{iview}(ifile)))/2;
314        end
315       
316        % recording the merged field
317        if testima    %in case of input images an image is produced   
318            if isa(MergeData.A,'uint8')
319                bitdepth=8;
320            elseif isa(MergeData.A,'uint16')
321                bitdepth=16;
322            end
323            imwrite(MergeData.A,mergename,'BitDepth',bitdepth);
324            %write xml calibration file
325            siz=size(MergeData.A);
326            npy=siz(1);
327            npx=siz(2);
328            if isfield(MergeData,'VarAttribute')&&isfield(MergeData.VarAttribute{1},'Coord_2')&&isfield(MergeData.VarAttribute{1},'Coord_1')
329                Rangx=MergeData.VarAttribute{1}.Coord_2;
330                Rangy=MergeData.VarAttribute{1}.Coord_1;
331            elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY')
332                Rangx=[MergeData.AX(1) MergeData.AX(end)];
333                Rangy=[MergeData.AY(1) MergeData.AY(end)];
334            else
335                Rangx=[0.5 npx-0.5];
336                Rangy=[npy-0.5 0.5];%default
337            end
338            pxcmx=(npx-1)/(Rangx(2)-Rangx(1));
339            pxcmy=(npy-1)/(Rangy(1)-Rangy(2));
340            T_x=-pxcmx*Rangx(1)+0.5;
341            T_y=-pxcmy*Rangy(2)+0.5;
342            GeometryCal.focal=1;
343            GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1];
344            GeometryCal.Tx_Ty_Tz=[T_x T_y 1];
345            ImaDoc.GeometryCalib=GeometryCal;
346            t=struct2xml(ImaDoc);
347            t=set(t,1,'name','ImaDoc');
348            save(t,[filebase_merge '.xml'])     
349            display([filebase_merge '.xml saved'])
350        else
351            MergeData.ListGlobalAttribute={'Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'};       
352            MergeData.nb_coord=2;
353            MergeData.nb_dim=2;
[93]354            dt=[];
355            if isfield(Field{1},'dt')&& isnumeric(Field{1}.dt)
356                dt=Field{1}.dt;
357            end
358            for iview =2:numel(Field)
359                if ~(isfield(Field{iview},'dt')&& isequal(Field{iview}.dt,dt))
360                    dt=[];%dt not the same for all fields
361                end
362            end
363            if isempty(dt)
364                MergeData.ListGlobalAttribute(6)=[];
365            else
366               MergeData.dt=dt;
367            end
368            %MergeData.dt=1;
[29]369            MergeData.Time=time_i;
370            error=struct2nc(mergename,MergeData); %save result file
371            if isempty(error)
372                display(['output file ' mergename ' written'])
373            else
374                display(error)
375            end
376        end
377    end
378end
379
380%--------------------------------------------------------------------------   
381function MergeData=merge_field(Data)
382% initiate Matlab  structure for physical field
383if isempty(Data)||~iscell(Data)
384    MergeData=[];
385    return
386end
387MergeData=Data{1};%default
388error=0;
389nbview=length(Data);
390if nbview==1
391    return
392end
393for iview=1:nbview
394    if ~isequal(MergeData.ListDimName,Data{iview}.ListDimName)
395        error=1;
396    end
397    if ~isequal(MergeData.ListVarName,Data{iview}.ListVarName)
398        error=1;
399    end
400end
401if error
402    MergeData.Txt='ERROR: attempt at merging fields of incompatible type';
403    return
404end
[89]405
[93]406
407
408
[89]409%group the variables (fields of 'FieldData') in cells of variables with the same dimensions
410[CellVarIndex,NbDim,VarTypeCell]=find_field_indices(Data{1});
411%LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS
412% CellVarIndex=cells of variable index arrays
413ivar_new=0; % index of the current variable in the projected field
414icoord=0;
415for icell=1:length(CellVarIndex)
416    if NbDim(icell)==1
417        continue
418    end
419    VarIndex=CellVarIndex{icell};%  indices of the selected variables in the list FieldData.ListVarName
420    VarType=VarTypeCell{icell};
421    ivar_X=VarType.coord_x;
422    ivar_Y=VarType.coord_y;
423    ivar_FF=VarType.errorflag;
424    if isempty(ivar_X)
425        test_grid=1;%test for input data on regular grid (e.g. image)coordinates
426    else
427        if length(ivar_Y)~=1
428                msgbox_uvmat('ERROR','y coordinate missing in proj_field.m')
429                return
430        end
431        test_grid=0;
432    end
433    %case of input fields with unstructured coordinates
434    if ~test_grid
435        for ivar=VarIndex
436            VarName=MergeData.ListVarName{ivar};
437            for iview=1:nbview
438                eval(['MergeData.' VarName '=[MergeData.' VarName '; Data{iview}.' VarName '];'])
439            end
440        end
441    %case of fields defined on a structured  grid
442    else 
443        testFF=0;
444        for iview=2:nbview
445            for ivar=VarIndex
446                VarName=MergeData.ListVarName{ivar};
447                if isfield(MergeData,'VarAttribute')
448                    if length(MergeData.VarAttribute)>=ivar && isfield(MergeData.VarAttribute{ivar},'Role') && isequal(MergeData.VarAttribute{ivar}.Role,'errorflag')
449                        testFF=1;
450                    end
451                end
452                eval(['MergeData.' VarName '=MergeData.' VarName '+ Data{iview}.' VarName ';'])
453            end
454        end
455        if testFF
456            nbaver=nbview-MergeData.FF;
457            indgood=find(nbaver>0);
458            for ivar=VarIndex
459                VarName=MergeData.ListVarName{ivar};
460                eval(['MergeData.' VarName '(indgood)=double(MergeData.' VarName '(indgood))./nbaver(indgood);'])
461            end
462        else
463            for ivar=VarIndex
464                VarName=MergeData.ListVarName{ivar};
465                eval(['MergeData.' VarName '=double(MergeData.' VarName ')./nbview;'])
466            end   
467        end
468    end
469end
470   
Note: See TracBrowser for help on using the repository browser.