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

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