pppy 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pppy/__init__.py +112 -0
- pppy/pppy.py +252 -0
- pppy/pppyComp.py +867 -0
- pppy-1.1.0.dist-info/METADATA +18 -0
- pppy-1.1.0.dist-info/RECORD +9 -0
- pppy-1.1.0.dist-info/WHEEL +5 -0
- pppy-1.1.0.dist-info/licenses/LICENCE-fr.txt +521 -0
- pppy-1.1.0.dist-info/licenses/LICENSE-en.txt +517 -0
- pppy-1.1.0.dist-info/top_level.txt +1 -0
pppy/__init__.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
This module aims at calling different implementations
|
|
4
|
+
of an individual parameterization to compare them.
|
|
5
|
+
|
|
6
|
+
``pppy`` stands for Physical Parameterizations with PYthon.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__all__ = []
|
|
10
|
+
|
|
11
|
+
__version__ = '1.1.0'
|
|
12
|
+
|
|
13
|
+
__license__ = 'CeCILL-C'
|
|
14
|
+
|
|
15
|
+
__authors__ = ['Sébastien Riette']
|
|
16
|
+
|
|
17
|
+
__contributors__ = []
|
|
18
|
+
|
|
19
|
+
#List of the known variable names
|
|
20
|
+
#A variable not listed in VAR_NAME will have an empty legend
|
|
21
|
+
VAR_NAME = {
|
|
22
|
+
'qv': "Specific content of vapor",
|
|
23
|
+
'qc': "Specific content of cloud droptlets",
|
|
24
|
+
'qr': "Specific content of rain",
|
|
25
|
+
'qi': "Specific content of cloud ice",
|
|
26
|
+
'qs': "Specific content of snow",
|
|
27
|
+
'qg': "Specific content of graupel",
|
|
28
|
+
'qh': "Specific content of hail",
|
|
29
|
+
'rv': "Mixing-ratio of vapor",
|
|
30
|
+
'rc': "Mixing-ratio of cloud droptlets",
|
|
31
|
+
'rr': "Mixing-ratio of rain",
|
|
32
|
+
'ri': "Mixing-ratio of cloud ice",
|
|
33
|
+
'rs': "Mixing-ratio of snow",
|
|
34
|
+
'rg': "Mixing-ratio of graupel",
|
|
35
|
+
'rh': "Mixing-ratio of hail",
|
|
36
|
+
'nc': "Number concentration of cloud droplets",
|
|
37
|
+
'nr': "Number concentration of rain",
|
|
38
|
+
'ni': "Number concentration of cloud ice",
|
|
39
|
+
'ns': "Number concentration of snow",
|
|
40
|
+
'ng': "Number concentration of graupel",
|
|
41
|
+
'nh': "Number concentration of hail",
|
|
42
|
+
'T': "Temperature",
|
|
43
|
+
'Theta': "Potential temperature",
|
|
44
|
+
'P': "Pressure",
|
|
45
|
+
't': "Time",
|
|
46
|
+
'Z_half': "Altitude of flux levels",
|
|
47
|
+
'Z_mass': "Altitude of mass levels",
|
|
48
|
+
'ccn1ft': "Water-friendly free aerosol number (CCN)",
|
|
49
|
+
'ccn1at': "Water-friendly activated aerosol number (CCN)",
|
|
50
|
+
'ifn1ft': "Ice-friendly free aerosol number (IFN)",
|
|
51
|
+
'ifn1at': "Ice-friendly activated aerosol number (IFN)",
|
|
52
|
+
'u': "U-component of wind",
|
|
53
|
+
'v': "V-component of wind",
|
|
54
|
+
'w': "Vertical velocity (positive for updraft)",
|
|
55
|
+
"CF": "Cloud fraction",
|
|
56
|
+
'Ps': 'Surface pressure',
|
|
57
|
+
'Zs': 'Surface altitude above sea level',
|
|
58
|
+
'slhf': 'Surface latent heat flux',
|
|
59
|
+
'sshf': 'Surface sensible heat flux',
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#Unit for variables
|
|
63
|
+
#A variable not listed in VAR_UNIT will have an empty unit in legends
|
|
64
|
+
VAR_UNIT = {
|
|
65
|
+
'qv': "kg/kg",
|
|
66
|
+
'qc': "kg/kg",
|
|
67
|
+
'qr': "kg/kg",
|
|
68
|
+
'qi': "kg/kg",
|
|
69
|
+
'qs': "kg/kg",
|
|
70
|
+
'qg': "kg/kg",
|
|
71
|
+
'qh': "kg/kg",
|
|
72
|
+
'rv': "kg/kg",
|
|
73
|
+
'rc': "kg/kg",
|
|
74
|
+
'rr': "kg/kg",
|
|
75
|
+
'ri': "kg/kg",
|
|
76
|
+
'rs': "kg/kg",
|
|
77
|
+
'rg': "kg/kg",
|
|
78
|
+
'rh': "kg/kg",
|
|
79
|
+
'nc': "#/kg",
|
|
80
|
+
'nr': "#/kg",
|
|
81
|
+
'ni': "#/kg",
|
|
82
|
+
'ns': "#/kg",
|
|
83
|
+
'ng': "#/kg",
|
|
84
|
+
'nh': "#/kg",
|
|
85
|
+
'T': "K",
|
|
86
|
+
'Theta': 'K',
|
|
87
|
+
'P': "Pa",
|
|
88
|
+
't': 's',
|
|
89
|
+
'Z_half': 'm',
|
|
90
|
+
'Z_mass': 'm',
|
|
91
|
+
'ccn1ft': "#/kg",
|
|
92
|
+
'ccn1at': "#/kg",
|
|
93
|
+
'ifn1ft': "#/kg",
|
|
94
|
+
'ifn1at': "#/kg",
|
|
95
|
+
'u': 'm/s',
|
|
96
|
+
'v': 'm/s',
|
|
97
|
+
'w': 'm/s',
|
|
98
|
+
'CF': '0-1',
|
|
99
|
+
'Ps': 'Pa',
|
|
100
|
+
'Zs': 'm',
|
|
101
|
+
'slhf': 'W/m²',
|
|
102
|
+
'sshf': 'W/m²'
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
COLORS = ['black', 'red', 'darksalmon', 'gold', 'olivedrab', 'silver',
|
|
106
|
+
'chartreuse', 'skyblue', 'darkblue', 'purple', 'magenta']
|
|
107
|
+
|
|
108
|
+
STYLES = ['-', ':', '--', '-.', (20.0, 20.0), (30.0, 10.0),
|
|
109
|
+
(20., 5., 1., 5., 1., 5., 1., 5.), (20., 5., 5., 5., 5., 5.)]
|
|
110
|
+
|
|
111
|
+
from .pppy import PPPY
|
|
112
|
+
from .pppyComp import PPPYComp
|
pppy/pppy.py
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
# Copyright (c) Météo France (2018-)
|
|
4
|
+
# This software is governed by the CeCILL-C license under French law.
|
|
5
|
+
# http://www.cecill.info
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
The PPPY class is an abstract class to represent an individual parameterization.
|
|
9
|
+
The methods that must (or can) be defined are:
|
|
10
|
+
- __init__ (optional) to list the options allowed for the parameterization
|
|
11
|
+
with default values. super().__init__(...) is then
|
|
12
|
+
called with all arguments.
|
|
13
|
+
- setup (optional) this is the place where the parameterization can be
|
|
14
|
+
initialized. The setup method is called in the
|
|
15
|
+
subprocess reserved for the parameterization whereas
|
|
16
|
+
__init__ is called in the main process. It can be
|
|
17
|
+
important to delay some initialisations here when dealing
|
|
18
|
+
with shared library to prevent interferences. Moreover,
|
|
19
|
+
setup is called each time the parameterization is used
|
|
20
|
+
whereas __init__ is called only once.
|
|
21
|
+
- finalize (optional) to remove temporary files for instance
|
|
22
|
+
- build_init_state (optional) this is the place to add variables to the
|
|
23
|
+
provided common state. For instance, if a
|
|
24
|
+
parameterization needs to track a specific
|
|
25
|
+
variable that is not in the variables tracked
|
|
26
|
+
by the tool.
|
|
27
|
+
- diag (optional) to compute and save diagnostics. This methods is called
|
|
28
|
+
at the end.
|
|
29
|
+
- execute The method receives the state (dictionary holding the different
|
|
30
|
+
variables) and must return the same dictionary containing the
|
|
31
|
+
different variables after a one-time-step advance. If conversions
|
|
32
|
+
(units, array shape, 32bit/64bit...) are needed to call the
|
|
33
|
+
parameterization, this is the place to perform them. Be careful
|
|
34
|
+
to do the reverse conversion after the actual call to return the
|
|
35
|
+
same variables in the output state as the one received.
|
|
36
|
+
If these methods are defined, they must call the method of the abstract class
|
|
37
|
+
through the super() mechanism.
|
|
38
|
+
|
|
39
|
+
The instances of PPPY are intended to be used with an instance of the PPPYComp class
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
import numpy
|
|
43
|
+
import os
|
|
44
|
+
import h5py
|
|
45
|
+
import copy
|
|
46
|
+
|
|
47
|
+
from pppy import VAR_NAME, VAR_UNIT
|
|
48
|
+
|
|
49
|
+
class PPPY():
|
|
50
|
+
"""
|
|
51
|
+
Abstract class used to call a single version of a parameterization.
|
|
52
|
+
|
|
53
|
+
To create a usable class, one needs to implement the following methods:
|
|
54
|
+
- setup (optional): called first, may be used to initialize some values
|
|
55
|
+
- build_init_state (optional): called after setup to complete initial state
|
|
56
|
+
- execute (mandatory): called to compute evolution of state variables
|
|
57
|
+
over one time step
|
|
58
|
+
- finalize (optional): called at the end to enable operations
|
|
59
|
+
like unloading library from memory
|
|
60
|
+
|
|
61
|
+
To be as general as possible, individual variables followed by the
|
|
62
|
+
parameterization are not defined. Only one variable is followed (named
|
|
63
|
+
state in the different methods) and this variable is a dictionary
|
|
64
|
+
whose keys are implementation dependent and can vary from one
|
|
65
|
+
parameterization to the other.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
def __init__(self, dt, method, name, tag, **options):
|
|
69
|
+
"""
|
|
70
|
+
At instantiation, we define the "time integration method" and
|
|
71
|
+
the timestep.
|
|
72
|
+
Moreover, we set name and tag to identify the parameterization.
|
|
73
|
+
|
|
74
|
+
:param dt: time step to use (in seconds)
|
|
75
|
+
:param method: among ('step-by-step', 'one-step'). Two kind of
|
|
76
|
+
simulations are implemented. 'step-by-step' is
|
|
77
|
+
like a true simulation, output of a time step is
|
|
78
|
+
computed from output of previous time step.
|
|
79
|
+
For 'one-step', output (at all output times)
|
|
80
|
+
is computed by direct integration from the initial state
|
|
81
|
+
:param name: name of the parameterization, used in legends
|
|
82
|
+
:param tag: string to identify uniquely the parameterization, the
|
|
83
|
+
string must be usable to build filenames
|
|
84
|
+
:param \*\*options: a specific implementation may add some args and
|
|
85
|
+
kwargs. Those transmitted to this __init__
|
|
86
|
+
method (through this \*\*options argument) will be
|
|
87
|
+
stored in the output file as metadata.
|
|
88
|
+
|
|
89
|
+
This method can be extended by a specific implementation to add,
|
|
90
|
+
in particular, specific options.
|
|
91
|
+
|
|
92
|
+
Instances are intended to be used by a PPPYComp instance.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
assert isinstance(dt, float), "dt must be a float"
|
|
96
|
+
assert dt > 0., "dt must be > 0."
|
|
97
|
+
assert method in ('step-by-step', 'one-step'), "method must be 'step-by-step' or 'one-step'"
|
|
98
|
+
|
|
99
|
+
self._dt = dt
|
|
100
|
+
self._method = method
|
|
101
|
+
self.name= name
|
|
102
|
+
self.tag = tag
|
|
103
|
+
self._options = options
|
|
104
|
+
|
|
105
|
+
def setup(self, init_state, duration):
|
|
106
|
+
"""
|
|
107
|
+
This method is called each time the __call__ method is called.
|
|
108
|
+
|
|
109
|
+
:param init_state: dictionary holding the 'state' variables
|
|
110
|
+
:param duration: the simulation total duration in seconds
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
pass
|
|
114
|
+
|
|
115
|
+
def finalize(self):
|
|
116
|
+
"""
|
|
117
|
+
This method is called at the end of each call to the __call__ method.
|
|
118
|
+
This method can be implemented if something needs to be done after execution.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
def execute(self, previous_state, timestep, timestep_number):
|
|
124
|
+
"""
|
|
125
|
+
This method do the computational part of the time advance
|
|
126
|
+
(setup and building of the initial state are done before).
|
|
127
|
+
|
|
128
|
+
:param previous_state: dictionary holding all the followed 'state' variables.
|
|
129
|
+
:param timestep: is the simulation duration in seconds (this is the time step
|
|
130
|
+
defined in __init__ if method is 'step-by-step')
|
|
131
|
+
:param timestep_number: is the number of current time step (always 1 if method is 'one-step')
|
|
132
|
+
:returns: a 'state' dictionary containing variable values after time integration
|
|
133
|
+
|
|
134
|
+
The method must be implemented.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
assert isinstance(previous_state, dict), "state must be a dictionary"
|
|
138
|
+
assert isinstance(timestep, float), "duration must be a float"
|
|
139
|
+
assert timestep > 0., "duration must be > 0."
|
|
140
|
+
assert isinstance(timestep_number, int), "timestep_number must be an integer"
|
|
141
|
+
assert timestep_number >= 1, "timestep_number must be at least 1"
|
|
142
|
+
|
|
143
|
+
def _open_file(self, output_file, times, init_state):
|
|
144
|
+
"""
|
|
145
|
+
This internal method opens an hdf5 file and creates all dataset.
|
|
146
|
+
|
|
147
|
+
:param output_file: filename of the hdf5 file to open
|
|
148
|
+
:param times: output times
|
|
149
|
+
:param init_state: dictionary containing the initial 'state'
|
|
150
|
+
|
|
151
|
+
:returns: The method returns the hdf5 file and a dictionary containing the datasets.
|
|
152
|
+
"""
|
|
153
|
+
output = h5py.File(output_file, 'w')
|
|
154
|
+
output.attrs['dt'] = self._dt
|
|
155
|
+
output.attrs['method'] = self._method
|
|
156
|
+
for key, value in self._options.items():
|
|
157
|
+
output.attrs[key] = value
|
|
158
|
+
dset = {}
|
|
159
|
+
dset['t'] = output.create_dataset("t", shape=(len(times),), dtype=float, data=times)
|
|
160
|
+
dset['t'].attrs['name'] = VAR_NAME.get('t', '')
|
|
161
|
+
dset['t'].attrs['unit'] = VAR_UNIT.get('t', '')
|
|
162
|
+
for key, value in init_state.items():
|
|
163
|
+
shape = tuple([len(times)] + list(value.shape))
|
|
164
|
+
dset[key] = output.create_dataset(key, shape=shape, dtype=value.dtype)
|
|
165
|
+
dset[key].set_fill_value = numpy.nan
|
|
166
|
+
dset[key][...] = numpy.nan
|
|
167
|
+
dset[key].attrs['name'] = VAR_NAME.get(key, '')
|
|
168
|
+
if VAR_UNIT.get(key, '') is not None:
|
|
169
|
+
dset[key].attrs['unit'] = VAR_UNIT.get(key, '')
|
|
170
|
+
dset[key][0] = value
|
|
171
|
+
return output, dset
|
|
172
|
+
|
|
173
|
+
def __call__(self, init_state, duration, output_file):
|
|
174
|
+
"""
|
|
175
|
+
This method do the time advance.
|
|
176
|
+
|
|
177
|
+
:param init_state: dictionary holding the 'state' variables
|
|
178
|
+
:param duration: the simulation total duration in seconds
|
|
179
|
+
:param output_file: the filename of the hdf5 file that will
|
|
180
|
+
contain the output variables
|
|
181
|
+
"""
|
|
182
|
+
|
|
183
|
+
assert isinstance(init_state, dict), "state must be a dictionary"
|
|
184
|
+
assert isinstance(duration, float), "duration must be a float"
|
|
185
|
+
assert duration > 0., "duration must be > 0."
|
|
186
|
+
|
|
187
|
+
assert all([isinstance(value, numpy.ndarray) for value in list(init_state.values())]), \
|
|
188
|
+
"All init_state item must be ndarrays"
|
|
189
|
+
|
|
190
|
+
if os.path.exists(output_file):
|
|
191
|
+
raise IOError("output file already exists")
|
|
192
|
+
|
|
193
|
+
#We prepare state and output file
|
|
194
|
+
self.setup(init_state, duration)
|
|
195
|
+
state = self.build_init_state(init_state)
|
|
196
|
+
old_state = copy.deepcopy(state)
|
|
197
|
+
times = numpy.array(list(numpy.arange(0, duration, self._dt)) + [duration])
|
|
198
|
+
output, dset = self._open_file(output_file, times, state)
|
|
199
|
+
|
|
200
|
+
try:
|
|
201
|
+
for i in range(1, len(times)):
|
|
202
|
+
if self._method == 'step-by-step':
|
|
203
|
+
state = self.execute(old_state, times[i] - times[i-1], i)
|
|
204
|
+
else:
|
|
205
|
+
state = self.execute(copy.deepcopy(old_state), times[i], 1)
|
|
206
|
+
for key, value in old_state.items():
|
|
207
|
+
if key not in state:
|
|
208
|
+
state[key] = value
|
|
209
|
+
if self._method == 'step-by-step':
|
|
210
|
+
old_state = state
|
|
211
|
+
for key, value in state.items():
|
|
212
|
+
dset[key][i] = value
|
|
213
|
+
self.diag(output, dset)
|
|
214
|
+
output.close()
|
|
215
|
+
self.finalize()
|
|
216
|
+
except:
|
|
217
|
+
try:
|
|
218
|
+
output.close()
|
|
219
|
+
except KeyboardInterrupt:
|
|
220
|
+
raise
|
|
221
|
+
except:
|
|
222
|
+
pass
|
|
223
|
+
if os.path.exists(output_file):
|
|
224
|
+
os.remove(output_file)
|
|
225
|
+
self.finalize()
|
|
226
|
+
import traceback
|
|
227
|
+
traceback.print_exc()
|
|
228
|
+
raise
|
|
229
|
+
|
|
230
|
+
def build_init_state(self, state):
|
|
231
|
+
"""
|
|
232
|
+
Method used to modify the initial state.
|
|
233
|
+
Initial state can be incomplete. For instance, when comparing microphysical
|
|
234
|
+
schemes, we can only know the initial mixing ratios and this method
|
|
235
|
+
must compute initial concentration number if the scheme needs it.
|
|
236
|
+
This method must add to the dictionary all the missing variables.
|
|
237
|
+
|
|
238
|
+
:param state: dictionary holding the initial 'state' that can be incomplete
|
|
239
|
+
for the actual scheme
|
|
240
|
+
:returns: complete 'state' dictionary
|
|
241
|
+
"""
|
|
242
|
+
|
|
243
|
+
assert isinstance(state, dict), "state must be a dictionay"
|
|
244
|
+
|
|
245
|
+
return state
|
|
246
|
+
|
|
247
|
+
def diag(self, hdf5file, dsets):
|
|
248
|
+
"""
|
|
249
|
+
Method used to acompute diagnostics to be stored in the output file
|
|
250
|
+
:param hdf5file: the output file (in hdf5 format)
|
|
251
|
+
:param dsets: a dictionary containing the existing datasets
|
|
252
|
+
"""
|