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

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

clean the head of the series fcts

File size: 17.4 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(s) and 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 ischar(Param.InputFields.FieldName)
224                Param.InputFields.FieldName={Param.InputFields.FieldName};
225            end
226            for ilist=1:numel(Param.InputFields.FieldName)
227                switch Param.InputFields.FieldName{ilist}
228                    case {'vort','div','strain'}
229                        check_tps=1;
230                end
231            end
232               
233            %% calculate tps coeff if needed
234             check_proj_tps= ~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'filter')&&~isfield(Data{iview},'Coord_tps');
235            Data{iview}=calc_tps(Data{iview},check_proj_tps);
236
237            %% projection on object (gridded plane)
238            if Param.CheckObject
239                [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
240                if ~isempty(errormsg)
241                    displ_uvmat('ERROR',['error in merge_proge/proj_field: ' errormsg],checkrun)
242                    return
243                end
244            end
245        end
246        %----------END LOOP ON VIEWS----------------------
247       
248        %% merge the nbview fields
249        MergeData=merge_field(Data);
250        if isfield(MergeData,'Txt')
251            displ_uvmat('ERROR',MergeData.Txt,checkrun)
252            return
253        end
254       
255        % time of the merged field:
256        if ~isempty(time)% time defined from ImaDoc
257            timeread=time(:,index);
258        end
259        timeread=mean(timeread);
260       
261        % generating the name of the merged field
262        i1=i1_series{iview}(index);
263        if ~isempty(i2_series{iview})
264            i2=i2_series{iview}(index);
265        else
266            i2=i1;
267        end
268        j1=1;
269        j2=1;
270        if ~isempty(j1_series{iview})
271            j1=j1_series{iview}(index);
272            if ~isempty(j2_series{iview})
273                j2=j2_series{iview}(index);
274            else
275                j2=j1;
276            end
277        end
278        OutputFile=fullfile_uvmat(RootPath{1},OutputSubDir,RootFile{1},FileExtOut,NomType{1},i1,i2,j1,j2);
279       
280        % recording the merged field
281        if CheckImage{1}    %in case of input images an image is produced
282            if isa(MergeData.A,'uint8')
283                bitdepth=8;
284            elseif isa(MergeData.A,'uint16')
285                bitdepth=16;
286            end
287            imwrite(MergeData.A,OutputFile,'BitDepth',bitdepth);
288            %write xml calibration file
289            siz=size(MergeData.A);
290            npy=siz(1);
291            npx=siz(2);
292            if isfield(MergeData,'VarAttribute')&&isfield(MergeData.VarAttribute{1},'Coord_2')&&isfield(MergeData.VarAttribute{1},'Coord_1')
293                Rangx=MergeData.VarAttribute{1}.Coord_2;
294                Rangy=MergeData.VarAttribute{1}.Coord_1;
295            elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY')
296                Rangx=[MergeData.AX(1) MergeData.AX(end)];
297                Rangy=[MergeData.AY(1) MergeData.AY(end)];
298            else
299                Rangx=[0.5 npx-0.5];
300                Rangy=[npy-0.5 0.5];%default
301            end
302            pxcmx=(npx-1)/(Rangx(2)-Rangx(1));
303            pxcmy=(npy-1)/(Rangy(1)-Rangy(2));
304            T_x=-pxcmx*Rangx(1)+0.5;
305            T_y=-pxcmy*Rangy(2)+0.5;
306            GeometryCal.focal=1;
307            GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1];
308            GeometryCal.Tx_Ty_Tz=[T_x T_y 1];
309            ImaDoc.GeometryCalib=GeometryCal;
310%             t=struct2xml(ImaDoc);
311%             t=set(t,1,'name','ImaDoc');
312%             save(t,[filebase_merge '.xml'])
313%             display([filebase_merge '.xml saved'])
314        else
315            MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'};
316            MergeData.Conventions='uvmat';
317            MergeData.nb_coord=2;
318            MergeData.nb_dim=2;
319            dt=[];
320            if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt)
321                dt=Data{1}.dt;
322            end
323            for iview =2:numel(Data)
324                if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt))
325                    dt=[];%dt not the same for all fields
326                end
327            end
328            if isempty(dt)
329                MergeData.ListGlobalAttribute(6)=[];
330            else
331                MergeData.dt=dt;
332            end
333            MergeData.Time=timeread;
334            error=struct2nc(OutputFile,MergeData);%save result file
335            if isempty(error)
336                display(['output file ' OutputFile ' written'])
337            else
338                display(error)
339            end
340        end
341    end
342end
343
344%'merge_field': concatene fields
345%------------------------------------------------------------------------
346function MergeData=merge_field(Data)
347%% default output
348if isempty(Data)||~iscell(Data)
349    MergeData=[];
350    return
351end
352MergeData=Data{1};%default
353error=0;
354nbview=length(Data);
355if nbview==1
356    return
357end
358
359%% group the variables (fields of 'Data') in cells of variables with the same dimensions
360[CellVarIndex,NbDim,VarTypeCell]=find_field_cells(Data{1});
361%LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS
362% CellVarIndex=cells of variable index arrays
363for icell=1:length(CellVarIndex)
364    if NbDim(icell)==1
365        continue
366    end
367    VarIndex=CellVarIndex{icell};%  indices of the selected variables in the list FieldData.ListVarName
368    VarType=VarTypeCell{icell};
369    ivar_X=VarType.coord_x;
370    ivar_Y=VarType.coord_y;
371    ivar_FF=VarType.errorflag;
372    if isempty(ivar_X)
373        test_grid=1;%test for input data on regular grid (e.g. image)coordinates
374    else
375        if length(ivar_Y)~=1
376                displ_uvmat('ERROR','y coordinate missing in proj_field.m',checkrun)
377                return
378        end
379        test_grid=0;
380    end
381    %case of input fields with unstructured coordinates
382    if ~test_grid
383        for ivar=VarIndex
384            VarName=MergeData.ListVarName{ivar};
385            for iview=1:nbview
386                MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
387            end
388        end
389    %case of fields defined on a structured  grid
390    else 
391        testFF=0;
392        for iview=2:nbview
393            for ivar=VarIndex
394                VarName=MergeData.ListVarName{ivar};
395                if isfield(MergeData,'VarAttribute')
396                    if length(MergeData.VarAttribute)>=ivar && isfield(MergeData.VarAttribute{ivar},'Role') && isequal(MergeData.VarAttribute{ivar}.Role,'errorflag')
397                        testFF=1;
398                    end
399                end
400                MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);
401            end
402        end
403        if testFF
404            nbaver=nbview-MergeData.FF;
405            indgood=find(nbaver>0);
406            for ivar=VarIndex
407                VarName=MergeData.ListVarName{ivar};
408                MergeData.(VarName)(indgood)=double(MergeData.(VarName)(indgood))./nbaver(indgood);
409            end
410        else
411            for ivar=VarIndex
412                VarName=MergeData.ListVarName{ivar};
413                MergeData.(VarName)=double(MergeData.(VarName))./nbview;
414            end   
415        end
416    end
417end
418
419   
Note: See TracBrowser for help on using the repository browser.