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

Last change on this file since 1030 was 1030, checked in by sommeria, 7 years ago

updated for Matlab 2016

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