source: trunk/src/series/sliding_average.m @ 1157

Last change on this file since 1157 was 1157, checked in by sommeria, 3 months ago

filter_time added

File size: 12.1 KB
Line 
1%'sliding_average': 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, (several5.804 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%             900
28%    .IndexRange: set the file or frame indices on which the action must be performseriesed
29%    .FieldTransform: .TransformName: name of the select39ed 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%             uvmat .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% Copyright 2008-2022, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
42%   http://www.legi.grenoble-inp.fr
43%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
44%
45%     This file is part of the toolbox UVMAT.
46%
47%     UVMAT is free software; you can redistribute it and/or modify
48%     it under the terms of the GNU General Public License as published
49%     by the Free Software Foundation; either version 2 of the license,
50%     or (at your option) any later version.
51%
52%     UVMAT is distributed in the hope that it will be useful,
53%     but WITHOUT ANY WARRANTY; without even the implied warranty of
54%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55%     GNU General Public License (see LICENSE.txt) for more details.
56%=======================================================================
57
58function ParamOut=sliding_average(Param)
59
60%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
61if isstruct(Param) && isequal(Param.Action.RUN,0)
62    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
63    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to mseriesax (options 'off'/'on', 'off' by default)
64    ParamOut.NbSlice=1; %nbre of slices ('off' by default)
65    ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
66    ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
67    ParamOut.FieldTransform = 'off';%can use a transform function
68    ParamOut.ProjObject='off';%can use projection object39(option 'off'/'on',
69    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
70    ParamOut.OutputDirExt='.tfilter';%set the output dir extension
71    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
72   
73    %% check the first input file in the series
74     first_j=[];%
75    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
76    PairString='';
77    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
78    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
79    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
80        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
81    if exist(FirstFileName,'file')
82        FileInfo=get_file_info(FirstFileName);
83        if ~(isfield(FileInfo,'FileType')&& strcmp(FileInfo.FileType,'netcdf'))
84            msgbox_uvmat('ERROR','this fct works only on netcdf files (fields projected on a grid, notraw civ data)')
85        return
86        end
87    else
88        msgbox_uvmat('ERROR',['the input file ' FirstFileName ' does not exist'])
89        return
90    end
91
92    %% setting of intput parameters
93%     answer = msgbox_uvmat('INPUT_MENU','select the variables to filter',ListVarName');
94%     ParamOut.ListVarName=ListVarName(answer);
95    answer = msgbox_uvmat('INPUT_TXT','set the filering window length','3');
96    ParamOut.WindowLength=str2double(answer);
97   
98%         [ParamOut.ActionInput,errormsg] = set_param_input(ListParam,DefaultValue,ParamIn);
99%         if ~isempty(errormsg)
100%             msgbox_uvmat('ERROR',errormsg)
101%         end
102%     [ParamOut,errormsg] = set_param_input(ListParam,DefaultValue,ParamIn);
103    return
104end
105
106%%%%%%%%%%%%  STANDARD PART  %%%%%%%%%%%%
107ParamOut=[];%default output
108%% read input parameters from an xml file if input is a file name (batch mode)
109checkrun=1;
110if ischar(Param)
111    Param=xml2struct(Param);% read Param as input file (batch case)
112    checkrun=0;
113end
114
115hseries=findobj(allchild(0),'Tag','series');
116RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
117WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
118
119%% define the directory for result file (with path=RootPath{1})
120OutputDir=[Param.OutputSubDir Param.OutputDirExt];
121   
122%% root input file(s) name, type and index series
123RootPath=Param.InputTable(:,1);
124RootFile=Param.InputTable(:,3);
125SubDir=Param.InputTable(:,2);
126NomType=Param.InputTable(:,4);
127FileExt=Param.InputTable(:,5);
128[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
129%%%%%%%%%%%%
130% The cell array filecell is the list of input file names, while
131% filecell{iview,fileindex}:
132%        iview: line in the table corresponding to a given file series
133%        fileindex: file index within  the file series,
134% 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
135% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
136%%%%%%%%%%%% NbView=1 : a single input series
137NbView=numel(i1_series);%number of input file series (lines in InputTable)
138NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
139NbField_i=size(i1_series{1},2); %nb of fields for the i index
140NbField=NbField_j*NbField_i; %total number of fields
141
142%% determine the file type on each line from the first input file
143ImageTypeOptions={'image','multimage','mmreader','video','cine_phantom'};
144NcTypeOptions={'netcdf','civx','civdata'};
145for iview=1:NbView
146    if ~exist(filecell{iview,1}','file')
147        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
148        return
149    end
150    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
151    FileType{iview}=FileInfo{iview}.FileType;
152    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
153    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
154    if ~isempty(j1_series{iview})
155        frame_index{iview}=j1_series{iview};
156    else
157        frame_index{iview}=i1_series{iview};
158    end
159end
160
161%% calibration data and timing: read the ImaDoc files
162XmlData=[];
163[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
164if size(time,1)>1
165    diff_time=max(max(diff(time)));
166    if diff_time>0
167        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
168    end   
169end
170
171%% coordinate transform or other user defined transform
172transform_fct='';%default
173if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
174    addpath(Param.FieldTransform.TransformPath)
175    transform_fct=str2func(Param.FieldTransform.TransformName);
176    rmpath(Param.FieldTransform.TransformPath)
177end
178
179%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
180 % EDIT FROM HERE
181
182%% check the validity of  input file types
183if CheckImage{1}
184    FileExtOut='.png'; % write result as .png images for image inputs
185elseif CheckNc{1}
186    FileExtOut='.nc';% write result as .nc files for netcdf inputs
187else
188    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
189    return
190end
191
192
193%% settings for the output file
194NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
195first_i=i1_series{1}(1);
196last_i=i1_series{1}(end);
197if isempty(j1_series{1})% if there is no second index j
198    first_j=1;last_j=1;
199else
200    first_j=j1_series{1}(1);
201    last_j=j1_series{1}(end);
202end
203
204%% Set field names and velocity types
205InputFields{1}=[];%default (case of images)series
206if isfield(Param,'InputFields')
207    InputFields{1}=Param.InputFields;
208end
209
210nbfiles=0;
211nbmissing=0;
212
213%% initialisation output data
214
215DataOut.ListGlobalAttribute= {'Conventions'};
216DataOut.Conventions='uvmat';
217DataOut.ListVarName={'Time','coord_y','coord_x','Ufilter','Vfilter'};
218DataOut.VarDimName={'Time','coord_y','coord_x',{'Time','coord_y','coord_x'},{'Time','coord_y','coord_x'}};
219
220%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
221% First get time %
222[Data,tild,errormsg]=nc2struct(filecell{1,1});   
223Time_1=Data.Time;
224if ~isempty(errormsg)
225    disp_uvmat('ERROR',errormsg,checkrun)
226    return
227end
228[Data,tild,errormsg]=nc2struct(filecell{1,2});   
229Time_2=Data.Time;
230dt=(Time_2-Time_1); %time interval
231NpTime=21; %Nbre de champ pour la moyenne glissante (choisir impair)
232
233OutputPath=fullfile(Param.OutputPath,num2str(Param.Experiment),num2str(Param.Device));
234RootFileOut=RootFile{1};
235NomTypeOut='_1';
236%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
237disp('loop for filtering started')
238for index=1:NbField
239    update_waitbar(WaitbarHandle,index/NbField)
240    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
241        disp('program stopped by user')
242        break
243    end
244    [Field,tild,~] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
245   
246    %%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
247    if index==1 %first field
248        DataOut.coord_x=Field.coord_x;
249        DataOut.coord_y=Field.coord_y;
250        npy=numel(DataOut.coord_y);
251        npx=numel(DataOut.coord_x);
252        Ufilter=zeros(NpTime,npy,npx);
253        Vfilter=zeros(NpTime,npy,npx);
254    end
255    Time(index)=Field.Time;%time
256    Ufilter=circshift(Ufilter,[-1 0 0]); %shift U by ishift along the first index
257    Vfilter=circshift(Vfilter,[-1 0 0]); %shift U by ishift along the first index   
258    Ufilter(end,:,:)=Field.U;
259    Vfilter(end,:,:)=Field.V;
260    DataOut.Time=(Time(index)-(NpTime-1)*dt/2);%mid time
261    DataOut.Ufilter=squeeze(nanmean(Ufilter,1));
262    DataOut.Vfilter=squeeze(nanmean(Vfilter,1));
263   
264
265    % writing the result file as netcdf file
266    i1=i1_series{1}(index)-ceil(NpTime/2);
267    OutputFile=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,'.nc',NomTypeOut,i1);
268    errormsg=struct2nc(OutputFile, DataOut);
269    if isempty(errormsg)
270        disp([OutputFile ' written'])
271    else
272        disp(errormsg)
273    end
274end
275   
Note: See TracBrowser for help on using the repository browser.