source: trunk/src/series/aver_stat.m @ 716

Last change on this file since 716 was 716, checked in by sommeria, 10 years ago

fct improved by reducing the input file check when selected.

File size: 14.4 KB
RevLine 
[573]1%'aver_stat': calculate field average over a time series
[169]2%------------------------------------------------------------------------
[457]3% function ParamOut=aver_stat(Param)
[169]4%
[447]5%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
[457]6%
[169]7%OUTPUT
[596]8% ParamOut: sets options in the GUI series.fig needed for the function
[169]9%
10%INPUT:
[447]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)
[447]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
[447]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%             
[447]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
[447]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
[447]38%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
[457]41function ParamOut=aver_stat(Param)
[447]42
43%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
[716]44if isstruct(Param) && isequal(Param.Action.RUN,0)% function activated from the GUI series but not RUN
[605]45    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
46    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
47    ParamOut.NbSlice='on'; %nbre of slices ('off' by default)
48    ParamOut.VelType='two';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
49    ParamOut.FieldName='two';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
50    ParamOut.FieldTransform = 'on';%can use a transform function
51    ParamOut.ProjObject='on';%can use projection object(option 'off'/'on',
52    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
[592]53    ParamOut.OutputDirExt='.stat';%set the output dir extension
[605]54    ParamOut.OutputFileMode='NbSlice';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
[716]55    % check the existence of the first file in the series
56    first_j=[];
57    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
58    last_j=[];
59    if isfield(Param.IndexRange,'last_j'); last_j=Param.IndexRange.last_j; end
60    PairString='';
61    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
62    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
63    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
64        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
65    if ~exist(FirstFileName,'file')
66        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
67    else
68        [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString);
69        LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
70        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
71        if ~exist(FirstFileName,'file')
72             msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist'])
73        end
[605]74    end
75    return
[27]76end
77
[474]78%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
[624]79ParamOut=[];%default output
[592]80%% read input parameters from an xml file if input is a file name (batch mode)
81checkrun=1;
[457]82if ischar(Param)
[592]83    Param=xml2struct(Param);% read Param as input file (batch case)
84    checkrun=0;
[361]85end
[624]86hseries=findobj(allchild(0),'Tag','series');
87RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
88WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
[592]89
[624]90%% define the directory for result file (with path=RootPath{1})
[474]91OutputDir=[Param.OutputSubDir Param.OutputDirExt];
92   
[609]93%% root input file(s) name, type and index series
[457]94RootPath=Param.InputTable(:,1);
95RootFile=Param.InputTable(:,3);
96SubDir=Param.InputTable(:,2);
97NomType=Param.InputTable(:,4);
98FileExt=Param.InputTable(:,5);
[374]99[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[474]100%%%%%%%%%%%%
101% The cell array filecell is the list of input file names, while
102% filecell{iview,fileindex}:
[447]103%        iview: line in the table corresponding to a given file series
104%        fileindex: file index within  the file series,
105% 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
106% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
[474]107%%%%%%%%%%%%
[673]108NbView=numel(i1_series);%number of input file series (lines in InputTable)
109NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
110NbField_i=size(i1_series{1},2); %nb of fields for the i index
111NbField=NbField_j*NbField_i; %total number of fields
[43]112
[609]113%% determine the file type on each line from the first input file
[447]114ImageTypeOptions={'image','multimage','mmreader','video'};
115NcTypeOptions={'netcdf','civx','civdata'};
[673]116for iview=1:NbView
[451]117    if ~exist(filecell{iview,1}','file')
118        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
119        return
120    end
121    [FileType{iview},FileInfo{iview},MovieObject{iview}]=get_file_type(filecell{iview,1});
[447]122    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
123    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
[454]124    if ~isempty(j1_series{iview})
125        frame_index{iview}=j1_series{iview};
126    else
127        frame_index{iview}=i1_series{iview};
128    end
[27]129end
130
[451]131%% calibration data and timing: read the ImaDoc files
[470]132[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
133if size(time,1)>1
[27]134    diff_time=max(max(diff(time)));
135    if diff_time>0
[447]136        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
[27]137    end   
138end
139
[447]140%% coordinate transform or other user defined transform
141transform_fct='';%default
[478]142if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]143    addpath(Param.FieldTransform.TransformPath)
144    transform_fct=str2func(Param.FieldTransform.TransformName);
145    rmpath(Param.FieldTransform.TransformPath)
[27]146end
[474]147
[447]148%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
149 % EDIT FROM HERE
150
151%% check the validity of  input file types
152if CheckImage{1}
153    FileExtOut='.png'; % write result as .png images for image inputs
154elseif CheckNc{1}
155    FileExtOut='.nc';% write result as .nc files for netcdf inputs
[635]156else
[447]157    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
158    return
159end
[673]160if NbView==2 && ~isequal(CheckImage{1},CheckImage{2})
[635]161    msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
[447]162    return
163end
[635]164
165%% settings for the output file
[609]166NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
[635]167first_i=i1_series{1}(1);
168last_i=i1_series{1}(end);
169if isempty(j1_series{1})% if there is no second index j
170    first_j=1;last_j=1;
171else
172    first_j=j1_series{1}(1);
173    last_j=j1_series{1}(end);
174end
[442]175
[447]176%% Set field names and velocity types
177InputFields{1}=[];%default (case of images)
178if isfield(Param,'InputFields')
179    InputFields{1}=Param.InputFields;
180end
[673]181if NbView==2
[447]182    InputFields{2}=[];%default (case of images)
183    if isfield(Param,'InputFields')
184        InputFields{2}=Param.InputFields{1};%default
185        if isfield(Param.InputFields,'FieldName_1')
186            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
187            if isfield(Param.InputFields,'VelType_1')
188                InputFields{2}.VelType=Param.InputFields.VelType_1;
189            end
190        end
[255]191    end
192end
[447]193
[595]194% for i_slice=1:NbSlice
[598]195% index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
196nbfiles=0;
197nbmissing=0;
[474]198
[598]199%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
[673]200for index=1:NbField
201    update_waitbar(WaitbarHandle,index/NbField)
[635]202    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
[624]203        disp('program stopped by user')
204        break
205    end
206   
[598]207    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
[673]208    for iview=1:NbView
[598]209        % reading input file(s)
210        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
211        if ~isempty(errormsg)
212            errormsg=['error of input reading: ' errormsg];
213            break
[447]214        end
[598]215        if ~isempty(NbSlice_calib)
216            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[526]217        end
[598]218    end
219    %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
220    %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
221    % EDIT FROM HERE
[624]222   
[598]223    if isempty(errormsg)
224        Field=Data{1}; % default input field structure
225        %% coordinate transform (or other user defined transform)
226        if ~isempty(transform_fct)
227            switch nargin(transform_fct)
228                case 4
229                    if length(Data)==2
230                        Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
231                    else
[526]232                        Field=transform_fct(Data{1},XmlData{1});
[598]233                    end
234                case 3
235                    if length(Data)==2
236                        Field=transform_fct(Data{1},XmlData{1},Data{2});
237                    else
238                        Field=transform_fct(Data{1},XmlData{1});
239                    end
240                case 2
241                    Field=transform_fct(Data{1},XmlData{1});
242                case 1
243                    Field=transform_fct(Data{1});
[255]244            end
[598]245        end
[624]246       
[598]247        %% calculate tps coefficients if needed
248        if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
249            Field=tps_coeff_field(Field,check_proj_tps);
250        end
[624]251       
[598]252        %field projection on an object
253        if Param.CheckObject
254            [Field,errormsg]=proj_field(Field,Param.ProjObject);
255            if ~isempty(errormsg)
256                msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
257                return
[494]258            end
[598]259        end
260        nbfiles=nbfiles+1;
[624]261       
[598]262        %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
263        if nbfiles==1 %first field
264            time_1=[];
265            if isfield(Field,'Time')
266                time_1=Field.Time(1);
267            end
268            DataOut=Field;%default
[668]269            DataOut.Conventions='uvmat'; %suppress Conventions='uvmat/civdata' for civ input files
[598]270            for ivar=1:length(Field.ListVarName)
271                VarName=Field.ListVarName{ivar};
272                DataOut.(VarName)=double(DataOut.(VarName));
273            end
274        else   %current field
275            for ivar=1:length(Field.ListVarName)
276                VarName=Field.ListVarName{ivar};
277                sizmean=size(DataOut.(VarName));
278                siz=size(Field.(VarName));
279                if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
[668]280                    disp_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'],checkrun)
[27]281                    return
[598]282                else
283                    DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
[27]284                end
[255]285            end
[447]286        end
[598]287        %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
288    else
[609]289        disp(errormsg)
[255]290    end
[598]291end
292%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
293
294for ivar=1:length(Field.ListVarName)
295    VarName=Field.ListVarName{ivar};
296    DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
297end
298if nbmissing~=0
299    msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
300end
301if isempty(time) % time is read from files
302    if isfield(Field,'Time')
303        time_end=Field.Time(1);%last time read
304        if ~isempty(time_1)
305            DataOut.Time=time_1;
306            DataOut.Time_end=time_end;
307        end
[27]308    end
[598]309else  % time from ImaDoc prevails if it exists
310    DataOut.Time=time(1);
311    DataOut.Time_end=time(end);
312end
313
[609]314%% writing the result file
315OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j);
[598]316if CheckImage{1} %case of images
317    if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16))
318        DataOut.A=uint16(DataOut.A);
319        imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images
320    else
321        DataOut.A=uint8(DataOut.A);
322        imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
[27]323    end
[609]324    disp([OutputFile ' written']);
[598]325else %case of netcdf input file , determine global attributes
326    errormsg=struct2nc(OutputFile,DataOut); %save result file
327    if isempty(errormsg)
[609]328        disp([OutputFile ' written']);
[598]329    else
[609]330        disp(['error in writting result file: ' errormsg])
[598]331    end
332end  % end averaging  loop
[255]333
[451]334%% open the result file with uvmat (in RUN mode)
335if checkrun
336    uvmat(OutputFile)% open the last result file with uvmat
337end
Note: See TracBrowser for help on using the repository browser.