#!/usr/bin/python
# Script for vertical well+marker database import to Gocad
# 
# COPYRIGHT: (c) Marcus Apel 2006
# All rights reserved.
# Contact: http://www.geo.tu-freiberg.de/~apelm
# 
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice and the following disclaimer. 
# Redistributions in binary form must reproduce the above copyright notice and the following
# disclaimer in the documentation and/or other materials provided with the distribution. 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# 
# REMARKS:
# Requires Python 2.4 (www.python.org).
#
# Format of Location table file:
# ID X Y Z DEPTH(optional)
#
# Format of Markers table file:
# ID TVD MARKERNAME
#

import sys, os, re, tkMessageBox
from tkFileDialog import *
loc = askopenfilename(filetypes=[("Location", "*.*")])
horiz = askopenfilename(filetypes=[("Markers", "*.*")]) 
loclines = open(loc).readlines()
horizlines = open(horiz).readlines() 
filename = os.path.split(loc)[1] 
try:
   for locline in loclines:
      locelem = locline.split() 
      depth = 0.
      if len(locelem) == 5 :
         depth = locelem[4]
      if len(locelem) < 4 :
          break;        
      outfile = open(locelem[0],'w')
      markerlist = []
      for horizline in horizlines:
         horizelem = horizline.split()
         if len(horizelem) < 3 :
             break;  
         if horizelem[0] == locelem[0]:
            markername = horizelem[2]+' 1 '+horizelem[1]
            markername = markername.replace('?','')
            markername = markername.replace('/','_')
            markername = markername.replace('(','')
            markername = markername.replace(')','')
            markername = markername.replace(',','-')
            markername = markerlist.append(markername)
            if depth < float(horizelem[1]):
               depth = float(horizelem[1])
      dz = float(locelem[3]) - depth

      outfile.write('GOCAD Well 1\n')  
      outfile.write('HEADER {\n')
      outfile.write('name:'+ locelem[0]+'\n')
      outfile.write('}\n')
      outfile.write('WREF '+locelem[1]+' '+locelem[2]+' '+locelem[3]+'\n')
      outfile.write('DATUM KellyBushing\nKB 0\n')       
      outfile.write('PATH 0 '+str(locelem[3])+' 0 0\n')
      outfile.write('PATH '+str(depth)+' '+str(dz)+' 0 0\n')
      for marker in markerlist:
         outfile.write('MRKR '+marker+'\n') 
      outfile.write('END\n')

   tkMessageBox.showinfo("OK", loc +" converted")
except:
   tkMessageBox.showinfo("Error", sys.exc_info()[1])

