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

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

commit the series fcts with the new form

File size: 17.2 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
[592]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
[29]62end
63
[457]64%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
[592]65%% read input parameters from an xml file if input is a file name (batch mode)
66checkrun=1;
[457]67if ischar(Param)
[592]68    Param=xml2struct(Param);% read Param as input file (batch case)
69    checkrun=0;
[374]70end
[592]71
[462]72ParamOut=Param; %default output
[550]73if ~isfield(Param,'InputFields')
74    Param.InputFields.FieldName='';
75end
[592]76OutputSubDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
[457]77
[577]78%% root input file type
[457]79RootPath=Param.InputTable(:,1);
80RootFile=Param.InputTable(:,3);
81SubDir=Param.InputTable(:,2);
82NomType=Param.InputTable(:,4);
83FileExt=Param.InputTable(:,5);
[374]84[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[474]85%%%%%%%%%%%%
86% The cell array filecell is the list of input file names, while
87% filecell{iview,fileindex}:
[457]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
[474]92%%%%%%%%%%%%
[457]93NbSlice=1;%default
[462]94if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
[457]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
[462]105ImageTypeOptions={'image','multimage','mmreader','video'};
106NcTypeOptions={'netcdf','civx','civdata'};
107for iview=1:nbview
108    if ~exist(filecell{iview,1}','file')
[474]109        displ_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
[462]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
[457]120end
121
[470]122
[457]123%% calibration data and timing: read the ImaDoc files
[470]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
[457]126    diff_time=max(max(diff(time)));
[474]127    if diff_time>0
128        displ_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)],checkrun)
[457]129    end   
130end
131
[409]132%% coordinate transform or other user defined transform
[374]133transform_fct='';%default
[478]134if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]135    addpath(Param.FieldTransform.TransformPath)
136    transform_fct=str2func(Param.FieldTransform.TransformName);
137    rmpath(Param.FieldTransform.TransformPath)
[374]138end
[474]139
[457]140%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
141 % EDIT FROM HERE
[116]142
[457]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
[474]148else
149    displ_uvmat('ERROR',['invalid file type input ' FileType{1}],checkrun)
[457]150    return
151end
[462]152for iview=1:nbview
153        if ~isequal(CheckImage{iview},CheckImage{1})||~isequal(CheckNc{iview},CheckNc{1})
[474]154        displ_uvmat('ERROR','input set of input series: need  either netcdf either image series',checkrun)
[457]155    return
[464]156    end
[457]157end
[462]158NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series
[474]159if checkrun==1
160    ParamOut.Specific=[];%no specific parameter
161    return %stop here for interactive input (option Param.Specific='?')
162end
[457]163
164%% Set field names and velocity types
[462]165%use Param.InputFields for all views
[457]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;
[474]173
[457]174    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
175    for index=index_slice
[470]176 
[457]177        if checkrun
[478]178            update_waitbar(hseries.Waitbar,index/(nbfield))
[457]179            stopstate=get(hseries.RUN,'BusyAction');
180        else
181            stopstate='queue';
182        end
[462]183       
184        %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
185        Data=cell(1,nbview);%initiate the set Data
[464]186        nbtime=0;
[29]187        for iview=1:nbview
[551]188            %% reading input file(s)
[464]189            [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},Param.InputFields,frame_index{iview}(index));
[462]190            if ~isempty(errormsg)
[474]191                errormsg=['merge_proj/read_field/' errormsg];
192                display(errormsg)
[29]193                break
194            end
[464]195            timeread(iview)=0;
196            if isfield(Data{iview},'Time')
[470]197                    timeread(iview)=Data{iview}.Time;
[464]198                    nbtime=nbtime+1;
199                end
[29]200            if ~isempty(NbSlice_calib)
[462]201                Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[29]202            end
[494]203           
[551]204            %% transform the input field (e.g; phys) if requested
[41]205            if ~isempty(transform_fct)
[551]206                if nargin(transform_fct)>=2
207                    Data{iview}=transform_fct(Data{iview},XmlData{iview});
208                else
209                    Data{iview}=transform_fct(Data{iview});
[550]210                end
[29]211            end
[494]212           
213            %% check whether tps is needed, then calculate tps coefficients if needed
214            check_tps=0;
[577]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={};
[494]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
[522]228               
229            %% calculate tps coeff if needed
230             check_proj_tps= ~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'filter')&&~isfield(Data{iview},'Coord_tps');
[585]231            Data{iview}=tps_coeff_field(Data{iview},check_proj_tps);
[551]232
233            %% projection on object (gridded plane)
[464]234            if Param.CheckObject
235                [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
[409]236                if ~isempty(errormsg)
[474]237                    displ_uvmat('ERROR',['error in merge_proge/proj_field: ' errormsg],checkrun)
[409]238                    return
239                end
[29]240            end
[464]241        end
[222]242        %----------END LOOP ON VIEWS----------------------
[464]243       
[222]244        %% merge the nbview fields
[462]245        MergeData=merge_field(Data);
[29]246        if isfield(MergeData,'Txt')
[474]247            displ_uvmat('ERROR',MergeData.Txt,checkrun)
[29]248            return
[464]249        end
[470]250       
251        % time of the merged field:
252        if ~isempty(time)% time defined from ImaDoc
253            timeread=time(:,index);
[29]254        end
[470]255        timeread=mean(timeread);
256       
[464]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
[474]274        OutputFile=fullfile_uvmat(RootPath{1},OutputSubDir,RootFile{1},FileExtOut,NomType{1},i1,i2,j1,j2);
[29]275       
[464]276        % recording the merged field
277        if CheckImage{1}    %in case of input images an image is produced
[29]278            if isa(MergeData.A,'uint8')
279                bitdepth=8;
280            elseif isa(MergeData.A,'uint16')
281                bitdepth=16;
282            end
[464]283            imwrite(MergeData.A,OutputFile,'BitDepth',bitdepth);
[29]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;
[550]306%             t=struct2xml(ImaDoc);
307%             t=set(t,1,'name','ImaDoc');
308%             save(t,[filebase_merge '.xml'])
309%             display([filebase_merge '.xml saved'])
[29]310        else
[464]311            MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','nb_coord','nb_dim','dt','Time','civ'};
[422]312            MergeData.Conventions='uvmat';
[29]313            MergeData.nb_coord=2;
314            MergeData.nb_dim=2;
[93]315            dt=[];
[462]316            if isfield(Data{1},'dt')&& isnumeric(Data{1}.dt)
317                dt=Data{1}.dt;
[93]318            end
[462]319            for iview =2:numel(Data)
320                if ~(isfield(Data{iview},'dt')&& isequal(Data{iview}.dt,dt))
[93]321                    dt=[];%dt not the same for all fields
322                end
323            end
324            if isempty(dt)
325                MergeData.ListGlobalAttribute(6)=[];
326            else
[464]327                MergeData.dt=dt;
[93]328            end
[470]329            MergeData.Time=timeread;
[462]330            error=struct2nc(OutputFile,MergeData);%save result file
[29]331            if isempty(error)
[462]332                display(['output file ' OutputFile ' written'])
[29]333            else
334                display(error)
335            end
336        end
337    end
338end
339
[222]340%'merge_field': concatene fields
341%------------------------------------------------------------------------
[29]342function MergeData=merge_field(Data)
[222]343%% default output
[29]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
[222]354
[522]355%% group the variables (fields of 'Data') in cells of variables with the same dimensions
356[CellVarIndex,NbDim,VarTypeCell]=find_field_cells(Data{1});
[89]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
[474]372                displ_uvmat('ERROR','y coordinate missing in proj_field.m',checkrun)
[89]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
[522]382                MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
[89]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
[522]396                MergeData.(VarName)=MergeData.(VarName) + Data{iview}.(VarName);
[89]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};
[522]404                MergeData.(VarName)(indgood)=double(MergeData.(VarName)(indgood))./nbaver(indgood);
[89]405            end
406        else
407            for ivar=VarIndex
408                VarName=MergeData.ListVarName{ivar};
[522]409                MergeData.(VarName)=double(MergeData.(VarName))./nbview;
[89]410            end   
411        end
412    end
413end
[222]414
[89]415   
Note: See TracBrowser for help on using the repository browser.