[d69b677] | 1 | #!/usr/bin/env python |
---|
[64cf353] | 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
| 5 | # |
---|
| 6 | # @Date: October 2014 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # |
---|
| 10 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
| 11 | # - job submission on ecgate and cca |
---|
| 12 | # - job templates suitable for twice daily operational dissemination |
---|
| 13 | # |
---|
| 14 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 15 | # - applied PEP8 style guide |
---|
| 16 | # - added documentation |
---|
| 17 | # - minor changes in programming style (for consistence) |
---|
| 18 | # |
---|
| 19 | # @License: |
---|
| 20 | # (C) Copyright 2014-2018. |
---|
| 21 | # |
---|
| 22 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 23 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 24 | # |
---|
| 25 | # @Program Functionality: |
---|
| 26 | # This program is the main program of flex_extract and controls the |
---|
| 27 | # program flow. |
---|
| 28 | # If it is supposed to work locally then it works through the necessary |
---|
[ff99eae] | 29 | # functions get_mars_data and prepareFlexpart. Otherwise it prepares |
---|
[991df6a] | 30 | # a shell job script which will do the necessary work on the |
---|
| 31 | # ECMWF server and is submitted via ecaccess-job-submit. |
---|
| 32 | # |
---|
| 33 | # @Program Content: |
---|
| 34 | # - main |
---|
| 35 | # - submit |
---|
| 36 | # |
---|
| 37 | #******************************************************************************* |
---|
[efdb01a] | 38 | |
---|
[64cf353] | 39 | # ------------------------------------------------------------------------------ |
---|
| 40 | # MODULES |
---|
| 41 | # ------------------------------------------------------------------------------ |
---|
[efdb01a] | 42 | import os |
---|
| 43 | import sys |
---|
[d69b677] | 44 | import subprocess |
---|
| 45 | import inspect |
---|
[efdb01a] | 46 | |
---|
| 47 | # software specific classes and modules from flex_extract |
---|
[ff99eae] | 48 | from tools import interpret_args_and_control, normal_exit |
---|
| 49 | from get_mars_data import get_mars_data |
---|
| 50 | from prepare_flexpart import prepare_flexpart |
---|
| 51 | |
---|
| 52 | # add path to pythonpath so that python finds its buddies |
---|
| 53 | LOCAL_PYTHON_PATH = os.path.dirname(os.path.abspath( |
---|
| 54 | inspect.getfile(inspect.currentframe()))) |
---|
| 55 | if LOCAL_PYTHON_PATH not in sys.path: |
---|
| 56 | sys.path.append(LOCAL_PYTHON_PATH) |
---|
[991df6a] | 57 | |
---|
[64cf353] | 58 | # ------------------------------------------------------------------------------ |
---|
| 59 | # FUNCTIONS |
---|
| 60 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 61 | |
---|
[d69b677] | 62 | def main(): |
---|
[64cf353] | 63 | ''' |
---|
| 64 | @Description: |
---|
[991df6a] | 65 | Get the arguments from script call and from CONTROL file. |
---|
[efdb01a] | 66 | Decides from the argument "queue" if the local version |
---|
| 67 | is done "queue=None" or the gateway version with "queue=ecgate" |
---|
| 68 | or "queue=cca". |
---|
[d69b677] | 69 | |
---|
[64cf353] | 70 | @Input: |
---|
| 71 | <nothing> |
---|
| 72 | |
---|
| 73 | @Return: |
---|
| 74 | <nothing> |
---|
| 75 | ''' |
---|
[ff99eae] | 76 | |
---|
| 77 | called_from_dir = os.getcwd() |
---|
[64cf353] | 78 | args, c = interpret_args_and_control() |
---|
[ff99eae] | 79 | |
---|
[efdb01a] | 80 | # on local side |
---|
[64cf353] | 81 | if args.queue is None: |
---|
| 82 | if c.inputdir[0] != '/': |
---|
[ff99eae] | 83 | c.inputdir = os.path.join(called_from_dir, c.inputdir) |
---|
[64cf353] | 84 | if c.outputdir[0] != '/': |
---|
[ff99eae] | 85 | c.outputdir = os.path.join(called_from_dir, c.outputdir) |
---|
| 86 | get_mars_data(args, c) |
---|
| 87 | prepare_flexpart(args, c) |
---|
| 88 | normal_exit(c) |
---|
[efdb01a] | 89 | # on ECMWF server |
---|
[d69b677] | 90 | else: |
---|
[64cf353] | 91 | submit(args.job_template, c, args.queue) |
---|
| 92 | |
---|
| 93 | return |
---|
| 94 | |
---|
| 95 | def submit(jtemplate, c, queue): |
---|
| 96 | ''' |
---|
| 97 | @Description: |
---|
| 98 | Prepares the job script and submit it to the specified queue. |
---|
| 99 | |
---|
| 100 | @Input: |
---|
| 101 | jtemplate: string |
---|
| 102 | Job template file for submission to ECMWF. It contains all necessary |
---|
| 103 | module and variable settings for the ECMWF environment as well as |
---|
| 104 | the job call and mail report instructions. |
---|
| 105 | Default is "job.temp". |
---|
| 106 | |
---|
[991df6a] | 107 | c: instance of class ControlFile |
---|
| 108 | Contains all the parameters of CONTROL file, which are e.g.: |
---|
[02c8c50] | 109 | DAY1(start_date), DAY2(end_date), DTIME, MAXSTEP, TYPE, TIME, |
---|
| 110 | STEP, CLASS(marsclass), STREAM, NUMBER, EXPVER, GRID, LEFT, |
---|
| 111 | LOWER, UPPER, RIGHT, LEVEL, LEVELIST, RESOL, GAUSS, ACCURACY, |
---|
| 112 | OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, SMOOTH, FORMAT, |
---|
| 113 | ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, ECFSDIR, |
---|
| 114 | MAILOPS, MAILFAIL, GRIB2FLEXPART, FLEXPARTDIR, BASETIME |
---|
| 115 | DATE_CHUNK, DEBUG, INPUTDIR, OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
| 116 | |
---|
| 117 | For more information about format and content of the parameter |
---|
| 118 | see documentation. |
---|
[64cf353] | 119 | |
---|
| 120 | queue: string |
---|
| 121 | Name of queue for submission to ECMWF (e.g. ecgate or cca ) |
---|
| 122 | |
---|
| 123 | @Return: |
---|
| 124 | <nothing> |
---|
| 125 | ''' |
---|
| 126 | |
---|
| 127 | # read template file and split from newline signs |
---|
| 128 | with open(jtemplate) as f: |
---|
| 129 | lftext = f.read().split('\n') |
---|
| 130 | insert_point = lftext.index('EOF') |
---|
| 131 | |
---|
[991df6a] | 132 | # put all parameters of ControlFile instance into a list |
---|
[ff99eae] | 133 | clist = c.to_list() # ondemand |
---|
[64cf353] | 134 | colist = [] # operational |
---|
| 135 | mt = 0 |
---|
| 136 | |
---|
[d69b677] | 137 | for elem in clist: |
---|
| 138 | if 'maxstep' in elem: |
---|
[64cf353] | 139 | mt = int(elem.split(' ')[1]) |
---|
| 140 | |
---|
[d69b677] | 141 | for elem in clist: |
---|
| 142 | if 'start_date' in elem: |
---|
[64cf353] | 143 | elem = 'start_date ' + '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
[d69b677] | 144 | if 'end_date' in elem: |
---|
[ff99eae] | 145 | elem = 'end_date ' + '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
[d69b677] | 146 | if 'base_time' in elem: |
---|
[64cf353] | 147 | elem = 'base_time ' + '${MSJ_BASETIME}' |
---|
| 148 | if 'time' in elem and mt > 24: |
---|
| 149 | elem = 'time ' + '${MSJ_BASETIME} {MSJ_BASETIME}' |
---|
[d69b677] | 150 | colist.append(elem) |
---|
[64cf353] | 151 | |
---|
| 152 | lftextondemand = lftext[:insert_point] + clist + lftext[insert_point + 2:] |
---|
| 153 | lftextoper = lftext[:insert_point] + colist + lftext[insert_point + 2:] |
---|
| 154 | |
---|
| 155 | with open('job.ksh', 'w') as h: |
---|
| 156 | h.write('\n'.join(lftextondemand)) |
---|
| 157 | |
---|
[efdb01a] | 158 | with open('joboper.ksh', 'w') as h: |
---|
| 159 | h.write('\n'.join(lftextoper)) |
---|
[64cf353] | 160 | |
---|
| 161 | # submit job script to queue |
---|
| 162 | try: |
---|
| 163 | p = subprocess.check_call(['ecaccess-job-submit', '-queueName', |
---|
[efdb01a] | 164 | queue, 'job.ksh']) |
---|
[ff99eae] | 165 | except subprocess.CalledProcessError as e: |
---|
| 166 | print 'ecaccess-job-submit failed!' |
---|
| 167 | print 'Error Message: ' |
---|
| 168 | print e.output |
---|
[d69b677] | 169 | exit(1) |
---|
| 170 | |
---|
[ff99eae] | 171 | print 'You should get an email with subject flex.hostname.pid' |
---|
[d69b677] | 172 | |
---|
[64cf353] | 173 | return |
---|
[d69b677] | 174 | |
---|
| 175 | if __name__ == "__main__": |
---|
| 176 | main() |
---|