From 38f98a7c3c772725e447ec6bbf908db283b0c158 Mon Sep 17 00:00:00 2001
From: David Robert Verelst <dave@dtu.dk>
Date: Sat, 6 Oct 2018 14:45:20 +0200
Subject: [PATCH] prepost: small clean-up regarding win64 optional interface

---
 wetb/prepost/Simulations.py                   | 12 +++----
 wetb/prepost/dlctemplate.py                   | 33 ++++++++++---------
 wetb/prepost/simchunks.py                     | 23 +++++++------
 .../ref/zip-chunks-gorm/remote_chnk_00000.p   |  4 +--
 .../ref/zip-chunks-jess/remote_chnk_00000.p   |  4 +--
 5 files changed, 37 insertions(+), 39 deletions(-)

diff --git a/wetb/prepost/Simulations.py b/wetb/prepost/Simulations.py
index 900eefb..c576989 100755
--- a/wetb/prepost/Simulations.py
+++ b/wetb/prepost/Simulations.py
@@ -563,7 +563,7 @@ def prepare_launch(iter_dict, opt_tags, master, variable_tag_func,
                 write_htc=True, runmethod='none', verbose=False,
                 copyback_turb=True, msg='', silent=False, check_log=True,
                 update_cases=False, ignore_non_unique=False,
-                run_only_new=False, windows_nr_cpus=2, wine_64bit=False,
+                run_only_new=False, windows_nr_cpus=2,
                 pbs_fname_appendix=True, short_job_names=True, qsub='',
                 update_model_data=True, maxcpu=1, pyenv='wetb_py3',
                 m=[3,4,6,8,9,10,12], postpro_node_zipchunks=True,
@@ -809,7 +809,7 @@ def prepare_launch(iter_dict, opt_tags, master, variable_tag_func,
            copyback_turb=copyback_turb, qsub=qsub,
            windows_nr_cpus=windows_nr_cpus, short_job_names=short_job_names,
            pbs_fname_appendix=pbs_fname_appendix, silent=silent, maxcpu=maxcpu,
-           pyenv=pyenv, wine_64bit=wine_64bit, m=[3,4,6,8,9,10,12],
+           pyenv=pyenv, m=[3,4,6,8,9,10,12],
            postpro_node_zipchunks=postpro_node_zipchunks,
            postpro_node=postpro_node, exesingle=exesingle, exechunks=exechunks,
            wine_arch=wine_arch, wine_prefix=wine_prefix, pyenv_cmd=pyenv_cmd)
@@ -1013,7 +1013,7 @@ def prepare_launch_cases(cases, runmethod='gorm', verbose=False,write_htc=True,
 def launch(cases, runmethod='none', verbose=False, copyback_turb=True,
            silent=False, check_log=True, windows_nr_cpus=2, qsub='time',
            pbs_fname_appendix=True, short_job_names=True,
-           maxcpu=1, pyenv='wetb_py3', wine_64bit=False, m=[3,4,6,8,9,10,12],
+           maxcpu=1, pyenv='wetb_py3', m=[3,4,6,8,9,10,12],
            postpro_node_zipchunks=True, postpro_node=False, exesingle=None,
            exechunks=None, wine_arch='win32', wine_prefix='~/.wine32',
            pyenv_cmd='source /home/python/miniconda3/bin/activate'):
@@ -1057,7 +1057,7 @@ def launch(cases, runmethod='none', verbose=False, copyback_turb=True,
         # create the pbs object
         pbs = PBS(cases, short_job_names=short_job_names, pyenv=pyenv,
                   pbs_fname_appendix=pbs_fname_appendix, qsub=qsub,
-                  verbose=verbose, silent=silent, wine_64bit=wine_64bit,
+                  verbose=verbose, silent=silent,
                   m=m, postpro_node_zipchunks=postpro_node_zipchunks,
                   postpro_node=postpro_node, exesingle=exesingle,
                   exechunks=exechunks, wine_arch=wine_arch,
@@ -1951,7 +1951,7 @@ class PBS(object):
 
     def __init__(self, cases, qsub='time', silent=False, pyenv='wetb_py3',
                  pbs_fname_appendix=True, short_job_names=True, verbose=False,
-                 wine_64bit=False, m=[3,4,6,8,9,10,12], exesingle=None,
+                 m=[3,4,6,8,9,10,12], exesingle=None,
                  postpro_node_zipchunks=True, postpro_node=False,
                  exechunks=None, wine_arch='win32', wine_prefix='~/.wine32'):
         """
@@ -1994,8 +1994,6 @@ class PBS(object):
         # run in 32-bit or 64-bit mode. Note this uses the same assumptions
         # on how to configure wine in toolbox/pbsutils/config-wine-hawc2.sh
         wineparam = (wine_arch, wine_prefix)
-        if wine_64bit:
-            wineparam = ('win64', '~/.wine')
         self.winebase = 'time WINEARCH=%s WINEPREFIX=%s ' % wineparam
 
         self.wine = self.winebase + 'wine'
diff --git a/wetb/prepost/dlctemplate.py b/wetb/prepost/dlctemplate.py
index 8f76856..a0cb6ab 100644
--- a/wetb/prepost/dlctemplate.py
+++ b/wetb/prepost/dlctemplate.py
@@ -266,9 +266,9 @@ def variable_tag_func_mod1(master, case_id_short=False):
 def launch_dlcs_excel(sim_id, silent=False, verbose=False, pbs_turb=False,
                       runmethod=None, write_htc=True, zipchunks=False,
                       walltime='04:00:00', postpro_node=False, compress=False,
-                      dlcs_dir='htc/DLCs', wine_64bit=False,
-                      m=[3,4,6,8,9,10,12], postpro_node_zipchunks=True,
-                      wine_arch='win32', wine_prefix='~/.wine32'):
+                      dlcs_dir='htc/DLCs', postpro_node_zipchunks=True,
+                      wine_arch='win32', wine_prefix='~/.wine32',
+                      m=[3,4,6,8,9,10,12]):
     """
     Launch load cases defined in Excel files
     """
@@ -331,11 +331,10 @@ def launch_dlcs_excel(sim_id, silent=False, verbose=False, pbs_turb=False,
                                ignore_non_unique=False, run_only_new=False,
                                pbs_fname_appendix=False, short_job_names=False,
                                silent=silent, verbose=verbose, pyenv=pyenv,
-                               wine_64bit=wine_64bit, m=[3,4,6,8,9,10,12],
+                               m=[3,4,6,8,9,10,12], postpro_node=postpro_node,
+                               exechunks=None, exesingle=None,
                                postpro_node_zipchunks=postpro_node_zipchunks,
-                               postpro_node=postpro_node, exesingle=None,
-                               exechunks=None, wine_arch=wine_arch,
-                               wine_prefix=wine_prefix)
+                               wine_arch=wine_arch, wine_prefix=wine_prefix)
 
     if pbs_turb:
         # to avoid confusing HAWC2 simulations and Mann64 generator PBS files,
@@ -351,15 +350,13 @@ def launch_dlcs_excel(sim_id, silent=False, verbose=False, pbs_turb=False,
         # note that walltime here is for running all cases assigned to the
         # respective nodes. It is not walltime per case.
         sorts_on = ['[DLC]', '[Windspeed]']
-        create_chunks_htc_pbs(cases, sort_by_values=sorts_on, ppn=20,
-                              nr_procs_series=3, walltime='20:00:00',
+        create_chunks_htc_pbs(cases, sort_by_values=sorts_on, queue='workq',
+                              ppn=20, nr_procs_series=3, walltime='20:00:00',
                               chunks_dir='zip-chunks-jess', compress=compress,
-                              queue='workq', wine_64bit=wine_64bit,
                               wine_arch=wine_arch, wine_prefix=wine_prefix)
-        create_chunks_htc_pbs(cases, sort_by_values=sorts_on, ppn=12,
-                              nr_procs_series=3, walltime='20:00:00',
+        create_chunks_htc_pbs(cases, sort_by_values=sorts_on, queue='workq',
+                              ppn=12, nr_procs_series=3, walltime='20:00:00',
                               chunks_dir='zip-chunks-gorm', compress=compress,
-                              queue='workq', wine_64bit=wine_64bit,
                               wine_arch=wine_arch, wine_prefix=wine_prefix)
 
     df = sim.Cases(cases).cases2df()
@@ -674,7 +671,8 @@ if __name__ == '__main__':
                         'generated DLC exchange files, default: htc/DLCs/')
     parser.add_argument('--wine_64bit', default=False, action='store_true',
                         dest='wine_64bit', help='Run wine in 64-bit mode. '
-                        'Only works on Jess.')
+                        'Only works on Jess. Sets --wine_arch and '
+                        '--wine_prefix to win64 and ~/.wine respectively.')
     parser.add_argument('--wine_arch', action='store', default='win32', type=str,
                         dest='wine_arch', help='Set to win32 for 32-bit, and '
                         'win64 for 64-bit. 64-bit only works on Jess. '
@@ -716,6 +714,9 @@ if __name__ == '__main__':
     P_RUN, P_SOURCE, PROJECT, sim_id, P_MASTERFILE, MASTERFILE, POST_DIR \
         = dlcdefs.configure_dirs(verbose=True)
 
+    if opt.wine_64bit:
+        opt.wine_arch, opt.wine_prefix = ('win64', '~/.wine')
+
     if opt.gendlcs:
         DLB = GenerateDLCCases()
         DLB.execute(filename=os.path.join(P_SOURCE, opt.dlcmaster),
@@ -728,9 +729,9 @@ if __name__ == '__main__':
                           pbs_turb=opt.pbs_turb, walltime=opt.walltime,
                           postpro_node=opt.postpro_node, runmethod=RUNMETHOD,
                           dlcs_dir=os.path.join(P_SOURCE, 'htc', 'DLCs'),
-                          compress=opt.compress, wine_64bit=opt.wine_64bit,
                           postpro_node_zipchunks=opt.no_postpro_node_zipchunks,
-                          wine_arch=opt.wine_arch, wine_prefix=opt.wine_prefix)
+                          wine_arch=opt.wine_arch, wine_prefix=opt.wine_prefix,
+                          compress=opt.compress)
     # post processing: check log files, calculate statistics
     if opt.check_logs or opt.stats or opt.fatigue or opt.envelopeblade \
         or opt.envelopeturbine or opt.AEP:
diff --git a/wetb/prepost/simchunks.py b/wetb/prepost/simchunks.py
index abb00f7..73a9ae1 100644
--- a/wetb/prepost/simchunks.py
+++ b/wetb/prepost/simchunks.py
@@ -35,10 +35,11 @@ from wetb.prepost.Simulations import Cases
 
 
 def create_chunks_htc_pbs(cases, sort_by_values=['[Windspeed]'], ppn=20, i0=0,
-                          nr_procs_series=9, queue='workq', pyenv='wetb_py3',
+                          nr_procs_series=9, queue='workq', compress=False,
                           walltime='24:00:00', chunks_dir='zip-chunks-jess',
-                          compress=False, wine_64bit=False, wine_arch='win32',
-                          wine_prefix='~/.wine32'):
+                          wine_arch='win32', wine_prefix='~/.wine32',
+                          pyenv_cmd='source /home/python/miniconda3/bin/activate',
+                          pyenv='wetb_py3'):
     """Group a large number of simulations htc and pbs launch scripts into
     different zip files so we can run them with find+xargs on various nodes.
     """
@@ -164,8 +165,7 @@ def create_chunks_htc_pbs(cases, sort_by_values=['[Windspeed]'], ppn=20, i0=0,
 # """
 
     def make_pbs_chunks(df, ii, sim_id, run_dir, model_zip, compress=False,
-                        wine_64bit=False, wine_arch='win32',
-                        wine_prefix='~/.wine32'):
+                        wine_arch='win32', wine_prefix='~/.wine32'):
         """Create a PBS that:
             * copies all required files (zip chunk) to scratch disk
             * copies all required turbulence files to scratch disk
@@ -178,8 +178,6 @@ def create_chunks_htc_pbs(cases, sort_by_values=['[Windspeed]'], ppn=20, i0=0,
         jobid = '%s_chnk_%05i' % (sim_id, ii)
 
         wineparam = (wine_arch, wine_prefix)
-        if wine_64bit:
-            wineparam = ('win64', '~/.wine')
 
         pbase = os.path.join('/scratch','$USER', '$PBS_JOBID', '')
         post_dir_base = post_dir.split(sim_id)[1]
@@ -218,7 +216,8 @@ def create_chunks_htc_pbs(cases, sort_by_values=['[Windspeed]'], ppn=20, i0=0,
         # activate the python environment
         if pyenv is not None:
             pbs += 'echo "activate python environment %s"\n' % pyenv
-            pbs += 'source /home/python/miniconda3/bin/activate %s\n' % pyenv
+            rpl = (pyenv_cmd, pyenv)
+            pbs += '%s %s\n' % rpl
             # sometimes activating an environment fails due to a FileExistsError
             # is this because it is activated at the same time on another node?
             # check twice if the environment got activated for real
@@ -226,9 +225,9 @@ def create_chunks_htc_pbs(cases, sort_by_values=['[Windspeed]'], ppn=20, i0=0,
             pbs += 'CMD=\"from distutils.sysconfig import get_python_lib;'
             pbs += 'print (get_python_lib().find(\'%s\'))"\n' % pyenv
             pbs += 'ACTIVATED=`python -c "$CMD"`\n'
-            pbs += 'if [ $ACTIVATED -eq -1 ]; then source activate %s;fi\n' % pyenv
+            pbs += 'if [ $ACTIVATED -eq -1 ]; then %s %s;fi\n' % rpl
             pbs += 'ACTIVATED=`python -c "$CMD"`\n'
-            pbs += 'if [ $ACTIVATED -eq -1 ]; then source activate %s;fi\n' % pyenv
+            pbs += 'if [ $ACTIVATED -eq -1 ]; then %s %s;fi\n' % rpl
 
         # =====================================================================
         # create all necessary directories at CPU_NR dirs
@@ -516,8 +515,8 @@ def create_chunks_htc_pbs(cases, sort_by_values=['[Windspeed]'], ppn=20, i0=0,
     for ii, dfi in enumerate(df_iter):
         fname, ind = make_zip_chunks(dfi, i0+ii, sim_id, run_dir, model_zip)
         make_pbs_chunks(dfi, i0+ii, sim_id, run_dir, model_zip,
-                        compress=compress, wine_64bit=wine_64bit,
-                        wine_arch=wine_arch, wine_prefix=wine_prefix)
+                        wine_arch=wine_arch, wine_prefix=wine_prefix,
+                        compress=compress)
         df_ind = df_ind.append(ind)
         print(fname)
 
diff --git a/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-gorm/remote_chnk_00000.p b/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-gorm/remote_chnk_00000.p
index d81898a..2e74bfe 100644
--- a/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-gorm/remote_chnk_00000.p
+++ b/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-gorm/remote_chnk_00000.p
@@ -18,9 +18,9 @@ source /home/python/miniconda3/bin/activate wetb_py3
 echo "CHECK 2x IF wetb_py3 IS ACTIVE, IF NOT TRY AGAIN"
 CMD="from distutils.sysconfig import get_python_lib;print (get_python_lib().find('wetb_py3'))"
 ACTIVATED=`python -c "$CMD"`
-if [ $ACTIVATED -eq -1 ]; then source activate wetb_py3;fi
+if [ $ACTIVATED -eq -1 ]; then source /home/python/miniconda3/bin/activate wetb_py3;fi
 ACTIVATED=`python -c "$CMD"`
-if [ $ACTIVATED -eq -1 ]; then source activate wetb_py3;fi
+if [ $ACTIVATED -eq -1 ]; then source /home/python/miniconda3/bin/activate wetb_py3;fi
 
 echo "----------------------------------------------------------------------"
 cd /scratch/$USER/$PBS_JOBID/
diff --git a/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-jess/remote_chnk_00000.p b/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-jess/remote_chnk_00000.p
index 55f11f2..fa2fb77 100644
--- a/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-jess/remote_chnk_00000.p
+++ b/wetb/prepost/tests/data/demo_dlc/ref/zip-chunks-jess/remote_chnk_00000.p
@@ -18,9 +18,9 @@ source /home/python/miniconda3/bin/activate wetb_py3
 echo "CHECK 2x IF wetb_py3 IS ACTIVE, IF NOT TRY AGAIN"
 CMD="from distutils.sysconfig import get_python_lib;print (get_python_lib().find('wetb_py3'))"
 ACTIVATED=`python -c "$CMD"`
-if [ $ACTIVATED -eq -1 ]; then source activate wetb_py3;fi
+if [ $ACTIVATED -eq -1 ]; then source /home/python/miniconda3/bin/activate wetb_py3;fi
 ACTIVATED=`python -c "$CMD"`
-if [ $ACTIVATED -eq -1 ]; then source activate wetb_py3;fi
+if [ $ACTIVATED -eq -1 ]; then source /home/python/miniconda3/bin/activate wetb_py3;fi
 
 echo "----------------------------------------------------------------------"
 cd /scratch/$USER/$PBS_JOBID/
-- 
GitLab