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

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

merge_proj updated to account for new format in field transform (variable nbre of input parma)

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