initexit.py 33.6 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
15
16
17
18
19
20
21

Valid options are defined in 

.. autofunction:: da.tools.initexit.ParseOptions

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
44
   :members: Initialize, Finalize,  CollectRestartData, MoveRestartData, 
             SubmitNextCycle, CleanUpCycle, SetupFileStructure, RecoverRun, RandomSeed
45
46
47
48
49
50
51
52
53
54
55

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:

.. autofunction:: da.tools.initexit.StartLogger 
.. autofunction:: da.tools.initexit.ValidateOptsArgs 


56
57
58
59
60
61
62
63
"""

needed_da_items=[
    'time.start',
    'time.finish',
    'time.nlag',
    'time.cycle',
    'dir.da_run',
64
65
66
67
68
    'da.system',
    'da.system.rc',
    'da.obsoperator',
    'da.obsoperator.rc',
    'da.optimizer.nmembers']
69
70
71
72
73
74
75
76
77
78
79
80
81
82

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

import logging
import os
import sys
import shutil
import datetime



class CycleControl(dict):
    """
83
    This object controls the CTDAS system flow and functionality.
84
    """
85
86
        
    def __init__(self,opts=[],args={}):
87
        """
Peters, Wouter's avatar
Peters, Wouter committed
88
89
90
91
        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``. 
        This rc-file is loaded by method :meth:`~da.tools.initexit.CycleControl.LoadRc` and validated
        by :meth:`~da.tools.initexit.CycleControl.ValidateRC`
92

Peters, Wouter's avatar
Peters, Wouter committed
93
94
        Options for the CycleControl consist of accepted command line flags or arguments 
        in :func:`~da.tools.initexit.CycleControl.ParseOptions`
95

96
97
98
99
100
101
102
103
104
105
106
107
108
        """

        self.LoadRc(args['rc'])
        self.ValidateRC()
        self.opts = opts

        # Add some useful variables to the rc-file dictionary

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

    def __str__(self):
        """
        String representation of a CycleControl object
        """

        msg = "==============================================================="    ; print msg
        msg = "DA Cycle rc-file is %s" % self.RcFileName                                ; print msg
        msg = "DA Cycle run directory is %s" % self['dir.da_run']           ; print msg
        msg = "DA Cycle inverse system is %s" % self['da.system']           ; print msg
121
        msg = "DA Cycle obs operator is %s" % self['da.obsoperator']        ; print msg
122
123
124
125
126
127
128
129
130
        msg = "==============================================================="    ; print msg

        return ""


    def LoadRc(self,RcFileName):
        """ 
        This method loads a DA Cycle rc-file with settings for this simulation 
        """
131
        import da.tools.rc as rc
132
133
134
135
136
137
138
139
140
141
142
143
144
145

        rcdata = rc.read(RcFileName)
        for k,v in rcdata.iteritems():
            self[k] = v
        self.RcFileName     = RcFileName
        self.DaRcLoaded    = True

        msg                 = 'DA Cycle rc-file (%s) loaded successfully'%self.RcFileName ; logging.info(msg)

        return True


    def ValidateRC(self):
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
146
147
        Validate the contents of the rc-file given a dictionary of required keys. 
        Currently required keys are :attr:`~da.tools.initexit.needed_da_items`
148
149
150
151
        """
        from da.tools.general import ToDatetime

        for k,v in self.iteritems():
152
	    if v in ['True','true', 't','T', 'y', 'yes']:
153
		self[k] = True
154
	    if v in ['False','false', 'f','F', 'n', 'no']:
155
		self[k] = False
156
157
158
159
160
161
162
163
164
165
166
            if 'date' in k : self[k] = ToDatetime(v)
            if 'time.start' in k : 
                self[k] = ToDatetime(v)
            if 'time.end' in k : 
                self[k] = ToDatetime(v)
            if 'time.finish' in k : 
                self[k] = ToDatetime(v)

        for key in needed_da_items:

            if not self.has_key(key):
167
168
169
170
171
172
173
                status,msge = ( False,'Missing a required value in rc-file : %s' % key)  
                logging.error(msge)
                msg = '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' ; logging.error(msg)
                msg = 'Please note the update on Dec 02 2011 where rc-file names for DaSystem and ' ; logging.error(msg)
                msg = 'are from now on specified in the main rc-file (see da/rc/da.rc for example)' ; logging.error(msg)
                msg = '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' ; logging.error(msg)
                raise IOError,msge
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

        status,msg = ( True,'DA Cycle settings have been validated succesfully' )  ; logging.debug(msg)

        return None

    def ParseTimes(self):
        """ 
        Parse time related parameters into datetime objects for later use 
        """
        from da.tools.general import AdvanceTime

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

        if finaldate <= startdate:
            msg   = 'The start date (%s) is not greater than the end date (%s), please revise'%(startdate.strftime('%Y%m%d'),finaldate.strftime('%Y%m%d')) 
            logging.error(msg)
            raise ValueError
        #
        cyclelength = self['time.cycle']                 # get time step

# Determine end date

        if cyclelength == 'infinite':
            enddate = finaldate
        else:
            enddate = AdvanceTime(startdate,cyclelength)

202
203
        dt      = enddate-startdate

204
205
206
207
208
209
210
        #
        if enddate > finaldate:  # do not run beyon finaldate
            enddate = finaldate

        self['time.start']   = startdate
        self['time.end']     = enddate
        self['time.finish']  = finaldate
211
        self['cyclelength']  = dt
212
213
214
215
216
217
218
219
220
221
222

        msg = "==============================================================="    ; logging.info(msg)
        msg = "DA Cycle start date is %s"   % startdate.strftime('%Y-%m-%d %H:%M')      ; logging.info(msg)
        msg = "DA Cycle end   date is %s"   % enddate.strftime('%Y-%m-%d %H:%M')        ; logging.info(msg)
        msg = "DA Cycle final date is %s"   % finaldate.strftime('%Y-%m-%d %H:%M')      ; logging.info(msg)
        msg = "DA Cycle cycle length is %s" % cyclelength                               ; logging.info(msg)
        msg = "DA Cycle restart is %s"      % str(self['time.restart'])     ; logging.info(msg)
        msg = "==============================================================="    ; logging.info(msg)

        return None

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    def SetSampleTimes(self,lag):
        """
        Set the times over which a sampling interval will loop, depending on 
        the lag. Note that lag falls in the interval [0,nlag-1]
        """
        import copy

        # Start from cycle times 
        self['time.sample.start']   = copy.deepcopy(self['time.start'])
        self['time.sample.end']     = copy.deepcopy(self['time.end'])

        # Now advance depending on lag

        for l in range(lag):
            dummy  = self.AdvanceSampleTimes()

        return None

    def AdvanceSampleTimes(self):
        """ 
        Advance sampling start and end time by one cycle interval
        """
        from da.tools.general import AdvanceTime

        startdate = self['time.sample.start']
        enddate   = self['time.sample.end']                  
        cyclelength   = self['cyclelength']                  

        startdate               = AdvanceTime(startdate,cyclelength.days)
        enddate                 = AdvanceTime(enddate,cyclelength.days)

        self['time.sample.start']   = startdate
        self['time.sample.end']     = enddate
    
        return None

    def AdvanceCycleTimes(self):
        """ 
        Advance cycle start and end time by one cycle interval
        """
        from da.tools.general import AdvanceTime

        startdate       = self['time.start']
        enddate         = self['time.end']                  
        cyclelength     = self['cyclelength']                  

        startdate       = AdvanceTime(startdate,cyclelength.days)
        enddate         = AdvanceTime(enddate,cyclelength.days)

        filtertime              = startdate.strftime('%Y%m%d')
        self['dir.output']      = os.path.join(self['dir.da_run'],'output',filtertime)

        self['time.start']   = startdate
        self['time.end']     = enddate

        return None

280
    def RandomSeed(self,action='read'):
Peters, Wouter's avatar
Peters, Wouter committed
281
282
        """ 
        Get the randomseed and save it, or read the random seed and set it. The seed is currently stored
283
        in a python :mod:`pickle` file, residing in the ``exec`` directory
Peters, Wouter's avatar
Peters, Wouter committed
284
285

        """
286
287
288
        import cPickle
        import numpy as np

289
        filename = os.path.join(self['dir.exec'],'randomseed.pickle')
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

        if action == 'write':
            f        = open(filename,'wb')
            seed     = np.random.get_state()
            dummy    = cPickle.dump (seed,f,-1)
            dummy    = f.close()

            msg      = "Saved the random seed generator values to file"

        if action == 'read':
            f        = open(filename,'rb')
            seed     = cPickle.load(f)
            dummy    = np.random.set_state(seed)
            dummy    = f.close()

            msg      = "Retrieved the random seed generator values from file"

        logging.info(msg)

309
        dummy    = self.RestartFileList.extend([filename])
310
311
312
313

        msg      = "Added the randomseed.pickle file to the RestartFileList" ; logging.debug(msg)


314
315
316
        return None

    def Initialize(self):
317
318
319
        """ 
        This method determines how to proceed with the cycle. Three options are implemented:

Peters, Wouter's avatar
Peters, Wouter committed
320
321
322
            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
323
324
325

        The choice that gets executed depends on the presence of 

Peters, Wouter's avatar
Peters, Wouter committed
326
327
            # the ``-r`` option on the command line, this triggers a recover
            # the ``time.restart : True`` option in the da.rc file
328
329

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

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

Peters, Wouter's avatar
Peters, Wouter committed
334
            1. *Fresh Start*
Peters, Wouter's avatar
Peters, Wouter committed
335
                * dummy = :meth:`~da.tools.initexit.CycleControl.SetupFileStructure()`  <- Create directory tree
Peters, Wouter's avatar
Peters, Wouter committed
336
            2. *Restart*
Peters, Wouter's avatar
Peters, Wouter committed
337
338
                * dummy = :meth:`~da.tools.initexit.CycleControl.SetupFileStructure()`
                * dummy = :meth:`~da.tools.initexit.CycleControl.RandomSeed`    <- Read the random seed from file
Peters, Wouter's avatar
Peters, Wouter committed
339
            3. *Recover*
Peters, Wouter's avatar
Peters, Wouter committed
340
                * dummy = :meth:`~da.tools.initexit.CycleControl.SetupFileStructure()`
341
                * dummy = :meth:`~da.tools.initexit.CycleControl.RecoverRun()`          <- Recover files from restart/one-ago dir, reset ``time.start``
Peters, Wouter's avatar
Peters, Wouter committed
342
343
344
345
                * dummy = :meth:`~da.tools.initexit.CycleControl.RandomSeed` 

        And is always followed by a call to

Peters, Wouter's avatar
Peters, Wouter committed
346
347
            * ParseTimes()
            * WriteRc('jobfilename')
Peters, Wouter's avatar
Peters, Wouter committed
348

349

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
        """        

#
# case 1: A recover from a previous crash, this is signaled by flag "-r"
#
        if self['da.crash.recover']:

            msg   = "Recovering simulation from data in: %s" % self['dir.da_run']  ; logging.info(msg)

            dummy = self.SetupFileStructure()

            dummy = self.RecoverRun()

            dummy = self.RandomSeed('read')
#
# case 2: A continuation, this is signaled by rc-item time.restart = True
#
        elif self['time.restart']:

            msg   = "Restarting filter from previous step"  ; logging.info(msg)

            dummy = self.SetupFileStructure()

373
374
            strippedname = os.path.split(self['jobrcfilename'])[-1]

375
376
            self['jobrcfilename'] = os.path.join(self['dir.exec'],strippedname)

377
378
379
380
381
382
383
384
385
386
387
            dummy = self.RandomSeed('read')
#
# case 3: A fresh start, this is signaled by rc-item time.restart = False
#
        elif not self['time.restart']:
            msg   = "First time step in filter sequence"   ; logging.info(msg)

            dummy = self.SetupFileStructure()

            # expand jobrcfilename to include exec dir from now on.

388
389
390
391
392
            # First strip current leading path from filename

            strippedname = os.path.split(self['jobrcfilename'])[-1]

            self['jobrcfilename'] = os.path.join(self['dir.exec'],strippedname)
393
394
395
396
397
398
399
400
401
402
403


        self.ParseTimes()
        self.WriteRC(self['jobrcfilename'])

        return None


    def SetupFileStructure(self):
        """ 
        Create file structure needed for data assimilation system.
Peters, Wouter's avatar
Peters, Wouter committed
404
405
406
407
408
409
410
411
412
        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``
413
414
            * ``${da_rundir}/restart/current``
            * ``${da_rundir}/restart/one-ago``
Peters, Wouter's avatar
Peters, Wouter committed
415

416
        .. note:: The exec dir will actually be a simlink to the directory where
417
418
419
                 the observation operator executable lives. This directory is passed through
                 the ``da.rc`` file. 

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

423
424
425
426
427
428
429
430
431
432
433
434
435
        """
        from da.tools.general import CreateDirs

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

        filtertime  = self['time.start'].strftime('%Y%m%d')

        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.diagnostics'] = os.path.join(self['dir.da_run'],'diagnostics')
        self['dir.analysis']    = os.path.join(self['dir.da_run'],'analysis')
        self['dir.jobs']        = os.path.join(self['dir.da_run'],'jobs')
436
437
438
        self['dir.restart']     = os.path.join(self['dir.da_run'],'restart')
        self['dir.restart.current']    = os.path.join(self['dir.restart'],'current')
        self['dir.restart.oneago']     = os.path.join(self['dir.restart'],'one-ago')
439
440
441
442
443
444
445
446

        CreateDirs(self['dir.da_run'])
        CreateDirs(os.path.join(self['dir.exec']))
        CreateDirs(os.path.join(self['dir.input']))
        CreateDirs(os.path.join(self['dir.output']))
        CreateDirs(os.path.join(self['dir.diagnostics']))
        CreateDirs(os.path.join(self['dir.analysis']))
        CreateDirs(os.path.join(self['dir.jobs']))
447
448
449
        CreateDirs(os.path.join(self['dir.restart']))
        CreateDirs(os.path.join(self['dir.restart.current']))
        CreateDirs(os.path.join(self['dir.restart.oneago']))
450
451
452
453
454

        msg = 'Succesfully created the file structure for the assimilation job'  ; logging.info(msg)


    def RecoverRun(self):
Peters, Wouter's avatar
Peters, Wouter committed
455
456
        """
        Prepare a recovery from a crashed run. This consists of: 
457
        
458
            - copying all data from the restart/one-ago folder (:meth:`~da.tools.initexit.CycleControl.MoveRestartData`),
459
            - replacing all ``rc-file`` items with those from the ``da_runtime.rc`` in the restart/current dir 
Peters, Wouter's avatar
Peters, Wouter committed
460
            - resetting the seed of the random number generator to the value it had before the crash (:meth:`~da.tools.initexit.CycleControl.RandomSeed`)
461
            - replacing the output dir name, since it has the sample time in it...
Peters, Wouter's avatar
Peters, Wouter committed
462
            
463
        """
464
465
        import da.tools.rc as rc
        import shutil
466
        from da.tools.general import CreateDirs
467

468
        # Replace rc-items with those from the crashed run's last rc-file (now in restart.current dir)
469

470
        file_rc_rec = os.path.join(self['dir.restart.current'],'da_runtime.rc')
471
472
473
474
475
476
477
        rc_rec      = rc.read(file_rc_rec)

        for k,v in rc_rec.iteritems():
            self[k] = v

        self.ValidateRC()

478
        msg         = "Replaced rc-items.... "                            ; logging.debug(msg)
479
480
        msg         = "Next cycle start date is %s" % self['time.start']  ; logging.debug(msg)

481
482
483
484
485
486
487
488
        # Copy randomseed.pickle file to exec dir

        source        = os.path.join(self['dir.restart.current'],'randomseed.pickle')
        dest          = os.path.join(self['dir.exec'],'randomseed.pickle')
        dummy         = shutil.copy(source,dest)

        msg           = "Replaced randomseed file with previous cycles' last values"          ; logging.debug(msg)

489
490
        # Re-create the output dir for this time step, if needed

491
        filtertime              = self['time.start'].strftime('%Y%m%d')
492
        self['dir.output']      = os.path.join(self['dir.da_run'],'output',filtertime)
493
        dummy                   = CreateDirs(os.path.join(self['dir.output']))
494

495
496
497
498
        return None

    def Finalize(self):
        """
499
500
501
502
        Finalize the da cycle, this means writing the save data and rc-files for the next run. 
        The following sequence of actions occur:

            * Write the randomseed to file for reuse in next cycle
Peters, Wouter's avatar
Peters, Wouter committed
503
            * Write a new ``rc-file`` with ``time.restart : True``, and new ``time.start`` and ``time.end``
504
505
506
507
            * 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

508
509
510
511
        """

        dummy = self.RandomSeed('write')
        dummy = self.WriteNewRCfile()
512
513
        dummy = self.MoveRestartData(io_option='store')  # Move restart data from current to one-ago
        dummy = self.CollectRestartData()  # Collect restart data for next cycle into a clean restart/current folder
514
        dummy = self.CollectOutput()  # Collect restart data for next cycle into a clean restart/current folder
515
516
        dummy = self.SubmitNextCycle()

517
    def CollectOutput(self):
518
        """ Collect files that are part of the requested output for this cycle. This function allows users to add files 
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
            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.


        """
        from da.tools.general import CreateDirs

        targetdir   = os.path.join(self['dir.output'])

        CreateDirs(os.path.join(targetdir) )

        msg         = "Collecting the required output data"                                       ; logging.info(msg)
        msg         = "           to   directory: %s " % targetdir                                ; logging.debug(msg)

        for file in set(self.OutputFileList):

            if os.path.isdir(file): # skip dirs
                continue
539
540
541
            if not os.path.exists(file): # skip dirs
                msg         = "           [not found] .... %s " % file                                     ; logging.warning(msg)
                continue
542
543
544
545
546
547

            msg         = "           [copy] .... %s " % file                                     ; logging.debug(msg)
            dummy       = shutil.copy(file,file.replace(os.path.split(file)[0],targetdir) )



548
    def CollectRestartData(self):
549
        """ Collect files needed for the restart of this cycle in case of a crash, or for the continuation of the next cycle. 
550
            All files needed are written to the restart/current directory. The list of files included is read from the 
551
552
553
            attribute "RestartFileList" which is a simple list of files that can be appended by other objects/methods that
            require restart data to be saved.

554
            .. note:: Before collecting the files in the ``RestartFileList``, the restart/current directory will be emptied and
555
556
557
558
559
                     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:
560

561
                * The ``da_runtime.rc`` file
562
                * The ``randomseed.pickle`` file
563
564
                * The savestate.nc file
                * The files in the ``ObservationOperator.RestartFileList``, i.e., restart data for the transport model
565

566

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

        """
571
        from da.tools.general import CreateDirs
572

573
        targetdir   = os.path.join(self['dir.restart.current'])
574

575
576
577
578
        msg         = "Purging the current restart directory before collecting new data"         ; logging.info(msg)

        CreateDirs(os.path.join(targetdir),forceclean=True)

579
        msg         = "Collecting the required restart data"                                      ; logging.info(msg)
580
581
        msg         = "           to   directory: %s " % targetdir                                ; logging.debug(msg)

582
        for file in set(self.RestartFileList):
583
584
585

            if os.path.isdir(file): # skip dirs
                continue
586
587
588
589
590
            if not os.path.exists(file): # skip dirs
                msg         = "           [not found] .... %s " % file                                     ; logging.warning(msg)
            else:
                msg         = "           [copy] .... %s " % file                                     ; logging.debug(msg)
                dummy       = shutil.copy(file,file.replace(os.path.split(file)[0],targetdir) )
591
592


593
    def MoveRestartData(self, io_option='restore'):
Peters, Wouter's avatar
Peters, Wouter committed
594
        """ 
595
        Store or restore model state to/from a restart directory. 
596

597
            Two IO options are available:
598

599
600
            (1) io_option = restore    : Get data from restart.oneago directory
            (2) io_option = store      : Save data to restart.oneago directory
601

602
            In case of a 'store' command the restart.oneago folder is re-created so that the contents are empty to begin with.
603
604
605
606
607
608
609

        """
        from da.tools.general import CreateDirs

        if io_option not in ['store','restore']:
            raise ValueError,'Invalid option specified for io_option (%s)' % io_option

610
        if io_option == 'store':
611

612
613
            targetdir = self['dir.restart.oneago']
            sourcedir = self['dir.restart.current']
614

615
        elif io_option == 'restore':
616

617
618
            sourcedir = self['dir.restart.oneago']
            targetdir = self['dir.restart.current']
619
620
621
622
623
624

# If "store" is requested, recreate target dir, cleaning the contents 

        if io_option == 'store':
            CreateDirs(os.path.join(targetdir),forceclean=True)

625
        msg         = "Performing a %s of data" % (io_option)                                     ; logging.debug(msg)
626
627
628
629
630
631
632
        msg         = "           from directory: %s " % sourcedir                                ; logging.debug(msg)
        msg         = "           to   directory: %s " % targetdir                                ; logging.debug(msg)


        for file in os.listdir(sourcedir):

            file = os.path.join(sourcedir,file)
633
            if not os.path.exists(file):
634
635
636
                msg         = "Cannot find requested file to move: %s " % file                                ; logging.debug(msg)
                sys.exit(2)

637
638

            if os.path.isdir(file): # skip dirs
639

640
641
                msg         = "           [skip] .... %s " % file                                     ; logging.debug(msg)
                continue    
642
            else:
643
644
645
646
647
648

                msg         = "           [copy] .... %s " % file                                                ; logging.debug(msg)
                dummy       = shutil.copy(file,file.replace(sourcedir,targetdir) )

#
    def WriteNewRCfile(self):
649
650
651
652
653
654
        """ 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
655
656
            
        """
657
        import da.tools.rc as rc
658
        import copy
659

660
        # We make a copy of the current DaCycle object, and modify the start + end dates and restart value
661

662
663
664
        newDaCycle                          = copy.deepcopy(self)
        dummy                               = newDaCycle.AdvanceCycleTimes()
        newDaCycle['time.restart']          = True
665

666
        # Create the name of the rc-file that will hold this new input, and write it
667

668
669
670
        fname   = os.path.join(self['dir.exec'],'da_runtime.rc')  # current exec dir holds next rc file
        dummy   = rc.write(fname,newDaCycle)
        msg     = 'Wrote new da_runtime.rc (%s) to exec dir'%fname ; logging.debug(msg)
671

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

674
675
        self['da.restart.fname']             = fname    # needed for next job template
        dummy                                = self.RestartFileList.extend([fname])  # current restart list holds next rc file name
676
677

        msg = 'Added da_runtime.rc to the RestartFileList for later collection' ; logging.debug(msg)
678
679
680
681


    def WriteRC(self,fname):
        """ Write RC file after each process to reflect updated info """
682
        import da.tools.rc as rc
683
684
685
686
687
688
689

        dummy  = rc.write(fname,self)
        msg    = 'Wrote expanded rc-file (%s)'%(fname) ; logging.debug(msg)
        return None

    def SubmitNextCycle(self):
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
690
        Submit the next job of a DA cycle, this consists of 
691
692
693
            * 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
694
695
696

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

697
698
699
        """
        import subprocess
        import os
700
        from string import join
701
702
703
704


        DaPlatForm = self.DaPlatForm

705
        if self['time.end'] < self['time.finish']:
706

Peters, Wouter's avatar
test    
Peters, Wouter committed
707
            # file ID and names
708
            jobid                   = self['time.end'].strftime('%Y%m%d') 
Peters, Wouter's avatar
test    
Peters, Wouter committed
709
710
711
712
713
            targetdir               = os.path.join(self['dir.exec'])
            jobfile                 = os.path.join(targetdir,'jb.%s.jb'%jobid)
            logfile                 = jobfile.replace('.jb','.log')

            # Template and commands for job
714
            jobparams               = {'jobname':"j.%s"%jobid,'jobtime':'06:00:00','logfile':logfile,'errfile':logfile}
715
            template                = DaPlatForm.GetJobTemplate(jobparams)
716
            execcommand             = os.path.join(self['dir.da_submit'],sys.argv[0]) 
717
            template                += 'python %s rc=%s %s' % (execcommand,self['da.restart.fname'],join(self.opts,''),) 
Peters, Wouter's avatar
test    
Peters, Wouter committed
718
719
720
721

            # write and submit 

            dummy                   = DaPlatForm.WriteJob(jobfile,template,jobid)
722
            jobid                   = DaPlatForm.SubmitJob(jobfile,joblog=logfile) 
723
724
725
726
727
728
        else:
            logging.info('Final date reached, no new cycle started')

        return None

    def SubmitSubStep(self,stepname):
Peters, Wouter's avatar
Peters, Wouter committed
729
730
731
732
733
734
735
736
        """ 
        Submit the next substep of a DA cycle, this consists of 
            * getting a job template as returned by :meth:`~da.tools.baseclasses.platform.GetJobTemplate`
            * adding the lines needed to start a next run with a newly created rc-file
            * Writing the jobfile as done by :meth:`~da.tools.baseclasses.platform.WriteJob`
            * Submitting the jobfile as done by :meth:`~da.tools.baseclasses.platform.WriteJob`

        """
737
738
739
740
741
742
743
744
745
746
        import subprocess
        import os
        from string import join


        DaPlatForm = self.DaPlatForm

        jobparams               = {'jobname':'das.%s'%stepname}
        template                = DaPlatForm.GetJobTemplate(jobparams)
        template                += 'cd %s\n'%os.getcwd()
747
        template                += '%s rc=%s process=%s %s' % (sys.argv[0],self['jobrcfilename'],stepname,join(self.opts,''),) 
748
749
750
751
752
        jobfile                 = DaPlatForm.WriteJob(self,template,stepname)
        jobid                   = DaPlatForm.SubmitJob(jobfile) 

        return None

753
754
    def CleanUpCycle(self):
        """
Peters, Wouter's avatar
test    
Peters, Wouter committed
755
        Nothing to do for now anymore
756
757
758
        """


759

760
def StartLogger(level=logging.INFO):
761
762
763
764
    """ start the logging of messages to screen"""

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

765
    logging.basicConfig(level    = level,
766
767
768
769
                        format   = ' [%(levelname)-7s] (%(asctime)s) py-%(module)-20s : %(message)s',
                        datefmt  = '%Y-%m-%d %H:%M:%S')

def ParseOptions():
770
771
772
773
774
775
776
777
778
779
780
781
782
    """ 
    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
    ========  =======

    """
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    import getopt
    import sys

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

    opts=[]
    args=[]
    try:                                
        opts, args = getopt.gnu_getopt(sys.argv[1:], "-hrv")
    except getopt.GetoptError, msg:           
        logging.error('%s'%msg)
        sys.exit(2)      

    for options in opts:
        options=options[0].lower()
        if options == '-h':
            print ""
            print helptext
            sys.exit(2)      
        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')
            dummy  = logging.root.setLevel(logging.DEBUG)

808
809
810
811
812
    if opts: 
        optslist=[item[0] for item in opts]
    else:
        optslist=[]

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# Parse arguments and return as dictionary

    arguments={}
    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:
            logging.error('%s'%'Argument passed without description (%s)' % item)
            raise getopt.GetoptError,arg

        arguments[key]=arg


830
    return optslist, arguments
831
832

def ValidateOptsArgs(opts,args):
833
834
835
836
837
    """ 
 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.  
 
    """
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857

    if not args.has_key("rc"):
        msg = "There is no rc-file specified on the command line. Please use rc=yourfile.rc"   ; logging.error(msg)
        raise IOError,msg
    elif not os.path.exists(args['rc']):
        msg = "The specified rc-file (%s) does not exist " % args['rc'] ;  logging.error(msg)
        raise IOError,msg

    # 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

    return opts,args


if __name__ == "__main__":
858
859
860
861
862

    sys.path.append('../../')
    opts,args = ParseOptions()
    print opts
    print args
863