Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Converts one or more zmap grids to ascii x,y,z tab-delimited files. Exported
files will be saved to the current directory with a ".xyz" extension and the
same file name as the original. Null grid values will not be included unless
the "--with_nulls" option is specified.
Usage: [options] INPUT_FILES...
-h --help Show this message
--with_nulls Include null grid points in the output
import os
import numpy as np
from docopt import docopt
def main(args):
for filename in args['INPUT_FILES']:
process_file(filename, args['--with_nulls'])
def process_file(filename, include_nulls=False):
x, y, z = grid2xyz(filename)
if include_nulls:
x, y, z = [item.ravel() for item in [x, y, z]]
x, y, z = [item.compressed() for item in [x, y, z]]
base, ext = os.path.splitext(filename)
np.savetxt(base + '.xyz', np.vstack([x, y, z]).T)
def grid2xyz(filename):
grid = ZmapGridReader(filename)
# Note the reversed y-order!! (Grid is flipped up/down)
y, x = np.mgrid[grid.ymax:grid.ymin:1j*grid.nrows,
x =,
y =,
return x, y,
class ZmapGridReader(object):
def __init__(self, filename):
self.filename = filename
with open(self.filename, 'r') as infile:
values = self._parse_header(infile)
null, nrows, ncols, xmin, xmax, ymin, ymax = values
self.null = float(null)
self.nrows, self.ncols = int(nrows), int(ncols)
self.xmin, self.xmax = float(xmin), float(xmax)
self.ymin, self.ymax = float(ymin), float(ymax) = self._read_values()
def _skip_comments(self, infile):
while True:
last_position = infile.tell()
if not infile.readline()[0].startswith('!'):
def _parse_header(self, infile):
def line(infile):
return infile.readline().replace(',', ' ').strip().split()
header = infile.readline()
width, null = line(infile)[:2]
nrows, ncols, xmin, xmax, ymin, ymax = line(infile)[:6] # Trailing ,
return null, nrows, ncols, xmin, xmax, ymin, ymax
def _read_values(self):
def value_iter(infile):
for line in infile:
line = line.strip().split()
for item in line:
yield float(item)
with open(self.filename, 'r') as infile:
for line in infile:
if line.strip() == '@':
data = np.fromiter(value_iter(infile), dtype=float)
data = data.reshape(self.ncols, self.nrows).T
return, self.null)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.