-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcloud.py
297 lines (247 loc) · 10.9 KB
/
cloud.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
""" Module of processes to interpret cloud parameters from Brewster in testkit"""
from __future__ import print_function
import numpy as np
import scipy as sp
from scipy import interpolate
__author__ = "Ben Burningham"
__copyright__ = "Copyright 2016 - Ben Burningham"
__credits__ = ["Ben Burningham","The EMCEE DOCS"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Ben Burningham"
__email__ = "[email protected]"
__status__ = "Development"
# now need to translate cloudparams in to cloud profile even
# if do_clouds is zero..
# 5 entries for cloudparams for simple slab model are:
# 0) dtau at 1um
# 1) top layer id (or pressure)
# 2) base ID (these are both in 64 layers)
# 3) rg
# 4) rsig
# in the case of a simple mixto cloud (i.e. cloudnum = 99 or 89) we have:
# 0) dtau (at 1um for non-grey)
# 1) top layer ID
# 2) bottom later ID
# 3) rg = albedo
# 4) rsig = power for tau power law
def atlas(do_clouds,cloudnum,cloudtype,cloudparams,press):
# Cloud types
# 1: slab cloud
# 2: deep thick cloud , we only see the top
# 3: slab with fixed thickness log dP = 0.005 (~1% height)
# 4: deep thick cloud with fixed height log dP = 0.005
# In both cases the cloud properties are density, rg, rsig for real clouds
# and dtau, w0, and power law for cloudnum = 99 or 89
nlayers = press.size
npatch = do_clouds.size
ncloud = 1
if (cloudparams.size > 5):
ncloud = cloudparams.shape[2]
cloudrad = np.zeros((npatch,nlayers,ncloud),dtype='d')
cloudsig = np.zeros_like(cloudrad)
cloudprof = np.zeros_like(cloudrad)
for i in range(0, npatch):
if (do_clouds[i] != 0):
for j in range(0,ncloud):
if (cloudtype[i,j] == 1 or cloudtype[i,j] == 3):
# 5 entries for cloudparams are:
# 0) total tau for cloud at 1 micron
# 1) log top pressure
# 2) pressure thickness in dex
# 3) rg
# 4) rsig
tau = cloudparams[0,i,j]
p1 = 10.**cloudparams[1,i,j]
if (cloudtype[i,j] == 1):
dP = cloudparams[2,i,j]
else:
dP = 0.005
p2 = p1 * 10.**dP
rad = cloudparams[3,i,j]
sig = cloudparams[4,i,j]
pdiff = np.empty(nlayers,dtype='f')
pdiff = abs(np.log(press) - np.log(p1))
l1 = np.argmin(pdiff)
# Whichever layer-mean-P the cloud top is closest
# to is the +1 layer of the cloud
# same for the -1 layer of the base
pdiff = abs(np.log(press) - np.log(p2))
l2 = np.argmin(pdiff)
# This is a slab cloud
# dtau/dP propto P
if (l1 == l2):
cloudprof[i,l1,j] = tau
else:
const = tau / (p2**2 - p1**2)
# partial top fill
pl1, pl2 = atlev(l1,press)
cloudprof[i,l1,j] = const * (pl2**2 - p1**2)
# partial bottom fill
pl1, pl2 = atlev(l2,press)
cloudprof[i,l2,j] = const * (p2**2 - pl1**2)
for k in range (l1+1,l2):
l1,l2 = atlev(k,press)
cloudprof[i,k,j] = const * (l2**2 - l1**2)
# We're sampling particle radius in log space
if (cloudnum[i,j] < 50.):
cloudrad[i,:,j] = 10.**rad
else:
cloudrad[i,:,j] = rad
cloudsig[i,:,j] = sig
if (cloudtype[i,j] == 2 or cloudtype[i,j] == 4):
# 5 entries for cloudparams are:
# 0) empty
# 1) top pressure
# 2) scale height (in dex)
# 3) rg
# 4) rsig
p0 = 10.**cloudparams[1,i,j]
if (cloudtype[i,j] == 2):
dP = cloudparams[2,i,j]
else:
dP = 0.005
scale = ((p0 * 10.**dP) - p0) / 10.**dP
rad = cloudparams[3,i,j]
sig = cloudparams[4,i,j]
pdiff = np.empty(nlayers,dtype='f')
# In cloud 99/89 case rsig is power law for tau~lambda^alpha
# Here P0 is the pressure where tau= 1 for the cloud
# so dtau / dP = const * exp((P-P0) / scale)
# See notes for derivation of constant and integral
const = 1. / (1 - np.exp(-p0 / scale))
for k in range (0,nlayers):
pl1, pl2 = atlev(k,press)
# now get dtau for each layer, where tau = 1 at P0
term1 = (pl2 - p0) / scale
term2 = (pl1 - p0) / scale
if (term1 > 10 or term2 > 10):
cloudprof[i,k,j] = 100.00
else:
cloudprof[i,k,j] = const * (np.exp(term1) -
np.exp(term2))
# We're sampling particle radius in log space
if (cloudnum[i,j] < 50.):
cloudrad[i,:,j] = 10.**rad
else:
cloudrad[i,:,j] = rad
cloudsig[i,:,j] = sig
if (cloudtype[i,j] == 0):
cloudprof[i,:,j] = 0.0
cloudrad[i,:,j] = 0.0
cloudsig[i,:,j] = 0.0
if (cloudtype[i,j] > 4):
print ("cloud layout not recognised. stopping")
return cloudprof,cloudrad,cloudsig
def atlev(l0,press):
nlayers = press.size
if (l0 <= nlayers-2):
pl1 = np.exp(((1.5)*np.log(press[l0])) - ((0.5)*np.log(press[l0+1])))
pl2 = np.exp((0.5)*(np.log(press[l0] * press[l0+1])))
else:
pl1 = np.exp((0.5 * np.log(press[l0-1] * press[l0])))
pl2 = press[l0]**2 / pl1
return pl1, pl2
def unpack_default(theta,pc,cloudtype,cloudnum,do_clouds):
nc =0
if (cloudtype.size > cloudtype.shape[1]):
nclouds = cloudtype.shape[1]
else:
nclouds = cloudtype.size
npatches = do_clouds.size
cloudparams = np.ones([5,npatches,nclouds],dtype='d')
cloudparams[0,:,:] = 0.
cloudparams[1,:,:] = 0.0
cloudparams[2,:,:] = 0.1
cloudparams[3,:,:] = 0.0
cloudparams[4,:] = 0.5
for i in range (0,npatches):
if (do_clouds[i] != 0):
for j in range (0, nclouds):
if ((cloudtype[i,j] == 2) and (cloudnum[i,j] == 99)):
cloudparams[1:4,i,j] = theta[pc+nc:pc+3+nc]
cloudparams[4,i,j] = 0.0
nc = nc + 3
elif ((cloudtype[i,j] == 1) and (cloudnum[i,j] == 99)):
cloudparams[0:4,i,j] = theta[pc+nc:pc+4+nc]
cloudparams[4,i,j] = 0.0
nc = nc + 4
elif ((cloudtype[i,j] == 2) and (cloudnum[i,j] < 90)):
cloudparams[1:5,i,j] = theta[pc+nc:pc+4+nc]
nc = nc +4
elif ((cloudtype[i,j] == 3) and (cloudnum[i,j] == 99)):
cloudparams[0:2,i,j] = theta[pc+nc:pc+nc+2]
cloudparams[3,i,j] = theta[pc+nc+2]
nc = nc +3
elif ((cloudtype[i,j] == 3) and (cloudnum[i,j] < 90)):
cloudparams[0:2,i,j] = theta[pc+nc:pc+nc+2]
cloudparams[3:5,i,j] = theta[pc+nc+2:pc+nc+4]
nc = nc + 4
elif ((cloudtype[i,j] == 4) and (cloudnum[i,j] == 99)):
cloudparams[1,i,j] = theta[pc+nc]
cloudparams[3,i,j] = theta[pc+nc+1]
nc = nc +2
elif ((cloudtype[i,j] == 4) and (cloudnum[i,j] < 90)):
cloudparams[1,i,j] = theta[pc+nc]
cloudparams[3:5,i,j] = theta[pc+nc+1:pc+nc+3]
nc = nc +3
elif (cloudtype[i,j] == 0):
cloudparams[:,i,j] = 0.0
else:
cloudparams[:,i,j] = theta[pc+nc:pc+5+nc]
nc = nc + 5
return(cloudparams,nc)
def unpack_patchy(theta,pc,cloudtype,cloudnum,do_clouds):
# This unpacks a patchy cloud
# This is achieved by equated patch 2 to patch 1,
# but all but some clouds in patch 2 are set to cloudtype = 0
nc =0
if (cloudtype.size > cloudtype.shape[1]):
nclouds = cloudtype.shape[1]
else:
nclouds = cloudtype.size
npatches = 2
cloudparams = np.ones([5,npatches,nclouds],dtype='d')
cloudparams[0,:,:] = 0.
cloudparams[1,:,:] = 0.0
cloudparams[2,:,:] = 0.1
cloudparams[3,:,:] = 0.0
cloudparams[4,:] = 0.0
# First patch
if (do_clouds[0] != 0):
for j in range (0, nclouds):
if ((cloudtype[0,j] == 2) and (cloudnum[0,j] == 99)):
cloudparams[1:4,0,j] = theta[pc+nc:pc+3+nc]
cloudparams[4,0,j] = 0.0
nc = nc + 3
elif ((cloudtype[0,j] == 1) and (cloudnum[0,j] == 99)):
cloudparams[0:4,0,j] = theta[pc+nc:pc+4+nc]
cloudparams[4,0,j] = 0.0
nc = nc + 4
elif ((cloudtype[0,j] == 2) and (cloudnum[0,j] < 90)):
cloudparams[1:5,0,j] = theta[pc+nc:pc+4+nc]
nc = nc +4
elif ((cloudtype[0,j] == 3) and (cloudnum[0,j] == 99)):
cloudparams[0:2,0,j] = theta[pc+nc:pc+nc+2]
cloudparams[3,0,j] = theta[pc+nc+2]
nc = nc +3
elif ((cloudtype[0,j] == 3) and (cloudnum[0,j] < 90)):
cloudparams[0:2,0,j] = theta[pc+nc:pc+nc+2]
cloudparams[3:5,0,j] = theta[pc+nc+2:pc+nc+4]
nc = nc + 4
elif ((cloudtype[0,j] == 4) and (cloudnum[0,j] == 99)):
cloudparams[1,0,j] = theta[pc+nc]
cloudparams[3,0,j] = theta[pc+nc+1]
nc = nc +2
elif ((cloudtype[0,j] == 4) and (cloudnum[0,j] < 90)):
cloudparams[1,0,j] = theta[pc+nc]
cloudparams[3:5,0,j] = theta[pc+nc+1:pc+nc+3]
nc = nc +3
else:
cloudparams[:,0,j] = theta[pc+nc:pc+5+nc]
nc = nc + 5
# 2nd patch
if (do_clouds[1] != 0):
cloudparams[:,1,:] = cloudparams[:,0,:]
return(cloudparams,nc)