cleopy 0.57.4__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.
- cleopy/__init__.py +22 -0
- cleopy/cxx2py.py +136 -0
- cleopy/editconfigfile.py +61 -0
- cleopy/gbxboundariesbinary_src/__init__.py +16 -0
- cleopy/gbxboundariesbinary_src/create_gbxboundaries.py +197 -0
- cleopy/gbxboundariesbinary_src/read_gbxboundaries.py +350 -0
- cleopy/geninitconds.py +144 -0
- cleopy/initsuperdropsbinary_src/__init__.py +15 -0
- cleopy/initsuperdropsbinary_src/attrsgen.py +206 -0
- cleopy/initsuperdropsbinary_src/crdgens.py +101 -0
- cleopy/initsuperdropsbinary_src/create_initsuperdrops.py +360 -0
- cleopy/initsuperdropsbinary_src/dryrgens.py +31 -0
- cleopy/initsuperdropsbinary_src/probdists.py +239 -0
- cleopy/initsuperdropsbinary_src/read_initsuperdrops.py +465 -0
- cleopy/initsuperdropsbinary_src/rgens.py +81 -0
- cleopy/readbinary.py +120 -0
- cleopy/readconfigfile.py +61 -0
- cleopy/sdmout_src/__init__.py +16 -0
- cleopy/sdmout_src/massmoms.py +83 -0
- cleopy/sdmout_src/pygbxsdat.py +59 -0
- cleopy/sdmout_src/pysetuptxt.py +90 -0
- cleopy/sdmout_src/pyzarr.py +158 -0
- cleopy/sdmout_src/sdtracing.py +141 -0
- cleopy/sdmout_src/superdrops.py +428 -0
- cleopy/sdmout_src/supersdata.py +228 -0
- cleopy/sdmout_src/thermodata.py +146 -0
- cleopy/sdmout_src/thermoeqns.py +129 -0
- cleopy/sdmout_src/timedata.py +54 -0
- cleopy/thermobinary_src/__init__.py +16 -0
- cleopy/thermobinary_src/create_thermodynamics.py +256 -0
- cleopy/thermobinary_src/read_thermodynamics.py +498 -0
- cleopy/thermobinary_src/thermodyngen.py +56 -0
- cleopy/thermobinary_src/thermogen.py +466 -0
- cleopy/thermobinary_src/windsgen.py +228 -0
- cleopy/writebinary.py +218 -0
- cleopy-0.57.4.dist-info/METADATA +47 -0
- cleopy-0.57.4.dist-info/RECORD +39 -0
- cleopy-0.57.4.dist-info/WHEEL +4 -0
- cleopy-0.57.4.dist-info/licenses/LICENSE.md +43 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: rgens.py
|
|
7
|
+
Project: initsuperdropsbinary_src
|
|
8
|
+
Created Date: Friday 13th October 2023
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
various ways of generatring radii of superdroplets for their initial conditions
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class MonoAttrGen:
|
|
23
|
+
"""method to generate superdroplets with an
|
|
24
|
+
attribute all equal to attr0"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, attr0):
|
|
27
|
+
self.attr0 = attr0
|
|
28
|
+
|
|
29
|
+
def __call__(self, nsupers):
|
|
30
|
+
"""Returns attribute for nsupers all
|
|
31
|
+
with the value of attr0"""
|
|
32
|
+
|
|
33
|
+
if type(nsupers) == np.ndarray:
|
|
34
|
+
nsupers = np.shape(nsupers)[0]
|
|
35
|
+
|
|
36
|
+
attrs = np.full(nsupers, self.attr0)
|
|
37
|
+
|
|
38
|
+
return attrs
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class SampleLog10RadiiGen:
|
|
42
|
+
"""method to generate superdroplet radii by randomly
|
|
43
|
+
sampling from bins that are linearly spaced in log10(r)
|
|
44
|
+
between rspan[0] and rspan[1]"""
|
|
45
|
+
|
|
46
|
+
def __init__(self, rspan):
|
|
47
|
+
self.rspan = rspan
|
|
48
|
+
|
|
49
|
+
def __call__(self, nsupers):
|
|
50
|
+
"""Returns radii for nsupers sampled from rspan [m]"""
|
|
51
|
+
|
|
52
|
+
return self.generate_radiisample(nsupers) # units [m]
|
|
53
|
+
|
|
54
|
+
def generate_radiisample(self, nbins):
|
|
55
|
+
"""Divide rspan [m] into evenly spaced bins in log10(r).
|
|
56
|
+
If edges=True, return values of radii at edges of bins.
|
|
57
|
+
Else sample each bin randomly to obtain the radius
|
|
58
|
+
of 'nsupers' no. of superdroplets"""
|
|
59
|
+
|
|
60
|
+
if nbins:
|
|
61
|
+
log10redgs = np.linspace(
|
|
62
|
+
np.log10(self.rspan[0]), np.log10(self.rspan[1]), nbins + 1
|
|
63
|
+
) # log10(r) bin edges
|
|
64
|
+
|
|
65
|
+
radii = self.randomlysample_log10rbins(nbins, log10redgs)
|
|
66
|
+
return radii # [m]
|
|
67
|
+
else:
|
|
68
|
+
return np.array([])
|
|
69
|
+
|
|
70
|
+
def randomlysample_log10rbins(self, nbins, log10redgs):
|
|
71
|
+
"""given the bin edges, randomly sample each bin of
|
|
72
|
+
log10(radius /m) and return the resultant radii [m]"""
|
|
73
|
+
|
|
74
|
+
log10r_binwidth = (log10redgs[-1] - log10redgs[0]) / nbins
|
|
75
|
+
|
|
76
|
+
randlog10deltar = np.random.uniform(low=0.0, high=log10r_binwidth, size=nbins)
|
|
77
|
+
randlog10r = log10redgs[:-1] + randlog10deltar
|
|
78
|
+
|
|
79
|
+
radii = 10 ** (randlog10r)
|
|
80
|
+
|
|
81
|
+
return radii # [m]
|
cleopy/readbinary.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2025 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: readbinary.py
|
|
7
|
+
Project: cleopy
|
|
8
|
+
Created Date: Tuesday 26th August 2025
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
import struct
|
|
21
|
+
|
|
22
|
+
from .writebinary import DataTypeCodes, MetadataPerVariable
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def readbinary(filename, isprint=True):
|
|
26
|
+
"""return list of vectors containing dimenionsless
|
|
27
|
+
data read from binary file"""
|
|
28
|
+
|
|
29
|
+
print("Reading binary file:\n " + str(filename))
|
|
30
|
+
|
|
31
|
+
nvars, metabytes, metapervar = read_metadata(filename, isprint=isprint)
|
|
32
|
+
|
|
33
|
+
data, ndata_pervar = read_data(filename, nvars, metabytes, metapervar)
|
|
34
|
+
|
|
35
|
+
return data, ndata_pervar
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def read_metadata(filename, isprint=True):
|
|
39
|
+
"""read global metadata and return the variable specific metadata
|
|
40
|
+
in a 2D array with each row being the metadata for a different variable"""
|
|
41
|
+
|
|
42
|
+
dtc = DataTypeCodes()
|
|
43
|
+
|
|
44
|
+
metabytes, gblmeta_bytes, nvars, mpv_bytes = first4uints(filename, dtc)
|
|
45
|
+
|
|
46
|
+
gblmetadata, metapervar = get_metadatapervar(
|
|
47
|
+
filename, dtc, gblmeta_bytes, nvars, metabytes
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
if isprint:
|
|
51
|
+
print("Metadata: \n", "'" + gblmetadata + "'")
|
|
52
|
+
|
|
53
|
+
return metapervar.shape[0], metabytes, metapervar
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def first4uints(filename, dtc):
|
|
57
|
+
with open(filename, mode="rb") as binaryfile:
|
|
58
|
+
bytes4uints = dtc.format_size("IIII")
|
|
59
|
+
uints = struct.unpack("<IIII", binaryfile.read(bytes4uints))
|
|
60
|
+
|
|
61
|
+
metabytes, gblmeta_bytes, nvars, metapervar_bytes = uints
|
|
62
|
+
|
|
63
|
+
return metabytes, gblmeta_bytes, nvars, metapervar_bytes
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def get_metadatapervar(filename, dtc, gblmeta_bytes, nvars, metabytes):
|
|
67
|
+
mpv_format = MetadataPerVariable().mpv_format
|
|
68
|
+
|
|
69
|
+
nchars = int(gblmeta_bytes / dtc.dtype2bytesize(type("c")))
|
|
70
|
+
format = "<" + "c" * nchars + mpv_format * nvars
|
|
71
|
+
|
|
72
|
+
skipbytes = dtc.format_size("IIII")
|
|
73
|
+
nbytes2read = metabytes - skipbytes
|
|
74
|
+
with open(filename, mode="rb") as binaryfile:
|
|
75
|
+
binaryfile.seek(skipbytes)
|
|
76
|
+
mdata = struct.unpack(format, binaryfile.read(nbytes2read))
|
|
77
|
+
metastr = mdata[:nchars]
|
|
78
|
+
mpervar = mdata[nchars:]
|
|
79
|
+
|
|
80
|
+
gblmetadata = "".join([str(i.decode()) for i in metastr])
|
|
81
|
+
metapervar = np.reshape(mpervar, (nvars, int(len(mpervar) / nvars)))
|
|
82
|
+
|
|
83
|
+
return gblmetadata, metapervar
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def read_data(filename, nvars, metabytes, metapervar):
|
|
87
|
+
dataformat, ndata_pervar = get_dataformat(nvars, metapervar)
|
|
88
|
+
with open(filename, mode="rb") as binaryfile:
|
|
89
|
+
binaryfile.seek(metabytes)
|
|
90
|
+
data = struct.unpack(dataformat, binaryfile.read())
|
|
91
|
+
|
|
92
|
+
if np.sum(ndata_pervar) != len(data):
|
|
93
|
+
err = (
|
|
94
|
+
str(len(data))
|
|
95
|
+
+ " datapoints read in total is incorrect given"
|
|
96
|
+
+ " metadata states no. datapoints per variable is "
|
|
97
|
+
+ str(ndata_pervar)
|
|
98
|
+
)
|
|
99
|
+
raise ValueError(err)
|
|
100
|
+
|
|
101
|
+
return data, ndata_pervar
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_dataformat(nvars, metapervar):
|
|
105
|
+
dtypes = metapervar[:, 3] # struct code for datatype of each variable
|
|
106
|
+
ndata_pervar = np.asarray(
|
|
107
|
+
metapervar[:, 2], dtype=np.uintc
|
|
108
|
+
) # no. datapoints of each variable
|
|
109
|
+
|
|
110
|
+
dataformat = "<"
|
|
111
|
+
for n in range(nvars):
|
|
112
|
+
format = [dtypes[n]] * ndata_pervar[
|
|
113
|
+
n
|
|
114
|
+
] # list of binary encoded characters for struct format
|
|
115
|
+
format = "".join(
|
|
116
|
+
[str(f.decode()) for f in format]
|
|
117
|
+
) # convert to one continous decoded string
|
|
118
|
+
dataformat += format
|
|
119
|
+
|
|
120
|
+
return dataformat, ndata_pervar
|
cleopy/readconfigfile.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: readconfigfile.py
|
|
7
|
+
Project: cleopy
|
|
8
|
+
Created Date: Wednesday 17th April 2024
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from ruamel.yaml import YAML
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def extract_floats(node, floats, notfloats):
|
|
22
|
+
"""Function to recursively searches YAML node
|
|
23
|
+
and add any value that is convertible to a float to a dictionary
|
|
24
|
+
with its key"""
|
|
25
|
+
|
|
26
|
+
if isinstance(node, dict):
|
|
27
|
+
for key, value in node.items():
|
|
28
|
+
if isinstance(value, (dict, list)):
|
|
29
|
+
extract_floats(value, floats, notfloats)
|
|
30
|
+
else:
|
|
31
|
+
try:
|
|
32
|
+
floats[key] = float(value)
|
|
33
|
+
except ValueError:
|
|
34
|
+
notfloats[key] = value
|
|
35
|
+
elif isinstance(node, list):
|
|
36
|
+
for item in node:
|
|
37
|
+
extract_floats(item, floats, notfloats)
|
|
38
|
+
|
|
39
|
+
return floats, notfloats
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def read_configparams_into_floats(filename):
|
|
43
|
+
"""returns dictionary of {key, values} pairs from
|
|
44
|
+
keys from a config yaml file which can be assigned float values.
|
|
45
|
+
Also obtains dictionary of notfloats for values that couldn't be converted
|
|
46
|
+
and converts nspacedims to integer if found in floats."""
|
|
47
|
+
|
|
48
|
+
yaml = YAML(typ="safe")
|
|
49
|
+
with open(filename, "r") as file:
|
|
50
|
+
config = yaml.load(file)
|
|
51
|
+
|
|
52
|
+
floats, notfloats = {}, {}
|
|
53
|
+
floats, notfloats = extract_floats(config, floats, notfloats)
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
floats["nspacedims"] = int(floats["nspacedims"]) # no spatial coords to SDs
|
|
57
|
+
except KeyError as e:
|
|
58
|
+
print("Warning, ignoring error", e)
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
return floats
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""
|
|
2
|
+
----- CLEO -----
|
|
3
|
+
File: __init__.py
|
|
4
|
+
Project: cleoout_src
|
|
5
|
+
Created Date: Tuesday 24th October 2023
|
|
6
|
+
Author: Clara Bayley (CB)
|
|
7
|
+
Additional Contributors:
|
|
8
|
+
-----
|
|
9
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
10
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
11
|
+
-----
|
|
12
|
+
Copyright (c) 2023 MPI-M, Clara Bayley
|
|
13
|
+
-----
|
|
14
|
+
File Description:
|
|
15
|
+
init for submodule
|
|
16
|
+
"""
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: massmoms.py
|
|
7
|
+
Project: sdmout_src
|
|
8
|
+
Created Date: Tuesday 24th October 2023
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
python class to handle mass moments data from SDM zarr store in cartesian domain
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
import xarray as xr
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class MassMoms:
|
|
25
|
+
def __init__(self, dataset, ntime, ndims, lab=""):
|
|
26
|
+
ds = self.tryopen_dataset(dataset)
|
|
27
|
+
reshape = [ntime] + list(ndims)
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
self.nsupers = self.var4d_fromzarr(
|
|
31
|
+
ds, reshape, "n" + lab + "supers"
|
|
32
|
+
) # number of superdroplets in gbxs over time
|
|
33
|
+
except KeyError:
|
|
34
|
+
self.nsupers = np.array([])
|
|
35
|
+
|
|
36
|
+
self.mom0 = self.var4d_fromzarr(
|
|
37
|
+
ds, reshape, "massmom0" + lab
|
|
38
|
+
) # number of droplets in gbxs over time
|
|
39
|
+
self.mom1 = self.var4d_fromzarr(
|
|
40
|
+
ds, reshape, "massmom1" + lab
|
|
41
|
+
) # total mass of droplets in gbxs over time
|
|
42
|
+
self.mom2 = self.var4d_fromzarr(
|
|
43
|
+
ds, reshape, "massmom2" + lab
|
|
44
|
+
) # 2nd mass moment of droplets (~reflectivity)
|
|
45
|
+
self.effmass = self.effective_mass()
|
|
46
|
+
|
|
47
|
+
self.mom1_units = ds["massmom1"].units # probably grams
|
|
48
|
+
self.mom2_units = ds["massmom2"].units # probably grams^2
|
|
49
|
+
self.effmass_units = (
|
|
50
|
+
ds["massmom2"].units + "/" + ds["massmom1"].units
|
|
51
|
+
) # probably grams
|
|
52
|
+
|
|
53
|
+
def tryopen_dataset(self, dataset):
|
|
54
|
+
if isinstance(dataset, str) or isinstance(dataset, Path):
|
|
55
|
+
print("mass moments dataset: ", dataset)
|
|
56
|
+
return xr.open_dataset(dataset, engine="zarr", consolidated=False)
|
|
57
|
+
else:
|
|
58
|
+
return dataset
|
|
59
|
+
|
|
60
|
+
def var4d_fromzarr(self, ds, reshape, key):
|
|
61
|
+
"""' returns 4D variable with dims
|
|
62
|
+
[time, y, x, z] from zarr dataset "ds" """
|
|
63
|
+
|
|
64
|
+
return np.reshape(ds[key].values, reshape)
|
|
65
|
+
|
|
66
|
+
def effective_mass(self):
|
|
67
|
+
"""effective mass of droplets"""
|
|
68
|
+
return self.mom2 / self.mom1
|
|
69
|
+
|
|
70
|
+
def __getitem__(self, key):
|
|
71
|
+
if key == "nsupers":
|
|
72
|
+
return self.nsupers
|
|
73
|
+
elif key == "mom0":
|
|
74
|
+
return self.mom0
|
|
75
|
+
elif key == "mom1":
|
|
76
|
+
return self.mom1
|
|
77
|
+
elif key == "mom2":
|
|
78
|
+
return self.mom2
|
|
79
|
+
elif key == "effmass":
|
|
80
|
+
return self.effmass
|
|
81
|
+
else:
|
|
82
|
+
err = "no known return provided for " + key + " key"
|
|
83
|
+
raise ValueError(err)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""
|
|
2
|
+
----- CLEO -----
|
|
3
|
+
File: pygbxsdat.py
|
|
4
|
+
Project: sdmout_src
|
|
5
|
+
Created Date: Tuesday 24th October 2023
|
|
6
|
+
Author: Clara Bayley (CB)
|
|
7
|
+
Additional Contributors:
|
|
8
|
+
-----
|
|
9
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
10
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
11
|
+
-----
|
|
12
|
+
Copyright (c) 2023 MPI-M, Clara Bayley
|
|
13
|
+
-----
|
|
14
|
+
File Description:
|
|
15
|
+
for reading data from gridbox (gbx)
|
|
16
|
+
boudnaries binary inputs files
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
from ..gbxboundariesbinary_src import read_gbxboundaries as rgrid
|
|
21
|
+
from ..cxx2py import print_dict_statement
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_gridboxes(grid_filename, COORD0, isprint=True):
|
|
25
|
+
gbxbounds, ndims = rgrid.read_dimless_gbxboundaries_binary(
|
|
26
|
+
grid_filename, COORD0=COORD0, return_ndims=True, isprint=False
|
|
27
|
+
)
|
|
28
|
+
zhalf, xhalf, yhalf = rgrid.halfcoords_from_gbxbounds(gbxbounds, isprint=False)
|
|
29
|
+
domainvol, gbxvols, ngrid = rgrid.domaininfo(gbxbounds, isprint=False)
|
|
30
|
+
|
|
31
|
+
gbxs = {
|
|
32
|
+
"ngrid": ngrid, # number of gridboxes
|
|
33
|
+
"ndims": np.flip(ndims), # dimensions (no. gridboxes in [y,x,z] direction)
|
|
34
|
+
"domainvol": domainvol,
|
|
35
|
+
"domainarea": domainvol
|
|
36
|
+
/ (np.amax(zhalf) - np.amin(zhalf)), # x-y plane horizontal are
|
|
37
|
+
"gbxvols": gbxvols, # list of vols of each gbx
|
|
38
|
+
"zhalf": zhalf, # half cell coords (boundaries)
|
|
39
|
+
"zfull": rgrid.fullcell(zhalf), # full cell coords (centres)
|
|
40
|
+
"xhalf": xhalf, # half cell coords (boundaries)
|
|
41
|
+
"xfull": rgrid.fullcell(xhalf), # full cell coords (centres)
|
|
42
|
+
"yhalf": yhalf, # half cell coords (boundaries)
|
|
43
|
+
"yfull": rgrid.fullcell(yhalf), # full cell coords (centres)
|
|
44
|
+
}
|
|
45
|
+
gbxs["gbxvols"] = np.reshape(gbxs["gbxvols"], gbxs["ndims"])
|
|
46
|
+
|
|
47
|
+
xxh, zzh = np.meshgrid(
|
|
48
|
+
gbxs["xhalf"], gbxs["zhalf"], indexing="ij"
|
|
49
|
+
) # dims [xdims, zdims] [m]
|
|
50
|
+
xxf, zzf = np.meshgrid(
|
|
51
|
+
gbxs["xfull"], gbxs["zfull"], indexing="ij"
|
|
52
|
+
) # dims [xdims, zdims] [m]
|
|
53
|
+
gbxs["xxh"], gbxs["zzh"] = xxh, zzh
|
|
54
|
+
gbxs["xxf"], gbxs["zzf"] = xxf, zzf
|
|
55
|
+
|
|
56
|
+
if isprint:
|
|
57
|
+
print_dict_statement(grid_filename, "gbxs", gbxs)
|
|
58
|
+
|
|
59
|
+
return gbxs
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: pysetuptxt.py
|
|
7
|
+
Project: sdmout_src
|
|
8
|
+
Created Date: Tuesday 24th October 2023
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
functions for reading setup.txt file
|
|
17
|
+
output alongside zarr storage
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from .. import cxx2py
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_consts(setuptxt, isprint=True):
|
|
24
|
+
"""returns dictionary of constants
|
|
25
|
+
read from from setup.txt file"""
|
|
26
|
+
|
|
27
|
+
return consts_dict(setuptxt, isprint=isprint)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_config(setuptxt, nattrs=3, isprint=True):
|
|
31
|
+
"""returns dictionary of configuration parameters
|
|
32
|
+
read from from setup.txt file"""
|
|
33
|
+
|
|
34
|
+
return config_dict(setuptxt, nattrs=nattrs, isprint=isprint)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def consts_dict(setuptxt, isprint):
|
|
38
|
+
consts = cxx2py.read_cxxconsts_into_floats(setuptxt)
|
|
39
|
+
consts.update(cxx2py.derive_more_floats(consts))
|
|
40
|
+
|
|
41
|
+
if isprint:
|
|
42
|
+
cxx2py.print_dict_statement(setuptxt, "consts", consts)
|
|
43
|
+
|
|
44
|
+
return consts
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def config_dict(setuptxt, nattrs, isprint):
|
|
48
|
+
config = read_configparams_fromsetuptxt_into_floats(setuptxt)
|
|
49
|
+
config["numSDattrs"] = config["nspacedims"] + nattrs
|
|
50
|
+
config["ntime"] = round(config["T_END"] / config["OBSTSTEP"]) + 1
|
|
51
|
+
|
|
52
|
+
if isprint:
|
|
53
|
+
cxx2py.print_dict_statement(setuptxt, "config", config)
|
|
54
|
+
|
|
55
|
+
return config
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def read_configparams_fromsetuptxt_into_floats(filename):
|
|
59
|
+
"""returns dictionary of value: float from
|
|
60
|
+
values assigned in a config .txt file.
|
|
61
|
+
Also returns dictionary of notfloats
|
|
62
|
+
for values that couldn't be converted."""
|
|
63
|
+
|
|
64
|
+
floats = {}
|
|
65
|
+
notfloats = {}
|
|
66
|
+
with open(filename) as file:
|
|
67
|
+
rlines = []
|
|
68
|
+
filelines = file.readlines()
|
|
69
|
+
for line in filelines:
|
|
70
|
+
if (line[0] != "#") and (line[0] != "/") and (":" in line):
|
|
71
|
+
goodline = cxx2py.remove_excess_line(line)
|
|
72
|
+
rlines.append(goodline)
|
|
73
|
+
|
|
74
|
+
for line in rlines:
|
|
75
|
+
ind = line.find(":")
|
|
76
|
+
name = line[:ind]
|
|
77
|
+
value = line[ind + 1 :]
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
floats[name] = float(value)
|
|
81
|
+
except ValueError:
|
|
82
|
+
notfloats[name] = value
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
floats["nspacedims"] = int(floats["nspacedims"]) # no spatial coords to SDs
|
|
86
|
+
except KeyError as e:
|
|
87
|
+
print("Warning, ignoring error", e)
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
return floats
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2025 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: pyzarr.py
|
|
7
|
+
Project: sdmout_src
|
|
8
|
+
Created Date: Tuesday 24th October 2023
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
functions to return zarr data in useful formats for plotting e.g. for handling ragged arrays
|
|
17
|
+
of superdroplet attributes
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
import xarray as xr
|
|
23
|
+
import awkward as ak
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
from . import thermodata
|
|
27
|
+
from . import superdrops
|
|
28
|
+
from . import massmoms
|
|
29
|
+
from . import timedata
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def get_rawdataset(dataset):
|
|
33
|
+
return xr.open_dataset(dataset, engine="zarr", consolidated=False)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def get_rawdata4key(dataset, key):
|
|
37
|
+
return get_rawdataset(dataset)[key]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_rawdata4raggedkey(dataset, key):
|
|
41
|
+
ds = get_rawdataset(dataset)
|
|
42
|
+
return ak.unflatten(ds[key].values, ds["raggedcount"].values)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def raggedvar_fromzarr(ds, raggedcount, var):
|
|
46
|
+
"""returns ragged ak.Array dims [time, ragged]
|
|
47
|
+
for a variable "var" in zarr ds"""
|
|
48
|
+
if isinstance(ds, str) or isinstance(ds, Path):
|
|
49
|
+
ds = get_rawdataset(ds)
|
|
50
|
+
|
|
51
|
+
return ak.unflatten(ds[var].values, raggedcount)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def var4d_fromzarr(ds, ntime, ndims, key):
|
|
55
|
+
"""' returns 4D variable with dims
|
|
56
|
+
[time, y, x, z] from zarr dataset "ds" """
|
|
57
|
+
if isinstance(ds, str) or isinstance(ds, Path):
|
|
58
|
+
ds = get_rawdataset(ds)
|
|
59
|
+
|
|
60
|
+
reshape = [ntime] + list(ndims)
|
|
61
|
+
return np.reshape(ds[key].values, reshape)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def var3d_fromzarr(ds, ndims, key):
|
|
65
|
+
"""' returns 3D variable with dims
|
|
66
|
+
[y, x, z] from zarr dataset "ds" """
|
|
67
|
+
if isinstance(ds, str) or isinstance(ds, Path):
|
|
68
|
+
ds = get_rawdataset(ds)
|
|
69
|
+
|
|
70
|
+
return np.reshape(ds[key].values, ndims)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def get_thermodata(dataset, ntime, ndims, consts, getwinds=False):
|
|
74
|
+
"""returns a thermodynamic data in a dictionary. The value under
|
|
75
|
+
each key is the thermodynamics data in a 2D array
|
|
76
|
+
with dimensions [time, gridbox]. E.g. thermo["qvap"][:,0] gives the
|
|
77
|
+
timeseries of qvap for the 0th gridbox. thermo["qvap][0] gives
|
|
78
|
+
the qvap of all gridboxes at the 0th output time"""
|
|
79
|
+
|
|
80
|
+
thermo = thermodata.Thermodata(dataset, ntime, ndims, consts)
|
|
81
|
+
if getwinds:
|
|
82
|
+
winds = thermodata.Winddata(dataset, ntime, ndims, consts)
|
|
83
|
+
return thermo, winds
|
|
84
|
+
else:
|
|
85
|
+
return thermo
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def get_supers(dataset, consts, is_print=False):
|
|
89
|
+
return superdrops.Superdrops(dataset, consts, is_print=is_print)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def get_time(dataset):
|
|
93
|
+
return timedata.Time(dataset)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def get_massmoms(dataset, ntime, ndims):
|
|
97
|
+
return massmoms.MassMoms(dataset, ntime, ndims)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_rainmassmoms(dataset, ntime, ndims):
|
|
101
|
+
return massmoms.MassMoms(dataset, ntime, ndims, lab="_raindrops")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_gbxindex(dataset, ndims):
|
|
105
|
+
return var3d_fromzarr(dataset, ndims, "gbxindex")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def get_totnsupers(dataset):
|
|
109
|
+
if isinstance(dataset, str) or isinstance(dataset, Path):
|
|
110
|
+
dataset = get_rawdataset(dataset)
|
|
111
|
+
try:
|
|
112
|
+
return dataset["totnsupers"].values
|
|
113
|
+
except KeyError:
|
|
114
|
+
return dataset["raggedcount"].values
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def get_nsupers(dataset, ntime, ndims):
|
|
118
|
+
return var4d_fromzarr(dataset, ntime, ndims, "nsupers")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def surfaceprecip_estimate(dataset, gbxs):
|
|
122
|
+
"""use last radius of SDs before they leave the domain to
|
|
123
|
+
estimate the volume of precipitation at each timestep.
|
|
124
|
+
Values should be approx. equal to sum over gbxs (multiplied
|
|
125
|
+
by area_gbx/area_domain) of logbook values for precip"""
|
|
126
|
+
|
|
127
|
+
ds = get_rawdataset(dataset)
|
|
128
|
+
|
|
129
|
+
sdId = ak.unflatten(ds["sdId"].values, ds["raggedcount"].values)
|
|
130
|
+
radius = ak.unflatten(ds["radius"].values, ds["raggedcount"].values)
|
|
131
|
+
xi = ak.unflatten(ds["xi"].values, ds["raggedcount"].values)
|
|
132
|
+
|
|
133
|
+
r3sum = []
|
|
134
|
+
for ti in range(ds.time.shape[0] - 1):
|
|
135
|
+
sd_ti, r_ti, xi_ti = sdId[ti], radius[ti], xi[ti]
|
|
136
|
+
sds_gone = set(sd_ti) - set(
|
|
137
|
+
sdId[ti + 1]
|
|
138
|
+
) # set of SD indexes that have left domain during timestep ti -> ti+1
|
|
139
|
+
isgone = np.where(
|
|
140
|
+
np.isin(sd_ti, list(sds_gone))
|
|
141
|
+
) # indexes in ragged arrays of SDs that leave during timestep ti -> ti+1
|
|
142
|
+
r3sum.append(
|
|
143
|
+
np.dot(r_ti[isgone] ** 3, xi_ti[isgone])
|
|
144
|
+
) # sum of (real) droplet radii^3 that left domain [microns^3]
|
|
145
|
+
precipvol = (
|
|
146
|
+
4 / 3 * np.pi * np.asarray(r3sum) / (1e18)
|
|
147
|
+
) # volume of water that left domain [m^3]
|
|
148
|
+
|
|
149
|
+
domainy = np.amax(gbxs["yhalf"]) - np.amin(gbxs["yhalf"]) # [m]
|
|
150
|
+
domainx = np.amax(gbxs["xhalf"]) - np.amin(gbxs["xhalf"]) # [m]
|
|
151
|
+
deltat = np.diff(ds["time"].values) / 60 / 60 # [hrs]
|
|
152
|
+
preciprate = precipvol * 1000 / (domainx * domainy) / deltat # [mm/hr]
|
|
153
|
+
|
|
154
|
+
precipaccum = np.cumsum(preciprate * deltat) # [mm]
|
|
155
|
+
preciprate = np.insert(preciprate, 0, 0) # at t=0, precip rate = 0
|
|
156
|
+
precipaccum = np.insert(precipaccum, 0, 0) # at t=0, accumulated precip = 0
|
|
157
|
+
|
|
158
|
+
return preciprate, precipaccum # [mm/hr] , [mm]
|