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

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

Revision History:
File created on 13 May 2009.

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

Valid options are defined in 

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

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

    ! Info on the data assimilation cycle

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

    ! Info on the DA system used

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

    ! Info on the forward model to be used

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

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

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

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

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

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


56
"""
karolina's avatar
karolina committed
57
58
59
60
import logging
import os
import sys
import shutil
karolina's avatar
karolina committed
61
62
63
64
65
import copy
from string import join

import da.tools.rc as rc
from da.tools.general import create_dirs
66

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

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


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

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

98
99
        """

100
101
        self.load_rc(args['rc'])
        self.validate_rc()
102
103
104
105
        self.opts = opts

        # Add some useful variables to the rc-file dictionary

karolina's avatar
karolina committed
106
107
        self['jobrcfilename'] = self.RcFileName
        self['dir.da_submit'] = os.getcwd()
108
        self['da.crash.recover'] = '-r' in opts
karolina's avatar
karolina committed
109
110
111
112
        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
113
114


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

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

karolina's avatar
karolina committed
126
        logging.info('DA Cycle rc-file (%s) loaded successfully' % self.RcFileName)
127
128


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

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

160
    def parse_times(self):
161
162
163
        """ 
        Parse time related parameters into datetime objects for later use 
        """
164
        from da.tools.general import advance_time
165
166
167
168
169

        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
        """ 
        Advance sampling start and end time by one cycle interval
        """
220
        from da.tools.general import advance_time
221

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

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

228
    def advance_cycle_times(self):
229
230
231
        """ 
        Advance cycle start and end time by one cycle interval
        """
232
233
234
        from da.tools.general import advance_time
              
        days = self['cyclelength'].days                  
235

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

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

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


246
    def random_seed(self, action='read'):
Peters, Wouter's avatar
Peters, Wouter committed
247
248
        """ 
        Get the randomseed and save it, or read the random seed and set it. The seed is currently stored
249
        in a python :mod:`pickle` file, residing in the ``exec`` directory
Peters, Wouter's avatar
Peters, Wouter committed
250
251

        """
252
253
254
        import cPickle
        import numpy as np

karolina's avatar
karolina committed
255
        filename = os.path.join(self['dir.exec'], 'randomseed.pickle')
256
257

        if action == 'write':
karolina's avatar
karolina committed
258
259
            f = open(filename, 'wb')
            seed = np.random.get_state()
karolina's avatar
karolina committed
260
261
            cPickle.dump(seed, f, -1)
            f.close()
262

karolina's avatar
karolina committed
263
            msg = "Saved the random seed generator values to file"
264
265

        if action == 'read':
karolina's avatar
karolina committed
266
267
            f = open(filename, 'rb')
            seed = cPickle.load(f)
karolina's avatar
karolina committed
268
269
            np.random.set_state(seed)
            f.close()
270

karolina's avatar
karolina committed
271
            msg = "Retrieved the random seed generator values from file"
272
273

        logging.info(msg)
karolina's avatar
karolina committed
274
        self.RestartFileList.append(filename)
karolina's avatar
karolina committed
275
        logging.debug("Added the randomseed.pickle file to the RestartFileList")
276
277


278
    def initialize(self):
279
280
281
        """ 
        This method determines how to proceed with the cycle. Three options are implemented:

Peters, Wouter's avatar
Peters, Wouter committed
282
283
284
            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
285
286
287

        The choice that gets executed depends on the presence of 

Peters, Wouter's avatar
Peters, Wouter committed
288
289
            # the ``-r`` option on the command line, this triggers a recover
            # the ``time.restart : True`` option in the da.rc file
290
291

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

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

Peters, Wouter's avatar
Peters, Wouter committed
296
            1. *Fresh Start*
297
                *  :meth:`~da.tools.initexit.CycleControl.setup_file_structure()`  <- Create directory tree
Peters, Wouter's avatar
Peters, Wouter committed
298
            2. *Restart*
299
300
                *  :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
301
            3. *Recover*
302
303
304
                *  :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
305
306
307

        And is always followed by a call to

308
            * parse_times()
Peters, Wouter's avatar
Peters, Wouter committed
309
            * WriteRc('jobfilename')
310
311
312
313
314
        """        
#
# case 1: A recover from a previous crash, this is signaled by flag "-r"
#
        if self['da.crash.recover']:
karolina's avatar
karolina committed
315
            logging.info("Recovering simulation from data in: %s" % self['dir.da_run'])
316
317
318
            self.setup_file_structure()
            self.recover_run()
            self.random_seed('read')
319
320
321
322
#
# case 2: A continuation, this is signaled by rc-item time.restart = True
#
        elif self['time.restart']:
karolina's avatar
karolina committed
323
            logging.info("Restarting filter from previous step")
324
            self.setup_file_structure()
325
            strippedname = os.path.split(self['jobrcfilename'])[-1]
karolina's avatar
karolina committed
326
            self['jobrcfilename'] = os.path.join(self['dir.exec'], strippedname)
327
            self.random_seed('read')
328
329
330
331
#
# case 3: A fresh start, this is signaled by rc-item time.restart = False
#
        elif not self['time.restart']:
karolina's avatar
karolina committed
332
            logging.info("First time step in filter sequence")
333
            self.setup_file_structure()
334
335

            # expand jobrcfilename to include exec dir from now on.
336
337
338
            # First strip current leading path from filename

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

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

344
    def setup_file_structure(self):
345
346
        """ 
        Create file structure needed for data assimilation system.
Peters, Wouter's avatar
Peters, Wouter committed
347
348
349
350
351
352
353
354
355
        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``
356
357
            * ``${da_rundir}/restart/current``
            * ``${da_rundir}/restart/one-ago``
Peters, Wouter's avatar
Peters, Wouter committed
358

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

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

366
367
368
369
        """

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

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

karolina's avatar
karolina committed
372
373
374
375
376
377
378
379
380
        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')
381

382
383
384
385
386
387
388
389
390
391
        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.diagnostics']))
        create_dirs(os.path.join(self['dir.analysis']))
        create_dirs(os.path.join(self['dir.jobs']))
        create_dirs(os.path.join(self['dir.restart']))
        create_dirs(os.path.join(self['dir.restart.current']))
        create_dirs(os.path.join(self['dir.restart.oneago']))
392

karolina's avatar
karolina committed
393
        logging.info('Succesfully created the file structure for the assimilation job')
394
#LU tutaj chyba brakuje move restart data
395
    def recover_run(self):
Peters, Wouter's avatar
Peters, Wouter committed
396
397
        """
        Prepare a recovery from a crashed run. This consists of: 
398
        
399
            - copying all data from the restart/one-ago folder (:meth:`~da.tools.initexit.CycleControl.move_restart_data`),
400
            - replacing all ``rc-file`` items with those from the ``da_runtime.rc`` in the restart/current dir 
401
            - resetting the seed of the random number generator to the value it had before the crash (:meth:`~da.tools.initexit.CycleControl.random_seed`)
402
            - replacing the output dir name, since it has the sample time in it...
Peters, Wouter's avatar
Peters, Wouter committed
403
            
404
        """
karolina's avatar
karolina committed
405
        
406

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

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

karolina's avatar
karolina committed
412
        for k, v in rc_rec.iteritems():
413
414
            self[k] = v

415
        self.validate_rc()
416

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

420
        # Copy randomseed.pickle file to exec dir
421
        source = os.path.join(self['dir.restart.current'], 'randomseed.pickle') #LU wydaje mi sie ze tutaj nie trzeba podawac nazwy pliku w folderze docelowym, jesli sie obczai ze to folder to sie kopiuje.
karolina's avatar
karolina committed
422
423
        dest = os.path.join(self['dir.exec'], 'randomseed.pickle')
        shutil.copy(source, dest)
424

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

karolina's avatar
karolina committed
428
429
        filtertime = self['time.start'].strftime('%Y%m%d')
        self['dir.output'] = os.path.join(self['dir.da_run'], 'output', filtertime)
430
        create_dirs(os.path.join(self['dir.output']))
431

432
    def finalize(self):
433
        """
434
        finalize the da cycle, this means writing the save data and rc-files for the next run. 
435
436
437
        The following sequence of actions occur:

            * Write the randomseed to file for reuse in next cycle
Peters, Wouter's avatar
Peters, Wouter committed
438
            * Write a new ``rc-file`` with ``time.restart : True``, and new ``time.start`` and ``time.end``
439
440
441
442
            * 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

443
        """
444
445
446
447
448
449
450
451
        self.random_seed('write')
        self.write_new_rc_file()
        self.move_restart_data(io_option='store')  # Move restart data from current to one-ago
        self.collect_restart_data()  # Collect restart data for next cycle into a clean restart/current folder
        self.collect_output()  # Collect restart data for next cycle into a clean restart/current folder
        self.submit_next_cycle()

    def collect_output(self):
452
        """ Collect files that are part of the requested output for this cycle. This function allows users to add files 
453
454
455
456
457
458
459
            to a list, and then the system will copy these to the current cycle's output directory.
            The list of files included is read from the 
            attribute "OutputFileList" which is a simple list of files that can be appended by other objects/methods that
            require output data to be saved.


        """
karolina's avatar
karolina committed
460
        targetdir = os.path.join(self['dir.output'])
461
        create_dirs(os.path.join(targetdir))
462

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

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

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
    def collect_restart_data(self):
479
        """ Collect files needed for the restart of this cycle in case of a crash, or for the continuation of the next cycle. 
480
            All files needed are written to the restart/current directory. The list of files included is read from the 
481
482
483
            attribute "RestartFileList" which is a simple list of files that can be appended by other objects/methods that
            require restart data to be saved.

484
            .. note:: Before collecting the files in the ``RestartFileList``, the restart/current directory will be emptied and
485
486
487
488
489
                     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:
490

491
                * The ``da_runtime.rc`` file
492
                * The ``randomseed.pickle`` file
493
494
                * The savestate.nc file
                * The files in the ``ObservationOperator.RestartFileList``, i.e., restart data for the transport model
495

496

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

        """

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

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

506
        create_dirs(os.path.join(targetdir), forceclean=True)
507

karolina's avatar
karolina committed
508
509
        logging.info("Collecting the required restart data")
        logging.debug("           to   directory: %s " % targetdir)
510

511
        for file in set(self.RestartFileList):
512
513
            if os.path.isdir(file): # skip dirs
                continue
514
            if not os.path.exists(file): # skip dirs
karolina's avatar
karolina committed
515
                logging.warning("           [not found] .... %s " % file)
516
            else:
karolina's avatar
karolina committed
517
518
                logging.debug("           [copy] .... %s " % file)
                shutil.copy(file, file.replace(os.path.split(file)[0], targetdir))
519
520


521
    def move_restart_data(self, io_option='restore'):
Peters, Wouter's avatar
Peters, Wouter committed
522
        """ 
523
        Store or restore model state to/from a restart directory. 
524

525
            Two IO options are available:
526

527
528
            (1) io_option = restore    : Get data from restart.oneago directory
            (2) io_option = store      : Save data to restart.oneago directory
529

530
            In case of a 'store' command the restart.oneago folder is re-created so that the contents are empty to begin with.
531
532

        """
karolina's avatar
karolina committed
533
534
        if io_option not in ['store', 'restore']:
            raise ValueError, 'Invalid option specified for io_option (%s)' % io_option
535

536
537
538
539
540
541
        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']
542
543
544
545

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

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

548
        logging.debug("Performing a %s of data" % io_option)
karolina's avatar
karolina committed
549
550
        logging.debug("           from directory: %s " % sourcedir)
        logging.debug("           to   directory: %s " % targetdir)
551
552
553


        for file in os.listdir(sourcedir):
karolina's avatar
karolina committed
554
            file = os.path.join(sourcedir, file)
555
            if not os.path.exists(file):
karolina's avatar
karolina committed
556
                logging.debug("Cannot find requested file to move: %s " % file)  
557
558
                sys.exit(2)

559
            if os.path.isdir(file): # skip dirs
karolina's avatar
karolina committed
560
                logging.debug("           [skip] .... %s " % file)
561
                continue    
562
            else:
karolina's avatar
karolina committed
563
564
                logging.debug("           [copy] .... %s " % file)
                shutil.copy(file, file.replace(sourcedir, targetdir))
565
566

#
567
    def write_new_rc_file(self):
568
569
570
571
572
573
        """ 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
574
575
            
        """
karolina's avatar
karolina committed
576
577
        
        
578

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

karolina's avatar
karolina committed
581
        newDaCycle = copy.deepcopy(self)
582
        newDaCycle.advance_cycle_times()
karolina's avatar
karolina committed
583
        newDaCycle['time.restart'] = True
584

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

karolina's avatar
karolina committed
587
588
589
        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)
590

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

karolina's avatar
karolina committed
593
        self['da.restart.fname'] = fname    # needed for next job template
594
        self.RestartFileList.append(fname)  # current restart list holds next rc file name
595

karolina's avatar
karolina committed
596
        logging.debug('Added da_runtime.rc to the RestartFileList for later collection')
597
598


599
    def write_rc(self, fname):
600
601
        """ Write RC file after each process to reflect updated info """

karolina's avatar
karolina committed
602
        rc.write(fname, self)
karolina's avatar
karolina committed
603
        logging.debug('Wrote expanded rc-file (%s)' % fname)
karolina's avatar
karolina committed
604
        
605

606
    def submit_next_cycle(self):
607
        """ 
Peters, Wouter's avatar
Peters, Wouter committed
608
        Submit the next job of a DA cycle, this consists of 
609
610
611
            * 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
612
613
614

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

615
        """
karolina's avatar
karolina committed
616
        
617

618
        if self['time.end'] < self['time.finish']:
619

Peters, Wouter's avatar
test    
Peters, Wouter committed
620
            # file ID and names
karolina's avatar
karolina committed
621
622
623
            jobid = self['time.end'].strftime('%Y%m%d') 
            targetdir = os.path.join(self['dir.exec'])
            jobfile = os.path.join(targetdir, 'jb.%s.jb' % jobid)
624
625
            logfile = os.path.join(targetdir, 'jb.%s.log' % jobid)
#LU tutaj sa parametry ktore ida na gore do pliku job. nie zawsze koniecznie potrzebne.
Peters, Wouter's avatar
test    
Peters, Wouter committed
626
            # Template and commands for job
627
628
            jobparams = {'jobname':"j.%s" % jobid, 'jobtime':'06:00:00', 'logfile': logfile, 'errfile': logfile}
            template = self.DaPlatForm.get_job_template(jobparams)
karolina's avatar
karolina committed
629
            execcommand = os.path.join(self['dir.da_submit'], sys.argv[0]) 
karolina's avatar
karolina committed
630
            template += 'python %s rc=%s %s >&%s' % (execcommand, self['da.restart.fname'], join(self.opts, ''), logfile) 
Peters, Wouter's avatar
test    
Peters, Wouter committed
631
632

            # write and submit 
633
634
            self.DaPlatForm.write_job(jobfile, template, jobid)
            jobid = self.DaPlatForm.submit_job(jobfile, joblog=logfile) 
635
636
637
638
        else:
            logging.info('Final date reached, no new cycle started')


639
def start_logger(level=logging.INFO):
640
641
642
643
    """ start the logging of messages to screen"""

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

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

648
def parse_options():
649
650
651
652
653
654
655
656
657
658
659
660
661
    """ 
    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
    ========  =======

    """
662
663
664
665
    import getopt

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

karolina's avatar
karolina committed
666
667
    opts = []
    args = []
668
    try:                                
669
        opts, args = getopt.gnu_getopt(sys.argv[1:], "-rv")
670
    except getopt.GetoptError, msg:           
karolina's avatar
karolina committed
671
        logging.error('%s' % msg)
672
673
674
        sys.exit(2)      

    for options in opts:
karolina's avatar
karolina committed
675
        options = options[0].lower()
676
677
678
679
        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
680
            logging.root.setLevel(logging.DEBUG)
681

682
    if opts: 
683
        optslist = [item[0] for item in opts] #LU ze co same minusy zwroci?
684
    else:
karolina's avatar
karolina committed
685
        optslist = []
686

687
688
# Parse arguments and return as dictionary

karolina's avatar
karolina committed
689
    arguments = {}
690
691
692
693
694
695
696
697
    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
698
699
            logging.error('%s' % 'Argument passed without description (%s)' % item)
            raise getopt.GetoptError, arg
700

karolina's avatar
karolina committed
701
        arguments[key] = arg
702
703


704
    return optslist, arguments
705

706
def validate_opts_args(opts, args):
707
708
709
710
711
    """ 
 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.  
 
    """
712
    if not args.has_key("rc"):
karolina's avatar
karolina committed
713
714
715
        msg = "There is no rc-file specified on the command line. Please use rc=yourfile.rc"
        logging.error(msg)
        raise IOError, msg
716
    elif not os.path.exists(args['rc']):
karolina's avatar
karolina committed
717
718
719
        msg = "The specified rc-file (%s) does not exist " % args['rc'] 
        logging.error(msg)
        raise IOError, msg
720
721
722
723
724
725
726
727
728

    # 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
729
    return opts, args
730
731
732


if __name__ == "__main__":
733
734

    sys.path.append('../../')
735
    opts, args = parse_options()
736
737
    print opts
    print args
738