{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "complimentary-direction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Created by Sumit Kumar on 2020-03-08 && modified by Xisco on 2021-04\n",
    "Last modified:\n",
    "\"\"\"\n",
    "\n",
    "import os, sys, numpy, glob, argparse\n",
    "from datetime import date\n",
    "import subprocess\n",
    "from subprocess import call\n",
    "import re\n",
    "\n",
    "today = date.today()\n",
    "date = today.strftime(\"%Y%m%d\")\n",
    "\n",
    "runname='run_0_mock'\n",
    "nmax = 0\n",
    "config_file ='config_n0_to_1_mock.ini'\n",
    "\n",
    "######\n",
    "times = '(0 0.2 0.4 0.8 1.2 2 2.5 5 7.5 10 12 15 18 20)'\n",
    "accounting_group = 'cbc.test.pe_ringdown'\n",
    "cpus=8\n",
    "npoints = 2000\n",
    "req_memory=\"16GB\"\n",
    "not_user='frjifo@aei.mpg.de'\n",
    "pythonfile='/work/francisco.jimenez/sio/git/rdstackingproject/code_new/RD_Fits.py'\n",
    "pythonscript='/work/francisco.jimenez/venv/bin/python'\n",
    "#######################################################\n",
    "\n",
    "pwd = os.getcwd()\n",
    "run_dir = '%s/%s'%(pwd,runname)\n",
    "logs_dir = '%s/logs'%run_dir\n",
    "\n",
    "os.system('mkdir -p %s'%logs_dir)\n",
    "os.system('cp %s %s/'%(config_file,run_dir))\n",
    "\n",
    "###########################################################################\n",
    "# Creating Condor submit file\n",
    "###########################################################################\n",
    "filename1 = '%s/%s'%(run_dir,'condor_submit')\n",
    "text_file1 = open(filename1 + \".sub\", \"w\")\n",
    "text_file1.write(\"universe   = vanilla\\n\")\n",
    "text_file1.write(\"getenv     = true\\n\")\n",
    "text_file1.write(\"# run script -- make sure that condor has execute permission for this file (chmod a+x script.py)\\n\")\n",
    "text_file1.write(\"executable = \"'%s/%s'%(run_dir,runname+'.sh \\n'))\n",
    "text_file1.write(\"# file to dump stdout (this directory should exist)\\n\")\n",
    "text_file1.write(\"output     = %s/%s-$(Process).out\\n\"%(logs_dir,runname))\n",
    "text_file1.write(\"# file to dump stderr\\n\")\n",
    "text_file1.write(\"error     = %s/%s-$(Process).err\\n\"%(logs_dir,runname))\n",
    "text_file1.write(\"# condor logs\\n\")\n",
    "text_file1.write(\"log     = %s/%s-$(Process).log\\n\"%(logs_dir,runname))\n",
    "text_file1.write(\"initialdir = %s \\n\"%run_dir)\n",
    "text_file1.write(\"notify_user =\"+not_user+' \\n')\n",
    "text_file1.write(\"notification = Complete\\n\")\n",
    "text_file1.write('''arguments  = \"-processid $(Process)\" \\n''')\n",
    "text_file1.write(\"request_memory = \"+str(req_memory)+\"\\n\")\n",
    "text_file1.write(\"request_cpus = \"+str(cpus)+\"\\n\") \n",
    "text_file1.write(\"on_exit_remove = (ExitBySignal == False) || ((ExitBySignal == True) && (ExitSignal != 11))\\n\")\n",
    "text_file1.write(\"accounting_group = %s\\n\"%accounting_group)\n",
    "text_file1.write(\"queue 1\\n\")\n",
    "text_file1.write(\"\\n\")\n",
    "text_file1.close()\n",
    "\n",
    "###########################################################\n",
    "# Creating python executable file\n",
    "############################################################\n",
    "filename2 = run_dir+'/'+runname+'.sh'\n",
    "text_file2 = open(filename2, \"w\") \n",
    "text_file2.write(\"#! /bin/bash \\n\")\n",
    "text_file2.write(\"\\n\")\n",
    "text_file2.write(\"times=\"+times+\"\\n\")\n",
    "text_file2.write(\"config_file=\"+config_file+\"\\n\")\n",
    "text_file2.write(\"pythonfile=\"+pythonfile+\"\\n\")\n",
    "text_file2.write(\"pythonscript=\"+pythonscript+\"\\n\")\n",
    "text_file2.write(\"\\n\")\n",
    "text_file2.write(\"for i in ${times[@]}; do\\n\")\n",
    "text_file2.write(\"   awk -v  a=\\\"$i\\\" '/^tshift/ && $3 != \\\"supplied\\\" { $3=a } { print }' $config_file > tmp && mv tmp $config_file\\n\")\n",
    "text_file2.write(\"   $pythonscript $pythonfile -c $config_file \\n\")\n",
    "text_file2.write(\"done\\n\")\n",
    "text_file2.write(\"awk -v  a=\\\"0\\\" '/^tshift/ && $3 != \\\"supplied\\\" { $3=a } { print }' $config_file > tmp && mv tmp $config_file \\n\")\n",
    "text_file2.write(\"\\n\")\n",
    "text_file2.close()\n",
    "os.system('chmod u+x %s'%filename2)\n",
    "\n",
    "os.system('cp '+runname+'.sh %s/'%run_dir)\n",
    "os.system('chmod u+x ./'+runname+'.sh')\n",
    "os.system('cd '+run_dir)\n",
    "\n",
    "###########################################################\n",
    "# Checking the configuration file and adding some important replacements\n",
    "############################################################\n",
    "\n",
    "filename3 = '%s/%s'%(run_dir,config_file)\n",
    "# change the number of cores\n",
    "bashCommand = \"awk -v  a=\"+str(cpus)+\" '/^nb_cores/ && $3 != \\\"supplied\\\" { $3=a } { print }' \"+str(config_file)+\" > tmp && mv tmp \"+str(config_file);\n",
    "subprocess.call(bashCommand,shell=True)\n",
    "\n",
    "filename3 = '%s/%s'%(run_dir,config_file)\n",
    "# change the number of nmax\n",
    "bashCommand = \"awk -v  a=\"+str(nmax)+\" '/^nmax/ && $3 != \\\"supplied\\\" { $3=a } { print }' \"+str(config_file)+\" > tmp && mv tmp \"+str(config_file);\n",
    "subprocess.call(bashCommand,shell=True)\n",
    "\n",
    "filename3 = '%s/%s'%(run_dir,config_file)\n",
    "# change the number of nmax\n",
    "bashCommand = \"awk -v  a=\"+str(npoints)+\" '/^npoints/ && $3 != \\\"supplied\\\" { $3=a } { print }' \"+str(config_file)+\" > tmp && mv tmp \"+str(config_file);\n",
    "subprocess.call(bashCommand,shell=True)\n",
    "\n",
    "###########################################################\n",
    "# Submit the job\n",
    "############################################################\n",
    "filename4 = '%s/%s'%(run_dir,'condor_submit.sub')\n",
    "bashCommand = ['condor_submit', str(filename4)]\n",
    "process = subprocess.Popen(bashCommand, stdout=subprocess.PIPE,stderr=subprocess.PIPE);\n",
    "output,error = process.communicate()\n",
    "error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "collect-bearing",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}