/
lin_model_from_parameters.jl
385 lines (319 loc) · 19.7 KB
/
lin_model_from_parameters.jl
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
#=
This file contains the `model_from_parameters` function, which computes all derived information
like optical thicknesses, from the input parameters. Produces a vSmartMOM_Model object.
=#
"Generate default set of parameters for Radiative Transfer calculations (from ModelParameters/)"
default_parameters() = parameters_from_yaml(joinpath(dirname(pathof(vSmartMOM)), "CoreRT", "DefaultParameters.yaml"))
"Take the parameters specified in the vSmartMOM_Parameters struct, and calculate derived attributes into a vSmartMOM_Model"
function lin_model_from_parameters(params::vSmartMOM_Parameters)
FT = params.float_type
#@show FT
# Number of total bands and aerosols (for convenience)
n_bands = length(params.spec_bands)
n_aer = isnothing(params.scattering_params) ? 0 : length(params.scattering_params.rt_aerosols)
# Create observation geometry
obs_geom = ObsGeometry{FT}(params.sza, params.vza, params.vaz, params.obs_alt)
# Create truncation type
truncation_type = Scattering.δBGE{FT}(params.l_trunc, params.Δ_angle)
#@show truncation_type
# Set quadrature points for streams
quad_points = rt_set_streams(params.quadrature_type, params.l_trunc, obs_geom, params.polarization_type, array_type(params.architecture))
# Get AtmosphericProfile from parameters
vmr = isnothing(params.absorption_params) ? Dict() : params.absorption_params.vmr
p_full, p_half, vmr_h2o, vcd_dry, vcd_h2o, new_vmr, Δz = compute_atmos_profile_fields(params.T, params.p, params.q, vmr)
profile = AtmosphericProfile(params.T, p_full, params.q, p_half, vmr_h2o, vcd_dry, vcd_h2o, new_vmr,Δz)
# Reduce the profile to the number of target layers (if specified)
if params.profile_reduction_n != -1
profile = reduce_profile(params.profile_reduction_n, profile);
end
# Rayleigh optical properties calculation
greek_rayleigh = Scattering.get_greek_rayleigh(FT(params.depol))
# Remove rayleight for testing:
τ_rayl = [zeros(FT,length(params.spec_bands[i]), length(params.T)) for i=1:n_bands];
#τ_rayl = [zeros(FT,1,length(profile.T)) for i=1:n_bands];
# This is a kludge for now, tau_abs sometimes needs to be a dual. Suniti & us need to rethink this all!!
# i.e. code the rt core with fixed amount of derivatives as in her paper, then compute chain rule for dtau/dVMr, etc...
FT2 = isnothing(params.absorption_params) || !haskey(params.absorption_params.vmr,"CO2") ? params.float_type : eltype(params.absorption_params.vmr["CO2"])
τ_abs = [zeros(FT2, length(params.spec_bands[i]), length(profile.p_full)) for i in 1:n_bands]
# Loop over all bands:
for i_band=1:n_bands
# i'th spectral band (convert from cm⁻¹ to μm)
curr_band_λ = FT.(1e4 ./ params.spec_bands[i_band])
# @show profile.vcd_dry, size(τ_rayl[i_band])
# Compute Rayleigh properties per layer for `i_band` band center
τ_rayl[i_band] .= getRayleighLayerOptProp(profile.p_half[end],
curr_band_λ, #(mean(curr_band_λ)),
params.depol, profile.vcd_dry);
#@show τ_rayl[i_band]
# If no absorption, continue to next band
isnothing(params.absorption_params) && continue
# Loop over all molecules in this band, obtain profile for each, and add them up
for molec_i in 1:length(params.absorption_params.molecules[i_band])
@show params.absorption_params.molecules[i_band][molec_i]
# This can be precomputed as well later in my mind, providing an absorption_model or an interpolation_model!
if isempty(params.absorption_params.luts)
# Obtain hitran data for this molecule
@timeit "Read HITRAN" hitran_data = read_hitran(artifact(params.absorption_params.molecules[i_band][molec_i]), iso=1)
println("Computing profile for $(params.absorption_params.molecules[i_band][molec_i]) with vmr $(profile.vmr[params.absorption_params.molecules[i_band][molec_i]]) for band #$(i_band)")
# Create absorption model with parameters beforehand now:
absorption_model = make_hitran_model(hitran_data,
params.absorption_params.broadening_function,
wing_cutoff = params.absorption_params.wing_cutoff,
CEF = params.absorption_params.CEF,
architecture = params.architecture,
vmr = 0);#mean(profile.vmr[params.absorption_params.molecules[i_band][molec_i]]))
# Calculate absorption profile
@timeit "Absorption Coeff" compute_absorption_profile!(τ_abs[i_band], absorption_model, params.spec_bands[i_band],profile.vmr[params.absorption_params.molecules[i_band][molec_i]], profile);
# Use LUT directly
else
compute_absorption_profile!(τ_abs[i_band], params.absorption_params.luts[i_band][molec_i], params.spec_bands[i_band],profile.vmr[params.absorption_params.molecules[i_band][molec_i]], profile);
end
end
end
# aerosol_optics[iBand][iAer]
aerosol_optics = [Array{AerosolOptics}(undef, (n_aer)) for i=1:n_bands];
lin_aerosol_optics = [Array{dAerosolOptics}(undef, (n_aer)) for i=1:n_bands];
FT2 = isnothing(params.scattering_params) ? params.float_type : typeof(params.scattering_params.rt_aerosols[1].τ_ref)
#@show FT2
#FT2 = params.float_type
# τ_aer[iBand][iAer,iZ]
τ_aer = [zeros(FT2, n_aer, length(profile.p_full)) for i=1:n_bands];
# Loop over aerosol type
for i_aer=1:n_aer
# Get curr_aerosol
c_aero = params.scattering_params.rt_aerosols[i_aer]
curr_aerosol = c_aero.aerosol
# Create Aerosol size distribution for each aerosol species
size_distribution = curr_aerosol.size_distribution
# Create a univariate aerosol distribution
mie_aerosol = Aerosol(size_distribution, curr_aerosol.nᵣ, curr_aerosol.nᵢ)
#@show typeof(curr_aerosol.nᵣ)
#mie_aerosol = make_mie_aerosol(size_distribution, curr_aerosol.nᵣ, curr_aerosol.nᵢ, params.scattering_params.r_max, params.scattering_params.nquad_radius) #Suniti: why is the refractive index needed here?
# Create the aerosol extinction cross-section at the reference wavelength:
mie_model = make_mie_model(params.scattering_params.decomp_type,
mie_aerosol,
params.scattering_params.λ_ref,
params.polarization_type,
truncation_type,
params.scattering_params.r_max,
params.scattering_params.nquad_radius)
mie_model.aerosol.nᵣ = real(params.scattering_params.n_ref)
mie_model.aerosol.nᵢ = -imag(params.scattering_params.n_ref)
@show params.scattering_params.n_ref
k_ref, dk_ref = compute_ref_aerosol_extinction(mie_model, params.float_type)
@show k_ref
#params.scattering_params.rt_aerosols[i_aer].p₀, params.scattering_params.rt_aerosols[i_aer].σp
# Loop over bands
for i_band=1:n_bands
# i'th spectral band (convert from cm⁻¹ to μm)
curr_band_λ = FT.(1e4 ./ params.spec_bands[i_band])
# Create the aerosols:
mie_model = make_mie_model(params.scattering_params.decomp_type,
mie_aerosol,
(maximum(curr_band_λ)+minimum(curr_band_λ))/2,
params.polarization_type,
truncation_type,
params.scattering_params.r_max,
params.scattering_params.nquad_radius)
n_ref = params.scattering_params.n_ref
#k = compute_ref_aerosol_extinction(mie_model, params.float_type)
#@show k
# Compute raw (not truncated) aerosol optical properties (not needed in RT eventually)
#@show FT2, FT
@timeit "Mie calc" aerosol_optics_raw, lin_aerosol_optics_raw =
compute_aerosol_optical_properties(mie_model, FT);
@show aerosol_optics_raw.k
aerosol_optics_raw.k_ref = k_ref
@show k_ref, dk_ref
@show size(lin_aerosol_optics_raw.dk_ref), size(dk_ref)
lin_aerosol_optics_raw.dk_ref = dk_ref
# Compute truncated aerosol optical properties (phase function and fᵗ), consistent with Ltrunc:
#@show i_aer, i_band
aerosol_optics[i_band][i_aer],lin_aerosol_optics[i_band][i_aer] = Scattering.truncate_phase(truncation_type,
aerosol_optics_raw, lin_aerosol_optics_raw; reportFit=false)
#aerosol_optics[i_band][i_aer] = aerosol_optics_raw
@show aerosol_optics[i_band][i_aer].k
#@show aerosol_optics[i_band][i_aer].fᵗ
# Compute nAer aerosol optical thickness profiles
τ_aer[i_band][i_aer,:] =
params.scattering_params.rt_aerosols[i_aer].τ_ref *
(aerosol_optics[i_band][i_aer].k/k_ref) *
getAerosolLayerOptProp(1, c_aero.profile, profile)
@info "AOD at band $i_band : $(sum(τ_aer[i_band][i_aer,:])), truncation factor = $(aerosol_optics[i_band][i_aer].fᵗ)"
end
end
# Check the floating-type output matches specified FT
# Plots:
#=
plt = lineplot(profile.T, -profile.p_full,#ylim=(1000,0),
title="Temperature Profile", xlabel="Temperature [K]", ylabel="- Pressure [hPa]", canvas = UnicodePlots.DotCanvas, border=:ascii, compact=true)
display(plt)
println()
plt = lineplot(profile.q, -profile.p_full,
title="Humidity Profile", xlabel="Specific humidity", ylabel="- Pressure [hPa]", canvas = UnicodePlots.DotCanvas, border=:ascii, compact=true)
display(plt)
#=@show typeof(τ_aer[1][1,:])
plt = lineplot(τ_aer[1][1,:],-profile.p_full,
title="AOD Profile Band 1", xlabel="AOD", ylabel="- Pressure [hPa]", canvas = UnicodePlots.DotCanvas, border=:ascii, compact=true)
display(plt)=#
=#
# Return the model
return vSmartMOM_Model(params,
aerosol_optics,
greek_rayleigh,
quad_points,
τ_abs,
τ_rayl,
τ_aer,
obs_geom,
profile), vSmartMOM_lin(lin_aerosol_optics)
end
#=
Modified version for vibrational Ramnan scattering
=#
"Take the parameters specified in the vSmartMOM_Parameters struct, and calculate derived attributes into a vSmartMOM_Model"
function model_from_parameters(RS_type::Union{VS_0to1_plus, VS_1to0_plus},
λ₀,
params::vSmartMOM_Parameters)
@show params.absorption_params.molecules
# Number of total bands and aerosols (for convenience)
n_bands = 3 #length(params.spec_bands)
n_aer = isnothing(params.scattering_params) ? 0 : length(params.scattering_params.rt_aerosols)
# Create observation geometry
obs_geom = ObsGeometry(params.sza, params.vza, params.vaz, params.obs_alt)
# Create truncation type
truncation_type = Scattering.δBGE{params.float_type}(params.l_trunc, params.Δ_angle)
# Set quadrature points for streams
quad_points = rt_set_streams(params.quadrature_type, params.l_trunc, obs_geom, params.polarization_type, array_type(params.architecture))
# Get AtmosphericProfile from parameters
vmr = isnothing(params.absorption_params) ? Dict() : params.absorption_params.vmr
p_full, p_half, vmr_h2o, vcd_dry, vcd_h2o, new_vmr, Δz = compute_atmos_profile_fields(params.T, params.p, params.q, vmr)
profile = AtmosphericProfile(params.T, p_full, params.q, p_half, vmr_h2o, vcd_dry, vcd_h2o, new_vmr,Δz)
# Reduce the profile to the number of target layers (if specified)
if params.profile_reduction_n != -1
profile = reduce_profile(params.profile_reduction_n, profile);
end
effT = (profile.vcd_dry' * profile.T) / sum(profile.vcd_dry);
# Define RS type
# Compute N2 and O2
RS_type.n2, RS_type.o2 =
InelasticScattering.getRamanAtmoConstants(1.e7/λ₀,effT);
println("here 0")
InelasticScattering.getRamanSSProp!(RS_type, λ₀);
println("here 1")
n_bands = length(RS_type.iBand)
@show RS_type.grid_in
params.spec_bands = RS_type.grid_in
@show params.spec_bands
# Rayleigh optical properties calculation
greek_rayleigh = Scattering.get_greek_rayleigh(params.depol)
τ_rayl = [zeros(params.float_type,1, length(profile.p_full)) for i=1:n_bands];
# τ_abs[iBand][iSpec,iZ]
τ_abs = [zeros(params.float_type, length(params.spec_bands[i]), length(profile.p_full)) for i in 1:n_bands]
@show params.absorption_params.molecules[2]
# Loop over all bands:
for i_band=1:n_bands
@show params.spec_bands[i_band]
# i'th spectral band (convert from cm⁻¹ to μm)
curr_band_λ = 1e4 ./ params.spec_bands[i_band]
# Compute Rayleigh properties per layer for `i_band` band center
τ_rayl[i_band] .= getRayleighLayerOptProp(profile.p_half[end],
(maximum(curr_band_λ) + minimum(curr_band_λ))/2,
params.depol, profile.vcd_dry);
# If no absorption, continue to next band
isnothing(params.absorption_params) && continue
@show i_band, params.absorption_params.molecules[i_band]
# Loop over all molecules in this band, obtain profile for each, and add them up
for molec_i in 1:length(params.absorption_params.molecules[i_band])
# This can be precomputed as well later in my mind, providing an absorption_model or an interpolation_model!
if isempty(params.absorption_params.luts)
# Obtain hitran data for this molecule
@timeit "Read HITRAN" hitran_data = read_hitran(artifact(params.absorption_params.molecules[i_band][molec_i]), iso=1)
println("Computing profile for $(params.absorption_params.molecules[i_band][molec_i]) with vmr $(profile.vmr[params.absorption_params.molecules[i_band][molec_i]]) for band #$(i_band)")
# Create absorption model with parameters beforehand now:
absorption_model = make_hitran_model(hitran_data,
params.absorption_params.broadening_function,
wing_cutoff = params.absorption_params.wing_cutoff,
CEF = params.absorption_params.CEF,
architecture = params.architecture,
vmr = 0);#mean(profile.vmr[params.absorption_params.molecules[i_band][molec_i]]))
# Calculate absorption profile
@timeit "Absorption Coeff" compute_absorption_profile!(τ_abs[i_band], absorption_model, params.spec_bands[i_band],profile.vmr[params.absorption_params.molecules[i_band][molec_i]], profile);
# Use LUT directly
else
compute_absorption_profile!(τ_abs[i_band], params.absorption_params.luts[i_band][molec_i], params.spec_bands[i_band],profile.vmr[params.absorption_params.molecules[i_band][molec_i]], profile);
end
end
end
# aerosol_optics[iBand][iAer]
aerosol_optics = [Array{AerosolOptics}(undef, (n_aer)) for i=1:n_bands];
FT2 = isnothing(params.scattering_params) ? params.float_type : typeof(params.scattering_params.rt_aerosols[1].τ_ref)
FT2 = params.float_type
# τ_aer[iBand][iAer,iZ]
τ_aer = [zeros(FT2, n_aer, length(profile.p_full)) for i=1:n_bands];
# Loop over aerosol type
for i_aer=1:n_aer
# Get curr_aerosol
c_aero = params.scattering_params.rt_aerosols[i_aer]
curr_aerosol = c_aero.aerosol
# Create Aerosol size distribution for each aerosol species
size_distribution = curr_aerosol.size_distribution
# Create a univariate aerosol distribution
mie_aerosol = Aerosol(size_distribution, curr_aerosol.nᵣ, curr_aerosol.nᵢ)
@show typeof(curr_aerosol.nᵣ)
#mie_aerosol = make_mie_aerosol(size_distribution, curr_aerosol.nᵣ, curr_aerosol.nᵢ, params.scattering_params.r_max, params.scattering_params.nquad_radius) #Suniti: why is the refractive index needed here?
# Create the aerosol extinction cross-section at the reference wavelength:
mie_model = make_mie_model(params.scattering_params.decomp_type,
mie_aerosol,
params.scattering_params.λ_ref,
params.polarization_type,
truncation_type,
params.scattering_params.r_max,
params.scattering_params.nquad_radius)
mie_model.aerosol.nᵣ = real(params.scattering_params.n_ref)
mie_model.aerosol.nᵢ = -imag(params.scattering_params.n_ref)
@show params.scattering_params.n_ref
k_ref = compute_ref_aerosol_extinction(mie_model, params.float_type)
#params.scattering_params.rt_aerosols[i_aer].p₀, params.scattering_params.rt_aerosols[i_aer].σp
# Loop over bands
for i_band=1:n_bands
# i'th spectral band (convert from cm⁻¹ to μm)
curr_band_λ = 1e4 ./ params.spec_bands[i_band]
# Create the aerosols:
mie_model = make_mie_model(params.scattering_params.decomp_type,
mie_aerosol,
(maximum(curr_band_λ)+minimum(curr_band_λ))/2,
params.polarization_type,
truncation_type,
params.scattering_params.r_max,
params.scattering_params.nquad_radius)
# Compute raw (not truncated) aerosol optical properties (not needed in RT eventually)
# @show FT2
@timeit "Mie calc" aerosol_optics_raw = compute_aerosol_optical_properties(mie_model, FT2);
# Compute truncated aerosol optical properties (phase function and fᵗ), consistent with Ltrunc:
@show i_aer, i_band
aerosol_optics[i_band][i_aer] = Scattering.truncate_phase(truncation_type,
aerosol_optics_raw; reportFit=false)
# Compute nAer aerosol optical thickness profiles
τ_aer[i_band][i_aer,:] =
params.scattering_params.rt_aerosols[i_aer].τ_ref *
(aerosol_optics[i_band][i_aer].k/k_ref) *
CoreRT.getAerosolLayerOptProp(1.0, c_aero.p₀, c_aero.σp, profile)
end
end
# Check the floating-type output matches specified FT
# Return the model
return vSmartMOM_Model(params, aerosol_optics, greek_rayleigh, quad_points, τ_abs, τ_rayl, τ_aer, obs_geom, profile)
end
function loadAbsco(file; scale=(1.0))
absco = Dataset(file)
mol = absco["Gas_Index"][1]
cs_name = "Gas_"* mol * "_Absorption"
# Loading cross sections:
σ = Float32(scale)*absco[cs_name][:]
# Temperature
T = absco["Temperature"][:]
p = absco["Pressure"][:]/100
ν = absco["Wavenumber"][:]
return Absorption.AbscoTable(parse(Int,mol), -1, ν, σ, p, T )
end