Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increase code coverage #144

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function GridLoad(γ=GridSpec(); ID=:default, option=:minimal)
end

[Γ[ii]=GridLoadVar(ii,gr) for ii in list_n]
option=="full"||option=="light" ? GridAddWS!(Γ) : nothing
op=="full"||op=="light" ? GridAddWS!(Γ) : nothing
return Dict_to_NamedTuple(Γ)
end

Expand Down
26 changes: 12 additions & 14 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -483,20 +483,18 @@ function UVtoUEVN(u::MeshArray,v::MeshArray,G::NamedTuple)
return uC.*G.AngleCS-vC.*G.AngleSN, uC.*G.AngleSN+vC.*G.AngleCS
end

function UVtoSpeed!(u::MeshArray,v::MeshArray,G::NamedTuple,dD)
(u,v)=exch_UV(u,v)
function UVtoSpeed!(uC::MeshArray,vC::MeshArray,G::NamedTuple,dD)
(u,v)=exch_UV(uC,vC)
for iF=1:u.grid.nFaces
for i in 1:size(vC[iF],1)
for i in 1:size(vC[iF],1)
for j in 1:size(vC[iF],2)
u0=nanmean(u[iF][i,j],u[iF][i+1,j])
v0=nanmean(v[iF][i,j],v[iF][i,j+1])
u1=u0*G.AngleCS[iF][i,j]-v0*G.AngleSN[iF][i,j]
v1=u0*G.AngleSN[iF][i,j]+v0*G.AngleCS[iF][i,j]
dD[iF][i,j]=sqrt(u1^2 + v1^2)
#uC[iF][i,j]=u0*G.AngleCS[iF][i,j]-v0*G.AngleSN[iF][i,j]
#vC[iF][i,j]=u0*G.AngleSN[iF][i,j]+v0*G.AngleCS[iF][i,j]
end
u0=nanmean(u[iF][i,j],u[iF][i+1,j])
v0=nanmean(v[iF][i,j],v[iF][i,j+1])
u1=u0*G.AngleCS[iF][i,j]-v0*G.AngleSN[iF][i,j]
v1=u0*G.AngleSN[iF][i,j]+v0*G.AngleCS[iF][i,j]
dD[iF][i,j]=sqrt(u1^2 + v1^2)
end
end
end
end

Expand Down Expand Up @@ -553,8 +551,8 @@ function calc_bolus(GM_PsiX,GM_PsiY, Γ)
bolusU.f[:,k].=(GM_PsiX.f[:,k+1].-GM_PsiX.f[:,k])/Γ.DRF[k];
bolusV.f[:,k].=(GM_PsiY.f[:,k+1].-GM_PsiY.f[:,k])/Γ.DRF[k];
end;
bolusU.f[:, nr] .= 0. -GM_PsiX.f[:,nr]./Γ.DRF[nr];
bolusV.f[:, nr] .= 0. -GM_PsiY.f[:,nr]./Γ.DRF[nr];
bolusU.f[:, nr] .= 0*GM_PsiX.f[:,nr] .-GM_PsiX.f[:,nr]./Γ.DRF[nr];
bolusV.f[:, nr] .= 0*GM_PsiY.f[:,nr] .-GM_PsiY.f[:,nr]./Γ.DRF[nr];

bolusU=bolusU.*mskW;
bolusV=bolusV.*mskS;
Expand All @@ -568,7 +566,7 @@ function calc_bolus(GM_PsiX,GM_PsiY, Γ)
for k in 1:nr
(tmpU,tmpV)=exch_UV(tmp_x[:, k],tmp_y[:, k])
for a=1:tmpU.grid.nFaces
(s1,s2)=size(tmpU.f[a])
(s1,s2)=size(tmp_x.f[a])
tmpU1=view(tmpU.f[a],1:s1,1:s2)
tmpU2=view(tmpU.f[a],2:s1+1,1:s2)
tmpV1=view(tmpV.f[a],1:s1,1:s2)
Expand Down
4 changes: 3 additions & 1 deletion src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ MeshArray=gcmarray

## Methods that apply to all AbstractMeshArray types

import Base: maximum, minimum, sum, fill, fill!
import Base: maximum, minimum, extrema, sum, fill, fill!

extrema(a::AbstractMeshArray) = [minimum(a) maximum(a)]

function maximum(a::AbstractMeshArray)
c=-Inf;
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Proj = "c94c279d-25a6-4763-9509-64d165bea63e"
Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
70 changes: 61 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Test, Documenter
using MeshArrays, DataDeps, CairoMakie, JLD2, Shapefile, Proj
using Test, Documenter, Suppressor, MeshArrays, CairoMakie
import DataDeps, JLD2, Shapefile, GeoJSON, Proj

MeshArrays.GRID_LL360_download()
MeshArrays.GRID_LLC90_download()
Expand Down Expand Up @@ -54,12 +54,14 @@ end
γ=GridSpec("LatLonCap",MeshArrays.GRID_LLC90)
Tx=γ.read(MeshArrays.GRID_LLC90*"TrspX.bin",MeshArray(γ,Float32))
Ty=γ.read(MeshArrays.GRID_LLC90*"TrspY.bin",MeshArray(γ,Float32))
Γ=GridLoad(γ;option="light")

Γ=GridLoad(γ;option=:full)
plot(Γ.XC)

hFacC=GridLoadVar("hFacC",γ)
μ=land_mask(hFacC[:,1])

lons=[-68 -63]; lats=[-54 -66]; name="Drake Passage"
Trsct=Transect(name,lons,lats,Γ,segment=:long,format=:NamedTuple)
Trsct=Transect(name,lons,lats,Γ)

#Various vector operations
Expand All @@ -71,13 +73,32 @@ end
UVtoTransport(U,V,Γ)
UVtoUEVN(U[:,1],V[:,1],Γ)
curl(U[:,1],V[:,1], merge(Γ,(hFacW=hFacW,hFacS=hFacS,RAZ=RAZ,)) )
dD=zeros(γ)
MeshArrays.UVtoSpeed!(U[:,1],V[:,1],Γ,dD)

#Meridional transport integral
uv=Dict("U"=>Tx,"V"=>Ty,"dimensions"=>["x","y"])
L=-85.0:5.0:85.0; LC=LatitudeCircles(L,Γ,format=:gridpath)
T=Array{Float64,1}(undef,length(LC))
[T[i]=1e-6*ThroughFlow(uv,LC[i],Γ) for i=1:length(LC)]

plot(LC)
plot(LC[1])

x=zeros(γ)
fill!(x,1.0)
y=fill(-1.0,γ)
extrema(y)
@test minimum(y)<minimum(x)

y*ones(3,2)
ones(γ)
ones(y)
zeros(y)

GM_PsiX=read(randn(90,1170,50),Γ.hFacW)
GM_PsiY=read(randn(90,1170,50),Γ.hFacS)
bolusU, bolusV, bolusW=MeshArrays.calc_bolus(GM_PsiX,GM_PsiY, Γ)

#See: OceanTransports/helper_functions.jl
#u,v,uC,vC=rotate_uv(uv,Γ);

Expand Down Expand Up @@ -115,7 +136,7 @@ end
MeshArrays.fsize(tmp.f,2)
size(tmp)
size(tmp,3)
show(tmp)
@suppress show(tmp)

x=tmp[1:10,1,1:2]; y=x[2]; x[3]=1.0
view(x,1:3,:,1)
Expand All @@ -136,11 +157,11 @@ end
size(tmp1)
tmp1[2]
view(tmp1,:)
show(tmp1)
@suppress show(tmp1)
similar(tmp1)
#tmp[tmp1]

show(tmp)
@suppress show(tmp)
MeshArrays.getindexetc(tmp,2)
MeshArray(γ,tmp.f,meta=tmp.meta)
MeshArray(γ,meta=tmp.meta)
Expand Down Expand Up @@ -197,8 +218,10 @@ end
proj=Proj.Transformation(MA_preset=2,lon0=lon0)
Dint=reshape(Interpolate(D,λ.f,λ.i,λ.j,λ.w),size(λ.lon))

fil=demo.download_polygons("ne_110m_admin_0_countries.shp")
# fil=demo.download_polygons("countries.geojson")
# pol=MeshArrays.read_polygons(fil)

fil=demo.download_polygons("ne_110m_admin_0_countries.shp")
pol=MeshArrays.read_polygons(fil)

f = Figure()
Expand All @@ -210,6 +233,35 @@ end
MeshArrays.grid_lines!(pr_ax;color=:lightgreen,linewidth=0.5)
f

meta=(colorrange=(0.0,6000.0),cmap=:BrBG_10,ttl="Ocean Depth (m)",lon0=lon0)
data=(lon=λ.lon,lat=λ.lat,var=Dint,meta=meta) #,polygons=pol)
plot_examples(:projmap,data,lon0,proj)

end

@testset "nanmath" begin
x=[NaN 1 2]
nansum(x)
nansum(x,1)
nanmax(x,2)
nanmin(x,2)

nanmean(NaN,1)
nanmean(1,NaN)
nanmean(2,1)
nanmean(NaN,NaN)
end

@testset "plotting" begin
lon,lat,earth_img=demo.get_basemap()
plot_examples(:basemap,lon,lat,earth_img)

pol_file=demo.download_polygons("ne_110m_admin_0_countries.shp")
pol=MeshArrays.read_polygons(pol_file)

lon0=-160
proj=Proj.Transformation(MA_preset=2,lon0=lon0)
plot_examples(:baseproj,proj,lon0,pol=pol)
end

@testset "doctests" begin
Expand Down
Loading