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

Last change on this file since 955 was 955, checked in by sommeria, 5 years ago

various

File size: 23.9 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
[810]41%=======================================================================
[924]42% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[810]43%   http://www.legi.grenoble-inp.fr
44%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
45%
46%     This file is part of the toolbox UVMAT.
47%
48%     UVMAT is free software; you can redistribute it and/or modify
49%     it under the terms of the GNU General Public License as published
50%     by the Free Software Foundation; either version 2 of the license,
51%     or (at your option) any later version.
52%
53%     UVMAT is distributed in the hope that it will be useful,
54%     but WITHOUT ANY WARRANTY; without even the implied warranty of
55%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56%     GNU General Public License (see LICENSE.txt) for more details.
57%=======================================================================
58
[462]59function ParamOut=merge_proj(Param)
[29]60
[606]61%% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed
[592]62if isstruct(Param) && isequal(Param.Action.RUN,0)
[606]63    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
[632]64    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
[606]65    ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
66    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
67    ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
68    ParamOut.FieldTransform = 'on';%can use a transform function
69    ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only)
70    ParamOut.ProjObject='on';%can use projection object(option 'off'/'on',
[635]71    ParamOut.Mask='on';%can use mask option   (option 'off'/'on', 'off' by default)
[592]72    ParamOut.OutputDirExt='.mproj';%set the output dir extension
[605]73    ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
[716]74      %check the input files
[905]75    ParamOut.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1)
[716]76    first_j=[];
77    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
78    PairString='';
79    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
80    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
81    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
82        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
83    if ~exist(FirstFileName,'file')
84        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
[605]85    elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject')
[635]86        msgbox_uvmat('WARNING','You may need a projection object of type plane for merge_proj')
[605]87    end
[606]88    return
[29]89end
90
[457]91%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
[632]92ParamOut=[]; %default output
[905]93RUNHandle=[];
94WaitbarHandle=[];
[592]95%% read input parameters from an xml file if input is a file name (batch mode)
96checkrun=1;
[457]97if ischar(Param)
[592]98    Param=xml2struct(Param);% read Param as input file (batch case)
99    checkrun=0;
[905]100else
101    hseries=findobj(allchild(0),'Tag','series');
102    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
103    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
[374]104end
[955]105tild=phys([],[]);% test to provoke the inclusion of the function phys at compilation
[624]106%% define the directory for result file (with path=RootPath{1})
107OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
108
[550]109if ~isfield(Param,'InputFields')
110    Param.InputFields.FieldName='';
111end
[457]112
[577]113%% root input file type
[457]114RootPath=Param.InputTable(:,1);
115RootFile=Param.InputTable(:,3);
116SubDir=Param.InputTable(:,2);
117NomType=Param.InputTable(:,4);
118FileExt=Param.InputTable(:,5);
[374]119[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[474]120%%%%%%%%%%%%
121% The cell array filecell is the list of input file names, while
122% filecell{iview,fileindex}:
[457]123%        iview: line in the table corresponding to a given file series
124%        fileindex: file index within  the file series,
125% 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
126% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
[474]127%%%%%%%%%%%%
[606]128% NbSlice=1;%default
129% if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
130%     NbSlice=Param.IndexRange.NbSlice;
131% end
132NbView=numel(i1_series);%number of input file series (lines in InputTable)
133NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
134NbField_i=size(i1_series{1},2); %nb of fields for the i index
135NbField=NbField_j*NbField_i; %total number of fields
[457]136
[673]137%% determine the file type on each line from the first input file
[462]138ImageTypeOptions={'image','multimage','mmreader','video'};
139NcTypeOptions={'netcdf','civx','civdata'};
[606]140for iview=1:NbView
[462]141    if ~exist(filecell{iview,1}','file')
[668]142        disp_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'],checkrun)
[462]143        return
144    end
[784]145    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
[783]146    FileType{iview}=FileInfo{iview}.FileType;
[462]147    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
[880]148    if CheckImage{iview}
149        ParamIn{iview}=MovieObject{iview};
150    else
151        ParamIn{iview}=Param.InputFields;
152    end
[462]153    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
154    if ~isempty(j1_series{iview})
155        frame_index{iview}=j1_series{iview};
156    else
157        frame_index{iview}=i1_series{iview};
158    end
[457]159end
[739]160if NbView >1 && max(cell2mat(CheckImage))>0 && ~isfield(Param,'ProjObject')
161    disp_uvmat('ERROR','projection on a common grid is needed to concatene images: use a Projection Object of type ''plane'' with ProjMode=''interp_lin''',checkrun)
162    return
163end
[457]164
165%% calibration data and timing: read the ImaDoc files
[470]166[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
167if size(time,1)>1
[457]168    diff_time=max(max(diff(time)));
[474]169    if diff_time>0
[676]170        disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun)
[457]171    end   
172end
[676]173if ~isempty(errormsg)
[829]174    disp_uvmat('WARNING',errormsg,checkrun)
[676]175end
176time=mean(time,1); %averaged time taken for the merged field
[457]177
[409]178%% coordinate transform or other user defined transform
[606]179transform_fct='';%default fct handle
[478]180if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[606]181        currentdir=pwd;
182        cd(Param.FieldTransform.TransformPath)
183        transform_fct=str2func(Param.FieldTransform.TransformName);
184        cd (currentdir)
[883]185        if isfield(Param,'TransformInput')
186            for iview=1:NbView
187            XmlData{iview}.TransformInput=Param.TransformInput;
188            end
189        end       
[374]190end
[457]191%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
192 % EDIT FROM HERE
[116]193
[457]194%% check the validity of  input file types
[606]195for iview=1:NbView
[673]196    if ~isequal(CheckImage{iview},1)&&~isequal(CheckNc{iview},1)
[668]197        disp_uvmat('ERROR','input set of input series: need  either netcdf either image series',checkrun)
[632]198        return
[464]199    end
[457]200end
[673]201
202%% output file type
203if min(cell2mat(CheckImage))==1 && (~Param.CheckObject || strcmp(Param.ProjObject.Type,'plane'))
204    FileExtOut='.png'; %image output (input and proj result = image)
205    for iview=1:NbView
206        BitDepth(iview)=FileInfo{iview}.BitDepth;
207    end
208    BitDepth=max(BitDepth);
209else
210    FileExtOut='.nc'; %netcdf output
211end
[880]212if isempty(j1_series{1})
213    NomTypeOut='_1';
214else
215    NomTypeOut='_1_1';
216end
[867]217%NomTypeOut=NomType;% output file index will indicate the first and last ref index in the series
[673]218RootFileOut=RootFile{1};
219for iview=2:NbView
220    if ~strcmp(RootFile{iview},RootFile{1})
221        RootFileOut='mproj';
222        break
223    end
224end
[457]225
[642]226%% mask (TODO: case of multilevels)
[668]227MaskData=cell(NbView,1);
[642]228if Param.CheckMask
[676]229    if ischar(Param.MaskTable)% case of a single mask (char chain)
230        Param.MaskTable={Param.MaskTable};
231    end
[668]232    for iview=1:numel(Param.MaskTable)
[673]233        if exist(Param.MaskTable{iview},'file')
234            [MaskData{iview},tild,errormsg] = read_field(Param.MaskTable{iview},'image');
235            if ~isempty(transform_fct) && nargin(transform_fct)>=2
236                MaskData{iview}=transform_fct(MaskData{iview},XmlData{iview});
237            end
[642]238        end
239    end
240end
241
[457]242%% Set field names and velocity types
[462]243%use Param.InputFields for all views
[457]244
[640]245%% MAIN LOOP ON FIELDS
[457]246%%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
[606]247% for i_slice=1:NbSlice
248%     index_slice=i_slice:NbSlice:NbField;% select file indices of the slice
249%     NbFiles=0;
250%     nbmissing=0;
[474]251
[457]252    %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
[880]253tstart=tic; %used to record the computing time
[938]254CheckOverwrite=1;%default
255if isfield(Param,'CheckOverwrite')
256    CheckOverwrite=Param.CheckOverwrite;
257end
[606]258for index=1:NbField
[626]259        update_waitbar(WaitbarHandle,index/NbField)
[642]260    if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
[624]261        disp('program stopped by user')
[606]262        return
263    end
[938]264   
265     %% generating the name of the merged field
266    i1=i1_series{1}(index);
267    if ~isempty(i2_series{end})
268        i2=i2_series{end}(index);
269    else
270        i2=i1;
271    end
272    j1=1;
273    j2=1;
274    if ~isempty(j1_series{1})
275        j1=j1_series{1}(index);
276        if ~isempty(j2_series{end})
277            j2=j2_series{end}(index);
278        else
279            j2=j1;
280        end
281    end
282    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFileOut,FileExtOut,NomTypeOut,i1,i2,j1,j2);
283    if ~CheckOverwrite && exist(OutputFile,'file')
284            disp(['existing output file ' OutputFile ' already exists, skip to next field'])
285            continue% skip iteration if the mode overwrite is desactivated and the result file already exists
286    end
287       
[606]288    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
289    Data=cell(1,NbView);%initiate the set Data
[676]290    timeread=zeros(1,NbView);
[606]291    for iview=1:NbView
[880]292        %% reading input file(s)     
293        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},ParamIn{iview},frame_index{iview}(index));
[606]294        if ~isempty(errormsg)
[746]295            disp_uvmat('ERROR',['ERROR in merge_proj/read_field/' errormsg],checkrun)
[614]296            return
[457]297        end
[938]298       
[857]299        ListVar=Data{iview}.ListVarName;
300        for ilist=1:numel(ListVar)
301            Data{iview}.(ListVar{ilist})=double(Data{iview}.(ListVar{ilist}));% transform all fields in double before all operations
302        end
[676]303        % get the time defined in the current file if not already defined from the xml file
304        if ~isempty(time) && isfield(Data{iview},'Time')
[606]305            timeread(iview)=Data{iview}.Time;
[595]306        end
[606]307        if ~isempty(NbSlice_calib)
308            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
309        end
[642]310       
[632]311        %% transform the input field (e.g; phys) if requested (no transform involving two input fields)
[606]312        if ~isempty(transform_fct)
313            if nargin(transform_fct)>=2
314                Data{iview}=transform_fct(Data{iview},XmlData{iview});
315            else
316                Data{iview}=transform_fct(Data{iview});
[29]317            end
[606]318        end
[632]319       
[642]320        %% calculate tps coefficients if needed
[614]321        check_proj_tps= isfield(Param,'ProjObject')&&~isempty(Param.ProjObject)&& strcmp(Param.ProjObject.ProjMode,'interp_tps')&&~isfield(Data{iview},'Coord_tps');
[606]322        Data{iview}=tps_coeff_field(Data{iview},check_proj_tps);
[642]323       
[606]324        %% projection on object (gridded plane)
325        if Param.CheckObject
326            [Data{iview},errormsg]=proj_field(Data{iview},Param.ProjObject);
327            if ~isempty(errormsg)
[746]328                disp_uvmat('ERROR',['ERROR in merge_proge/proj_field: ' errormsg],checkrun)
[606]329                return
[29]330            end
[464]331        end
[640]332       
333        %% mask
[642]334        if Param.CheckMask && ~isempty(MaskData{iview})
335             [Data{iview},errormsg]=mask_proj(Data{iview},MaskData{iview});
[640]336        end
[606]337    end
[673]338    %%%%%%%%%%%%%%%% END LOOP ON VIEWS %%%%%%%%%%%%%%%%
[606]339
340    %% merge the NbView fields
[739]341    [MergeData,errormsg]=merge_field(Data);
342    if ~isempty(errormsg)
343        disp_uvmat('ERROR',errormsg,checkrun);
[606]344        return
345    end
346
[673]347    %% time of the merged field: take the average of the different views
[676]348    if ~isempty(time)
349        timeread=time(index);   
350    elseif ~isempty(find(timeread))% time defined from ImaDoc
351        timeread=mean(timeread(timeread~=0));% take average over times form the files (when defined)
352    else
353        timeread=index;% take time=file index
[606]354    end
355
[938]356 
[673]357    %% recording the merged field
358    if strcmp(FileExtOut,'.png')    %output as image
359        if BitDepth==8
360            imwrite(uint8(MergeData.A),OutputFile,'BitDepth',8)
[464]361        else
[673]362            imwrite(uint16(MergeData.A),OutputFile,'BitDepth',16)
[464]363        end
[673]364        if index==1
365            %write xml calibration file, using the first file
366            siz=size(MergeData.A);
367            npy=siz(1);
368            npx=siz(2);
[880]369            if isfield(MergeData,'Coord_x') && isfield(MergeData,'Coord_y')
370                Rangx=MergeData.Coord_x;
371                Rangy=MergeData.Coord_y;
[673]372            elseif isfield(MergeData,'AX')&& isfield(MergeData,'AY')
373                Rangx=[MergeData.AX(1) MergeData.AX(end)];
374                Rangy=[MergeData.AY(1) MergeData.AY(end)];
375            else
376                Rangx=[0.5 npx-0.5];
377                Rangy=[npy-0.5 0.5];%default
378            end
379            pxcmx=(npx-1)/(Rangx(2)-Rangx(1));
380            pxcmy=(npy-1)/(Rangy(1)-Rangy(2));
381            T_x=-pxcmx*Rangx(1)+0.5;
382            T_y=-pxcmy*Rangy(2)+0.5;
383            GeometryCal.CalibrationType='rescale';
384            GeometryCal.CoordUnit=MergeData.CoordUnit;
385            GeometryCal.focal=1;
386            GeometryCal.R=[pxcmx,0,0;0,pxcmy,0;0,0,1];
387            GeometryCal.Tx_Ty_Tz=[T_x T_y 1];
388            ImaDoc.GeometryCalib=GeometryCal;
389            t=struct2xml(ImaDoc);
390            t=set(t,1,'name','ImaDoc');
391            save(t,[fileparts(OutputFile) '.xml'])
392        end
393       
[937]394    else   %output as netcdf files
395        MergeData.ListGlobalAttribute={'Conventions','Project','InputFile_1','InputFile_end','NbCoord','NbDim'};
[606]396        MergeData.Conventions='uvmat';
[937]397        MergeData.NbCoord=2;
398        MergeData.NbDim=2;
399        % time interval of PIV
400        Dt=[];
401        if isfield(Data{1},'Dt')&& isnumeric(Data{1}.Dt)
402            Dt=Data{1}.Dt;
[606]403        end
404        for iview =2:numel(Data)
[937]405            if ~(isfield(Data{iview},'Dt')&& isequal(Data{iview}.Dt,Dt))
406                Dt=[];%dt not the same for all fields
[464]407            end
408        end
[676]409        if ~isempty(timeread)
410            MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'Time'}];
411            MergeData.Time=timeread;
412        end
[937]413        % position of projection plane
414        if isfield(Data{1},'ProjObjectCoord')&& isfield(Data{1},'ProjObjectAngle')
415            ProjObjectCoord=Data{1}.ProjObjectCoord;
416            ProjObjectAngle=Data{1}.ProjObjectAngle;
417            for iview =2:numel(Data)
418                if ~(isfield(Data{iview},'ProjObjectCoord')&& isequal(Data{iview}.ProjObjectCoord,ProjObjectCoord))...
419                        ||~(isfield(Data{iview},'ProjObjectAngle')&& isequal(Data{iview}.ProjObjectAngle,ProjObjectAngle))
420                    ProjObjectCoord=[];%dt not the same for all fields
421                end
422            end
423            if ~isempty(ProjObjectCoord)
424                MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'ProjObjectCoord'} {'ProjObjectAngle'}];
425                MergeData.ProjObjectCoord=ProjObjectCoord;
426                MergeData.ProjObjectAngle=ProjObjectAngle;
427            end
[29]428        end
[937]429        % coord unit
430        if isfield(Data{1},'CoordUnit')
431            CoordUnit=Data{1}.CoordUnit;
432            for iview =2:numel(Data)
433                if ~(isfield(Data{iview},'CoordUnit')&& isequal(Data{iview}.CoordUnit,CoordUnit))
434                    CoordUnit=[];%CoordUnit not the same for all fields
435                end
436            end
437            if ~isempty(CoordUnit)
438                MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'CoordUnit'}];
439                MergeData.CoordUnit=CoordUnit;
440            end
441        end
442        % time unit
443        if isfield(Data{1},'TimeUnit')
444            TimeUnit=Data{1}.TimeUnit;
445            for iview =2:numel(Data)
446                if ~(isfield(Data{iview},'TimeUnit')&& isequal(Data{iview}.TimeUnit,TimeUnit))
447                    TimeUnit=[];%TimeUnit not the same for all fields
448                end
449            end
450            if ~isempty(TimeUnit)
451                MergeData.ListGlobalAttribute=[MergeData.ListGlobalAttribute {'TimeUnit'}];
452                MergeData.TimeUnit=TimeUnit;
453            end
454        end
[606]455        error=struct2nc(OutputFile,MergeData);%save result file
456        if isempty(error)
[739]457            disp(['output file ' OutputFile ' written'])
[606]458        else
[739]459            disp(error)
[606]460        end
[29]461    end
462end
[905]463ellapsed_time=toc(tstart);
464disp(['total ellapsed time ' num2str(ellapsed_time/60,2) ' minutes'])
465disp([ num2str(ellapsed_time/(60*NbField),3) ' minutes per iteration'])
[29]466
[222]467%'merge_field': concatene fields
468%------------------------------------------------------------------------
[739]469function [MergeData,errormsg]=merge_field(Data)
[222]470%% default output
[29]471if isempty(Data)||~iscell(Data)
472    MergeData=[];
473    return
474end
[739]475errormsg='';
[867]476MergeData=Data{1};% merged field= first field by default, reproduces the global attributes of the first field
[606]477NbView=length(Data);
[867]478if NbView==1% if there is only one field, just reproduce it in MergeData
479    return
[29]480end
[222]481
[522]482%% group the variables (fields of 'Data') in cells of variables with the same dimensions
[668]483[CellInfo,NbDim,errormsg]=find_field_cells(Data{1});
[739]484if ~isempty(errormsg)
485    return
486end
[670]487
[89]488%LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS
[668]489for icell=1:length(CellInfo)
[670]490    if NbDim(icell)~=1 % skip field cells which are of dim 1
491        switch CellInfo{icell}.CoordType
[867]492            case 'scattered'  %case of input fields with unstructured coordinates: just concatene data
[670]493                for ivar=CellInfo{icell}.VarIndex %  indices of the selected variables in the list FieldData.ListVarName
494                    VarName=Data{1}.ListVarName{ivar};
495                    for iview=2:NbView
496                        MergeData.(VarName)=[MergeData.(VarName); Data{iview}.(VarName)];
[89]497                    end
498                end
[670]499            case 'grid'        %case of fields defined on a structured  grid
500                FFName='';
[676]501                if isfield(CellInfo{icell},'VarIndex_errorflag') && ~isempty(CellInfo{icell}.VarIndex_errorflag)
[670]502                    FFName=Data{1}.ListVarName{CellInfo{icell}.VarIndex_errorflag};% name of errorflag variable
[867]503                    MergeData.ListVarName(CellInfo{icell}.VarIndex_errorflag)=[];%remove error flag variable in MergeData (will use NaN instead)
504                    MergeData.VarDimName(CellInfo{icell}.VarIndex_errorflag)=[];
505                    MergeData.VarAttribute(CellInfo{icell}.VarIndex_errorflag)=[];
[670]506                end
507                % select good data on each view
508                for ivar=CellInfo{icell}.VarIndex  %  indices of the selected variables in the list FieldData.ListVarName
509                    VarName=Data{1}.ListVarName{ivar};
510                    for iview=1:NbView
511                        if isempty(FFName)
512                            check_bad=isnan(Data{iview}.(VarName));%=0 for NaN data values, 1 else
513                        else
514                            check_bad=isnan(Data{iview}.(VarName)) | Data{iview}.(FFName)~=0;%=0 for NaN or error flagged data values, 1 else
515                        end
[867]516                        Data{iview}.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag
[670]517                        if iview==1
[867]518                            %MergeData.(VarName)=Data{1}.(VarName);% initiate MergeData with the first field
[874]519                            MergeData.(VarName)(check_bad)=0; %set to zero NaN or data marked by error flag
[670]520                            NbAver=~check_bad;% initiate NbAver: the nbre of good data for each point
[739]521                        elseif size(Data{iview}.(VarName))~=size(MergeData.(VarName))
522                            errormsg='sizes of the input matrices do not agree, need to interpolate on a common grid using a projection object';
523                            return
[867]524                        else                             
525                            MergeData.(VarName)=MergeData.(VarName) +double(Data{iview}.(VarName));%add data
[670]526                            NbAver=NbAver + ~check_bad;% add 1 for good data, 0 else
527                        end
528                    end
529                    MergeData.(VarName)(NbAver~=0)=MergeData.(VarName)(NbAver~=0)./NbAver(NbAver~=0);% take average of defined data at each point
[867]530                    MergeData.(VarName)(NbAver==0)=NaN;% set to NaN the points with no good data
[670]531                end
[89]532        end
[867]533%         if isempty(FFName)
534%             FFName='FF';
535%         end
536%         MergeData.(FFName)(NbAver~=0)=0;% flag to 1 undefined summed data
537%         MergeData.(FFName)(NbAver==0)=1;% flag to 1 undefined summed data
[89]538    end
539end
[222]540
[670]541
[810]542   
Note: See TracBrowser for help on using the repository browser.