Skip to content

Commit

Permalink
Field and Grid include dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
ggebbie committed Mar 7, 2024
1 parent f3b0dec commit 414ee14
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/TMI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export config, config_from_mat, config_from_nc,
wetlocation, iswet,
control2state, control2state!,
surfacecontrol2field, surfacecontrol2field!,
sparsedatamap, config2nc, gridprops,
sparsedatamap, config2nc, axislabels,
matrix_zyx2xyz,
surface_oxygensaturation, oxygen, location_obs,
zerosurfaceboundary,
Expand Down
4 changes: 2 additions & 2 deletions src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ function cartesianindex(file::String)
end

"""
function gridprops(file)
function axislabels(file)
Read and assemble the grid properties.
# Arguments
- `file`: TMI NetCDF file name
# Output
- `grid`: TMI grid coordinates
"""
function gridprops(file)
function axislabels(file)
if file[end-1:end] == "nc"

lon = convert(Vector{Float64},ncread(file,"lon"))
Expand Down
26 changes: 5 additions & 21 deletions src/field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,14 @@
longname::String
units::String
"""
struct Field{T}
tracer::Array{T,3}
γ::Grid
struct Field{T,N}
tracer::Array{T,N}
γ::Grid{T,N}
name::Symbol
longname::String
units::String
end

# """
# function Field(tracer::Array{T,3},γ::Grid) where T <: Real

# Outer constructor for Field if there's no worry about
# tracer type, long name, or units.
# # Arguments
# - `tracer::Array{T,3}`
# - `γ::Grid`
# # Output
# - `field::Field`
# """
#function Field(tracer::Array{T,3},γ::Grid) where T <: Real
# return Field(tracer,γ,:none,"unknown","unknown")
#end

function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, x::Field)
summary(io, x); println(io)
print(io, "Field size ")
Expand Down Expand Up @@ -121,13 +106,12 @@ end
read MATLAB field and transfer zyx format to xyz
"""
function readfield(file,tracername,γ::Grid)
function readfield(file,tracername,γ::Grid{T,N}) where {T,N}

# The mode "r" stands for read-only. The mode "r" is the default mode and the parameter can be omitted.
tracer, units, longname = _read3d(file,tracername)
checkgrid!(tracer,γ.wet)

c = Field(tracer,γ,tracerdict()[tracername],longname,units)
c = Field{T,N}(tracer,γ,tracerdict()[tracername],longname,units)
return c
end
function readfield(matfile,mattracername,γ::Grid,Izyx)
Expand Down
59 changes: 43 additions & 16 deletions src/grid.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Base: index_ndims, methods_including_ambiguous
"""
struct Grid
TMI grid with accounting for wet/dry points
"""
struct Grid{T <: Real}
lon::Vector{T}
lat::Vector{T}
depth::Vector{T}
wet::BitArray{3}
interior::BitArray{3}
struct Grid{T,N}
# lon::Vector{T}
# lat::Vector{T}
# depth::Vector{T}
axes::NTuple{N,Vector{T}}
wet::BitArray{N}
interior::BitArray{N}
end

"""
Expand All @@ -23,16 +25,23 @@ function Grid(TMIfile)
- `γ::Grid`: TMI grid struct
"""
function Grid(TMIfile::String; A = watermassmatrix(TMIfile))

# get properties of grid
lon,lat,depth = gridprops(TMIfile)
labels = axislabels(TMIfile)

ndims = length(labels)
#dimsize = Tuple{Int64}(undef,ndims)
dimsize = Tuple([length(labels[d]) for d in 1:ndims])
#end

# make ocean mask
wet = wetmask(TMIfile,length(lon),length(lat),length(depth))
#wet = wetmask(TMIfile,length(lon),length(lat),length(depth))
wet = wetmask(TMIfile,dimsize)

# make interior mask
interior = interiormask(A,wet,length(lon),length(lat),length(depth))

return Grid(lon,lat,depth,wet,interior)
#interior = interiormask(A,wet,length(lon),length(lat),length(depth))
interior = interiormask(A,wet,dimsize)
return Grid(labels,wet,interior)
end

"""
Expand Down Expand Up @@ -70,7 +79,7 @@ function Grid(foreign_file::S, maskname::S, lonname::S, latname::S, depthname::S
interior = deepcopy(wet)
interior[:,:,1] .= false

return Grid(lon,lat,depth,wet,interior)
return Grid((lon,lat,depth),wet,interior)
end

"""
Expand All @@ -79,12 +88,18 @@ end
Do not store Cartesian and linear indices.
Compute them on demand.
"""
Base.propertynames::Grid) = (:I,:R,fieldnames(typeof(γ))...)
Base.propertynames::Grid) = (:I,:R,:lon,:lat,:depth,fieldnames(typeof(γ))...)
function Base.getproperty::Grid, d::Symbol)
if d === :I
return cartesianindex.wet)
elseif d === :R
return linearindex.wet)
elseif d === :lon
return γ.axes[1]
elseif d === :lat
return γ.axes[2]
elseif d === :depth
return γ.axes[3]
else
return getfield(γ, d)
end
Expand Down Expand Up @@ -137,12 +152,17 @@ function checkgrid!(tracer,wet)
end
end

function wetmask(TMIfile::String,dimsize::NTuple)
# read Cartesian Index from file.
I = cartesianindex(TMIfile)
wet = falses(dimsize)
wet[I] .= 1
return wet
end
function wetmask(TMIfile::String,nx,ny,nz)
# older version that assumes N = 3
# read Cartesian Index from file.
I = cartesianindex(TMIfile)
# make a mask
# first choice: smaller but inconsistent with input grid
#wet = falses(maximum(I)[1],maximum(I)[2],maximum(I)[3])
wet = falses(nx,ny,nz)
wet[I] .= 1
return wet
Expand All @@ -158,3 +178,10 @@ function interiormask(A,wet,nx,ny,nz)
interior[I[list]] .= true
return interior
end
function interiormask(A,wet,dimsize::NTuple)
interior = falses(dimsize)
I = cartesianindex(wet)
list = findall(.!(isone.(sum(abs.(A),dims=2))))
interior[I[list]] .= true
return interior
end

0 comments on commit 414ee14

Please sign in to comment.