Skip to content

Commit 90d9669

Browse files
authored
Merge pull request #179 from gaelforget/v0p3p21b
improve, generalize grid_factors
2 parents ab70769 + 4fbd94e commit 90d9669

File tree

3 files changed

+92
-33
lines changed

3 files changed

+92
-33
lines changed

docs/src/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ More :
3232

3333
```
3434
Grids_simple.periodic_domain
35+
Grids_simple.grid_factors
36+
Grids_simple.grid_add_z
3537
Grids_simple.UnitGrid
36-
Grids_simple.GridLoad_lonlatdep
37-
Grids_simple.GridLoad_lonlat
3838
```
3939

4040
More :

src/Grids_simple.jl

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,55 +140,106 @@ end
140140

141141
## Grid_latlon with specified vertical grid and land mask
142142

143+
rSphere = 6370.0*1000
144+
143145
"""
144146
GridLoad_lonlatdep(depth,mask)
147+
148+
!! deprecated, replaced with `grid_add_z` !!
145149
"""
146-
function GridLoad_lonlatdep(depth,mask)
147-
G=GridLoad_lonlat()
148-
g=G.XC.grid
149-
hFacC=read(mask,g)
150+
GridLoad_lonlatdep(depth,mask) = grid_add_z(GridLoad_lonlat(),depth,mask)
151+
152+
153+
"""
154+
grid_add_z(G,depth,mask)
155+
156+
```
157+
xy=Grids_simple.xy_IAP()
158+
gr=Grids_simple.grid_factors(xy)
159+
160+
dep=[10 100 1000]; msk=ones(gr[:XC].fSize[1]...,3)
161+
gr=Grids_simple.grid_add_z(gr,dep,msk)
162+
```
163+
"""
164+
grid_add_z(G,depth,mask) = begin
150165
RC=depth
151166
RF=[0;0.5*(RC[1:end-1]+RC[2:end]);RC[end]+0.5*(RC[end]-RC[end-1])]
152-
merge(G,(hFacC=hFacC,RC=RC,RF=RF,DRF=diff(RF)))
167+
g=G.XC.grid
168+
hFacC=read(mask,g)
169+
merge(G,(hFacC=hFacC,RC=RC,RF=RF,DRF=diff(RF)))
153170
end
154-
171+
155172
"""
156173
GridLoad_lonlat()
174+
175+
!! deprecated, replaced with `grid_factors` !!
157176
"""
158-
function GridLoad_lonlat()
159-
g=GridSpec("PeriodicChannel")
160-
g.fSize[1]=(360,180)
161-
g.ioSize.=[360 180]
162-
grid_factors(g)
163-
end
164-
165-
grid_factors(g)=begin
177+
GridLoad_lonlat(xy=xy_IAP())=grid_factors(xy)
178+
179+
xy_IAP()=begin
166180
xg=0.0:1:360
167181
yg=-90.0:1:90
168182
xc=0.5:1:359.5
169183
yc=-89.5:1:89.5
170-
rSphere = 6370.0*1000
171-
dxF = rSphere*deg2rad.(cosd.(yc)*1.0)
184+
(xc=xc,yc=yc,xg=xg,yg=yg)
185+
end
186+
187+
xy_Oscar()=begin
188+
xg=-0.125:0.25:359.875
189+
yg=-89.875:0.25:89.875
190+
xc=0.0:0.25:359.75
191+
yc=-89.75:0.25:89.75
192+
(xc=xc,yc=yc,xg=xg,yg=yg)
193+
end
194+
195+
xy_OISST()=begin
196+
xg=0.0:0.25:360.0
197+
yg=-90.0:0.25:90.0
198+
xc=0.125:0.25:359.875
199+
yc=-89.875:0.25:89.875
200+
(xc=xc,yc=yc,xg=xg,yg=yg)
201+
end
202+
203+
"""
204+
grid_factors(xy::NamedTuple)
205+
206+
```
207+
xy=Grids_simple.xy_OISST()
208+
gr=Grids_simple.grid_factors(xy)
209+
```
210+
"""
211+
grid_factors(xy::NamedTuple)=begin
212+
(; xc, yc, xg, yg) = xy
213+
214+
ni=length(xc)
215+
nj=length(yc)
216+
dx=diff(xg)[1]
217+
218+
g=GridSpec("PeriodicChannel")
219+
g.fSize[1]=(ni,nj)
220+
g.ioSize.=[ni nj]
221+
222+
dxF = rSphere*deg2rad.(cosd.(yc)*dx)
172223
dyF = rSphere*deg2rad.(diff(yg))
173-
dxG = rSphere*deg2rad.(cosd.(yg[1:end-1])*1.0)
224+
dxG = rSphere*deg2rad.(cosd.(yg[1:end-1])*dx)
174225
dyG = rSphere*deg2rad.(diff(yg))
175226
dxC = dxF
176227
dyC = rSphere*deg2rad.(diff(yg))
177228
x=sind.(yg[2:end])-sind.(yg[1:end-1])
178-
RAC = rSphere*rSphere*1.0*deg2rad.(abs.(x))
229+
RAC = rSphere*rSphere*dx*deg2rad.(abs.(x))
179230

180231
(
181-
XG=read(xg[1:end-1]*ones(1,180),g),
182-
YG=read(permutedims(yc*ones(1,360)),g),
183-
XC=read(xc*ones(1,180),g),
184-
YC=read(permutedims(yc*ones(1,360)),g),
185-
dxF=read(permutedims(dxF*ones(1,360)),g),
186-
dyF=read(permutedims(dyF*ones(1,360)),g),
187-
dxG=read(permutedims(dxG*ones(1,360)),g),
188-
dyG=read(permutedims(dyG*ones(1,360)),g),
189-
dxC=read(permutedims(dxC*ones(1,360)),g),
190-
dyC=read(permutedims(dyC*ones(1,360)),g),
191-
RAC=read(permutedims(RAC*ones(1,360)),g),
232+
XG=read(xg[1:end-1]*ones(1,nj),g),
233+
YG=read(permutedims(yc*ones(1,ni)),g),
234+
XC=read(xc*ones(1,nj),g),
235+
YC=read(permutedims(yc*ones(1,ni)),g),
236+
dxF=read(permutedims(dxF*ones(1,ni)),g),
237+
dyF=read(permutedims(dyF*ones(1,ni)),g),
238+
dxG=read(permutedims(dxG*ones(1,ni)),g),
239+
dyG=read(permutedims(dyG*ones(1,ni)),g),
240+
dxC=read(permutedims(dxC*ones(1,ni)),g),
241+
dyC=read(permutedims(dyC*ones(1,ni)),g),
242+
RAC=read(permutedims(RAC*ones(1,ni)),g),
192243
)
193244
end
194245

test/runtests.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,15 @@ end
242242
MeshArrays.write_tiles(tmp,Γ.XC)
243243
@test isfile(tmp)
244244

245-
G=Grids_simple.GridLoad_lonlatdep([10 100 1000],ones(360,180,3))
245+
xy=Grids_simple.xy_OISST()
246+
xy=Grids_simple.xy_Oscar()
247+
248+
xy=Grids_simple.xy_IAP()
249+
gr=Grids_simple.grid_factors(xy)
250+
dep=[10 100 1000]; msk=ones(gr[:XC].fSize[1]...,3)
251+
gr=Grids_simple.grid_add_z(gr,dep,msk)
252+
253+
@test haskey(gr,:hFacC)
246254
end
247255

248256
@testset "Plotting:" begin
@@ -278,7 +286,7 @@ end
278286
heatmap(D,interpolation=λ,title="ocean depth") #same but w title
279287

280288
lon0=-160
281-
proj=Proj.Transformation(MA_preset=2,lon0=lon0)
289+
proj=Proj.Transformation(MA_preset=2,lon0=lon0)
282290
Dint=reshape(Interpolate(D,λ.f,λ.i,λ.j,λ.w),size.lon))
283291

284292
Interpolate(D,λ)

0 commit comments

Comments
 (0)