[d69b677] | 1 | #!/usr/bin/env python |
---|
[64cf353] | 2 | # -*- coding: utf-8 -*- |
---|
| 3 | #************************************************************************ |
---|
| 4 | # TODO AP |
---|
| 5 | #AP |
---|
| 6 | # - Functionality Provided is not correct for this file |
---|
| 7 | # - localpythonpath should not be set in module load section! |
---|
| 8 | # - create a class Installation and divide installation in 3 subdefs for |
---|
| 9 | # ecgate, local and cca seperatly |
---|
| 10 | # - Change History ist nicht angepasst ans File! Original geben lassen |
---|
| 11 | #************************************************************************ |
---|
| 12 | """ |
---|
| 13 | @Author: Anne Fouilloux (University of Oslo) |
---|
| 14 | |
---|
| 15 | @Date: October 2014 |
---|
| 16 | |
---|
| 17 | @ChangeHistory: |
---|
| 18 | November 2015 - Leopold Haimberger (University of Vienna): |
---|
| 19 | - using the WebAPI also for general MARS retrievals |
---|
| 20 | - job submission on ecgate and cca |
---|
| 21 | - job templates suitable for twice daily operational dissemination |
---|
| 22 | - dividing retrievals of longer periods into digestable chunks |
---|
| 23 | - retrieve also longer term forecasts, not only analyses and |
---|
| 24 | short term forecast data |
---|
| 25 | - conversion into GRIB2 |
---|
| 26 | - conversion into .fp format for faster execution of FLEXPART |
---|
| 27 | |
---|
| 28 | February 2018 - Anne Philipp (University of Vienna): |
---|
| 29 | - applied PEP8 style guide |
---|
| 30 | - added documentation |
---|
| 31 | |
---|
| 32 | @License: |
---|
| 33 | (C) Copyright 2014 UIO. |
---|
| 34 | |
---|
| 35 | This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 36 | which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 37 | |
---|
| 38 | @Requirements: |
---|
| 39 | - A standard python 2.6 or 2.7 installation |
---|
| 40 | - dateutils |
---|
| 41 | - matplotlib (optional, for debugging) |
---|
| 42 | - ECMWF specific packages, all available from https://software.ecmwf.int/ |
---|
| 43 | ECMWF WebMARS, gribAPI with python enabled, emoslib and |
---|
| 44 | ecaccess web toolkit |
---|
| 45 | |
---|
| 46 | @Description: |
---|
| 47 | Further documentation may be obtained from www.flexpart.eu. |
---|
| 48 | |
---|
| 49 | Functionality provided: |
---|
| 50 | Prepare input 3D-wind fields in hybrid coordinates + |
---|
| 51 | surface fields for FLEXPART runs |
---|
| 52 | """ |
---|
| 53 | # ------------------------------------------------------------------------------ |
---|
| 54 | # MODULES |
---|
| 55 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 56 | import calendar |
---|
| 57 | import shutil |
---|
| 58 | import datetime |
---|
| 59 | import time |
---|
| 60 | import os,sys,glob |
---|
| 61 | import subprocess |
---|
| 62 | import inspect |
---|
| 63 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
| 64 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
| 65 | sys.path.append(localpythonpath) |
---|
| 66 | from UIOTools import UIOFiles |
---|
| 67 | from string import strip |
---|
| 68 | from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter |
---|
| 69 | from GribTools import GribTools |
---|
[02c8c50] | 70 | from FlexpartTools import ECFlexpart, Control, install_args_and_control |
---|
[d69b677] | 71 | from getMARSdata import getMARSdata |
---|
| 72 | from prepareFLEXPART import prepareFLEXPART |
---|
| 73 | |
---|
[64cf353] | 74 | # ------------------------------------------------------------------------------ |
---|
| 75 | # FUNCTIONS |
---|
| 76 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 77 | def main(): |
---|
[a4b6cef] | 78 | ''' |
---|
| 79 | ''' |
---|
[d69b677] | 80 | os.chdir(localpythonpath) |
---|
[a4b6cef] | 81 | args, c = install_args_and_control() |
---|
| 82 | if args.install_target is not None: |
---|
| 83 | install_via_gateway(c, args.install_target) |
---|
[d69b677] | 84 | else: |
---|
[a4b6cef] | 85 | print('Please specify installation target (local|ecgate|cca)') |
---|
| 86 | print('use -h or --help for help') |
---|
[d69b677] | 87 | sys.exit() |
---|
[a4b6cef] | 88 | |
---|
| 89 | def install_via_gateway(c, target): |
---|
| 90 | |
---|
| 91 | ecd = c.ecmwfdatadir |
---|
| 92 | template = ecd + 'python/compilejob.temp' |
---|
| 93 | job = ecd + 'python/compilejob.ksh' |
---|
| 94 | fo = open(job, 'w') |
---|
| 95 | #AP could do with open(template) as f, open(job, 'w') as fo: |
---|
| 96 | #AP or nested with statements |
---|
[d69b677] | 97 | with open(template) as f: |
---|
[a4b6cef] | 98 | fdata = f.read().split('\n') |
---|
| 99 | for data in fdata: |
---|
| 100 | if 'MAKEFILE=' in data: |
---|
| 101 | if c.makefile is not None: |
---|
| 102 | data = 'export MAKEFILE=' + c.makefile |
---|
| 103 | if 'FLEXPART_ROOT_SCRIPTS=' in data: |
---|
| 104 | if c.flexpart_root_scripts != '../': |
---|
| 105 | data = 'export FLEXPART_ROOT_SCRIPTS=' + \ |
---|
| 106 | c.flexpart_root_scripts |
---|
| 107 | else: |
---|
| 108 | data='export FLEXPART_ROOT_SCRIPTS=$HOME' |
---|
| 109 | if target.lower() != 'local': |
---|
| 110 | if '--workdir' in data: |
---|
| 111 | data = '#SBATCH --workdir=/scratch/ms/' + c.ecgid + \ |
---|
| 112 | '/' + c.ecuid |
---|
| 113 | if '##PBS -o' in data: |
---|
| 114 | data = '##PBS -o /scratch/ms/' + c.ecgid + '/' + c.ecuid + \ |
---|
| 115 | 'flex_ecmwf.$Jobname.$Job_ID.out' |
---|
| 116 | if 'FLEXPART_ROOT_SCRIPTS=' in data: |
---|
| 117 | if c.ec_flexpart_root_scripts != '../': |
---|
| 118 | data = 'export FLEXPART_ROOT_SCRIPTS=' + \ |
---|
| 119 | c.ec_flexpart_root_scripts |
---|
| 120 | else: |
---|
| 121 | data = 'export FLEXPART_ROOT_SCRIPTS=$HOME' |
---|
| 122 | fo.write(data + '\n') |
---|
[d69b677] | 123 | f.close() |
---|
| 124 | fo.close() |
---|
[a4b6cef] | 125 | |
---|
| 126 | if target.lower() != 'local': |
---|
| 127 | template = ecd + 'python/job.temp.o' |
---|
[64cf353] | 128 | #AP hier eventuell Zeile für Zeile lesen und dann if Entscheidung |
---|
[a4b6cef] | 129 | with open(template) as f: |
---|
| 130 | fdata = f.read().split('\n') |
---|
| 131 | f.close() |
---|
| 132 | fo = open(template[:-2], 'w') |
---|
| 133 | for data in fdata: |
---|
| 134 | if '--workdir' in data: |
---|
| 135 | data = '#SBATCH --workdir=/scratch/ms/' + c.ecgid + \ |
---|
| 136 | '/' + c.ecuid |
---|
| 137 | if '##PBS -o' in data: |
---|
| 138 | data = '##PBS -o /scratch/ms/' + c.ecgid + '/' + \ |
---|
| 139 | c.ecuid + 'flex_ecmwf.$Jobname.$Job_ID.out' |
---|
| 140 | if 'export PATH=${PATH}:' in data: |
---|
| 141 | data += c.ec_flexpart_root_scripts + '/ECMWFDATA7.0/python' |
---|
| 142 | if 'cat>>' in data or 'cat >>' in data: |
---|
| 143 | i = data.index('>') |
---|
| 144 | fo.write(data[:i] + data[i+1:] + '\n') |
---|
| 145 | fo.write('GATEWAY ' + c.gateway + '\n') |
---|
| 146 | fo.write('DESTINATION ' + c.destination + '\n') |
---|
| 147 | fo.write('EOF\n') |
---|
| 148 | |
---|
| 149 | fo.write(data + '\n') |
---|
| 150 | fo.close() |
---|
| 151 | |
---|
| 152 | job = ecd + 'python/ECMWF_ENV' |
---|
| 153 | with open(job, 'w') as fo: |
---|
| 154 | fo.write('ECUID ' + c.ecuid + '\n') |
---|
| 155 | fo.write('ECGID ' + c.ecgid + '\n') |
---|
| 156 | fo.write('GATEWAY ' + c.gateway + '\n') |
---|
| 157 | fo.write('DESTINATION ' + c.destination + '\n') |
---|
| 158 | fo.close() |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | if target.lower() == 'local': |
---|
[d69b677] | 163 | # compile CONVERT2 |
---|
[a4b6cef] | 164 | if c.flexpart_root_scripts is None or c.flexpart_root_scripts == '../': |
---|
| 165 | print('Warning: FLEXPART_ROOT_SCRIPTS has not been specified') |
---|
| 166 | print('Only CONVERT2 will be compiled in ' + ecd + '/../src') |
---|
| 167 | else: |
---|
| 168 | c.flexpart_root_scripts = os.path.expandvars(os.path.expanduser( |
---|
| 169 | c.flexpart_root_scripts)) |
---|
| 170 | if os.path.abspath(ecd) != os.path.abspath(c.flexpart_root_scripts): |
---|
| 171 | os.chdir('/') |
---|
| 172 | p = subprocess.check_call(['tar', '-cvf', |
---|
| 173 | ecd + '../ECMWFDATA7.0.tar', |
---|
| 174 | ecd + 'python', |
---|
| 175 | ecd + 'grib_templates', |
---|
| 176 | ecd + 'src']) |
---|
| 177 | try: |
---|
| 178 | os.makedirs(c.flexpart_root_scripts + '/ECMWFDATA7.0') |
---|
| 179 | except: |
---|
| 180 | pass |
---|
| 181 | os.chdir(c.flexpart_root_scripts + '/ECMWFDATA7.0') |
---|
| 182 | p = subprocess.check_call(['tar', '-xvf', |
---|
| 183 | ecd + '../ECMWFDATA7.0.tar']) |
---|
| 184 | os.chdir(c.flexpart_root_scripts + '/ECMWFDATA7.0/src') |
---|
| 185 | |
---|
| 186 | os.chdir('../src') |
---|
| 187 | print(('install ECMWFDATA7.0 software on ' + target + ' in directory ' |
---|
| 188 | + os.getcwd())) |
---|
| 189 | if c.makefile is None: |
---|
| 190 | makefile = 'Makefile.local.ifort' |
---|
| 191 | else: |
---|
| 192 | makefile = c.makefile |
---|
| 193 | flist = glob.glob('*.mod') + glob.glob('*.o') |
---|
| 194 | if flist: |
---|
| 195 | p = subprocess.check_call(['rm'] + flist) |
---|
[d69b677] | 196 | try: |
---|
[a4b6cef] | 197 | print(('Using makefile: ' + makefile)) |
---|
| 198 | p = subprocess.check_call(['make', '-f', makefile]) |
---|
| 199 | p = subprocess.check_call(['ls', '-l',' CONVERT2']) |
---|
| 200 | except: |
---|
| 201 | print('compile failed - please edit ' + makefile + |
---|
| 202 | ' or try another Makefile in the src directory.') |
---|
| 203 | print('most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB \ |
---|
| 204 | and EMOSLIB must be adapted.') |
---|
| 205 | print('Available Makefiles:') |
---|
| 206 | print(glob.glob('Makefile*')) |
---|
| 207 | |
---|
| 208 | elif target.lower() == 'ecgate': |
---|
[d69b677] | 209 | os.chdir('/') |
---|
[a4b6cef] | 210 | p = subprocess.check_call(['tar', '-cvf', |
---|
| 211 | ecd + '../ECMWFDATA7.0.tar', |
---|
| 212 | ecd + 'python', |
---|
| 213 | ecd + 'grib_templates', |
---|
| 214 | ecd + 'src']) |
---|
| 215 | try: |
---|
| 216 | p = subprocess.check_call(['ecaccess-file-put', |
---|
| 217 | ecd + '../ECMWFDATA7.0.tar', |
---|
| 218 | 'ecgate:/scratch/ms/' + c.ecgid + '/' + |
---|
| 219 | c.ecuid + '/ECMWFDATA7.0.tar']) |
---|
| 220 | except: |
---|
| 221 | print('ecaccess-file-put failed! Probably the eccert key has expired.') |
---|
| 222 | exit(1) |
---|
| 223 | p = subprocess.check_call(['ecaccess-job-submit', |
---|
| 224 | '-queueName', |
---|
| 225 | target, |
---|
| 226 | ecd + 'python/compilejob.ksh']) |
---|
| 227 | print('compilejob.ksh has been submitted to ecgate for \ |
---|
| 228 | installation in ' + c.ec_flexpart_root_scripts + |
---|
| 229 | '/ECMWFDATA7.0') |
---|
| 230 | print('You should get an email with subject flexcompile within \ |
---|
| 231 | the next few minutes') |
---|
| 232 | |
---|
| 233 | elif target.lower() == 'cca': |
---|
[d69b677] | 234 | os.chdir('/') |
---|
[a4b6cef] | 235 | p = subprocess.check_call(['tar', '-cvf', |
---|
| 236 | ecd + '../ECMWFDATA7.0.tar', |
---|
| 237 | ecd + 'python', |
---|
| 238 | ecd + 'grib_templates', |
---|
| 239 | ecd + 'src']) |
---|
| 240 | try: |
---|
| 241 | p = subprocess.check_call(['ecaccess-file-put', |
---|
| 242 | ecd + '../ECMWFDATA7.0.tar', |
---|
| 243 | 'cca:/scratch/ms/' + c.ecgid + '/' + |
---|
| 244 | c.ecuid + '/ECMWFDATA7.0.tar']) |
---|
| 245 | except: |
---|
| 246 | print('ecaccess-file-put failed! \ |
---|
| 247 | Probably the eccert key has expired.') |
---|
| 248 | exit(1) |
---|
| 249 | |
---|
| 250 | p=subprocess.check_call(['ecaccess-job-submit', |
---|
| 251 | '-queueName', |
---|
| 252 | target, |
---|
| 253 | ecd + 'python/compilejob.ksh'])) |
---|
| 254 | print('compilejob.ksh has been submitted to cca for installation in ' + |
---|
| 255 | c.ec_flexpart_root_scripts + '/ECMWFDATA7.0') |
---|
| 256 | print('You should get an email with subject flexcompile \ |
---|
| 257 | within the next few minutes') |
---|
[64cf353] | 258 | |
---|
[d69b677] | 259 | else: |
---|
[a4b6cef] | 260 | print('ERROR: unknown installation target ', target) |
---|
| 261 | print('Valid targets: ecgate, cca, local') |
---|
| 262 | |
---|
[d69b677] | 263 | return |
---|
| 264 | |
---|
[a4b6cef] | 265 | |
---|
[d69b677] | 266 | if __name__ == "__main__": |
---|
| 267 | main() |
---|