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

Last change on this file since 1160 was 1129, checked in by sommeria, 9 months ago

LIF calibration update and extrct_multitif

File size: 14.3 KB
RevLine 
[1129]1%'aver_stat': calculate Reynolds stress components over time series
[751]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
[810]41%=======================================================================
[1126]42% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[810]43%   http://www.legi.grenoble-inp.fr
[1127]44%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[810]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
[751]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)
[1082]65    ParamOut.NbSlice='on'; %nbre of slices ('on' if needed as input, fixed value e.g. 1, 'off' by default)
[751]66    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
[1031]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
[751]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%% root input file(s) name, type and index series
93RootPath=Param.InputTable(:,1);
94RootFile=Param.InputTable(:,3);
95SubDir=Param.InputTable(:,2);
96NomType=Param.InputTable(:,4);
97FileExt=Param.InputTable(:,5);
98[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
99%%%%%%%%%%%%
100% The cell array filecell is the list of input file names, while
101% filecell{iview,fileindex}:
102%        iview: line in the table corresponding to a given file series
103%        fileindex: file index within  the file series,
104% 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
105% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
106%%%%%%%%%%%% NbView=1 : a single input series
107NbView=numel(i1_series);%number of input file series (lines in InputTable)
108NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
109NbField_i=size(i1_series{1},2); %nb of fields for the i index
110NbField=NbField_j*NbField_i; %total number of fields
111
112%% determine the file type on each line from the first input file
113NcTypeOptions={'netcdf','civx','civdata'};
114for iview=1:NbView
115    if ~exist(filecell{iview,1}','file')
116        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
117        return
118    end
[784]119    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
[783]120    FileType{iview}=FileInfo{iview}.FileType;
[751]121    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
122    if ~isempty(j1_series{iview})
123        frame_index{iview}=j1_series{iview};
124    else
125        frame_index{iview}=i1_series{iview};
126    end
127end
128
129%% calibration data and timing: read the ImaDoc files
130XmlData=[];
131[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
132if size(time,1)>1
133    diff_time=max(max(diff(time)));
134    if diff_time>0
135        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
136    end   
137end
138
139%% coordinate transform or other user defined transform
140transform_fct='';%default
141if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
142    addpath(Param.FieldTransform.TransformPath)
143    transform_fct=str2func(Param.FieldTransform.TransformName);
144    rmpath(Param.FieldTransform.TransformPath)
145end
146
147%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
148 % EDIT FROM HERE
149
150%% settings for the output file
[1033]151FileExtOut='.nc';% write result as .nc files for netcdf inputs
[751]152NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
153first_i=i1_series{1}(1);
154last_i=i1_series{1}(end);
155if isempty(j1_series{1})% if there is no second index j
156    first_j=1;last_j=1;
157else
158    first_j=j1_series{1}(1);
159    last_j=j1_series{1}(end);
160end
161
162%% Set field names and velocity types
163InputFields{1}=[];%default (case of images)
164if isfield(Param,'InputFields')
165    InputFields{1}=Param.InputFields;
166end
[1032]167if isempty(InputFields{1}.FieldName)
168    disp('ERROR: input fields U, V and posibly curl and div must be entered by get_field...')
169    return
170end
[751]171
172nbfiles=0;
173nbmissing=0;
174
175%initialisation
176DataOut.ListGlobalAttribute= {'Conventions'};
177DataOut.Conventions= 'uvmat';
[1082]178DataOut.ListVarName={};
179DataOut.VarDimName={};
[751]180DataOut.UMean=0;
181DataOut.VMean=0;
182DataOut.u2Mean=0;
183DataOut.v2Mean=0;
184DataOut.u2Mean_1=0;
185DataOut.v2Mean_1=0;
186DataOut.uvMean=0;
187DataOut.Counter=0;
[1032]188DataOut.CurlMean=0;
189DataOut.DivMean=0;
190DataOut.Curl2Mean=0;
191DataOut.Div2Mean=0;
[1077]192DataOut.KEflux=0;
[751]193U2Mean=0;
194V2Mean=0;
195UVMean=0;
196U2Mean_1=0;
197V2Mean_1=0;
[761]198Counter_1=0;
199
[1129]200if Param.IndexRange.NbSlice==1
201    interval=Param.IndexRange.incr_i% statistics is done taking into account the input index increment
202else
203    interval=Param.IndexRange.NbSlice;% statistics is done slice by slice without taking into account the input index increment
204end
205
[751]206%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
[1129]207for i_slice=1:Param.IndexRange.NbSlice
208    i_slice
[1115]209    ind_first=Param.IndexRange.first_i;
[1129]210    for index_i=ind_first:interval:Param.IndexRange.last_i
[1100]211        if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
212            disp('program stopped by user')
213            break
214        end
[1115]215        for index_j=first_j:last_j
[1129]216            InputFile=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},index_i,index_i,index_j,index_j)
217            [Field,tild,errormsg] = read_field(InputFile,FileType{iview},InputFields{iview});       
[1100]218           
219            %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
[1115]220            if index_i==ind_first && index_j==first_j %initiate the output data structure in the first field
[1100]221                [CellInfo,NbDim,errormsg]=find_field_cells(Field);
222                YName='coord_y';%default
223                XName='coord_x';%default
224                for icell=1:numel(NbDim)
225                    if NbDim(icell)==2 && strcmp(CellInfo{icell}.CoordType,'grid')
226                        YName=CellInfo{icell}.YName;
227                        XName=CellInfo{icell}.XName;
228                        break
229                    end
230                end
231                DataOut.ListVarName={YName, XName ,'UMean' , 'VMean','u2Mean','v2Mean','u2Mean_1','v2Mean_1','uvMean','CurlMean','DivMean','Curl2Mean','Div2Mean','Counter'};
232                DataOut.VarDimName={YName,XName,{YName,XName},{YName,XName},{YName,XName},{YName,XName},{YName,XName},{YName,XName},...
233                    {YName,XName},{YName,XName},{YName,XName},{YName,XName},{YName,XName},{YName,XName}};
234                DataOut.(YName)=Field.(YName);
235                DataOut.(XName)=Field.(XName);
236                Uprev=Field.U;% store the current field for next iteration
237                Vprev=Field.V;
238                if isfield(Field,'FF')
239                    FFprev=Field.FF;% possible flag for false data
240                else
241                    %FFprev=true(size(Field.U));
242                    FFprev=isnan(Field.U);
243                end
[1082]244            end
[1100]245            FF=isnan(Field.U);%|Field.U<-60|Field.U>30;% threshold on U
246            DataOut.Counter=DataOut.Counter+ (~FF);% add 1 to the couter for non NaN point
247            Counter_1=Counter_1+(~FF & ~FFprev);
248            Field.U(FF)=0;% set to 0 the nan values
249            Field.V(FF)=0;
250            DataOut.UMean=DataOut.UMean+Field.U; %increment the sum
251            DataOut.VMean=DataOut.VMean+Field.V; %increment the sum
252           
253            U2Mean=U2Mean+(Field.U).*(Field.U); %increment the U squared sum
254            V2Mean=V2Mean+(Field.V).*(Field.V); %increment the V squared sum
255            UVMean=UVMean+(Field.U).*(Field.V); %increment the sum
256            U2Mean_1=U2Mean_1+(Field.U).*Uprev; %increment the U squared sum
257            V2Mean_1=V2Mean_1+(Field.V).*Vprev; %increment the V squared sum
258            Uprev=Field.U; %store for next iteration
259            Vprev=Field.V;
260            FFprev=FF;
261            if isfield(Field,'curl') && isfield(Field,'div')
262                Field.curl(FF)=0;% set to 0 the nan values
263                Field.div(FF)=0;
264                DataOut.CurlMean=DataOut.CurlMean+Field.curl;
265                DataOut.DivMean=DataOut.DivMean+Field.div;
266                DataOut.Curl2Mean=DataOut.Curl2Mean+Field.curl.*Field.curl;
267                DataOut.Div2Mean=DataOut.Div2Mean+Field.div.*Field.div;
268            end
[1082]269        end
[751]270    end
[1100]271    %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
[1077]272   
[1100]273    DataOut.Counter(DataOut.Counter==0)=1;% put counter to 1 when it is zero
274    DataOut.UMean=DataOut.UMean./DataOut.Counter; % normalize the mean
275    DataOut.VMean=DataOut.VMean./DataOut.Counter; % normalize the mean
276    U2Mean=U2Mean./DataOut.Counter; % normalize the mean
277    V2Mean=V2Mean./DataOut.Counter; % normalize the mean
278    UVMean=UVMean./DataOut.Counter; % normalize the mean
279    U2Mean_1=U2Mean_1./Counter_1; % normalize the mean
280    V2Mean_1=V2Mean_1./Counter_1; % normalize the mean
281    DataOut.u2Mean=U2Mean-DataOut.UMean.*DataOut.UMean; % normalize the mean
282    DataOut.v2Mean=V2Mean-DataOut.VMean.*DataOut.VMean; % normalize the mean
283    DataOut.uvMean=UVMean-DataOut.UMean.*DataOut.VMean; % normalize the mean \
284    DataOut.u2Mean_1=U2Mean_1-DataOut.UMean.*DataOut.UMean; % normalize the mean
285    DataOut.v2Mean_1=V2Mean_1-DataOut.VMean.*DataOut.VMean; % normalize the mean
286    DataOut.CurlMean=DataOut.CurlMean./DataOut.Counter;
287    DataOut.DivMean=DataOut.DivMean./DataOut.Counter;
288    DataOut.Curl2Mean=DataOut.Curl2Mean./DataOut.Counter-DataOut.CurlMean.*DataOut.CurlMean;
289    DataOut.Div2Mean=DataOut.Div2Mean./DataOut.Counter-DataOut.DivMean.*DataOut.DivMean;   
290   
291    %% writing the result file as netcdf file
[1129]292    RootPathOut=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
293    OutputDir=[Param.OutputSubDir Param.OutputDirExt];
294    OutputFile=fullfile_uvmat(RootPathOut,OutputDir,RootFile{1},FileExtOut,NomTypeOut,ind_first,ind_first,first_j,last_j);
[1100]295    %case of netcdf input file , determine global attributes
296    errormsg=struct2nc(OutputFile,DataOut); %save result file
297    if isempty(errormsg)
298        disp([OutputFile ' written']);
299    else
300        disp(['error in writting result file: ' errormsg])
[1129]301    end   
302end
303
[751]304%% open the result file with uvmat (in RUN mode)
[1095]305if checkrun && isequal(Param.IndexRange.NbSlice,1)
[751]306    uvmat(OutputFile)% open the last result file with uvmat
307end
Note: See TracBrowser for help on using the repository browser.