From 0540bd0a86f9c95077ae8ffbb0ee2b7acd98cf07 Mon Sep 17 00:00:00 2001
From: Anne Tipka <anne.tipka@ctbto.org>
Date: Tue, 25 Oct 2022 08:48:28 +0000
Subject: [PATCH] added option to provide event(ECMWF trigger events)
 information to CONTROL files and start the job in operational mode

---
 Source/Python/Classes/ControlFile.py | 12 ++++++
 Source/Python/Mods/tools.py          | 63 ++++++++++++++++++++++++++--
 Source/Python/submit.py              |  5 ++-
 3 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/Source/Python/Classes/ControlFile.py b/Source/Python/Classes/ControlFile.py
index 5a14859..6b14383 100644
--- a/Source/Python/Classes/ControlFile.py
+++ b/Source/Python/Classes/ControlFile.py
@@ -22,6 +22,9 @@
 #        - update default makefile to None
 #   August 2020 - Leopold Haimberger
 #        - added a class parameter for system installation path
+#   October 2022 - Anne Tipka (previously Philipp)
+#        - added class parameter for packingType (compression) of grib messages
+#        - added class parameter for event trigger for time critical jobs
 #
 # @License:
 #    (C) Copyright 2014-2020.
@@ -332,6 +335,13 @@ class ControlFile(object):
         Switch to select the calculation of extra ensemble members for the
         ELDA stream. It doubles the amount of retrieved ensemble members.
 
+    eventid : int
+        Id for one of the events for time critical jobs 1 at ECMWF servers.
+
+    eventjobname : str
+        The name the ecaccess-job-submit command will be given.
+        This is how the job will be recognized in the list of jobs.
+
     logicals : list of str
         List of the names of logical switches which controls the flow
         of the program. Default list is ['gauss', 'omega', 'omegadiff', 'eta',
@@ -425,6 +435,8 @@ class ControlFile(object):
         self.purefc = 0
         self.rrint = 0
         self.doubleelda = 0
+        self.eventid = None
+        self.eventjobname = ''
 
         self.logicals = ['gauss', 'omega', 'omegadiff', 'eta', 'etadiff',
                          'dpdeta', 'cwc', 'wrf', 'ecstorage',
diff --git a/Source/Python/Mods/tools.py b/Source/Python/Mods/tools.py
index 203dcbe..8846689 100644
--- a/Source/Python/Mods/tools.py
+++ b/Source/Python/Mods/tools.py
@@ -704,7 +704,7 @@ def submit_sbatch_job(jobname):
     print('SUBMITTED SBATCH JOB ',jobname)
     return job_id.decode()
 
-def submit_job_to_ecserver(target, jobname):
+def submit_job_to_ecserver(target, jobfile):
     '''Uses ecaccess-job-submit command to submit a job to the ECMWF server.
 
     Note
@@ -718,7 +718,7 @@ def submit_job_to_ecserver(target, jobname):
     target : str
         The target where the file should be sent to, e.g. the queue.
 
-    jobname : str
+    jobfile : str
         The name of the jobfile to be submitted to the ECMWF server.
 
     Return
@@ -729,7 +729,64 @@ def submit_job_to_ecserver(target, jobname):
 
     try:
         job_id = subprocess.check_output(['ecaccess-job-submit', '-queueName',
-                                          target, jobname])
+                                          target, jobfile])
+
+    except subprocess.CalledProcessError as e:
+        print('... ERROR CODE: ' + str(e.returncode))
+        print('... ERROR MESSAGE:\n \t ' + str(e))
+
+        print('\n... Do you have a valid ecaccess certification key?')
+        sys.exit('... ecaccess-job-submit FAILED!')
+    except OSError as e:
+        print('... ERROR CODE: ' + str(e.errno))
+        print('... ERROR MESSAGE:\n \t ' + str(e.strerror))
+
+        print('\n... Most likely the ECACCESS library is not available!')
+        sys.exit('... ecaccess-job-submit FAILED!')
+
+    return job_id.decode()
+
+def submit_eventjob_to_ecserver(target, jobfile, eventid, jobname, ecuid):
+    '''Uses ecaccess-job-submit command to submit a job to the ECMWF server.
+
+    Note
+    ----
+    The return value is just for testing reasons. It does not have
+    to be used from the calling function since the whole error handling
+    is done in here.
+
+    Parameters
+    ----------
+    target : str
+        The target where the file should be sent to, e.g. the queue.
+
+    jobfile : str
+        The name of the jobfile to be submitted to the ECMWF server.
+
+    eventid : int
+        The id number from an event listed by ECMWF through ecaccess-event-list.
+
+    jobname : str
+        The name of the job, helps to distingush between jobs.
+        Will be shown in the queue and list of jobs.
+
+    ecuid : str
+        The user id on ECMWF server.
+
+    Return
+    ------
+    job_id : int
+        The id number of the job as a reference at the ECMWF server.
+    '''
+
+    try:
+        job_id = subprocess.check_output(['ecaccess-job-submit',
+                                          '-queueName', target, '-jobName', jobname,
+                                          '-onStart', '-onSuccess', '-onFailure',
+                                          '-onRetry', '-retryCount', '5',
+                                          '-mailTo', ecuid,
+                                          '-eventIds', eventid,
+                                          jobfile])
 
     except subprocess.CalledProcessError as e:
         print('... ERROR CODE: ' + str(e.returncode))
diff --git a/Source/Python/submit.py b/Source/Python/submit.py
index 6d5eb8b..0c6c66f 100755
--- a/Source/Python/submit.py
+++ b/Source/Python/submit.py
@@ -201,7 +201,10 @@ def submit(jtemplate, c, queue):
 
         mk_jobscript(jtemplate, job_file, clist)
 
-        job_id = submit_job_to_ecserver(queue, job_file)
+        #job_id = submit_job_to_ecserver(queue, job_file)
+        job_id = submit_eventjob_to_ecserver(queue, c.eventjobname,
+                                             c.eventid, c.ecuid)
+
         print('The job id is: ' + str(job_id.strip()))
 
     print('You should get an email per job with subject flex.hostname.pid')
-- 
GitLab