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

Last change on this file since 789 was 784, checked in by sommeria, 11 years ago

adpat uvmat to reading data from rdvision

File size: 14.6 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);
[751]99hdisp=disp_uvmat('WAITING...','checking the file series',checkrun);
[374]100[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
[751]101if ~isempty(hdisp),delete(hdisp),end;
[474]102%%%%%%%%%%%%
103% The cell array filecell is the list of input file names, while
104% filecell{iview,fileindex}:
[447]105%        iview: line in the table corresponding to a given file series
106%        fileindex: file index within  the file series,
107% 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
108% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
[474]109%%%%%%%%%%%%
[673]110NbView=numel(i1_series);%number of input file series (lines in InputTable)
111NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
112NbField_i=size(i1_series{1},2); %nb of fields for the i index
113NbField=NbField_j*NbField_i; %total number of fields
[43]114
[609]115%% determine the file type on each line from the first input file
[447]116ImageTypeOptions={'image','multimage','mmreader','video'};
117NcTypeOptions={'netcdf','civx','civdata'};
[673]118for iview=1:NbView
[451]119    if ~exist(filecell{iview,1}','file')
120        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
121        return
122    end
[784]123    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
[783]124    FileType{iview}=FileInfo{iview}.FileType;
[447]125    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
126    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
[454]127    if ~isempty(j1_series{iview})
128        frame_index{iview}=j1_series{iview};
129    else
130        frame_index{iview}=i1_series{iview};
131    end
[27]132end
133
[451]134%% calibration data and timing: read the ImaDoc files
[470]135[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
136if size(time,1)>1
[27]137    diff_time=max(max(diff(time)));
138    if diff_time>0
[447]139        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
[27]140    end   
141end
142
[447]143%% coordinate transform or other user defined transform
144transform_fct='';%default
[478]145if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
[474]146    addpath(Param.FieldTransform.TransformPath)
147    transform_fct=str2func(Param.FieldTransform.TransformName);
148    rmpath(Param.FieldTransform.TransformPath)
[27]149end
[474]150
[447]151%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
152 % EDIT FROM HERE
153
154%% check the validity of  input file types
155if CheckImage{1}
156    FileExtOut='.png'; % write result as .png images for image inputs
157elseif CheckNc{1}
158    FileExtOut='.nc';% write result as .nc files for netcdf inputs
[635]159else
[447]160    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
161    return
162end
[673]163if NbView==2 && ~isequal(CheckImage{1},CheckImage{2})
[635]164    msgbox_uvmat('ERROR','input must be two image series or two netcdf file series')
[447]165    return
166end
[635]167
168%% settings for the output file
[609]169NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
[635]170first_i=i1_series{1}(1);
171last_i=i1_series{1}(end);
172if isempty(j1_series{1})% if there is no second index j
173    first_j=1;last_j=1;
174else
175    first_j=j1_series{1}(1);
176    last_j=j1_series{1}(end);
177end
[442]178
[447]179%% Set field names and velocity types
180InputFields{1}=[];%default (case of images)
181if isfield(Param,'InputFields')
182    InputFields{1}=Param.InputFields;
183end
[673]184if NbView==2
[447]185    InputFields{2}=[];%default (case of images)
186    if isfield(Param,'InputFields')
187        InputFields{2}=Param.InputFields{1};%default
188        if isfield(Param.InputFields,'FieldName_1')
189            InputFields{2}.FieldName=Param.InputFields.FieldName_1;
190            if isfield(Param.InputFields,'VelType_1')
191                InputFields{2}.VelType=Param.InputFields.VelType_1;
192            end
193        end
[255]194    end
195end
[447]196
[595]197% for i_slice=1:NbSlice
[598]198% index_slice=i_slice:NbSlice:nbfield;% select file indices of the slice
199nbfiles=0;
200nbmissing=0;
[474]201
[598]202%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
[673]203for index=1:NbField
204    update_waitbar(WaitbarHandle,index/NbField)
[635]205    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
[624]206        disp('program stopped by user')
207        break
208    end
209   
[598]210    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
[673]211    for iview=1:NbView
[598]212        % reading input file(s)
213        [Data{iview},tild,errormsg] = read_field(filecell{iview,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
214        if ~isempty(errormsg)
215            errormsg=['error of input reading: ' errormsg];
216            break
[447]217        end
[598]218        if ~isempty(NbSlice_calib)
219            Data{iview}.ZIndex=mod(i1_series{iview}(index)-1,NbSlice_calib{iview})+1;%Zindex for phys transform
[526]220        end
[598]221    end
222    %%%%%%%%%%%%%%%% end loop on views (input lines) %%%%%%%%%%%%%%%%
223    %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
224    % EDIT FROM HERE
[624]225   
[598]226    if isempty(errormsg)
227        Field=Data{1}; % default input field structure
228        %% coordinate transform (or other user defined transform)
229        if ~isempty(transform_fct)
230            switch nargin(transform_fct)
231                case 4
232                    if length(Data)==2
233                        Field=transform_fct(Data{1},XmlData{1},Data{2},XmlData{2});
234                    else
[526]235                        Field=transform_fct(Data{1},XmlData{1});
[598]236                    end
237                case 3
238                    if length(Data)==2
239                        Field=transform_fct(Data{1},XmlData{1},Data{2});
240                    else
241                        Field=transform_fct(Data{1},XmlData{1});
242                    end
243                case 2
244                    Field=transform_fct(Data{1},XmlData{1});
245                case 1
246                    Field=transform_fct(Data{1});
[255]247            end
[598]248        end
[624]249       
[598]250        %% calculate tps coefficients if needed
251        if isfield(Param,'ProjObject')&&isfield(Param.ProjObject,'ProjMode')&& strcmp(Param.ProjObject.ProjMode,'interp_tps')
252            Field=tps_coeff_field(Field,check_proj_tps);
253        end
[624]254       
[598]255        %field projection on an object
256        if Param.CheckObject
257            [Field,errormsg]=proj_field(Field,Param.ProjObject);
258            if ~isempty(errormsg)
259                msgbox_uvmat('ERROR',['error in aver_stat/proj_field:' errormsg])
260                return
[494]261            end
[598]262        end
263        nbfiles=nbfiles+1;
[624]264       
[598]265        %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
266        if nbfiles==1 %first field
267            time_1=[];
268            if isfield(Field,'Time')
269                time_1=Field.Time(1);
270            end
271            DataOut=Field;%default
[668]272            DataOut.Conventions='uvmat'; %suppress Conventions='uvmat/civdata' for civ input files
[598]273            for ivar=1:length(Field.ListVarName)
274                VarName=Field.ListVarName{ivar};
275                DataOut.(VarName)=double(DataOut.(VarName));
276            end
277        else   %current field
278            for ivar=1:length(Field.ListVarName)
279                VarName=Field.ListVarName{ivar};
280                sizmean=size(DataOut.(VarName));
281                siz=size(Field.(VarName));
282                if ~isequal(DataOut.(VarName),0)&& ~isequal(siz,sizmean)
[668]283                    disp_uvmat('ERROR',['unequal size of input field ' VarName ', need to project  on a grid'],checkrun)
[27]284                    return
[598]285                else
286                    DataOut.(VarName)=DataOut.(VarName)+ double(Field.(VarName)); % update the sum
[27]287                end
[255]288            end
[447]289        end
[598]290        %%%%%%%%%%%%   END MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
291    else
[609]292        disp(errormsg)
[255]293    end
[598]294end
295%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
296
297for ivar=1:length(Field.ListVarName)
298    VarName=Field.ListVarName{ivar};
299    DataOut.(VarName)=DataOut.(VarName)/nbfiles; % normalize the mean
300end
301if nbmissing~=0
302    msgbox_uvmat('WARNING',[num2str(nbmissing) ' input files are missing or skipted'])
303end
304if isempty(time) % time is read from files
305    if isfield(Field,'Time')
306        time_end=Field.Time(1);%last time read
307        if ~isempty(time_1)
308            DataOut.Time=time_1;
309            DataOut.Time_end=time_end;
310        end
[27]311    end
[598]312else  % time from ImaDoc prevails if it exists
313    DataOut.Time=time(1);
314    DataOut.Time_end=time(end);
315end
316
[609]317%% writing the result file
318OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j);
[598]319if CheckImage{1} %case of images
320    if isequal(FileInfo{1}.BitDepth,16)||(numel(FileInfo)==2 &&isequal(FileInfo{2}.BitDepth,16))
321        DataOut.A=uint16(DataOut.A);
322        imwrite(DataOut.A,OutputFile,'BitDepth',16); % case of 16 bit images
323    else
324        DataOut.A=uint8(DataOut.A);
325        imwrite(DataOut.A,OutputFile,'BitDepth',8); % case of 16 bit images
[27]326    end
[609]327    disp([OutputFile ' written']);
[598]328else %case of netcdf input file , determine global attributes
329    errormsg=struct2nc(OutputFile,DataOut); %save result file
330    if isempty(errormsg)
[609]331        disp([OutputFile ' written']);
[598]332    else
[609]333        disp(['error in writting result file: ' errormsg])
[598]334    end
335end  % end averaging  loop
[255]336
[451]337%% open the result file with uvmat (in RUN mode)
338if checkrun
339    uvmat(OutputFile)% open the last result file with uvmat
340end
Note: See TracBrowser for help on using the repository browser.