initexit.py 31 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
"""
karolina's avatar
karolina committed
57
58
59
60
import logging
import os
import sys
import shutil
61

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

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


class CycleControl(dict):
    """
80
    This object controls the CTDAS system flow and functionality.
81
    """
82
        
karolina's avatar
karolina committed
83
    def __init__(self, opts=[], args={}):
84
        """
Peters, Wouter's avatar
Peters, Wouter committed
85
86
87
88
        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`
89

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

93
94
95
96
97
98
99
100
        """

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

        # Add some useful variables to the rc-file dictionary

karolina's avatar
karolina committed
101
102
        self['jobrcfilename'] = self.RcFileName
        self['dir.da_submit'] = os.getcwd()
103
        self['da.crash.recover'] = '-r' in opts
karolina's avatar
karolina committed
104
105
106
107
        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
108
109

    def __str__(self):
karolina's avatar
karolina committed
110
111
112
113
114
115
116
        msg = "===============================================================" 
        msg += "DA Cycle rc-file is %s" % self.RcFileName            
        msg += "DA Cycle run directory is %s" % self['dir.da_run']   
        msg += "DA Cycle inverse system is %s" % self['da.system']   
        msg += "DA Cycle obs operator is %s" % self['da.obsoperator'] 
        msg += "===============================================================" 
        return msg
117
118


karolina's avatar
karolina committed
119
    def LoadRc(self, RcFileName):
120
121
122
        """ 
        This method loads a DA Cycle rc-file with settings for this simulation 
        """
123
        import da.tools.rc as rc
124
125

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

karolina's avatar
karolina committed
131
        logging.info('DA Cycle rc-file (%s) loaded successfully' % self.RcFileName)
132
133
134
135
136
137

        return True


    def ValidateRC(self):
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
138
139
        Validate the contents of the rc-file given a dictionary of required keys. 
        Currently required keys are :attr:`~da.tools.initexit.needed_da_items`
140
141
142
        """
        from da.tools.general import ToDatetime

karolina's avatar
karolina committed
143
144
145
146
147
        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
148
149
150
151
152
153
154
155
156
            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):
karolina's avatar
karolina committed
157
158
159
160
161
162
163
164
                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')
165
166
167
168
169
170
171
172
173
174
175

    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:
karolina's avatar
karolina committed
176
            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'))) 
177
178
179
180
181
182
183
184
            raise ValueError
        cyclelength = self['time.cycle']                 # get time step

# Determine end date

        if cyclelength == 'infinite':
            enddate = finaldate
        else:
karolina's avatar
karolina committed
185
            enddate = AdvanceTime(startdate, cyclelength)
186

karolina's avatar
karolina committed
187
        dt = enddate - startdate
188

karolina's avatar
karolina committed
189
        if enddate > finaldate:  # do not run beyond finaldate
190
191
            enddate = finaldate

karolina's avatar
karolina committed
192
193
194
195
        self['time.start'] = startdate
        self['time.end'] = enddate
        self['time.finish'] = finaldate
        self['cyclelength'] = dt
196

karolina's avatar
karolina committed
197
198
199
200
201
202
203
        logging.info("===============================================================")
        logging.info("DA Cycle start date is %s" % startdate.strftime('%Y-%m-%d %H:%M'))
        logging.info("DA Cycle end   date is %s" % enddate.strftime('%Y-%m-%d %H:%M'))
        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("===============================================================")
204
205


karolina's avatar
karolina committed
206
    def SetSampleTimes(self, lag):
207
208
209
210
211
212
213
        """
        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 
karolina's avatar
karolina committed
214
215
        self['time.sample.start'] = copy.deepcopy(self['time.start'])
        self['time.sample.end'] = copy.deepcopy(self['time.end'])
216
217
218
219

        # Now advance depending on lag

        for l in range(lag):
karolina's avatar
karolina committed
220
            self.AdvanceSampleTimes()
221
222
223
224
225
226
227
228
229


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

        startdate = self['time.sample.start']
karolina's avatar
karolina committed
230
231
        enddate = self['time.sample.end']                  
        cyclelength = self['cyclelength']                  
232

karolina's avatar
karolina committed
233
234
        startdate = AdvanceTime(startdate, cyclelength.days)
        enddate = AdvanceTime(enddate, cyclelength.days)
235

karolina's avatar
karolina committed
236
237
        self['time.sample.start'] = startdate
        self['time.sample.end'] = enddate
238
239
240
241
242
243
244
245
    

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

karolina's avatar
karolina committed
246
247
248
        startdate = self['time.start']
        enddate = self['time.end']                  
        cyclelength = self['cyclelength']                  
249

karolina's avatar
karolina committed
250
251
        startdate = AdvanceTime(startdate, cyclelength.days)
        enddate = AdvanceTime(enddate, cyclelength.days)
252

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

karolina's avatar
karolina committed
256
257
        self['time.start'] = startdate
        self['time.end'] = enddate
258
259


karolina's avatar
karolina committed
260
    def RandomSeed(self, action='read'):
Peters, Wouter's avatar
Peters, Wouter committed
261
262
        """ 
        Get the randomseed and save it, or read the random seed and set it. The seed is currently stored
263
        in a python :mod:`pickle` file, residing in the ``exec`` directory
Peters, Wouter's avatar
Peters, Wouter committed
264
265

        """
266
267
268
        import cPickle
        import numpy as np

karolina's avatar
karolina committed
269
        filename = os.path.join(self['dir.exec'], 'randomseed.pickle')
270
271

        if action == 'write':
karolina's avatar
karolina committed
272
273
274
275
            f = open(filename, 'wb')
            seed = np.random.get_state()
            dummy = cPickle.dump (seed, f, -1)
            dummy = f.close()
276

karolina's avatar
karolina committed
277
            msg = "Saved the random seed generator values to file"
278
279

        if action == 'read':
karolina's avatar
karolina committed
280
281
282
283
            f = open(filename, 'rb')
            seed = cPickle.load(f)
            dummy = np.random.set_state(seed)
            dummy = f.close()
284

karolina's avatar
karolina committed
285
            msg = "Retrieved the random seed generator values from file"
286
287

        logging.info(msg)
karolina's avatar
karolina committed
288
289
        self.RestartFileList.extend([filename])
        logging.debug("Added the randomseed.pickle file to the RestartFileList")
290
291


292
293
294
        return None

    def Initialize(self):
295
296
297
        """ 
        This method determines how to proceed with the cycle. Three options are implemented:

Peters, Wouter's avatar
Peters, Wouter committed
298
299
300
            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
301
302
303

        The choice that gets executed depends on the presence of 

Peters, Wouter's avatar
Peters, Wouter committed
304
305
            # the ``-r`` option on the command line, this triggers a recover
            # the ``time.restart : True`` option in the da.rc file
306
307

        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
308
309
        through method :meth:`~da.tools.initexit.CycleControl.SubmitNextCycle`.

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

Peters, Wouter's avatar
Peters, Wouter committed
312
            1. *Fresh Start*
Peters, Wouter's avatar
Peters, Wouter committed
313
                * dummy = :meth:`~da.tools.initexit.CycleControl.SetupFileStructure()`  <- Create directory tree
Peters, Wouter's avatar
Peters, Wouter committed
314
            2. *Restart*
Peters, Wouter's avatar
Peters, Wouter committed
315
316
                * 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
317
            3. *Recover*
Peters, Wouter's avatar
Peters, Wouter committed
318
                * dummy = :meth:`~da.tools.initexit.CycleControl.SetupFileStructure()`
319
                * dummy = :meth:`~da.tools.initexit.CycleControl.RecoverRun()`          <- Recover files from restart/one-ago dir, reset ``time.start``
Peters, Wouter's avatar
Peters, Wouter committed
320
321
322
323
                * dummy = :meth:`~da.tools.initexit.CycleControl.RandomSeed` 

        And is always followed by a call to

Peters, Wouter's avatar
Peters, Wouter committed
324
325
            * ParseTimes()
            * WriteRc('jobfilename')
326
327
328
329
330
        """        
#
# case 1: A recover from a previous crash, this is signaled by flag "-r"
#
        if self['da.crash.recover']:
karolina's avatar
karolina committed
331
332
333
334
            logging.info("Recovering simulation from data in: %s" % self['dir.da_run'])
            self.SetupFileStructure()
            self.RecoverRun()
            self.RandomSeed('read')
335
336
337
338
#
# case 2: A continuation, this is signaled by rc-item time.restart = True
#
        elif self['time.restart']:
karolina's avatar
karolina committed
339
340
            logging.info("Restarting filter from previous step")
            self.SetupFileStructure()
341
            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
342
343
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
            self.RandomSeed('read')
344
345
346
347
#
# case 3: A fresh start, this is signaled by rc-item time.restart = False
#
        elif not self['time.restart']:
karolina's avatar
karolina committed
348
349
            logging.info("First time step in filter sequence")
            self.SetupFileStructure()
350
351

            # expand jobrcfilename to include exec dir from now on.
352
353
354
            # First strip current leading path from filename

            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
355
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
356
357
358
359
360
361
362

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

    def SetupFileStructure(self):
        """ 
        Create file structure needed for data assimilation system.
Peters, Wouter's avatar
Peters, Wouter committed
363
364
365
366
367
368
369
370
371
        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``
372
373
            * ``${da_rundir}/restart/current``
            * ``${da_rundir}/restart/one-ago``
Peters, Wouter's avatar
Peters, Wouter committed
374

375
        .. note:: The exec dir will actually be a simlink to the directory where
376
377
378
                 the observation operator executable lives. This directory is passed through
                 the ``da.rc`` file. 

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

382
383
384
385
386
        """
        from da.tools.general import CreateDirs

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

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

karolina's avatar
karolina committed
389
390
391
392
393
394
395
396
397
        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')
        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')
398
399
400
401
402
403
404
405

        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']))
406
407
408
        CreateDirs(os.path.join(self['dir.restart']))
        CreateDirs(os.path.join(self['dir.restart.current']))
        CreateDirs(os.path.join(self['dir.restart.oneago']))
409

karolina's avatar
karolina committed
410
        logging.info('Succesfully created the file structure for the assimilation job')
411
412

    def RecoverRun(self):
Peters, Wouter's avatar
Peters, Wouter committed
413
414
        """
        Prepare a recovery from a crashed run. This consists of: 
415
        
416
            - copying all data from the restart/one-ago folder (:meth:`~da.tools.initexit.CycleControl.MoveRestartData`),
417
            - replacing all ``rc-file`` items with those from the ``da_runtime.rc`` in the restart/current dir 
Peters, Wouter's avatar
Peters, Wouter committed
418
            - resetting the seed of the random number generator to the value it had before the crash (:meth:`~da.tools.initexit.CycleControl.RandomSeed`)
419
            - replacing the output dir name, since it has the sample time in it...
Peters, Wouter's avatar
Peters, Wouter committed
420
            
421
        """
422
        import da.tools.rc as rc
423
        from da.tools.general import CreateDirs
424

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

karolina's avatar
karolina committed
427
428
        file_rc_rec = os.path.join(self['dir.restart.current'], 'da_runtime.rc')
        rc_rec = rc.read(file_rc_rec)
429

karolina's avatar
karolina committed
430
        for k, v in rc_rec.iteritems():
431
432
433
434
            self[k] = v

        self.ValidateRC()

karolina's avatar
karolina committed
435
436
        logging.debug("Replaced rc-items.... ")
        logging.debug("Next cycle start date is %s" % self['time.start'])
437

438
        # Copy randomseed.pickle file to exec dir
karolina's avatar
karolina committed
439
440
441
        source = os.path.join(self['dir.restart.current'], 'randomseed.pickle')
        dest = os.path.join(self['dir.exec'], 'randomseed.pickle')
        shutil.copy(source, dest)
442

karolina's avatar
karolina committed
443
        logging.debug("Replaced randomseed file with previous cycles' last values")
444
445
        # Re-create the output dir for this time step, if needed

karolina's avatar
karolina committed
446
447
448
        filtertime = self['time.start'].strftime('%Y%m%d')
        self['dir.output'] = os.path.join(self['dir.da_run'], 'output', filtertime)
        CreateDirs(os.path.join(self['dir.output']))
449
450
451

    def Finalize(self):
        """
452
453
454
455
        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
456
            * Write a new ``rc-file`` with ``time.restart : True``, and new ``time.start`` and ``time.end``
457
458
459
460
            * 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

461
        """
karolina's avatar
karolina committed
462
463
464
465
466
467
        self.RandomSeed('write')
        self.WriteNewRCfile()
        self.MoveRestartData(io_option='store')  # Move restart data from current to one-ago
        self.CollectRestartData()  # Collect restart data for next cycle into a clean restart/current folder
        self.CollectOutput()  # Collect restart data for next cycle into a clean restart/current folder
        self.SubmitNextCycle()
468

469
    def CollectOutput(self):
470
        """ Collect files that are part of the requested output for this cycle. This function allows users to add files 
471
472
473
474
475
476
477
478
479
            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

karolina's avatar
karolina committed
480
481
        targetdir = os.path.join(self['dir.output'])
        CreateDirs(os.path.join(targetdir))
482

karolina's avatar
karolina committed
483
484
        logging.info("Collecting the required output data") 
        logging.debug("           to   directory: %s " % targetdir)
485
486
487
488

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

karolina's avatar
karolina committed
493
494
            logging.debug("           [copy] .... %s " % file)
            shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
495
496
497



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

504
            .. note:: Before collecting the files in the ``RestartFileList``, the restart/current directory will be emptied and
505
506
507
508
509
                     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:
510

511
                * The ``da_runtime.rc`` file
512
                * The ``randomseed.pickle`` file
513
514
                * The savestate.nc file
                * The files in the ``ObservationOperator.RestartFileList``, i.e., restart data for the transport model
515

516

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

        """
521
        from da.tools.general import CreateDirs
522

karolina's avatar
karolina committed
523
        targetdir = os.path.join(self['dir.restart.current'])
524

karolina's avatar
karolina committed
525
        logging.info("Purging the current restart directory before collecting new data")
526

karolina's avatar
karolina committed
527
        CreateDirs(os.path.join(targetdir), forceclean=True)
528

karolina's avatar
karolina committed
529
530
        logging.info("Collecting the required restart data")
        logging.debug("           to   directory: %s " % targetdir)
531

532
        for file in set(self.RestartFileList):
533
534
            if os.path.isdir(file): # skip dirs
                continue
535
            if not os.path.exists(file): # skip dirs
karolina's avatar
karolina committed
536
                logging.warning("           [not found] .... %s " % file)
537
            else:
karolina's avatar
karolina committed
538
539
                logging.debug("           [copy] .... %s " % file)
                shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
540
541


542
    def MoveRestartData(self, io_option='restore'):
Peters, Wouter's avatar
Peters, Wouter committed
543
        """ 
544
        Store or restore model state to/from a restart directory. 
545

546
            Two IO options are available:
547

548
549
            (1) io_option = restore    : Get data from restart.oneago directory
            (2) io_option = store      : Save data to restart.oneago directory
550

551
            In case of a 'store' command the restart.oneago folder is re-created so that the contents are empty to begin with.
552
553
554
555

        """
        from da.tools.general import CreateDirs

karolina's avatar
karolina committed
556
557
        if io_option not in ['store', 'restore']:
            raise ValueError, 'Invalid option specified for io_option (%s)' % io_option
558

559
560
561
562
563
564
        if io_option == 'store':
            targetdir = self['dir.restart.oneago']
            sourcedir = self['dir.restart.current']
        elif io_option == 'restore':
            sourcedir = self['dir.restart.oneago']
            targetdir = self['dir.restart.current']
565
566
567
568

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

        if io_option == 'store':
karolina's avatar
karolina committed
569
            CreateDirs(os.path.join(targetdir), forceclean=True)
570

karolina's avatar
karolina committed
571
572
573
        logging.debug("Performing a %s of data" % (io_option))
        logging.debug("           from directory: %s " % sourcedir)
        logging.debug("           to   directory: %s " % targetdir)
574
575
576


        for file in os.listdir(sourcedir):
karolina's avatar
karolina committed
577
            file = os.path.join(sourcedir, file)
578
            if not os.path.exists(file):
karolina's avatar
karolina committed
579
                logging.debug("Cannot find requested file to move: %s " % file)  
580
581
                sys.exit(2)

582
            if os.path.isdir(file): # skip dirs
karolina's avatar
karolina committed
583
                logging.debug("           [skip] .... %s " % file)
584
                continue    
585
            else:
karolina's avatar
karolina committed
586
587
                logging.debug("           [copy] .... %s " % file)
                shutil.copy(file, file.replace(sourcedir, targetdir))
588
589
590

#
    def WriteNewRCfile(self):
591
592
593
594
595
596
        """ 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
597
598
            
        """
599
        import da.tools.rc as rc
600
        import copy
601

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

karolina's avatar
karolina committed
604
605
606
        newDaCycle = copy.deepcopy(self)
        newDaCycle.AdvanceCycleTimes()
        newDaCycle['time.restart'] = True
607

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

karolina's avatar
karolina committed
610
611
612
        fname = os.path.join(self['dir.exec'], 'da_runtime.rc')  # current exec dir holds next rc file
        rc.write(fname, newDaCycle)
        logging.debug('Wrote new da_runtime.rc (%s) to exec dir' % fname)
613

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

karolina's avatar
karolina committed
616
617
        self['da.restart.fname'] = fname    # needed for next job template
        self.RestartFileList.extend([fname])  # current restart list holds next rc file name
618

karolina's avatar
karolina committed
619
        logging.debug('Added da_runtime.rc to the RestartFileList for later collection')
620
621


karolina's avatar
karolina committed
622
    def WriteRC(self, fname):
623
        """ Write RC file after each process to reflect updated info """
624
        import da.tools.rc as rc
625

karolina's avatar
karolina committed
626
627
628
        rc.write(fname, self)
        logging.debug('Wrote expanded rc-file (%s)' % (fname))
        
629
630
631

    def SubmitNextCycle(self):
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
632
        Submit the next job of a DA cycle, this consists of 
633
634
635
            * 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
636
637
638

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

639
        """
640
        from string import join
641
642
643

        DaPlatForm = self.DaPlatForm

644
        if self['time.end'] < self['time.finish']:
645

Peters, Wouter's avatar
test    
Peters, Wouter committed
646
            # file ID and names
karolina's avatar
karolina committed
647
648
649
650
            jobid = self['time.end'].strftime('%Y%m%d') 
            targetdir = os.path.join(self['dir.exec'])
            jobfile = os.path.join(targetdir, 'jb.%s.jb' % jobid)
            logfile = jobfile.replace('.jb', '.log')
Peters, Wouter's avatar
test    
Peters, Wouter committed
651
652

            # Template and commands for job
karolina's avatar
karolina committed
653
654
655
656
            jobparams = {'jobname':"j.%s" % jobid, 'jobtime':'06:00:00', 'logfile':logfile, 'errfile':logfile}
            template = DaPlatForm.GetJobTemplate(jobparams)
            execcommand = os.path.join(self['dir.da_submit'], sys.argv[0]) 
            template += 'python %s rc=%s %s' % (execcommand, self['da.restart.fname'], join(self.opts, ''),) 
Peters, Wouter's avatar
test    
Peters, Wouter committed
657
658

            # write and submit 
karolina's avatar
karolina committed
659
660
            DaPlatForm.WriteJob(jobfile, template, jobid)
            jobid = DaPlatForm.SubmitJob(jobfile, joblog=logfile) 
661
662
663
664
        else:
            logging.info('Final date reached, no new cycle started')


karolina's avatar
karolina committed
665
    def SubmitSubStep(self, stepname):
Peters, Wouter's avatar
Peters, Wouter committed
666
667
668
669
670
671
672
673
        """ 
        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`

        """
674
        from string import join
karolina's avatar
karolina committed
675
        
676
677
        DaPlatForm = self.DaPlatForm

karolina's avatar
karolina committed
678
679
680
681
682
683
        jobparams = {'jobname':'das.%s' % stepname}
        template = DaPlatForm.GetJobTemplate(jobparams)
        template += 'cd %s\n' % os.getcwd()
        template += '%s rc=%s process=%s %s' % (sys.argv[0], self['jobrcfilename'], stepname, join(self.opts, ''),) 
        jobfile = DaPlatForm.WriteJob(self, template, stepname)
        DaPlatForm.SubmitJob(jobfile) 
684

685
686
    def CleanUpCycle(self):
        """
Peters, Wouter's avatar
test    
Peters, Wouter committed
687
        Nothing to do for now anymore
688
689
690
        """


691

692
def StartLogger(level=logging.INFO):
693
694
695
696
    """ start the logging of messages to screen"""

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

karolina's avatar
karolina committed
697
698
699
    logging.basicConfig(level=level,
                        format=' [%(levelname)-7s] (%(asctime)s) py-%(module)-20s : %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
700
701

def ParseOptions():
702
703
704
705
706
707
708
709
710
711
712
713
714
    """ 
    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
    ========  =======

    """
715
716
717
718
    import getopt

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

karolina's avatar
karolina committed
719
720
    opts = []
    args = []
721
722
723
    try:                                
        opts, args = getopt.gnu_getopt(sys.argv[1:], "-hrv")
    except getopt.GetoptError, msg:           
karolina's avatar
karolina committed
724
        logging.error('%s' % msg)
725
726
727
        sys.exit(2)      

    for options in opts:
karolina's avatar
karolina committed
728
        options = options[0].lower()
729
730
731
732
733
734
735
736
        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')
karolina's avatar
karolina committed
737
            logging.root.setLevel(logging.DEBUG)
738

739
    if opts: 
karolina's avatar
karolina committed
740
        optslist = [item[0] for item in opts]
741
    else:
karolina's avatar
karolina committed
742
        optslist = []
743

744
745
# Parse arguments and return as dictionary

karolina's avatar
karolina committed
746
    arguments = {}
747
748
749
750
751
752
753
754
    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
755
756
            logging.error('%s' % 'Argument passed without description (%s)' % item)
            raise getopt.GetoptError, arg
757

karolina's avatar
karolina committed
758
        arguments[key] = arg
759
760


761
    return optslist, arguments
762

karolina's avatar
karolina committed
763
def ValidateOptsArgs(opts, args):
764
765
766
767
768
    """ 
 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.  
 
    """
769
    if not args.has_key("rc"):
karolina's avatar
karolina committed
770
771
772
        msg = "There is no rc-file specified on the command line. Please use rc=yourfile.rc"
        logging.error(msg)
        raise IOError, msg
773
    elif not os.path.exists(args['rc']):
karolina's avatar
karolina committed
774
775
776
        msg = "The specified rc-file (%s) does not exist " % args['rc'] 
        logging.error(msg)
        raise IOError, msg
777
778
779
780
781
782
783
784
785

    # 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
786
    return opts, args
787
788
789


if __name__ == "__main__":
790
791

    sys.path.append('../../')
karolina's avatar
karolina committed
792
    opts, args = ParseOptions()
793
794
    print opts
    print args
795