From df847a2e50994bd5aaaf9b36ed5fa31c51681fb7 Mon Sep 17 00:00:00 2001
From: lkugler <lukas.kugler@gmail.com>
Date: Sun, 12 Feb 2023 23:00:44 +0100
Subject: [PATCH] setup clusters

---
 analysis_only.py                       |  7 ----
 config/cfg.py                          |  2 +-
 config/clusters.py                     | 13 +++++---
 cycled_exp.py                          | 11 ++++---
 dartwrf/obs/calculate_obs_locations.py |  3 +-
 dartwrf/utils.py                       | 45 +++++++++++++++++---------
 generate_free.py                       |  5 ---
 7 files changed, 46 insertions(+), 40 deletions(-)

diff --git a/analysis_only.py b/analysis_only.py
index 0646f6c..96e1721 100755
--- a/analysis_only.py
+++ b/analysis_only.py
@@ -9,15 +9,8 @@ from dartwrf import utils
 from config.cfg import exp
 from config.clusters import cluster
 
-log_dir = cluster.archivedir+'/logs/'
-slurm_scripts_dir = cluster.archivedir+'/slurm-scripts/'
-print('logging to', log_dir)
-print('scripts, which are submitted to SLURM:', slurm_scripts_dir)
-
 
 ###############################
-backup_scripts()
-
 
 prior_path_exp = '/mnt/jetfs/scratch/lkugler/data/sim_archive/exp_v1.19_P3_wbub7_noDA'
 prior_init_time = dt.datetime(2008,7,30,12)
diff --git a/config/cfg.py b/config/cfg.py
index dc554fe..b9e356e 100755
--- a/config/cfg.py
+++ b/config/cfg.py
@@ -23,7 +23,7 @@ exp.use_existing_obsseq = False  # False or pathname (use precomputed obs_seq.ou
 #exp.nature_wrfout = '/home/fs71386/lkugler/data/sim_archive/exp_v1.19_P5+su_nat2/2008-07-30_07:00/1/wrfout_d01_%Y-%m-%d_%H:%M:%S'
 #exp.nature_wrfout = '/jetfs/home/lkugler/data/sim_archive/exp_v1.19_P3_wbub7_nat/2008-07-30_12:00/1/wrfout_d01_%Y-%m-%d_%H:%M:%S'
 #exp.nature_wrfout = '/home/fs71386/lkugler/data/sim_archive/exp_v1.19_Pwbub5_nat/2008-07-30_12:00/1/wrfout_d01_%Y-%m-%d_%H:%M:%S'
-exp.nature_wrfout = cluster.archive_base+'/exp_v1.18_P1_nature/2008-07-30_06:00/1/wrfout_d01_%Y-%m-%d_%H:%M:%S'
+exp.nature_wrfout = '/jetfs/home/lkugler/data/sim_archive/exp_v1.18_P1_nature/2008-07-30_06:00/1/wrfout_d01_%Y-%m-%d_%H:%M:%S'
 #exp.nature_wrfout = '/home/fs71386/lkugler/data/sim_archive/exp_v1.19_P4_nat/2008-07-30_07:00/1/wrfout_d01_%Y-%m-%d_%H:%M:%S'
 
 exp.input_profile = '/jetfs/home/lkugler/data/initial_profiles/wrf/ens/2022-03-31/raso.fc.<iens>.wrfprof'
diff --git a/config/clusters.py b/config/clusters.py
index 7c6efe9..f7d090c 100755
--- a/config/clusters.py
+++ b/config/clusters.py
@@ -1,6 +1,7 @@
 import os, sys
 import datetime as dt
 from dartwrf import utils
+from config.cfg import exp
 
 """Configuration name docs
 
@@ -42,9 +43,7 @@ slurm_cfg               python dictionary, containing options of SLURM
 """
 
 
-
-
-vsc = utils.ClusterConfig()
+vsc = utils.ClusterConfig(exp)
 vsc.name = 'vsc' 
 vsc.max_nproc = 20
 vsc.size_jobarray = 10  # 10 jobs with each 4 WRF processes per node
@@ -77,7 +76,7 @@ vsc.slurm_cfg = {"account": "p71386", "partition": "skylake_0384", "qos": "p7138
                  "nodes": "1", "ntasks": "1", "ntasks-per-node": "48", "ntasks-per-core": "1",
                  "mail-type": "FAIL", "mail-user": "lukas.kugler@univie.ac.at"}
 
-jet = utils.ClusterConfig()
+jet = utils.ClusterConfig(exp)
 jet.name = 'jet'
 jet.max_nproc = 12
 jet.use_slurm = True
@@ -113,7 +112,7 @@ jet.slurm_cfg = {"account": "lkugler", "partition": "compute", #"nodelist": "jet
                  "mail-type": "FAIL", "mail-user": "lukas.kugler@univie.ac.at"}
 
 
-srvx1 = utils.ClusterConfig()
+srvx1 = utils.ClusterConfig(exp)
 srvx1.name = 'srvx1'
 srvx1.max_nproc = 6
 srvx1.use_slurm = False
@@ -145,3 +144,7 @@ srvx1.run_WRF = srvx1.scriptsdir+'/run_ens.jet.sh'
 srvx1.slurm_cfg = {"account": "lkugler", "partition": "compute",
                  "ntasks": "1", "ntasks-per-core": "1", "mem": "50G",
                  "mail-type": "FAIL", "mail-user": "lukas.kugler@univie.ac.at"}
+
+#################################
+# select cluster configuration
+cluster = jet
\ No newline at end of file
diff --git a/cycled_exp.py b/cycled_exp.py
index 204be18..9057bfe 100755
--- a/cycled_exp.py
+++ b/cycled_exp.py
@@ -9,10 +9,12 @@ from dartwrf.utils import script_to_str
 from config.cfg import exp
 from config.clusters import cluster
 
-log_dir = cluster.archivedir+'/logs/'
-slurm_scripts_dir = cluster.archivedir+'/slurm-scripts/'
-print('logging to', log_dir)
-print('scripts, which are submitted to SLURM:', slurm_scripts_dir)
+def prepare_WRFrundir(init_time):
+    """Create WRF/run directories and wrfinput files
+    """
+    cmd = cluster.python+' '+cluster.scripts_rundir+'/prepare_wrfrundir.py '+init_time.strftime('%Y-%m-%d_%H:%M')
+    print(cmd)
+    os.system(cmd)
 
 def run_ideal(depends_on=None):
     """Run ideal for every ensemble member"""
@@ -202,7 +204,6 @@ if __name__ == "__main__":
     timedelta_integrate = dt.timedelta(minutes=15)
     timedelta_btw_assim = dt.timedelta(minutes=15)
 
-    cluster.backup_scripts()
     id = None
 
     if False:  # warm bubble
diff --git a/dartwrf/obs/calculate_obs_locations.py b/dartwrf/obs/calculate_obs_locations.py
index d4dc65c..1245cb5 100755
--- a/dartwrf/obs/calculate_obs_locations.py
+++ b/dartwrf/obs/calculate_obs_locations.py
@@ -7,7 +7,8 @@ import numpy as np
 import datetime as dt
 import xarray as xr
 
-from config.cfg import exp, cluster
+from config.cfg import exp
+from config.clusters import cluster
 
 #####################
 # Global variables
diff --git a/dartwrf/utils.py b/dartwrf/utils.py
index ddc1350..b46f196 100755
--- a/dartwrf/utils.py
+++ b/dartwrf/utils.py
@@ -21,19 +21,33 @@ class ClusterConfig(object):
     """Collection of variables to use in code later on"""
     def __init__(self, exp):
         self.exp = exp
+        self.set_up = False
 
+    def setup(self):
+        # Set paths and backup scripts
+        self.log_dir = self.archivedir+'/logs/'
+        self.slurm_scripts_dir = self.archivedir+'/slurm-scripts/'
+        print('logging to', self.log_dir)
+        print('scripts, which are submitted to SLURM:', self.slurm_scripts_dir)
+
+        self.backup_scripts()
+        self.set_up = True
+
+    @property
     def archivedir(self):
         return self.archive_base+'/'+self.exp.expname
 
-    def wrf_rundir(self, iens):
-        return self.wrf_rundir_base+'/'+self.exp.expname+'/'+str(iens)
-
+    @property
     def scripts_rundir(self):
         return self.archivedir+'/DART-WRF/'
 
+    @property
     def dartrundir(self):
         return self.dart_rundir_base+'/'+self.exp.expname+'/'
 
+    def wrf_rundir(self, iens):
+        return self.wrf_rundir_base+'/'+self.exp.expname+'/'+str(iens)
+
     def create_job(self, *args, cfg_update=dict(), **kwargs):
         """Shortcut to slurmpy's class; keep certain default kwargs
         and only update some with kwarg `cfg_update`
@@ -41,13 +55,18 @@ class ClusterConfig(object):
 
         depending on cluster config : run either locally or via SLURM 
         """
+        if not self.set_up:
+            self.setup()
+
         if self.use_slurm:
             return Slurm(*args, slurm_kwargs=dict(self.slurm_cfg, **cfg_update), 
-                     log_dir=log_dir, scripts_dir=slurm_scripts_dir, **kwargs)
+                        log_dir=self.log_dir, 
+                        scripts_dir=self.slurm_scripts_dir, 
+                        **kwargs)
         else:
             return Shellslurm(*args)
 
-    def backup_scripts():
+    def backup_scripts(self):
         """Copies scripts and configuration to archive dir output folder"""
         os.makedirs(self.archivedir, exist_ok=True)
     
@@ -57,17 +76,11 @@ class ClusterConfig(object):
             pass
         except:
             raise
-        try:
-            copy(os.path.basename(__file__), self.scripts_rundir+'/')
-        except Exception as e:
-            warnings.warn(str(e))
-    
-    def prepare_WRFrundir(init_time):
-        """Create WRF/run directories and wrfinput files
-        """
-        cmd = self.python+' '+self.scripts_rundir+'/prepare_wrfrundir.py '+init_time.strftime('%Y-%m-%d_%H:%M')
-        print(cmd)
-        os.system(cmd)
+        # try:
+        #     copy(os.path.basename(__file__), self.scripts_rundir+'/')
+        # except Exception as e:
+        #     warnings.warn(str(e))
+        print('scripts have been copied to', self.archivedir)
 
 def shell(args):
     print(args)
diff --git a/generate_free.py b/generate_free.py
index 8359c54..e75feff 100755
--- a/generate_free.py
+++ b/generate_free.py
@@ -12,15 +12,10 @@ from config.clusters import cluster
 from dartwrf.utils import script_to_str, symlink
 from cycled_exp import *
 
-log_dir = cluster.archivedir+'/logs/'
-slurm_scripts_dir = cluster.archivedir+'/slurm-scripts/'
-print('logging to', log_dir)
-print('scripts, which are submitted to SLURM:', slurm_scripts_dir)
 
 ################################
 print('starting osse')
 
-cluster.backup_scripts()
 id = None
 
 
-- 
GitLab