Rasters lib

l: did the following code using raster:

using NCDatasets

using Plots

using Rasters

# Caminho para o arquivo NetCDF

arquivo_nc = "/run/media/lufla/Backup_Lufla/data_rafael/MONAN_DIAG_G_MOD_GFS_2024041500_2024041909.x40962L55.nc"

# Abrindo o dataset NetCDF

ds = NCDatasets.Dataset(arquivo_nc)

# Lendo a variável t2m (temperatura a 2 metros)

t2m = ds["t2m"][:]

# Fechando o dataset

close(ds)

# Criando um raster a partir dos dados de temperatura

raster_t2m = Raster(t2m)

# Plotando o raster

p = plot(raster_t2m, title="Temperatura a 2 metros", xlabel="Longitude", ylabel="Latitude", color=:thermal)

savefig(p, "t2m_temperature_plot.png")

But, when I run the code I receive the following error:

julia> raster_t2m = Raster(t2m[:,:,1])
ERROR: UndefKeywordError: keyword argument dims not assigned
Stacktrace:
 [1] Raster(A::Matrix{Union{Missing, Float32}})
   @ Rasters ~/.julia/packages/Rasters/CrXfm/src/array.jl:223
 [2] top-level scope
   @ REPL[8]:1

I dont understand what happened. ANy one can help me?

You can find the docs of the Rasters function/constructor at

for an abstract array like your t2m you have to provide the number of dimensions your array has (last case in the signatures).

I did it! Bu the results are not good. t2m has 3 dimensions:

julia> t2m = ds["t2m"][:]
720×360×1 Array{Union{Missing, Float32}, 3}:
[:, :, 1] =
 217.618  219.611  218.098  220.566  229.613  227.332  229.356  229.494  230.496  234.835  240.339  241.75   …  251.195  250.139  249.563  249.709  250.583  252.223  252.918  253.285  253.419  254.077  253.815
 217.603  219.551  218.008  220.648  229.703  227.463  229.438  229.362  230.514  235.821  240.763  241.232     251.088  249.963  249.409  249.628  250.518  252.175  252.907  253.289  253.416  254.08   253.818
 217.589  219.493  217.918  220.726  229.792  227.606  229.519  229.228  230.794  236.799  241.168  241.11      251.004  249.764  249.256  249.547  250.453  252.128  252.898  253.294  253.413  254.083  253.82
 217.575  219.435  217.828  220.8    229.879  227.774  229.597  229.138  231.074  237.774  241.856  240.993     250.928  249.583  249.118  249.461  250.389  252.08   252.89   253.298  253.41   254.086  253.823
 217.561  219.377  217.739  220.869  229.963  227.943  229.674  228.8    231.354  238.745  242.676  240.878     250.851  249.402  248.979  249.372  250.365  252.032  252.884  253.303  253.416  254.09   253.826
 217.548  219.319  217.649  220.934  230.047  228.114  229.749  228.4    231.632  238.97   243.492  240.766  …  250.774  249.219  248.859  249.288  250.342  251.98   252.877  253.307  253.423  254.093  253.829
 217.534  219.262  217.559  220.994  230.126  228.286  229.885  228.002  231.91   239.07   244.303  240.8       250.646  249.057  248.758  249.205  250.32   251.922  252.87   253.312  253.431  254.096  253.832
 217.52   219.206  217.48   221.044  230.159  228.461  230.145  227.605  231.887  239.172  245.11   240.944     250.501  248.894  248.658  249.124  250.3    251.864  252.864  253.317  253.438  254.099  253.835
 217.507  219.15   217.403  221.085  230.193  228.637  230.433  227.21   231.371  239.277  244.527  241.094     250.354  248.73   248.56   249.043  250.28   251.807  252.858  253.321  253.445  254.102  253.838
 217.493  219.094  217.326  221.122  230.226  228.796  230.72   226.817  230.843  239.383  243.703  241.251     250.204  248.576  248.462  248.963  250.261  251.751  252.852  253.333  253.452  254.106  253.841
 217.48   219.039  217.249  221.154  230.259  228.919  231.005  226.662  230.303  238.953  242.875  241.11   …  250.035  248.456  248.385  248.893  250.237  251.703  252.841  253.344  253.459  254.109  253.843
 217.467  218.984  217.172  221.182  230.291  229.123  231.288  226.79   229.751  237.477  242.046  240.685     249.841  248.367  248.337  248.837  250.209  251.668  252.823  253.353  253.465  254.112  253.846
   ⋮                                            ⋮                                            ⋮               ⋱             ⋮                                            ⋮                                 
 217.801  220.052  219.013  221.576  228.461  227.067  228.908  230.241  230.348  229.045  233.487  239.397     252.12   251.297  250.919  251.056  251.796  252.736  253.119  253.307  253.439  254.043  253.786
 217.784  220.059  218.94   221.614  228.573  227.066  228.989  230.053  230.181  228.944  234.759  240.032  …  252.108  251.225  250.812  250.89   251.686  252.702  253.098  253.304  253.44   254.049  253.789
 217.767  220.066  218.866  221.649  228.686  227.183  229.067  229.974  230.107  228.889  236.025  240.659     252.096  251.152  250.701  250.724  251.55   252.669  253.077  253.3    253.441  254.052  253.791
 217.75   220.073  218.793  221.645  228.802  227.301  229.118  229.993  230.029  228.827  237.285  241.799     252.084  251.063  250.588  250.559  251.414  252.614  253.06   253.296  253.443  254.055  253.794
 217.733  220.053  218.719  221.494  228.918  227.418  229.134  230.011  229.948  228.758  238.311  242.538     252.069  250.968  250.476  250.426  251.28   252.561  253.042  253.292  253.445  254.057  253.796
 217.717  219.984  218.634  221.339  229.033  227.405  229.148  230.03   229.863  228.937  238.586  243.027     251.944  250.871  250.363  250.316  251.146  252.507  253.025  253.289  253.441  254.06   253.799
 217.7    219.915  218.545  221.181  229.148  227.376  229.16   230.048  230.219  229.919  238.794  243.515  …  251.819  250.772  250.239  250.206  251.013  252.458  253.008  253.285  253.437  254.063  253.801
 217.683  219.851  218.456  221.02   229.249  227.349  229.17   230.011  230.346  230.91   238.997  244.002     251.695  250.67   250.109  250.096  250.885  252.411  252.991  253.281  253.433  254.066  253.804
 217.667  219.79   218.366  220.855  229.341  227.322  229.178  229.884  230.38   231.892  239.196  243.791     251.553  250.542  249.979  249.986  250.787  252.364  252.973  253.277  253.43   254.068  253.807
 217.65   219.73   218.277  220.688  229.432  227.296  229.216  229.755  230.416  232.866  239.474  243.109     251.406  250.414  249.846  249.876  250.717  252.317  252.957  253.276  253.426  254.071  253.809
 217.634  219.67   218.187  220.517  229.523  227.271  229.273  229.625  230.455  233.84   239.91   242.429     251.301  250.283  249.71   249.791  250.65   252.271  252.937  253.281  253.422  254.074  253.812

julia> raster_t2m = Raster(t2m)
ERROR: UndefKeywordError: keyword argument dims not assigned
Stacktrace:
 [1] Raster(A::Array{Union{Missing, Float32}, 3})
   @ Rasters ~/.julia/packages/Rasters/CrXfm/src/array.jl:223
 [2] top-level scope
   @ REPL[7]:1

julia> raster_t2m = Raster(t2m, 3)
ERROR: MethodError: no method matching Raster(::Array{Union{Missing, Float32}, 3}, ::Int64)
Closest candidates are:
  Raster(::AbstractArray; dims, kw...) at /home/lufla/.julia/packages/Rasters/CrXfm/src/array.jl:223
  Raster(::AbstractArray, ::Tuple; refdims, name, metadata, missingval) at /home/lufla/.julia/packages/Rasters/CrXfm/src/array.jl:210
  Raster(::A, ::D, ::R, ::Na, ::Me, ::Mi) where {T, N, D<:Tuple, R<:Tuple, A<:AbstractArray{T, N}, Na, Me, Mi} at /home/lufla/.julia/packages/Rasters/CrXfm/src/array.jl:203
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

If a make more explicit the tuple:

julia> raster_t2m = Raster(t2m, dims = (720, 360, 1))
ERROR: MethodError: no method matching _format(::Int64, ::Base.OneTo{Int64})
Closest candidates are:
  _format(::DimensionalData.Dimensions.LookupArrays.Explicit, ::Any, ::Any) at /home/lufla/.julia/packages/DimensionalData/MFtne/src/Dimensions/format.jl:99
  _format(::DimensionalData.Dimensions.LookupArrays.AutoLookup, ::AbstractRange) at /home/lufla/.julia/packages/DimensionalData/MFtne/src/Dimensions/format.jl:82
  _format(::Coord, ::AbstractRange) at /home/lufla/.julia/packages/DimensionalData/MFtne/src/Dimensions/coord.jl:79
  ...
Stacktrace:
 [1] map
   @ ./tuple.jl:236 [inlined]
 [2] format(dims::Tuple{Int64, Int64, Int64}, axes::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}, Base.OneTo{Int64}})
   @ DimensionalData.Dimensions ~/.julia/packages/DimensionalData/MFtne/src/Dimensions/format.jl:29
 [3] format(dims::Tuple{Int64, Int64, Int64}, A::Array{Union{Missing, Float32}, 3})
   @ DimensionalData.Dimensions ~/.julia/packages/DimensionalData/MFtne/src/Dimensions/format.jl:25
 [4] Raster(A::Array{Union{Missing, Float32}, 3}, dims::Tuple{Int64, Int64, Int64}; refdims::Tuple{}, name::Symbol, metadata::DimensionalData.Dimensions.LookupArrays.NoMetadata, missingval::Missing)
   @ Rasters ~/.julia/packages/Rasters/CrXfm/src/array.jl:213
 [5] Raster(A::Array{Union{Missing, Float32}, 3}, dims::Tuple{Int64, Int64, Int64})
   @ Rasters ~/.julia/packages/Rasters/CrXfm/src/array.jl:213
 [6] Raster(A::Array{Union{Missing, Float32}, 3}; dims::Tuple{Int64, Int64, Int64}, kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Rasters ~/.julia/packages/Rasters/CrXfm/src/array.jl:223
 [7] top-level scope
   @ REPL[12]:1

Hm, maybe open an issue at the repository? From the documentation something like Raster(A, (4,3,2)) (assuming your A has that size) should work. So something is either wrongly documented or has a bug.

At least if I provide not-enough or too-many dimensions I get a nice error message that the dimensions do not fit. I am surprised about the strange error when providing the right dimensions.

Even more am I surprised (but not an expert in that package either) why dims has to be provided, since an abstract array should have size implemented providing exactly such a tuple.

Thanks @kellertuer . I will open an issue there.

1 Like

While I still do not understand the Raster constructor, your problem was resolved super fast over there :+1: Error openning a variable from NetCDF · Issue #670 · rafaqz/Rasters.jl · GitHub

All good now?