text: import os,sys
  import numpy as np
  
  ## simple file to process output list from runBatch.py. 
  # Does not go back to original FAST output.
  # expects a file that has
  # line 1: header, ie list of fields
  # rest of lines: values of corresponding fields.
  
  # If file has a probability field, can be used for cartesian grid integration for E(x)
  # If file comes from a runcase batch generated by sampling, then E(x) is just 1/N * sum(x)
  
  def get_options():
      from optparse import OptionParser
      parser = OptionParser()    
      parser.add_option("-i", "--input", dest="main_input",  type="string", default="runbatch.out",  help="results file to process")
      parser.add_option("-o", "--output", dest="main_output",  type="string", default="runbatch.res",  help="file for processed results")
      parser.add_option("-f", "--field_name", dest="field_name",  type="string", default=None,  help="field of interest, by name")
      parser.add_option("-n", "--field_idx", dest="field_idx", help="field of interest, by 0-based index", type="int", default=None)
      parser.add_option("-a", "--doall", dest="do_all", help="process all field, and cumulatively", action="store_true", default=False)
  #    parser.add_option("-s", "--start_at", dest="start_at", help="index of sample to start at", type="int", default=0)
              
      (options, args) = parser.parse_args()
      return options, args
  
  def find_field(name, hdr):
      for i in range(len(hdr)):
          if name == hdr[i]:
              return i
      print "field %s not found in input" % name
      sys.exit()
  
  def dosum_once(dat, field_idx):
      tot = 0
      N = len(dat)
      for i in range(N):
         tot += dat[i][field_idx]
      tot/=N
      return tot
  
  
  def dosum_cumulative(dat):
      N = len(dat)
      M = len(dat[0])
      xtot = np.zeros(M)
      res = []
      for i in range(N):
          xtot += dat[i]
          xnow = xtot/(i+1)
          res.append(xnow)
      res = np.array(res)
      return res
  
  """
      for i in range():
          y = [fsamplines[i][j] for j in range(dim)]
          x = [y[0],pi/180. * y[3], y[1],y[2]]
          x = np.array(x)
  #        x = np.array(xx[i])
          if (dim == 1):
              x = np.array([x])
          val = fsamplines[i][20]  ### NOTE exact field of interest!
          lsum += val
          if (check_prob):
              prob = sctx.calc_prob(x)
              print i, x, "   %e" %( prob)
          if (i > 0 and i % incr == 0):
              vals = fsamplines[0:i]
              vals = [vals[j][20] for j in range(i)]
              sd = np.std(vals)
              est = lsum/float(i)
              err = sd/np.sqrt(i)
              print i, est, sd, err, est+err, est-err
  
      lsum /= nsample
      return lsum
  """
  
  def process_results():
      options, args = get_options()
      fname = options.main_input
      lines = file(fname).readlines()  ## TODO: might by big
      hdr = lines[0].split()
      dat = []
      for i in range(1,len(lines)):
          ln = lines[i].split()
          ln = np.array([float(f) for f in ln])
          dat.append(ln)
      dat = np.array(dat)
      N = len(dat)
  
      if (not options.do_all and options.field_idx == None and options.field_name == None):
          print "please specify either --all or --field_name or --field_idx"
          sys.exit()
      if (options.do_all):
          do_all = True
          print "summing cumulatively over all fields"
      else:
          if (options.field_idx != None and options.field_name != None):
              print "pleast specify only ONE of --field_name or --field_idx"
              sys.exit()
  
          if (options.field_name != None):
              field_idx = find_field(options.field_name, hdr)
          else:
              field_idx = options.field_idx
  
          field_name = hdr[field_idx]
          print "summing once for field ", field_name, " idx ", field_idx
  
      
      if (do_all):
          res = dosum_cumulative(dat)
          fout = file(options.main_output, "w")
          for i in range(len(hdr)):
              fout.write( "%s " % hdr[i])
          fout.write("\n")
          for i in range(len(dat)):
              for j in range(len(hdr)):
                  fout.write("%e " % res[i][j])
              fout.write("\n")
          fout.close()
          print "wrote cumulative averages for all fields to file %s" % (options.main_output)
          print "There were %d records, %d fields each" % (len(dat), len(hdr))
  
          gpname = "%s.gp" % options.main_output
          fout = file(gpname, "w")
          fout.write("set term png\n")
          for i in range(len(hdr)):
              fout.write("set output 'runbatch-%s.png'\n" % hdr[i])
              fout.write("plot '%s' u :%d w l t '%s'\n" % (options.main_output, i+1, hdr[i]))        
          fout.close()
  
      else:
          tot = dosum_once(dat, field_idx)
          print "E(field %i, ie %s) over %d samples is %e" % (field_idx, field_name, N, tot)
  
  if __name__=="__main__":
      process_results()
  