source: trunk/src/series/turb_correlation_time.m @ 1062

Last change on this file since 1062 was 1061, checked in by g7moreau, 6 years ago
  • Update copyright to year 2019
File size: 15.4 KB
Line 
1%'turb_correlation_time': calculate the time correlation function at each point
2%------------------------------------------------------------------------
3% function ParamOut=turb_correlation_time(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-2019, 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_correlation_time(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='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
67    ParamOut.FieldName='one';% 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='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='.corr_t';%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
88
89hseries=findobj(allchild(0),'Tag','series');
90RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
91WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
92
93%% define the directory for result file (with path=RootPath{1})
94OutputDir=[Param.OutputSubDir Param.OutputDirExt];
95   
96%% root input file(s) name, type and index series
97RootPath=Param.InputTable(:,1);
98RootFile=Param.InputTable(:,3);
99SubDir=Param.InputTable(:,2);
100NomType=Param.InputTable(:,4);
101FileExt=Param.InputTable(:,5);
102[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
103%%%%%%%%%%%%
104% The cell array filecell is the list of input file names, while
105% filecell{iview,fileindex}:
106%        iview: line in the table corresponding to a given file series
107%        fileindex: file index within  the file series,
108% 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
109% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
110%%%%%%%%%%%% NbView=1 : a single input series
111NbView=numel(i1_series);%number of input file series (lines in InputTable)
112NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
113NbField_i=size(i1_series{1},2); %nb of fields for the i index
114NbField=NbField_j*NbField_i; %total number of fields
115NpTime=256;% max number of times for each side of the time correlation fct
116NpTime=min(NpTime,floor(NbField/4)); %nbre of points for the correlation fct on each side of 0
117
118%% determine the file type on each line from the first input file
119ImageTypeOptions={'image','multimage','mmreader','video','cine_phantom'};
120NcTypeOptions={'netcdf','civx','civdata'};
121for iview=1:NbView
122    if ~exist(filecell{iview,1}','file')
123        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
124        return
125    end
126    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
127    FileType{iview}=FileInfo{iview}.FileType;
128    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
129    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
130    if ~isempty(j1_series{iview})
131        frame_index{iview}=j1_series{iview};
132    else
133        frame_index{iview}=i1_series{iview};
134    end
135end
136
137%% calibration data and timing: read the ImaDoc files
138XmlData=[];
139[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
140if size(time,1)>1
141    diff_time=max(max(diff(time)));
142    if diff_time>0
143        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
144    end   
145end
146
147%% coordinate transform or other user defined transform
148transform_fct='';%default
149if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
150    addpath(Param.FieldTransform.TransformPath)
151    transform_fct=str2func(Param.FieldTransform.TransformName);
152    rmpath(Param.FieldTransform.TransformPath)
153end
154
155%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
156 % EDIT FROM HERE
157
158%% check the validity of  input file types
159if CheckImage{1}
160    FileExtOut='.png'; % write result as .png images for image inputs
161elseif CheckNc{1}
162    FileExtOut='.nc';% write result as .nc files for netcdf inputs
163else
164    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
165    return
166end
167
168
169%% settings for the output file
170NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
171first_i=i1_series{1}(1);
172last_i=i1_series{1}(end);
173if isempty(j1_series{1})% if there is no second index j
174    first_j=1;last_j=1;
175else
176    first_j=j1_series{1}(1);
177    last_j=j1_series{1}(end);
178end
179
180%% Set field names and velocity types
181InputFields{1}=[];%default (case of images)
182if isfield(Param,'InputFields')
183    InputFields{1}=Param.InputFields;
184end
185
186nbfiles=0;
187nbmissing=0;
188
189%initialisation
190DataOut.ListGlobalAttribute= {'Conventions'};
191DataOut.Conventions='uvmat';
192DataOut.ListVarName={'delta_t','coord_y','coord_x','UUCorr' , 'VVCorr','UVCorr','Counter'};
193DataOut.VarDimName={'delta_t','coord_y','coord_x',...
194    {'delta_t','coord_y','coord_x'},{'delta_t','coord_y','coord_x'},{'delta_t','coord_y','coord_x'},{'delta_t','coord_y','coord_x'}};
195
196%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
197% First get mean values %
198disp('loop for mean started')
199Time=zeros(1,NbField);
200for index=1:NbField
201    update_waitbar(WaitbarHandle,index/NbField)
202    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
203        disp('program stopped by user')
204        break
205    end
206    [Field,tild,errormsg] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
207    if index==1 %first field
208        if ~isempty(errormsg)
209            disp_uvmat('ERROR',errormsg,checkrun)
210            return
211        elseif ~isfield(Field,'U')||~isfield(Field,'V')
212            disp_uvmat('ERROR','this function requires the velocity components U and V as input',checkrun)
213            return
214        elseif ~isfield(Field,'Time')
215            disp_uvmat('ERROR','the attribute Time needs to be defined as input',checkrun)
216            return
217        end
218        [npy,npx]=size(Field.U);
219        UMean=zeros(npy,npx);
220        VMean=zeros(npy,npx);
221        Counter=false(npy,npx);
222        % transcripts the global attributes
223        if isfield(Field,'ListGlobalAttribute')
224            DataOut.ListGlobalAttribute= Field.ListGlobalAttribute;
225            for ilist=1:numel(Field.ListGlobalAttribute)
226                AttrName=Field.ListGlobalAttribute{ilist};
227                DataOut.(AttrName)=Field.(AttrName);
228            end
229        end
230    end
231    Time(index)=Field.Time;
232    FF=isnan(Field.U);%|Field.U<-60|Field.U>30;% threshold on U
233    Field.U(FF)=0;% set to 0 the nan values,
234    Field.V(FF)=0;
235    UMean=UMean+Field.U;
236    VMean=VMean+Field.V;
237    Counter=Counter+~FF;
238end
239UMean=UMean./Counter;
240VMean=VMean./Counter;
241
242
243%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
244disp('loop for correlation started')
245for index=1:NbField
246    update_waitbar(WaitbarHandle,index/NbField)
247    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
248        disp('program stopped by user')
249        break
250    end
251    [Field,tild,errormsg] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
252   
253    %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
254    if index==1 %first field
255        [npy,npx]=size(Field.U);     
256        dt=diff(Time);
257        Maxdt=max(dt); Mindt=min(dt);
258        if Maxdt-Mindt>0.001*Maxdt
259            disp_uvmat('ERROR','the time increment between fields is not constant',checkrun)
260            return
261        else
262            dt=(Maxdt+Mindt)/2;
263        end
264        DataOut.delta_t=(0:dt:dt*NpTime)';
265        DataOut.coord_x=Field.coord_x;
266        DataOut.coord_y=Field.coord_y;
267        DataOut.UUCorr=zeros(NpTime+1,npy,npx);
268        DataOut.VVCorr=zeros(NpTime+1,npy,npx);
269        DataOut.UVCorr=zeros(NpTime+1,npy,npx);
270        DataOut.Counter=zeros(NpTime+1,npy,npx);
271        U_shift=zeros(NpTime+1,npy,npx);
272        V_shift=zeros(NpTime+1,npy,npx);
273        FF_shift=zeros(NpTime+1,npy,npx);
274        UUCorr=zeros(NpTime+1,npy,npx);
275        VVCorr=zeros(NpTime+1,npy,npx);
276        UVCorr=zeros(NpTime+1,npy,npx);
277        FFCorr=false(NpTime+1,npy,npx);
278    end
279    Field.U=Field.U-UMean;
280    Field.V=Field.V-VMean;
281    FF=isnan(Field.U);%|Field.U<-60|Field.U>30;% threshold on U
282    Field.U(FF)=0;% set to 0 the nan values,'delta_x'
283    Field.V(FF)=0;
284    if index<=NpTime+1
285        U_shift(NpTime+2-index,:,:)=Field.U;
286        V_shift(NpTime+2-index,:,:)=Field.V;
287        FF_shift(NpTime+2-index,:,:)=FF;
288    else
289        U_shift=circshift(U_shift,[1 0 0]); %shift U by ishift along the first index
290        V_shift=circshift(V_shift,[1 0 0]); %shift U by ishift along the first index
291        FF_shift=circshift(FF_shift,[1 0 0]); %shift U by ishift along the first index
292        U_shift(1,:,:)=Field.U;
293        V_shift(1,:,:)=Field.V;
294        FF_shift(1,:,:)=FF;
295    end
296    for ishift=1:NpTime+1% calculate the field U shifted
297        UUCorr(ishift,:,:)=Field.U.*squeeze(U_shift(ishift,:,:));
298        VVCorr(ishift,:,:)=Field.V.*squeeze(V_shift(ishift,:,:));
299        UVCorr(ishift,:,:)=Field.U.*squeeze(V_shift(ishift,:,:));
300        FFCorr(ishift,:,:)=FF | squeeze(FF_shift(ishift,:,:));
301    end
302    DataOut.UUCorr=DataOut.UUCorr+UUCorr;
303    DataOut.VVCorr=DataOut.VVCorr+VVCorr;
304    DataOut.UVCorr=DataOut.UVCorr+UVCorr;
305    DataOut.Counter=DataOut.Counter+~FFCorr;
306end
307%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
308DataOut.Counter(DataOut.Counter==0)=1;% put counter to 1 when it is zero (to avoid NaN)
309DataOut.UUCorr=DataOut.UUCorr./DataOut.Counter;
310DataOut.VVCorr=DataOut.VVCorr./DataOut.Counter;
311DataOut.UVCorr=DataOut.UVCorr./DataOut.Counter;
312
313% DataOut.UMean=DataOut.UMean./DataOut.Counter; % normalize the mean
314% DataOut.VMean=DataOut.VMean./DataOut.Counter; % normalize the mean
315% U2Mean=U2Mean./DataOut.Counter; % normalize the mean
316% V2Mean=V2Mean./DataOut.Counter; % normalize the mean
317% UVMean=UVMean./DataOut.Counter; % normalize the mean
318% U2Mean_1=U2Mean_1./Counter_1; % normalize the mean
319% V2Mean_1=V2Mean_1./Counter_1; % normalize the mean
320% DataOut.u2Mean=U2Mean-DataOut.UMean.*DataOut.UMean; % normalize the meanFFCorr
321% DataOut.v2Mean=V2Mean-DataOut.VMean.*DataOut.VMean; % normalize the mean
322% DataOut.uvMean=UVMean-DataOut.UMean.*DataOut.VMean; % normalize the mean \
323% DataOut.u2Mean_1=U2Mean_1-DataOut.UMean.*DataOut.UMean; % normalize the mean
324% DataOut.v2Mean_1=V2Mean_1-DataOut.VMean.*DataOut.VMean; % normalize the mean
325
326
327%% calculate the profiles
328% npx=numel(DataOut.coord_x);
329% band=ceil(npx/5) :floor(4*npx/5);% keep only the central band
330% for ivar=3:numel(DataOut.ListVarName)-1
331%     VarName=DataOut.ListVarName{ivar};% name of the variable
332%     DataOut.ListVarName=[DataOut.ListVarName {[VarName 'Profile']}];%append the name of the profile variable
333%     DataOut.VarDimName=[DataOut.VarDimName {'coord_y'}];
334%    DataOut.([VarName 'Profile'])=mean(DataOut.(VarName)(:,band),2); %take the mean profile of U, excluding the edges
335% end
336
337%% writing the result file as netcdf file
338OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j);
339 %case of netcdf input file , determine global attributes
340 errormsg=struct2nc(OutputFile,DataOut); %save result file
341 if isempty(errormsg)
342     disp([OutputFile ' written']);
343 else
344     disp(['error in writting result file: ' errormsg])
345 end
346
347
348%% open the result file with uvmat (in RUN mode)
349if checkrun
350    uvmat(OutputFile)% open the last result file with uvmat
351end
Note: See TracBrowser for help on using the repository browser.