das.py 3.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# das.py

"""
Author : peters 

Revision History:
File created on 29 Sep 2009.

"""
11
from da_initexit import CycleControl
12
13
14
15
16
17
18
19
20

header  = '\n\n    ***************************************   '
footer  = '    *************************************** \n  '

validprocess=['jobstart','jobinput','sample','invert','propagate','resubmit','all']

def JobStart(opts,args):
    """ Set up the job specific directory structure and create an expanded rc-file """

21
    DaCycle = CycleControl(opts,args)
22

23
    DaCycle.Initialize()
24

25
    return DaCycle
26
27


28
def JobInput(CycleInfo):
29
30
    """ Set up the input data for the forward model: obs and parameters/fluxes"""
    from tools_da import PrepareObs
31
32
    from tools_da import PrepareState
    from tools_da import AddObsToState
33

34
    Samples       = PrepareObs(CycleInfo,'forecast')
35

36
    StateVector   = PrepareState(CycleInfo )
37

38
39
40
41
    dummy         = AddObsToState(CycleInfo, StateVector, Samples)


    return StateVector
42

43
def Sample(CycleInfo, StateVector):
44
    """ Sample the filter state for the inversion """
45
    from tools_da import RunForecastModel
46
47
48
49
50
    from tools_da import ReadModelSamples

    # Implement something that writes the ensemble member parameter info to file, or manipulates them further into the 
    # type of info needed in TM5/WRF/SIBCASA
    # Run the forecast model
51

52
    dummy = RunForecastModel(CycleInfo,'forecast')
53

54
55
56
57
58
    # Read forecast model samples that were written to NetCDF files

    dummy = ReadModelSamples(StateVector)

    # Optionally, post-processing of the model output can be added that deals for instance with
59
60
61
62
    # sub-sampling of time series, vertical averaging, etc.

    return None

63
def Invert(CycleInfo, StateVector ):
64
65
66
    """ Perform the inverse calculation """
    import tools_da

67
    dummy = tools_da.Optimize(CycleInfo,StateVector)
68
69
70

    return None

71
def Propagate(CycleInfo):
72
    """ Propagate the filter state to the next step """
73
    from tools_da import RunForecastModel
74
75
76

# This is the advance of the modeled CO2 state. Optionally, routines can be added to advance the state vector (mean+covariance)

77
    dummy = RunForecastModel(CycleInfo,'advance')
78
79
80

    return None

81
def SaveAndSubmit(CycleInfo):
82
83
    """ Save the model state and submit the next job """

84
    dummy = CycleInfo.Finalize()
85
86
87
88
89
90
91
92

    return None

if __name__ == "__main__":
    import sys
    import os
    import logging
    import shutil
93
94
95

    from tools_da import CleanUpCycle
    from tools_da import ValidateOptsArgs
96
97
98
99
100
101
102
    from tools_da import ParseOptions
    from tools_da import StartLogger

# Append current working dir to path

    sys.path.append(os.getcwd())

103
104
105
106
107
# Start a logger for all that happens from here on

    dummy   = StartLogger()

# Parse options from the command line
108
109
110

    opts, args = ParseOptions()

111
112
113
114
115
116
117
118
119
# Validate Options and arguments passed

    opts,args   = ValidateOptsArgs(opts,args)

# Start the subprocesses

    msg         = header+"starting JobStart"+footer           ; logging.info(msg) 
    CycleInfo   = JobStart(opts,args)

120
121
    msg   = header+"starting JobInput"+footer           ; logging.info(msg) 
    StateVector = JobInput(CycleInfo)
122
123

    msg   = header+"starting Sample Taking"+footer         ; logging.info(msg) 
124
    dummy = Sample(CycleInfo, StateVector)
125

126
127
    msg   = header+"starting Invert"+footer             ; logging.info(msg) 
    dummy = Invert(CycleInfo, StateVector )
128
129
130
131
132
133
134
135
136
137
138

    msg   = header+"starting Propagate"+footer          ; logging.info(msg) 
    dummy = Propagate(CycleInfo)

    msg   = header+"starting SaveAndSubmit"+footer      ; logging.info(msg) 
    dummy = SaveAndSubmit(CycleInfo)

    msg   = "Cycle finished...exiting"     ; logging.info(msg)

    dummy = CleanUpCycle(CycleInfo)

139
    sys.exit(0)