ct_optimizer_tools.py 2.42 KB
Newer Older
Peters, Wouter's avatar
Peters, Wouter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# ct_optimizer_tools.py

"""
Author : peters 

Revision History:
File created on 28 Jul 2010.

"""

import os
import sys
import rc
import logging
import datetime

identifier = 'CarbonTracker CO2'
################### Begin Class CtOptimizer ###################

class CtOptimizer():
    """
        This creates an instance of a CarbonTracker optimization object. It handles the minimum least squares optimization
        of the CT state vector given a set of CT sample objects. Two routines will be implemented: one where the optimization
        is sequential and one where it is the equivalent matrix solution. The choice can be made based on considerations of speed
        and efficiency.
    """

    def __init__(self, CycleInfo, DaInfo):

        self.nlag               = int(CycleInfo.da_settings['time.nlag'])
        self.nmembers           = int(CycleInfo.da_settings['forecast.nmembers'])
        self.nparams            = int(DaInfo.da_settings['nparameters'])

        self.CreateMatrices()

        return None

    def CreateMatrices(self):
        """ Create Matrix space needed in optimization routine """
        import numpy as np

        self.X_prior         = np.zeros( (self.nlag*self.nparams,), float)
        self.X_prior_prime   = np.zeros( (self.nmembers,self.nlag*self.nparams,), float)

    def FillMatrices(self,StateVector):
        import numpy as np

        for n in range(self.nlag):

            self.X_prior[n*self.nparams:(n+1)*self.nparams]            = StateVector.MeanValues[n]
            members                                                    = StateVector.EnsembleMembers[n]
            self.X_prior_prime[:,n*self.nparams:(n+1)*self.nparams]    = np.array([m.MeanDeviations for m in members])
        
        return None


################### End Class CtOptimizer ###################





if __name__ == "__main__":

    import os
    import sys
    from tools_da import StartLogger 
    from da_initexit import CycleControl 
    import numpy as np
    from ct_tools import DaInfo
    from ct_statevector_tools import CtStateVector, PrepareState

    opts = ['-v']
    args = {'rc':'da.rc','logfile':'da_initexit.log','jobrcfilename':'test.rc'}

    StartLogger()
    DaCycle = CycleControl(opts,args)
    Da_Info = DaInfo('carbontracker.rc')

    DaCycle.Initialize()
    print DaCycle

    StateVector = PrepareState(DaCycle)

    opt = CtOptimizer(DaCycle,Da_Info)

    opt.FillMatrices(StateVector)