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

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

commit the series fcts with the new form

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