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

Last change on this file since 1135 was 1135, checked in by sommeria, 6 months ago

fct for python added

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