1 | %'filter_time': apply a a sliding filter in a time series |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % function ParamOut=filter_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 | |
---|
58 | function ParamOut=filter_time(Param) |
---|
59 | |
---|
60 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName |
---|
61 | if 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 the fltering window length |
---|
93 | answer = msgbox_uvmat('INPUT_TXT','set the filering window length','3'); |
---|
94 | ParamOut.ActionInput.WindowLength=str2double(answer); |
---|
95 | return |
---|
96 | end |
---|
97 | |
---|
98 | %%%%%%%%%%%% STANDARD PART %%%%%%%%%%%% |
---|
99 | ParamOut=[];%default output |
---|
100 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
101 | checkrun=1; |
---|
102 | if ischar(Param) |
---|
103 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
104 | checkrun=0; |
---|
105 | end |
---|
106 | |
---|
107 | hseries=findobj(allchild(0),'Tag','series'); |
---|
108 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
109 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
110 | |
---|
111 | |
---|
112 | %% root input file(s) name, type and index series |
---|
113 | RootPath=Param.InputTable(:,1); |
---|
114 | RootFile=Param.InputTable(:,3); |
---|
115 | SubDir=Param.InputTable(:,2); |
---|
116 | NomType=Param.InputTable(:,4); |
---|
117 | FileExt=Param.InputTable(:,5); |
---|
118 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
119 | %%%%%%%%%%%% |
---|
120 | % The cell array filecell is the list of input file names, while |
---|
121 | % filecell{iview,fileindex}: |
---|
122 | % iview: line in the table corresponding to a given file series |
---|
123 | % fileindex: file index within the file series, |
---|
124 | % 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 |
---|
125 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
126 | %%%%%%%%%%%% NbView=1 : a single input series |
---|
127 | |
---|
128 | NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
129 | NbField_i=size(i1_series{1},2); %nb of fields for the i index |
---|
130 | NbField=NbField_j*NbField_i; %total number of fields |
---|
131 | |
---|
132 | %% define the output file (unique for the whole series) |
---|
133 | OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device); |
---|
134 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
135 | first_j=[];last_j=[];% %% check the first input file in the series |
---|
136 | |
---|
137 | PairString=''; |
---|
138 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
139 | |
---|
140 | |
---|
141 | % OutputPath=fullfile(Param.OutputPath,num2str(Param.Experiment),num2str(Param.Device)); |
---|
142 | % RootFileOut=RootFile{1}; |
---|
143 | % NomTypeOut='_1'; |
---|
144 | |
---|
145 | %%%%%%%%%%%%%%%% loop on field indices %%%%%%%%%%%%%%%% |
---|
146 | disp('loop for filtering started') |
---|
147 | |
---|
148 | for index=1:NbField |
---|
149 | index |
---|
150 | |
---|
151 | Field= read_field(filecell{1,index},'netcdf',Param.InputFields); |
---|
152 | |
---|
153 | %%%%%%%%%%% MAIN RUNNING OPERATIONS %%%%%%%%%%%% |
---|
154 | if index==1 %first field, initialisation output data |
---|
155 | |
---|
156 | DataOut.ListGlobalAttribute= {'Conventions','Time'}; |
---|
157 | DataOut.Conventions='uvmat'; |
---|
158 | %DataOut.ListVarName={'coord_y','coord_x','Ufilter','Vfilter','Wfilter'}; |
---|
159 | ListFields=Param.InputFields.FieldName; |
---|
160 | YName=Param.InputFields.Coord_y; |
---|
161 | XName=Param.InputFields.Coord_x; |
---|
162 | NbField=numel(ListFields); |
---|
163 | for ilist=1:NbField |
---|
164 | ListFilter{ilist}=[ListFields{ilist} '_filter']; |
---|
165 | VarDimName{ilist}={YName,XName}; |
---|
166 | end |
---|
167 | DataOut.ListVarName=[{YName,XName} ListFilter]; |
---|
168 | DataOut.VarDimName=[{YName,XName} VarDimName]; |
---|
169 | npy=numel(Field.(YName)); |
---|
170 | npx=numel(Field.(XName)); |
---|
171 | DataOut.(XName)=Field.(XName); |
---|
172 | DataOut.(YName)=Field.(YName); |
---|
173 | TimeBlock=zeros(Param.ActionInput.WindowLength,1); |
---|
174 | |
---|
175 | UVblock=zeros(Param.ActionInput.WindowLength,NbField,npy,npx); |
---|
176 | % Vblock=zeros(Param.ActionInput.WindowLength,npy,npx); |
---|
177 | % Wblock=zeros(Param.ActionInput.WindowLength,npy,npx); |
---|
178 | end |
---|
179 | TimeBlock=circshift(TimeBlock,[-1 0 ]); |
---|
180 | UVblock=circshift(UVblock,[-1 0 0 0]); %shift U by ishift along the first index |
---|
181 | % Vblock=circshift(Vblock,[-1 0 0 0]); %shift U by ishift along the first index |
---|
182 | % Wblock=circshift(Wblock,[-1 0 0]); %shift U by ishift along the first index |
---|
183 | TimeBlock(end)=Field.Time; |
---|
184 | for ilist=1:NbField |
---|
185 | UVblock(end,ilist,:,:)=Field.(ListFields{ilist}); |
---|
186 | end |
---|
187 | % Vblock(end,:,:)=Field.V; |
---|
188 | % Vblock(end,:,:)=Field.W; |
---|
189 | sumindex=min(index,Param.ActionInput.WindowLength)-1; |
---|
190 | DataOut.Timefilter=mean(TimeBlock(end-sumindex:end,:));%mid time |
---|
191 | for ilist=1:NbField |
---|
192 | DataOut.(ListFilter{ilist})=squeeze(mean(UVblock(end-sumindex:end,ilist,:,:),1,'omitnan')); |
---|
193 | end |
---|
194 | % DataOut.Vfilter=squeeze(mean(Vblock(end-sumindex:end,:,:),1,'omitnan')); |
---|
195 | % DataOut.Wfilter=squeeze(mean(Wblock(end-sumindex:end,:,:),1,'omitnan')); |
---|
196 | %updating output the netcdf file |
---|
197 | [i1,i2,j1,j2] = get_file_index(i1_series{1}(index),[],PairString); |
---|
198 | OutputFile=fullfile_uvmat(OutputPath,OutputDir,Param.InputTable{1,3},'.nc',NomType{1},i1,i2,j1,j2); |
---|
199 | errormsg=struct2nc(OutputFile, DataOut); |
---|
200 | if isempty(errormsg) |
---|
201 | disp([OutputFile ' written']) |
---|
202 | else |
---|
203 | disp(errormsg) |
---|
204 | end |
---|
205 | end |
---|
206 | % writing the result file as netcdf file |
---|
207 | % i1=i1_series{1}(index)-ceil(NpTime/2); |
---|
208 | % OutputFile=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,'.nc',NomTypeOut,i1); |
---|
209 | |
---|
210 | 'END' |
---|