new_contents: #! /usr/bin/env python
  #
  # This script converts old-style RIPP blocks into new-style DYNK blocks
  #
  
  import sys
  if len(sys.argv) == 1:
      ifilename = "fort.3"
  elif len(sys.argv) == 2:
      ifilename = sys.argv[1]
  else:
      print "Usage: python rippconverter.py (<file to convert>)"
  
  ifile = open(ifilename,'r')
  ofile = open(ifilename+".dynk",'w')
  
  inBlock = False
  blockName = None
  blockLine = 0
  ENDE = False
  
  error = False
  
  for l in ifile:
      if inBlock:
          #print blockLine, "'"+l[:-1]+"'"
          if l[:4] == "NEXT":
              inBlock = False
              blockName = None
              blockLine = 0
              ofile.write(l)
              continue
          
          if blockName == "RIPP":
              print "Converting '"+l[:-1]+"':"
              ls = l.split()
              rName  = ls[0]
              rDepth = ls[1]
              rFreq  = ls[2]
              if len(ls) < 4:
                  print "WARNING: RIPP phase not set for '"+rName+"', defaulting to 0.0"
                  rPhase = "0.0" 
              else:
                  rPhase = ls[3]
              if len(ls) < 5:
                  print "WARNING: RIPP nrturn not set for '"+rName+"', defaulting to 0"
                  rTurn = "0"
              else:
                  rTurn = ls[4]
  
              FUN = "FUN RIPP-"+rName+" COSF_RIPP " + rDepth + " " + rFreq + " " + rPhase
              print FUN
              SET = "SET "+rName+" average_ms RIPP-"+rName+" 1 -1 "+rTurn
              print SET
  
              ofile.write(FUN+"\n")
              ofile.write(SET+"\n")
          else:
              ofile.write(l)
          blockLine = blockLine+1
      else:
          if l[:4] in ("SING","BLOC","STRU","PRIN","DISP","TUNE","SYNC","ITER","FLUC","MULT","CHRO","TRAC","DIFF","LINE","LIMI","ORBI","INIT","COMB","SUBR","RESO","SEAR","ORGA","POST","RIPP","DECO","COMM","NORM","CORR","BEAM","TROM","COLL","DUMP"):
              blockName = l[:4]
              inBlock = True
              blockLine = 1
              #print blockName
              if l[:4] == "RIPP":
                  ofile.write("DYNK (autogenerated from RIPP block by rippconverter.py)\n")
                  ofile.write("/"+ l)
                  ofile.write("NOFILE\n")
              else:
                  ofile.write(l)
          elif l[:4] == "DYNK":
              print "ERROR, DYNK block found?"
              print l
              error=True
              break
          elif l[:4] == "ENDE":
              ofile.write(l)
              ENDE = True
          elif  l[:4] == "GEOM" or l[:4] == "FREE":
              ofile.write(l)
          elif l[:1] == "/":
              ofile.write(l)
          else:
              print "unknown line found outside block:"
              print l
  
  ifile.close()
  ofile.close()
  
  if error:
      print "Failed."
      exit(1)
  
  print "Done!"
  
  print "Showing you 'diff fort.3 fort.3.dynk':"
  from subprocess import call
  call(["diff","fort.3","fort.3.dynk"])
  
subject: Copy rippconverter.py from kyrsjo/SixtrackTools