-
Notifications
You must be signed in to change notification settings - Fork 2
/
obs_factory.py
184 lines (158 loc) · 5.96 KB
/
obs_factory.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""This file is part of pyPDAF
Copyright (C) 2022 University of Reading and
National Centre for Earth Observation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import log
import numpy as np
import pyPDAF.PDAF as PDAF
import config_obsA
import config_obsB
import localisation
import model
import obsA
import obsB
import parallelisation
class obs_factory:
"""This class implements all user-supplied functions
used by PDAFomi. These functions are called at every time steps
Attributes
----------
pe : `parallelisation.parallelisation`
parallelization object
model : `model.model`
model object
local : `localisation.localisation`
localisation object
obs_list : list
list of observation types
nobs : int
total number of observation types
"""
def __init__(self, pe:parallelisation.parallelisation, model_t:model.model, local:localisation.localisation) -> None:
# Initialise observations
self.pe: parallelisation.parallelisation = pe
self.model:model.model = model_t
self.local:localisation.localisation = local
self.obs_list:list = []
self.nobs:int = 0
if config_obsA.assim:
self.nobs += 1
self.obs_list.append(obsA.obsA(self.nobs, self.pe, self.model, self.local)
)
if config_obsB.assim:
self.nobs += 1
self.obs_list.append(obsB.obsB(self.nobs, self.pe, self.model, self.local)
)
log.logger.info (f'total number of observation types: {self.nobs}')
PDAF.omi_init(self.nobs)
def init_dim_obs_pdafomi(self, step:int, dim_obs:int) -> int:
"""initialise observation dimensions
Parameters
----------
step : int
current time step
dim_obs : int
dimension of observation vector
Returns
-------
dim_obs : int
dimension of observation vector
"""
# it is possibly useful to add some checks on obs.doassim here
# For example, set obs.doassim = 0 if one type of observation
# is not used for this particular step.
for obs in self.obs_list:
if step % obs.dtobs == 0:
obs.doassim = 1
# calculate the dimension of full observation vector
dim_obs = 0
for obs in self.obs_list:
if obs.doassim == 1:
dim_obs_o:int = obs.init_dim(step, dim_obs)
dim_obs = dim_obs + dim_obs_o
return dim_obs
def obs_op_pdafomi(self, step:int, dim_p:int, dim_obs_p:int, state_p:np.ndarray, ostate:np.ndarray) -> np.ndarray:
"""turn state vector to observation space
Parameters
----------
step : int
current time step
dim_p: int
dimension of state vector on local processor
dim_obs_p: int
dimension of observation vector on local processor
state_p : ndarray
local PE state vector
ostate : ndarray
state vector in obs space
Returns
-------
ostate : ndarray
state vector in obs space
"""
for obs in self.obs_list:
if obs.doassim == 1:
ostate = obs.obs_op(step, state_p, ostate)
return ostate
def init_dim_obs_l_pdafomi(self, domain_p:int, step:int, dim_obs:int, dim_obs_l:int) -> int:
"""initialise number of observations in each local domain
Parameters
----------
domain_p : int
index of current local analysis domain
step : int
current time step
dim_obs : int
dimension of observation vector
dim_obs_l : int
dimension of local observation vector
Returns
-------
dim_obs_l : int
dimension of local observation vector
"""
dim_obs_l = 0
for obs in self.obs_list:
if obs.doassim == 1:
dim_obs_l_o:int = obs.init_dim_obs_l(domain_p, step, dim_obs, dim_obs_l)
dim_obs_l = dim_obs_l + dim_obs_l_o
return dim_obs_l
def localize_covar_pdafomi(self, dim_p:int, dim_obs:int, HP_p:np.ndarray, HPH:np.ndarray) -> tuple[np.ndarray, np.ndarray]:
"""getting localised covariance matrix
Parameters
----------
dim_p: int
dimension of state vector on local processor
dim_obs_p: int
dimension of observation vector on local processor
HP_p : ndarray
matrix HP
HPH : ndarray
matrix HPH
Returns
-------
HP_p : ndarray
matrix HP
HPH : ndarray
matrix HPH
"""
# coords_p is the coordinate of the state vector which should have the same
# unit as the obervation coordinates
coords_p = np.zeros((2, dim_p))
offset = self.pe.mype_filter*self.model.nx_p
coords_p[0] = np.tile(np.arange(self.model.nx_p) + offset, self.model.ny_p)
coords_p[1] = np.repeat(np.arange(self.model.ny_p), self.model.nx_p)
coords_p = coords_p + 1
for obs in self.obs_list:
obs.localize_covar(dim_p, dim_obs, HP_p, HPH, coords_p)
return HP_p, HPH