source: trunk/src/series/sub_field_series.m @ 508

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

relabel_i_j , sub_field_series improved

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