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

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

bugs repaired

File size: 13.4 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,'MinIndex_j'); first_j=Param.IndexRange.MinIndex_j; end
75    last_j=[];
76    if isfield(Param.IndexRange,'MaxIndex_j'); last_j=Param.IndexRange.MaxIndex_j; end
77    PairString='';
78    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
79    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
80    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
81        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
82    if ~exist(FirstFileName,'file')
83        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
84        return
85    else
86        [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString);
87        LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
88        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
89        if ~exist(FirstFileName,'file')
90             msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist'])
91        end
92    end
93
94    %% check the validity of  input file types
95    Data=nc2struct(FirstFileName,[]);
96    if isfield(Data,'CivStage')
97        switch Data.CivStage
98            case {1,2}
99                CivStage='civ1';
100                MaxDiff=1.5; SubDomainSize=250; FieldSmooth=10; %default
101            case 3
102                CivStage='civ1';
103                MaxDiff=Data.Patch1_MaxDiff;
104               SubDomainSize=Data.Patch1_SubDomainSize;
105               FieldSmooth=Data.Patch1_FieldSmooth;
106            case {4,5}
107                CivStage='civ2';
108                MaxDiff=1.5; SubDomainSize=250; FieldSmooth=5; %default
109            otherwise
110                CivStage='civ2';
111                 MaxDiff=Data.Patch2_MaxDiff;
112               SubDomainSize=Data.Patch2_SubDomainSize;
113               FieldSmooth=Data.Patch2_FieldSmooth;
114        end
115    else
116        msgbox_uvmat('ERROR',['invalid file type input: ' FileType ' not a civ data'])
117        return
118    end
119
120    %% numbers of fields
121    incr_j=1;%default
122    if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j)
123        incr_j=Param.IndexRange.incr_j;
124    end
125    if isempty(first_j)||isempty(last_j)
126        nbfield_j=1;
127    else
128        nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices)
129    end
130    first_i=1;last_i=1;incr_i=1;%default
131    if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end   
132    if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end
133    if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i)
134        incr_i=Param.IndexRange.incr_i;
135    end
136    nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices)
137    nbfield=nbfield_j*nbfield_i; %total number of fields
138   
139    %% setting of intput parameters
140    ListParam={'CivStage';'FieldSmooth';'MaxDiff';'SubDomainSize'};
141    DefaultValue={CivStage;FieldSmooth;MaxDiff;SubDomainSize};
142    if isfield(Param,'ActionInput')
143        ParamIn=Param.ActionInput;
144    else
145        ParamIn=[];
146    end
147        [ParamOut.ActionInput,errormsg] = set_param_input(ListParam,DefaultValue,ParamIn);
148        if ~isempty(errormsg)
149            msgbox_uvmat('ERROR',errormsg)
150        end
151    return
152end
153%%%%%%%%%%%%%%%%%    STOP HERE FOR PAMETER INPUT MODE   %%%%%%%%%%%%%%%%%
154
155%% read input parameters from an xml file if input is a file name (batch mode)
156checkrun=1;
157RUNHandle=[];
158WaitbarHandle=[];
159if ischar(Param)
160    Param=xml2struct(Param);% read Param as input file (batch case)
161    checkrun=0;
162else
163    hseries=findobj(allchild(0),'Tag','series');
164    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
165    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
166end
167
168%% input preparation
169RootPath=Param.InputTable{:,1};
170RootFile=Param.InputTable{:,3};
171NomType=Param.InputTable{:,4};
172[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
173% if ~isempty(hdisp),delete(hdisp),end;
174%%%%%%%%%%%%
175% The cell array filecell is the list of input file names, while
176% filecell{iview,fileindex}:
177%        iview: line in the table corresponding to a given file series
178%        fileindex: file index within  the file series,
179% 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
180% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
181%%%%%%%%%%%%
182
183%% output file naming
184FileExtOut='.nc'; % write result as .png images for image inputsFileInfo.FileType='image'
185NomTypeOut=NomType;
186OutputDir=[Param.OutputSubDir Param.OutputDirExt];
187OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
188RootFileOut=RootFile;
189
190%% file index parameters
191if strcmp(Param.ActionInput.CivStage,'civ1')
192    CivStage=3;
193else
194    CivStage=6;
195end
196
197Param.InputFields.VelType=Param.ActionInput.CivStage;
198SubDomainSize=Param.ActionInput.SubDomainSize;
199MaxDiff=Param.ActionInput.MaxDiff;
200FieldSmooth=(Param.ActionInput.FieldSmooth)*[0.1 0.2 0.5 1 2 5 10];%scan the smoothing param from 1/10 to 10 current value
201NbSmooth=numel(FieldSmooth);
202% for irho=1:NbSmooth
203%     str=num2str(FieldSmooth(irho));
204%     str=regexprep(str,'\.','p');
205%     Ustr{irho}=['U_' str];
206%     Vstr{irho}=['V_' str];
207%     Xstr{irho}=['X_' str];
208%     Ystr{irho}=['Y_' str];
209%     Dimstr{irho}='NbVec';
210%     str_i{irho}=str;
211% end
212
213%% Prepare the structure of output netcdf file
214DataOut.ListGlobalAttribute={'Conventions','Program','CivStage','SubDomainSize','MaxDiff','CoordUnit','FieldSmooth'};
215DataOut.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
216if isfield(Param,'UvmatRevision')
217DataOut.Program=['test_patch_tps, uvmat r' Param.UvmatRevision];
218else
219    DataOut.Program='test_patch_tps';
220end
221DataOut.CivStage=CivStage;%update the current civStage after smoothing
222DataOut.SubDomainSize=SubDomainSize;
223DataOut.MaxDiff=MaxDiff;
224DataOut.CoordUnit='pixel';
225ListVarName={'Civ2_X','Civ2_Y','Civ2_U','Civ2_V','Civ2_C','Civ2_FF','Civ2_U_smooth','Civ2_V_smooth'};
226if CivStage==3
227    ListVarName=regexprep(ListVarName,'Civ2','Civ1');
228    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Civ1_Dt','Civ1_Time'}];
229else
230    DataOut.ListGlobalAttribute=[DataOut.ListGlobalAttribute {'Civ2_Dt','Civ2_Time'}];
231end
232DataOut.ListVarName=ListVarName;
233DataOut.VarDimName={'nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec'};
234DataOut.VarAttribute{1}.Role='coord_x';
235DataOut.VarAttribute{2}.Role='coord_y';
236DataOut.VarAttribute{3}.Role='vector_x';
237DataOut.VarAttribute{4}.Role='vector_y';
238DataOut.VarAttribute{5}.Role='ancillary';
239DataOut.VarAttribute{6}.Role='errorflag';
240DataOut.VarAttribute{7}.Role='vector_x';
241DataOut.VarAttribute{8}.Role='vector_y';
242
243%% MAIN LOOP
244
245Data=read_field(filecell{1,1},'civdata',Param.InputFields);
246ind_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))
247NbGood=numel(ind_good);
248Xin=Data.X(ind_good);
249Yin=Data.Y(ind_good);
250DataOut.(ListVarName{1})=Xin;
251DataOut.(ListVarName{2})=Yin;
252Uin=Data.U(ind_good);
253Vin=Data.V(ind_good);
254DataOut.(ListVarName{3})=Uin;
255DataOut.(ListVarName{4})=Vin;
256DataOut.(ListVarName{5})=Data.C(ind_good);
257[~,InputFile]=fileparts(filecell{1,1});
258tic
259for iter=1:NbSmooth
260    DataOut.FieldSmooth=FieldSmooth(iter);
261    [SubRange,NbCentres,Coord_tps,U_tps,V_tps,~,U_smooth, V_smooth,~,FFres]=...
262        filter_tps([Xin Yin],Uin,Vin,[],SubDomainSize,FieldSmooth(iter),MaxDiff);
263    if iter==1
264        figure(1)
265        cla
266        scatter(Xin,Yin,1)
267        for irec=1:size(SubRange,3)
268            rectangle('Position',[SubRange(:,1,irec)' SubRange(:,2,irec)'-SubRange(:,1,irec)'])
269        end
270        title('subdomains for thin shell splines (tps)')
271        if CivStage==3
272            DataOut.Civ1_Dt=Data.Civ1_Dt;
273            DataOut.Civ1_Time=Data.Civ1_Time;
274        else
275            DataOut.Civ2_Dt=Data.Civ2_Dt;
276            DataOut.Civ2_Time=Data.Civ2_Time;
277        end
278    end
279    ind_good=find(FFres==0);
280    U_Diff=Uin(ind_good)-U_smooth(ind_good);
281    V_Diff=Vin(ind_good)-V_smooth(ind_good);
282    DataOut.Diff_rms(iter)=sqrt(mean(U_Diff.*U_Diff+V_Diff.*V_Diff)/2);
283    DataOut.NbExclude(iter)=(NbGood-numel(ind_good))/NbGood;
284    DataOut.(ListVarName{6})=4*FFres;
285    DataOut.(ListVarName{7})=U_smooth;
286    DataOut.(ListVarName{8})=V_smooth;
287    OutputFile=[InputFile '_iter' num2str(iter) '.nc'];
288    OutputFile=fullfile(OutputPath,OutputDir,OutputFile);
289    errormsg=struct2nc(OutputFile,DataOut)
290end
291time=toc
292
293%plot rms difference and proportion of excluded vectors
294figure(2)
295cla
296if CivStage==3% civ1
297    ref=0.2; %recommanded value for diff rms
298    txt='civ1';
299else
300    ref=0.1;
301    txt='civ2';
302end
303semilogx(FieldSmooth,DataOut.Diff_rms,'b+-',FieldSmooth,DataOut.NbExclude,'m+-',FieldSmooth,ref*ones(size(FieldSmooth)),'b--')
304grid on
305title( [filecell{1,1} ':' txt])
306legend({'rms vel. diff. ' ;' ratio excluded vectors';['recommended diff for' txt]},'Location','northwest')
307xlabel('smoothing parameter')
308ylabel('rms (pixels) and proportion of excluded vectors')
309OutputFig=fullfile(OutputPath,OutputDir,'plot_rms_diff.png');
310saveas(2,OutputFig)
311
312
313
314
315 
316
317 
Note: See TracBrowser for help on using the repository browser.