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

Last change on this file since 127 was 127, checked in by sommeria, 13 years ago

merge_proj.m: cleaning
sub_field.m: ?
dataview.m: modifs not finished
name-generator: bug repair for new nom_type
nc2struct: cleaning
uvmat: cleaning
read_civxdata: transform all variables to double
civ: bug repair for new nom_type, not finished

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