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

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

clean the head of the series fcts

File size: 17.4 KB
RevLine 
[573]1%'merge_proj': concatene several fields from series, can project them on a regular grid in phys coordinates
[457]2%------------------------------------------------------------------------
[464]3% function ParamOut=merge_proj(Param)
[457]4%------------------------------------------------------------------------
[222]5
[457]6%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
[169]7%
[457]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%
[169]20%OUTPUT
21% GUI_input=list of options in the GUI series.fig needed for the function
22%
23%INPUT:
[457]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
[169]28%
[457]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
[474]33%    .OutputDirExt: directory extension for data outputs
[457]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
[462]48function ParamOut=merge_proj(Param)
[29]49
[457]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
[474]52    ParamOut={'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default)
[462]53        'WholeIndexRange';'off';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
[29]54        'NbSlice';'on'; ...%nbre of slices ('off' by default)
[462]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)
[457]57        'FieldTransform'; 'on';...%can use a transform function
58        'ProjObject';'on';...%can use projection object(option 'off'/'on',
[462]59        'Mask';'off';...%can use mask option   (option 'off'/'on', 'off' by default)
60        'OutputDirExt';'.mproj';...%set the output dir extension
[29]61               ''};
[457]62        return
[29]63end
64
[457]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
[374]73    hseries=guidata(Param.hseries);%handles of the GUI series
[462]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
[374]79end
[462]80ParamOut=Param; %default output
[550]81if ~isfield(Param,'InputFields')
82    Param.InputFields.FieldName='';
83end
[457]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);
[551]91OutputSubDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
[374]92[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[474]93%%%%%%%%%%%%
94% The cell array filecell is the list of input file names, while
95% filecell{iview,fileindex}:
[457]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
[474]100%%%%%%%%%%%%
[457]101NbSlice=1;%default
[462]102if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
[457]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
[462]113ImageTypeOptions={'image','multimage','mmreader','video'};
114NcTypeOptions={'netcdf','civx','civdata'};
115for iview=1:nbview
116    if ~exist(filecell{iview,1}','file')
[474]117        displ_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
[462]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
[457]128end
129
[470]130
[457]131%% calibration data and timing: read the ImaDoc files
[470]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
[457]134    diff_time=max(max(diff(time)));
[474]135    if diff_time>0
136        displ_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)],checkrun)
[457]137    end   
138end
139
[409]140%% coordinate transform or other user defined transform
[374]141transform_fct='';%default
[478]142if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]143    addpath(Param.FieldTransform.TransformPath)
144    transform_fct=str2func(Param.FieldTransform.TransformName);
145    rmpath(Param.FieldTransform.TransformPath)
[374]146end
[474]147
[457]148%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
149 % EDIT FROM HERE
[116]150
[457]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
[474]156else
157    displ_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
[457]158    return
159end
[462]160for iview=1:nbview
161        if ~isequal(CheckImage{iview},CheckImage{1})||~isequal(CheckNc{iview},CheckNc{1})
[474]162        displ_uvmat('ERROR','input set of input series: need  either netcdf either image series',checkrun)
[457]163    return
[464]164    end
[457]165end
[462]166NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series
[474]167if checkrun==1
168    ParamOut.Specific=[];%no specific parameter
169    return %stop here for interactive input (option Param.Specific='?')
170end
[457]171
172%% Set field names and velocity types
[462]173%use Param.InputFields for all views
[457]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;
[474]181
[457]182    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
183    for index=index_slice
[470]184 
[457]185        if checkrun
[478]186            update_waitbar(hseries.Waitbar,index/(nbfield))
[457]187            stopstate=get(hseries.RUN,'BusyAction');
188        else
189            stopstate='queue';
190        end
[462]191       
192        %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
193        Data=cell(1,nbview);%initiate the set Data
[464]194        nbtime=0;
[29]195        for iview=1:nbview
[551]196            %% reading input file(s)
[464]197            [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index));
[462]198            if ~isempty(errormsg)
[474]199                errormsg=['merge_proj/read_field/' errormsg];
200                display(errormsg)
[29]201                break
202            end
[464]203            timeread(iview)=0;
204            if isfield(Data{iview},'Time')
[470]205                    timeread(iview)=Data{iview}.Time;
[464]206                    nbtime=nbtime+1;
207                end
[29]208            if ~isempty(NbSlice_calib)
[462]209                Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[29]210            end
[494]211           
[551]212            %% transform the input field (e.g; phys) if requested
[41]213            if ~isempty(transform_fct)
[551]214                if nargin(transform_fct)>=2
215                    Data{iview}=transform_fct(Data{iview},XmlData{iview});
216                else
217                    Data{iview}=transform_fct(Data{iview});
[550]218                end
[29]219            end
[494]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
[522]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);
[551]236
237            %% projection on object (gridded plane)
[464]238            if Param.CheckObject
239                [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
[409]240                if ~isempty(errormsg)
[474]241                    displ_uvmat('ERROR',['error in merge_proge/proj_field: ' errormsg],checkrun)
[409]242                    return
243                end
[29]244            end
[464]245        end
[222]246        %----------END LOOP ON VIEWS----------------------
[464]247       
[222]248        %% merge the nbview fields
[462]249        MergeData=merge_field(Data);
[29]250        if isfield(MergeData,'Txt')
[474]251            displ_uvmat('ERROR',MergeData.Txt,checkrun)
[29]252            return
[464]253        end
[470]254       
255        % time of the merged field:
256        if ~isempty(time)% time defined from ImaDoc
257            timeread=time(:,index);
[29]258        end
[470]259        timeread=mean(timeread);
260       
[464]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
[474]278        OutputFile=fullfile_uvmat(RootPath{1},OutputSubDir,RootFile{1},FileExtOut,NomType{1},i1,i2,j1,j2);
[29]279       
[464]280        % recording the merged field
281        if CheckImage{1}    %in case of input images an image is produced
[29]282            if isa(MergeData.A,'uint8')
283                bitdepth=8;
284            elseif isa(MergeData.A,'uint16')
285                bitdepth=16;
286            end
[464]287            imwrite(MergeData.A,OutputFile,'BitDepth',bitdepth);
[29]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;
[550]310%             t=struct2xml(ImaDoc);
311%             t=set(t,1,'name','ImaDoc');
312%             save(t,[filebase_merge '.xml'])
313%             display([filebase_merge '.xml saved'])
[29]314        else
[464]315            MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'};
[422]316            MergeData.Conventions='uvmat';
[29]317            MergeData.nb_coord=2;
318            MergeData.nb_dim=2;
[93]319            dt=[];
[462]320            if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt)
321                dt=Data{1}.dt;
[93]322            end
[462]323            for iview =2:numel(Data)
324                if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt))
[93]325                    dt=[];%dt not the same for all fields
326                end
327            end
328            if isempty(dt)
329                MergeData.ListGlobalAttribute(6)=[];
330            else
[464]331                MergeData.dt=dt;
[93]332            end
[470]333            MergeData.Time=timeread;
[462]334            error=struct2nc(OutputFile,MergeData);%save result file
[29]335            if isempty(error)
[462]336                display(['output file ' OutputFile ' written'])
[29]337            else
338                display(error)
339            end
340        end
341    end
342end
343
[222]344%'merge_field': concatene fields
345%------------------------------------------------------------------------
[29]346function MergeData=merge_field(Data)
[222]347%% default output
[29]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
[222]358
[522]359%% group the variables (fields of 'Data') in cells of variables with the same dimensions
360[CellVarIndex,NbDim,VarTypeCell]=find_field_cells(Data{1});
[89]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
[474]376                displ_uvmat('ERROR','y coordinate missing in proj_field.m',checkrun)
[89]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
[522]386                MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
[89]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
[522]400                MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);
[89]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};
[522]408                MergeData.(VarName)(indgood)=double(MergeData.(VarName)(indgood))./nbaver(indgood);
[89]409            end
410        else
411            for ivar=VarIndex
412                VarName=MergeData.ListVarName{ivar};
[522]413                MergeData.(VarName)=double(MergeData.(VarName))./nbview;
[89]414            end   
415        end
416    end
417end
[222]418
[89]419   
Note: See TracBrowser for help on using the repository browser.