Hello,
I’m getting slightly different results when using pygmt.grdtrack on the same dataset, depending on whether I read the dataset directly from the GRD file or with xarray.load_dataset(). I strongly suspect it has something to do with float32 vs float64, so I’ll lay out an example as briefly as I can.
I have several GRD files (netCDF 4 format) with data as float32 and the coordinates (lon/lat) as float64, which is necessary because the grid is 128 pixels per degree. For this example, I cut down to a 16x16 grid centered on (45, 45).
rtwalker@pgda101:~$ gmt grd2xyz small.grd --FORMAT_FLOAT_OUT=%.30g
44.98828125 45.01171875 -0.563958942890167236328125
44.99609375 45.01171875 -0.56336867809295654296875
45.00390625 45.01171875 -0.564554393291473388671875
45.01171875 45.01171875 -0.5662910938262939453125
44.98828125 45.00390625 -0.554759800434112548828125
44.99609375 45.00390625 -0.551300346851348876953125
45.00390625 45.00390625 -0.553443372249603271484375
45.01171875 45.00390625 -0.556558072566986083984375
44.98828125 44.99609375 -0.546033084392547607421875
44.99609375 44.99609375 -0.54321348667144775390625
45.00390625 44.99609375 -0.545960903167724609375
45.01171875 44.99609375 -0.5504894256591796875
44.98828125 44.98828125 -0.54240500926971435546875
44.99609375 44.98828125 -0.544292449951171875
45.00390625 44.98828125 -0.545701205730438232421875
45.01171875 44.98828125 -0.547770082950592041015625
Using dem = xarray.load_dataset(‘small.grd’), I’ve verified that the values and coordinates have exactly the same values and are the right data types in xarray. I then define a dataframe that just contains (45, 45).
After running
df = pygmt.grdtrack(df, 'small.grd', newcolname='elevation_grd', coltypes='g')
df = pygmt.grdtrack(df, dem.z, newcolname='elevation_xr', coltypes='g')
I get this from df.head():
longitude latitude elevation_grd elevation_xr
45 45 -0.54725 -0.54848
which is not good.
However, if I do this:
dem64 = dem.assign(z64=dem.z.astype('float64'))
df = pygmt.grdtrack(df, dem64.z64, newcolname='elevation_xr64', coltypes='g')
then df becomes
longitude latitude elevation_grd elevation_xr elevation_xr64
45 45 -0.54725 -0.54848 -0.54725
so the result is the same as from reading the GRD file directly.
I don’t know the internals, but this looks to me like there’s a problem with the variable and coordinates being different precision floats. (Using float32 for everything would truncate the coordinate values, so that’s my best uninformed guess about why the result is different.) Is there a better way to handle this that doesn’t require casting the entire dataset to float64?
Thanks!
Ryan