1 | %'turb_correlation_x': calculate the x wise correlation function at each point |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % function ParamOut=turb_correlation_x(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-2017, 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 | |
---|
59 | function ParamOut=turb_correlation_x(Param) |
---|
60 | |
---|
61 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName |
---|
62 | if 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='off'; %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_x';%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 |
---|
78 | end |
---|
79 | |
---|
80 | %%%%%%%%%%%% STANDARD PART %%%%%%%%%%%% |
---|
81 | ParamOut=[];%default output |
---|
82 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
83 | checkrun=1; |
---|
84 | if ischar(Param) |
---|
85 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
86 | checkrun=0; |
---|
87 | end |
---|
88 | hseries=findobj(allchild(0),'Tag','series'); |
---|
89 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
90 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
91 | |
---|
92 | %% define the directory for result file (with path=RootPath{1}) |
---|
93 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
94 | |
---|
95 | %% root input file(s) name, type and index series |
---|
96 | RootPath=Param.InputTable(:,1); |
---|
97 | RootFile=Param.InputTable(:,3); |
---|
98 | SubDir=Param.InputTable(:,2); |
---|
99 | NomType=Param.InputTable(:,4); |
---|
100 | FileExt=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 |
---|
110 | NbView=numel(i1_series);%number of input file series (lines in InputTable) |
---|
111 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
112 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
113 | NbField=NbField_j*NbField_i; %total number of fields |
---|
114 | |
---|
115 | %% determine the file type on each line from the first input file |
---|
116 | ImageTypeOptions={'image','multimage','mmreader','video','cine_phantom'}; |
---|
117 | NcTypeOptions={'netcdf','civx','civdata'}; |
---|
118 | for 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 |
---|
132 | end |
---|
133 | |
---|
134 | %% calibration data and timing: read the ImaDoc files |
---|
135 | XmlData=[]; |
---|
136 | [XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series); |
---|
137 | if 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 |
---|
142 | end |
---|
143 | |
---|
144 | %% coordinate transform or other user defined transform |
---|
145 | transform_fct='';%default |
---|
146 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName) |
---|
147 | addpath(Param.FieldTransform.TransformPath) |
---|
148 | transform_fct=str2func(Param.FieldTransform.TransformName); |
---|
149 | rmpath(Param.FieldTransform.TransformPath) |
---|
150 | end |
---|
151 | |
---|
152 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
153 | % EDIT FROM HERE |
---|
154 | |
---|
155 | %% check the validity of input file types |
---|
156 | if CheckImage{1} |
---|
157 | FileExtOut='.png'; % write result as .png images for image inputs |
---|
158 | elseif CheckNc{1} |
---|
159 | FileExtOut='.nc';% write result as .nc files for netcdf inputs |
---|
160 | else |
---|
161 | msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}]) |
---|
162 | return |
---|
163 | end |
---|
164 | |
---|
165 | |
---|
166 | %% settings for the output file |
---|
167 | NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file |
---|
168 | first_i=i1_series{1}(1); |
---|
169 | last_i=i1_series{1}(end); |
---|
170 | if isempty(j1_series{1})% if there is no second index j |
---|
171 | first_j=1;last_j=1; |
---|
172 | else |
---|
173 | first_j=j1_series{1}(1); |
---|
174 | last_j=j1_series{1}(end); |
---|
175 | end |
---|
176 | |
---|
177 | %% Set field names and velocity types |
---|
178 | InputFields{1}=[];%default (case of images) |
---|
179 | if isfield(Param,'InputFields') |
---|
180 | InputFields{1}=Param.InputFields; |
---|
181 | end |
---|
182 | |
---|
183 | nbfiles=0; |
---|
184 | nbmissing=0; |
---|
185 | |
---|
186 | %initialisation |
---|
187 | DataOut.ListGlobalAttribute= {'Conventions'}; |
---|
188 | DataOut.Conventions= 'uvmat'; |
---|
189 | DataOut.ListVarName={'delta_x','coord_y','coord_x','UUCorr' , 'VVCorr','UVCorr','Counter'}; |
---|
190 | DataOut.VarDimName={'delta_x','coord_y','coord_x',... |
---|
191 | {'delta_x','coord_y','coord_x'},{'delta_x','coord_y','coord_x'},{'delta_x','coord_y','coord_x'},{'delta_x','coord_y','coord_x'}}; |
---|
192 | DataOut.UUCorr=0; |
---|
193 | DataOut.Counter=0; |
---|
194 | |
---|
195 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
196 | % First get mean values % |
---|
197 | disp('loop for mean started') |
---|
198 | for index=1:NbField |
---|
199 | update_waitbar(WaitbarHandle,index/NbField) |
---|
200 | if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
201 | disp('program stopped by user') |
---|
202 | break |
---|
203 | end |
---|
204 | [Field,tild,errormsg] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index)); |
---|
205 | if index==1 %first field |
---|
206 | if ~isfield(Field,'U')||~isfield(Field,'V') |
---|
207 | disp_uvmat('ERROR','this function requires the velocity components U and V as input',checkrun) |
---|
208 | return |
---|
209 | end |
---|
210 | [npy,npx]=size(Field.U); |
---|
211 | UMean=zeros(npy,npx); |
---|
212 | VMean=zeros(npy,npx); |
---|
213 | Counter=false(npy,npx); |
---|
214 | % transcripts the global attributes |
---|
215 | if isfield(Field,'ListGlobalAttribute') |
---|
216 | DataOut.ListGlobalAttribute= Field.ListGlobalAttribute; |
---|
217 | for ilist=1:numel(Field.ListGlobalAttribute) |
---|
218 | AttrName=Field.ListGlobalAttribute{ilist}; |
---|
219 | DataOut.(AttrName)=Field.(AttrName); |
---|
220 | end |
---|
221 | end |
---|
222 | end |
---|
223 | FF=isnan(Field.U);%|Field.U<-60|Field.U>30;% threshold on U |
---|
224 | Field.U(FF)=0;% set to 0 the nan values, |
---|
225 | Field.V(FF)=0; |
---|
226 | UMean=UMean+Field.U; |
---|
227 | VMean=VMean+Field.V; |
---|
228 | Counter=Counter+~FF; |
---|
229 | end |
---|
230 | UMean=UMean./Counter; |
---|
231 | VMean=VMean./Counter; |
---|
232 | |
---|
233 | |
---|
234 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
235 | disp('loop for correlation started') |
---|
236 | for index=1:NbField |
---|
237 | update_waitbar(WaitbarHandle,index/NbField) |
---|
238 | if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
239 | disp('program stopped by user') |
---|
240 | break |
---|
241 | end |
---|
242 | [Field,tild,errormsg] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index)); |
---|
243 | |
---|
244 | %%%%%%%%%%%% MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
245 | if index==1 %first field |
---|
246 | [npy,npx]=size(Field.U); |
---|
247 | npcorr=floor(npx/4); %nbre of points for the correlation fct on each side of 0 |
---|
248 | dx=(Field.coord_x(end)-Field.coord_x(1))/(numel(Field.coord_x)-1); |
---|
249 | DataOut.delta_x=(-dx*npcorr:dx:dx*npcorr)'; |
---|
250 | DataOut.coord_x=Field.coord_x; |
---|
251 | DataOut.coord_y=Field.coord_y; |
---|
252 | DataOut.UUCorr=zeros(2*npcorr+1,npy,npx); |
---|
253 | DataOut.VVCorr=zeros(2*npcorr+1,npy,npx); |
---|
254 | DataOut.UVCorr=zeros(2*npcorr+1,npy,npx); |
---|
255 | DataOut.Counter=zeros(2*npcorr+1,npy,npx); |
---|
256 | end |
---|
257 | FF=isnan(Field.U);%|Field.U<-60|Field.U>30;% threshold on U |
---|
258 | Field.U(FF)=0;% set to 0 the nan values,'delta_x' |
---|
259 | Field.V(FF)=0; |
---|
260 | Field.U=Field.U-UMean; |
---|
261 | Field.V=Field.V-VMean; |
---|
262 | UUCorr=zeros(2*npcorr+1,npy,npx); |
---|
263 | VVCorr=zeros(2*npcorr+1,npy,npx); |
---|
264 | UVCorr=zeros(2*npcorr+1,npy,npx); |
---|
265 | FFCorr=false(2*npcorr+1,npy,npx); |
---|
266 | for ishift=-npcorr:npcorr% calculate the field U shifted |
---|
267 | U_shift=circshift(Field.U,[0 -ishift]); %shift U by ishift along the index x |
---|
268 | V_shift=circshift(Field.V,[0 -ishift]); %shift U by ishift along the index x |
---|
269 | FF_shift=circshift(FF,[0 -ishift]); %shift U by ishift along the index x |
---|
270 | if ishift<0 |
---|
271 | U_shift(:,1:-ishift)=0; |
---|
272 | V_shift(:,1:-ishift)=0; |
---|
273 | FF_shift(:,1:-ishift)=1; |
---|
274 | elseif ishift>0 |
---|
275 | U_shift(:,end-ishift:end)=0; |
---|
276 | V_shift(:,end-ishift:end)=0; |
---|
277 | FF_shift(:,end-ishift:end)=1; |
---|
278 | end |
---|
279 | UUCorr(ishift+npcorr+1,:,:)=Field.U.*U_shift; |
---|
280 | VVCorr(ishift+npcorr+1,:,:)=Field.V.*V_shift; |
---|
281 | UVCorr(ishift+npcorr+1,:,:)=Field.U.*V_shift; |
---|
282 | FFCorr(ishift+npcorr+1,:,:)=FF | FF_shift; |
---|
283 | end |
---|
284 | DataOut.UUCorr=DataOut.UUCorr+UUCorr; |
---|
285 | DataOut.VVCorr=DataOut.VVCorr+VVCorr; |
---|
286 | DataOut.UVCorr=DataOut.UVCorr+UVCorr; |
---|
287 | DataOut.Counter=DataOut.Counter+~FFCorr; |
---|
288 | end |
---|
289 | %%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%% |
---|
290 | DataOut.UUCorr=DataOut.UUCorr./DataOut.Counter; |
---|
291 | DataOut.VVCorr=DataOut.VVCorr./DataOut.Counter; |
---|
292 | DataOut.UVCorr=DataOut.UVCorr./DataOut.Counter; |
---|
293 | %DataOut.Counter(DataOut.Counter==0)=1;% put counter to 1 when it is zero |
---|
294 | % DataOut.UMean=DataOut.UMean./DataOut.Counter; % normalize the mean |
---|
295 | % DataOut.VMean=DataOut.VMean./DataOut.Counter; % normalize the mean |
---|
296 | % U2Mean=U2Mean./DataOut.Counter; % normalize the mean |
---|
297 | % V2Mean=V2Mean./DataOut.Counter; % normalize the mean |
---|
298 | % UVMean=UVMean./DataOut.Counter; % normalize the mean |
---|
299 | % U2Mean_1=U2Mean_1./Counter_1; % normalize the mean |
---|
300 | % V2Mean_1=V2Mean_1./Counter_1; % normalize the mean |
---|
301 | % DataOut.u2Mean=U2Mean-DataOut.UMean.*DataOut.UMean; % normalize the meanFFCorr |
---|
302 | % DataOut.v2Mean=V2Mean-DataOut.VMean.*DataOut.VMean; % normalize the mean |
---|
303 | % DataOut.uvMean=UVMean-DataOut.UMean.*DataOut.VMean; % normalize the mean \ |
---|
304 | % DataOut.u2Mean_1=U2Mean_1-DataOut.UMean.*DataOut.UMean; % normalize the mean |
---|
305 | % DataOut.v2Mean_1=V2Mean_1-DataOut.VMean.*DataOut.VMean; % normalize the mean |
---|
306 | |
---|
307 | |
---|
308 | %% calculate the profiles |
---|
309 | % npx=numel(DataOut.coord_x); |
---|
310 | % band=ceil(npx/5) :floor(4*npx/5);% keep only the central band |
---|
311 | % for ivar=3:numel(DataOut.ListVarName)-1 |
---|
312 | % VarName=DataOut.ListVarName{ivar};% name of the variable |
---|
313 | % DataOut.ListVarName=[DataOut.ListVarName {[VarName 'Profile']}];%append the name of the profile variable |
---|
314 | % DataOut.VarDimName=[DataOut.VarDimName {'coord_y'}]; |
---|
315 | % DataOut.([VarName 'Profile'])=mean(DataOut.(VarName)(:,band),2); %take the mean profile of U, excluding the edges |
---|
316 | % end |
---|
317 | |
---|
318 | %% writing the result file as netcdf file |
---|
319 | OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,first_i,last_i,first_j,last_j); |
---|
320 | %case of netcdf input file , determine global attributes |
---|
321 | errormsg=struct2nc(OutputFile,DataOut); %save result file |
---|
322 | if isempty(errormsg) |
---|
323 | disp([OutputFile ' written']); |
---|
324 | else |
---|
325 | disp(['error in writting result file: ' errormsg]) |
---|
326 | end |
---|
327 | |
---|
328 | |
---|
329 | %% open the result file with uvmat (in RUN mode) |
---|
330 | if checkrun |
---|
331 | uvmat(OutputFile)% open the last result file with uvmat |
---|
332 | end |
---|