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