Changeset 54a8a01 in flex_extract.git for python/submit.py


Ignore:
Timestamp:
Aug 31, 2018, 7:50:37 AM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
597d4d1
Parents:
e1228f3
Message:

restructuring, documentations and bug fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/submit.py

    rff99eae r54a8a01  
    4444import subprocess
    4545import inspect
     46import collections
    4647
    4748# software specific classes and modules from flex_extract
    48 from tools import interpret_args_and_control, normal_exit
     49import _config
     50from tools import normal_exit, get_cmdline_arguments, submit_job_to_ecserver, \
     51                  read_ecenv
    4952from get_mars_data import get_mars_data
    5053from prepare_flexpart import prepare_flexpart
    51 
    52 # add path to pythonpath so that python finds its buddies
    53 LOCAL_PYTHON_PATH = os.path.dirname(os.path.abspath(
    54     inspect.getfile(inspect.currentframe())))
    55 if LOCAL_PYTHON_PATH not in sys.path:
    56     sys.path.append(LOCAL_PYTHON_PATH)
     54from ControlFile import ControlFile
    5755
    5856# ------------------------------------------------------------------------------
     
    7674
    7775    called_from_dir = os.getcwd()
    78     args, c = interpret_args_and_control()
     76
     77    args = get_cmdline_arguments()
     78
     79    try:
     80        c = ControlFile(args.controlfile)
     81    except IOError:
     82        try:
     83            c = ControlFile(LOCAL_PYTHON_PATH + args.controlfile)
     84        except IOError:
     85            print 'Could not read CONTROL file "' + args.controlfile + '"'
     86            print 'Either it does not exist or its syntax is wrong.'
     87            print 'Try "' + sys.argv[0].split('/')[-1] + \
     88                  ' -h" to print usage information'
     89            sys.exit(1)
     90
     91    env_parameter = read_ecenv(c.ecmwfdatadir + 'python/ECMWF_ENV')
     92    c.assign_args_to_control(args)
     93    c.assign_envs_to_control(env_parameter)
     94    c.check_conditions()
    7995
    8096    # on local side
     97    # on ECMWF server this would be the local side
    8198    if args.queue is None:
    8299        if c.inputdir[0] != '/':
     
    84101        if c.outputdir[0] != '/':
    85102            c.outputdir = os.path.join(called_from_dir, c.outputdir)
    86         get_mars_data(args, c)
    87         prepare_flexpart(args, c)
    88         normal_exit(c)
     103        get_mars_data(c)
     104        prepare_flexpart(args.ppid, c)
     105        normal_exit(c.mailfail, 'Done!')
    89106    # on ECMWF server
    90107    else:
     
    125142    '''
    126143
    127     # read template file and split from newline signs
     144    # read template file and get index for CONTROL input
    128145    with open(jtemplate) as f:
    129146        lftext = f.read().split('\n')
    130         insert_point = lftext.index('EOF')
     147    insert_point = lftext.index('EOF')
    131148
    132     # put all parameters of ControlFile instance into a list
    133     clist = c.to_list() # ondemand
    134     colist = []  # operational
    135     mt = 0
     149    if not c.basetime:
     150    # --------- create on demand job script ------------------------------------
     151        if c.maxstep > 24:
     152            print '---- Pure forecast mode! ----'
     153        else:
     154            print '---- On-demand mode! ----'
     155        job_file = jtemplate[:-4] + 'ksh'
     156        clist = c.to_list()
    136157
    137     for elem in clist:
    138         if 'maxstep' in elem:
    139             mt = int(elem.split(' ')[1])
     158        lftextondemand = lftext[:insert_point] + clist + lftext[insert_point:]
    140159
    141     for elem in clist:
    142         if 'start_date' in elem:
    143             elem = 'start_date ' + '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}'
    144         if 'end_date' in elem:
    145             elem = 'end_date ' + '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}'
    146         if 'base_time' in elem:
    147             elem = 'base_time ' + '${MSJ_BASETIME}'
    148         if 'time' in elem and mt > 24:
    149             elem = 'time ' + '${MSJ_BASETIME} {MSJ_BASETIME}'
    150         colist.append(elem)
     160        with open(job_file, 'w') as f:
     161            f.write('\n'.join(lftextondemand))
    151162
    152     lftextondemand = lftext[:insert_point] + clist + lftext[insert_point + 2:]
    153     lftextoper = lftext[:insert_point] + colist + lftext[insert_point + 2:]
     163        submit_job_to_ecserver('', queue, job_file)
    154164
    155     with open('job.ksh', 'w') as h:
    156         h.write('\n'.join(lftextondemand))
     165    else:
     166    # --------- create operational job script ----------------------------------
     167        print '---- Operational mode! ----'
     168        job_file = jtemplate[:-5] + 'oper.ksh'
     169        #colist = []
    157170
    158     with open('joboper.ksh', 'w') as h:
    159         h.write('\n'.join(lftextoper))
     171        if c.maxstep:
     172            mt = int(c.maxstep)
     173        else:
     174            mt = 0
    160175
    161     # submit job script to queue
    162     try:
    163         p = subprocess.check_call(['ecaccess-job-submit', '-queueName',
    164                                    queue, 'job.ksh'])
    165     except subprocess.CalledProcessError as e:
    166         print 'ecaccess-job-submit failed!'
    167         print 'Error Message: '
    168         print e.output
    169         exit(1)
     176        c.start_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}'
     177        c.end_date = '${MSJ_YEAR}${MSJ_MONTH}${MSJ_DAY}'
     178        c.base_time = '${MSJ_BASETIME}'
     179        if mt > 24:
     180            c.time = '${MSJ_BASETIME} {MSJ_BASETIME}'
    170181
     182        colist = c.to_list()
     183
     184        lftextoper = lftext[:insert_point] + colist + lftext[insert_point + 2:]
     185
     186        with open(job_file, 'w') as f:
     187            f.write('\n'.join(lftextoper))
     188
     189        submit_job_to_ecserver('', queue, job_file)
     190
     191    # --------------------------------------------------------------------------
    171192    print 'You should get an email with subject flex.hostname.pid'
    172193
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG