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

Last change on this file since 1096 was 1096, checked in by sommeria, 3 years ago

bugs fixed

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