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

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

error messages improved

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