initexit.py 24.8 KB
Newer Older
1
2
3
4
#!/usr/bin/env python
# da_initexit.py

"""
Peters, Wouter's avatar
Peters, Wouter committed
5
6
.. module:: initexit
.. moduleauthor:: Wouter Peters 
7
8
9
10

Revision History:
File created on 13 May 2009.

Peters, Wouter's avatar
Peters, Wouter committed
11
The CycleControl class is found in the module :mod:`initexit`. It is derived from the standard python :class:`dictionary` object. It is the only core object of CTDAS that is automatically created in the pipeline, the user (normally) does not need to modify or extend it. The class is created based on options and arguments passes on the command line when submitting your main CTDAS job. 
12
13
14

Valid options are defined in 

15
.. autofunction:: da.tools.initexit.parse_options
16
17
18
19
20
21

With the name of a valid ``rc-file``, the CycleControl object is instantiated and validated. An example rc-file looks
like this:::

    ! Info on the data assimilation cycle

22
23
24
25
26
27
    time.restart        : False                     ! Restart from an existing run T/F
    time.start          : 2000-01-01 00:00:00       ! Start time of first cycle
    time.finish         : 2000-01-08 00:00:00       ! End time of last cycle
    time.cycle          : 7                         ! length of each cycle, 7 means one week
    time.nlag           : 5                         ! number of cycles in one smoother window
    dir.da_run          : ${HOME}/tmp/test_da       ! the run directory for you project
28
29
30

    ! Info on the DA system used

31
32
    da.system           : CarbonTracker             ! an identifier for your inversion system
    da.system.rc        : da/rc/carbontracker.rc    ! the settings needed in your inversion system
33
34
35

    ! Info on the forward model to be used

36
37
38
    da.obsoperator         : TM5                                ! an identifier for your observation operator
    da.obsoperator.rc      : ${HOME}/Modeling/TM5/tm5-ctdas.rc  ! the rc-file needed to run youobservation operator
    da.optimizer.nmembers  : 30                                 ! the number of ensemble members desired in the optimization
39
40
41
42

The most important method of the CycleControl object are listed below:

.. autoclass:: da.tools.initexit.CycleControl 
43
   :members: initialize, finalize,  collect_restart_data, move_restart_data, 
44
             submit_next_cycle, setup_file_structure, recover_run, random_seed
45
46
47
48
49
50
51

Two important attributes of the CycleControl object are:
    (1) DaSystem, an instance of a :ref:`dasystem`
    (2) DaPlatForm, an instance of a :ref:`platform`

Other functions in the module initexit that are related to the control of a DA cycle are:

52
53
.. autofunction:: da.tools.initexit.start_logger 
.. autofunction:: da.tools.initexit.validate_opts_args 
54
55


56
"""
karolina's avatar
karolina committed
57
58
59
60
import logging
import os
import sys
import shutil
karolina's avatar
karolina committed
61
import copy
62
63
64
import getopt
import cPickle
import numpy as np
karolina's avatar
karolina committed
65
66
67
from string import join

import da.tools.rc as rc
68
from da.tools.general import create_dirs, to_datetime, advance_time
69

karolina's avatar
karolina committed
70
needed_da_items = [
71
72
73
74
75
    'time.start',
    'time.finish',
    'time.nlag',
    'time.cycle',
    'dir.da_run',
76
77
78
79
80
    'da.system',
    'da.system.rc',
    'da.obsoperator',
    'da.obsoperator.rc',
    'da.optimizer.nmembers']
81
82
83
84
85
86
87

# only needed in an earlier implemented where each substep was a separate job
# validprocesses = ['start','done','samplestate','advance','invert']


class CycleControl(dict):
    """
88
    This object controls the CTDAS system flow and functionality.
89
    """
90
        
karolina's avatar
karolina committed
91
    def __init__(self, opts=[], args={}):
92
        """
Peters, Wouter's avatar
Peters, Wouter committed
93
94
        The CycleControl object is instantiated with a set of options and arguments.
        The list of arguments must contain the name of an existing ``rc-file``. 
95
96
        This rc-file is loaded by method :meth:`~da.tools.initexit.CycleControl.load_rc` and validated
        by :meth:`~da.tools.initexit.CycleControl.validate_rc`
97

Peters, Wouter's avatar
Peters, Wouter committed
98
        Options for the CycleControl consist of accepted command line flags or arguments 
99
        in :func:`~da.tools.initexit.CycleControl.parse_options`
100

101
102
        """

103
104
        self.load_rc(args['rc'])
        self.validate_rc()
105
106
107
108
        self.opts = opts

        # Add some useful variables to the rc-file dictionary

karolina's avatar
karolina committed
109
110
        self['jobrcfilename'] = self.RcFileName
        self['dir.da_submit'] = os.getcwd()
111
        self['da.crash.recover'] = '-r' in opts
karolina's avatar
karolina committed
112
113
114
115
        self['verbose'] = '-v' in opts
        self.DaSystem = None # to be filled later
        self.RestartFileList = [] # List of files needed for restart, to be extended later
        self.OutputFileList = [] # List of files needed for output, to be extended later
116
117


118
    def load_rc(self, RcFileName):
119
120
121
122
123
        """ 
        This method loads a DA Cycle rc-file with settings for this simulation 
        """

        rcdata = rc.read(RcFileName)
karolina's avatar
karolina committed
124
        for k, v in rcdata.iteritems():
125
            self[k] = v
karolina's avatar
karolina committed
126
127
        self.RcFileName = RcFileName
        self.DaRcLoaded = True
128

karolina's avatar
karolina committed
129
        logging.info('DA Cycle rc-file (%s) loaded successfully' % self.RcFileName)
130
131


132
    def validate_rc(self):
133
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
134
135
        Validate the contents of the rc-file given a dictionary of required keys. 
        Currently required keys are :attr:`~da.tools.initexit.needed_da_items`
136
137
        """

karolina's avatar
karolina committed
138
139
140
141
142
        for k, v in self.iteritems():
            if v in ['True', 'true', 't', 'T', 'y', 'yes']:
                self[k] = True
            if v in ['False', 'false', 'f', 'F', 'n', 'no']:
                self[k] = False
143
            if 'date' in k : 
144
                self[k] = to_datetime(v)
145
            if k in ['time.start', 'time.end', 'time.finish', 'da.restart.tstamp']:
146
                self[k] = to_datetime(v)
147
148
149
150
151
152
#            if 'time.start' in k : 
#                self[k] = to_datetime(v)
#            if 'time.end' in k : 
#                self[k] = to_datetime(v)
#            if 'time.finish' in k : 
#                self[k] = to_datetime(v)
153
154
        for key in needed_da_items:
            if not self.has_key(key):
karolina's avatar
karolina committed
155
156
157
158
159
160
161
162
                msg = 'Missing a required value in rc-file : %s' % key
                logging.error(msg)
                logging.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ')
                logging.error('Please note the update on Dec 02 2011 where rc-file names for DaSystem and ')
                logging.error('are from now on specified in the main rc-file (see da/rc/da.rc for example)')
                logging.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ')
                raise IOError, msg
        logging.debug('DA Cycle settings have been validated succesfully')
163

164
    def parse_times(self):
165
166
167
168
169
170
171
172
        """ 
        Parse time related parameters into datetime objects for later use 
        """

        startdate = self['time.start']
        finaldate = self['time.finish']                  

        if finaldate <= startdate:
karolina's avatar
karolina committed
173
            logging.error('The start date (%s) is not greater than the end date (%s), please revise' % (startdate.strftime('%Y%m%d'), finaldate.strftime('%Y%m%d'))) 
174
175
176
177
178
179
180
181
            raise ValueError
        cyclelength = self['time.cycle']                 # get time step

# Determine end date

        if cyclelength == 'infinite':
            enddate = finaldate
        else:
182
            enddate = advance_time(startdate, cyclelength)
183

karolina's avatar
karolina committed
184
        dt = enddate - startdate
185

karolina's avatar
karolina committed
186
        if enddate > finaldate:  # do not run beyond finaldate
187
188
            enddate = finaldate

karolina's avatar
karolina committed
189
190
191
192
        self['time.start'] = startdate
        self['time.end'] = enddate
        self['time.finish'] = finaldate
        self['cyclelength'] = dt
193

karolina's avatar
karolina committed
194
195
        logging.info("===============================================================")
        logging.info("DA Cycle start date is %s" % startdate.strftime('%Y-%m-%d %H:%M'))
196
        logging.info("DA Cycle end date is %s" % enddate.strftime('%Y-%m-%d %H:%M'))
karolina's avatar
karolina committed
197
198
199
200
        logging.info("DA Cycle final date is %s" % finaldate.strftime('%Y-%m-%d %H:%M'))  
        logging.info("DA Cycle cycle length is %s" % cyclelength)
        logging.info("DA Cycle restart is %s" % str(self['time.restart']))
        logging.info("===============================================================")
201
202


203
    def set_sample_times(self, lag):
204
205
206
207
208
209
        """
        Set the times over which a sampling interval will loop, depending on 
        the lag. Note that lag falls in the interval [0,nlag-1]
        """

        # Start from cycle times 
karolina's avatar
karolina committed
210
211
        self['time.sample.start'] = copy.deepcopy(self['time.start'])
        self['time.sample.end'] = copy.deepcopy(self['time.end'])
212
213
214
215

        # Now advance depending on lag

        for l in range(lag):
216
            self.advance_sample_times()
217
218


219
    def advance_sample_times(self):
220
221
222
223
        """ 
        Advance sampling start and end time by one cycle interval
        """

224
        days = self['cyclelength'].days                
225

226
227
        self['time.sample.start'] = advance_time(self['time.sample.start'], days)
        self['time.sample.end'] = advance_time(self['time.sample.end'], days)
228
229
    

230
    def advance_cycle_times(self):
231
232
233
        """ 
        Advance cycle start and end time by one cycle interval
        """
234
235
              
        days = self['cyclelength'].days                  
236

237
238
        startdate = advance_time(self['time.start'], days)
        enddate = advance_time(self['time.end'], days)
239

karolina's avatar
karolina committed
240
241
        filtertime = startdate.strftime('%Y%m%d')
        self['dir.output'] = os.path.join(self['dir.da_run'], 'output', filtertime)
242

karolina's avatar
karolina committed
243
244
        self['time.start'] = startdate
        self['time.end'] = enddate
245
246


247
248
249
250
251
252
    def write_random_seed(self):
        filename = os.path.join(self['dir.restart'], 'randomseed_%s.pickle' % self['time.start'].strftime('%Y%m%d'))
        f = open(filename, 'wb')
        seed = np.random.get_state()
        cPickle.dump(seed, f, -1)
        f.close()
253

254
        logging.info("Saved the random seed generator values to file")
255
256
257



258
259
260
261
262
263
    def read_random_seed(self):
        filename = os.path.join(self['dir.restart'], 'randomseed_%s.pickle' % self['da.restart.tstamp'].strftime('%Y%m%d'))
        f = open(filename, 'rb')
        seed = cPickle.load(f)
        np.random.set_state(seed)
        f.close()
264

265
        logging.info("Retrieved the random seed generator values of last cycle from file")
266
267


268
    def initialize(self):
269
270
271
        """ 
        This method determines how to proceed with the cycle. Three options are implemented:

Peters, Wouter's avatar
Peters, Wouter committed
272
273
274
            1. *Fresh start*  : set up the required file structure for this simulation and start
            2. *Restart*      : use latest da_runtime variables from the exec dir and restart
            3. *Recover*      : restart after crash by getting data from restart/one-ago folder
275
276
277

        The choice that gets executed depends on the presence of 

Peters, Wouter's avatar
Peters, Wouter committed
278
279
            # the ``-r`` option on the command line, this triggers a recover
            # the ``time.restart : True`` option in the da.rc file
280
281

        The latter is automatically set if the filter submits the next cycle at the end of the current one, 
282
        through method :meth:`~da.tools.initexit.CycleControl.submit_next_cycle`.
Peters, Wouter's avatar
Peters, Wouter committed
283

Peters, Wouter's avatar
Peters, Wouter committed
284
        The specific call tree under each scenario is: 
Peters, Wouter's avatar
Peters, Wouter committed
285

Peters, Wouter's avatar
Peters, Wouter committed
286
            1. *Fresh Start*
287
                *  :meth:`~da.tools.initexit.CycleControl.setup_file_structure()`  <- Create directory tree
Peters, Wouter's avatar
Peters, Wouter committed
288
            2. *Restart*
289
290
                *  :meth:`~da.tools.initexit.CycleControl.setup_file_structure()`
                *  :meth:`~da.tools.initexit.CycleControl.random_seed`    <- Read the random seed from file
Peters, Wouter's avatar
Peters, Wouter committed
291
            3. *Recover*
292
293
294
                *  :meth:`~da.tools.initexit.CycleControl.setup_file_structure()`
                *  :meth:`~da.tools.initexit.CycleControl.recover_run()`          <- Recover files from restart/one-ago dir, reset ``time.start``
                *  :meth:`~da.tools.initexit.CycleControl.random_seed` 
Peters, Wouter's avatar
Peters, Wouter committed
295
296
297

        And is always followed by a call to

298
            * parse_times()
Peters, Wouter's avatar
Peters, Wouter committed
299
            * WriteRc('jobfilename')
300
        """        
301
        if self['time.restart']:
karolina's avatar
karolina committed
302
            logging.info("Restarting filter from previous step")
303
            self.setup_file_structure()
304
            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
305
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
306
            self.read_random_seed()
307
308
#
#
309
310
        #elif not self['time.restart']:
        else: #assume that it is a fresh start, change this condition to more specific if crash recover added
karolina's avatar
karolina committed
311
            logging.info("First time step in filter sequence")
312
            self.setup_file_structure()
313
314

            # expand jobrcfilename to include exec dir from now on.
315
316
317
            # First strip current leading path from filename

            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
318
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
319

320
        self.parse_times()
321
        self.write_rc(self['jobrcfilename'])
322

323
    def setup_file_structure(self):
324
325
        """ 
        Create file structure needed for data assimilation system.
Peters, Wouter's avatar
Peters, Wouter committed
326
327
328
329
330
331
332
333
334
        In principle this looks like:

            * ``${da_rundir}``
            * ``${da_rundir}/input``
            * ``${da_rundir}/output``
            * ``${da_rundir}/exec``
            * ``${da_rundir}/diagnostics``
            * ``${da_rundir}/analysis``
            * ``${da_rundir}/jobs``
335
336
            * ``${da_rundir}/restart/current``
            * ``${da_rundir}/restart/one-ago``
Peters, Wouter's avatar
Peters, Wouter committed
337

338
        .. note:: The exec dir will actually be a simlink to the directory where
339
340
341
                 the observation operator executable lives. This directory is passed through
                 the ``da.rc`` file. 

342
        .. note:: The observation input files will be placed in the exec dir,
343
                 and the resulting simulated values will be retrieved from there as well.
Peters, Wouter's avatar
Peters, Wouter committed
344

345
346
347
348
        """

# Create the run directory for this DA job, including I/O structure

karolina's avatar
karolina committed
349
        filtertime = self['time.start'].strftime('%Y%m%d')
350

karolina's avatar
karolina committed
351
352
353
354
355
356
        self['dir.exec'] = os.path.join(self['dir.da_run'], 'exec')
        self['dir.input'] = os.path.join(self['dir.da_run'], 'input')
        self['dir.output'] = os.path.join(self['dir.da_run'], 'output', filtertime)
        self['dir.analysis'] = os.path.join(self['dir.da_run'], 'analysis')
        self['dir.jobs'] = os.path.join(self['dir.da_run'], 'jobs')
        self['dir.restart'] = os.path.join(self['dir.da_run'], 'restart')
357
358
        #self['dir.restart.current'] = os.path.join(self['dir.restart'], 'current')
        #self['dir.restart.oneago'] = os.path.join(self['dir.restart'], 'one-ago')
359

360
361
362
363
364
365
366
        create_dirs(self['dir.da_run'])
        create_dirs(os.path.join(self['dir.exec']))
        create_dirs(os.path.join(self['dir.input']))
        create_dirs(os.path.join(self['dir.output']))
        create_dirs(os.path.join(self['dir.analysis']))
        create_dirs(os.path.join(self['dir.jobs']))
        create_dirs(os.path.join(self['dir.restart']))
367
368
        #create_dirs(os.path.join(self['dir.restart.current']))
        #create_dirs(os.path.join(self['dir.restart.oneago']))
369

karolina's avatar
karolina committed
370
        logging.info('Succesfully created the file structure for the assimilation job')
371
372


373
    def finalize(self):
374
        """
375
        finalize the da cycle, this means writing the save data and rc-files for the next run. 
376
377
378
        The following sequence of actions occur:

            * Write the randomseed to file for reuse in next cycle
Peters, Wouter's avatar
Peters, Wouter committed
379
            * Write a new ``rc-file`` with ``time.restart : True``, and new ``time.start`` and ``time.end``
380
381
382
383
            * Collect all needed data needed for check-pointing (restart from current system state)
            * Move the previous check pointing data out of the way, and replace with current
            * Submit the next cycle

384
        """
385
386
        self.write_random_seed()                              
        self.write_new_rc_file()                              
387
        
388
389
390
391
392
        self.collect_restart_data()  # Collect restart data for next cycle into a clean restart/current folder
        self.collect_output()  # Collect restart data for next cycle into a clean restart/current folder
        self.submit_next_cycle()

    def collect_output(self):
393
        """ Collect files that are part of the requested output for this cycle. This function allows users to add files 
394
395
396
397
398
399
400
            to a list, and then the system will copy these to the current cycle's output directory.
            The list of files included is read from the 
            attribute "OutputFileList" which is a simple list of files that can be appended by other objects/methods that
            require output data to be saved.


        """
karolina's avatar
karolina committed
401
        targetdir = os.path.join(self['dir.output'])
402
        create_dirs(targetdir)
403

karolina's avatar
karolina committed
404
405
        logging.info("Collecting the required output data") 
        logging.debug("           to   directory: %s " % targetdir)
406
407
408
409

        for file in set(self.OutputFileList):
            if os.path.isdir(file): # skip dirs
                continue
410
            if not os.path.exists(file): # skip dirs
karolina's avatar
karolina committed
411
                logging.warning("           [not found] .... %s " % file)
412
                continue
413

karolina's avatar
karolina committed
414
415
            logging.debug("           [copy] .... %s " % file)
            shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
416
417
418



419
    def collect_restart_data(self):
420
        """ Collect files needed for the restart of this cycle in case of a crash, or for the continuation of the next cycle. 
421
            All files needed are written to the restart/current directory. The list of files included is read from the 
422
423
424
            attribute "RestartFileList" which is a simple list of files that can be appended by other objects/methods that
            require restart data to be saved.

425
            .. note:: Before collecting the files in the ``RestartFileList``, the restart/current directory will be emptied and
426
427
428
429
430
                     recreated. This prevents files from accumulating in the restart/current and restart/one-ago folders. It 
                     also means that if a file is missing from the ``RestartFileList``, it will not be available for check-pointing
                     if your run crashes or dies!

            Currently, the following files are included:
431

432
                * The ``da_runtime.rc`` file
433
                * The ``randomseed.pickle`` file
434
435
                * The savestate.nc file
                * The files in the ``ObservationOperator.RestartFileList``, i.e., restart data for the transport model
436

437

Peters, Wouter's avatar
Peters, Wouter committed
438
            .. note:: We assume that the restart files for the :ref:`ObservationOperator` 
439
                      reside in a separate folder, i.e, the ObservationOperator does *not* write directly to the CTDAS restart dir!
440
441
442

        """

443
        targetdir = os.path.join(self['dir.restart'])
444

445
        #logging.info("Purging the current restart directory before collecting new data")
446

447
        #create_dirs(targetdir, forceclean=True)
448

karolina's avatar
karolina committed
449
450
        logging.info("Collecting the required restart data")
        logging.debug("           to   directory: %s " % targetdir)
451

452
        for file in set(self.RestartFileList):
453
454
            if os.path.isdir(file): # skip dirs
                continue
455
            if not os.path.exists(file): 
karolina's avatar
karolina committed
456
                logging.warning("           [not found] .... %s " % file)
457
            else:
karolina's avatar
karolina committed
458
459
                logging.debug("           [copy] .... %s " % file)
                shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
460
461
462
463



#
464
    def write_new_rc_file(self):
465
466
467
468
469
470
        """ Write the rc-file for the next DA cycle. 

            .. note:: The start time for the next cycle is the end time of this one, while 
                      the end time for the next cycle is the current end time + one cycle length. 
                      
            The resulting rc-file is written to the ``dir.exec`` so that it can be used when resubmitting the next cycle
471
472
            
        """
karolina's avatar
karolina committed
473
        
474
        # We make a copy of the current DaCycle object, and modify the start + end dates and restart value
475

karolina's avatar
karolina committed
476
        newDaCycle = copy.deepcopy(self)
477
        newDaCycle['da.restart.tstamp'] = self['time.start']
478
        newDaCycle.advance_cycle_times()
karolina's avatar
karolina committed
479
        newDaCycle['time.restart'] = True
480
        
481
        # Create the name of the rc-file that will hold this new input, and write it
482

483
484
485
486
        #fname = os.path.join(self['dir.exec'], 'da_runtime.rc')  # current exec dir holds next rc file
        
        fname = os.path.join(self['dir.restart'], 'da_runtime_%s.rc' % newDaCycle['time.start'].strftime('%Y%m%d'))#advanced time
        
karolina's avatar
karolina committed
487
488
        rc.write(fname, newDaCycle)
        logging.debug('Wrote new da_runtime.rc (%s) to exec dir' % fname)
489

490
        # The rest is info needed for a system restart, so it modifies the current DaCycle object (self)
491

karolina's avatar
karolina committed
492
        self['da.restart.fname'] = fname    # needed for next job template
493
        #self.RestartFileList.append(fname)  # not that needed since it is already written to the restart dir...
494
        #logging.debug('Added da_runtime.rc to the RestartFileList for later collection')
495
496


497
    def write_rc(self, fname):
498
499
        """ Write RC file after each process to reflect updated info """

karolina's avatar
karolina committed
500
        rc.write(fname, self)
karolina's avatar
karolina committed
501
        logging.debug('Wrote expanded rc-file (%s)' % fname)
karolina's avatar
karolina committed
502
        
503

504
    def submit_next_cycle(self):
505
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
506
        Submit the next job of a DA cycle, this consists of 
507
508
509
            * Changing to the working directory from which the job was started initially
            * create a line to start the master script again with a newly created rc-file
            * Submitting the jobfile 
Peters, Wouter's avatar
Peters, Wouter committed
510
511
512

        If the end of the cycle series is reached, no new job is submitted.

513
        """
karolina's avatar
karolina committed
514
        
515

516
        if self['time.end'] < self['time.finish']:
517

Peters, Wouter's avatar
test    
Peters, Wouter committed
518
            # file ID and names
karolina's avatar
karolina committed
519
520
521
            jobid = self['time.end'].strftime('%Y%m%d') 
            targetdir = os.path.join(self['dir.exec'])
            jobfile = os.path.join(targetdir, 'jb.%s.jb' % jobid)
522
            logfile = os.path.join(targetdir, 'jb.%s.log' % jobid)
Peters, Wouter's avatar
test    
Peters, Wouter committed
523
            # Template and commands for job
524
525
            jobparams = {'jobname':"j.%s" % jobid, 'jobtime':'06:00:00', 'logfile': logfile, 'errfile': logfile}
            template = self.DaPlatForm.get_job_template(jobparams)
karolina's avatar
karolina committed
526
            execcommand = os.path.join(self['dir.da_submit'], sys.argv[0]) 
karolina's avatar
karolina committed
527
            template += 'python %s rc=%s %s >&%s' % (execcommand, self['da.restart.fname'], join(self.opts, ''), logfile) 
Peters, Wouter's avatar
test    
Peters, Wouter committed
528
529

            # write and submit 
530
531
            self.DaPlatForm.write_job(jobfile, template, jobid)
            jobid = self.DaPlatForm.submit_job(jobfile, joblog=logfile) 
532
533
534
535
        else:
            logging.info('Final date reached, no new cycle started')


536
def start_logger(level=logging.INFO):
537
538
539
540
    """ start the logging of messages to screen"""

# start the logging basic configuration by setting up a log file

karolina's avatar
karolina committed
541
542
543
    logging.basicConfig(level=level,
                        format=' [%(levelname)-7s] (%(asctime)s) py-%(module)-20s : %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
544

545
def parse_options():
546
547
548
549
550
551
552
553
554
555
556
557
558
    """ 
    Function parses options from the command line and returns the arguments as a dictionary.
    Accepted command line arguments are:

    ========  =======
    Argument  Meaning
    ========  =======
    -v        verbose output in log files
    -h        display help
    -r        start a simulation by recovering from a previous crash
    ========  =======

    """
559
560
561

# Parse keywords, the only option accepted so far is the "-h" flag for help

karolina's avatar
karolina committed
562
563
    opts = []
    args = []
564
    try:                                
565
        opts, args = getopt.gnu_getopt(sys.argv[1:], "-rv")
566
    except getopt.GetoptError, msg:           
karolina's avatar
karolina committed
567
        logging.error('%s' % msg)
568
569
570
        sys.exit(2)      

    for options in opts:
karolina's avatar
karolina committed
571
        options = options[0].lower()
572
573
574
575
        if options == '-r':
            logging.info('-r flag specified on command line: recovering from crash')
        if options == '-v':
            logging.info('-v flag specified on command line: extra verbose output')
karolina's avatar
karolina committed
576
            logging.root.setLevel(logging.DEBUG)
577

578
    if opts: 
579
        optslist = [item[0] for item in opts]
580
    else:
karolina's avatar
karolina committed
581
        optslist = []
582

583
584
# Parse arguments and return as dictionary

karolina's avatar
karolina committed
585
    arguments = {}
586
587
588
589
590
591
592
593
    for item in args:
        #item=item.lower()

# Catch arguments that are passed not in "key=value" format

        if '=' in item:
            key, arg = item.split('=')
        else:
karolina's avatar
karolina committed
594
595
            logging.error('%s' % 'Argument passed without description (%s)' % item)
            raise getopt.GetoptError, arg
596

karolina's avatar
karolina committed
597
        arguments[key] = arg
598
599


600
    return optslist, arguments
601

602
def validate_opts_args(opts, args):
603
604
605
606
607
    """ 
 Validate the options and arguments passed from the command line before starting the cycle. The validation consists of checking for the presence of an argument "rc", and the existence of
 the specified rc-file.  
 
    """
608
    if not args.has_key("rc"):
karolina's avatar
karolina committed
609
610
611
        msg = "There is no rc-file specified on the command line. Please use rc=yourfile.rc"
        logging.error(msg)
        raise IOError, msg
612
    elif not os.path.exists(args['rc']):
karolina's avatar
karolina committed
613
614
615
        msg = "The specified rc-file (%s) does not exist " % args['rc'] 
        logging.error(msg)
        raise IOError, msg
616
617
618
619
620
621
622
623
624

    # WP not needed anymore
    #if not args.has_key('process'):
    #    msg = "There is no process specified on the command line, assuming process=Start"   ; logging.info(msg)
    #    args['process'] = 'start'
    #if args['process'].lower() not in validprocesses:
    #    msg = "The specified process (%s) is not valid"%args['process']   ; logging.error(msg)
    #    raise IOError,msg

karolina's avatar
karolina committed
625
    return opts, args
626
627
628


if __name__ == "__main__":
629

630