Skip to content

Commit

Permalink
Allow writing Coord containing data to NetCDF (#93)
Browse files Browse the repository at this point in the history
* reorganize files

* add `"cell"` as common name in dimensions

* working code for saving Coord [needs tests]

* fix `sametimespan` tests

* move to NCDatasets 0.11

* allow string values for dimensions without errors

TODO: Test it!

* clarify docstring of `sametimespan`

* file re-org, and bump version

* add tests for writing coord stuff

* final todo comments
  • Loading branch information
Datseris authored May 4, 2022
1 parent 25d87e7 commit 35b5e8f
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 229 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.16
- Data with `Coord` dimension can now be saved as .nc files with `ncwrite`.
- When loading data with `Coord`, the longitude was automatically wrapped to -180 to 180 degrees. There was no reason for this, now no wrapping is done, the user may want to do it on their own after loading.

# 0.15
- Removed functions for calculating some decompositions of albedo to atmosphere/surface contributions. They were not basic enough for this package.

Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClimateBase"
uuid = "35604d93-0fb8-4872-9436-495b01d137e2"
authors = ["Datseris <[email protected]>", "Philippe Roy <[email protected]>"]
version = "0.15.2"
version = "0.16.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -17,7 +17,7 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
[compat]
DimensionalData = "0.20.1"
Interpolations = "0.13.2"
NCDatasets = "0.10, 0.11"
NCDatasets = "0.11"
Requires = "1"
SignalDecomposition = "1"
StaticArrays = "0.12, 1.0"
Expand Down
6 changes: 4 additions & 2 deletions src/ClimateBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ export NCDataset

include("core/coredefs.jl")
include("core/prettyprint.jl")
include("core/nc_io.jl")
include("core/aggregation.jl")
include("core/interpolation.jl")

include("physical_dimensions/spatial.jl")
include("physical_dimensions/spatial_equalarea.jl")
include("physical_dimensions/temporal.jl")

include("climate/solar.jl")
include("io/vector2range.jl")
include("io/netcdf.jl")

# All following will be moved to ClimateTools.jl once its updated
include("climate/solar.jl")
include("tsa/continuation.jl")
include("tsa/decomposition.jl")

Expand Down
4 changes: 2 additions & 2 deletions src/core/aggregation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function nomissing(da::AbstractArray{Union{T,Missing},N}, check = any(ismissing,
check && error("array contains missing values")
return Array{T,N}(da)
end
nomissing(da::AbstractArray{<:Union{Real, Dates.TimeType}}, args...) = da
nomissing(da, args...) = da
nomissing(da::ClimArray) = ClimArray(nomissing(da.data), da.dims, da.refdims, da.name, da.attrib)


Expand Down Expand Up @@ -69,7 +69,7 @@ If `A` is one dimensional, `dropagg` will return the single number of applying `
Optionally you can provide statistical weights in the form of a `W::ClimArray`.
`W` must either have same size as `A` or have only one dimension and satisfy
`length(W) == length(dims(A, d))` (i.e., a weight for each value of the dimension `d`).
The latter case can only work when `d` is a single dimension. See also
The latter case can only work when `d` is a single dimension. See also
[`missing_weights`](@ref) for (properly) dealing with data that have `missing` values.
"""
function dropagg(f, A, dims)
Expand Down
69 changes: 69 additions & 0 deletions src/io/netcdf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#=
Code related with input output (IO) of .nc files directly to/from ClimArrays
utilizing the NCDatasets.jl package and a buttload of convenience code.
An initial version of parts of this code was taken from:
https://github.com/rafaqz/GeoData.jl
=#
using NCDatasets: NCDatasets, NCDataset
export NCDatasets, NCDataset
export nckeys, ncdetails, globalattr, ncsize
export ncread, ncwrite

dim_to_commonname(::Lat) = "lat"
dim_to_commonname(::Lon) = "lon"
dim_to_commonname(::Time) = "time"
dim_to_commonname(::Pre) = "level"
dim_to_commonname(D::Dim) = string(DimensionalData.name(D))
dim_to_commonname(::Coord) = "cell"

const POSSIBLE_CELL_NAMES = ("ncells", "cell", "rgrid", "grid")


"""
nckeys(file::String)
Return all keys of the `.nc` file in `file`.
"""
function nckeys(path::String)
NCDataset(path) do ds
return keys(ds)
end
end
nckeys(a::NCDataset) = keys(a)

"""
ncdetails(file, io = stdout)
Print details about the `.nc` file in `file` on `io`.
"""
function ncdetails(file, io = stdout)
NCDataset(file) do ds
show(io, MIME"text/plain"(), ds)
end
end
ncdetails(ds::NCDataset, io = stdout) = show(io, MIME"text/plain"(), ds)

"""
ncsize(file, var)
Return the size of the variable of the `.nc` file without actually loading any data.
"""
function ncsize(file, var)
NCDataset(file) do ds
return size(ds[var])
end
end


"""
globalattr(file::String) → Dict
Return the global attributes of the .nc file.
"""
function globalattr(file::String)
NCDataset(file) do ds
return Dict(ds.attrib)
end
end

#########################################################################
# Imports
#########################################################################
include("netcdf_read.jl")
include("netcdf_write.jl")
Loading

0 comments on commit 35b5e8f

Please sign in to comment.