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

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

functions adpated to mode background

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