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

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

cleaning of merge_proj

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