Mat2grid question

Sorry for the very basic question. Unfortunately, I’m struggling to get it to work. Is below the right way to use mat2grid? My variable A stores the temperature values at a grid that is defined by lon and lat with the size of 1440 x 720.

lon = collect(20:0.25:379.75);
lat = collect(-90:0.25:89.75);
B = mat2grid(A, x=lon, y=lat);

Here are their sizes:

size(A) = (1440, 720)
size(lon) = (1440,)
size(lat) = (720,)

What did I do wrong? Many thanks!

lon is x and lat is y but your A grid has lon size rows and lat size columns. Is your A matrix transposed?

Note, maybe ask this type of questions in the GMT forum?

1 Like

Many thanks. Unfortunately, now I have a new error as below.

So this is the official GMT forum? Glad to hear about that. I’ll try to ask this type of questions over there:

Error message:

**ERROR:** LoadError: MethodError: no method matching GMTgrid(::String, ::String, ::Int64, ::Vector{Float64}, ::Vector{Float64}, ::Int64, ::Float64, ::String, ::String, ::String, ::Vector{String}, ::Vector{Float64}, ::Vector{Float64}, ::Vector{Float64}, ::LinearAlgebra.Adjoint{Float64, Matrix{Float64}}, ::String, ::String, ::String, ::String, ::String, ::Float32, ::Float32, ::Int64)

Closest candidates are:
GMTgrid(::String, ::String, ::Int64, ::Vector{Float64}, ::Vector{Float64}, ::Int64, ::Union{Float32, Float64}, ::String, ::String, ::String, ::Vector{String}, ::Vector{Float64}, ::Vector{Float64}, ::Union{Vector{String}, Vector{<:Real}}, ::Array{T, N}, ::String, ::String, ::String, ::String, ::String, ::Union{Float32, Float64}, ::Union{Float32, Float64}, ::Int64) where {T<:Real, N} at ~/.julia/packages/GMT/TPV4a/src/gmt_main.jl:2
Stacktrace:
[1] **mat2grid(** mat::LinearAlgebra.Adjoint{Float64, Matrix{Float64}}, xx::Vector{Float64}, yy::Vector{Float64}; reg::Nothing, x::Vector{Float64}, y::Vector{Float64}, v::Vector{Float64}, hdr::Nothing, proj4::String, wkt::String, epsg::Int64, tit::String, rem::String, cmd::String, names::Vector{String}, scale::Float32, offset::Float32 **)**
@ GMT ~/.julia/packages/GMT/TPV4a/src/utils_types.jl:622

Yes, but I live on GMT+1, the timezone (and very soon GMT+0), probably will look only tomorrow. Please provide all info that I need to reproduce the case.

Yes, will do. Many thanks for all of your help! :+1: Good night :zzz:

Use Array(A’) after A matrix transposed.

1 Like

Just an aside, you will very rarely need to collect a range. Since a range is only 3 numbers (start, step, end) it’s way more storage efficient than a vector. Especially since vectors are mutable and therefore difficult to optimize around.

2 Likes

Many thanks for the solution! It works well :+1:

Gustaphe,

Thank you for that tip! So I should just do the below?
lon = [20:0.25:279.75];

BTW, I find it weird that mat2grid is asking for the longitude and latitude vectors or ranges, instead of the gridded longitude and latitude. Won’t the latter be faster? How could I do that?

I have no idea about mat2grid - it gives “Binding mat2grid does not exist” on my machine, so it’s probably from some package you forgot to mention.

But unless it’s really poorly written, it should accept a range just as well as an array:

lon = 20:0.25:279.75

if you but that in square brackets, you are making a vector with one element of type StepRangeLen

1 Like

Thank you!

So you have an alternative function other than mat2grid?

Like I said, I don’t know what it does, so no.

1 Like

Yes, you can create a GMTgrid type yourself and making sure all mandatory fields are filled correctly.

grids are not just matrices. If you care about coordinates, you need to provide the x,y origins, grid spacing. Coordinate system when that matters. Vertical coordinates when dealing with cubes, other metadata, etc. mat2grid is a convenient function to do all that, but users are ofc free to create the GMTgrid type by themselves.

When data is to be transmitted to C you need to provide the actual numbers. C doesn’t know what are UnitRange or LinearAlgebra.Adjoint types.

1 Like

Yes. But that decision should be delayed as close as possible to the ccall imo. If there is a documented function mat2grid, I as a user shouldn’t need to know it’s going through C.

G = mat2grid(rand(Float32, 129, 129), x=0:0.5:64, y=0:0.5:64);
 imshow(G, coast=true, cmap=:topo, proj=:guess)

A different story is to accept abstract arrays in the type and pay the performance penalty or complicate a lot the multiple dispatch of this function.

1 Like