Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error openning a variable from NetCDF #670

Closed
luflarois opened this issue May 22, 2024 · 3 comments
Closed

Error openning a variable from NetCDF #670

luflarois opened this issue May 22, 2024 · 3 comments

Comments

@luflarois
Copy link

I did the following code:

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")

when I run it I receive the error:

 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

I did try to inform the dimensions:

raster_t2m = Raster(t2m, (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] top-level scope
  @ REPL[11]:1

what I doing wrong?

@rafaqz
Copy link
Owner

rafaqz commented May 22, 2024

You don't need to use NCDatasets directly. It's just:

raster = Raster(arquivo_nc; name=:t2m)

Or

st = RasterStack(arquivo_nc)

raster = st[:t2m]

We only need to do import NCDatasets so the backend package is available to Rasters to use behind the scenes.

@luflarois
Copy link
Author

Hi @rafaqz , using this way it works. But I can't plot the data. Do U know how to plot it?

@rafaqz
Copy link
Owner

rafaqz commented May 22, 2024

Well, it depends on the dimensionality of :t2m. If its 2d, you can just do Makie.plot(raster). If its 3d you need to take a slice, like this if you have a time dimension for example Makie.plot(raster[Ti=1]).

There is also Rasters.rplot(raster) that will tile multiple slices.

@rafaqz rafaqz closed this as completed May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants