[79729d5] | 1 | #!/usr/bin/env python3 |
---|
[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, |
---|
[79729d5] | 70 | submit_job_to_ecserver, read_ecenv) |
---|
[25b14be] | 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 | |
---|
[d727af2] | 75 | |
---|
[64cf353] | 76 | # ------------------------------------------------------------------------------ |
---|
[6f951ca] | 77 | # METHODS |
---|
[64cf353] | 78 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 79 | |
---|
[d69b677] | 80 | def main(): |
---|
[274f9ef] | 81 | '''Get the arguments from script call and from CONTROL file. |
---|
| 82 | Decides from the argument "queue" if the local version |
---|
| 83 | is done "queue=None" or the gateway version with "queue=ecgate" |
---|
| 84 | or "queue=cca". |
---|
| 85 | |
---|
| 86 | Parameters |
---|
| 87 | ---------- |
---|
[d69b677] | 88 | |
---|
[d727af2] | 89 | |
---|
[274f9ef] | 90 | Return |
---|
| 91 | ------ |
---|
[64cf353] | 92 | |
---|
| 93 | ''' |
---|
[ff99eae] | 94 | |
---|
[f20af73] | 95 | c, ppid, queue, job_template = setup_controldata() |
---|
[ff99eae] | 96 | |
---|
[efdb01a] | 97 | # on local side |
---|
[f20af73] | 98 | # starting from an ECMWF server this would also be the local side |
---|
[2fb99de] | 99 | called_from_dir = os.getcwd() |
---|
[f20af73] | 100 | if queue is None: |
---|
[64cf353] | 101 | if c.inputdir[0] != '/': |
---|
[ff99eae] | 102 | c.inputdir = os.path.join(called_from_dir, c.inputdir) |
---|
[64cf353] | 103 | if c.outputdir[0] != '/': |
---|
[ff99eae] | 104 | c.outputdir = os.path.join(called_from_dir, c.outputdir) |
---|
[54a8a01] | 105 | get_mars_data(c) |
---|
[2fb99de] | 106 | if c.request == 0 or c.request == 2: |
---|
[f20af73] | 107 | prepare_flexpart(ppid, c) |
---|
[96e1533] | 108 | exit_message = 'FLEX_EXTRACT IS DONE!' |
---|
[2fb99de] | 109 | else: |
---|
[96e1533] | 110 | exit_message = 'PRINTING MARS_REQUESTS DONE!' |
---|
| 111 | # send files to ECMWF server |
---|
[d69b677] | 112 | else: |
---|
[f20af73] | 113 | submit(job_template, c, queue) |
---|
[e0e99a5] | 114 | exit_message = 'FLEX_EXTRACT JOB SCRIPT IS SUBMITED!' |
---|
[64cf353] | 115 | |
---|
[403cbf1] | 116 | normal_exit(exit_message) |
---|
[96e1533] | 117 | |
---|
[64cf353] | 118 | return |
---|
| 119 | |
---|
| 120 | def submit(jtemplate, c, queue): |
---|
[ff2a11c] | 121 | '''Prepares the job script and submits it to the specified queue. |
---|
[274f9ef] | 122 | |
---|
| 123 | Parameters |
---|
| 124 | ---------- |
---|
[6f951ca] | 125 | jtemplate : str |
---|
[274f9ef] | 126 | Job template file from sub-directory "_templates" for |
---|
| 127 | submission to ECMWF. It contains all necessary |
---|
| 128 | module and variable settings for the ECMWF environment as well as |
---|
| 129 | the job call and mail report instructions. |
---|
| 130 | Default is "job.temp". |
---|
| 131 | |
---|
[6f951ca] | 132 | c : ControlFile |
---|
[274f9ef] | 133 | Contains all the parameters of CONTROL file and |
---|
| 134 | command line. |
---|
| 135 | |
---|
[6f951ca] | 136 | queue : str |
---|
[274f9ef] | 137 | Name of queue for submission to ECMWF (e.g. ecgate or cca ) |
---|
| 138 | |
---|
| 139 | Return |
---|
| 140 | ------ |
---|
| 141 | |
---|
[64cf353] | 142 | ''' |
---|
| 143 | |
---|
[d4696e0] | 144 | if not c.oper: |
---|
[54a8a01] | 145 | # --------- create on demand job script ------------------------------------ |
---|
[a4531f1] | 146 | if c.purefc: |
---|
[2fb99de] | 147 | print('---- Pure forecast mode! ----') |
---|
[54a8a01] | 148 | else: |
---|
[2fb99de] | 149 | print('---- On-demand mode! ----') |
---|
[54a8a01] | 150 | |
---|
[ff2a11c] | 151 | job_file = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
| 152 | jtemplate[:-5] + '.ksh') |
---|
[54a8a01] | 153 | |
---|
[f2616a3] | 154 | # divide time periode into specified number of job chunks |
---|
| 155 | # to have multiple job scripts |
---|
| 156 | if c.job_chunk: |
---|
| 157 | start = datetime.strptime(c.start_date, '%Y%m%d') |
---|
| 158 | end = datetime.strptime(c.end_date, '%Y%m%d') |
---|
| 159 | chunk = timedelta(days=c.job_chunk) |
---|
[fc05fbd] | 160 | oneday = timedelta(days=1) |
---|
[54a8a01] | 161 | |
---|
[f2616a3] | 162 | while start <= end: |
---|
| 163 | if (start + chunk) <= end: |
---|
[fc05fbd] | 164 | c.end_date = (start + chunk - oneday).strftime("%Y%m%d") |
---|
[f2616a3] | 165 | else: |
---|
| 166 | c.end_date = end.strftime("%Y%m%d") |
---|
| 167 | |
---|
| 168 | clist = c.to_list() |
---|
| 169 | mk_jobscript(jtemplate, job_file, clist) |
---|
| 170 | |
---|
| 171 | job_id = submit_job_to_ecserver(queue, job_file) |
---|
| 172 | print('The job id is: ' + str(job_id.strip())) |
---|
| 173 | |
---|
| 174 | start = start + chunk |
---|
| 175 | c.start_date = start.strftime("%Y%m%d") |
---|
| 176 | # submit a single job script |
---|
| 177 | else: |
---|
| 178 | clist = c.to_list() |
---|
| 179 | |
---|
| 180 | mk_jobscript(jtemplate, job_file, clist) |
---|
| 181 | |
---|
| 182 | job_id = submit_job_to_ecserver(queue, job_file) |
---|
| 183 | print('The job id is: ' + str(job_id.strip())) |
---|
[54a8a01] | 184 | |
---|
| 185 | else: |
---|
| 186 | # --------- create operational job script ---------------------------------- |
---|
[2fb99de] | 187 | print('---- Operational mode! ----') |
---|
[ff2a11c] | 188 | |
---|
[2fb99de] | 189 | job_file = os.path.join(_config.PATH_JOBSCRIPTS, |
---|
| 190 | jtemplate[:-5] + 'oper.ksh') |
---|
[54a8a01] | 191 | |
---|
| 192 | c.start_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
| 193 | c.end_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}' |
---|
[d4696e0] | 194 | c.basetime = '${MSJ_BASETIME}' |
---|
[ff2a11c] | 195 | if c.maxstep > 24: |
---|
[54a8a01] | 196 | c.time = '${MSJ_BASETIME} {MSJ_BASETIME}' |
---|
| 197 | |
---|
[ff2a11c] | 198 | clist = c.to_list() |
---|
[54a8a01] | 199 | |
---|
[ff2a11c] | 200 | mk_jobscript(jtemplate, job_file, clist) |
---|
[d69b677] | 201 | |
---|
[f2616a3] | 202 | job_id = submit_job_to_ecserver(queue, job_file) |
---|
| 203 | print('The job id is: ' + str(job_id.strip())) |
---|
| 204 | |
---|
| 205 | print('You should get an email per job with subject flex.hostname.pid') |
---|
[d69b677] | 206 | |
---|
[64cf353] | 207 | return |
---|
[d69b677] | 208 | |
---|
[ff2a11c] | 209 | def mk_jobscript(jtemplate, job_file, clist): |
---|
| 210 | '''Creates the job script from template. |
---|
| 211 | |
---|
| 212 | Parameters |
---|
| 213 | ---------- |
---|
[6f951ca] | 214 | jtemplate : str |
---|
[ff2a11c] | 215 | Job template file from sub-directory "_templates" for |
---|
| 216 | submission to ECMWF. It contains all necessary |
---|
| 217 | module and variable settings for the ECMWF environment as well as |
---|
| 218 | the job call and mail report instructions. |
---|
| 219 | Default is "job.temp". |
---|
| 220 | |
---|
[6f951ca] | 221 | job_file : str |
---|
[ff2a11c] | 222 | Path to the job script file. |
---|
| 223 | |
---|
[6f951ca] | 224 | clist : list of str |
---|
[ff2a11c] | 225 | Contains all necessary parameters for ECMWF CONTROL file. |
---|
| 226 | |
---|
| 227 | Return |
---|
| 228 | ------ |
---|
| 229 | |
---|
| 230 | ''' |
---|
| 231 | from genshi.template.text import NewTextTemplate |
---|
| 232 | from genshi.template import TemplateLoader |
---|
| 233 | from genshi.template.eval import UndefinedError |
---|
| 234 | |
---|
| 235 | # load template and insert control content as list |
---|
| 236 | try: |
---|
| 237 | loader = TemplateLoader(_config.PATH_TEMPLATES, auto_reload=False) |
---|
| 238 | control_template = loader.load(jtemplate, |
---|
| 239 | cls=NewTextTemplate) |
---|
| 240 | |
---|
| 241 | stream = control_template.generate(control_content=clist) |
---|
| 242 | except UndefinedError as e: |
---|
| 243 | print('... ERROR ' + str(e)) |
---|
| 244 | |
---|
| 245 | sys.exit('\n... error occured while trying to generate jobscript') |
---|
| 246 | except OSError as e: |
---|
| 247 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 248 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 249 | |
---|
| 250 | sys.exit('\n... error occured while trying to generate jobscript') |
---|
| 251 | |
---|
| 252 | # create jobscript file |
---|
| 253 | try: |
---|
| 254 | with open(job_file, 'w') as f: |
---|
| 255 | f.write(stream.render('text')) |
---|
| 256 | except OSError as e: |
---|
| 257 | print('... ERROR CODE: ' + str(e.errno)) |
---|
| 258 | print('... ERROR MESSAGE:\n \t ' + str(e.strerror)) |
---|
| 259 | |
---|
| 260 | sys.exit('\n... error occured while trying to write ' + job_file) |
---|
| 261 | |
---|
| 262 | return |
---|
| 263 | |
---|
[f2616a3] | 264 | |
---|
[d69b677] | 265 | if __name__ == "__main__": |
---|
| 266 | main() |
---|