# Grdcontour - printf format for open and closed contours

**URL:** <https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940>\
**Category:** Q&A\
**Created:** [June 1, 2024, 1:21am UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940 "2024-06-01T01:21:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wally](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@Wally](https://forum.generic-mapping-tools.org/u/Wally)\
**Post date:** [June 1, 2024, 1:21am UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/1 "2024-06-01T01:21:23Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [June 1, 2024, 2:02am UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/2 "2024-06-01T02:02:10Z")

</div>

> [@Wally](#):
>
> 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.

 ![hawaii](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/0/08a4a771198a5bf72e3d14f35f5309a5b3993c40.png)

---

<div class="post-metadata">

**Author:** ![Wally](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@Wally](https://forum.generic-mapping-tools.org/u/Wally)\
**Post date:** [June 1, 2024, 2:48am UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/3 "2024-06-01T02:48:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [June 1, 2024, 1:12pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/4 "2024-06-01T13:12:07Z")

</div>

> [@Wally](#):
>
> th 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 (

Here I plotted the open and closed contour:

## Closed contours:

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

 ![Closed](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/2/22862b0d371b33340e0d9779957e27fe233795ed.png)

## Open contours:

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

 ![Open](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/7/7ffb9e2f5e1f2db554effbf312ffd920ae638463.png)

---

<div class="post-metadata">

**Author:** ![Wally](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@Wally](https://forum.generic-mapping-tools.org/u/Wally)\
**Post date:** [June 1, 2024, 1:15pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/5 "2024-06-01T13:15:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [June 1, 2024, 2:05pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/6 "2024-06-01T14:05:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wally](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@Wally](https://forum.generic-mapping-tools.org/u/Wally)\
**Post date:** [June 1, 2024, 2:15pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/7 "2024-06-01T14:15:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [June 1, 2024, 2:33pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/8 "2024-06-01T14:33:08Z")

</div>

> [@Wally](#):
>
> I was hoping for a more elegant solution than manually deleting the duplicate nodes.

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

`gmt convert -Td` ([link](https://docs.generic-mapping-tools.org/dev/gmtconvert.html#t)) should work but I am not getting the result that I want.

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [June 1, 2024, 3:01pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/9 "2024-06-01T15:01:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Esteban82](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/esteban82/32/54_2.png) [@Esteban82](https://forum.generic-mapping-tools.org/u/Esteban82)\
**Post date:** [June 1, 2024, 3:48pm UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/10 "2024-06-01T15:48:29Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Wally](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@Wally](https://forum.generic-mapping-tools.org/u/Wally)\
**Post date:** [June 4, 2024, 1:57am UTC](https://forum.generic-mapping-tools.org/t/grdcontour-printf-format-for-open-and-closed-contours/4940/11 "2024-06-04T01:57:44Z")

</div>

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.
