From 8d1e98edaa038e502f500ea0adfb6c6e31b9d99d Mon Sep 17 00:00:00 2001 From: Felix Cremer Date: Wed, 13 Aug 2025 11:16:32 +0200 Subject: [PATCH 1/3] Add prefer_datetime keyword for Raster constructor and forward it to cfvariable --- src/array.jl | 3 ++- src/sources/commondatamodel.jl | 18 +++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/array.jl b/src/array.jl index 7dbfa923a..631b41788 100644 --- a/src/array.jl +++ b/src/array.jl @@ -353,6 +353,7 @@ function _raster(ds; raw::Bool=false, mod=nokw, f=identity, + prefer_datetime=true, )::Raster _maybe_warn_replace_missing(replace_missing) # `raw` option will ignore `scaled` and `missingval` @@ -381,7 +382,7 @@ function _raster(ds; Array(modvar) end # Generate dims - dims_out = isnokw(dims) ? _dims(var, crs, mappedcrs) : format(dims, data_out) + dims_out = isnokw(dims) ? _dims(var, crs, mappedcrs, prefer_datetime) : format(dims, data_out) # Return the data to the parent function mv_outer = _outer_missingval(mod) data_out, dims_out, metadata_out, mv_outer diff --git a/src/sources/commondatamodel.jl b/src/sources/commondatamodel.jl index 07fb842ed..e3b157b8b 100644 --- a/src/sources/commondatamodel.jl +++ b/src/sources/commondatamodel.jl @@ -111,6 +111,7 @@ defaultmappedcrs(::CDMsource) = EPSG(4326) function _nondimnames(ds) dimnames = CDM.dimnames(ds) + isempty(dimnames) && return String[] toremove = if "bnds" in dimnames dimnames = setdiff(dimnames, ("bnds",)) boundsnames = String[] @@ -122,10 +123,13 @@ function _nondimnames(ds) end end union(dimnames, boundsnames)::Vector{String} + elseif isempty(dimnames) + String[] else collect(dimnames)::Vector{String} end # Maybe this should be fixed in ZarrDatasets but it works with this patch. + nondim = collect(setdiff(keys(ds), toremove)) return nondim end @@ -158,10 +162,10 @@ function _layers(ds::AbstractDataset, names, group) _layers(ds.group[group], names, nokw) end -function _dims(var::AbstractVariable{<:Any,N}, crs=nokw, mappedcrs=nokw) where N +function _dims(var::AbstractVariable{<:Any,N}, crs=nokw, mappedcrs=nokw, prefer_datetime=true) where N dimnames = CDM.dimnames(var) ntuple(Val(N)) do i - _cdmdim(CDM.dataset(var), dimnames[i], crs, mappedcrs) + _cdmdim(CDM.dataset(var), dimnames[i], crs, mappedcrs, prefer_datetime) end end _metadata(var::AbstractVariable; attr=CDM.attribs(var)) = @@ -211,6 +215,7 @@ end # TODO don't load all keys here with _layers _name_or_firstname(ds::AbstractDataset, name) = Symbol(name) function _name_or_firstname(ds::AbstractDataset, name::Union{Nothing,NoKW}=nokw) + names = _nondimnames(ds) if length(names) > 0 return Symbol(first(names)) @@ -219,11 +224,11 @@ function _name_or_firstname(ds::AbstractDataset, name::Union{Nothing,NoKW}=nokw) end end -function _cdmdim(ds, dimname::Key, crs=nokw, mappedcrs=nokw) +function _cdmdim(ds, dimname::Key, crs=nokw, mappedcrs=nokw, prefer_datetime=true) if haskey(ds, dimname) var = ds[dimname] D = _cdmdimtype(CDM.attribs(var), dimname) - lookup = _cdmlookup(ds, dimname, D, crs, mappedcrs) + lookup = _cdmlookup(ds, dimname, D, crs, mappedcrs, prefer_datetime) return D(lookup) else # The var doesn't exist. Maybe its `complex` or some other marker, @@ -270,8 +275,8 @@ end # _cdmlookup # Generate a `Lookup` from a nCDM dim. -function _cdmlookup(ds::AbstractDataset, dimname, D::Type, crs, mappedcrs) - var = ds[dimname] +function _cdmlookup(ds::AbstractDataset, dimname, D::Type, crs, mappedcrs, prefer_datetime) + var = CDM.cfvariable(ds, dimname;prefer_datetime) index = Missings.disallowmissing(var[:]) attr = CDM.attribs(var) metadata = _metadatadict(sourcetrait(ds), attr) @@ -356,7 +361,6 @@ end function _cdmspan(index, order) # Handle a length 1 index length(index) == 1 && return Regular(zero(eltype(index))), Points() - step = if eltype(index) <: AbstractFloat # Calculate step, avoiding as many floating point errors as possible st = Base.step(Base.range(Float64(first(index)), Float64(last(index)); length = length(index))) From 3174a263deb6d7d19d9ed75fe718fe87755edf1b Mon Sep 17 00:00:00 2001 From: Felix Cremer Date: Thu, 21 Aug 2025 17:13:02 +0200 Subject: [PATCH 2/3] Add prefer_datetime keyword to GDALSource --- Project.toml | 2 +- ext/RastersArchGDALExt/gdal_source.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 210753fee..2155767e2 100644 --- a/Project.toml +++ b/Project.toml @@ -55,7 +55,7 @@ Aqua = "0.8" ArchGDAL = "0.9, 0.10" CFTime = "0.1" ColorTypes = "0.10, 0.11, 0.12" -CommonDataModel = "0.2.3, 0.3" +CommonDataModel = "0.2.3, 0.3.10" ConstructionBase = "1" CoordinateTransformations = "0.6.2" DataFrames = "1" diff --git a/ext/RastersArchGDALExt/gdal_source.jl b/ext/RastersArchGDALExt/gdal_source.jl index 789e33e9c..63a71b6d9 100644 --- a/ext/RastersArchGDALExt/gdal_source.jl +++ b/ext/RastersArchGDALExt/gdal_source.jl @@ -131,7 +131,7 @@ RA._open(f, ::GDALsource, A::AG.RasterDataset; mod=RA.NoMod(), kw...) = # These methods are type piracy on DimensionalData/ArchGDAL and may have to move some day # We allow passing in crs and mappedcrs manually -function RA._dims(raster::AG.RasterDataset, crs=nokw, mappedcrs=nokw) +function RA._dims(raster::AG.RasterDataset, crs=nokw, mappedcrs=nokw, prefer_datetime=true) gt_dims = try AG.getgeotransform(raster) catch From db0d92364b4d23e014f972dae5a8932c216f7192 Mon Sep 17 00:00:00 2001 From: Rafael Schouten Date: Fri, 22 Aug 2025 09:50:11 +1000 Subject: [PATCH 3/3] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2155767e2..7d4f20fc7 100644 --- a/Project.toml +++ b/Project.toml @@ -55,7 +55,7 @@ Aqua = "0.8" ArchGDAL = "0.9, 0.10" CFTime = "0.1" ColorTypes = "0.10, 0.11, 0.12" -CommonDataModel = "0.2.3, 0.3.10" +CommonDataModel = "0.3.10" ConstructionBase = "1" CoordinateTransformations = "0.6.2" DataFrames = "1"