Skip to content

Commit 02496bb

Browse files
authored
Merge pull request #143 from gaelforget/v0p3p12_e
methods improvements
2 parents eeb335e + 3006188 commit 02496bb

File tree

5 files changed

+186
-90
lines changed

5 files changed

+186
-90
lines changed

ext/MeshArraysMakieExt.jl

+8
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@ function scatter!(pr_ax::PrAxis,lon,lat,kargs...; kwargs...)
739739
scatter!(pr_ax.ax,x,y,kargs...; kwargs...)
740740
end
741741

742+
plot(x::MeshArray; kwargs...) = begin
743+
if ndims(x) == 1
744+
heatmap(x; kwargs...)
745+
else
746+
heatmap(x[:,1]; kwargs...)
747+
end
748+
end
749+
742750
##
743751

744752
function plot(x::Union{gridpath,Vector{gridpath}})

src/Grids.jl

+68-44
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,39 @@ function simple_periodic_domain(np::Integer,nq=missing)
130130
return UnitGrid(γ)
131131
end
132132

133-
## GridSpec function with default GridName argument:
133+
## GridSpec function with category argument:
134134

135-
GridSpec() = GridSpec("PeriodicDomain","./")
135+
"""
136+
GridSpec(category="PeriodicDomain",path=tempname(); ID=:unknown)
136137
137-
## GridSpec function with GridName argument:
138+
- Select one of the pre-defined grids either by ID (keyword) or by category.
139+
- Return the corresponding `gcmgrid` specification, including the path where grid files can be accessed (`path`).
138140
139-
"""
140-
GridSpec(GridName,GridParentDir="./")
141+
1. selection by `ID`
141142
142-
Select one of the pre-defined grids (by `GridName`) and return
143-
the corresponding `gcmgrid` -- a global grid specification
144-
which contains the grid files location (`GridParentDir`).
145-
143+
- `:LLC90`
144+
- `:CS32`
145+
- `:onedegree`
146+
- `:default`
147+
148+
Example:
149+
150+
```
151+
using MeshArrays
152+
g = GridSpec(ID=:LLC90)
153+
```
154+
155+
note : the path to these fully supported grids are handled internally in `MeshArrays.jl`.
146156
147-
Possible choices for `GridName`:
157+
2. by `category` and `path`
148158
149159
- `"PeriodicDomain"`
150160
- `"PeriodicChannel"`
151161
- `"CubeSphere"`
152162
- `"LatLonCap"``
153163
164+
Examples:
165+
154166
```jldoctest; output = false
155167
using MeshArrays
156168
g = GridSpec()
@@ -164,51 +176,62 @@ isa(g,gcmgrid)
164176
true
165177
```
166178
"""
167-
function GridSpec(GridName,GridParentDir="./")
179+
function GridSpec(category="PeriodicDomain",path=tempname(); ID=:unknown)
168180

169-
grDir=GridParentDir
170-
if GridName=="LatLonCap"
181+
if category=="LatLonCap"
171182
nFaces=5
172183
grTopo="LatLonCap"
173184
ioSize=[90 1170]
174185
facesSize=[(90, 270), (90, 270), (90, 90), (270, 90), (270, 90)]
175186
ioPrec=Float64
176-
elseif GridName=="CubeSphere"
187+
elseif category=="CubeSphere"
177188
nFaces=6
178189
grTopo="CubeSphere"
179190
ioSize=[32 192]
180191
facesSize=[(32, 32), (32, 32), (32, 32), (32, 32), (32, 32), (32, 32)]
181192
ioPrec=Float32
182-
elseif GridName=="PeriodicChannel"
193+
elseif category=="PeriodicChannel"
183194
nFaces=1
184195
grTopo="PeriodicChannel"
185196
ioSize=[360 160]
186197
facesSize=[(360, 160)]
187198
ioPrec=Float32
188-
elseif GridName=="PeriodicDomain"
199+
elseif category=="PeriodicDomain"
189200
nFaces=4
190201
grTopo="PeriodicDomain"
191202
ioSize=[80 42]
192203
facesSize=[(40, 21), (40, 21), (40, 21), (40, 21)]
193204
ioPrec=Float32
194205
else
195-
error("unknown GridName case")
206+
error("unknown category case")
196207
end
197208

198-
return gcmgrid(grDir,grTopo,nFaces,facesSize, ioSize, ioPrec, read, write)
209+
if ID==:unknown
210+
gcmgrid(path,grTopo,nFaces,facesSize, ioSize, ioPrec, read, write)
211+
elseif ID==:LLC90
212+
GridSpec("LatLonCap",MeshArrays.GRID_LLC90)
213+
elseif ID==:CS32
214+
GridSpec("CubeSphere",MeshArrays.GRID_CS32)
215+
elseif ID==:onedegree
216+
GridSpec("PeriodicChannel",MeshArrays.GRID_LL360)
217+
elseif ID==:default
218+
GridSpec()
219+
else
220+
error("unknwown grid")
221+
end
199222

200223
end
201224

202225
## GridLoad function
203226

204227
"""
205-
GridLoad(γ::gcmgrid;option="minimal")
228+
GridLoad(γ=GridSpec(); ID=:default, option=:minimal)
206229
207-
Return a `NamedTuple` of grid variables read from files located in `γ.path` (see `?GridSpec`).
208-
209-
By default, option="minimal" means that only grid cell center positions (XC, YC) are loaded.
210-
211-
The "full" option provides a complete set of grid variables.
230+
- Return a `NamedTuple` of grid variables read from files located in `γ.path` (see `?GridSpec`).
231+
- option :
232+
- (default) option=:minimal means that only grid cell center positions (XC, YC) are loaded.
233+
- option=:full provides a complete set of 2D grid variables.
234+
- option=:full provides a complete set of 2D & 3d grid variables.
212235
213236
Based on the MITgcm naming convention, grid variables are:
214237
@@ -222,11 +245,7 @@ https://mitgcm.readthedocs.io/en/latest/algorithm/algorithm.html#spatial-discret
222245
223246
```jldoctest; output = false
224247
using MeshArrays
225-
226-
γ = GridSpec("CubeSphere",MeshArrays.GRID_CS32)
227-
#γ = GridSpec("LatLonCap",MeshArrays.GRID_LLC90)
228-
#γ = GridSpec("PeriodicChannel",MeshArrays.GRID_LL360)
229-
248+
γ = GridSpec(ID=:LLC90)
230249
Γ = GridLoad(γ;option="full")
231250
232251
isa(Γ.XC,MeshArray)
@@ -236,33 +255,38 @@ isa(Γ.XC,MeshArray)
236255
true
237256
```
238257
"""
239-
function GridLoad::gcmgrid;option="minimal")
258+
function GridLoad=GridSpec(); ID=:default, option=:minimal)
259+
260+
gr = (ID!==:default ? GridSpec(ID=ID) : γ)
240261

241-
γ.path==GRID_CS32 ? GRID_CS32_download() : nothing
242-
γ.path==GRID_LL360 ? GRID_LL360_download() : nothing
243-
γ.path==GRID_LLC90 ? GRID_LLC90_download() : nothing
262+
gr.path==GRID_CS32 ? GRID_CS32_download() : nothing
263+
gr.path==GRID_LL360 ? GRID_LL360_download() : nothing
264+
gr.path==GRID_LLC90 ? GRID_LLC90_download() : nothing
244265

245266
Γ=Dict()
246267

247-
if option=="full"
248-
list_n=("XC","XG","YC","YG","RAC","RAW","RAS","RAZ","DXC","DXG","DYC","DYG","Depth");
249-
if (!isempty(filter(x -> occursin("AngleCS",x), readdir.path))))
268+
op=string(option)
269+
if op=="full"
270+
list_n=("XC","XG","YC","YG","RAC","RAW","RAS","RAZ","DXC","DXG","DYC","DYG","Depth")
271+
if (!isempty(filter(x -> occursin("AngleCS",x), readdir(gr.path))))
250272
list_n=(list_n...,"AngleCS","AngleSN");
251273
end
252274
list_n=(list_n...,"DRC","DRF","RC","RF")
253-
list_n=(list_n...,"hFacC","hFacS","hFacW");
254-
elseif option=="light"
255-
list_n=("XC","XG","YC","YG","RAC","DXC","DXG","DYC","DYG","Depth");
256-
if (!isempty(filter(x -> occursin("AngleCS",x), readdir(γ.path))))
257-
list_n=(list_n...,"AngleCS","AngleSN");
275+
list_n=(list_n...,"hFacC","hFacS","hFacW")
276+
elseif op=="light"
277+
list_n=("XC","XG","YC","YG","RAC","DXC","DXG","DYC","DYG","Depth")
278+
if (!isempty(filter(x -> occursin("AngleCS",x), readdir(gr.path))))
279+
list_n=(list_n...,"AngleCS","AngleSN")
258280
end
259281
list_n=(list_n...,"DRC","DRF","RC","RF")
282+
elseif op=="minimal"||op=="minimum"
283+
list_n=("XC","YC")
260284
else
261-
list_n=("XC","YC");
285+
error("unknown option")
262286
end
263287

264-
[Γ[ii]=GridLoadVar(ii,γ) for ii in list_n]
265-
option=="full" ? GridAddWS!(Γ) : nothing
288+
[Γ[ii]=GridLoadVar(ii,gr) for ii in list_n]
289+
option=="full"||option=="light" ? GridAddWS!(Γ) : nothing
266290
return Dict_to_NamedTuple(Γ)
267291
end
268292

src/Operations.jl

+33-14
Original file line numberDiff line numberDiff line change
@@ -331,26 +331,45 @@ end
331331
## LatitudeCircles function
332332

333333
"""
334-
LatitudeCircles(LatValues,Γ::NamedTuple; format=:gridpath)
334+
LatitudeCircles(LatValues,Γ::NamedTuple; format=:gridpath, range=(0.0,360.0))
335335
336-
Compute integration paths that follow latitude circles
336+
Compute integration paths that follow latitude circles, within the specified longitude `range`.
337337
"""
338-
function LatitudeCircles(LatValues,Γ::NamedTuple; format=:gridpath)
338+
function LatitudeCircles(LatValues,Γ::NamedTuple;
339+
format=:gridpath, range=(0.0,360.0))
339340
T=(format==:NamedTuple ? NamedTuple : gridpath)
340341
LatitudeCircles=Array{T}(undef,length(LatValues))
341342
for j=1:length(LatValues)
342-
mskCint=1*.YC .>= LatValues[j])
343-
mskC,mskW,mskS=edge_mask(mskCint)
344-
LatitudeCircles[j]=
345-
if format==:NamedTuple
346-
(lat=LatValues[j],tabC=MskToTab(mskC),
347-
tabW=MskToTab(mskW),tabS=MskToTab(mskS))
348-
else
349-
gridpath(name="Parallel $(LatValues[j])", grid=Γ,
350-
C=MskToTab(mskC),W=MskToTab(mskW),S=MskToTab(mskS))
351-
end
343+
LatitudeCircles[j]=LatitudeCircle(LatValues[j],Γ;
344+
format=format,range=range)
352345
end
353-
return LatitudeCircles
346+
(length(LatValues)==1 ? LatitudeCircles[1] : LatitudeCircles)
347+
end
348+
349+
function LatitudeCircle(lat,Γ::NamedTuple;
350+
format=:gridpath, range=(0.0,360.0))
351+
mskCint=1*.YC .>= lat)
352+
mskC,mskW,mskS=edge_mask(mskCint)
353+
restrict_longitudes!(mskC,Γ.XC,range=range)
354+
restrict_longitudes!(mskS,Γ.XS,range=range)
355+
restrict_longitudes!(mskW,Γ.XW,range=range)
356+
LC=if format==:NamedTuple
357+
(lat=LatValues[j],tabC=MskToTab(mskC),
358+
tabW=MskToTab(mskW),tabS=MskToTab(mskS))
359+
else
360+
gridpath(name="Parallel $lat", grid=Γ,
361+
C=MskToTab(mskC),W=MskToTab(mskW),S=MskToTab(mskS))
362+
end
363+
364+
end
365+
366+
is_in_lon_range(x,range)=(range[2].-range[1]>=360)||
367+
(mod(x-range[1],360).<mod(range[2].-range[1],360))
368+
369+
function restrict_longitudes!(x::MeshArray,lon::MeshArray;range=(0.0,360.0))
370+
for f in 1:x.grid.nFaces
371+
x[f].=x[f].*is_in_lon_range.(lon[f],Ref(range))
372+
end
354373
end
355374

356375
function MskToTab(msk::MeshArray)

0 commit comments

Comments
 (0)