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

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

system of mirror output path introduced, and series/sliding_average added

File size: 12.6 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-2020, 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=sliding_average(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='.tfilter';%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
115
116%% determine the file type on each line from the first input file
117ImageTypeOptions={'image','multimage','mmreader','video','cine_phantom'};
118NcTypeOptions={'netcdf','civx','civdata'};
119for iview=1:NbView
120    if ~exist(filecell{iview,1}','file')
121        msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
122        return
123    end
124    [FileInfo{iview},MovieObject{iview}]=get_file_info(filecell{iview,1});
125    FileType{iview}=FileInfo{iview}.FileType;
126    CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
127    CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
128    if ~isempty(j1_series{iview})
129        frame_index{iview}=j1_series{iview};
130    else
131        frame_index{iview}=i1_series{iview};
132    end
133end
134
135%% calibration data and timing: read the ImaDoc files
136XmlData=[];
137[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
138if size(time,1)>1
139    diff_time=max(max(diff(time)));
140    if diff_time>0
141        msgbox_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time)])
142    end   
143end
144
145%% coordinate transform or other user defined transform
146transform_fct='';%default
147if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
148    addpath(Param.FieldTransform.TransformPath)
149    transform_fct=str2func(Param.FieldTransform.TransformName);
150    rmpath(Param.FieldTransform.TransformPath)
151end
152
153%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
154 % EDIT FROM HERE
155
156%% check the validity of  input file types
157if CheckImage{1}
158    FileExtOut='.png'; % write result as .png images for image inputs
159elseif CheckNc{1}
160    FileExtOut='.nc';% write result as .nc files for netcdf inputs
161else
162    msgbox_uvmat('ERROR',['invalid file type input ' FileType{1}])
163    return
164end
165
166
167%% settings for the output file
168NomTypeOut=nomtype2pair(NomType{1});% determine the index nomenclature type for the output file
169first_i=i1_series{1}(1);
170last_i=i1_series{1}(end);
171if isempty(j1_series{1})% if there is no second index j
172    first_j=1;last_j=1;
173else
174    first_j=j1_series{1}(1);
175    last_j=j1_series{1}(end);
176end
177
178%% Set field names and velocity types
179InputFields{1}=[];%default (case of images)
180if isfield(Param,'InputFields')
181    InputFields{1}=Param.InputFields;
182end
183
184nbfiles=0;
185nbmissing=0;
186
187%% initialisation
188T=24.2; %main wave period
189NbPeriod=2; %number of periods for the sliding average
190omega=2*pi/T;
191
192DataOut.ListGlobalAttribute= {'Conventions','Time'};
193DataOut.Conventions='uvmat';
194DataOut.ListVarName={'coord_y','coord_x','Umean','Vmean','Ucos','Vcos','DUDXsin','DUDYsin','DVDXsin','DVDYsin','Ustokes','Vstokes'};
195DataOut.VarDimName={'coord_y','coord_x',{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},...
196    {'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'}};
197
198%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
199% First get time %
200[Data,tild,errormsg]=nc2struct(filecell{1,1});   
201Time_1=Data.Time;
202if ~isempty(errormsg)
203    disp_uvmat('ERROR',errormsg,checkrun)
204    return
205end
206[Data,tild,errormsg]=nc2struct(filecell{1,end});   
207Time_end=Data.Time;
208dt=(Time_end-Time_1)/(NbField-1); %time interval
209NpTime=round(NbPeriod*T/dt+1)
210
211%%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%%
212disp('loop for filtering started')
213for index=1:NbField
214    update_waitbar(WaitbarHandle,index/NbField)
215    if ~isempty(RUNHandle)&& ~strcmp(get(RUNHandle,'BusyAction'),'queue')
216        disp('program stopped by user')
217        break
218    end
219    [Field,tild,errormsg] = read_field(filecell{1,index},FileType{iview},InputFields{iview},frame_index{iview}(index));
220   
221    %%%%%%%%%%%% MAIN RUNNING OPERATIONS  %%%%%%%%%%%%
222    if index==1 %first field
223        DataOut.coord_x=Field.coord_x;
224        DataOut.coord_y=Field.coord_y;
225        npy=numel(DataOut.coord_y);
226        npx=numel(DataOut.coord_x);
227        Umean=zeros(NpTime,npy,npx);
228        Vmean=zeros(NpTime,npy,npx);
229        Ucos=zeros(NpTime,npy,npx);
230        Vcos=zeros(NpTime,npy,npx);
231        DUDXsin=zeros(NpTime,npy,npx);
232        DUDYsin=zeros(NpTime,npy,npx);
233        DVDXsin=zeros(NpTime,npy,npx);
234        DVDYsin=zeros(NpTime,npy,npx);
235    end
236      Time(index)=Field.Time;
237    Umean=circshift(Umean,[-1 0 0]); %shift U by ishift along the first index
238        Vmean=circshift(Vmean,[-1 0 0]); %shift U by ishift along the first index
239        Ucos=circshift(Ucos,[-1 0 0]); %shift U by ishift along the first index
240        Vcos=circshift(Vcos,[-1 0 0]); %shift U by ishift along the first index
241        DUDXsin=circshift(DUDXsin,[-1 0 0]);
242        DUDYsin=circshift(DUDYsin,[-1 0 0]);
243        DVDXsin=circshift(DVDXsin,[-1 0 0]);
244        DVDYsin=circshift(DVDYsin,[-1 0 0]);
245        Umean(end,:,:)=Field.U;
246        Vmean(end,:,:)=Field.V;
247        Ucos(end,:,:)=Field.U*cos(omega*Time(index));
248        Vcos(end,:,:)=Field.V*cos(omega*Time(index));
249        DUDXsin(end,:,:)=Field.DUDX*sin(omega*Time(index));
250        DUDYsin(end,:,:)=Field.DUDY*sin(omega*Time(index));
251        DVDXsin(end,:,:)=Field.DVDX*sin(omega*Time(index));
252        DVDYsin(end,:,:)=Field.DVDY*sin(omega*Time(index));
253        DataOut.Time=Time(index)-(NpTime-1)*dt/2;
254        DataOut.Umean=squeeze(nanmean(Umean,1));
255        DataOut.Vmean=squeeze(nanmean(Vmean,1));
256        DataOut.Ucos=2*squeeze(nanmean(Ucos,1));
257        DataOut.Vcos=2*squeeze(nanmean(Vcos,1));
258        DataOut.DUDXsin=2*squeeze(nanmean(DUDXsin,1));
259        DataOut.DUDYsin=2*squeeze(nanmean(DUDYsin,1));
260        DataOut.DVDXsin=2*squeeze(nanmean(DVDXsin,1));
261        DataOut.DVDYsin=2*squeeze(nanmean(DVDYsin,1));
262        DataOut.Ustokes=(1/omega)*(DataOut.Ucos.*DataOut.DUDXsin+DataOut.Vcos.*DataOut.DUDYsin);
263        DataOut.Vstokes=(1/omega)*(DataOut.Ucos.*DataOut.DVDXsin+DataOut.Vcos.*DataOut.DVDYsin);
264    % writing the result file as netcdf file
265    if index-round(NpTime/2)>=1
266        OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomType{1},index-round(NpTime/2));
267        %case of netcdf input file , determine global attributes
268        errormsg=struct2nc(OutputFile,DataOut); %save result file
269        if isempty(errormsg)
270            disp([OutputFile ' written']);
271        else
272            disp(['error in writting result file: ' errormsg])
273        end
274    end
275end
276%%%%%%%%%%%%%%%% end loop on field indices %%%%%%%%%%%%%%%%
277
Note: See TracBrowser for help on using the repository browser.