def main_routine (baseDir="./",configPath="./python/parameter.cfg",generationType="r"):
  print "#################################################################"
  print "#################################################################"
  print "### optimal control #############################################"
  print "### preparation of smallest overlap functional ##################"
  print "#################################################################"
  print "#################################################################"

  ### globals for functional variation
  global time, cavityRead, cavityMemo, \
         nRead, nDown, nUp, nWrite, dimH, \
         cfg, \
         h0, hc_up, hc_down, hs_up, hs_down, hsep, \
         weight_up, weight_down, sepZeroWeight, \
         normUp, normDown, normRead, \
         hole, \
         iteration

  tmpDir=baseDir+"tmp/"
  #  parser.add_argument("--cfg" ,  help="config path")
  ### check for arguments, g: generate data, r: read data ###

  ### read config file ###
  print ("load from config file: " + configPath)

  configParser = cp.ConfigParser()
  configParser.read(configPath)
  print (configParser.sections())
  cfg=configParser.__dict__['_sections'].copy() 

  #for src, target in cfg['NVSETUP'].items():
  #  print(src + " : " + target)

  nRead =int(cfg['OCFourier']['{read_harmonic}'])
  nWrite=int(cfg['OCFourier']['{write_harmonic}'])
  nDown =nRead+nWrite
  nUp   =nDown+nWrite
  nTimeRead =int(cfg['OCTime']['{read_timecnt}'])
  dimH      =nRead + 2*nWrite

  normRead =float(cfg['OCConstraints']['{amplitude_read}']) 
  normDown =float(cfg['OCConstraints']['{amplitude_down}']) 
  normUp   =float(cfg['OCConstraints']['{amplitude_up}']) 

  name_readwrite = IOHelper.getNameReadWrite(**cfg)
  name_vector    = IOHelper.getVectorOverlap(**cfg)
  ### read config file ###


  ### prepare data with fortran ###
  cmd = "mkdir -p " + tmpDir
  print (tmpDir)
  call(cmd.split())
  replace_in_file('./python/py.parNvCenter.F95'       , tmpDir +'parNvCenter.F95',        **cfg['NVSETUP'])
  replace_in_file('./python/py.parSmallestOverlap.F95', tmpDir +'parSmallestOverlap.F95', **cfg['OCFourier'])
  replace_in_file(tmpDir +'parSmallestOverlap.F95', tmpDir +'parSmallestOverlap.F95',     **cfg['OCConstraints'])
  replace_in_file(tmpDir +'parSmallestOverlap.F95', tmpDir +'parSmallestOverlap.F95',     **cfg['OCTime'])
  replace_in_file(tmpDir +'parSmallestOverlap.F95', tmpDir +'parSmallestOverlap.F95',     **cfg['FILES'])

  #write config file
  with open(cfg['FILES']['{prefix}']+"parameter.cfg", 'wb') as configfile:
    configParser.write(configfile)

  cmd = "mv "+tmpDir+"parSmallestOverlap.F95 "+baseDir+"srcOptCntrl/parSmallestOverlap.F95"
  call(cmd.split())
  cmd = "mv "+tmpDir+"parNvCenter.F95 "+baseDir+"srcNv/parNvCenter.F95"
  call(cmd.split())

  print ("compile fortran routines")
  cmd = "./scripts/ifort-generateHarmonics.sh " + baseDir
  call(cmd.split())

  print ("invoke " +baseDir +"generateHarmonics")

  cmd = baseDir+"generateHarmonics"
  generateHarmonics = Popen(cmd.split(), stdin=PIPE)
  cmd = "echo " + generationType
  generateInput     = Popen(cmd.split(), stdout=generateHarmonics.stdin)
  output            = generateHarmonics.communicate()[0]
  generateInput.wait()
  ### prepare data with fortran ###


  ### read data for functional variation ###
  __,cavityMemo,cavityRead =IOHelper.harmonics_readwrite(**cfg)
  time                     =IOHelper.functionaltimes_readwrite(**cfg)

  h0,hs_down,hs_up,hc_down,hc_up,hsep=IOHelper.read_MtrxOverlap(**cfg['FILES']) 
  hole                               =IOHelper.read_HoleData(**cfg['FILES']) 
  ### read data for functional variation ###


  ### functional variation ###
  print ("\nstart minimization: ")

  varMax = 20
  varStep = 1
  success = False

  #constraints for constraintWeightDown and constraintWeightUp
  funcTime  =float(time['tfunc'])
  weight_down=float(cfg ['OCConstraints']['{temporal_weight_down}'])*funcTime/2.0
  weight_up  =float(cfg ['OCConstraints']['{temporal_weight_up}'])*funcTime/2.0

  minContraints= ( {'type' : 'ineq', 'fun'  : constraintNormRead },
                   {'type' : 'eq', 'fun'  : constraintNormDown },
                   {'type' : 'eq', 'fun'  : constraintNormUp },
                   {'type' : 'eq', 'fun'  : constraintWeightDown },
                   {'type' : 'eq', 'fun'  : constraintWeightUp },
#                   {'type' : 'eq', 'fun'  : constraintRealMaxCoefficient },
                   {'type' : 'ineq', 'fun'  : constraintZeroStartDown },
                   {'type' : 'ineq', 'fun'  : constraintZeroStartUp },
                   {'type' : 'ineq', 'fun'  : constraintPartialOverlap },
                   {'type' : 'ineq', 'fun'  : constraintStoreUp },
                   {'type' : 'ineq', 'fun'  : constraintStoreDownIneq },
#
  #                 {'type' : 'ineq', 'fun'  : constraintSeperateZero },
#                   {'type' : 'eq', 'fun'  : constraintStoreDown },
#                   {'type' : 'eq', 'fun'  : constraintHoleValue },
#                   {'type' : 'eq', 'fun'  : constraintHoleSlope },
#                   {'type' : 'eq', 'fun'  : constraintHoleCurv },
                 )


  while varStep < varMax and not success :
    gamma0=initGamma0()
    x,x0  =initx(gamma0)

  #    print (" calculate sepzero to initialize vector x (method=SLSQP):")
  #    iteration=1
  #    res=minimize(seperateZeroStore, #seperateZero, # smallestAbsoluteOverlap,
  #                 x0, #res.x, 
  #                 method='SLSQP',
  #                 constraints=sepzeroContraints,
  #                 tol=cfg['OCConstraints']['{tol_sepzero}'],
  #                 options={'maxiter' : 25000, 'disp' : True},
  #                 callback=monitor
  #               )

  #    x0=res.x
  #    sepZeroWeight = res.fun

  #    funcTime  =float(time['tfunc'])
  #    weight=float(cfg ['OCConstraints']['{temporal_weight_limit}'])
  #    sepZeroWeight=weight*funcTime/2e0

    print (" calculate smallest overlap of absolute values (method=SLSQP):")
    iteration=1
    res=minimize(seperateZero, #smallestOverlapSeperateZero, # smallestOverlapSeperatePeaks, #smallestComplxOverlap, # smallestAbsoluteOverlap, # smallestOverlapSeperateZeroPartial, #
                 x0, 
                 method='SLSQP',
                 constraints=minContraints,
                 tol=cfg['OCConstraints']['{tol_classic}'],
                 options={'maxiter' : 25000, 'disp' : True},
                 callback=monitor
               )

    success=res.success
    gamma=getGamma(res.x)

    print ("")    
    print (" current norm(aplhaR) = " +str(sp.linalg.norm(gamma[     :nRead])))
    print (" current norm(aplhaD) = " +str(sp.linalg.norm(gamma[nRead:nDown])))
    print (" current norm(aplhaU) = " +str(sp.linalg.norm(gamma[nDown:nUp  ])))
    print (" current partial Olap = " +str(partialOverlap(res.x)))
#    print (" current hole(value)  = " +str(constraintHoleValue(res.x)))
#    print (" current hole(slope)  = " +str(constraintHoleSlope(res.x)))
#    print (" current hole(value)  = " +str(constraintHoleCurv(res.x)))

   
    varStep+=1

  if (success):
    print ("\ndone with minimization, succeeded:")
  else:
    print ("\ndone with minimization, no success:")

  sp.savetxt(name_vector,sp.array([gamma.real,gamma.conj().imag]).T) 
                             # [real(gamma), imag(gamma)].conj()

  cmd=baseDir+"generateOptimized"
  call(cmd.split())
def main_routine (wd="./",cfg="./python/parameter.cfg",generationType="p",\
                  toMinimize =2,\
                  cavityMatch=1,\
                  silent=0,\
                  useBeta=0,\
                  cutoff=10000,dimGrid=11,id0=0,id1=1,id2=2,pltId='funval',pltLim=1000,pltBins=40,myCmap="brg"):
  print "#################################################################"
  print "#################################################################"
  print "### optimal control #############################################"
  print "### memory pulse evaluation #####################################"
  print "#################################################################"
  print "#################################################################"

  ### globals for functional variation
  global cons, fun, dim, conf

  conf['toMinimize'] =toMinimize
  conf['cavityMatch']=cavityMatch
  conf['silent']     =silent
  if (useBeta == 0):
    conf['useBeta']    = False
  else:
    conf['useBeta']    = True

  conf['id0']        =id0
  conf['id1']        =id1
  conf['id2']        =id2
  conf['cutoff']     =cutoff

  ### globals for functional variation

  ### generate working environment ###
  print ("### working directory: " + wd)
  tmpDir = wd+"tmp/"  
  cmd = "mkdir -p " + tmpDir
  call(cmd.split())
  ### generate working environment ###

  ### read config file ###
  print ("### load config file: " + cfg)
  configParser = cp.ConfigParser()
  configParser.read(cfg)
  print (configParser.sections())
  cfg=configParser.__dict__['_sections'].copy() 

  conf['MEConstraints'] = cfg['MEConstraints'].copy()
  conf['FITNESS']=  cfg['FITNESS'].copy()
  conf['FITNESS']['{mutationrate}']=sp.zeros([conf['entries']])
  conf['FITNESS']['{mutationrate}'][conf['funval']]       =cfg['FITNESS']['{mut_functional}']
  conf['FITNESS']['{mutationrate}'][conf['fidelity_down']]=cfg['FITNESS']['{mut_fidelity_down}']
  conf['FITNESS']['{mutationrate}'][conf['fidelity_up']]  =cfg['FITNESS']['{mut_fidelity_up}']
  conf['FITNESS']['{mutationrate}'][conf['memolap']]      =cfg['FITNESS']['{mut_memolap}']
  conf['FITNESS']['{mutationrate}'][conf['alpha']]        =cfg['FITNESS']['{mut_alpha}']
  conf['FITNESS']['{mutationrate}'][conf['beta']]         =cfg['FITNESS']['{mut_beta}']
  conf['FITNESS']['{mutationrate}'][conf['success']]      =cfg['FITNESS']['{mut_success}']

  cons['alpha_norm']=float(cfg['MEConstraints']["{storage_amplitude}"])
  cons['beta_low']  =float(cfg['MEConstraints']["{limit_low_beta}"])
  cons['beta_top']  =float(cfg['MEConstraints']["{limit_top_beta}"])
  cons['chi2_Tol']  =float(cfg['MEConstraints']['{tol_chi2}'])

  dim['alpha']=int(cfg['MEFourier']['{storage_harmonic}'])
  dim['total']=dim['alpha']+3

  prefix        =cfg['FILES']['{prefix}']
  postfix       =cfg['FILES']['{postfix}']
  name_optimized=cfg['FILES']['{name_optimized}']

  name_spin     =cfg['FILES']['{name_spin}']
  name_cavity   =cfg['FILES']['{name_cavity}']

  cfg['dim_grid']    =dimGrid
  name_readwrite=IOHelper.getNameReadWrite(**cfg) 
  name_storage  =IOHelper.getNameStorage  (**cfg) 
  name_varinit  =IOHelper.getNameInitialVariation (**cfg)

  myTime        =IOHelper.functionaltimes_readwrite(**cfg) # reads time and updates cfg: 
                                                           #   cfg['METime']['{fidelity_ti}'] = myTime['idx_ti']
                                                           #   cfg['METime']['{fidelity_tf}'] = myTime['idx_tf']


  gridPhi    =sp.linspace(0.0, 2.0*sp.pi, num=2*dimGrid-1)
  gridTheta  =sp.linspace(0.0, sp.pi, num=dimGrid)
  minfun     =sp.zeros([len(gridPhi),len(gridTheta),conf['entries']+3])

  if (generationType == "p"):
    print "## read from file: " + name_varinit
    raw = sp.loadtxt(name_varinit)
    minfun  = raw.reshape(len(gridPhi),len(gridTheta),conf['entries']+3)
    for i in sp.arange(0,len(gridPhi),1):
      for j in sp.arange(0,len(gridTheta),1):
        minfun[i,j,conf['fitness']] = MemoryPulseFunctional.fitnessFunction(minfun[i,j,:])

  else:
    ### prepare and complie fortran routines ###
    print ("### prepare fortran routines")
    replace_in_file('./python/py.parNvCenter.F95'   , tmpDir +'parNvCenter.F95'   , **cfg['NVSETUP'])
    replace_in_file('./python/py.parMemoryPulse.F95', tmpDir +'parMemoryPulse.F95', **cfg['MEFourier'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['OCFourier'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['MESpin'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['MEConstraints'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['OCConstraints'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['METime'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['OCTime'])
    replace_in_file(tmpDir +'parMemoryPulse.F95'    , tmpDir +'parMemoryPulse.F95', **cfg['FILES'])

    #write config file
    with open(prefix+"parameter.cfg", 'wb') as configfile:
      configParser.write(configfile)
    ### read config file ###

    print ("### compile fortran routines")
    cmd = "mv "+tmpDir+"parMemoryPulse.F95 "+wd+"srcOptCntrl/parMemoryPulse.F95"
    call(cmd.split())
    cmd = "mv "+tmpDir+"parNvCenter.F95 "+wd+"srcNv/parNvCenter.F95"
    call(cmd.split())

    cmd = "./scripts/ifort-memoryHarmonics.sh " + wd
    call(cmd.split())

    print ("### invoke  fortran routines")
    print ("### generation Type: " + generationType)
    cmd = wd+"memoryHarmonics"                                             # location of executable fortran program
    generateHarmonics = Popen(cmd.split(), stdin=PIPE)                     # run fortran program with piped standard input
    cmd = "echo " + generationType                                         # communication with fortran-routine: chose action -> read or generate
    generateInput     = Popen(cmd.split(), stdout=generateHarmonics.stdin) # send action to fortran program
    output            = generateHarmonics.communicate()[0]
    generateInput.wait()
    ### prepare and complie fortran routines ###

    ### read data for functional variation ###
    cons['cavityT2_down'], \
    cons['cavityT2_up']    = IOHelper.read_CavityMemory (**cfg['FILES'])

    fun ['mtrxBeta_up'],   \
    cons['vecT2_up']   ,   \
    cons['fidelity_up'],   \
    cons['mtrxMemOlap']    = IOHelper.read_MtrxMemory("up",  **cfg['FILES']) 

    fun ['mtrxBeta_down'], \
    cons['vecT2_down']   , \
    cons['fidelity_down'], \
    __                     = IOHelper.read_MtrxMemory("down",  **cfg['FILES']) 
    ### read data for functional variation ###

    ### functional variation ###
    print ("\n### start minimization: ")
    print ("### on initial "+ str(sp.shape(minfun)[0])+"x"+str(sp.shape(minfun)[1])  + "-grid (phi,theta) on sphere")

    mincnt=0
    cntPhi  =0
    t0=0
    tcnt=len(gridTheta)

    for phi in gridPhi[0:len(gridPhi)-1]: 
      theta_i=0
      if(cntPhi > 0):
        t0=1
        tcnt=len(gridTheta)-1
        theta_i=1

      ### parallel evaluation with <cpu_count()> cores ###################################
      pool    = multiprocessing.Pool(processes=multiprocessing.cpu_count())

      results = [ pool.apply_async(evaluateSphericalVariation, args=(phi,theta,cntPhi,cntTheta))
                 for theta,cntTheta in zip(gridTheta[t0:tcnt],sp.arange(t0,tcnt,1)) ]

      for p in results:
        myResult,i,j =p.get()
        minfun[i,j,:]=myResult

      #kill multiprocessing pool !!!
      pool.terminate()
      ### parallel evaluation with <cpu_count()> cores ###################################


      ### sequentiell evaluateion 1 core #################################################
#      for theta in gridTheta[t0:tcnt]:
#        minfun[cntPhi,theta_i,:],__,__=MemoryPulseFunctional.evaluateSphericalVariation (phi,theta,cntPhi,theta_i)
#        theta_i+=1
      ### sequentiell evaluateion 1 core #################################################

      cntPhi=cntPhi+1
  #  end for phi

    minfun[ :, 0,:]=minfun[0, 0,:] # theta=0  : same vector on unit-sphere
    minfun[ :,-1,:]=minfun[0,-1,:] # theta=pi : same vector on unit-sphere
    minfun[-1, :,:]=minfun[0, :,:] # periodic

    print "\n### write to file: " + name_varinit
    sp.savetxt(name_varinit,minfun.reshape((len(gridPhi)*len(gridTheta),conf['entries']+3)),\
       header=' alpha0['+str(id0)+']; alpha0['+str(id1)+']; alpha0['+str(id2)+'];'\
             +' fitness; success; norm[alpha]; abs[beta]; memolap; fidelity_up; fidelity_down; minfun') 

  print "#################################################################"
  print "#################################################################"


  font = {
  'fontsize'            : 26,
  }

  mytitle=MemoryPulseFunctional.getName(pltId)

  ### prepare surface_plot ###############################################################
  x = outer(cos(gridPhi), sin(gridTheta))
  y = outer(sin(gridPhi), sin(gridTheta))
  z = outer(ones(size(gridPhi)), cos(gridTheta))

  myDensity=sp.zeros([len(gridPhi),len(gridTheta)])
  for i in sp.arange(0,len(gridPhi),1):
    for j in sp.arange(0,len(gridTheta),1):
      if (minfun[i,j,conf[pltId]] >= pltLim):
        myDensity[i,j] = pltLim+1
      else:
        myDensity[i,j] = minfun[i,j,conf[pltId]]

      
  # "afmhot", "hot", "terrain", "brg"
  cm       = plt.cm.get_cmap(myCmap) 
  myColors = cm(myDensity/pltLim)
  ### prepare surface_plot ###############################################################

  ### prepare colormap ###################################################################
  fig0_colors=sp.linspace(0,pltLim,pltBins)
  ### prepare colormap ###################################################################

  ### prepare histogram ##################################################################
  yHist,xHist = sp.histogram(myDensity.flatten(),pltBins)
#  xSpan = pltxHist.max()-xHist.min()
#  Color = [cm(((ix-xHist.min())/xSpan)) for ix in xHist]

  Color = [cm(((ix)/pltLim)) for ix in xHist]
  ### prepare histogram ##################################################################

  ### calculate fittest solutions ########################################################
  cntFit = 0

  for i in sp.arange(0,len(gridPhi),1):
    for j in sp.arange(0,len(gridTheta),1):

      if minfun[i,j,conf['fitness']] != 0.0 :
        cntFit+=1
      
      if (minfun[i,j,conf[pltId]] >= pltLim):
        myDensity[i,j] = pltLim
      else:
        myDensity[i,j] = minfun[i,j,conf[pltId]]
      
  if (cntFit > 0):
    fitAlpha =sp.zeros([cntFit,int(cfg['MEFourier']['{storage_harmonic}'])])  

    cntFit = 0
  
    for i in sp.arange(0,len(gridPhi),1):
      for j in sp.arange(0,len(gridTheta),1):
        if minfun[i,j,conf['fitness']] != 0.0 :
          print minfun[i,j,3:]
  
          fitAlpha[cntFit,conf['id0']]=minfun[i,j,0]
          fitAlpha[cntFit,conf['id1']]=minfun[i,j,1]
          fitAlpha[cntFit,conf['id2']]=minfun[i,j,2]

          cntFit+=1

    name_fittest=IOHelper.getNameInitialFittest(**cfg)
    print "\n### write fittest to file: " + name_fittest
    sp.savetxt(name_fittest,fitAlpha,\
       header='# alpha0[0] ... alpha0[n]') 

  print "### fitness counter: {0:} of {1:}".format(cntFit,sp.shape(minfun)[0]*sp.shape(minfun)[1])
  ### calculate fittest solutions ########################################################


  fig = plt.figure()


  ### plot surface_plot ##################################################################
  fig3D = fig.add_subplot(131, projection='3d')
  surf  = fig3D.plot_surface(x, y, z, rstride=1, cstride=1,facecolors=myColors)
  fig3D.plot(cos(gridPhi), sin(gridPhi), zs=0, zdir='z',lw=0.5, color="black")
  fig3D.plot(cos(gridPhi), sin(gridPhi), zs=0, zdir='x',lw=0.5, color="black")
  fig3D.set_xlabel("$\\alpha_{:}$".format(id0), **font)
  fig3D.set_ylabel("$\\alpha_{:}$".format(id1), **font)
  fig3D.set_zlabel("$\\alpha_{:}$".format(id2), **font)
  fig3D.set_title("a) 4d-plot of "+mytitle, fontsize = 20)
  ### plot surface_plot ##################################################################


  ### plot colormap ######################################################################
  fig0 = fig.add_subplot(132)
  fig0.invert_yaxis()
  cp0=fig0.contourf(gridPhi/sp.pi,gridTheta/sp.pi,minfun[:,:,conf[pltId]].T,fig0_colors,cmap=cm)
  plt.colorbar(cp0)
  fig0.set_xlabel("$\phi/\pi$", **font)
  fig0.set_ylabel("$\\theta/\pi$", **font)
  fig0.set_title("b) map of "+mytitle, fontsize = 20)
  ### plot colormap ######################################################################


  ### plot histogram #####################################################################
  figBar=fig.add_subplot(133)
  figBar.bar(xHist[:-1],yHist,color=Color,width=xHist[1]-xHist[0])
  figBar.set_xlim([0,pltLim])
  figBar.set_ylim([0,max(yHist[0:len(yHist)-1])])
  figBar.set_xlabel(mytitle, fontsize=20)
  figBar.set_ylabel("count", fontsize=20)
  figBar.set_title("c) histogram", fontsize = 20)
  ### plot histogram #####################################################################

  plt.show()