Making data in a .csv readable by the grid function in grdimage

I’m very new to programming so I’m very sorry if the question is obvious, but how do I convert the data in this .csv file into a format that can be read by the grid parameter in grdimage?

This is the data:
x being longitude
y being latitude
and
z being elevation

This is where I’m trying to use the grid parameter:

I understand that the format of the file has to be in NetCDF, but I don’t really understand how to turn the csv into it. I tried for a couple of hours but to no avail.

Sorry if my question was too vague. I’ll add as much context and as many images as requested. It’s my first time posting on any sort of forum. Thanks in advance for the help!

Your data looks gridded so should be able to do this:
First find your region:

gmt info -I0.0005 data.txt

Then use this in:

gmt xyz2grd -I0.0005 -Goutput.nc -R<region you got> data.txt

And you should be good to go!

NB! I don’t remember if gmt handles comma as field separator. Might be. Or else you can replace it with space or set IO_INPUT_SEPARATOR (or something like that).

I think I might have made a mistake. It looks like there was a specific section to ask about PyGMT and I didn’t post it there. I’m sorry. I’m kind of dumb and didn’t really notice. I also didn’t really understand your solution so I’ll try to rephrase the question.

I am trying to follow this example: https://www.earthinversion.com/utilities/pygmt-high-resolution-topographic-map-in-python/#importing-libraries up to the part where contour lines are plotted and the only thing I want to change is the data being used. Instead of ‘@earthrelief_30s’, I am trying to change the region (min/max lon and lan) and use data stored in a local .csv file pictured below:

example:

Data: x = longitude / y = latitude / z = elevation

It is my understanding that for grdImage to work, the data has to be in the format netCDF. I have tried to convert my data but after a multitude of failed attempts, I still don’t know how to.

My question is: “How do I turn my .csv data into a format that will in turn output a functioning map.

Thank you for your help.

The rest of the code is the same as that in the example, displayed in the link and in the code below:

import pygmt

minlon, maxlon = -67.025, -67.006 #change 1

minlat, maxlat = 17.963, 17.977 #change 2

Def main():

topo_data = ‘path_to_local_data_file/data.csv’ #change 3/final change

fig = pygmt.Figure()

pygmt.makecpt(

cmap=‘topo’,

series=‘-8000/8000/1000’,

continuous=True

)

fig.grdimage(

grid=topo_data,

region=[minlon, maxlon, minlat, maxlat],

projection=‘M4i’,

shading=True,

frame=True

)

fig.grdcontour(

grid=topo_data,

interval=4000,

annotation=“4000+f6p”,

limit=“-8000/0”,

pen=“a0.15p”

)

fig.colorbar(

frame=‘+l"Topography"’

)

fig.show()

if name == “main”: main()

If your x,y,z data is truly a grid (i.e., equidistant spacing between the x and y) then you do need to convert it to an actual grid via xyz2grd as Andreas suggested. Whether it is a netCDF grid (which is the default but there are other formats) of not is less important. As it is your x,y,z is a 3-column data table and not a grid.

How would one go about making it a grid?

See Andreas’s reply to you earlier. I am not using PyGMT but I believe xyz2grd is available there as well. Or you can do that on the command line.

I’ve never used PyGMT (yet), but maybe this will help you, https://www.pygmt.org/latest/api/generated/pygmt.xyz2grd.html.

I appreciate you both so much that I could kiss you. I think your suggestions worked. This is how I’m converting the data:

I now have another problem. It’s an extension of the previous one so I’ll ask in the same thread. When I try to generate the map the area appears grey

Writing:
print(pd.read_csv(‘path_to_local_file/data.csv’).__str__())
I get this:


but writing:
print(bathy_data.__str__())
I get this:

Anyone know what might be going on and how I can make it so that the map generates as intended?

I don’t know PyGMT, so can’t really help you with those commands.

The gray area usually means NaN (“Not a Number”); your grid covers the area depicted by your plot, but only contains NaN’s. Is this a plot of the entire grid or just a part of it?