# Format output with grd2xyz

**URL:** <https://forum.generic-mapping-tools.org/t/format-output-with-grd2xyz/3274>\
**Category:** Q&A\
**Created:** [September 15, 2022, 7:38pm UTC](https://forum.generic-mapping-tools.org/t/format-output-with-grd2xyz/3274 "2022-09-15T19:38:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [September 15, 2022, 7:38pm UTC](https://forum.generic-mapping-tools.org/t/format-output-with-grd2xyz/3274/1 "2022-09-15T19:38:22Z")

</div>

I’m trying to convert xyz -\> grd and back to xyz again with gmt. The output should look like the input.  
(The format is _EarthVision_, if anyone is wondering. As far as I know it’s not supported by gdal.)

Here’s my dummy input grid (only 0 values in z):

```auto
# Type: scattered data
# Version: 6
# Description: No description
# Format: free
# Field: 1 x
# Field: 2 y
# Field: 3 z meters
# Field: 4 column
# Field: 5 row
# Projection: Local Rectangular
# Units: meters
# End:
# Information from grid:
# Grid_size: 5 x 6
# Grid_space: -300.000000,-100.000000,-50.000000,200.000000
# Scattered data: Not_available
# Z_field: z
# Vertical_faults: Not_available
# History: No history
# Z_units: meters
-300.000000 -50.000000 0.000000 1 1
-250.000000 -50.000000 0.000000 2 1
-200.000000 -50.000000 0.000000 3 1
-150.000000 -50.000000 0.000000 4 1
-100.000000 -50.000000 0.000000 5 1
-300.000000 0.000000 0.000000 1 2
-250.000000 0.000000 0.000000 2 2
-200.000000 0.000000 0.000000 3 2
-150.000000 0.000000 0.000000 4 2
-100.000000 0.000000 0.000000 5 2
-300.000000 50.000000 0.000000 1 3
-250.000000 50.000000 0.000000 2 3
-200.000000 50.000000 0.000000 3 3
-150.000000 50.000000 0.000000 4 3
-100.000000 50.000000 0.000000 5 3
-300.000000 100.000000 0.000000 1 4
-250.000000 100.000000 0.000000 2 4
-200.000000 100.000000 0.000000 3 4
-150.000000 100.000000 0.000000 4 4
-100.000000 100.000000 0.000000 5 4
-300.000000 150.000000 0.000000 1 5
-250.000000 150.000000 0.000000 2 5
-200.000000 150.000000 0.000000 3 5
-150.000000 150.000000 0.000000 4 5
-100.000000 150.000000 0.000000 5 5
-300.000000 200.000000 0.000000 1 6
-250.000000 200.000000 0.000000 2 6
-200.000000 200.000000 0.000000 3 6
-150.000000 200.000000 0.000000 4 6
-100.000000 200.000000 0.000000 5 6

```

Make nc-grid of this;

```auto
# convert xyz -> nc
gmt xyz2grd -R-300/-100/-50/200 -Gtest.ev.nc test.ev -I50

```

Now for the task of formatting the output.

This is what I have:

```auto
# output xyz
gmt grd2xyz --IO_COL_SEPARATOR=space test.ev.nc > 1

# output column and rows
gmt grd2xyz -Cf --IO_COL_SEPARATOR=space test.ev.nc -o0,1 > 2

# sort by row, then column
paste -d ' ' 1 2 | sort -n -k5,5 -k4,4

```

Which gives:

```auto
-300 200 0 1 1
-250 200 0 2 1
-200 200 0 3 1
-150 200 0 4 1
-100 200 0 5 1
-300 150 0 1 2
-250 150 0 2 2
-200 150 0 3 2
-150 150 0 4 2
-100 150 0 5 2
-300 100 0 1 3
-250 100 0 2 3
-200 100 0 3 3
-150 100 0 4 3
-100 100 0 5 3
-300 50 0 1 4
-250 50 0 2 4
-200 50 0 3 4
-150 50 0 4 4
-100 50 0 5 4
-300 0 0 1 5
-250 0 0 2 5
-200 0 0 3 5
-150 0 0 4 5
-100 0 0 5 5
-300 -50 0 1 6
-250 -50 0 2 6
-200 -50 0 3 6
-150 -50 0 4 6
-100 -50 0 5 6

```

The problem is that `grd2xyz` starts counting the rows on the top left in the grid.  
I need it to start counting the rows from the _bottom left_.  
I _think_.

Another way to put it: the y-values decrease with increasing row numbers.  
They should _increase_ with increasing row numbers,

Any way to do this?  
I don’t know if what I have is a good solution, so any tips are welcome.

---

<div class="post-metadata">

**Author:** ![jaltekruse](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/jaltekruse/32/1607_2.png) [@jaltekruse](https://forum.generic-mapping-tools.org/u/jaltekruse)\
**Post date:** [September 15, 2022, 11:56pm UTC](https://forum.generic-mapping-tools.org/t/format-output-with-grd2xyz/3274/2 "2022-09-15T23:56:21Z")

</div>

I’m not sure I understand what you’re trying to do, since it looks like you just want the inputs without the headers (`sed -e '/^#/d' test.ev`). Presuming you’re doing more than that, I think this will do it:

```auto
gmt xyz2grd -R-300/-100/-50/200 -Gtest.ev.nc test.ev -I50
# 1-based row and column numbers, flip rows to number south to north
gmt grdmath -Rtest.ev.nc XCOL 1 ADD = test.col.grd
gmt grdmath -Rtest.ev.nc YROW NY SUB ABS = test.row.grd

gmt grd2xyz test.ev.nc --FORMAT_FLOAT_OUT=%.6f > test.out.xyz
gmt grd2xyz test.col.grd -Z > test.cols
gmt grd2xyz test.row.grd -Z > test.rows
paste -d ' ' test.out.xyz test.cols test.rows | sort -n -k5 -k4 > test.xyzcr

# check output
sed -e '/^#/d' test.ev > test.nohead
diff -w test.nohead test.xyzcr

```

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [September 16, 2022, 6:37am UTC](https://forum.generic-mapping-tools.org/t/format-output-with-grd2xyz/3274/3 "2022-09-16T06:37:55Z")

</div>

Based on a test, your script snippet does what I want and formats the grid output the right way.

Thank you!
