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

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

filter_time added

File size: 12.9 KB
Line 
1%'test_filter_tps': test the optimum value for the spline smmoothing function used in civ_series
2%------------------------------------------------------------------------
3% Method:
4    %open a netcdf file with civ results.
5   
6% function ParamOut=test_patch_tps (Param)
7%
8%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
9%
10%OUTPUT
11% ParamOut: sets input parameters when the function is selected, not activated(input Param.Action.RUN=0),=[] when the function is running
12%
13%INPUT:
14% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
15% In batch mode, Param is the name of the corresponding xml file containing the same information
16% when Param.Action.RUN=0 (as activated when the current Action is selected
17% in series), the function ouput paramOut set the activation of the needed GUI elements
18%
19% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
20% see the current structure Param)
21%    .InputTable: cell of input file names, (several lines for multiple input)
22%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
23%    .OutputSubDir: name of the subdirectory for data outputs
24%    .OutputDirExt: directory extension for data outputs
25%    .Action: .ActionName: name of the current activated function
26%             .ActionPath:   path of the current activated function
27%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
28%             .RUN =0 for GUI input, =1 for function activation
29%             .RunMode='local','background', 'cluster': type of function  use
30%             
31%    .IndexRange: set the file or frame indices on which the action must be performed
32%    .InputFields: sub structure describing the input fields withfields
33%              .FieldName: name(s) of the field
34%              .VelType: velocity type
35
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37
38%=======================================================================
39% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
40%   http://www.legi.grenoble-inp.fr
41%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
42%
43%     This file is part of the toolbox UVMAT.
44%
45%     UVMAT is free software; you can redistribute it and/or modify
46%     it under the terms of the GNU General Public License as published
47%     by the Free Software Foundation; either version 2 of the license,
48%     or (at your option) any later version.
49%
50%     UVMAT is distributed in the hope that it will be useful,
51%     but WITHOUT ANY WARRANTY; without even the implied warranty of
52%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
53%     GNU General Public License (see LICENSE.txt) for more details.
54%=======================================================================
55
56function ParamOut=test_filter_tps (Param)
57
58%%%%%%%%%%%%%%%%%    INPUT PREPARATION MODE (no RUN)    %%%%%%%%%%%%%%%%%
59if isstruct(Param) && isequal(Param.Action.RUN,0)
60    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
61    ParamOut.WholeIndexRange='one';% prescribes the file index ranges from min to max (options 'off'/'on'/'one' (single input index), 'off' by default)
62    ParamOut.NbSlice='off'; % edit box nbre of slices made active
63    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
64    ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
65    ParamOut.FieldTransform = 'off';%can use a transform function
66    ParamOut.ProjObject='off';%cannot use projection object(option 'off'/'on',
67    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
68    ParamOut.OutputDirExt='.test_filter';%set the output dir extension
69    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
70   
71    %% root input file(s) and type
72    % check the existence of the first file in the series
73     first_j=[];% note that the function will propose to cover the whole range of indices
74    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
75    PairString='';
76    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
77    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
78    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
79        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
80    if ~exist(FirstFileName,'file')
81        msgbox_uvmat('ERROR',['the input file ' FirstFileName ' does not exist'])
82        return
83    end
84
85    %% check the validity of  input file types
86    Data=nc2struct(FirstFileName,[]);
87    if isfield(Data,'CivStage')
88        switch Data.CivStage
89            case {1,2}
90                CivStage='civ1';
91                MaxDiff=1.5; SubDomainSize=250; FieldSmooth=10; %default
92            case 3
93                CivStage='civ1';
94                MaxDiff=Data.Patch1_MaxDiff;
95               SubDomainSize=Data.Patch1_SubDomainSize;
96               FieldSmooth=Data.Patch1_FieldSmooth;
97            case {4,5}
98                CivStage='civ2';
99                MaxDiff=1.5; SubDomainSize=250; FieldSmooth=5; %default
100            otherwise
101                CivStage='civ2';
102                 MaxDiff=Data.Patch2_MaxDiff;
103               SubDomainSize=Data.Patch2_SubDomainSize;
104               FieldSmooth=Data.Patch2_FieldSmooth;
105        end
106    else
107        msgbox_uvmat('ERROR','invalid file type input: test_filter_tps proceeds raw civ data')
108        return
109    end
110
111    %% numbers of fields
112    incr_j=1;%default
113    if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j)
114        incr_j=Param.IndexRange.incr_j;
115    end
116    if isempty(first_j)||isempty(last_j)
117        nbfield_j=1;
118    else
119        nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices)
120    end
121    first_i=1;last_i=1;incr_i=1;%default
122    if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end   
123    if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end
124    if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i)
125        incr_i=Param.IndexRange.incr_i;
126    end
127    nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices)
128    nbfield=nbfield_j*nbfield_i; %total number of fields
129   
130    %% setting of intput parameters
131    ListParam={'CivStage';'FieldSmooth';'MaxDiff';'SubDomainSize'};
132    DefaultValue={CivStage;FieldSmooth;MaxDiff;SubDomainSize};
133    if isfield(Param,'ActionInput')
134        ParamIn=Param.ActionInput;
135    else
136        ParamIn=[];
137    end
138        [ParamOut.ActionInput,errormsg] = set_param_input(ListParam,DefaultValue,ParamIn);
139        if ~isempty(errormsg)
140            msgbox_uvmat('ERROR',errormsg)
141        end
142    return
143end
144%%%%%%%%%%%%%%%%%    STOP HERE FOR PAMETER INPUT MODE   %%%%%%%%%%%%%%%%%
145
146%% read input parameters from an xml file if input is a file name (batch mode)
147checkrun=1;
148RUNHandle=[];
149WaitbarHandle=[];
150if ischar(Param)
151    Param=xml2struct(Param);% read Param as input file (batch case)
152    checkrun=0;
153else
154    hseries=findobj(allchild(0),'Tag','series');
155    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
156    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
157end
158
159%% input preparation
160RootPath=Param.InputTable{:,1};
161RootFile=Param.InputTable{:,3};
162NomType=Param.InputTable{:,4};
163[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
164% if ~isempty(hdisp),delete(hdisp),end;
165%%%%%%%%%%%%
166% The cell array filecell is the list of input file names, while
167% filecell{iview,fileindex}:
168%        iview: line in the table corresponding to a given file series
169%        fileindex: file index within  the file series,
170% 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
171% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
172%%%%%%%%%%%%
173
174%% output file naming
175FileExtOut='.nc'; % write result as .png images for image inputsFileInfo.FileType='image'
176NomTypeOut=NomType;
177OutputDir=[Param.OutputSubDir Param.OutputDirExt];
178OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
179RootFileOut=RootFile;
180
181%% file index parameters
182if strcmp(Param.ActionInput.CivStage,'civ1')
183    CivStage=3;
184else
185    CivStage=6;
186end
187
188Param.InputFields.VelType=Param.ActionInput.CivStage;
189SubDomainSize=Param.ActionInput.SubDomainSize;
190MaxDiff=Param.ActionInput.MaxDiff;
191FieldSmooth=(Param.ActionInput.FieldSmooth)*[0.1 0.2 0.5 1 2 5 10];%scan the smoothing param from 1/10 to 10 current value
192NbSmooth=numel(FieldSmooth);
193% for irho=1:NbSmooth
194%     str=num2str(FieldSmooth(irho));
195%     str=regexprep(str,'\.','p');
196%     Ustr{irho}=['U_' str];
197%     Vstr{irho}=['V_' str];
198%     Xstr{irho}=['X_' str];
199%     Ystr{irho}=['Y_' str];
200%     Dimstr{irho}='NbVec';
201%     str_i{irho}=str;
202% end
203
204%% Prepare the structure of output netcdf file
205DataOut.ListGlobalAttribute={'Conventions','Program','CivStage','SubDomainSize','MaxDiff','CoordUnit','FieldSmooth'};
206DataOut.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
207if isfield(Param,'UvmatRevision')
208DataOut.Program=['test_patch_tps, uvmat r' Param.UvmatRevision];
209else
210    DataOut.Program='test_patch_tps';
211end
212DataOut.CivStage=CivStage;%update the current civStage after smoothing
213DataOut.SubDomainSize=SubDomainSize;
214DataOut.MaxDiff=MaxDiff;
215DataOut.CoordUnit='pixel';
216ListVarName={'Civ2_X','Civ2_Y','Civ2_U','Civ2_V','Civ2_C','Civ2_FF','Civ2_U_smooth','Civ2_V_smooth'};
217if CivStage==3
218    ListVarName=regexprep(ListVarName,'Civ2','Civ1');
219    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Civ1_Dt','Civ1_Time'}];
220else
221    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Civ2_Dt','Civ2_Time'}];
222end
223DataOut.ListVarName=ListVarName;
224DataOut.VarDimName={'nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec'};
225DataOut.VarAttribute{1}.Role='coord_x';
226DataOut.VarAttribute{2}.Role='coord_y';
227DataOut.VarAttribute{3}.Role='vector_x';
228DataOut.VarAttribute{4}.Role='vector_y';
229DataOut.VarAttribute{5}.Role='ancillary';
230DataOut.VarAttribute{6}.Role='errorflag';
231DataOut.VarAttribute{7}.Role='vector_x';
232DataOut.VarAttribute{8}.Role='vector_y';
233
234%% MAIN LOOP
235
236Data=read_field(filecell{1,1},'civdata',Param.InputFields);
237ind_good=find(Data.FF==0|Data.FF==4);%keep good civ data, and also the ones excluded by the criterium of discrepancy betwween smoothed and raw fields (FF=2 or 20 (old convention))
238NbGood=numel(ind_good);
239Xin=Data.X(ind_good);
240Yin=Data.Y(ind_good);
241DataOut.(ListVarName{1})=Xin;
242DataOut.(ListVarName{2})=Yin;
243Uin=Data.U(ind_good);
244Vin=Data.V(ind_good);
245DataOut.(ListVarName{3})=Uin;
246DataOut.(ListVarName{4})=Vin;
247DataOut.(ListVarName{5})=Data.C(ind_good);
248[~,InputFile]=fileparts(filecell{1,1});
249tic
250for iter=1:NbSmooth
251    DataOut.FieldSmooth=FieldSmooth(iter);
252    [SubRange,NbCentres,Coord_tps,U_tps,V_tps,~,U_smooth, V_smooth,~,FFres]=...
253        filter_tps([Xin Yin],Uin,Vin,[],SubDomainSize,FieldSmooth(iter),MaxDiff);
254    if iter==1
255        figure(1)
256        cla
257        scatter(Xin,Yin,1)
258        for irec=1:size(SubRange,3)
259            rectangle('Position',[SubRange(:,1,irec)' SubRange(:,2,irec)'-SubRange(:,1,irec)'])
260        end
261        title('subdomains for thin shell splines (tps)')
262        if CivStage==3
263            DataOut.Civ1_Dt=Data.Civ1_Dt;
264            DataOut.Civ1_Time=Data.Civ1_Time;
265        else
266            DataOut.Civ2_Dt=Data.Civ2_Dt;
267            DataOut.Civ2_Time=Data.Civ2_Time;
268        end
269    end
270    ind_good=find(FFres==0);
271    U_Diff=Uin(ind_good)-U_smooth(ind_good);
272    V_Diff=Vin(ind_good)-V_smooth(ind_good);
273    DataOut.Diff_rms(iter)=sqrt(mean(U_Diff.*U_Diff+V_Diff.*V_Diff)/2);
274    DataOut.NbExclude(iter)=(NbGood-numel(ind_good))/NbGood;
275    DataOut.(ListVarName{6})=4*FFres;
276    DataOut.(ListVarName{7})=U_smooth;
277    DataOut.(ListVarName{8})=V_smooth;
278    OutputFile=[InputFile '_iter' num2str(iter) '.nc'];
279    OutputFile=fullfile(OutputPath,OutputDir,OutputFile);
280    errormsg=struct2nc(OutputFile,DataOut)
281end
282time=toc
283
284%plot rms difference and proportion of excluded vectors
285figure(2)
286cla
287if CivStage==3% civ1
288    ref=0.2; %recommanded value for diff rms
289    txt='civ1';
290else
291    ref=0.1;
292    txt='civ2';
293end
294semilogx(FieldSmooth,DataOut.Diff_rms,'b+-',FieldSmooth,DataOut.NbExclude,'m+-',FieldSmooth,ref*ones(size(FieldSmooth)),'b--')
295grid on
296title( [filecell{1,1} ':' txt])
297legend({'rms vel. diff. ' ;' ratio excluded vectors';['recommended diff for' txt]},'Location','northwest')
298xlabel('smoothing parameter')
299ylabel('rms (pixels) and proportion of excluded vectors')
300OutputFig=fullfile(OutputPath,OutputDir,'plot_rms_diff.png');
301saveas(2,OutputFig)
302
303
304
305
306 
307
308 
Note: See TracBrowser for help on using the repository browser.