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

Last change on this file since 585 was 585, checked in by sommeria, 8 years ago

tps_coeff_field introduced instead of calc_tps

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