Repository navigation
Expand file tree
/
Copy pathclouds_mod.f90
More file actions
405 lines (312 loc) · 15.5 KB
/
Copy pathclouds_mod.f90
File metadata and controls
405 lines (312 loc) · 15.5 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
module clouds
contains
subroutine cloudatlas(column,sizdist)
use sizes
use common_arrays
use define_types
use phys_const
implicit none
type(a_layer), intent(inout):: column(nlayers)
integer :: icloud, imiewave, irad,ilayer,oldw1, oldw2, idum1, idum2,iwave
integer :: sizdist,loc1,loc1a,loc1b,loc1m
integer :: loc(1)
character(len=50) :: miefile
double precision ,allocatable, dimension(:,:,:) :: qscat,qext,cos_qscat
double precision, dimension(nmiewave):: miewavelen,miewaven, wdiff,ext,scat,cqs
double precision, allocatable, dimension(:,:) :: radius_in, radius
double precision, allocatable, dimension(:,:) :: dr, rup
double precision, allocatable, dimension(:,:,:) :: scat_cloud,ext_cloud
double precision, allocatable, dimension(:,:,:) :: cqs_cloud
double precision, allocatable, dimension(:,:):: opd_ext,opd_scat, cos_qs
double precision :: norm, rr, r2, rg, rsig, pw, pir2ndz, arg1, arg2,bot
double precision :: f1, f2, intfact,lintfact,vrat,rmin,sum_ndz_2, sum_ndz_1
double precision :: a, b, ndz, drr, arg3, argscat, argext, argcosqs
double precision :: logcon, qpir2, frac
real, allocatable,dimension(:) :: cld1arr
! this will take the clouds and in turn calculate their opacities the layers
! based on name, density, mean radius and width of log normal distribution
! or using Hansen distribution: effective radius, radius spread and density
! get the distribution from do_clouds
! 2 = log normal, 1 = hansen
allocate(qscat(nmiewave,nrad,nclouds),qext(nmiewave,nrad,nclouds))
allocate(cos_qscat(nmiewave,nrad,nclouds))
allocate(radius_in(nrad,nclouds), radius(nrad,nclouds))
allocate(dr(nrad,nclouds), rup(nrad,nclouds))
allocate(scat_cloud(nlayers,nmiewave,nclouds))
allocate(ext_cloud(nlayers,nmiewave,nclouds))
allocate(cqs_cloud(nlayers,nmiewave,nclouds))
allocate(opd_ext(nlayers,nmiewave),opd_scat(nlayers,nmiewave))
allocate(cos_qs(nlayers,nmiewave))
allocate(cld1arr(nlayers))
! first set up the grids and get the Mie coefficients, cloud by cloud
!call init_column(column)
do icloud = 1, nclouds
! This bit is lifted from setup_clouds3.1.f in EGP
! it sets up the radius grid.
! constants/ parameters are in sizes
! set rmin and vrat here though:
if (trim(column(1)%cloud(icloud)%name) .eq. 'soot' ) then
vrat = 1.3
rmin = 1e-8
else
vrat = 2.2
rmin = 1e-7
endif
pw = 1. / 3.
f1 = ( 2*vrat / ( 1 + vrat) )**pw
f2 = ( 2 / ( 1 + vrat ) )**pw * vrat**(pw-1)
do irad = 1, nrad
radius(irad,icloud) = rmin * vrat**(float(irad-1)/3.)
rup(irad,icloud) = f1*radius(irad,icloud)
dr(irad,icloud) = f2*radius(irad,icloud)
enddo
! first get the mie or DHS coefficients for the condensate
if (sizdist .gt. 0) then
write(miefile,"(A,A,A)")"../Clouds/",trim(column(1)%cloud(icloud)%name),".mieff"
else if (sizdist .lt. 0) then
write(miefile,"(A,A,A)")"../Clouds/",trim(column(1)%cloud(icloud)%name),".dhs"
end if
open(unit=10, file= miefile, status='old')
read(10,*) idum1, idum2
if (idum1 .ne. nmiewave .or. idum2 .ne. nrad) then
write(*,*) "Problem with mie coefficients file contents wrong waves or radii",trim(miefile)
stop
end if
do irad = 1, nrad
read(10,*) radius_in(irad,icloud)
if (abs(radius(irad,icloud) - radius_in(irad,icloud)) .gt. &
0.01*radius(irad,icloud)) then
write(*,*) "Radius grid mismatch in mie file: ",trim(miefile)
stop
end if
do imiewave = 1, nmiewave
read(10,*) miewavelen(imiewave), qscat(imiewave,irad,icloud), &
qext(imiewave,irad,icloud), cos_qscat(imiewave,irad,icloud)
end do
end do
close(10)
end do ! cloud loop
miewaven = 1.0 / miewavelen
! let's get the location for wave = 1um in miewavelen for later
loc = minloc(abs(miewavelen - 1e-4))
loc1 = loc(1)
!write(*,*) miewavelen(loc1)
! now for each layer, run through the particle sizes summing up the
! cross sections to get optical depth for this cloud from extinction
! and scattering
! this is all hacked from calc_optics. Credit to Ackerman & Marley
! ensure that we're starting from zero values
scat_cloud = 0.d0
ext_cloud = 0.d0
cqs_cloud = 0.d0
opd_ext = 0.d0
opd_scat = 0.d0
cos_qs = 0.d0
sum_ndz_1 = 0.d0
sum_ndz_2 = 0.d0
! This sets up options to print ndz etc
do ilayer = 1, nlayers
cld1arr(ilayer) = column(ilayer)%cloud(1)%dtau1
end do
loc = maxloc(cld1arr)
idum1 = loc(1)
do ilayer = 1, nlayers
do icloud = 1, nclouds
! first we need the cloud density in this layer
! we get this from the layer optical depth of the cloud at 1um
! which is what we're given
if (column(ilayer)%cloud(icloud)%dtau1 .gt. 1.d-6) then
if (abs(sizdist) .eq. 2) then
! we take geometric mean parameter from python code
! as a value between 0 and 1. This is then translated here to
! hold a value between 1 and 5
rsig = 1. + (column(ilayer)%cloud(icloud)%rsig * 4)
! radii supplied in um, convert to cm
rg = column(ilayer)%cloud(icloud)%rg * 1e-4
r2 = rg**2 * exp( 2*log(rsig)**2 )
! Calculate normalization factor , i.e N_0
! This is based on setting tau_cl = 1 at 1 micron
! so we sum up the cross-section contrbutions at 1um from
! all radii particles across the distribution
! get Ndz from 1/ this sum
norm = 0.
do irad = 1,nrad
rr = radius(irad,icloud)
arg1 = dr(irad,icloud) / ( sqrt(2.*PI)*rr*log(rsig) )
arg2 = -(log( rr/ rg ))**2 / ( 2*(log(rsig))**2 )
qpir2 = PI * rr**2 * qext(loc1,irad,icloud)
norm = norm + (qpir2 * arg1 * exp(arg2))
end do
! so Ndz (i.e total number density * height of layer)
ndz = 1. / norm
! now loop over radius and fill up wavelength dependent opacity for
! each cloud
do imiewave = 1, nmiewave
do irad = 1, nrad
rr = radius(irad,icloud)
arg1 = dr(irad,icloud) / ( sqrt(2.*PI)*rr*log(rsig) )
arg2 = -(log( rr/rg))**2 / ( 2*(log(rsig))**2 )
pir2ndz = ndz * PI * rr**2 * arg1* exp( arg2 )
scat_cloud(ilayer,imiewave,icloud) = &
scat_cloud(ilayer,imiewave,icloud) + &
qscat(imiewave,irad,icloud)*pir2ndz
ext_cloud(ilayer,imiewave,icloud) = &
ext_cloud(ilayer,imiewave,icloud) + &
qext(imiewave,irad,icloud)*pir2ndz
cqs_cloud(ilayer,imiewave,icloud) = &
cqs_cloud(ilayer,imiewave,icloud) + &
cos_qscat(imiewave,irad,icloud)*pir2ndz
enddo ! radius loop
! sum over clouds
opd_scat(ilayer,imiewave) = opd_scat(ilayer,imiewave) + &
scat_cloud(ilayer,imiewave,icloud)
opd_ext(ilayer,imiewave) = opd_ext(ilayer,imiewave) + &
ext_cloud(ilayer,imiewave,icloud)
cos_qs(ilayer,imiewave) = cos_qs(ilayer,imiewave) + &
cqs_cloud(ilayer,imiewave,icloud)
! scat_cloud(ilayer,imiewave,icloud))
end do ! miewave loop
elseif (abs(sizdist) .eq. 1) then
! Hansen distribution
! radii supplied in um, convert to cm
a = column(ilayer)%cloud(icloud)%rg * 1d-4
! b is not a length, it is dimensionless
b = column(ilayer)%cloud(icloud)%rsig
! first need to get ndz from the optical depth dtau at 1um
bot = 0.d0
do irad = 1, nrad
rr = radius(irad,icloud)
drr = dr(irad,icloud)
!write(*,*) (-rr/(a*b)), log(drr)
arg1 = (-rr/(a*b)) + log(drr)
!write(*,*) arg1
arg2 = ((1.- 3.*b)/b) * log(rr)
argext = log(qext(loc1,irad,icloud) * PI * rr**2.)
bot = bot + exp(arg1 + arg2 + argext)
end do ! radius loop
logcon = log(column(ilayer)%cloud(icloud)%dtau1 / bot)
arg3 = ((((2.*b) - 1.)/b) * log(a*b))
arg2 = log_gamma((1.-(2.*b))/b)
ndz = exp(logcon +arg2 - arg3)
!if (icloud .eq. 1) then
! sum_ndz_1 = sum_ndz_1 +ndz
! frac = (ndz *1e4 / (column(ilayer)%dz)) * ((4/3)*PI * (0.008e-4**3)) * 0.032 * AVOGADRO / (column(ilayer)%ndens)
! write(*,*) 'MgSiO3 mixing fraction = ', frac
! write(*,*) 'sum ndz MgSiO3 = ', sum_ndz_1
!end if
!if (icloud .eq. 2) then
! sum_ndz_2 = sum_ndz_2 +ndz
! frac = (ndz * 1e4/ (column(ilayer)%dz)) * ((4/3)*PI * (0.34e-4**3)) * 0.044 * AVOGADRO / (column(ilayer)%ndens)
! write(*,*) 'SiO2 mixing fraction = ', frac
! write(*,*) 'sum ndz SiO2 = ', sum_ndz_2
!end if
!TEST writes for ndz etc
!if (ilayer .eq. idum1) then
! write(*,*) 'ndz = ', ndz
! write(*,*) 'dz = ', column(ilayer)%dz
! write(*,*) 'n = ',ndz / column(ilayer)%dz,' m^-3'
! write(*,*) 'at pressure ', column(ilayer)%press,' bars'
!end if
arg1 = ((((2.*b) - 1.)/b) * log(a*b)) + log(ndz)
logcon = (arg1 - arg2)
do imiewave = 1, nmiewave
do irad = 1, nrad
rr = radius(irad,icloud)
drr = dr(irad,icloud)
!write(*,*) (-rr/(a*b)), log(drr)
arg1 = (-rr/(a*b)) + log(drr)
!write(*,*) arg1
arg2 = ((1. - 3.*b)/b) * log(rr)
argscat = log(qscat(imiewave,irad,icloud) * PI * rr**2)
argext = log(qext(imiewave,irad,icloud) * PI * rr**2)
argcosqs = cos_qscat(imiewave,irad,icloud) * PI * rr**2
!write(*,*) logcon, arg1, arg2, arg3, arg4
scat_cloud(ilayer,imiewave,icloud) = &
scat_cloud(ilayer,imiewave,icloud) + &
exp(logcon + arg1 + arg2 + argscat)
ext_cloud(ilayer,imiewave,icloud) = &
ext_cloud(ilayer,imiewave,icloud) + &
exp(logcon + arg1 + arg2 + argext)
cqs_cloud(ilayer,imiewave,icloud) = &
cqs_cloud(ilayer,imiewave,icloud) + &
(exp(logcon + arg1 + arg2) * argcosqs)
end do ! radius loop
! sum over clouds
opd_scat(ilayer,imiewave) = opd_scat(ilayer,imiewave) + &
scat_cloud(ilayer,imiewave,icloud)
opd_ext(ilayer,imiewave) = opd_ext(ilayer,imiewave) + &
ext_cloud(ilayer,imiewave,icloud)
cos_qs(ilayer,imiewave) = cos_qs(ilayer,imiewave) + &
cqs_cloud(ilayer,imiewave,icloud)
end do ! miewave loop
end if
!write (*,*) scat_cloud(ilayer,loc1,icloud)
!write (*,*) ext_cloud(ilayer,loc1,icloud)
!write (*,*) cqs_cloud(ilayer,loc1,icloud)
end if
end do ! cloud loop
! rebin to working resolution (nwave) grid and write to
do iwave= 1 , nwave
wdiff = abs(miewaven - wavenum(iwave))
oldw1 = minloc(wdiff,1)
if (miewaven(oldw1) .lt. wavenum(iwave)) then
oldw2 = oldw1 + 1
else
oldw2 = oldw1
oldw1 = oldw2 - 1
end if
!intfact = (log10(wavenum(iwave)) - log10(miewaven(oldw1))) / &
! (log10(miewaven(oldw2)) - log10(miewaven(oldw1)))
! gg should be interpolated in linear space - it is always small
lintfact = (wavenum(iwave) - miewaven(oldw1)) / &
(miewaven(oldw2) - miewaven(oldw1))
column(ilayer)%opd_ext(iwave) = &
((opd_ext(ilayer,oldw2) - opd_ext(ilayer,oldw1)) * lintfact) &
+ opd_ext(ilayer,oldw1)
column(ilayer)%opd_scat(iwave) = &
((opd_scat(ilayer,oldw2) - opd_scat(ilayer,oldw1)) * lintfact) &
+ opd_scat(ilayer,oldw1)
column(ilayer)%gg(iwave) = &
(((cos_qs(ilayer,oldw2) - cos_qs(ilayer,oldw1)) * lintfact) &
+ cos_qs(ilayer,oldw1) ) / column(ilayer)%opd_scat(iwave)
if (column(ilayer)%opd_scat(iwave) .lt. 1d-50) then
column(ilayer)%opd_scat(iwave) = 0.
column(ilayer)%gg(iwave) = 0.
end if
if (column(ilayer)%opd_ext(iwave) .lt. 1d-50) then
column(ilayer)%opd_ext(iwave) = 0.
end if
end do ! wave loop
end do ! layer loop
! TK test line see what we've got
!loc = minloc(abs((1e4/wavenum) - 1.2 ))
!loc1a = loc(1)
!write(*,*) "wavenum, wavelength for check = ", wavenum(loc1a), wavelen(loc1a)
!write(*,*) "clouds line 187 opd_scat layer ",column(idum1)%opd_scat(loc1a)
!write(*,*) "clouds line 188 opd_ext layer",column(idum1)%opd_ext(loc1a)
!write(*,*) "clouds line 189 gg layer ",column(idum1)%gg(loc1a)
! test line write cloud optical depth out
!open(unit=10,file='cloud3_diagn.txt',form='formatted',status='new')
!scat = 0.
!ext = 0.
!cqs = 0.
!loc = minloc(abs((1e4*miewavelen) - 1.0 ))
!loc1m = loc(1)
!do ilayer = 1, nlayers
! if (ext(loc1m) .lt. 1.0) then
! do imiewave= 1, nmiewave
! scat(imiewave) = scat(imiewave) + scat_cloud(ilayer,imiewave,3)
! ext(imiewave) = ext(imiewave) + ext_cloud(ilayer,imiewave,3)
! cqs(imiewave) = cqs(imiewave) + cqs_cloud(ilayer,imiewave,3)
! end do
! end if
!enddo
!do imiewave = 1, nmiewave
! write(10,*) miewavelen(imiewave),scat(imiewave),ext(imiewave),cqs(imiewave)
!end do
!close(10)
deallocate(qscat,qext,cos_qscat,radius_in,radius,dr,rup)
deallocate(scat_cloud,ext_cloud,cqs_cloud)
deallocate(opd_ext,opd_scat,cos_qs)
end subroutine cloudatlas
end module clouds