# Path: pygeo/fastio.py
# def readfast (filename, dims=None):
#   '''
#   Read FAST model file to Python ndarray.
#   '''
# 
#   if (dims is None):
#     dims = getParams()['dims']
# 
#   (nx, ny, nz) = dims
# 
#   fp = open(filename, 'rb')
#   map = mmap.mmap(fp.fileno(), 0, flags=mmap.MAP_PRIVATE)
# 
#   vol = np.zeros(dims, dtype=np.int16)
#   for k in range(nz):
#     for j in range(ny):
#       bytestart = k*ny*(2*rlpad+2*nx) + j*(2*rlpad+2*nx)
#       entries = struct.unpack('%dH'%(nx,), map[bytestart+rlpad:bytestart+rlpad+nx*2])
#       vol[:,j,k] = np.array(entries)
# 
#   map.close()
#   fp.close()
# 
#   return vol
# 
# def getParams (filename = 'for.header'):
#   with open(filename, 'r') as fp:
#     res = fp.read()
# 
#   res = res.strip().split()
#   paramdict = {}
#   paramdict['x'] = [float(item) for item in res[0:2]]
#   paramdict['y'] = [float(item) for item in res[2:4]]
#   paramdict['z'] = [float(item) for item in res[4:6]]
#   paramdict['dx'] = float(res[6])
#   paramdict['dims'] = [int(item) for item in res[7:10]]
# 
#   return paramdict


import numpy as np
from pygeo.fastio import readfast, getParams
from optparse import OptionParser
parser = OptionParser(	usage		= USAGE,
			version		= VERSION,
			description	= DESCRIPTION)

parser.add_option('-d', '--dims', action='store', dest='dims',
		help='dimensions of FAST model')

parser.add_option('-r', '--ref', action='store', dest='refmodel',
		help='reference model to subtract')

parser.set_defaults(	dims = None,
			refmodel = None)

(options, args) = parser.parse_args()

if (len(args) < 2):
  parser.print_help()
  exit(0)

infile = args[0]
outfile = args[1]

if (options.dims is None):
  dims = getParams()['dims'] 
else:
  dims = [int(item) for item in options.dims.strip().split(',')]

velmodel = readfast(infile, dims).copy(order='F')

if (options.refmodel is not None):

# Path: pygeo/fastio.py
# def readfast (filename, dims=None):
#   '''
#   Read FAST model file to Python ndarray.
#   '''
# 
#   if (dims is None):
#     dims = getParams()['dims']
# 
#   (nx, ny, nz) = dims
# 
#   fp = open(filename, 'rb')
#   map = mmap.mmap(fp.fileno(), 0, flags=mmap.MAP_PRIVATE)
# 
#   vol = np.zeros(dims, dtype=np.int16)
#   for k in range(nz):
#     for j in range(ny):
#       bytestart = k*ny*(2*rlpad+2*nx) + j*(2*rlpad+2*nx)
#       entries = struct.unpack('%dH'%(nx,), map[bytestart+rlpad:bytestart+rlpad+nx*2])
#       vol[:,j,k] = np.array(entries)
# 
#   map.close()
#   fp.close()
# 
#   return vol
# 
# def getParams (filename = 'for.header'):
#   with open(filename, 'r') as fp:
#     res = fp.read()
# 
#   res = res.strip().split()
#   paramdict = {}
#   paramdict['x'] = [float(item) for item in res[0:2]]
#   paramdict['y'] = [float(item) for item in res[2:4]]
#   paramdict['z'] = [float(item) for item in res[4:6]]
#   paramdict['dx'] = float(res[6])
#   paramdict['dims'] = [int(item) for item in res[7:10]]
# 
#   return paramdict


# Path: util/fast2paraview.py
import numpy as np
from pygeo.fastio import readfast, getParams
from optparse import OptionParser

parser = OptionParser(	usage		= USAGE,
			version		= VERSION,
			description	= DESCRIPTION)

parser.add_option('-d', '--dims', action='store', dest='dims',
		help='dimensions of FAST model')

parser.add_option('-r', '--ref', action='store', dest='refmodel',
		help='reference model to subtract')

parser.set_defaults(	dims = None,
			refmodel = None)

(options, args) = parser.parse_args()

if (len(args) < 2):
  parser.print_help()
  exit(0)

infile = args[0]
outfile = args[1]

if (options.dims is None):
  dims = getParams()['dims'] 
else:
  dims = [int(item) for item in options.dims.strip().split(',')]

velmodel = readfast(infile, dims).copy(order='F')

if (options.refmodel is not None):

  refmodel = readfast(options.refmodel, dims).copy(order='F')