source: trunk/src/RUN_FIX.m @ 56

Last change on this file since 56 was 56, checked in by sommeria, 11 years ago

uvmat: edit vector button activated
debugging and cleaning after modifications made with the windows version

File size: 7.7 KB
Line 
1%'RUN_FIX': function for fixing velocity fields:
2%-----------------------------------------------
3% RUN_FIX(filename,field,flagindex,thresh_vecC,thresh_vel,iter,flag_mask,maskname,fileref,fieldref)
4%
5%filename: name of the netcdf file (used as input and output)
6%field: structure specifying the names of the fields to fix (depending on civ1 or civ2)
7    %.vel_type='civ1' or 'civ2';
8    %.nb=name of the dimension common to the field to fix ('nb_vectors' for civ1);
9    %.fixflag=name of fix flag variable ('vec_FixFlag' for civ1)
10%flagindex: flag specifying which values of vec_f are removed:
11        % if flagindex(1)=1: vec_f=-2 vectors are removed
12        % if flagindex(2)=1: vec_f=3 vectors are removed
13        % if flagindex(3)=1: vec_f=2 vectors are removed (if iter=1) or vec_f=4 vectors are removed (if iter=2)
14%iter=1 for civ1 fields and iter=2 for civ2 fields
15%thresh_vecC: threshold in the image correlation vec_C
16%flag_mask: =1 mask used to remove vectors (0 else)
17%maskname: name of the mask image file for fix
18%thresh_vel: threshold on velocity, or on the difference with the reference file fileref if exists
19%inf_sup=1: remove values smaller than threshold thresh_vel, =2, larger than threshold
20%fileref: .nc file name for a reference velocity (='': refrence 0 used)
21%fieldref: 'civ1','filter1'...feld used in fileref
22
23function error=RUN_FIX(filename,field,flagindex,iter,thresh_vecC,flag_mask,maskname,thresh_vel,inf_sup,fileref,fieldref)
24error=[]; %default
25vel_type{1}=field.vel_type;
26%check writing access
27[errorread,message]=fileattrib(filename);
28if ~isempty(message) && ~isequal(message.UserWrite,1)
29     msgbox_uvmat('ERROR',['no writting access to ' filename ' (RUN_FIX.m)']);
30    return
31end
32Field=read_civxdata(filename,'ima_cor',field.vel_type);
33if isfield(Field,'Txt')
34    error=Field.Txt; %error in reading
35    return
36end
37
38if ~isfield(Field,'X') || ~isfield(Field,'Y') || ~isfield(Field,'U') || ~isfield(Field,'V')
39    error=['input file ' filename ' does not contain vectors in RUN_FIX.m']; %bad input file
40    return
41end
42if ~isfield(Field,'C')
43    Field.C=ones(size(Field.X));%correlation=1 by default
44end
45if ~isfield(Field,'F')
46    Field.F=ones(size(Field.X));%warning flag=1 by default
47end
48if ~isfield(Field,'FF')
49    Field.FF=zeros(size(Field.X));%fixflag=0 by default
50end
51
52vec_f_unit=abs(Field.F)-10*double(uint16(abs(Field.F)/10)); %unityterm of vec_F in abs value
53vec_f_sign=sign(Field.F).*vec_f_unit;% gives the unity digit of vec_f with correct sign
54flag1=(flagindex(1)==1)&(vec_f_sign==-2);%removed vectors vec_f=-2
55flag2=(flagindex(2)==1)&(vec_f_sign==3);%removed vectors vec_f=3
56if iter==1
57        flag3=(flagindex(3)==1)&(vec_f_sign==2); % Hart vectors
58elseif iter==2
59        flag3=(flagindex(3)==1)&(vec_f_sign==4); %
60end
61flag4=(Field.C < thresh_vecC)&(flag1~=1)&(flag2~=1)&(flag3~=1); % =1 for low vec_C vectors not previously removed
62
63% criterium on velocity values
64delta_u=Field.U;%default without ref file
65delta_v=Field.V;
66if exist('fileref','var') && ~isempty(fileref)
67    if ~exist(fileref,'file')
68        error='reference file not found in RUN_FIX.m';
69        display(error);
70        return
71    end
72    FieldRef=read_civxdata(fileref,[],fieldref);   
73    if isfield(FieldRef,'FF')
74        index_true=find(FieldRef.FF==0);
75        FieldRef.X=FieldRef.X(index_true);
76        FieldRef.Y=FieldRef.Y(index_true);
77        FieldRef.U=FieldRef.U(index_true);
78        FieldRef.V=FieldRef.V(index_true);
79    end
80    if ~isfield(FieldRef,'X') || ~isfield(FieldRef,'Y') || ~isfield(FieldRef,'U') || ~isfield(FieldRef,'V')
81        error='reference file is not a velocity field in RUN_FIX.m '; %bad input file
82        return
83    end
84    if length(FieldRef.X)<=1
85        errordlg('reference field with one vector or less in RUN_FIX.m')
86        return
87    end
88    vec_U_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.U,Field.X,Field.Y);  %interpolate vectors in the ref field
89    vec_V_ref=griddata_uvmat(FieldRef.X,FieldRef.Y,FieldRef.V,Field.X,Field.Y);  %interpolate vectors in the ref field to the positions  of the main field     
90    delta_u=Field.U-vec_U_ref;%take the difference with the interpolated ref field
91    delta_v=Field.V-vec_V_ref;
92end
93thresh_vel_x=thresh_vel;
94thresh_vel_y=thresh_vel;
95if isequal(inf_sup,1)
96    flag5=abs(delta_u)<thresh_vel_x & abs(delta_v)<thresh_vel_y &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
97elseif isequal(inf_sup,2)
98    flag5=(abs(delta_u)>thresh_vel_x | abs(delta_v)>thresh_vel_y) &(flag1~=1)&(flag2~=1)&(flag3~=1)&(flag4~=1);
99end
100
101            % flag7 introduce a grey mask, matrix M
102if isequal (flag_mask,1)
103   M=imread(maskname);
104   nxy=size(M);
105   M=reshape(M,1,nxy(1)*nxy(2));
106   rangx0=[0.5 nxy(2)-0.5];
107   rangy0=[0.5 nxy(1)-0.5];
108   vec_x1=Field.X-Field.U/2;%beginning points
109   vec_x2=Field.X+Field.U/2;%end points of vectors
110   vec_y1=Field.Y-Field.V/2;%beginning points
111   vec_y2=Field.Y+Field.V/2;%end points of vectors
112   indx=1+round((nxy(2)-1)*(vec_x1-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa vec_x
113   indy=1+round((nxy(1)-1)*(vec_y1-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y   
114   test_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
115   indx=indx.*test_in+(1-test_in); %replace indx by 1 out of the mask range
116   indy=indy.*test_in+(1-test_in); %replace indy by 1 out of the mask range
117   ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
118   Mvalues=M(ICOMB);
119   flag7b=((20 < Mvalues) & (Mvalues < 200))| ~test_in';
120   indx=1+round((nxy(2)-1)*(vec_x2-rangx0(1))/(rangx0(2)-rangx0(1)));% image index x at abcissa Field.X
121   indy=1+round((nxy(1)-1)*(vec_y2-rangy0(1))/(rangy0(2)-rangy0(1)));% image index y at ordinate vec_y
122   test_in=~(indx < 1 |indy < 1 | indx > nxy(2) |indy > nxy(1)); %=0 out of the mask image, 1 inside
123   indx=indx.*test_in+(1-test_in); %replace indx by 1 out of the mask range
124   indy=indy.*test_in+(1-test_in); %replace indy by 1 out of the mask range
125   ICOMB=((indx-1)*nxy(1)+(nxy(1)+1-indy));%determine the indices in the image reshaped in a Matlab vector
126   Mvalues=M(ICOMB);
127   flag7e=((Mvalues > 20) & (Mvalues < 200))| ~test_in';
128   flag7=(flag7b|flag7e)';
129else
130   flag7=0;
131end   
132flagmagenta=flag1|flag2|flag3|flag4|flag5|flag7;
133fixflag_unit=Field.FF-10*floor(Field.FF/10); %unity term of fix_flag
134
135%write fix flags in the netcdf file
136hhh=which('netcdf.open');% look for built-in matlab netcdf library
137if ~isequal(hhh,'')% case of new builtin Matlab netcdf library
138    nc=netcdf.open(filename,'NC_WRITE');
139    netcdf.reDef(nc)
140    if iter==1
141        netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'fix1',1)
142    elseif iter==2
143        netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'fix2',1)
144    end
145    dimid = netcdf.inqDimID(nc,field.nb);
146    try
147        varid = netcdf.inqVarID(nc,field.fixflag);% look for already existing fixflag variable
148    catch
149        varid=netcdf.defVar(nc,field.fixflag,'double',dimid);%create fixflag variable if it does not exist
150    end
151    netcdf.endDef(nc)
152    netcdf.putVar(nc,varid,fixflag_unit+10*flagmagenta);
153    netcdf.close(nc)
154else %old netcdf library
155    netcdf_toolbox(filename,Field,flagmagenta,iter,field)
156end
157
158function netcdf_toolbox(filename,Field,flagmagenta,iter,field)
159nc=netcdf(filename,'write'); %open netcdf file for writing
160result=redef(nc);
161if isempty(result), msgbox_uvmat('ERROR','##Bad redef operation.'),end 
162if iter==1
163    nc.fix=1;
164elseif iter==2
165    nc.fix2=1;
166end
167nc{field.fixflag}=ncfloat(field.nb);
168fixflag_unit=Field.FF-10*floor(Field.FF/10); %unity term of fix_flag
169nc{field.fixflag}(:)=fixflag_unit+10*flagmagenta;
170close(nc);
Note: See TracBrowser for help on using the repository browser.