Skip to content

Commit

Permalink
Small tidyups, remove CommonDataModel extension
Browse files Browse the repository at this point in the history
Move CommonDataModel extension changes to a separate PR
  • Loading branch information
sjdaines committed Dec 30, 2024
1 parent b97581c commit abb7ef6
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 26 deletions.
71 changes: 71 additions & 0 deletions Project cdm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name = "PALEOboxes"
uuid = "804b410e-d900-4b2a-9ecd-f5a06d4c1fd4"
authors = ["Stuart Daines <[email protected]>"]
version = "0.22.0"

[deps]
Atomix = "a9b6321e-bd34-4604-b9c9-b65b8de01458"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
MAT = "23992714-dd62-5051-b70f-ba57cb901cac"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SIMD = "fdea26ae-647d-5447-a871-4b548cad5224"
SLEEF_jll = "63e82ce6-3d80-5af4-a84c-484b71ab98bb"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
TestEnv = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[weakdeps]
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"

[extensions]
CommonDataModelExt = "CommonDataModel"

[compat]
Atomix = "0.1, 1.0"
BenchmarkTools = "1.0"
CommonDataModel = "0.3.7"
DataFrames = "1.1"
DocStringExtensions = "0.8, 0.9"
Documenter = "1"
Graphs = "1.4"
Infiltrator = "1.0"
Interpolations = "0.13, 0.14, 0.15"
MAT = "0.10"
NCDatasets = "0.12, 0.13, 0.14"
OrderedCollections = "1.4"
PrecompileTools = "1.0"
Preferences = "1.2"
Revise = "3.1"
SIMD = "3.3"
SLEEF_jll = "3.4"
StaticArrays = "1.4"
StructArrays = "0.6, 0.7"
TestEnv = "1.0"
TimerOutputs = "0.5"
YAML = "0.4.7"
julia = "1.10"

[extras]
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["CommonDataModel", "Documenter", "Logging", "Test"]
6 changes: 5 additions & 1 deletion src/CoordsDims.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ struct NamedDimension
end

function Base.show(io::IO, nd::NamedDimension)
print(io, "NamedDimension(name=", nd.name, ", size=", nd.size, ")")
if get(io, :typeinfo, nothing) === NamedDimension
print(io, "(name=", nd.name, ", size=", nd.size, ")")
else
print(io, "NamedDimension(name=", nd.name, ", size=", nd.size, ")")
end
return nothing
end

Expand Down
52 changes: 38 additions & 14 deletions src/Grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -684,24 +684,25 @@ function CartesianGrid(
end

"""
CartesianGrid(GridType, ncfilename::AbstractString, dimnames::Vector{<:AbstractString}; equalspacededges=false) -
> grid::GridType, coord_vars, coord_edges_vars
CartesianGrid(GridType, ncfilename::AbstractString, dimnames::Vector{<:AbstractString};
equalspacededges=false, convert_edges_to_bounds=false) -> grid::GridType, coord_vars, coord_edges_bounds_vars
Read a netcdf file with CF1.0 coordinates, and create corresponding a CartesianLinearGrid or CartesianArrayGrid from dimensions `dimnames`.
`coord_vars` and optional `coord_edges_vars` are Vectors of `coorddimname=>values` for creating coordinate variables.
`coord_vars` and optional `coord_edges_bounds_vars` are Vectors of `coorddimname => values` for creating coordinate variables.
"""
function CartesianGrid(
GridType::Type{<:Union{CartesianLinearGrid, CartesianArrayGrid}},
ncfilename::AbstractString,
dimnames::Vector{<:AbstractString};
equalspacededges=false
equalspacededges=false,
convert_edges_to_bounds=false,
)
@info "CartesianGrid creating $GridType{$(length(dimnames))} from dimnames=$dimnames in netcdf file $(ncfilename)"

grid = GridType{length(dimnames)}()

coord_vars = []
coord_edges_vars = []
coord_edges_bounds_vars = []

NCDatasets.Dataset(ncfilename) do ds
# read dimensions and coordinates
Expand All @@ -713,20 +714,43 @@ function CartesianGrid(
grid.dimensions[i] = PB.NamedDimension(dimname, dim)
v_cf = ds[dimname]
coord_vals = Array(v_cf)
@assert ndims(coord_vals) == 1
coord_vals = [cv for cv in coord_vals] # narrow type to eliminate unnecessary Missing
push!(coord_vars, dimname=>coord_vals)
if haskey(v_cf.attrib, "edges")
edgesname = v_cf.attrib["edges"]
edgesdim = ds.dim[edgesname]
edgesdim = ds.dim[edgesname]
@info " reading coordinate edges from $edgesname = $edgesdim"
push!(grid.dimensions_extra, PB.NamedDimension(edgesname, edgesdim))
push!(coord_edges_vars, edgesname=>Array(ds[edgesname]))
edges_vals = Array(ds[edgesname])
@assert size(edges_vals) == (edgesdim, )
edges_vals = [ev for ev in edges_vals]
if convert_edges_to_bounds
boundsname = dimname*"_bnds"
@info " converting edges to bounds $boundsname"
coord_bounds = similar(coord_vals, eltype(coord_vals), 2, length(coord_vals))
coord_bounds[1, :] = edges_vals[1:end-1]
coord_bounds[2, :] = edges_vals[2:end]
push!(coord_edges_bounds_vars, boundsname => coord_bounds)
else
push!(grid.dimensions_extra, PB.NamedDimension(edgesname, edgesdim))
push!(coord_edges_bounds_vars, edgesname => edges_vals)
end
elseif equalspacededges
edgesname = dimname*"_edges"
ew = coord_vals[2] - coord_vals[1]
edgesvals = [coord_vals .- ew/2.0; coord_vals[end] + ew/2.0 ]
@info " assuming equal spacing $ew to calculate coordinate edges $edgesname"
push!(grid.dimensions_extra, PB.NamedDimension(edgesname, dim+1))
push!(coord_edges_vars, edgesname=>edgesvals)
if convert_edges_to_bounds
boundsname = dimname*"_bnds"
@info " assuming equal spacing $ew to calculate coordinate bounds $boundsname"
coord_bounds = similar(coord_vals, eltype(coord_vals), 2, length(coord_vals))
coord_bounds[1, :] = coord_vals .- ew/2.0
coord_bounds[2, :] = coord_vals .+ ew/2.0
push!(coord_edges_bounds_vars, boundsname => coord_bounds)
else
edgesname = dimname*"_edges"
@info " assuming equal spacing $ew to calculate coordinate edges $edgesname"
edges_vals = [coord_vals .- ew/2.0; coord_vals[end] + ew/2.0 ]
push!(grid.dimensions_extra, PB.NamedDimension(edgesname, dim + 1))
push!(coord_edges_bounds_vars, edgesname => edges_vals)
end
else
@info " no edges attribute and equalspacededges=false"
end
Expand Down Expand Up @@ -760,7 +784,7 @@ function CartesianGrid(
grid.ncells = prod(grid_size)
end

return grid, coord_vars, coord_edges_vars
return grid, coord_vars, coord_edges_bounds_vars
end

"""
Expand Down
Loading

0 comments on commit abb7ef6

Please sign in to comment.