Grdconvert will not accept -R option

I am trying to extract bathymetric profiles from the New Zealand 250 m grid available at NIWA in ESRI float format, working in a windows 10 terminal.

gmt grdconvert nzbathymetry_2016_ascii-grid.asc=ef -R178/179.4/-39.5/-38.8 -GsouthNZ.nc

fails with “syntax error -R”

Without the -R option grdconvert produces an nc file,

gmt grdconvert nzbathymetry_2016_ascii-grid.asc=ef -GAllNZ.nc

but, I still get ‘syntax error’ with the -R option

gmt grdcut AllNZ.nc -Rd178/-39.5/179.4/-38.8+r -GAllNZS.nc

I’ve tried many variations on the syntax, but have not been able to get around this, though nothing appears wrong when looking at the nc file with grdinfo or gdalinfo.

It is known that those grids have a problem with the registration (see it here) maybe it can explain something. But note that this syntax -Rd178/-39.5/179.4/-38.8+r is invalid. -Rd is a global region. An a final thing is that you can access the .asc file directly. No need to convert before cut.

It seems I naively assumed the esri header info would allow GMT to translate the xy grid data to lat lon for me. The NIWA website says the xy grid data use EPSG 3994. Perhaps I need to use the EPSG code in my GMT commands? I see there was a correction to the GDAL libraries four years ago to make these data read correctly.

GMT can read those esri files natively. No need to convert or do anything else. But, as I said and linked, those files have a wrong header info regarding the grid registration.

I think the data in the files you linked are different in that they appear to be natively in lat lon, while my file is just in grid xy offsets on pixel centers with 250 m pixels. I have not figured out how to reference the xy grid to translate it to lat lon it seems.

Those files are .asc esri ascii grids. Isn’t it the same case as yours?

Doesn’t this work (aside from the fact that coordinates might be off by half a cell size)

gmt grd2xyz nzbathymetry_2016_ascii-grid.asc > t.dat

grd2xyz works, producing xoffset, yoffset, depth triples. Now I need to get them into latitude longitude coordinates.

grd2xyz produces x_coord, y_coord, z where x|y_coord are coordinates in the grid’s coordinate system. Are you saying that they are not geographical coordinates and you want them? Then use mapproject or grdproject the esri grid.

My ultimate goal is to extract bathymetric profiles for acoustic propagation models. I tried:
gmt grdproject nzbathymetry_2016_ascii-grid.asc -GNZLL.nc -Jm100 -I -C
which produces a lat lon grid, but it is nowhere near New Zealand. I used 100 as 100 E is listed as the longitude of the central meridian while -41 is listed as the latitude of truescale.

OK, -Jm100 is not good for sure. Lower case are for map scales. Are you sure the asc grid is in Mercator projection? Because to inverse project you must know the data projection … but I remember you mentioned an EPSG, try

gmt grdproject nzbathymetry_2016_ascii-grid.asc -GNZLL.nc -J3994 -I -C -F

don’t remember if the -C -F is needed with EPGSs but it shouldn’t hurt

It is closer, but not right yet.
gmt grdinfo NZLL.nc
grdinfo [ERROR]: Grid x increment <= 0.0
grdinfo (gmtlib_read_grd_info): Use grdedit -A on your grid file to make region and increments compatible [NZLL.nc]
NZLL.nc: Title: Produced by grdproject
NZLL.nc: Command: grdproject nzbathymetry_2016_ascii-grid.asc -GC:\Users\mark\WhaleAcoust\Hikurangi\map\GMT\NZLL.nc -J+3994 -I -C -F
NZLL.nc: Remark:
NZLL.nc: Pixel node registration used [Geographic grid]
NZLL.nc: Grid file format: nf = GMT netCDF format (32-bit float), CF-1.7
NZLL.nc: x_min: 157.000000576 x_max: 193.001585358 x_inc: -0.0267413680438 name: longitude n_columns: 12116
NZLL.nc: y_min: -57.4999382033 y_max: -23.9993740265 y_inc: 0.00217663336864 name: latitude n_rows: 15391
NZLL.nc: z_min: 0 z_max: 0 name: z
NZLL.nc: scale_factor: 1 add_offset: 0
NZLL.nc: format: netCDF-4 chunk_size: 129,129 shuffle: on deflation_level: 3
GEOGCS[“unknown”,
DATUM[“Unknown based on WGS84 ellipsoid”,
SPHEROID[“WGS 84”,6378137,298.257223563,
AUTHORITY[“EPSG”,“7030”]]],
PRIMEM[“Greenwich”,0,
AUTHORITY[“EPSG”,“8901”]],
UNIT[“degree”,0.0174532925199433,
AUTHORITY[“EPSG”,“9122”]],
AXIS[“Longitude”,EAST],
AXIS[“Latitude”,NORTH]]

OK, make me a link with that file.
But have you looked at the registration problem? Are you sure the asc file has the correct registration?

I am not sure the original has the correct registration. The bathymetry data is also available other places which offer more info on it:
https://www.arcgis.com/home/item.html?id=a2582b1eb3584237a3b50418f379ca84

Those are very big files, > 560 Mb zipped ascii grids and the download sped for me (that have a 200 Mb network) is quite low ~2 hours.

Please print the grdinfo and gdalinfo outputs of your grid.

and what is the size of your grid?

The size of the grid is 12116 columns and 15391 rows
grdinfo nzbathymetry_2016_ascii-grid.asc
nzbathymetry_2016_ascii-grid.asc: Title:
nzbathymetry_2016_ascii-grid.asc: Command:
nzbathymetry_2016_ascii-grid.asc: Remark:
nzbathymetry_2016_ascii-grid.asc: Pixel node registration used [Cartesian grid]
nzbathymetry_2016_ascii-grid.asc: Grid file format: ei = ESRI Arc/Info ASCII Grid Interchange format (ASCII integer)
nzbathymetry_2016_ascii-grid.asc: x_min: 4795705.6 x_max: 7824705.6 x_inc: 250 name: x n_columns: 12116
nzbathymetry_2016_ascii-grid.asc: y_min: -5915585.8 y_max: -2067835.8 y_inc: 250 name: y n_rows: 15391
nzbathymetry_2016_ascii-grid.asc: z_min: NaN z_max: NaN name: z
nzbathymetry_2016_ascii-grid.asc: scale_factor: 1 add_offset: 0

gdalinfo nzbathymetry_2016_ascii-grid.asc
Driver: AAIGrid/Arc/Info ASCII Grid
Files: nzbathymetry_2016_ascii-grid.asc
Size is 12116, 15391
Origin = (4795705.599999999627471,-2067835.799999999813735)
Pixel Size = (250.000000000000000,-250.000000000000000)
Corner Coordinates:
Upper Left ( 4795705.600,-2067835.800)
Lower Left ( 4795705.600,-5915585.800)
Upper Right ( 7824705.600,-2067835.800)
Lower Right ( 7824705.600,-5915585.800)
Center ( 6310205.600,-3991710.800)
Band 1 Block=12116x1 Type=Float32, ColorInterp=Undefined
NoData Value=-9999

Try

gdalwarp -s_srs EPSG:3994 -t_srs EPSG:4326 --config CENTER_LONG 180 -overwrite nzbathymetry_2016_ascii-grid.asc nzbat_geo.nc

The arcgis site suggests the bounds should be
West longitude: 161.15
East longitude: 179.695
North latitude: -31.329
South latitude: -49.249
but
gmt grdinfo nzbat_geo.nc
grdinfo [WARNING]: Guessing of registration in conflict between x and y, using gridline
nzbat_geo.nc: Title: GDAL Band Number 1
nzbat_geo.nc: Command: Wed Jul 08 06:15:41 2020: GDAL Create(nzbat_geo.nc, … )
nzbat_geo.nc: Remark:
nzbat_geo.nc: Gridline node registration used [Geographic grid]
nzbat_geo.nc: Grid file format: nf = GMT netCDF format (32-bit float), CF-1.7
nzbat_geo.nc: x_min: 157.001255882 x_max: 193.00091029 x_inc: 0.00251061122869 name: longitude n_columns: 14340
nzbat_geo.nc: y_min: -57.4997149565 y_max: -24.0006293321 y_inc: 0.00251061122869 name: latitude n_rows: 13344
nzbat_geo.nc: z_min: 0 z_max: 0 name: GDAL Band Number 1
nzbat_geo.nc: scale_factor: 1 add_offset: 0
nzbat_geo.nc: format: classic

The command I gave gives what it seems to be the correct transform. Not sure about the registration conflict. Some GDAL rounding perhaps.

Could you give me the commands you just used to make the figure you posted?

That was just a screen capture of an image using Mirone. To do it with GMT you would use (classical mode)

gmt grdimage gridfile -I+d -J... -R... -K > ...
gmt pscoast -R -J -Da -W0.5 -O >> ...

or with the GMT julia (nzbat_geo.nc is the name I gave to the converted grid)

 imshow("nzbat_geo.nc", limits=(177.5, 178.5, -39.5, -38.8), shade=true, coast=true, fmt=:png, show=true)

it gives this