Grdcontour - printf format for open and closed contours

I wish to generate both and open closed contours. The GMT6.5 grdcontour reference and example explain how to use the ‘%c’ C language printf format specifier with the -D option to generate both contours. Accordingly, I have the following command:

gmt grdcontour @earth_relief_15s -R-156.1/-154.8/18.9/20.3 -C3000, -Dhawaii_contour_3000m_15s_%c.txt

I was expecting an output of two files:
hawaii_contour_3000m_15s_C.txt for the closed contour, and
hawaii_contour_3000m_15s_O.txt for the open.

What I actually got was:
hawaii_contour_3000m_15s_C.txt, and
hawaii_contour_3000m_15s_

The closed contour file was as expected, but the open contour file was incorrectly named and contained no data. Am I using the correct command or is there a bug in the code?

P.S. I use a windows PC and I know that the %c modifier has to be escaped as %%c when it is used in a batch file.

P.P.S. For the GMT developers: The final example in the grdcontour module reference is:

gmt grdcontour @AK_gulf_grav.nc -C150 -S4 -DAK_contours_%c.txt

The remote data file AK_gulf_grav.nc doesn’t appear to be available.

Hi Wally

I test it and worked well for me. Why do you get?

Also, I notice that there are not open contour (for 3000 m) in your region of Hawaii. So, it is ok to get an empty file.

Thanks for the prompt reply Federico.

Firstly, I’ve just retried the example script with the remote @AK_gulf_grav.nc file and it works now. Maybe I had an internet connection problem when I tried it earlier.

Secondly, perhaps I’ve misinterpreted what open and closed contours are and what the %c modifier does. I assumed that when grdcontour (with the -D %c modifier) is finding the contour from the elevation data it created two files: a closed contour (with the first and last point duplicated) and an open contour (with no duplicates). If this is not the case, could you explain the difference between open and closed contours?

Here I plotted the open and closed contour:

Closed contours:

gmt plot AK_contours_C.txt -W -png Closed -Bf

Open contours:

gmt plot AK_contours_O.txt -W -png Open -Bf

Further to my last, the reason I need an open contour is that I will be using sphdistance that uses the STRIPACK algorithm. STRIPACK fails if there are duplicate nodes (i.e. closed contours). Sphdistance has the -D option to ignore duplicate nodes, but as the reference documentation states: ‘… it is a very brute-force yet exact comparison that is very slow for large data sets’. Whilst in this (small) example -D is not limiting, I have another project that will be using a large data set. Hence my desire to obtain an open elevation contour to minimise CPU processing time.

Well, in the case of the files created by grdcontour -D it is easy. The duplicates records are the first and last of each segment. So, you have to only delete one of them.

I was afraid that might be my only way of producing the open contours. I was hoping for a more elegant solution than manually deleting the duplicate nodes.

Thanks for your time and help Federico.

I didn’t mean that you have to do it manually.

gmt convert -Td (link) should work but I am not getting the result that I want.

No, it seems that convert -Td only works for adjacent duplicate lines.

But you could use sort and uniq to delete the duplicate nodes. sort modifies the original order of the file. I don’t know if this affects how STRIPACK works.

sort AK_contours_C.txt | uniq > new_contour.txt

I’ve since learnt that I can use GRDMATH LDIST to compute the distance grid and it accepts closed contours. So I no longer need open contours and my original problem has gone away. Thanks Federico for your help.