source: trunk/src/series/test_patch_tps.m @ 1141

Last change on this file since 1141 was 1141, checked in by sommeria, 5 months ago

smoothig parameter renormalised in filter_tps

File size: 12.3 KB
Line 
1%'test_patch_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%              .FieldName_1: name of the second field in case of two input series
36%              .VelType_1: velocity type of the second field in case of two input series
37%              .Coord_y: name of y coordinate variable
38%              .Coord_x: name of x coordinate variable
39
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41
42%=======================================================================
43% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
44%   http://www.legi.grenoble-inp.fr
45%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
46%
47%     This file is part of the toolbox UVMAT.
48%
49%     UVMAT is free software; you can redistribute it and/or modify
50%     it under the terms of the GNU General Public License as published
51%     by the Free Software Foundation; either version 2 of the license,
52%     or (at your option) any later version.
53%
54%     UVMAT is distributed in the hope that it will be useful,
55%     but WITHOUT ANY WARRANTY; without even the implied warranty of
56%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57%     GNU General Public License (see LICENSE.txt) for more details.
58%=======================================================================
59
60function ParamOut=test_patch_tps (Param)
61
62%%%%%%%%%%%%%%%%%    INPUT PREPARATION MODE (no RUN)    %%%%%%%%%%%%%%%%%
63if isstruct(Param) && isequal(Param.Action.RUN,0)
64    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
65    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
66    ParamOut.NbSlice='off'; % edit box nbre of slices made active
67    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
68    ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
69    ParamOut.FieldTransform = 'off';%can use a transform function
70    ParamOut.ProjObject='off';%cannot use projection object(option 'off'/'on',
71    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
72    ParamOut.OutputDirExt='.test_patch';%set the output dir extension
73    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
74   
75    %% root input file(s) and type
76    % check the existence of the first file in the series
77     first_j=[];% note that the function will propose to cover the whole range of indices
78    if isfield(Param.IndexRange,'MinIndex_j'); first_j=Param.IndexRange.MinIndex_j; end
79    last_j=[];
80    if isfield(Param.IndexRange,'MaxIndex_j'); last_j=Param.IndexRange.MaxIndex_j; end
81    PairString='';
82    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
83    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
84    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
85        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
86    if ~exist(FirstFileName,'file')
87        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
88        return
89    else
90        [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString);
91        LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
92        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
93        if ~exist(FirstFileName,'file')
94             msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist'])
95        end
96    end
97
98    %% check the validity of  input file types
99    Data=nc2struct(FirstFileName,[]);
100    if isfield(Data,'CivStage')
101        switch Data.CivStage
102            case {1,2}
103                CivStage='civ1';
104                MaxDiff=1.5; SubDomainSize=250; FieldSmooth=10; %default
105            case 3
106                CivStage='civ1';
107                MaxDiff=Data.Patch1_MaxDiff;
108               SubDomainSize=Data.Patch1_SubDomainSize;
109               FieldSmooth=Data.Patch1_FieldSmooth;
110            case {4,5}
111                CivStage='civ2';
112                MaxDiff=1.5; SubDomainSize=500; FieldSmooth=2; %default
113            case 6
114                CivStage='civ2';
115                 MaxDiff=Data.Patch2_MaxDiff;
116               SubDomainSize=Data.Patch2_SubDomainSize;
117               FieldSmooth=Data.Patch2_FieldSmooth;
118        end
119    else
120        msgbox_uvmat('ERROR',['invalid file type input: ' FileType ' not a civ data'])
121        return
122    end
123
124    %% numbers of fields
125    incr_j=1;%default
126    if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j)
127        incr_j=Param.IndexRange.incr_j;
128    end
129    if isempty(first_j)||isempty(last_j)
130        nbfield_j=1;
131    else
132        nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices)
133    end
134    first_i=1;last_i=1;incr_i=1;%default
135    if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end   
136    if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end
137    if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i)
138        incr_i=Param.IndexRange.incr_i;
139    end
140    nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices)
141    nbfield=nbfield_j*nbfield_i; %total number of fields
142   
143    %% setting of intput parameters
144    ListParam={'CivStage';'FieldSmooth';'MaxDiff';'SubDomainSize'};
145    DefaultValue={CivStage;FieldSmooth;MaxDiff;SubDomainSize};
146    if isfield(Param,'ActionInput')
147        ParamIn=Param.ActionInput;
148    else
149        ParamIn=[];
150    end
151        [ParamOut.ActionInput,errormsg] = set_param_input(ListParam,DefaultValue,ParamIn);
152        if ~isempty(errormsg)
153            msgbox_uvmat('ERROR',errormsg)
154        end
155    return
156end
157%%%%%%%%%%%%%%%%%    STOP HERE FOR PAMETER INPUT MODE   %%%%%%%%%%%%%%%%%
158
159%% read input parameters from an xml file if input is a file name (batch mode)
160checkrun=1;
161RUNHandle=[];
162WaitbarHandle=[];
163if ischar(Param)
164    Param=xml2struct(Param);% read Param as input file (batch case)
165    checkrun=0;
166else
167hseries=findobj(allchild(0),'Tag','series');
168RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
169WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
170end
171
172%% input preparation
173RootPath=Param.InputTable{:,1};
174RootFile=Param.InputTable{:,3};
175NomType=Param.InputTable{:,4};
176[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
177% if ~isempty(hdisp),delete(hdisp),end;
178%%%%%%%%%%%%
179    % The cell array filecell is the list of input file names, while
180    % filecell{iview,fileindex}:
181    %        iview: line in the table corresponding to a given file series
182    %        fileindex: file index within  the file series,
183    % 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
184    % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
185%%%%%%%%%%%%
186
187%% output file naming
188FileExtOut='.nc'; % write result as .png images for image inputsFileInfo.FileType='image'
189NomTypeOut=NomType;
190OutputDir=[Param.OutputSubDir Param.OutputDirExt];
191OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
192RootFileOut=RootFile;
193
194%% file index parameters
195
196Param.InputFields.VelType=Param.ActionInput.CivStage;
197SubDomainSize=Param.ActionInput.SubDomainSize;
198MaxDiff=Param.ActionInput.MaxDiff;
199FieldSmooth=(Param.ActionInput.FieldSmooth)*[0.1 0.2 0.5 1 2 5 10];%scan the smoothing param from 1/10 to 10 current value
200NbSmooth=numel(FieldSmooth);
201for irho=1:NbSmooth
202    str=num2str(FieldSmooth(irho));
203    str=regexprep(str,'\.','p');
204    Ustr{irho}=['U_' str];
205    Vstr{irho}=['V_' str];
206    Xstr{irho}=['X_' str];
207    Ystr{irho}=['Y_' str];
208    Dimstr{irho}='NbVec';
209    str_i{irho}=str;
210end
211
212%% Prepare the structure of output netcdf file
213DataOut.ListGlobalAttribute={'CivStage'};
214DataOut.CivStage=Param.InputFields.VelType;
215DataOut.ListVarName=[{'FieldSmooth','Diff_rms','NbExclude','FF','X','Y'} Ustr Vstr] ;
216DataOut.VarDimName=[{'FieldSmooth','FieldSmooth','FieldSmooth','NbVec','NbVec','NbVec'} Dimstr Dimstr];
217DataOut.VarAttribute{4}.Role='falseflag';
218DataOut.FieldSmooth=FieldSmooth;
219
220%% MAIN LOOP
221for index=1:1%numel(filecell)
222    Data=read_field(filecell{1,index},'civdata',Param.InputFields);
223            ind_good=find(Data.FF==0|Data.FF==20);%keep good civ data, and also the ones excluded by the criterium of discrepancy betwween smoothed and raw fields (FF=20)
224        NbGood=numel(ind_good);
225        Xin=Data.X(ind_good);
226        Yin=Data.Y(ind_good);
227        Uin=Data.U(ind_good);
228        Vin=Data.V(ind_good);
229        tic
230    for irho=1:NbSmooth
231        [SubRange,NbCentres,Coord_tps,U_tps,V_tps,~,U_smooth, V_smooth,~,FFres]=...
232            filter_tps([Xin Yin],Uin,Vin,[],SubDomainSize,FieldSmooth(irho),MaxDiff);
233        if irho==1
234            DataOut.FF=zeros(size(Xin));
235            DataOut.X=Xin;
236            DataOut.Y=Yin;
237        figure(1)
238            cla
239            scatter(Xin,Yin,1)
240            for irec=1:size(SubRange,3)
241                rectangle('Position',[SubRange(:,1,irec)' SubRange(:,2,irec)'-SubRange(:,1,irec)'])
242            end       
243            title('subdomains for thin shell splines (tps)')
244        end
245        ind_good=find(FFres==0);
246        ind_false=find(FFres~=0);
247        U_Diff=Uin(ind_good)-U_smooth(ind_good);
248        V_Diff=Vin(ind_good)-V_smooth(ind_good);
249        DataOut.Diff_rms(irho)=sqrt(mean(U_Diff.*U_Diff+V_Diff.*V_Diff)/2);
250        DataOut.NbExclude(irho)=(NbGood-numel(ind_good))/NbGood;
251        DataOut.(['U_' str_i{irho}])=U_smooth;
252        DataOut.(['V_' str_i{irho}])=V_smooth;
253        DataOut.FF(ind_false)=FieldSmooth(irho);
254    end
255    time=toc
256    OutputFile=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,FileExtOut,NomTypeOut,i1_series{1}(index),[],j1_series{1}(index),j2_series{1}(index))
257    errormsg=struct2nc(OutputFile,DataOut)
258end
259figure
260    semilogx(FieldSmooth,DataOut.Diff_rms,'b',FieldSmooth,DataOut.NbExclude,'r',FieldSmooth,0.1*ones(size(FieldSmooth)),'m')
261    grid on
262    title( [filecell{1,1} ':' Param.InputFields.VelType])
263    legend({'rms vel. diff. ' ;' ratio excluded vectors';'recommended diff'},'Location','northwest')
264    xlabel('smoothing parameter')
265    ylabel('rms (pixels) and exclusion ratio')
266    OutputFig=fullfile(OutputPath,OutputDir,'plot_rms_diff.png')
267    saveas(2,OutputFig)
268
269
270
271
272 
273
274 
Note: See TracBrowser for help on using the repository browser.