initexit.py 28 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: setup, 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
import logging
import os
import sys
60
import glob
karolina's avatar
karolina committed
61
import shutil
karolina's avatar
karolina committed
62
import copy
63
64
65
import getopt
import cPickle
import numpy as np
karolina's avatar
karolina committed
66
from string import join
67

karolina's avatar
karolina committed
68
import da.tools.rc as rc
69
from da.tools.general import create_dirs, to_datetime, advance_time
70

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

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


class CycleControl(dict):
    """
92
    This object controls the CTDAS system flow and functionality.
93
    """
94
        
karolina's avatar
karolina committed
95
    def __init__(self, opts=[], args={}):
96
        """
Peters, Wouter's avatar
Peters, Wouter committed
97
98
        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``. 
99
100
        This rc-file is loaded by method :meth:`~da.tools.initexit.CycleControl.load_rc` and validated
        by :meth:`~da.tools.initexit.CycleControl.validate_rc`
101

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

105
        """
karolina's avatar
karolina committed
106
107
        rcfile = args['rc']
        self.load_rc(rcfile)
108
        self.validate_rc()
109
110
111
112
        self.opts = opts

        # Add some useful variables to the rc-file dictionary

karolina's avatar
karolina committed
113
        self['jobrcfilename'] = rcfile
karolina's avatar
karolina committed
114
        self['dir.da_submit'] = os.getcwd()
115
        self['da.crash.recover'] = '-r' in opts
116
        self['transition'] = '-t' in opts
karolina's avatar
karolina committed
117
        self['verbose'] = '-v' in opts
118
119
120
        self.dasystem = None # to be filled later
        self.restart_filelist = [] # List of files needed for restart, to be extended later
        self.output_filelist = [] # List of files needed for output, to be extended later
121
122


karolina's avatar
karolina committed
123
    def load_rc(self, rcfilename):
124
125
126
127
        """ 
        This method loads a DA Cycle rc-file with settings for this simulation 
        """

karolina's avatar
karolina committed
128
        rcdata = rc.read(rcfilename)
karolina's avatar
karolina committed
129
        for k, v in rcdata.iteritems():
130
131
            self[k] = v

karolina's avatar
karolina committed
132
133
        logging.info('DA Cycle rc-file (%s) loaded successfully' % rcfilename)
        
134

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

karolina's avatar
karolina committed
141
142
143
144
145
        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
146
            if 'date' in k : 
147
                self[k] = to_datetime(v)
148
            if k in ['time.start', 'time.end', 'time.finish', 'da.restart.tstamp']:
149
                self[k] = to_datetime(v)
150
151
        for key in needed_da_items:
            if not self.has_key(key):
karolina's avatar
karolina committed
152
153
154
155
156
157
158
159
                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')
160

161
    def parse_times(self):
162
163
164
165
166
167
168
169
        """ 
        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
170
            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'))) 
171
172
173
174
175
176
177
178
            raise ValueError
        cyclelength = self['time.cycle']                 # get time step

# Determine end date

        if cyclelength == 'infinite':
            enddate = finaldate
        else:
179
            enddate = advance_time(startdate, cyclelength)
180

karolina's avatar
karolina committed
181
        dt = enddate - startdate
182

karolina's avatar
karolina committed
183
        if enddate > finaldate:  # do not run beyond finaldate
184
185
            enddate = finaldate

karolina's avatar
karolina committed
186
187
188
189
        self['time.start'] = startdate
        self['time.end'] = enddate
        self['time.finish'] = finaldate
        self['cyclelength'] = dt
190

karolina's avatar
karolina committed
191
192
        logging.info("===============================================================")
        logging.info("DA Cycle start date is %s" % startdate.strftime('%Y-%m-%d %H:%M'))
193
        logging.info("DA Cycle end date is %s" % enddate.strftime('%Y-%m-%d %H:%M'))
karolina's avatar
karolina committed
194
195
196
197
        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("===============================================================")
198
199


200
    def set_sample_times(self, lag):
201
202
203
204
205
206
        """
        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
207
208
        self['time.sample.start'] = copy.deepcopy(self['time.start'])
        self['time.sample.end'] = copy.deepcopy(self['time.end'])
209
210
211
212

        # Now advance depending on lag

        for l in range(lag):
213
            self.advance_sample_times()
214
215


216
    def advance_sample_times(self):
217
218
219
220
        """ 
        Advance sampling start and end time by one cycle interval
        """

221
        days = self['cyclelength'].days                
222

223
224
        self['time.sample.start'] = advance_time(self['time.sample.start'], days)
        self['time.sample.end'] = advance_time(self['time.sample.end'], days)
225
226
    

227
    def advance_cycle_times(self):
228
229
230
        """ 
        Advance cycle start and end time by one cycle interval
        """
231
232
              
        days = self['cyclelength'].days                  
Peters, Wouter's avatar
Peters, Wouter committed
233

234
235
        startdate = advance_time(self['time.start'], days)
        enddate = advance_time(self['time.end'], days)
236

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

karolina's avatar
karolina committed
240
241
        self['time.start'] = startdate
        self['time.end'] = enddate
242
243


244
245
246
247
248
249
    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()
250

251
        logging.info("Saved the random seed generator values to file")
252
253


254
255
    def read_random_seed(self, first=False):
        if first:
256
            filename = self.dasystem['random.seed.init']
257
258
259
260
            logging.info("Initialised random seed from: %s"%filename)
        else: 
            filename = os.path.join(self['dir.restart'], 'randomseed_%s.pickle' % self['da.restart.tstamp'].strftime('%Y%m%d'))
            logging.info("Retrieved the random seed generator values of last cycle from file")
261
262
263
264
        f = open(filename, 'rb')
        seed = cPickle.load(f)
        np.random.set_state(seed)
        f.close()
265

266

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

Peters, Wouter's avatar
Peters, Wouter committed
271
272
273
            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
274
275
276

        The choice that gets executed depends on the presence of 

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

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

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

Peters, Wouter's avatar
Peters, Wouter committed
285
            1. *Fresh Start*
286
                *  :meth:`~da.tools.initexit.CycleControl.setup_file_structure()`  <- Create directory tree
Peters, Wouter's avatar
Peters, Wouter committed
287
            2. *Restart*
288
289
                *  :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
290
            3. *Recover*
291
292
293
                *  :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
294
295
296

        And is always followed by a call to

297
            * parse_times()
Peters, Wouter's avatar
Peters, Wouter committed
298
            * WriteRc('jobfilename')
299
        """        
300
301
302
303
304
305
306
307
        if self['transition']:
            logging.info("Transition of filter from previous step with od meteo from 25 to 34 levels")
            self.setup_file_structure()
            strippedname = os.path.split(self['jobrcfilename'])[-1]
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
            self.read_random_seed(False)

        elif self['time.restart']:
karolina's avatar
karolina committed
308
            logging.info("Restarting filter from previous step")
309
            self.setup_file_structure()
310
            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
311
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
312
313
            self.read_random_seed(False)

314
        else: #assume that it is a fresh start, change this condition to more specific if crash recover added
karolina's avatar
karolina committed
315
            logging.info("First time step in filter sequence")
316
            self.setup_file_structure()
317
318

            # expand jobrcfilename to include exec dir from now on.
319
320
321
            # First strip current leading path from filename

            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
322
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
323
324
            shutil.copy(os.path.join(self.dasystem['regionsfile']),os.path.join(self['dir.exec'],'da','analysis','copied_regions.nc'))
            logging.info('Copied regions file to the analysis directory: %s'%os.path.join(self.dasystem['regionsfile'])) 
Ingrid Luijkx's avatar
Ingrid Luijkx committed
325
326
327
328
329
330
            if self.dasystem.has_key('extendedregionsfile'):
                shutil.copy(os.path.join(self.dasystem['extendedregionsfile']),os.path.join(self['dir.exec'],'da','analysis','copied_regions_extended.nc')) 
                logging.info('Copied extended regions file to the analysis directory: %s'%os.path.join(self.dasystem['extendedregionsfile'])) 
            else: 
                shutil.copy(os.path.join(self['dir.exec'],'da','analysis','olson_extended.nc'),os.path.join(self['dir.exec'],'da','analysis','copied_regions_extended.nc')) 
                logging.info('Copied extended regions within the analysis directory: %s'%os.path.join(self['dir.exec'],'da','analysis','olson_extended.nc')) 
331
332
333
334
335
336
            for filename in glob.glob(os.path.join(self['dir.exec'],'da','analysis','*.pickle')):
                logging.info('Deleting pickle file %s to make sure the correct regions are used'%os.path.split(filename)[1])
                os.remove(filename) 
            for filename in glob.glob(os.path.join(self['dir.exec'],'*.pickle')):
                logging.info('Deleting pickle file %s to make sure the correct regions are used'%os.path.split(filename)[1])
                os.remove(filename) 
337
            if self.dasystem.has_key('random.seed.init'):
338
                self.read_random_seed(True)
339

340
        self.parse_times()
341
        #self.write_rc(self['jobrcfilename'])
342

343
    def setup_file_structure(self):
344
345
        """ 
        Create file structure needed for data assimilation system.
Peters, Wouter's avatar
Peters, Wouter committed
346
347
348
349
350
351
352
353
        In principle this looks like:

            * ``${da_rundir}``
            * ``${da_rundir}/input``
            * ``${da_rundir}/output``
            * ``${da_rundir}/exec``
            * ``${da_rundir}/analysis``
            * ``${da_rundir}/jobs``
354
355
            * ``${da_rundir}/restart/current``
            * ``${da_rundir}/restart/one-ago``
Peters, Wouter's avatar
Peters, Wouter committed
356

357
        .. note:: The exec dir will actually be a simlink to the directory where
358
359
360
                 the observation operator executable lives. This directory is passed through
                 the ``da.rc`` file. 

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

364
365
366
367
        """

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

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

karolina's avatar
karolina committed
370
371
372
373
374
375
        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')
376

377
378
379
380
381
382
383
        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']))
384

karolina's avatar
karolina committed
385
        logging.info('Succesfully created the file structure for the assimilation job')
386

387

388
    def finalize(self):
389
        """
390
        finalize the da cycle, this means writing the save data and rc-files for the next run. 
391
392
393
        The following sequence of actions occur:

            * Write the randomseed to file for reuse in next cycle
Peters, Wouter's avatar
Peters, Wouter committed
394
            * Write a new ``rc-file`` with ``time.restart : True``, and new ``time.start`` and ``time.end``
395
396
397
398
            * 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

399
        """
400
401
        self.write_random_seed()                              
        self.write_new_rc_file()                              
402
        
403
404
405
        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()
406

407
    def collect_output(self):
408
        """ Collect files that are part of the requested output for this cycle. This function allows users to add files 
409
410
            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 
411
            attribute "output_filelist" which is a simple list of files that can be appended by other objects/methods that
412
413
414
415
            require output data to be saved.


        """
karolina's avatar
karolina committed
416
        targetdir = os.path.join(self['dir.output'])
417
        create_dirs(targetdir)
418

karolina's avatar
karolina committed
419
420
        logging.info("Collecting the required output data") 
        logging.debug("           to   directory: %s " % targetdir)
421

422
        for file in set(self.output_filelist):
423
424
            if os.path.isdir(file): # skip dirs
                continue
425
            if not os.path.exists(file): # skip dirs
karolina's avatar
karolina committed
426
                logging.warning("           [not found] .... %s " % file)
427
                continue
428

karolina's avatar
karolina committed
429
430
            logging.debug("           [copy] .... %s " % file)
            shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
431
432
433



434
    def collect_restart_data(self):
435
        """ Collect files needed for the restart of this cycle in case of a crash, or for the continuation of the next cycle. 
436
            All files needed are written to the restart/current directory. The list of files included is read from the 
437
            attribute "restart_filelist" which is a simple list of files that can be appended by other objects/methods that
438
439
            require restart data to be saved.

440
            .. note:: Before collecting the files in the ``restart_filelist``, the restart/current directory will be emptied and
441
                     recreated. This prevents files from accumulating in the restart/current and restart/one-ago folders. It 
442
                     also means that if a file is missing from the ``restart_filelist``, it will not be available for check-pointing
443
444
445
                     if your run crashes or dies!

            Currently, the following files are included:
446

447
                * The ``da_runtime.rc`` file
448
                * The ``randomseed.pickle`` file
449
                * The savestate.nc file
450
                * The files in the ``ObservationOperator.restart_filelist``, i.e., restart data for the transport model
451

452

Peters, Wouter's avatar
Peters, Wouter committed
453
            .. note:: We assume that the restart files for the :ref:`ObservationOperator` 
454
                      reside in a separate folder, i.e, the ObservationOperator does *not* write directly to the CTDAS restart dir!
455
456
457

        """

458
        targetdir = os.path.join(self['dir.restart'])
459

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

462
        #create_dirs(targetdir, forceclean=True)
463

karolina's avatar
karolina committed
464
465
        logging.info("Collecting the required restart data")
        logging.debug("           to   directory: %s " % targetdir)
466

467
        for file in set(self.restart_filelist):
468
469
            if os.path.isdir(file): # skip dirs
                continue
470
            if not os.path.exists(file): 
karolina's avatar
karolina committed
471
                logging.warning("           [not found] .... %s " % file)
472
            else:
karolina's avatar
karolina committed
473
474
                logging.debug("           [copy] .... %s " % file)
                shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
475
476
477
478



#
479
    def write_new_rc_file(self):
480
481
482
483
484
485
        """ 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
486
487
            
        """
karolina's avatar
karolina committed
488
        
489
        # We make a copy of the current dacycle object, and modify the start + end dates and restart value
490

karolina's avatar
karolina committed
491
492
493
494
        new_dacycle = copy.deepcopy(self)
        new_dacycle['da.restart.tstamp'] = self['time.start']
        new_dacycle.advance_cycle_times()
        new_dacycle['time.restart'] = True
495
        
496
        # Create the name of the rc-file that will hold this new input, and write it
497

498
499
        #fname = os.path.join(self['dir.exec'], 'da_runtime.rc')  # current exec dir holds next rc file
        
karolina's avatar
karolina committed
500
        fname = os.path.join(self['dir.restart'], 'da_runtime_%s.rc' % new_dacycle['time.start'].strftime('%Y%m%d'))#advanced time
501
        
karolina's avatar
karolina committed
502
        rc.write(fname, new_dacycle)
503
        logging.debug('Wrote new da_runtime.rc (%s) to restart dir' % fname)
504

505
        # The rest is info needed for a system restart, so it modifies the current dacycle object (self)
506

karolina's avatar
karolina committed
507
        self['da.restart.fname'] = fname    # needed for next job template
508
509
        #self.restart_filelist.append(fname)  # not that needed since it is already written to the restart dir...
        #logging.debug('Added da_runtime.rc to the restart_filelist for later collection')
510
511


512
    def write_rc(self, fname):
513
514
        """ Write RC file after each process to reflect updated info """

karolina's avatar
karolina committed
515
        rc.write(fname, self)
karolina's avatar
karolina committed
516
        logging.debug('Wrote expanded rc-file (%s)' % fname)
karolina's avatar
karolina committed
517
        
518

519
    def submit_next_cycle(self):
520
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
521
        Submit the next job of a DA cycle, this consists of 
522
523
524
            * 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
525
526
527

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

528
        """
karolina's avatar
karolina committed
529
        
530

531
        if self['time.end'] < self['time.finish']:
532

Peters, Wouter's avatar
test    
Peters, Wouter committed
533
            # file ID and names
karolina's avatar
karolina committed
534
535
536
            jobid = self['time.end'].strftime('%Y%m%d') 
            targetdir = os.path.join(self['dir.exec'])
            jobfile = os.path.join(targetdir, 'jb.%s.jb' % jobid)
537
            logfile = os.path.join(targetdir, 'jb.%s.log' % jobid)
Peters, Wouter's avatar
test    
Peters, Wouter committed
538
            # Template and commands for job
539
            jobparams = {'jobname':"j.%s" % jobid, 'jobnodes':self['da.resources.ntasks'], 'jobtime': self['da.resources.ntime'], 'logfile': logfile, 'errfile': logfile}
540
            template = self.daplatform.get_job_template(jobparams)
karolina's avatar
karolina committed
541
            execcommand = os.path.join(self['dir.da_submit'], sys.argv[0]) 
542
543
            if '-t' in self.opts:
                (self.opts).remove('-t') 
544

545
546
547
548
549
            if not os.environ.has_key('icycle_in_job'):
                logging.info('Environment variable icycle_in_job not found, resubmitting after this cycle')
                os.environ['icycle_in_job'] = self['da.resources.ncycles_per_job']  # assume that if no cycle number is set, we should submit the next job by default
            else:
                logging.info('Environment variable icycle_in_job was found, processing cycle %s of %s in this job'%(os.environ['icycle_in_job'],self['da.resources.ncycles_per_job']) )
550

551
            ncycles = int(self['da.resources.ncycles_per_job'])
552
553
554
555
            for cycle in range(ncycles): 
                nextjobid = '%s'% ( (self['time.end']+cycle*self['cyclelength']).strftime('%Y%m%d'),)
                nextrestartfilename = self['da.restart.fname'].replace(jobid,nextjobid)
                nextlogfilename = logfile.replace(jobid,nextjobid)
556
            	template += '\nexport icycle_in_job=%d\npython %s rc=%s %s >&%s\n' % (cycle+1,execcommand, nextrestartfilename, join(self.opts, ''), nextlogfilename,) 
557
                #template += '\nexport icycle_in_job=%d\npython %s rc=%s %s >&%s &\n' % (cycle+1,execcommand, nextrestartfilename, join(self.opts, ''), nextlogfilename,)
Peters, Wouter's avatar
test    
Peters, Wouter committed
558
559

            # write and submit 
560
            self.daplatform.write_job(jobfile, template, jobid)
561
	    if 'da.resources.ncycles_per_job' in self:
562
		do_submit = (int(os.environ['icycle_in_job']) >= int(self['da.resources.ncycles_per_job']))
563
	    else:
564
                dosubmit = False
565
566
567
568
          
            if do_submit:
                jobid = self.daplatform.submit_job(jobfile, joblog=logfile)

569
570
571
572
        else:
            logging.info('Final date reached, no new cycle started')


573
def start_logger(level=logging.INFO):
574
575
576
577
    """ start the logging of messages to screen"""

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

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

582
def parse_options():
583
584
585
586
587
588
589
590
591
592
    """ 
    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
593
    -t        start a simulation by transitioning from 25 to 34 layers in December 2005 (od meteo)
594
595
596
    ========  =======

    """
597
598
599

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

karolina's avatar
karolina committed
600
601
    opts = []
    args = []
602
    try:                                
603
        opts, args = getopt.gnu_getopt(sys.argv[1:], "-rvt")
604
    except getopt.GetoptError, msg:           
karolina's avatar
karolina committed
605
        logging.error('%s' % msg)
606
607
608
        sys.exit(2)      

    for options in opts:
karolina's avatar
karolina committed
609
        options = options[0].lower()
610
611
        if options == '-r':
            logging.info('-r flag specified on command line: recovering from crash')
612
613
        if options == '-t':
            logging.info('-t flag specified on command line: transition with od from December 2005')    
614
615
        if options == '-v':
            logging.info('-v flag specified on command line: extra verbose output')
karolina's avatar
karolina committed
616
            logging.root.setLevel(logging.DEBUG)
617

618
    if opts: 
619
        optslist = [item[0] for item in opts]
620
    else:
karolina's avatar
karolina committed
621
        optslist = []
622

623
624
# Parse arguments and return as dictionary

karolina's avatar
karolina committed
625
    arguments = {}
626
627
628
629
630
631
632
633
    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
634
635
            logging.error('%s' % 'Argument passed without description (%s)' % item)
            raise getopt.GetoptError, arg
636

karolina's avatar
karolina committed
637
        arguments[key] = arg
638
639


640
    return optslist, arguments
641

642
def validate_opts_args(opts, args):
643
644
645
646
647
    """ 
 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.  
 
    """
648
    if not args.has_key("rc"):
karolina's avatar
karolina committed
649
650
651
        msg = "There is no rc-file specified on the command line. Please use rc=yourfile.rc"
        logging.error(msg)
        raise IOError, msg
652
    elif not os.path.exists(args['rc']):
karolina's avatar
karolina committed
653
654
655
        msg = "The specified rc-file (%s) does not exist " % args['rc'] 
        logging.error(msg)
        raise IOError, msg
656
657
658
659
660
661
662
663
664

    # 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
665
    return opts, args
666
667
668


if __name__ == "__main__":
669
    pass
670