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

Last change on this file since 851 was 829, checked in by sommeria, 10 years ago

fcts transferred to /usr_fct

File size: 20.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
41%=======================================================================
42% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
43%   http://www.legi.grenoble-inp.fr
44%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
45%
46%     This file is part of the toolbox UVMAT.
47%
48%     UVMAT is free software; you can redistribute it and/or modify
49%     it under the terms of the GNU General Public License as published
50%     by the Free Software Foundation; either version 2 of the license,
51%     or (at your option) any later version.
52%
53%     UVMAT is distributed in the hope that it will be useful,
54%     but WITHOUT ANY WARRANTY; without even the implied warranty of
55%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56%     GNU General Public License (see LICENSE.txt) for more details.
57%=======================================================================
58
59function ParamOut=merge_proj(Param)
60
61%% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed
62if isstruct(Param) && isequal(Param.Action.RUN,0)
63    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
64    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
65    ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
66    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
67    ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
68    ParamOut.FieldTransform = 'on';%can use a transform function
69    ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only)
70    ParamOut.ProjObject='on';%can use projection object(option 'off'/'on',
71    ParamOut.Mask='on';%can use mask option   (option 'off'/'on', 'off' by default)
72    ParamOut.OutputDirExt='.mproj';%set the output dir extension
73    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
74      %check the input files
75    first_j=[];
76    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
77    PairString='';
78    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
79    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
80    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
81        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
82    if ~exist(FirstFileName,'file')
83        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
84    elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject')
85        msgbox_uvmat('WARNING','You may need a projection object of type plane for merge_proj')
86    end
87    return
88end
89
90%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
91ParamOut=[]; %default output
92%% read input parameters from an xml file if input is a file name (batch mode)
93checkrun=1;
94if ischar(Param)
95    Param=xml2struct(Param);% read Param as input file (batch case)
96    checkrun=0;
97end
98hseries=findobj(allchild(0),'Tag','series');
99RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
100WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
101
102%% define the directory for result file (with path=RootPath{1})
103OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
104
105if ~isfield(Param,'InputFields')
106    Param.InputFields.FieldName='';
107end
108
109%% root input file type
110RootPath=Param.InputTable(:,1);
111RootFile=Param.InputTable(:,3);
112SubDir=Param.InputTable(:,2);
113NomType=Param.InputTable(:,4);
114FileExt=Param.InputTable(:,5);
115[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
116%%%%%%%%%%%%
117% The cell array filecell is the list of input file names, while
118% filecell{iview,fileindex}:
119%        iview: line in the table corresponding to a given file series
120%        fileindex: file index within  the file series,
121% 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
122% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
123%%%%%%%%%%%%
124% NbSlice=1;%default
125% if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
126%     NbSlice=Param.IndexRange.NbSlice;
127% end
128NbView=numel(i1_series);%number of input file series (lines in InputTable)
129NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
130NbField_i=size(i1_series{1},2); %nb of fields for the i index
131NbField=NbField_j*NbField_i; %total number of fields
132
133%% determine the file type on each line from the first input file
134ImageTypeOptions={'image','multimage','mmreader','video'};
135NcTypeOptions={'netcdf','civx','civdata'};
136for iview=1:NbView
137    if ~exist(filecell{iview,1}','file')
138        disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
139        return
140    end
141    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
142    FileType{iview}=FileInfo{iview}.FileType;
143    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
144    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
145    if ~isempty(j1_series{iview})
146        frame_index{iview}=j1_series{iview};
147    else
148        frame_index{iview}=i1_series{iview};
149    end
150end
151if NbView >1 && max(cell2mat(CheckImage))>0 && ~isfield(Param,'ProjObject')
152    disp_uvmat('ERROR','projection on a common grid is needed to concatene images: use a Projection Object of type ''plane'' with ProjMode=''interp_lin''',checkrun)
153    return
154end
155
156%% calibration data and timing: read the ImaDoc files
157[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
158if size(time,1)>1
159    diff_time=max(max(diff(time)));
160    if diff_time>0
161        disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun)
162    end   
163end
164if ~isempty(errormsg)
165    disp_uvmat('WARNING',errormsg,checkrun)
166end
167time=mean(time,1); %averaged time taken for the merged field
168
169%% coordinate transform or other user defined transform
170transform_fct='';%default fct handle
171if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
172        currentdir=pwd;
173        cd(Param.FieldTransform.TransformPath)
174        transform_fct=str2func(Param.FieldTransform.TransformName);
175        cd (currentdir)
176end
177%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
178 % EDIT FROM HERE
179
180%% check the validity of  input file types
181for iview=1:NbView
182    if ~isequal(CheckImage{iview},1)&&~isequal(CheckNc{iview},1)
183        disp_uvmat('ERROR','input set of input series: need  either netcdf either image series',checkrun)
184        return
185    end
186end
187
188%% output file type
189if min(cell2mat(CheckImage))==1 && (~Param.CheckObject || strcmp(Param.ProjObject.Type,'plane'))
190    FileExtOut='.png'; %image output (input and proj result = image)
191    for iview=1:NbView
192        BitDepth(iview)=FileInfo{iview}.BitDepth;
193    end
194    BitDepth=max(BitDepth);
195else
196    FileExtOut='.nc'; %netcdf output
197end
198NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series
199RootFileOut=RootFile{1};
200for iview=2:NbView
201    if ~strcmp(RootFile{iview},RootFile{1})
202        RootFileOut='mproj';
203        break
204    end
205end
206
207%% mask (TODO: case of multilevels)
208MaskData=cell(NbView,1);
209if Param.CheckMask
210    if ischar(Param.MaskTable)% case of a single mask (char chain)
211        Param.MaskTable={Param.MaskTable};
212    end
213    for iview=1:numel(Param.MaskTable)
214        if exist(Param.MaskTable{iview},'file')
215            [MaskData{iview},tild,errormsg] = read_field(Param.MaskTable{iview},'image');
216            if ~isempty(transform_fct) && nargin(transform_fct)>=2
217                MaskData{iview}=transform_fct(MaskData{iview},XmlData{iview});
218            end
219        end
220    end
221end
222
223%% Set field names and velocity types
224%use Param.InputFields for all views
225
226%% MAIN LOOP ON FIELDS
227%%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
228% for i_slice=1:NbSlice
229%     index_slice=i_slice:NbSlice:NbField;% select file indices of the slice
230%     NbFiles=0;
231%     nbmissing=0;
232
233    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
234for index=1:NbField
235        update_waitbar(WaitbarHandle,index/NbField)
236    if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
237        disp('program stopped by user')
238        return
239    end
240   
241    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
242    Data=cell(1,NbView);%initiate the set Data
243    timeread=zeros(1,NbView);
244    for iview=1:NbView
245        %% reading input file(s)
246        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index));
247        if ~isempty(errormsg)
248            disp_uvmat('ERROR',['ERROR in merge_proj/read_field/' errormsg],checkrun)
249            return
250        end
251        % get the time defined in the current file if not already defined from the xml file
252        if ~isempty(time) && isfield(Data{iview},'Time')
253            timeread(iview)=Data{iview}.Time;
254        end
255        if ~isempty(NbSlice_calib)
256            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
257        end
258       
259        %% transform the input field (e.g; phys) if requested (no transform involving two input fields)
260        if ~isempty(transform_fct)
261            if nargin(transform_fct)>=2
262                Data{iview}=transform_fct(Data{iview},XmlData{iview});
263            else
264                Data{iview}=transform_fct(Data{iview});
265            end
266        end
267       
268        %% calculate tps coefficients if needed
269        check_proj_tps= isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps');
270        Data{iview}=tps_coeff_field(Data{iview},check_proj_tps);
271       
272        %% projection on object (gridded plane)
273        if Param.CheckObject
274            [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
275            if ~isempty(errormsg)
276                disp_uvmat('ERROR',['ERROR in merge_proge/proj_field: ' errormsg],checkrun)
277                return
278            end
279        end
280       
281        %% mask
282        if Param.CheckMask && ~isempty(MaskData{iview})
283             [Data{iview},errormsg]=mask_proj(Data{iview},MaskData{iview});
284        end
285    end
286    %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%%
287
288    %% merge the NbView fields
289    [MergeData,errormsg]=merge_field(Data);
290    if ~isempty(errormsg)
291        disp_uvmat('ERROR',errormsg,checkrun);
292        return
293    end
294
295    %% time of the merged field: take the average of the different views
296    if ~isempty(time)
297        timeread=time(index);   
298    elseif ~isempty(find(timeread))% time defined from ImaDoc
299        timeread=mean(timeread(timeread~=0));% take average over times form the files (when defined)
300    else
301        timeread=index;% take time=file index
302    end
303
304    %% generating the name of the merged field
305    i1=i1_series{1}(index);
306    if ~isempty(i2_series{end})
307        i2=i2_series{end}(index);
308    else
309        i2=i1;
310    end
311    j1=1;
312    j2=1;
313    if ~isempty(j1_series{1})
314        j1=j1_series{1}(index);
315        if ~isempty(j2_series{end})
316            j2=j2_series{end}(index);
317        else
318            j2=j1;
319        end
320    end
321    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFileOut,FileExtOut,NomType{1},i1,i2,j1,j2);
322
323    %% recording the merged field
324    if strcmp(FileExtOut,'.png')    %output as image
325        if BitDepth==8
326            imwrite(uint8(MergeData.A),OutputFile,'BitDepth',8)
327        else
328            imwrite(uint16(MergeData.A),OutputFile,'BitDepth',16)
329        end
330        if index==1
331            %write xml calibration file, using the first file
332            siz=size(MergeData.A);
333            npy=siz(1);
334            npx=siz(2);
335            if isfield(MergeData,'coord_x') && isfield(MergeData,'coord_y')
336                Rangx=MergeData.coord_x;
337                Rangy=MergeData.coord_y;
338            elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY')
339                Rangx=[MergeData.AX(1) MergeData.AX(end)];
340                Rangy=[MergeData.AY(1) MergeData.AY(end)];
341            else
342                Rangx=[0.5 npx-0.5];
343                Rangy=[npy-0.5 0.5];%default
344            end
345            pxcmx=(npx-1)/(Rangx(2)-Rangx(1));
346            pxcmy=(npy-1)/(Rangy(1)-Rangy(2));
347            T_x=-pxcmx*Rangx(1)+0.5;
348            T_y=-pxcmy*Rangy(2)+0.5;
349            GeometryCal.CalibrationType='rescale';
350            GeometryCal.CoordUnit=MergeData.CoordUnit;
351            GeometryCal.focal=1;
352            GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1];
353            GeometryCal.Tx_Ty_Tz=[T_x T_y 1];
354            ImaDoc.GeometryCalib=GeometryCal;
355            t=struct2xml(ImaDoc);
356            t=set(t,1,'name','ImaDoc');
357            save(t,[fileparts(OutputFile) '.xml'])
358        end
359       
360    else
361        MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim'};
362        MergeData.Conventions='uvmat';
363        MergeData.nb_coord=2;
364        MergeData.nb_dim=2;
365        dt=[];
366        if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt)
367            dt=Data{1}.dt;
368        end
369        for iview =2:numel(Data)
370            if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt))
371                dt=[];%dt not the same for all fields
372            end
373        end
374        if ~isempty(timeread)
375            MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'Time'}];
376            MergeData.Time=timeread;
377        end
378        if ~isempty(dt)
379            MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'dt'}];
380            MergeData.dt=dt;
381        end
382        error=struct2nc(OutputFile,MergeData);%save result file
383        if isempty(error)
384            disp(['output file ' OutputFile ' written'])
385        else
386            disp(error)
387        end
388    end
389end
390
391
392%'merge_field': concatene fields
393%------------------------------------------------------------------------
394function [MergeData,errormsg]=merge_field(Data)
395%% default output
396if isempty(Data)||~iscell(Data)
397    MergeData=[];
398    return
399end
400errormsg='';
401MergeData=Data{1};% merged field= first field by default, reproduces the glabal attributes of the first field
402NbView=length(Data);
403if NbView==1
404    return
405end
406
407%% group the variables (fields of 'Data') in cells of variables with the same dimensions
408[CellInfo,NbDim,errormsg]=find_field_cells(Data{1});
409if ~isempty(errormsg)
410    return
411end
412
413%LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS
414for icell=1:length(CellInfo)
415    if NbDim(icell)~=1 % skip field cells which are of dim 1
416        switch CellInfo{icell}.CoordType
417            case 'scattered'  %case of input fields with unstructured coordinates: just concacene data
418                for ivar=CellInfo{icell}.VarIndex %  indices of the selected variables in the list FieldData.ListVarName
419                    VarName=Data{1}.ListVarName{ivar};
420                    %MergeData=Data{1};% merged field= first field by default, reproduces the glabal attributes of the first field
421                    for iview=2:NbView
422                        MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
423                    end
424                end
425            case 'grid'        %case of fields defined on a structured  grid
426                FFName='';
427                if isfield(CellInfo{icell},'VarIndex_errorflag') && ~isempty(CellInfo{icell}.VarIndex_errorflag)
428                    FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable
429                end
430                % select good data on each view
431                for ivar=CellInfo{icell}.VarIndex  %  indices of the selected variables in the list FieldData.ListVarName
432                    VarName=Data{1}.ListVarName{ivar};
433                    for iview=1:NbView
434                        if isempty(FFName)
435                            check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else
436                        else
437                            check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else
438                        end
439                        Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or masked data
440                        if iview==1
441                            MergeData.(VarName)=Data{1}.(VarName);% correct the field of MergeData
442                            NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point
443                        elseif size(Data{iview}.(VarName))~=size(MergeData.(VarName))
444                            errormsg='sizes of the input matrices do not agree, need to interpolate on a common grid using a projection object';
445                            return
446                        else                     
447                            MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);%add data
448                            NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else
449                        end
450                    end
451                    MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point
452                end
453        end
454        if isempty(FFName)
455            FFName='FF';
456        end
457        MergeData.(FFName)(NbAver~=0)=0;% flag to 1 undefined summed data
458        MergeData.(FFName)(NbAver==0)=1;% flag to 1 undefined summed data
459    end
460end
461
462
463   
Note: See TracBrowser for help on using the repository browser.