source: trunk/src/series/turb_stat.m @ 1032

Last change on this file since 1032 was 1032, checked in by sommeria, 3 years ago

reading LaVision? images improved

File size: 13.7 KB
Line 
1%'aver_stat': calculate Reynolds steress components over time series
2%------------------------------------------------------------------------
3% function ParamOut=turb_stat(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=turb_stat(Param)
60
61%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
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=1; %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='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
68    ParamOut.FieldTransform = 'off';%can use a transform function
69    ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
70    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
71    ParamOut.OutputDirExt='.staturb';%set the output dir extension
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
73%     filecell=get_file_series(Param);%check existence of the first input file
74%     if ~exist(filecell{1,1},'file')
75%         msgbox_uvmat('WARNING','the first input file does not exist')
76%     end
77    return
78end
79
80%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
81ParamOut=[];%default output
82%% read input parameters from an xml file if input is a file name (batch mode)
83checkrun=1;
84if ischar(Param)
85    Param=xml2struct(Param);% read Param as input file (batch case)
86    checkrun=0;
87end
88hseries=findobj(allchild(0),'Tag','series');
89RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
90WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
91
92%% define the directory for result file (with path=RootPath{1})
93OutputDir=[Param.OutputSubDir Param.OutputDirExt];
94   
95%% root input file(s) name, type and index series
96RootPath=Param.InputTable(:,1);
97RootFile=Param.InputTable(:,3);
98SubDir=Param.InputTable(:,2);
99NomType=Param.InputTable(:,4);
100FileExt=Param.InputTable(:,5);
101[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
102%%%%%%%%%%%%
103% The cell array filecell is the list of input file names, while
104% filecell{iview,fileindex}:
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
109%%%%%%%%%%%% NbView=1 : a single input series
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
114
115%% determine the file type on each line from the first input file
116ImageTypeOptions={'image','multimage','mmreader','video','cine_phantom'};
117NcTypeOptions={'netcdf','civx','civdata'};
118for iview=1:NbView
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
123    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
124    FileType{iview}=FileInfo{iview}.FileType;
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
127    if ~isempty(j1_series{iview})
128        frame_index{iview}=j1_series{iview};
129    else
130        frame_index{iview}=i1_series{iview};
131    end
132end
133
134%% calibration data and timing: read the ImaDoc files
135XmlData=[];
136[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
137if size(time,1)>1
138    diff_time=max(max(diff(time)));
139    if diff_time>0
140        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
141    end   
142end
143
144%% coordinate transform or other user defined transform
145transform_fct='';%default
146if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
147    addpath(Param.FieldTransform.TransformPath)
148    transform_fct=str2func(Param.FieldTransform.TransformName);
149    rmpath(Param.FieldTransform.TransformPath)
150end
151
152%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
153 % EDIT FROM HERE
154
155%% check the validity of  input file types
156if CheckImage{1}
157    FileExtOut='.png'; % write result as .png images for image inputs
158elseif CheckNc{1}
159    FileExtOut='.nc';% write result as .nc files for netcdf inputs
160else
161    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
162    return
163end
164
165
166%% settings for the output file
167NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
168first_i=i1_series{1}(1);
169last_i=i1_series{1}(end);
170if isempty(j1_series{1})% if there is no second index j
171    first_j=1;last_j=1;
172else
173    first_j=j1_series{1}(1);
174    last_j=j1_series{1}(end);
175end
176
177%% Set field names and velocity types
178InputFields{1}=[];%default (case of images)
179if isfield(Param,'InputFields')
180    InputFields{1}=Param.InputFields;
181end
182if isempty(InputFields{1}.FieldName)
183    disp('ERROR: input fields U, V and posibly curl and div must be entered by get_field...')
184    return
185end
186
187nbfiles=0;
188nbmissing=0;
189
190%initialisation
191DataOut.ListGlobalAttribute= {'Conventions'};
192DataOut.Conventions= 'uvmat';
193DataOut.ListVarName={'coord_y', 'coord_x' ,'UMean' , 'VMean','u2Mean','v2Mean','u2Mean_1','v2Mean_1','uvMean','CurlMean','DivMean','Curl2Mean','Div2Mean','Counter'};
194DataOut.VarDimName={'coord_y','coord_x',{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},...
195    {'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'}};
196DataOut.UMean=0;
197DataOut.VMean=0;
198DataOut.u2Mean=0;
199DataOut.v2Mean=0;
200DataOut.u2Mean_1=0;
201DataOut.v2Mean_1=0;
202DataOut.uvMean=0;
203DataOut.Counter=0;
204DataOut.CurlMean=0;
205DataOut.DivMean=0;
206DataOut.Curl2Mean=0;
207DataOut.Div2Mean=0;
208U2Mean=0;
209V2Mean=0;
210UVMean=0;
211U2Mean_1=0;
212V2Mean_1=0;
213Counter_1=0;
214
215%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
216for index=1:NbField
217    update_waitbar(WaitbarHandle,index/NbField)
218    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
219        disp('program stopped by user')
220        break
221    end
222    [Field,tild,errormsg] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
223
224    %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
225    if index==1 %initiate the output data structure in the first field
226       
227        DataOut.coord_y=Field.coord_y;
228        DataOut.coord_x=Field.coord_x;
229        Uprev=Field.U;% store the current field for next iteration
230        Vprev=Field.V;
231        if isfield(Field,'FF')
232        FFprev=Field.FF;% possible flag for false data
233        else
234            %FFprev=true(size(Field.U));
235            FFprev=isnan(Field.U);
236        end
237    end
238    FF=isnan(Field.U);%|Field.U<-60|Field.U>30;% threshold on U
239    DataOut.Counter=DataOut.Counter+ (~FF);% add 1 to the couter for non NaN point
240    Counter_1=Counter_1+(~FF & ~FFprev);
241    Field.U(FF)=0;% set to 0 the nan values
242    Field.V(FF)=0;
243    DataOut.UMean=DataOut.UMean+Field.U; %increment the sum
244    DataOut.VMean=DataOut.VMean+Field.V; %increment the sum
245    U2Mean=U2Mean+(Field.U).*(Field.U); %increment the U squared sum
246    V2Mean=V2Mean+(Field.V).*(Field.V); %increment the V squared sum
247    UVMean=UVMean+(Field.U).*(Field.V); %increment the sum
248    U2Mean_1=U2Mean_1+(Field.U).*Uprev; %increment the U squared sum
249    V2Mean_1=V2Mean_1+(Field.V).*Vprev; %increment the V squared sum
250    Uprev=Field.U; %store for next iteration
251    Vprev=Field.V;
252    FFprev=FF;
253    if isfield(Field,'curl') && isfield(Field,'div')
254        Field.curl(FF)=0;% set to 0 the nan values
255        Field.div(FF)=0;
256        DataOut.CurlMean=DataOut.CurlMean+Field.curl;
257        DataOut.DivMean=DataOut.DivMean+Field.div;
258        DataOut.Curl2Mean=DataOut.Curl2Mean+Field.curl.*Field.curl;
259        DataOut.Div2Mean=DataOut.Div2Mean+Field.div.*Field.div;
260    end
261end
262%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
263
264DataOut.Counter(DataOut.Counter==0)=1;% put counter to 1 when it is zero
265DataOut.UMean=DataOut.UMean./DataOut.Counter; % normalize the mean
266DataOut.VMean=DataOut.VMean./DataOut.Counter; % normalize the mean
267U2Mean=U2Mean./DataOut.Counter; % normalize the mean
268V2Mean=V2Mean./DataOut.Counter; % normalize the mean
269UVMean=UVMean./DataOut.Counter; % normalize the mean
270U2Mean_1=U2Mean_1./Counter_1; % normalize the mean
271V2Mean_1=V2Mean_1./Counter_1; % normalize the mean
272DataOut.u2Mean=U2Mean-DataOut.UMean.*DataOut.UMean; % normalize the mean
273DataOut.v2Mean=V2Mean-DataOut.VMean.*DataOut.VMean; % normalize the mean
274DataOut.uvMean=UVMean-DataOut.UMean.*DataOut.VMean; % normalize the mean \
275DataOut.u2Mean_1=U2Mean_1-DataOut.UMean.*DataOut.UMean; % normalize the mean
276DataOut.v2Mean_1=V2Mean_1-DataOut.VMean.*DataOut.VMean; % normalize the mean
277DataOut.CurlMean=DataOut.CurlMean./DataOut.Counter;
278DataOut.DivMean=DataOut.DivMean./DataOut.Counter;
279DataOut.Curl2Mean=DataOut.Curl2Mean./DataOut.Counter-DataOut.CurlMean.*DataOut.CurlMean;
280DataOut.Div2Mean=DataOut.Div2Mean./DataOut.Counter-DataOut.DivMean.*DataOut.DivMean;
281
282%% calculate the profiles
283% npx=numel(DataOut.coord_x);
284% band=ceil(npx/5) :floor(4*npx/5);% keep only the central band
285% for ivar=3:numel(DataOut.ListVarName)-1
286%     VarName=DataOut.ListVarName{ivar};% name of the variable
287%     DataOut.ListVarName=[DataOut.ListVarName {[VarName 'Profile']}];%append the name of the profile variable
288%     DataOut.VarDimName=[DataOut.VarDimName {'coord_y'}];
289%    DataOut.([VarName 'Profile'])=mean(DataOut.(VarName)(:,band),2); %take the mean profile of U, excluding the edges
290% end
291
292%% writing the result file as netcdf file
293OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j);
294 %case of netcdf input file , determine global attributes
295 errormsg=struct2nc(OutputFile,DataOut); %save result file
296 if isempty(errormsg)
297     disp([OutputFile ' written']);
298 else
299     disp(['error in writting result file: ' errormsg])
300 end
301
302
303%% open the result file with uvmat (in RUN mode)
304if checkrun
305    uvmat(OutputFile)% open the last result file with uvmat
306end
Note: See TracBrowser for help on using the repository browser.