1 | function h = smoothgrid(h,maskr,hmin,hmax_coast,rmax,n_filter_deep_topo,n_filter_final) |
---|
2 | % |
---|
3 | % Smooth the topography to get a maximum r factor = rmax |
---|
4 | % |
---|
5 | % n_filter_deep_topo: |
---|
6 | % Number of pass of a selective filter to reduce the isolated |
---|
7 | % seamounts on the deep ocean. |
---|
8 | % |
---|
9 | % n_filter_final: |
---|
10 | % Number of pass of a single hanning filter at the end of the |
---|
11 | % procedure to ensure that there is no 2DX noise in the |
---|
12 | % topography. |
---|
13 | % |
---|
14 | % Further Information: |
---|
15 | % http://www.brest.ird.fr/Roms_tools/ |
---|
16 | % |
---|
17 | % This file is part of ROMSTOOLS |
---|
18 | % |
---|
19 | % ROMSTOOLS is free software; you can redistribute it and/or modify |
---|
20 | % it under the terms of the GNU General Public License as published |
---|
21 | % by the Free Software Foundation; either version 2 of the License, |
---|
22 | % or (at your option) any later version. |
---|
23 | % |
---|
24 | % ROMSTOOLS is distributed in the hope that it will be useful, but |
---|
25 | % WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
26 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
27 | % GNU General Public License for more details. |
---|
28 | % |
---|
29 | % You should have received a copy of the GNU General Public License |
---|
30 | % along with this program; if not, write to the Free Software |
---|
31 | % Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
32 | % MA 02111-1307 USA |
---|
33 | % |
---|
34 | % Copyright (c) 2005-2006 by Pierrick Penven |
---|
35 | % e-mail:Pierrick.Penven@ird.fr |
---|
36 | % |
---|
37 | % Contributions of A. Shchepetkin (UCLA), P. Marchesiello (IRD) |
---|
38 | % and X. Capet (UCLA) |
---|
39 | % |
---|
40 | % Updated Aug-2006 by Pierrick Penven |
---|
41 | % |
---|
42 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
43 | |
---|
44 | % |
---|
45 | % Cut the topography |
---|
46 | % |
---|
47 | h(h<hmin)=hmin; |
---|
48 | h(h>5000)=5000; |
---|
49 | % |
---|
50 | % |
---|
51 | % 1: Deep Ocean Filter |
---|
52 | % |
---|
53 | if n_filter_deep_topo>=1 |
---|
54 | disp(' Apply a filter on the Deep Ocean to remove the isolated seamounts :') |
---|
55 | disp([' ',num2str(n_filter_deep_topo),' pass of a selective filter.']) |
---|
56 | % |
---|
57 | % Build a smoothing coefficient that is a linear function |
---|
58 | % of a smooth topography. |
---|
59 | % |
---|
60 | coef=h; |
---|
61 | for i=1:8 |
---|
62 | coef=hanning_smoother(coef); % coef is a smoothed bathy |
---|
63 | end |
---|
64 | coef=0.125*(coef./max(max(coef))); % rescale the smoothed bathy |
---|
65 | % |
---|
66 | for i=1:n_filter_deep_topo; |
---|
67 | h=hanning_smoother_coef2d(h,coef); % smooth with avariable coef |
---|
68 | h(maskr==0 & h>hmax_coast)=hmax_coast; |
---|
69 | end |
---|
70 | end |
---|
71 | % |
---|
72 | % Apply a selective filter on log(h) to reduce grad(h)/h. |
---|
73 | % |
---|
74 | disp(' Apply a selective filter on log(h) to reduce grad(h)/h :') |
---|
75 | h=rotfilter(h,maskr,hmax_coast,rmax); |
---|
76 | % |
---|
77 | % Smooth the topography again to prevent 2D noise |
---|
78 | % |
---|
79 | if n_filter_final>1 |
---|
80 | disp(' Smooth the topography a last time to prevent 2DX noise:') |
---|
81 | disp([' ',num2str(n_filter_final),' pass of a hanning smoother.']) |
---|
82 | for i=1:n_filter_final |
---|
83 | h=hanning_smoother(h); |
---|
84 | h(maskr==0 & h>hmax_coast)=hmax_coast; |
---|
85 | end |
---|
86 | end |
---|
87 | % |
---|
88 | h(h<hmin)=hmin; |
---|
89 | % |
---|
90 | return |
---|
91 | % |
---|
92 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
93 | function h=rotfilter(h,maskr,hmax_coast,rmax) |
---|
94 | % |
---|
95 | % Apply a selective filter on log(h) to reduce grad(h)/h. |
---|
96 | % |
---|
97 | [M,L]=size(h); |
---|
98 | Mm=M-1; |
---|
99 | Mmm=M-2; |
---|
100 | Lm=L-1; |
---|
101 | Lmm=L-2; |
---|
102 | cff=0.8; |
---|
103 | nu=3/16; |
---|
104 | [rx,ry]=rfact(h); |
---|
105 | r=max(max(max(rx)),max(max(ry))); |
---|
106 | h=log(h); |
---|
107 | hmax_coast=log(hmax_coast); |
---|
108 | i=0; |
---|
109 | while r>rmax |
---|
110 | i=i+1; |
---|
111 | cx=double(rx>cff*rmax); |
---|
112 | cx=hanning_smoother(cx); |
---|
113 | cy=double(ry>cff*rmax); |
---|
114 | cy=hanning_smoother(cy); |
---|
115 | fx=cx.*FX(h); |
---|
116 | fy=cy.*FY(h); |
---|
117 | h(2:Mm,2:Lm)=h(2:Mm,2:Lm)+nu*... |
---|
118 | ((fx(2:Mm,2:Lm)-fx(2:Mm,1:Lmm))+... |
---|
119 | (fy(2:Mm,2:Lm)-fy(1:Mmm,2:Lm))); |
---|
120 | h(1,:)=h(2,:); |
---|
121 | h(M,:)=h(Mm,:); |
---|
122 | h(:,1)=h(:,2); |
---|
123 | h(:,L)=h(:,Lm); |
---|
124 | h(maskr==0 & h>hmax_coast)=hmax_coast; |
---|
125 | [rx,ry]=rfact(exp(h)); |
---|
126 | r=max(max(max(rx)),max(max(ry))); |
---|
127 | end |
---|
128 | |
---|
129 | disp([' ',num2str(i),' iterations - rmax = ',num2str(r)]) |
---|
130 | h=exp(h); |
---|
131 | |
---|
132 | return |
---|
133 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
134 | function [rx,ry]=rfact(h); |
---|
135 | [M,L]=size(h); |
---|
136 | Mm=M-1; |
---|
137 | Mmm=M-2; |
---|
138 | Lm=L-1; |
---|
139 | Lmm=L-2; |
---|
140 | rx=abs(h(1:M,2:L)-h(1:M,1:Lm))./(h(1:M,2:L)+h(1:M,1:Lm)); |
---|
141 | ry=abs(h(2:M,1:L)-h(1:Mm,1:L))./(h(2:M,1:L)+h(1:Mm,1:L)); |
---|
142 | |
---|
143 | return |
---|
144 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
145 | function h=hanning_smoother(h); |
---|
146 | [M,L]=size(h); |
---|
147 | Mm=M-1; |
---|
148 | Mmm=M-2; |
---|
149 | Lm=L-1; |
---|
150 | Lmm=L-2; |
---|
151 | |
---|
152 | h(2:Mm,2:Lm)=0.125*(h(1:Mmm,2:Lm)+h(3:M,2:Lm)+... |
---|
153 | h(2:Mm,1:Lmm)+h(2:Mm,3:L)+... |
---|
154 | 4*h(2:Mm,2:Lm)); |
---|
155 | |
---|
156 | h(1,:)=h(2,:); |
---|
157 | h(M,:)=h(Mm,:); |
---|
158 | h(:,1)=h(:,2); |
---|
159 | h(:,L)=h(:,Lm); |
---|
160 | |
---|
161 | return |
---|
162 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
163 | function h=hanning_smoother_coef2d(h,coef); |
---|
164 | [M,L]=size(h); |
---|
165 | Mm=M-1; |
---|
166 | Mmm=M-2; |
---|
167 | Lm=L-1; |
---|
168 | Lmm=L-2; |
---|
169 | h(2:Mm,2:Lm)=coef(2:Mm,2:Lm).*(h(1:Mmm,2:Lm)+h(3:M,2:Lm)+... |
---|
170 | h(2:Mm,1:Lmm)+h(2:Mm,3:L))... |
---|
171 | +(1-4.*coef(2:Mm,2:Lm)).*h(2:Mm,2:Lm); |
---|
172 | h(1,:)=h(2,:); |
---|
173 | h(M,:)=h(Mm,:); |
---|
174 | h(:,1)=h(:,2); |
---|
175 | h(:,L)=h(:,Lm); |
---|
176 | |
---|
177 | return |
---|
178 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
179 | function fx=FX(h); |
---|
180 | [M,L]=size(h); |
---|
181 | Mm=M-1; |
---|
182 | Mmm=M-2; |
---|
183 | Lm=L-1; |
---|
184 | Lmm=L-2; |
---|
185 | |
---|
186 | fx(2:Mm,:)=(h(2:Mm,2:L)-h(2:Mm,1:Lm))*5/6 +... |
---|
187 | (h(1:Mmm,2:L)-h(1:Mmm,1:Lm)+h(3:M,2:L)-h(3:M,1:Lm))/12; |
---|
188 | |
---|
189 | fx(1,:)=fx(2,:); |
---|
190 | fx(M,:)=fx(Mm,:); |
---|
191 | |
---|
192 | return |
---|
193 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
194 | function fy=FY(h); |
---|
195 | [M,L]=size(h); |
---|
196 | Mm=M-1; |
---|
197 | Mmm=M-2; |
---|
198 | Lm=L-1; |
---|
199 | Lmm=L-2; |
---|
200 | |
---|
201 | fy(:,2:Lm)=(h(2:M,2:Lm)-h(1:Mm,2:Lm))*5/6 +... |
---|
202 | (h(2:M,1:Lmm)-h(1:Mm,1:Lmm)+h(2:M,3:L)-h(1:Mm,3:L))/12; |
---|
203 | |
---|
204 | fy(:,1)=fy(:,2); |
---|
205 | fy(:,L)=fy(:,Lm); |
---|
206 | |
---|
207 | return |
---|
208 | |
---|