Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Aug 10, 2024
1 parent c8a3828 commit 7a87d81
Show file tree
Hide file tree
Showing 21 changed files with 491 additions and 239 deletions.
2 changes: 1 addition & 1 deletion ext/RastersArchGDALExt/RastersArchGDALExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using Rasters: GDALsource, AbstractProjected, AbstractRaster, AbstractRasterStac
GDAL_EMPTY_TRANSFORM, GDAL_TOPLEFT_X, GDAL_WE_RES, GDAL_ROT1, GDAL_TOPLEFT_Y, GDAL_ROT2, GDAL_NS_RES,
_no_crs_error

import Rasters: reproject, resample, warp, cellsize, nokw
import Rasters: reproject, resample, warp, cellsize, nokw, isnokw, isnokwornothing

const RA = Rasters
const DD = DimensionalData
Expand Down
19 changes: 10 additions & 9 deletions ext/RastersArchGDALExt/gdal_source.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ function Base.write(
kw...
) where T
RA.check_can_write(filename, force)
A1 = _maybe_correct_to_write(A, missingval)
A1 = _maybe_correct_to_write(A)
mod = RA._writer_mod(eltype; missingval, maskingval, scale, offset, coerce)
_create_with_driver(filename, dims(A1), T;
missingval, _block_template=A1, scale, offset, verbose, kw...
) do dataset
verbose && _maybe_warn_south_up(A, verbose, "Writing South-up. Use `reverse(myrast; dims=Y)` first to write conventional North-up")
if write
open(A1; write=true) do O
RA._maybe_modify(AG.RasterDataset(dataset), mod) .= parent(O)
Expand All @@ -76,7 +75,9 @@ function Base.write(
end

function RA._open(f, ::GDALsource, filename::AbstractString;
write=false, mod=NoMod(), kw...
write=false,
mod=RA.NoMod(),
kw...
)
# Check the file actually exists because the GDAL error is unhelpful
if !isfile(filename)
Expand All @@ -95,11 +96,11 @@ function RA._open(f, ::GDALsource, filename::AbstractString;
end
flags = write ? AG.OF_UPDATE : AG.OF_READONLY
return AG.readraster(filename; flags) do A
C = RA._maybe_modify(A, mod)
RA.cleanreturn(f(C))
A1 = RA._maybe_modify(A, mod)
RA.cleanreturn(f(A1))
end
end
RA._open(f, ::GDALsource, A::AG.RasterDataset; mod=NoMod(), kw...) =
RA._open(f, ::GDALsource, A::AG.RasterDataset; mod=RA.NoMod(), kw...) =
RA.cleanreturn(f(RA._maybe_modify(A, mod)))


Expand Down Expand Up @@ -372,9 +373,9 @@ function _create_with_driver(f, filename, dims::Tuple, T;
offset=nokw,
kw...
)
verbose && _maybe_warn_south_up(dims, verbose, "Creating a South-up raster. Use `reverse(myrast; dims=Y)` first to write conventional North-up")
verbose && _maybe_warn_south_up(dims, verbose, "Creating a South-up raster. You may wish to reverse the `Y` dimension to use conventional North-up")

missingval = RA.isnokw(missingval) || ismissing(missingval) ? RA._writeable_missing(T; verbose) : missingval
missingval = ismissing(missingval) ? RA._writeable_missing(T; verbose) : missingval
_gdal_validate(dims)

x, y = map(DD.dims(dims, (XDim, YDim))) do d
Expand Down Expand Up @@ -545,7 +546,7 @@ function _set_dataset_properties!(dataset::AG.Dataset, dims::Tuple, missingval,
gt = RA.dims2geotransform(x, y)
AG.setgeotransform!(dataset, gt)

if !isnothing(missingval)
if !RA.isnokwornothing(missingval)
bands = hasdim(dims, Band) ? axes(DD.dims(dims, Band), 1) : 1
for i in bands
rasterband = AG.getband(dataset, i)
Expand Down
7 changes: 6 additions & 1 deletion ext/RastersArchGDALExt/resample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ function resample(xs::Union{Tuple,NamedTuple}; to=first(xs), kw...)
map(x -> resample(x; to, kw...), xs)
end
function resample(A::RasterStackOrArray;
to=nothing, res=nothing, crs=nothing, size=nothing, method=:near, kw...
to=nothing,
res=nothing,
crs=nothing,
size=nothing,
method=:near,
kw...
)
(isnothing(size) || isnothing(res)) || _size_and_res_error()

Expand Down
8 changes: 7 additions & 1 deletion ext/RastersNCDatasetsExt/ncdatasets_source.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Base.write(filename::AbstractString, ::NCDsource, s::AbstractRasterStac
ds = NCD.Dataset(filename, mode; attrib=RA._attribdict(metadata(s)))
try
if missingval isa NamedTuple
map(k -> _writevar!(ds, s[k]; missinval=missingval[k], kw...), keys(s))
map(k -> _writevar!(ds, s[k]; missingval=missingval[k], kw...), keys(s))
else
map(k -> _writevar!(ds, s[k]; missingval, kw...), keys(s))
end
Expand Down Expand Up @@ -153,6 +153,12 @@ end
RA._sourcetrait(::NCD.Dataset) = NCDsource()
RA._sourcetrait(::NCD.Variable) = NCDsource()

@inline function RA.get_scale(metadata::Metadata{NCDsource}, scaled::Bool)
scale = scaled ? get(metadata, "scale_factor", nothing) : nothing
offset = scaled ? get(metadata, "add_offset", nothing) : nothing
return scale, offset
end

# precompilation

# const _NCDVar = NCDatasets.CFVariable{Union{Missing, Float32}, 3, NCDatasets.Variable{Float32, 3, NCDatasets.NCDataset}, NCDatasets.Attributes{NCDatasets.NCDataset{Nothing}}, NamedTuple{(:fillvalue, :scale_factor, :add_offset, :calendar, :time_origin, :time_factor), Tuple{Float32, Nothing, Nothing, Nothing, Nothing, Nothing}}}
Expand Down
4 changes: 2 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ function Raster(ds, filename::AbstractString;
source = _sourcetrait(filename, source)
data1, dims1, metadata1, missingval2 = _open(source, ds; name=name1, group, mod=NoMod()) do var
metadata1 = isnokw(metadata) ? _metadata(var) : metadata
missingval1 = _fix_missingval(var, missingval)
missingval1 = isnokwornothing(missingval) ? Rasters.missingval(var, metadata1) : missingval
maskingval1 = isnokw(maskingval) && !isnothing(missingval1) ? missing : maskingval
# If maskingval is `nothing` use missingval as missingval
missingval2 = isnothing(maskingval1) ? missingval1 : maskingval1
missingval2 = isnokwornothing(maskingval1) ? missingval1 : maskingval1
mod = isnokw(mod) ? _mod(eltype(var), metadata1, missingval1, maskingval1; scaled, coerce) : mod
data = if lazy
FileArray{typeof(source)}(var, filename;
Expand Down
Loading

0 comments on commit 7a87d81

Please sign in to comment.