[e18f4b5] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 4 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 5 | # |
---|
| 6 | # Functionality provided: Prepare input 3D-wind fields in hybrid coordinates + surface fields for FLEXPART runs |
---|
| 7 | # |
---|
| 8 | # Creation: October 2014 - Anne Fouilloux - University of Oslo |
---|
| 9 | # Extension November 2015 - Leopold Haimberger - University of Vienna for: |
---|
| 10 | # - using the WebAPI also for general MARS retrievals |
---|
| 11 | # - job submission on ecgate and cca |
---|
| 12 | # - job templates suitable for twice daily operational dissemination |
---|
| 13 | # - dividing retrievals of longer periods into digestable chunks |
---|
| 14 | # - retrieve also longer term forecasts, not only analyses and short term forecast data |
---|
| 15 | # - conversion into GRIB2 |
---|
| 16 | # - conversion into .fp format for faster execution of FLEXPART |
---|
| 17 | # |
---|
| 18 | # |
---|
| 19 | # Further documentation may be obtained from www.flexpart.eu |
---|
| 20 | # |
---|
| 21 | # Requirements: |
---|
| 22 | # in addition to a standard python 2.6 or 2.7 installation the following packages need to be installed |
---|
| 23 | # ECMWF WebMARS, gribAPI with python enabled, emoslib, ecaccess web toolkit, all available from https://software.ecmwf.int/ |
---|
| 24 | # dateutils |
---|
| 25 | # matplotlib (optional, for debugging) |
---|
| 26 | # |
---|
| 27 | # |
---|
| 28 | import calendar |
---|
| 29 | import shutil |
---|
| 30 | import datetime |
---|
| 31 | import time |
---|
| 32 | import os,sys,glob |
---|
| 33 | import subprocess |
---|
| 34 | import inspect |
---|
| 35 | # add path to submit.py to pythonpath so that python finds its buddies |
---|
| 36 | localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
---|
| 37 | sys.path.append(localpythonpath) |
---|
| 38 | from UIOTools import UIOFiles |
---|
| 39 | from string import strip |
---|
| 40 | from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter |
---|
| 41 | from GribTools import GribTools |
---|
| 42 | from FlexpartTools import EIFlexpart, Control, install_args_and_control |
---|
| 43 | from getMARSdata import getMARSdata |
---|
| 44 | from prepareFLEXPART import prepareFLEXPART |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | def main(): |
---|
| 49 | |
---|
| 50 | calledfromdir=os.getcwd() |
---|
| 51 | os.chdir(localpythonpath) |
---|
| 52 | args,c=install_args_and_control() |
---|
| 53 | # if c.outputdir[0]!='/': |
---|
| 54 | # c.outputdir=os.path.join(calledfromdir,c.outputdir) |
---|
| 55 | # c.inputdir=c.outputdir |
---|
| 56 | if args.install_target!=None: |
---|
| 57 | install_via_gateway(c,args.install_target) |
---|
| 58 | |
---|
| 59 | else: |
---|
| 60 | print 'Please specify installation target (local|ecgate|cca)' |
---|
| 61 | print 'use -h or --help for help' |
---|
| 62 | sys.exit() |
---|
| 63 | |
---|
| 64 | def install_via_gateway(c,target): |
---|
| 65 | |
---|
| 66 | ecd=c.flexextractdir |
---|
| 67 | template=ecd+'python/compilejob.temp' |
---|
| 68 | job=ecd+'python/compilejob.ksh' |
---|
| 69 | fo=open(job,'w') |
---|
| 70 | with open(template) as f: |
---|
| 71 | fdata = f.read().split('\n') |
---|
| 72 | for data in fdata: |
---|
| 73 | if 'MAKEFILE=' in data: |
---|
| 74 | if c.makefile is not None: |
---|
| 75 | data='export MAKEFILE='+c.makefile |
---|
| 76 | if 'FLEXPART_ROOT_SCRIPTS=' in data: |
---|
| 77 | if c.flexpart_root_scripts!='../': |
---|
| 78 | data='export FLEXPART_ROOT_SCRIPTS='+c.flexpart_root_scripts |
---|
| 79 | else: |
---|
| 80 | data='export FLEXPART_ROOT_SCRIPTS=$HOME' |
---|
| 81 | if target.lower()!='local': |
---|
| 82 | if '--workdir' in data: |
---|
| 83 | data='#SBATCH --workdir=/scratch/ms/'+c.ecgid+'/'+c.ecuid |
---|
| 84 | if '##PBS -o' in data: |
---|
| 85 | data='##PBS -o /scratch/ms/'+c.ecgid+'/'+c.ecuid+'flex_ecmwf.$Jobname.$Job_ID.out' |
---|
| 86 | if 'FLEXPART_ROOT_SCRIPTS=' in data: |
---|
| 87 | if c.ec_flexpart_root_scripts!='../': |
---|
| 88 | data='export FLEXPART_ROOT_SCRIPTS='+c.ec_flexpart_root_scripts |
---|
| 89 | else: |
---|
| 90 | data='export FLEXPART_ROOT_SCRIPTS=$HOME' |
---|
| 91 | |
---|
| 92 | fo.write(data+'\n') |
---|
| 93 | f.close() |
---|
| 94 | fo.close() |
---|
| 95 | |
---|
| 96 | if target.lower()!='local': |
---|
| 97 | template=ecd+'python/job.temp.o' |
---|
| 98 | with open(template) as f: |
---|
| 99 | fdata = f.read().split('\n') |
---|
| 100 | f.close() |
---|
| 101 | fo=open(template[:-2],'w') |
---|
| 102 | for data in fdata: |
---|
| 103 | if '--workdir' in data: |
---|
| 104 | data='#SBATCH --workdir=/scratch/ms/'+c.ecgid+'/'+c.ecuid |
---|
| 105 | if '##PBS -o' in data: |
---|
| 106 | data='##PBS -o /scratch/ms/'+c.ecgid+'/'+c.ecuid+'flex_ecmwf.$Jobname.$Job_ID.out' |
---|
| 107 | if 'export PATH=${PATH}:' in data: |
---|
[fb8810c] | 108 | data+=c.ec_flexpart_root_scripts+'/flex_extract_v7.0.4/python' |
---|
[e18f4b5] | 109 | |
---|
| 110 | if 'cat>>' in data or 'cat >>' in data: |
---|
| 111 | i=data.index('>') |
---|
| 112 | fo.write(data[:i]+data[i+1:]+'\n') |
---|
| 113 | fo.write('GATEWAY '+c.gateway+'\n') |
---|
| 114 | fo.write('DESTINATION '+c.destination+'\n') |
---|
| 115 | fo.write('EOF\n') |
---|
| 116 | |
---|
| 117 | fo.write(data+'\n') |
---|
| 118 | fo.close() |
---|
| 119 | |
---|
| 120 | job=ecd+'python/ECMWF_ENV' |
---|
| 121 | fo=open(job,'w') |
---|
| 122 | fo.write('ECUID '+c.ecuid+'\n') |
---|
| 123 | fo.write('ECGID '+c.ecgid+'\n') |
---|
| 124 | fo.write('GATEWAY '+c.gateway+'\n') |
---|
| 125 | fo.write('DESTINATION '+c.destination+'\n') |
---|
| 126 | fo.close() |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | if target.lower()=='local': |
---|
| 131 | # compile CONVERT2 |
---|
| 132 | if c.flexpart_root_scripts==None or c.flexpart_root_scripts=='../': |
---|
| 133 | print 'Warning: FLEXPART_ROOT_SCRIPTS has not been specified' |
---|
| 134 | print 'Only CONVERT2 will be compiled in '+ecd+'/../src' |
---|
| 135 | else: |
---|
| 136 | c.flexpart_root_scripts=os.path.expandvars(os.path.expanduser(c.flexpart_root_scripts)) |
---|
| 137 | if os.path.abspath(ecd)!=os.path.abspath(c.flexpart_root_scripts): |
---|
| 138 | os.chdir('/') |
---|
[fb8810c] | 139 | p=subprocess.check_call(['tar','-cvf',ecd+'../flex_extract_v7.0.4.tar',ecd+'python',ecd+'grib_templates',ecd+'src']) |
---|
[e18f4b5] | 140 | try: |
---|
[fb8810c] | 141 | os.makedirs(c.flexpart_root_scripts+'/flex_extract_v7.0.4') |
---|
[e18f4b5] | 142 | except: |
---|
| 143 | pass |
---|
[fb8810c] | 144 | os.chdir(c.flexpart_root_scripts+'/flex_extract_v7.0.4') |
---|
| 145 | p=subprocess.check_call(['tar','-xvf',ecd+'../flex_extract_v7.0.4.tar']) |
---|
| 146 | os.chdir(c.flexpart_root_scripts+'/flex_extract_v7.0.4/src') |
---|
[e18f4b5] | 147 | |
---|
| 148 | os.chdir('../src') |
---|
[fb8810c] | 149 | print 'install flex_extract_v7.0.4 software on '+target+' in directory '+os.getcwd() |
---|
[e18f4b5] | 150 | if c.makefile==None: |
---|
| 151 | makefile='Makefile.local.ifort' |
---|
| 152 | else: |
---|
| 153 | makefile=c.makefile |
---|
| 154 | flist=glob.glob('*.mod')+glob.glob('*.o') |
---|
| 155 | if flist: |
---|
| 156 | p=subprocess.check_call(['rm']+flist) |
---|
| 157 | try: |
---|
| 158 | print 'Using makefile: '+makefile |
---|
| 159 | p=subprocess.check_call(['make','-f',makefile]) |
---|
| 160 | p=subprocess.check_call(['ls','-l','CONVERT2']) |
---|
| 161 | except: |
---|
| 162 | print 'compile failed - please edit '+makefile+' or try another Makefile in the src directory.' |
---|
| 163 | print 'most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB and EMOSLIB must be adapted.' |
---|
| 164 | print 'Available Makefiles:' |
---|
| 165 | print glob.glob('Makefile*') |
---|
| 166 | |
---|
| 167 | elif target.lower()=='ecgate': |
---|
| 168 | os.chdir('/') |
---|
[fb8810c] | 169 | p=subprocess.check_call(['tar','-cvf',ecd+'../flex_extract_v7.0.4.tar',ecd+'python',ecd+'grib_templates',ecd+'src']) |
---|
[e18f4b5] | 170 | try: |
---|
[fb8810c] | 171 | p=subprocess.check_call(['ecaccess-file-put',ecd+'../flex_extract_v7.0.4.tar','ecgate:/home/ms/'+c.ecgid+'/'+c.ecuid+'/flex_extract_v7.0.4.tar']) |
---|
[e18f4b5] | 172 | except: |
---|
| 173 | print 'ecaccess-file-put failed! Probably the eccert key has expired.' |
---|
| 174 | exit(1) |
---|
| 175 | p=subprocess.check_call(['ecaccess-job-submit','-queueName',target,ecd+'python/compilejob.ksh']) |
---|
[fb8810c] | 176 | print 'compilejob.ksh has been submitted to ecgate for installation in '+c.ec_flexpart_root_scripts+'/flex_extract_v7.0.4' |
---|
[e18f4b5] | 177 | print 'You should get an email with subject flexcompile within the next few minutes' |
---|
| 178 | elif target.lower()=='cca': |
---|
| 179 | os.chdir('/') |
---|
[fb8810c] | 180 | p=subprocess.check_call(['tar','-cvf',ecd+'../flex_extract_v7.0.4.tar',ecd+'python',ecd+'grib_templates',ecd+'src']) |
---|
[e18f4b5] | 181 | try: |
---|
[fb8810c] | 182 | p=subprocess.check_call(['ecaccess-file-put',ecd+'../flex_extract_v7.0.4.tar','cca:/home/ms/'+c.ecgid+'/'+c.ecuid+'/flex_extract_v7.0.4.tar']) |
---|
[e18f4b5] | 183 | except: |
---|
| 184 | print 'ecaccess-file-put failed! Probably the eccert key has expired.' |
---|
| 185 | exit(1) |
---|
| 186 | |
---|
| 187 | p=subprocess.check_call(['ecaccess-job-submit','-queueName',target,ecd+'python/compilejob.ksh']) |
---|
[fb8810c] | 188 | print 'compilejob.ksh has been submitted to cca for installation in '+c.ec_flexpart_root_scripts+'/flex_extract_v7.0.4' |
---|
[e18f4b5] | 189 | print 'You should get an email with subject flexcompile within the next few minutes' |
---|
| 190 | else: |
---|
| 191 | print 'ERROR: unknown installation target ',target |
---|
| 192 | print 'Valid targets: ecgate, cca, local' |
---|
| 193 | return |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | if __name__ == "__main__": |
---|
| 197 | main() |
---|