# Path: pygeo/fastio.py
# 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
# 
# 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 writefast (filename, model):
#   '''
#   Write FAST model file from Python ndarray.
#   '''
# 
#   (nx, ny, nz) = model.shape
#   rmark = struct.pack(rlpak, np.int64(nx*2))
# 
#   fp = open(filename, 'wb')
# 
#   for k in range(nz):
#     for j in range(ny):
#       outstring = struct.pack('%dH'%(nx,), *list(model[:,j,k]))
#       fp.write(rmark)
#       fp.write(outstring)
#       fp.write(rmark)
# 
#   fp.close()


import os
import warnings
import numpy as np
from optparse import OptionParser
from pygeo.fastio import getParams, readfast, writefast
from scipy.interpolate import griddata
if (len(args) < 1):
  parser.error('Please specify an input model name.')

infile = args[0]

if (not os.path.isfile(infile)):
  parser.error('Input file "%s" does not exist!'%(infile,))

if (not os.path.isfile(hcfile)):
  parser.error('Hit count file "%s" does not exist!'%(hcfile,))

if (len(args) < 2):
  outfile = infile + addfilename
else:
  outfile = args[1]

try:
  params = getParams()
except IOError:
  parser.error('FAST header file "for.header" does not exist; are you executing in the FAST working directory?')

dims = params['dims']
pseudodims = (dims[0],1,dims[2])

try:
  model3d = readfast(infile, dims)
except:
  parser.error('Problem reading "%s"!'%(infile,))

try:

# Path: pygeo/fastio.py
# 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
# 
# 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 writefast (filename, model):
#   '''
#   Write FAST model file from Python ndarray.
#   '''
# 
#   (nx, ny, nz) = model.shape
#   rmark = struct.pack(rlpak, np.int64(nx*2))
# 
#   fp = open(filename, 'wb')
# 
#   for k in range(nz):
#     for j in range(ny):
#       outstring = struct.pack('%dH'%(nx,), *list(model[:,j,k]))
#       fp.write(rmark)
#       fp.write(outstring)
#       fp.write(rmark)
# 
#   fp.close()


# Path: util/slicefromhitcount.py
import os
import warnings
import numpy as np
from optparse import OptionParser
from pygeo.fastio import getParams, readfast, writefast
from scipy.interpolate import griddata

if (len(args) < 1):
  parser.error('Please specify an input model name.')

infile = args[0]

if (not os.path.isfile(infile)):
  parser.error('Input file "%s" does not exist!'%(infile,))

if (not os.path.isfile(hcfile)):
  parser.error('Hit count file "%s" does not exist!'%(hcfile,))

if (len(args) < 2):
  outfile = infile + addfilename
else:
  outfile = args[1]

try:
  params = getParams()
except IOError:
  parser.error('FAST header file "for.header" does not exist; are you executing in the FAST working directory?')

dims = params['dims']
pseudodims = (dims[0],1,dims[2])

try:
  model3d = readfast(infile, dims)
except:
  parser.error('Problem reading "%s"!'%(infile,))

try:

  hitcount = readfast(hcfile, dims)