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

Last change on this file since 551 was 551, checked in by sommeria, 12 years ago

bugs corrected

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