[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) |
---|
[2fb99de] | 18 | # - changed path names to variables from config file |
---|
| 19 | # - added option for writing mars requests to extra file |
---|
[6f951ca] | 20 | # additionally, as option without submitting the mars jobs |
---|
| 21 | # - splitted submit function to use genshi templates for the |
---|
| 22 | # job script and avoid code duplication |
---|
[991df6a] | 23 | # |
---|
| 24 | # @License: |
---|
[6f951ca] | 25 | # (C) Copyright 2014-2019. |
---|
| 26 | # Anne Philipp, Leopold Haimberger |
---|
[991df6a] | 27 | # |
---|
[6f951ca] | 28 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 29 | # International License. To view a copy of this license, visit |
---|
| 30 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 31 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[991df6a] | 32 | #******************************************************************************* |
---|
[6f951ca] | 33 | '''This script allows the user to extract meteorological fields from the ECMWF. |
---|
| 34 | |
---|
| 35 | It prepares the settings for retrieving the data from ECMWF servers and |
---|
| 36 | process the resulting files to prepare them for the use with FLEXPART or |
---|
| 37 | FLEXTRA. |
---|
| 38 | |
---|
| 39 | If it is supposed to work locally then it works through the necessary |
---|
| 40 | functions get_mars_data and prepare_flexpart. Otherwise it prepares |
---|
| 41 | a job script (korn shell) which will do the necessary work on the |
---|
| 42 | ECMWF server. This script will de submitted via the ecaccess command |
---|
| 43 | ecaccess-job-submit. |
---|
| 44 | |
---|
| 45 | This file can also be imported as a module which then contains the following |
---|
| 46 | functions: |
---|
| 47 | |
---|
| 48 | * main - the main function of the script |
---|
| 49 | * submit - calls mk_jobscript depending on operation mode and submits its |
---|
| 50 | * mk_jobscript - creates the job script from a template |
---|
| 51 | |
---|
| 52 | Type: submit.py --help |
---|
| 53 | to get information about command line parameters. |
---|
| 54 | Read the documentation for usage instructions. |
---|
| 55 | ''' |
---|
[efdb01a] | 56 | |
---|
[64cf353] | 57 | # ------------------------------------------------------------------------------ |
---|
| 58 | # MODULES |
---|
| 59 | # ------------------------------------------------------------------------------ |
---|
[efdb01a] | 60 | import os |
---|
| 61 | import sys |
---|
[d69b677] | 62 | import subprocess |
---|
| 63 | import inspect |
---|
[54a8a01] | 64 | import collections |
---|
[f2616a3] | 65 | from datetime import datetime, timedelta |
---|
[efdb01a] | 66 | |
---|
| 67 | # software specific classes and modules from flex_extract |
---|
[54a8a01] | 68 | import _config |
---|
[f20af73] | 69 | from mods.tools import (setup_controldata, normal_exit, get_cmdline_args, |
---|
[25b14be] | 70 | submit_job_to_ecserver, read_ecenv) |
---|
| 71 | from mods.get_mars_data import get_mars_data |
---|
| 72 | from mods.prepare_flexpart import prepare_flexpart |
---|
| 73 | from classes.ControlFile import ControlFile |
---|
[991df6a] | 74 | |
---|
[64cf353] | 75 | # ------------------------------------------------------------------------------ |
---|
[6f951ca] | 76 | # METHODS |
---|
[64cf353] | 77 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 78 | |
---|
[d69b677] | 79 | def main(): |
---|
[274f9ef] | 80 | '''Get the arguments from script call and from CONTROL file. |
---|
| 81 | Decides from the argument "queue" if the local version |
---|
| 82 | is done "queue=None" or the gateway version with "queue=ecgate" |
---|
| 83 | or "queue=cca". |
---|
| 84 | |
---|
| 85 | Parameters |
---|
| 86 | ---------- |
---|
[d69b677] | 87 | |
---|
[274f9ef] | 88 | Return |
---|
| 89 | ------ |
---|
[64cf353] | 90 | |
---|
| 91 | ''' |
---|
[ff99eae] | 92 | |
---|
[f20af73] | 93 | c, ppid, queue, job_template = setup_controldata() |
---|
[ff99eae] | 94 | |
---|
[efdb01a] | 95 | # on local side |
---|
[f20af73] | 96 | # starting from an ECMWF server this would also be the local side |
---|
[2fb99de] | 97 | called_from_dir = os.getcwd() |
---|
[f20af73] | 98 | if queue is None: |
---|
[64cf353] | 99 | if c.inputdir[0] != '/': |
---|
[ff99eae] | 100 | c.inputdir = os.path.join(called_from_dir, c.inputdir) |
---|
[64cf353] | 101 | if c.outputdir[0] != '/': |
---|
[ff99eae] | 102 | c.outputdir = os.path.join(called_from_dir, c.outputdir) |
---|
[54a8a01] | 103 | get_mars_data(c) |
---|
[2fb99de] | 104 | if c.request == 0 or c.request == 2: |
---|
[f20af73] | 105 | prepare_flexpart(ppid, c) |
---|
[96e1533] | 106 | exit_message = 'FLEX_EXTRACT IS DONE!' |
---|
[2fb99de] | 107 | else: |
---|
[96e1533] | 108 | exit_message = 'PRINTING MARS_REQUESTS DONE!' |
---|
| 109 | # send files to ECMWF server |
---|
[d69b677] | 110 | else: |
---|
[f20af73] | 111 | submit(job_template, c, queue) |
---|
[e0e99a5] | 112 | exit_message = 'FLEX_EXTRACT JOB SCRIPT IS SUBMITED!' |
---|
[64cf353] | 113 | |
---|
[403cbf1] | 114 | normal_exit(exit_message) |
---|
[96e1533] | 115 | |
---|
[64cf353] | 116 | return |
---|
| 117 | |
---|
| 118 | def submit(jtemplate, c, queue): |
---|
[ff2a11c] | 119 | '''Prepares the job script and submits it to the specified queue. |
---|
[274f9ef] | 120 | |
---|
| 121 | Parameters |
---|
| 122 | ---------- |
---|
[6f951ca] | 123 | jtemplate : str |
---|
[274f9ef] | 124 | Job template file from sub-directory "_templates" for |
---|
| 125 | submission to ECMWF. It contains all necessary |
---|
| 126 | module and variable settings for the ECMWF environment as well as |
---|
| 127 | the job call and mail report instructions. |
---|
| 128 | Default is "job.temp". |
---|
| 129 | |
---|
[6f951ca] | 130 | c : ControlFile |
---|
[274f9ef] | 131 | Contains all the parameters of CONTROL file and |
---|
| 132 | command line. |
---|
| 133 | |
---|
[6f951ca] | 134 | queue : str |
---|
[274f9ef] | 135 | Name of queue for submission to ECMWF (e.g. ecgate or cca ) |
---|
| 136 | |
---|
| 137 | Return |
---|
| 138 | ------ |
---|
| 139 | |
---|
[64cf353] | 140 | ''' |
---|
| 141 | |
---|
[d4696e0] | 142 | if not c.oper: |
---|
[54a8a01] | 143 | # --------- create on demand job script ------------------------------------ |
---|
[a4531f1] | 144 | if c.purefc: |
---|
[2fb99de] | 145 | print('---- Pure forecast mode! ----') |
---|
[54a8a01] | 146 | else: |
---|
[2fb99de] | 147 | print('---- On-demand mode! ----') |
---|
[54a8a01] | 148 | |
---|
[ff2a11c] | 149 | job_file = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
| 150 | jtemplate[:-5] + '.ksh') |
---|
[54a8a01] | 151 | |
---|
[f2616a3] | 152 | # divide time periode into specified number of job chunks |
---|
| 153 | # to have multiple job scripts |
---|
| 154 | if c.job_chunk: |
---|
| 155 | start = datetime.strptime(c.start_date, '%Y%m%d') |
---|
| 156 | end = datetime.strptime(c.end_date, '%Y%m%d') |
---|
| 157 | chunk = timedelta(days=c.job_chunk) |
---|
[fc05fbd] | 158 | oneday = timedelta(days=1) |
---|
[54a8a01] | 159 | |
---|
[f2616a3] | 160 | while start <= end: |
---|
| 161 | if (start + chunk) <= end: |
---|
[fc05fbd] | 162 | c.end_date = (start + chunk - oneday).strftime("%Y%m%d") |
---|
[f2616a3] | 163 | else: |
---|
| 164 | c.end_date = end.strftime("%Y%m%d") |
---|
| 165 | |
---|
| 166 | clist = c.to_list() |
---|
| 167 | mk_jobscript(jtemplate, job_file, clist) |
---|
| 168 | |
---|
| 169 | job_id = submit_job_to_ecserver(queue, job_file) |
---|
| 170 | print('The job id is: ' + str(job_id.strip())) |
---|
| 171 | |
---|
| 172 | start = start + chunk |
---|
| 173 | c.start_date = start.strftime("%Y%m%d") |
---|
| 174 | # submit a single job script |
---|
| 175 | else: |
---|
| 176 | clist = c.to_list() |
---|
| 177 | |
---|
| 178 | mk_jobscript(jtemplate, job_file, clist) |
---|
| 179 | |
---|
| 180 | job_id = submit_job_to_ecserver(queue, job_file) |
---|
| 181 | print('The job id is: ' + str(job_id.strip())) |
---|
[54a8a01] | 182 | |
---|
| 183 | else: |
---|
| 184 | # --------- create operational job script ---------------------------------- |
---|
[2fb99de] | 185 | print('---- Operational mode! ----') |
---|
[ff2a11c] | 186 | |
---|
[2fb99de] | 187 | job_file = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
| 188 | jtemplate[:-5] + 'oper.ksh') |
---|
[54a8a01] | 189 | |
---|
| 190 | c.start_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
| 191 | c.end_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
[d4696e0] | 192 | c.basetime = '${MSJ_BASETIME}' |
---|
[ff2a11c] | 193 | if c.maxstep > 24: |
---|
[54a8a01] | 194 | c.time = '${MSJ_BASETIME} {MSJ_BASETIME}' |
---|
| 195 | |
---|
[ff2a11c] | 196 | clist = c.to_list() |
---|
[54a8a01] | 197 | |
---|
[ff2a11c] | 198 | mk_jobscript(jtemplate, job_file, clist) |
---|
[d69b677] | 199 | |
---|
[f2616a3] | 200 | job_id = submit_job_to_ecserver(queue, job_file) |
---|
| 201 | print('The job id is: ' + str(job_id.strip())) |
---|
| 202 | |
---|
| 203 | print('You should get an email per job with subject flex.hostname.pid') |
---|
[d69b677] | 204 | |
---|
[64cf353] | 205 | return |
---|
[d69b677] | 206 | |
---|
[ff2a11c] | 207 | def mk_jobscript(jtemplate, job_file, clist): |
---|
| 208 | '''Creates the job script from template. |
---|
| 209 | |
---|
| 210 | Parameters |
---|
| 211 | ---------- |
---|
[6f951ca] | 212 | jtemplate : str |
---|
[ff2a11c] | 213 | Job template file from sub-directory "_templates" for |
---|
| 214 | submission to ECMWF. It contains all necessary |
---|
| 215 | module and variable settings for the ECMWF environment as well as |
---|
| 216 | the job call and mail report instructions. |
---|
| 217 | Default is "job.temp". |
---|
| 218 | |
---|
[6f951ca] | 219 | job_file : str |
---|
[ff2a11c] | 220 | Path to the job script file. |
---|
| 221 | |
---|
[6f951ca] | 222 | clist : list of str |
---|
[ff2a11c] | 223 | Contains all necessary parameters for ECMWF CONTROL file. |
---|
| 224 | |
---|
| 225 | Return |
---|
| 226 | ------ |
---|
| 227 | |
---|
| 228 | ''' |
---|
| 229 | from genshi.template.text import NewTextTemplate |
---|
| 230 | from genshi.template import TemplateLoader |
---|
| 231 | from genshi.template.eval import UndefinedError |
---|
| 232 | |
---|
| 233 | # load template and insert control content as list |
---|
| 234 | try: |
---|
| 235 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 236 | control_template = loader.load(jtemplate, |
---|
| 237 | cls=NewTextTemplate) |
---|
| 238 | |
---|
| 239 | stream = control_template.generate(control_content=clist) |
---|
| 240 | except UndefinedError as e: |
---|
| 241 | print('... ERROR ' + str(e)) |
---|
| 242 | |
---|
| 243 | sys.exit('\n... error occured while trying to generate jobscript') |
---|
| 244 | except OSError as e: |
---|
| 245 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 246 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 247 | |
---|
| 248 | sys.exit('\n... error occured while trying to generate jobscript') |
---|
| 249 | |
---|
| 250 | # create jobscript file |
---|
| 251 | try: |
---|
| 252 | with open(job_file, 'w') as f: |
---|
| 253 | f.write(stream.render('text')) |
---|
| 254 | except OSError as e: |
---|
| 255 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 256 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 257 | |
---|
| 258 | sys.exit('\n... error occured while trying to write ' + job_file) |
---|
| 259 | |
---|
| 260 | return |
---|
| 261 | |
---|
[f2616a3] | 262 | |
---|
[d69b677] | 263 | if __name__ == "__main__": |
---|
| 264 | main() |
---|