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