Back to SIOSEIS Examples.          Go to the list of seismic processes.          Go to SIOSEIS introduction.

For real time SGG MCS data, back up and converting .sgd to .sgy

#! /usr/bin/env python
# Author: B. Murphy, J.Turnbull
# Modified: 10/05/2011 
# For real time SGG MCS data, back up and converting .sgd to .sgy
# Shipboard Geophysical Group, Scripps Institution of Oceanography
moddate = '10/05/2011'

import os, commands, time, sys, filecmp, shutil

os.system('clear')
print """
  ###########################
  #                         #
  #       SGG SIO STS       # 
  #                         #
  #   Realtime MCS Script   #
  #                         #
  # -server data backup     #
  # -segd -> segy coversion #
  #                         #
  # -modified:              # 
  #  %s             #
  #                         #
  ###########################
""" %moddate

# -------------------
# Define Variables
# -------------------
MASTERID = os.getpid()
SRCDIR = "/mnt/acquisition"
print "'realtime' PID is: "+str(MASTERID)+"\n" 
print "Enter 'q' or 'Q' to quit at any time..."

TEST = os.listdir(SRCDIR)
while TEST != []:
  NUM = len(TEST)
  CLEAR = raw_input("""
WARNING:

'%s' contains %s file(s).

Proceed, Retry, Quit [yes/r/q]? """ %(SRCDIR, NUM))
  if CLEAR == 'r' or 'R':
    TEST = os.listdir(SRCDIR)
  if CLEAR == 'q' or CLEAR == 'Q':
    print "\nClosing... Goodbye.\n"
    sys.exit(0)
  if CLEAR == 'yes' or CLEAR == 'Yes':
    TEST = []  
    
PROCEED = 'y'
while PROCEED == 'y' or PROCEED == 'Y':
  CRUISE = raw_input("\nPlease enter the cruise name: ")
  if CRUISE == 'q' or CRUISE == 'Q':
    print "\nClosing... Goodbye.\n"
    sys.exit(0)
  RECLEN = raw_input("Please enter the recording length [seconds]: ")
  if RECLEN == 'q' or RECLEN == 'Q':
    print "\nClosing... Goodbye.\n"
    sys.exit(0)     
  while RECLEN.isdigit() == False:
    print "\n'Recording length' must be a positive integer!"
    RECLEN = raw_input("Please enter the recording length [seconds]: ")
    if RECLEN == 'q' or RECLEN == 'Q':
      print "\nClosing... Goodbye.\n"
      sys.exit(0)
  PROCEED = raw_input("""
We are on cruise: '%s'
Recording for: %s second(s)\n
Need to re-enter [y/n]? """ % (CRUISE, RECLEN))
  if PROCEED == 'q' or PROCEED == 'Q':
    print "\nClosing... Goodbye.\n"
    sys.exit(0)

CONT = 'n'
PROCEED2 = 'y'
while CONT == 'n' or CONT == 'N':
  while PROCEED2 == 'y' or PROCEED2 == 'Y':
    SITENU = raw_input("\nEnter a site name or number: ")
    if SITENU == 'q' or SITENU == 'Q':
      print "\nClosing... Goodbye.\n"
      sys.exit(0)
# --------------------
# Uncomment to force SITENU to be integer
#    while SITENU.isdigit() == False:
#      print "\n'Site Number' must be a positive integer!"
#      SITENU = raw_input("Please enter a site number: ")
#      if SITENU == 'q' or SITENU == 'Q':
#        print "\nClosing... Goodbye.\n"
#        sys.exit(0)
    LINENU = raw_input("Enter a line name or number: ")
    if LINENU == 'q' or SITENU == 'Q':
      print "\nClosing... Goodbye.\n"
      sys.exit(0)
# --------------------
# Uncomment to force LINENU to be integer
#    while LINENU.isdigit() == False:
#      print "\n'Line Number' must be a positive integer!"
#      LINENU = raw_input("Please enter a line number: ")
#      if LINENU == 'q' or LINENU == 'Q':
#        print "\nClosing... Goodbye.\n"
#        sys.exit(0)
    print "\nSite: " + SITENU
    print "Line: " + LINENU
    PROCEED2 = raw_input("\nNeed to re-enter [y/n]? ")
    if PROCEED2 == 'q' or PROCEED2 == 'Q':
      print "\nClosing... Goodbye.\n"
      sys.exit(0)
  PROCEED2 = 'y'
 
#-------------------------
# Define from config and user input
  DATADIR = '/home/seismic/data/'+CRUISE
  PROCIDS = DATADIR+'/tmp/procids_'+str(MASTERID)+'.tmp'
  SGDDIR = DATADIR+'/SEGD/site'+SITENU+'/line'+LINENU
  SGYDIR = DATADIR+'/SEGY/site'+SITENU
  SGYFIL = SGYDIR+'/line'+LINENU+'.sgy'
  LOGDIR = DATADIR+'/LOG/sioseis'
  LOGFIL = LOGDIR+'/site'+SITENU+'_line'+LINENU+'_sioseis.log'
  PSHFIL = DATADIR+'/tmp/realtime_'+str(MASTERID)
  end1 = SGDDIR+'/closing'
  end2 = SGDDIR+'/in'

#------------------------------------------
# CHECK FOR EXISTING PATHS, FILES AND DIRS
  print '\nChecking for directories'
  for directory in (SGDDIR, SGYDIR, LOGDIR):
    if not os.path.exists(directory):
      os.makedirs(directory)
  if os.path.exists(LOGFIL):
    a = 0
    while os.path.exists(LOGFIL):
      LOGFIL = LOGDIR+'/site'+SITENU+'_line'+LINENU+'_sioseis'+'.'+str(a)+'.log'
      SGYFIL = SGYDIR+'/line'+LINENU+'.'+str(a)+'.log'
      a = a + 1
  print 'Sioseis log will be written to:\n'+LOGFIL
  print '\nSEGY file will be: ' + SGYFIL

  RUN = DATADIR+'/tmp/tmp_'+str(MASTERID)
  if not os.path.exists(DATADIR+'/tmp'):
    os.makedirs(DATADIR+'/tmp')
  R = open (RUN, 'w')
  R.close()

#-------------------------
# RSYNC
  def sync():
   sgds = []
   DIFF = []
   while MASTERID == os.getppid() and os.path.exists(RUN):    
     DIFF = filecmp.dircmp(SRCDIR+'/', SGDDIR+'/').left_only
     if len(DIFF) == 0:
       time.sleep(1)
       DIFF = filecmp.dircmp(SRCDIR+'/', SGDDIR+'/').left_only
     if DIFF:
       for i in DIFF:
         sgds.insert(0, i+'\n')
         shutil.copy2(SRCDIR+'/'+i, SGDDIR+'/')
       f = open (PSHFIL, 'w')
       for i in sgds [0:2]:
         f.write(str(i))
       f.close()
       time.sleep(1)
   if os.path.exists(PSHFIL):
     a = open (end1, 'w')
     a.close()
     sgds.insert(0, end1+'\n')
     f = open (PSHFIL, 'r+')
     for i in sgds[0:2]:
       f.write(str(i))
     f.close()
     time.sleep(3)
   sioclose = os.system('echo "-1" > '+end2)
   os._exit(0)

#-------------------------
# EXECUTING PROCESSES
  P = open(PROCIDS, 'w')
  P.write(str(os.getpid())+'\n')
  RPID = os.fork()
  if RPID == 0:
    sync()
  else:
    P.write(str(RPID)+'\n')
    print '\nStarted backup with PID: ', str(RPID)
  SPID = os.fork()
  if SPID == 0:
    os.chdir(SGDDIR)
    while MASTERID == os.getppid() and os.path.exists(RUN):
      if os.path.exists(PSHFIL):
        siorun = os.system('python.sgy.csh '+SGYFIL+' '+PSHFIL+' '+RECLEN+' > '+LOGFIL)
      else:
        time.sleep(1)
      time.sleep(2) #remove after test
    os._exit(0)
  else:
    P.write(str(SPID)+'\n')
    print 'Started SIOseis with PID: ', str(SPID)
    print 'Standing by for first file...'
  P.close()
  time.sleep(3)

#--------------------------
# EXITING PROCESSES 
  CONT = raw_input("Enter 'N' for new site or line, enter 'Q' to quit all processes\n")
  while CONT != 'N'and CONT !='n' and CONT != 'Q' and CONT != 'q':
    CONT = raw_input("Please enter 'N' or 'Q' to continue\n")
  os.remove(RUN)
  print 'waiting for all processes to close...\n'
  os.waitpid(0, 0)
  if os.path.exists(end1):
    os.remove(end1)
  while os.path.exists(end2):
    print 'still waiting on SIOseis...'
    time.sleep(5) 

print 'All Done, Goodbye'
sys.exit()