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

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

corrections done in civ

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