source: altifloat/matlab_toolbox/filter_coeff.m @ 199

Last change on this file since 199 was 129, checked in by jbrlod, 10 years ago

last version of Varanth

File size: 10.3 KB
Line 
1function [ output_args ] = filter_coeff( aviso_netcdf_file_bck1, aviso_netcdf_file_next1, filterlarger )
2
3
4
5write_u=1;%if you want to write the velocities (true and background) to YAO DAT file
6write_coefs=1;%if you want to write the filters coefs
7%------------GRID-------------------------------------------------------%
8
9%for plotting
10
11Nlat=58; %Phi
12Nlong=87; %Lambda
13%The grid in degrees
14load '../obs_float/meshgrid2.dat';
15
16GR_long=reshape(meshgrid2(:,1),Nlong,Nlat); %will give a Nlong by Nlat matrix
17%containing the long of the points
18
19GR_lat=reshape(meshgrid2(:,2),Nlong,Nlat); %will give a Nlong by Nlat matrix
20%containing the lat of the points
21
22
23
24
25
26
27%-----------The velocities U and V---------------------------------------%
28
29
30
31%Load directly from AVISO folder where the u s for the whole mediterranean:
32
33
34
35%UTRUE=the first week
36
37U1=ncread(strcat('../obs_aviso/',aviso_netcdf_file_bck1,'.nc'),'Grid_0001');
38uaviso1=U1(1:58,end-86:end)./100; %choose the eatern med and make it in m/s
39
40V1=ncread(strcat('../obs_aviso/',aviso_netcdf_file_bck1,'.nc'),'Grid_0002');
41vaviso1=V1(1:58,end-86:end)./100; %choose the eatern med and make it in m/s
42
43
44
45
46%U the second week
47
48U2=ncread(strcat('../obs_aviso/',aviso_netcdf_file_next1,'.nc'),'Grid_0001');
49uaviso2=U2(1:58,end-86:end)./100;
50
51V2=ncread(strcat('../obs_aviso/',aviso_netcdf_file_next1,'.nc'),'Grid_0002');
52vaviso2=V2(1:58,end-86:end)./100;
53
54
55%do the mask manually:
56II1=find(isfinite(uaviso1)==0);
57uaviso1(II1)=0;
58II2=find(isfinite(uaviso2)==0);
59uaviso2(II2)=0;
60
61JJ1=find(isfinite(vaviso1)==0);
62vaviso1(JJ1)=0;
63JJ2=find(isfinite(vaviso2)==0);
64vaviso2(JJ2)=0;
65
66
67%interpolate to get the field every 2 h during the first week, in case need velocity in between:
68Nt=7*24/2; %one week in units of delta t
69for jt=1:Nt+1
70    umod(:,:,jt)=(uaviso1*(Nt+1-jt)./Nt)+ (uaviso2*(jt-1)/Nt);
71    vmod(:,:,jt)=(vaviso1*(Nt+1-jt)./Nt)+ (vaviso2*(jt-1)/Nt);
72end
73   
74
75
76
77
78
79%change grid from degrees to uniteless
80
81R_earth=6371229; %in meters
82delta_x=zeros(Nlong,Nlat);
83delta_y=zeros(Nlong,Nlat);
84
85for ilon=1:Nlong-1;
86    for jlat=1:Nlat-1;
87       
88    delta_x(ilon,jlat)=R_earth*(2*pi/360)*(GR_long(ilon+1,jlat)-GR_long(ilon,jlat))...
89        .*cos(2*pi*GR_lat(ilon,jlat)/360);
90    delta_y(ilon,jlat)=R_earth*(2*pi/360)*(GR_lat(ilon,jlat+1)-GR_lat(ilon,jlat)); 
91   
92   
93end
94end
95
96
97
98delta_x(Nlong,1:Nlat)=delta_x(Nlong-1,1:Nlat);
99delta_y(Nlong,1:Nlat)=delta_y(Nlong-1,1:Nlat);
100
101
102delta_x(1:Nlong,Nlat)=delta_x(1:Nlong,Nlat-1);
103delta_y(1:Nlong,Nlat)=delta_y(1:Nlong,Nlat-1);
104
105
106
107%choose UTRUE=Ufirst week
108
109uu=umod(:,:,1)./delta_x.'; %u has units of pers second
110vv=vmod(:,:,1)./delta_y.';
111
112%Choose Uback=Uafter 3days
113
114uu_pert=umod(:,:,35)./delta_x.';
115vv_pert=vmod(:,:,35)./delta_y.';
116
117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118
119
120%%%%%%%%%%%%%%%%%%%PUT U in YAO format%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121
122%create a Nlat*Nlong by 5 matrix first
123u_yao1=reshape(uu, Nlong*Nlat,1);
124v_yao1=reshape(vv, Nlong*Nlat,1);
125
126up_yao1=reshape(uu_pert, Nlong*Nlat,1);
127vp_yao1=reshape(vv_pert, Nlong*Nlat,1);
128%create the vectors rehsaped
129cd1=[]; for k=1:Nlong; cd1=[cd1; k*ones(Nlat,1)]; end
130cd2=repmat([1:1:Nlat].',Nlong,1);
131
132Yao_u=[ ones(Nlong*Nlat,1)  cd1 cd2 u_yao1];
133Yao_v=[ ones(Nlong*Nlat,1)  cd1 cd2 v_yao1];
134
135Yao_up=[ ones(Nlong*Nlat,1)  cd1 cd2 up_yao1];
136Yao_vp=[ ones(Nlong*Nlat,1)  cd1 cd2 vp_yao1];
137
138if (write_u==1)
139
140dlmwrite('../obs_float/uzero.dat',Yao_u,'\t');
141dlmwrite('../obs_float/vzero.dat',Yao_v,'\t');
142%save UV_0.mat uu vv;
143dlmwrite('../obs_float/unext3d.dat',Yao_up,'\t');
144dlmwrite('../obs_float/vnext3d.dat',Yao_vp,'\t');
145%save UVnext3d.mat uu_pert vv_pert cc1 cc2;
146end;
147
148
149
150
151
152
153
154%---------------------FILTER--------------------------------------------%
155
156%%%%%%%%%%%Get filter coeff%%%%%%%%%
157
158
159%length of filter in m, you can change this to 25 000 or 15 000, etc..
160%WHEN you change this, 2 important numbers for the YAO code change, the
161%filter order and the filter's time step.
162lfil=filterlarger;
163
164
165
166load '../obs_float/mask.dat';
167mask2=reshape(mask,87,58);
168
169umask=zeros(Nlong,Nlat); vmask=umask;
170for i=1:Nlong-1
171    for j=1:Nlat-1
172        umask(i,j)=mask2(i,j).*mask2(i+1,j);
173        vmask(i,j)=mask2(i,j).*mask2(i,j+1);
174    end
175end
176
177[minx gg1]=min(min(delta_x));
178[miny gg2]=min(min(delta_y));
179itxmin=floor(2*lfil*lfil./minx.^2);
180itymin=floor(2*lfil*lfil./miny.^2);
181
182
183filter_order=ceil((max(itxmin,itymin)+1)./2)
184%THIS IS AN IMPORTANT NUMBER, when you change the filterlength it will
185%change. IN YAO, this variable is 
186%defval OFIL 4 //order of the filter
187%in the floater_delta.d file. SO just copy th evalue from here
188
189
190
191
192
193
194
195
196nbitfil=filter_order*2;%nb of filter passes
197%nb of filter passes
198
199
200
201dtfil=0.5*lfil*lfil./nbitfil %filter time step
202%%%%%%THIS NUMBER IS ALSO IMPORTANT AS IT GOES IN THE floater.h code in the
203%%%%%%variable YREAL dtfil=...; As you change the filter length it will
204%%%%%%change
205
206
207%%%%%%filter normalization coefs%%%%%%%%%%%%%%%%
208
209
210%%Version Leila :
211% fileu=umask./sqrt(delta_x.*delta_y);
212% filev=vmask./sqrt(delta_x.*delta_y);
213
214%%Test 29/07/2014
215fileu=1./sqrt(delta_x.*delta_y);
216filev=1./sqrt(delta_x.*delta_y);
217
218dx=delta_x;
219dy=delta_y;
220c1=zeros(Nlong,Nlat); c2=c1; c3=c1; c4=c1; c5=c1; c6=c1; c7=c1; c8=c1; c9=c1;
221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222%ucoefs
223for i=2:Nlong-1
224    for j=2:Nlat-1;
225c1(i,j) = -1/dx(i,j)^2-dy(i,j)/(dx(i,j)*dx(i+1,j)*dy(i+1,j))-mask2(i,j-1)*mask2(i,j)*mask2(i+1,j-1)*mask2(i+1,j)*dx(i,j)/(dy(i,j)*dx(i,j-1)*dy(i,j-1))-mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)/dy(i,j)^2;
226c2(i,j) = 1/(dx(i,j)*dx(i+1,j));
227c3(i,j) = mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)*dx(i,j+1)/(dy(i,j)^2*dx(i,j));
228c4(i,j) = mask2(i,j-1)*mask2(i,j)*mask2(i+1,j-1)*mask2(i+1,j)/(dy(i,j)*dy(i,j-1));
229c5(i,j) = dy(i-1,j)/(dx(i,j)^2*dy(i,j));
230c6(i,j) = -1/(dx(i,j)*dy(i,j))+mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)/(dx(i,j)*dy(i,j));
231c7(i,j) = mask2(i,j-1)*mask2(i,j)*mask2(i+1,j-1)*mask2(i+1,j)*dy(i+1,j-1)/(dy(i,j)*dx(i,j-1)*dy(i,j-1))-dx(i+1,j-1)/(dx(i,j)*dx(i+1,j)*dy(i+1,j));
232c8(i,j) = -mask2(i,j-1)*mask2(i,j)*mask2(i+1,j-1)*mask2(i+1,j)/(dy(i,j)*dx(i,j-1))+dx(i,j-1)/(dx(i,j)^2*dy(i,j));
233c9(i,j) = -mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)*dy(i+1,j)/(dy(i,j)^2*dx(i,j))+1/(dx(i,j)*dy(i+1,j));
234    end
235end
236c1=c1.*umask; c2=c2.*umask; c3=c3.*umask;
237c4=c4.*umask; c5=c5.*umask; c6=c6.*umask;
238c7=c7.*umask; c8=c8.*umask; c9=c9.*umask;
239
240
241
242
243
244
245
246%vcoefs
247d1=zeros(Nlong,Nlat); d2=d1; d3=d1; d4=d1; d5=d1; d6=d1; d7=d1; d8=d1; d9=d1;
248for i=2:Nlong-1
249    for j=2:Nlat-1;
250d1(i,j) = -mask2(i-1,j)*mask2(i-1,j+1)*mask2(i,j)*mask2(i,j+1)*dy(i,j)/(dx(i,j)*dx(i-1,j)*dy(i-1,j))-mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)/dx(i,j)^2-1/dy(i,j)^2-dx(i,j)/(dy(i,j)*dx(i,j+1)*dy(i,j+1));
251d2(i,j) = mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)*dy(i+1,j)/(dx(i,j)^2*dy(i,j));
252d3(i,j) = 1/(dy(i,j)*dy(i,j+1));
253d4(i,j) = dx(i,j-1)/(dy(i,j)^2*dx(i,j));
254d5(i,j) = mask2(i-1,j)*mask2(i-1,j+1)*mask2(i,j)*mask2(i,j+1)/(dx(i,j)*dx(i-1,j));
255d6(i,j) = -1/(dx(i,j)*dy(i,j))+mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)/(dx(i,j)*dy(i,j));
256d7(i,j) = -mask2(i,j)*mask2(i,j+1)*mask2(i+1,j)*mask2(i+1,j+1)*dx(i,j+1)/(dx(i,j)^2*dy(i,j))+1/(dy(i,j)*dx(i,j+1));
257d8(i,j) = dy(i-1,j)/(dy(i,j)^2*dx(i,j))-mask2(i-1,j)*mask2(i-1,j+1)*mask2(i,j)*mask2(i,j+1)/(dx(i,j)*dy(i-1,j));
258d9(i,j) = -dy(i-1,j+1)/(dy(i,j)*dx(i,j+1)*dy(i,j+1))+mask2(i-1,j)*mask2(i-1,j+1)*mask2(i,j)*mask2(i,j+1)*dx(i-1,j+1)/(dx(i,j)*dx(i-1,j)*dy(i-1,j));
259    end
260end
261
262
263d1=d1.*vmask; d2=d2.*vmask; d3=d3.*vmask;
264d4=d4.*vmask; d5=d5.*vmask; d6=d6.*vmask;
265d7=d7.*vmask; d8=d8.*vmask; d9=d9.*vmask;
266
267
268%save into yao format
269%get dt value and order of filter
270%put mask as ones in yao put a fake mask
271%load the fileu /v as the norm
272
273
274%%%%%%%%%%%%%%%%%SAVE THESE COEF INTO YAO FORMAT%%%%%%%%%%%%%%%%%%
275
276%1------------------PUT U in YAO format-------------------------------------%
277%1-CC
278c1_yao=reshape(c1.', Nlong*Nlat,1);
279c2_yao=reshape(c2.', Nlong*Nlat,1);
280c3_yao=reshape(c3.', Nlong*Nlat,1);
281c4_yao=reshape(c4.', Nlong*Nlat,1);
282c5_yao=reshape(c5.', Nlong*Nlat,1);
283c6_yao=reshape(c6.', Nlong*Nlat,1);
284c7_yao=reshape(c7.', Nlong*Nlat,1);
285c8_yao=reshape(c8.', Nlong*Nlat,1);
286c9_yao=reshape(c9.', Nlong*Nlat,1);
287
288%create the i j indices
289cd1=[]; for k=1:Nlong; cd1=[cd1; k*ones(Nlat,1)]; end
290cd2=repmat([1:1:Nlat].',Nlong,1);
291
292Yao_CC=[ ones(Nlong*Nlat,1)  cd1 cd2 c1_yao;...
293    2*ones(Nlong*Nlat,1)  cd1 cd2 c2_yao;...
294    3*ones(Nlong*Nlat,1)  cd1 cd2 c3_yao; ...
295    4*ones(Nlong*Nlat,1)  cd1 cd2 c4_yao;...
296    5*ones(Nlong*Nlat,1)  cd1 cd2 c5_yao;...
297    6*ones(Nlong*Nlat,1)  cd1 cd2 c6_yao;...
298    7*ones(Nlong*Nlat,1)  cd1 cd2 c7_yao;...
299    8*ones(Nlong*Nlat,1)  cd1 cd2 c8_yao;...
300    9*ones(Nlong*Nlat,1)  cd1 cd2 c9_yao;];
301   
302%2-DD
303d1_yao=reshape(d1.', Nlong*Nlat,1);
304d2_yao=reshape(d2.', Nlong*Nlat,1);
305d3_yao=reshape(d3.', Nlong*Nlat,1);
306d4_yao=reshape(d4.', Nlong*Nlat,1);
307d5_yao=reshape(d5.', Nlong*Nlat,1);
308d6_yao=reshape(d6.', Nlong*Nlat,1);
309d7_yao=reshape(d7.', Nlong*Nlat,1);
310d8_yao=reshape(d8.', Nlong*Nlat,1);
311d9_yao=reshape(d9.', Nlong*Nlat,1);
312
313
314
315Yao_DD=[ ones(Nlong*Nlat,1)  cd1 cd2 d1_yao;...
316    2*ones(Nlong*Nlat,1)  cd1 cd2 d2_yao;...
317    3*ones(Nlong*Nlat,1)  cd1 cd2 d3_yao; ...
318    4*ones(Nlong*Nlat,1)  cd1 cd2 d4_yao;...
319    5*ones(Nlong*Nlat,1)  cd1 cd2 d5_yao;...
320    6*ones(Nlong*Nlat,1)  cd1 cd2 d6_yao;...
321    7*ones(Nlong*Nlat,1)  cd1 cd2 d7_yao;...
322    8*ones(Nlong*Nlat,1)  cd1 cd2 d8_yao;...
323    9*ones(Nlong*Nlat,1)  cd1 cd2 d9_yao;];
324
325%3- Fake mask of ones, to remove from yao later!
326maskk=ones(Nlat,Nlong);
327mask_yao=reshape(maskk, Nlong*Nlat,1);
328Yao_mask=[ ones(Nlong*Nlat,1)  cd1 cd2 mask_yao];
329
330%4- The normalization after finishing the filter
331norm_u=fileu.';
332nu_yao=reshape(norm_u, Nlong*Nlat,1);
333Yao_nu=[ ones(Nlong*Nlat,1)  cd1 cd2 nu_yao];
334
335norm_v=filev.';
336nv_yao=reshape(norm_v, Nlong*Nlat,1);
337Yao_nv=[ ones(Nlong*Nlat,1)  cd1 cd2 nv_yao];
338
339%2-Save
340 
341 if (write_coefs==1)
342 
343dlmwrite('../obs_float/CC.dat',Yao_CC,'\t');
344dlmwrite('../obs_float/DD.dat',Yao_DD,'\t');
345dlmwrite('../obs_float/Mask.dat',Yao_mask,'\t');
346dlmwrite('../obs_float/NU.dat',Yao_nu,'\t');
347dlmwrite('../obs_float/NV.dat',Yao_nv,'\t');
348
349 end;
350 
351%%%%%TO load these in YAO, you need to add these lines, in the ini file (if
352%there is a filter option_
353%loadstate cfil 0 ij 0 A 1 ../obs_float/CC.dat D
354%loadstate dfil 0 ij 0 A 1 ../obs_float/DD.dat D
355%loadstate mask 0 ij 0 A 1 ../obs_float/Mask.dat D
356%loadstate u_norm 0 ij 0 A 1 ../obs_float/NU.dat D
357%loadstate v_norm 0 ij 0 A 1 ../obs_float/NV.dat D
358
359fprintf ('Please change tha values given in the YAO code.\n')
360
361
362
363end
364
Note: See TracBrowser for help on using the repository browser.