source: flex_extract.git/python/install.py @ a4b6cef

ctbtodev
Last change on this file since a4b6cef was a4b6cef, checked in by Anne Philipp <bscannephilipp@…>, 6 years ago

pep8 changes on install.py

  • Property mode set to 100644
File size: 9.9 KB
Line 
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#
28import calendar
29import shutil
30import datetime
31import time
32import os,sys,glob
33import subprocess
34import inspect
35# add path to submit.py to pythonpath so that python finds its buddies
36localpythonpath=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
37sys.path.append(localpythonpath)
38from UIOTools import UIOFiles
39from string import strip
40from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter
41from GribTools import GribTools
42from FlexpartTools import EIFlexpart, Control, install_args_and_control
43from getMARSdata import getMARSdata
44from prepareFLEXPART import prepareFLEXPART
45
46
47def main():
48    '''
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 is not None:
57        install_via_gateway(c, args.install_target)
58    else:
59        print('Please specify installation target (local|ecgate|cca)')
60        print('use -h or --help for help')
61    sys.exit()
62
63def install_via_gateway(c, target):
64
65    ecd = c.ecmwfdatadir
66    template = ecd + 'python/compilejob.temp'
67    job = ecd + 'python/compilejob.ksh'
68    fo = open(job, 'w')
69#AP could do with open(template) as f, open(job, 'w') as fo:
70#AP or nested with statements
71    with open(template) as f:
72        fdata = f.read().split('\n')
73        for data in fdata:
74            if 'MAKEFILE=' in data:
75                if c.makefile is not None:
76                    data = 'export MAKEFILE=' + c.makefile
77            if 'FLEXPART_ROOT_SCRIPTS=' in data:
78                if c.flexpart_root_scripts != '../':
79                    data = 'export FLEXPART_ROOT_SCRIPTS=' + \
80                            c.flexpart_root_scripts
81                else:
82                    data='export FLEXPART_ROOT_SCRIPTS=$HOME'
83            if target.lower() != 'local':
84                if '--workdir' in data:
85                    data = '#SBATCH --workdir=/scratch/ms/' + c.ecgid + \
86                            '/' + c.ecuid
87                if '##PBS -o' in data:
88                    data = '##PBS -o /scratch/ms/' + c.ecgid + '/' + c.ecuid + \
89                            'flex_ecmwf.$Jobname.$Job_ID.out'
90                if 'FLEXPART_ROOT_SCRIPTS=' in data:
91                    if c.ec_flexpart_root_scripts != '../':
92                        data = 'export FLEXPART_ROOT_SCRIPTS=' + \
93                                c.ec_flexpart_root_scripts
94                    else:
95                        data = 'export FLEXPART_ROOT_SCRIPTS=$HOME'
96            fo.write(data + '\n')
97    f.close()
98    fo.close()
99
100    if target.lower() != 'local':
101        template = ecd + 'python/job.temp.o'
102#AP hier eventuell Zeile für Zeile lesen und dann if Entscheidung
103        with open(template) as f:
104            fdata = f.read().split('\n')
105        f.close()
106        fo = open(template[:-2], 'w')
107        for data in fdata:
108            if '--workdir' in data:
109                data = '#SBATCH --workdir=/scratch/ms/' + c.ecgid + \
110                        '/' + c.ecuid
111            if '##PBS -o' in data:
112                data = '##PBS -o /scratch/ms/' + c.ecgid + '/' + \
113                        c.ecuid + 'flex_ecmwf.$Jobname.$Job_ID.out'
114            if  'export PATH=${PATH}:' in data:
115                data += c.ec_flexpart_root_scripts + '/ECMWFDATA7.0/python'
116            if 'cat>>' in data or 'cat >>' in data:
117                i = data.index('>')
118                fo.write(data[:i] + data[i+1:] + '\n')
119                fo.write('GATEWAY ' + c.gateway + '\n')
120                fo.write('DESTINATION ' + c.destination + '\n')
121                fo.write('EOF\n')
122
123            fo.write(data + '\n')
124        fo.close()
125
126        job = ecd + 'python/ECMWF_ENV'
127        with open(job, 'w') as fo:
128            fo.write('ECUID ' + c.ecuid + '\n')
129            fo.write('ECGID ' + c.ecgid + '\n')
130            fo.write('GATEWAY ' + c.gateway + '\n')
131            fo.write('DESTINATION ' + c.destination + '\n')
132        fo.close()
133
134
135
136    if target.lower() == 'local':
137        # compile CONVERT2
138        if c.flexpart_root_scripts is None or c.flexpart_root_scripts == '../':
139            print('Warning: FLEXPART_ROOT_SCRIPTS has not been specified')
140            print('Only CONVERT2 will be compiled in ' + ecd + '/../src')
141        else:
142            c.flexpart_root_scripts = os.path.expandvars(os.path.expanduser(
143                                        c.flexpart_root_scripts))
144            if os.path.abspath(ecd) != os.path.abspath(c.flexpart_root_scripts):
145                os.chdir('/')
146                p = subprocess.check_call(['tar', '-cvf',
147                                           ecd + '../ECMWFDATA7.0.tar',
148                                           ecd + 'python',
149                                           ecd + 'grib_templates',
150                                           ecd + 'src'])
151                try:
152                    os.makedirs(c.flexpart_root_scripts + '/ECMWFDATA7.0')
153                except:
154                    pass
155                os.chdir(c.flexpart_root_scripts + '/ECMWFDATA7.0')
156                p = subprocess.check_call(['tar', '-xvf',
157                                           ecd + '../ECMWFDATA7.0.tar'])
158                os.chdir(c.flexpart_root_scripts + '/ECMWFDATA7.0/src')
159
160        os.chdir('../src')
161        print(('install ECMWFDATA7.0 software on ' + target + ' in directory '
162               + os.getcwd()))
163        if c.makefile is None:
164            makefile = 'Makefile.local.ifort'
165        else:
166            makefile = c.makefile
167        flist = glob.glob('*.mod') + glob.glob('*.o')
168        if flist:
169            p = subprocess.check_call(['rm'] + flist)
170        try:
171            print(('Using makefile: ' + makefile))
172            p = subprocess.check_call(['make', '-f', makefile])
173            p = subprocess.check_call(['ls', '-l',' CONVERT2'])
174        except:
175            print('compile failed - please edit ' + makefile +
176                  ' or try another Makefile in the src directory.')
177            print('most likely GRIB_API_INCLUDE_DIR, GRIB_API_LIB \
178                    and EMOSLIB must be adapted.')
179            print('Available Makefiles:')
180            print(glob.glob('Makefile*'))
181
182    elif target.lower() == 'ecgate':
183        os.chdir('/')
184        p = subprocess.check_call(['tar', '-cvf',
185                                   ecd + '../ECMWFDATA7.0.tar',
186                                   ecd + 'python',
187                                   ecd + 'grib_templates',
188                                   ecd + 'src'])
189        try:
190            p = subprocess.check_call(['ecaccess-file-put',
191                                       ecd + '../ECMWFDATA7.0.tar',
192                                       'ecgate:/scratch/ms/' + c.ecgid + '/' +
193                                       c.ecuid + '/ECMWFDATA7.0.tar'])
194        except:
195            print('ecaccess-file-put failed! Probably the eccert key has expired.')
196            exit(1)
197        p = subprocess.check_call(['ecaccess-job-submit',
198                                   '-queueName',
199                                   target,
200                                   ecd + 'python/compilejob.ksh'])
201        print('compilejob.ksh has been submitted to ecgate for \
202                installation in ' + c.ec_flexpart_root_scripts +
203                '/ECMWFDATA7.0')
204        print('You should get an email with subject flexcompile within \
205                the next few minutes')
206
207    elif target.lower() == 'cca':
208        os.chdir('/')
209        p = subprocess.check_call(['tar', '-cvf',
210                                   ecd + '../ECMWFDATA7.0.tar',
211                                   ecd + 'python',
212                                   ecd + 'grib_templates',
213                                   ecd + 'src'])
214        try:
215            p = subprocess.check_call(['ecaccess-file-put',
216                                       ecd + '../ECMWFDATA7.0.tar',
217                                       'cca:/scratch/ms/' + c.ecgid + '/' +
218                                       c.ecuid + '/ECMWFDATA7.0.tar'])
219        except:
220            print('ecaccess-file-put failed! \
221                    Probably the eccert key has expired.')
222            exit(1)
223
224        p=subprocess.check_call(['ecaccess-job-submit',
225                                '-queueName',
226                                target,
227                                ecd + 'python/compilejob.ksh']))
228        print('compilejob.ksh has been submitted to cca for installation in ' +
229              c.ec_flexpart_root_scripts + '/ECMWFDATA7.0')
230        print('You should get an email with subject flexcompile \
231                within the next few minutes')
232    else:
233        print('ERROR: unknown installation target ', target)
234        print('Valid targets: ecgate, cca, local')
235
236    return
237
238
239if __name__ == "__main__":
240    main()
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG