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