Can GMT extract the z part of a .nc file into a vector form without going through grd2xyz?

Hello GMT fellows,

Does GMT have a way of extracting the z variable of a grid file into a vector form without going through grd2xyz? The use of grd2xyz on a large file takes some time. I want to plot the histogram of QQQ.nc, and I am thinking if there could be an easier way of getting only the z part of the file. For instance, in MATLAB, I would’ve done:

QQQ = gmt('read -Tg QQQ.nc');
z_matrix = QQQ.z;
z_vector = z_matrix(:);

Then proceed with the plotting…
Thanks in advance for your responses

Yes, but …

the problem is that you don’t have z_matrix(:) part on CLI. You can do

gmtconvert QQQ.nc?z

but z is 2D

Python and the Python gdal module could be used to read your GMT grid, extract the z values to an array, and then reshape it to a vector. Then you can plot with PyGMT.

Along those lines you may as well do things like

julia> using GMT

julia> z = gmtread("@earth_relief_05m", region=(-34,-23,35,42)).z[:];
julia> histogram(z, title="The Azores Plateau", show=true)

1 Like

Thank you very for this answer. This is the better option