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

Last change on this file since 626 was 626, checked in by sommeria, 11 years ago

bug with waitbar repaired

File size: 17.1 KB
Line 
1%'merge_proj': concatene several fields from series, can project them on a regular grid in phys coordinates
2%------------------------------------------------------------------------
3% function ParamOut=merge_proj(Param)
4%------------------------------------------------------------------------
5%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
6%
7%OUTPUT
8% ParamOut: sets options in the GUI series.fig needed for the function
9%
10%INPUT:
11% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
12% In batch mode, Param is the name of the corresponding xml file containing the same information
13% when Param.Action.RUN=0 (as activated when the current Action is selected
14% in series), the function ouput paramOut set the activation of the needed GUI elements
15%
16% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
17% see the current structure Param)
18%    .InputTable: cell of input file names, (several lines for multiple input)
19%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
20%    .OutputSubDir: name of the subdirectory for data outputs
21%    .OutputDirExt: directory extension for data outputs
22%    .Action: .ActionName: name of the current activated function
23%             .ActionPath:   path of the current activated function
24%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
25%             .RUN =0 for GUI input, =1 for function activation
26%             .RunMode='local','background', 'cluster': type of function  use
27%             
28%    .IndexRange: set the file or frame indices on which the action must be performed
29%    .FieldTransform: .TransformName: name of the selected transform function
30%                     .TransformPath:   path  of the selected transform function
31%    .InputFields: sub structure describing the input fields withfields
32%              .FieldName: name(s) of the field
33%              .VelType: velocity type
34%              .FieldName_1: name of the second field in case of two input series
35%              .VelType_1: velocity type of the second field in case of two input series
36%              .Coord_y: name of y coordinate variable
37%              .Coord_x: name of x coordinate variable
38%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
41function ParamOut=merge_proj(Param)
42
43%% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed
44if isstruct(Param) && isequal(Param.Action.RUN,0)
45    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
46    ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
47    ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
48    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
49    ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
50    ParamOut.FieldTransform = 'on';%can use a transform function
51    ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only)
52    ParamOut.ProjObject='on';%can use projection object(option 'off'/'on',
53    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
54    ParamOut.OutputDirExt='.mproj';%set the output dir extension
55    ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
56    filecell=get_file_series(Param);%check existence of the first input file
57    if ~exist(filecell{1,1},'file')
58        msgbox_uvmat('WARNING','the first input file does not exist')
59    elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject')
60        msgbox_uvmat('WARNING','a projection object of type plane needs to be introduced for merge_proj')
61    end
62    return
63end
64
65%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
66ParamOut=[] %default output
67%% read input parameters from an xml file if input is a file name (batch mode)
68checkrun=1;
69if ischar(Param)
70    Param=xml2struct(Param);% read Param as input file (batch case)
71    checkrun=0;
72end
73hseries=findobj(allchild(0),'Tag','series');
74RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
75WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
76
77%% define the directory for result file (with path=RootPath{1})
78OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
79
80if ~isfield(Param,'InputFields')
81    Param.InputFields.FieldName='';
82end
83
84%% root input file type
85RootPath=Param.InputTable(:,1);
86RootFile=Param.InputTable(:,3);
87SubDir=Param.InputTable(:,2);
88NomType=Param.InputTable(:,4);
89FileExt=Param.InputTable(:,5);
90[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
91%%%%%%%%%%%%
92% The cell array filecell is the list of input file names, while
93% filecell{iview,fileindex}:
94%        iview: line in the table corresponding to a given file series
95%        fileindex: file index within  the file series,
96% i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j
97% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
98%%%%%%%%%%%%
99% NbSlice=1;%default
100% if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
101%     NbSlice=Param.IndexRange.NbSlice;
102% end
103NbView=numel(i1_series);%number of input file series (lines in InputTable)
104NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
105NbField_i=size(i1_series{1},2); %nb of fields for the i index
106NbField=NbField_j*NbField_i; %total number of fields
107
108%determine the file type on each line from the first input file
109ImageTypeOptions={'image','multimage','mmreader','video'};
110NcTypeOptions={'netcdf','civx','civdata'};
111for iview=1:NbView
112    if ~exist(filecell{iview,1}','file')
113        displ_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
114        return
115    end
116    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
117    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
118    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
119    if ~isempty(j1_series{iview})
120        frame_index{iview}=j1_series{iview};
121    else
122        frame_index{iview}=i1_series{iview};
123    end
124end
125
126%% calibration data and timing: read the ImaDoc files
127[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
128if size(time,1)>1
129    diff_time=max(max(diff(time)));
130    if diff_time>0
131        displ_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)],checkrun)
132    end   
133end
134
135%% coordinate transform or other user defined transform
136% transform_fct='';%default fct handle
137% if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
138%     if isdeployed
139%         transform_fct=Param.FieldTransform.TransformName;
140%         dd=phys([]);%activate phys for compilation
141%     else
142%         currentdir=pwd;
143%         cd(Param.FieldTransform.TransformPath)
144%         transform_fct=str2func(Param.FieldTransform.TransformName);
145%         cd (currentdir)
146%     end
147% end
148transform_fct='';%default fct handle
149if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
150        currentdir=pwd;
151        cd(Param.FieldTransform.TransformPath)
152        transform_fct=str2func(Param.FieldTransform.TransformName);
153        cd (currentdir)
154end
155%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
156 % EDIT FROM HERE
157
158%% check the validity of  input file types
159if CheckImage{1}
160    FileExtOut='.png'; % write result as .png images for image inputs
161elseif CheckNc{1}
162    FileExtOut='.nc';% write result as .nc files for netcdf inputs
163else
164    displ_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
165    return
166end
167for iview=1:NbView
168        if ~isequal(CheckImage{iview},CheckImage{1})||~isequal(CheckNc{iview},CheckNc{1})
169        displ_uvmat('ERROR','input set of input series: need  either netcdf either image series',checkrun)
170    return
171    end
172end
173NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series
174% if checkrun==1
175%     ParamOut.Specific=[];%no specific parameter
176%     return %stop here for interactive input (option Param.Specific='?')
177% end
178
179%% Set field names and velocity types
180%use Param.InputFields for all views
181
182%% MAIN LOOP ON SLICES
183%%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
184% for i_slice=1:NbSlice
185%     index_slice=i_slice:NbSlice:NbField;% select file indices of the slice
186%     NbFiles=0;
187%     nbmissing=0;
188
189    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
190for index=1:NbField
191        update_waitbar(WaitbarHandle,index/NbField)
192    if ishandle(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
193        disp('program stopped by user')
194        return
195    end
196    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
197    Data=cell(1,NbView);%initiate the set Data
198    nbtime=0;
199    for iview=1:NbView
200        %% reading input file(s)
201        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index));
202        if ~isempty(errormsg)
203            disp(['ERROR in merge_proj/read_field/' errormsg])
204            return
205        end
206        timeread(iview)=0;
207        if isfield(Data{iview},'Time')
208            timeread(iview)=Data{iview}.Time;
209            nbtime=nbtime+1;
210        end
211        if ~isempty(NbSlice_calib)
212            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
213        end
214
215        %% transform the input field (e.g; phys) if requested
216        if ~isempty(transform_fct)
217            if nargin(transform_fct)>=2
218                Data{iview}=transform_fct(Data{iview},XmlData{iview});
219            else
220                Data{iview}=transform_fct(Data{iview});
221            end
222        end
223%          Data{iview}=phys(Data{iview},XmlData{iview});
224        %% check whether tps is needed, then calculate tps coefficients if needed
225        check_tps=0;
226        if isfield(Param.InputFields,'FieldName')
227            if ischar(Param.InputFields.FieldName)
228                Param.InputFields.FieldName={Param.InputFields.FieldName};
229            end
230        else
231            Param.InputFields.FieldName={};
232        end
233        for ilist=1:numel(Param.InputFields.FieldName)
234            switch Param.InputFields.FieldName{ilist}
235                case {'vort','div','strain'}
236                    check_tps=1;
237            end
238        end
239
240        %% calculate tps coeff if needed
241        check_proj_tps= isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps');
242        Data{iview}=tps_coeff_field(Data{iview},check_proj_tps);
243
244        %% projection on object (gridded plane)
245        if Param.CheckObject
246            [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
247            if ~isempty(errormsg)
248                disp(['ERROR in merge_proge/proj_field: ' errormsg])
249                return
250            end
251        end
252    end
253    %----------END LOOP ON VIEWS----------------------
254
255    %% merge the NbView fields
256    MergeData=merge_field(Data);
257    if isfield(MergeData,'Txt')
258        disp(MergeData.Txt);
259        return
260    end
261
262    % time of the merged field:
263    if ~isempty(time)% time defined from ImaDoc
264        timeread=time(:,index);
265    end
266    timeread=mean(timeread);
267
268    % generating the name of the merged field
269    i1=i1_series{iview}(index);
270    if ~isempty(i2_series{iview})
271        i2=i2_series{iview}(index);
272    else
273        i2=i1;
274    end
275    j1=1;
276    j2=1;
277    if ~isempty(j1_series{iview})
278        j1=j1_series{iview}(index);
279        if ~isempty(j2_series{iview})
280            j2=j2_series{iview}(index);
281        else
282            j2=j1;
283        end
284    end
285    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomType{1},i1,i2,j1,j2);
286
287    % recording the merged field
288    if CheckImage{1}    %in case of input images an image is produced
289        if isa(MergeData.A,'uint8')
290            bitdepth=8;
291        elseif isa(MergeData.A,'uint16')
292            bitdepth=16;
293        end
294        imwrite(MergeData.A,OutputFile,'BitDepth',bitdepth);
295        %write xml calibration file
296        siz=size(MergeData.A);
297        npy=siz(1);
298        npx=siz(2);
299        if isfield(MergeData,'VarAttribute')&&isfield(MergeData.VarAttribute{1},'Coord_2')&&isfield(MergeData.VarAttribute{1},'Coord_1')
300            Rangx=MergeData.VarAttribute{1}.Coord_2;
301            Rangy=MergeData.VarAttribute{1}.Coord_1;
302        elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY')
303            Rangx=[MergeData.AX(1) MergeData.AX(end)];
304            Rangy=[MergeData.AY(1) MergeData.AY(end)];
305        else
306            Rangx=[0.5 npx-0.5];
307            Rangy=[npy-0.5 0.5];%default
308        end
309        pxcmx=(npx-1)/(Rangx(2)-Rangx(1));
310        pxcmy=(npy-1)/(Rangy(1)-Rangy(2));
311        T_x=-pxcmx*Rangx(1)+0.5;
312        T_y=-pxcmy*Rangy(2)+0.5;
313        GeometryCal.focal=1;
314        GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1];
315        GeometryCal.Tx_Ty_Tz=[T_x T_y 1];
316        ImaDoc.GeometryCalib=GeometryCal;
317        %             t=struct2xml(ImaDoc);
318        %             t=set(t,1,'name','ImaDoc');
319        %             save(t,[filebase_merge '.xml'])
320        %             display([filebase_merge '.xml saved'])
321    else
322        MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'};
323        MergeData.Conventions='uvmat';
324        MergeData.nb_coord=2;
325        MergeData.nb_dim=2;
326        dt=[];
327        if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt)
328            dt=Data{1}.dt;
329        end
330        for iview =2:numel(Data)
331            if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt))
332                dt=[];%dt not the same for all fields
333            end
334        end
335        if isempty(dt)
336            MergeData.ListGlobalAttribute(6)=[];
337        else
338            MergeData.dt=dt;
339        end
340        MergeData.Time=timeread;
341        error=struct2nc(OutputFile,MergeData);%save result file
342        if isempty(error)
343            display(['output file ' OutputFile ' written'])
344        else
345            display(error)
346        end
347    end
348end
349
350
351%'merge_field': concatene fields
352%------------------------------------------------------------------------
353function MergeData=merge_field(Data)
354%% default output
355if isempty(Data)||~iscell(Data)
356    MergeData=[];
357    return
358end
359MergeData=Data{1};%default
360error=0;
361NbView=length(Data);
362if NbView==1
363    return
364end
365
366%% group the variables (fields of 'Data') in cells of variables with the same dimensions
367[CellVarIndex,NbDim,VarTypeCell]=find_field_cells(Data{1});
368%LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS
369% CellVarIndex=cells of variable index arrays
370for icell=1:length(CellVarIndex)
371    if NbDim(icell)==1
372        continue
373    end
374    VarIndex=CellVarIndex{icell};%  indices of the selected variables in the list FieldData.ListVarName
375    VarType=VarTypeCell{icell};
376    ivar_X=VarType.coord_x;
377    ivar_Y=VarType.coord_y;
378    ivar_FF=VarType.errorflag;
379    if isempty(ivar_X)
380        test_grid=1;%test for input data on regular grid (e.g. image)coordinates
381    else
382        if length(ivar_Y)~=1
383                displ_uvmat('ERROR','y coordinate missing in proj_field.m',checkrun)
384                return
385        end
386        test_grid=0;
387    end
388    %case of input fields with unstructured coordinates
389    if ~test_grid
390        for ivar=VarIndex
391            VarName=MergeData.ListVarName{ivar};
392            for iview=1:NbView
393                MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
394            end
395        end
396    %case of fields defined on a structured  grid
397    else 
398        testFF=0;
399        for iview=2:NbView
400            for ivar=VarIndex
401                VarName=MergeData.ListVarName{ivar};
402                if isfield(MergeData,'VarAttribute')
403                    if length(MergeData.VarAttribute)>=ivar && isfield(MergeData.VarAttribute{ivar},'Role') && isequal(MergeData.VarAttribute{ivar}.Role,'errorflag')
404                        testFF=1;
405                    end
406                end
407                MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);
408            end
409        end
410        if testFF
411            nbaver=NbView-MergeData.FF;
412            indgood=find(nbaver>0);
413            for ivar=VarIndex
414                VarName=MergeData.ListVarName{ivar};
415                MergeData.(VarName)(indgood)=double(MergeData.(VarName)(indgood))./nbaver(indgood);
416            end
417        else
418            for ivar=VarIndex
419                VarName=MergeData.ListVarName{ivar};
420                MergeData.(VarName)=double(MergeData.(VarName))./NbView;
421            end   
422        end
423    end
424end
425
426   
Note: See TracBrowser for help on using the repository browser.