-
Notifications
You must be signed in to change notification settings - Fork 1
/
VRT_module.py
1484 lines (1121 loc) · 64.7 KB
/
VRT_module.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 12 15:35:03 2021
@author: indujaa
"""
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 8 21:34:38 2020
Quick lookups
Stokes matrix for different polarizations:
H - [1 1 0 0]
V - [1 -1 0 0]
LC - [2 0 0 -2]
RC - [2 0 0 2]
+45 linear - [1 0 1 0]
-45 linear - [1 0 -1 0]
if poltype == "lin":
B = np.matrix([[0.5, 0.5, 0., 0.], [0.5, -0.5, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
elif poltype == "circ":
B = 0.5 * np.matrix([[0, 1.0, 1j, 0.], [1., 0., 0., 1.], [1., 0., 0., -1.], [0, 1.0, -1j, 0.]])
Multiple scattering can be ignored for volfrac < 0.1 in a lossy medium (Fa 2011, Tsang 1985, Jun 1984)
@author: Indujaa
"""
import sys
import numpy as np
from cmath import *
from decimal import *
from scipy.constants import *
import scipy.integrate
from scipy.special import gamma
import pandas as pd
import matlab.engine
from pytmatrix import tmatrix, orientation, scatter, refractive
from pytmatrix.psd import PSDIntegrator, GammaPSD, ExponentialPSD, UnnormalizedGammaPSD
import radar_inst as radar
from RoughSurface import RoughSurface
import FresnelCoefficients as Fresnel
class VRT:
"""
A class containing all the attributes and methods for the surface scatter +
radiative transfer model.
"""
def __init__(self):
# Constants
self.wavelength = radar.wavelength # wavelength in meters
self.nu = c / self.wavelength
self.k = 2 * np.pi / self.wavelength
## Magellan altitude at periapsis
self.R = radar.altitude # 250 km periapsis; 8029 km apoapsis
# Polarization information
self.polV = radar.polarization1
self.polH = radar.polarization2
def setGeometricOptics(self):
"""
Sets the viewing geometry properties (mainly angles) for each layer.
"""
self.phi_i = np.deg2rad(0)
self.phi_s = np.pi - self.phi_i
def sigma2sigma0(self, sigma):
"""
Converts radar cross section coefficient into log space. Returns values in dB
Parameters:
sigma (float or list or array): An array or single value of radar cross-section
Returns:
sigma0 (float or list or array): An array or single value of backscatter coeffiecient
"""
try:
if len(sigma) > 1:
sigma = np.array(sigma)
except:
pass
sigma0 = 10 * np.log10(sigma)
return sigma0
def sigma02sigma(self, sigma0):
"""
Converts radar cross section coefficient from log to non-log space.
Returns values in between 0-1 or >1 deoending on the target.
Parameters:
sigma0(float or list or array): An array or single value of backscatter coeffiecient
Returns:
sigma (float or list or array): An array or single value of radar cross-section
"""
try:
if len(sigma0) > 1:
sigma0 = np.array(sigma0)
except:
pass
sigma = 10 ** (sigma0/10)
return sigma
def Muellermod2sigma(self, M):
"""
Converts modified 4x4 Mueller matrix into backscatter coefficient (NOT IN dB) in hh and vv
Parameters:
M(2D array): A 4x4 array contaiing the modified Mueller matrix elements
Returns:
shh(float): Radar backscatter coefficient in hh
svv(float): Radar backscatter coefficient in vv
"""
svv = 4 * np.pi * M[0,0]
shh = 4 * np.pi * M[1,1]
return svv, shh
def Mueller2sigma(self, M):
"""
Converts 4x4 Mueller matrix into backscatter coefficient (NOT IN dB) in hh and vv
Parameters:
M(2D array): A 4x4 array contaiing the 16 Mueller matrix elements
Returns:
shh(float): Radar backscatter coefficient in hh
svv(float): Radar backscatter coefficient in vv
"""
# # multiply by 4*pi*cos theta if needed
# # division by a factor of 4 or 2?
svv = 4 * np.pi * (M[0,0] + 2*M[0,1] + M[1,1])
shh = 4 * np.pi * (M[0,0] - 2*M[0,1] + M[1,1])
return svv, shh
def sigma2Mueller(self, svv, shh, svh=0):
"""
Converts backscatter coefficient (NOT IN dB) in hh and vv into a 4x4 Mueller matrix
Parameters:
shh(float): Radar backscatter coefficient in hh
svv(float): Radar backscatter coefficient in vv
svh(float or None): Radar backscatter coefficient in vh
Returns:
M(2D array): A 4x4 array contaiing the 16 Mueller matrix elements
"""
shv = - svh
s11 = svv / 4 / np.pi
s22 = shh / 4 / np.pi
s12 = svh / 4 / np.pi
s21 = shv / 4 / np.pi
# # Mueller matrix implementation
m11 = s11 + s22 + 2*s21
m12 = s11- s22
m13 = 0
m14 = 0
m22 = s11 + s22 - 2*s21
m23 = 0
m24 = 0
# # check m33 and m34 and check if s12 or s21 is negative
# m33 = 2*shv
m33 = 2* (s11*s22)**0.5
m34 = 0
# m44 = 2*shv
m44 = 2* (s11*s22)**0.5
M = np.array([[m11,m12,m13,m14], [m12,m22,m23,m24], [m13, m23, m33, m34], [-m14, -m24, -m34, m44]]) / 4
# Modified Mueller materix form
B = np.matrix([[0.5, 0.5, 0., 0.], [0.5, -0.5, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
M = B*M*np.linalg.inv(B)
return M
def Mueller2cpr(self,M, poltype="linear"):
"""
Converts 4x4 Mueller matrix into circular polarization ratio (CPR)
Parameters:
M(2D array): A 4x4 array contaiing the 16 Mueller matrix elements
poltype (string or None): "linear" or "circular"
Returns:
CPR(float): circular polarization ratio value
"""
# # cpr = S1-S4 / S1+S4 = Iv+Ih-V / Iv+Ih+V
if poltype == "linear":
Is = np.linalg.multi_dot([M, np.array([0,1,0,0])])
elif poltype == "circular":
Is = np.linalg.multi_dot([M, np.array([0.5,0.5,0,1])])
cpr = (Is[0,0] + Is[0,1] - Is[0,3]) / (Is[0,0] + Is[0,1] + Is[0,3])
return cpr
def Mueller2dlp(self,M, poltype="linear"):
"""
Converts 4x4 Mueller matrix into degree of linear polarization (DLP)
Parameters:
M(2D array): A 4x4 array contaiing the 16 Mueller matrix elements
poltype (string or None): "linear" or "circular"
Returns:
DLP(float): degree of linear polarization value
"""
# # dlp = np.sqrt(S2**2 + S3**2) / S1 = Iv+Ih-V / Iv+Ih+V
if poltype == "linear":
Is = np.linalg.multi_dot([M, np.array([0,1,0,0])])
elif poltype == "circular":
Is = np.linalg.multi_dot([M, np.array([0.5,0.5,0,1])])
dlp = np.sqrt((Is[0,0] - Is[0,1])**2 + Is[0,2]**2) / (Is[0,0] + Is[0,1])
return dlp
def sigma2dlp(self, svv, shh, svh=0, poltype = "circular"):
"""
Converts backscatter coefficient (NOT IN dB) into circular polarization ratio (CPR)
Parameters:
shh(float): Radar backscatter coefficient in hh
svv(float): Radar backscatter coefficient in vv
svh(float or None): Radar backscatter coefficient in vh
poltype (string or None): "linear" or "circular"
Returns:
CPR(float): circular polarization ratio value
"""
M = self.sigma2Mueller(svv, shh, svh)
return self.Mueller2dlp(M, poltype)
def sigma2cpr(self, svv, shh, svh):
"""
Converts backscatter coefficient (NOT IN dB) into degree of linear polarization (DLP)
Parameters:
shh(float): Radar backscatter coefficient in hh
svv(float): Radar backscatter coefficient in vv
svh(float or None): Radar backscatter coefficient in vh
poltype (string or None): "linear" or "circular"
Returns:
DLP(float): degree of linear polarization value
"""
cpr = (svv+shh+2*np.sqrt(svv*shh))/(svv+shh-2*np.sqrt(svv*shh))
return cpr
def TransmissionAngle(self, thetai, eps1, eps2):
"""
Compute the angle of transmisssion (rad) from one medium to another.
Parameters:
thetai (float): Incidence angle (in radians)
eps1 (complex): complex permittivity of the upper medium
eps2 (complex): complex permittivity of the lower medium
Returns:
thetai (float): Transmission angle (in radians) into lower layer
"""
mu1 = np.cos(thetai)
n = np.sqrt(eps2/eps1)
mu2 = np.sqrt(1.0 - ((1.0 - mu1**2) / n**2)).real
thetat = np.arccos(mu2)
return thetat
def CoherentTransMatrix(self, eps1, eps2, ks, thetai_rad):
"""
Compute the 4x4 real-valued transmission matrix
# # adapted from Fung 1994 + SMRT's iem_fung92.py - SMRT has an additional mu2/mu1 in tvh
# # value of tvh calculated from smrt's fresnel.py
# # add T_coh *= transmittivity in v and h
Parameters:
eps1 (complex): complex permittivity of the upper medium
eps2 (complex): complex permittivity of the lower medium
ks (float): electromagnetic roughness at the interface between layer 1 and layer 2
thetai_rad (float): Incidence angle (in radians)
Returns:
T_coh (2D array): A 4x4 numpy array containing elements of the coherent transmission matrix
"""
mu1 = np.cos(thetai_rad)
n = np.sqrt(eps2/eps1)
mu2 = np.sqrt(1.0 - (1.0 - mu1**2) / n**2).real
refh,transh,rh,th = Fresnel.FresnelH(eps1, eps2, thetai_rad)
refv,transv,rv,tv = Fresnel.FresnelV(eps1, eps2, thetai_rad)
K_i = self.k * np.sqrt(eps1.real) * mu1
K_t = self.k * np.sqrt(eps2 - ((1 - mu1**2) * eps1)).real
loss_factor = np.exp(-ks**2 * (K_t - K_i)**2)
# # Modified Mueller matrix convention - not following the refractive index factor in eq 11.61b in Ulaby big book
# fact = (n**3 * mu2 / mu1).real
m11 = np.abs(transv) ** 2
m22 = np.abs(transh) ** 2
m33 = (transv * np.conj(transh)).real
m43 = (transv * np.conj(transh)).imag
# # Mueller matrix convention
T = 0.5 * np.matrix([[tv+th, tv-th, 0., 0.], [tv-th, tv+th, 0., 0.], [0., 0., 2*(tv*th)**0.5, 0.], [0., 0., 0., 2*(tv*th)**0.5]])
# Modified Mueller materix form
B = np.matrix([[0.5, 0.5, 0., 0.], [0.5, -0.5, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
T = B*T*np.linalg.inv(B)
T_coh = loss_factor * T
return T_coh
def CoherentRefMatrix(self, eps1, eps2, ks, thetai_rad):
"""
Compute the 4x4 real-valued reflection matrix
# # adapted from Fung 1994 + SMRT's iem_fung92.py - SMRT has an additional mu2/mu1 in tvh
# # value of tvh calculated from smrt's fresnel.py
# # add T_coh *= transmittivity in v and h
Parameters:
eps1 (complex): complex permittivity of the upper medium
eps2 (complex): complex permittivity of the lower medium
ks (float): electromagnetic roughness at the interface between layer 1 and layer 2
thetai_rad (float): Incidence angle (in radians)
Returns:
R_coh (2D array): A 4x4 numpy array containing elements of the coherent reflection matrix
"""
mu = np.cos(thetai_rad)
refh,transh,rh,th = Fresnel.FresnelH(eps1, eps2, thetai_rad)
refv,transv,rv,tv = Fresnel.FresnelV(eps1, eps2, thetai_rad)
K_s2 = self.k ** 2 * (eps1.real ** 2 + eps1.imag ** 2) # square of the wavenumber in the scattered medium
loss_factor = np.exp(-2 * ks**2 * K_s2 * mu**2)
# # Modified Mueller matrix convention
r11 = rh
r22 = rv
r33 = (refv * np.conj(refh)).real
r43 = (refv * np.conj(refh)).imag
# R = np.matrix([[r11, 0., 0., 0.], [0., r22, 0., 0.], [0., 0., r33, -r43], [0., 0., r43, r33]])
# Mueller matrix convention
R = 0.5 * np.matrix([[rv+rh, rv-rh, 0., 0.], [rv-rh, rv+rh, 0., 0.], [0., 0., 2*(rv*rh)**0.5, 0.], [0., 0., 0., 2*(rv*rh)**0.5]])
# Modified Mueller materix form
B = np.matrix([[0.5, 0.5, 0., 0.], [0.5, -0.5, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
R = B*R*np.linalg.inv(B)
R_coh = loss_factor * R
return R_coh
def numscatterers_pervolume(self, psd, dist_type="Gamma", Nw=1, Lambda=1, mu=100, D_max = 0.06, D_med = 1):
"""
Compute the integrated total number concentration of particles in the upper medium.
Parameters:
psd (instance of pytmatrix.psd class): particle size distribution
dist_type (string): Type of distribution. Curren options:"Exponential" or Gamma"
nf (int or None): nf or Nw paramter use din the pytmatrix.psd class
Lambda (int or None): shape paramter for exponential distribution
mu (int or None): paramter for gamma disstribution
D_max (int or None): maximum particle size/diamter
D_med (int or None): median particle size/diamter
Returns:
D_func_int (float): Total number concentration of particles per unit volume)
"""
if dist_type == "gamma":
nf = Nw * 6.0/3.67**4 * (3.67+mu)**(mu+4)/gamma(mu+4)
Dfunc = lambda d: nf * np.exp(mu*np.log(d)-(3.67+mu)*(d))
D_func_int, err = scipy.integrate.quad(Dfunc, 0.0001, np.inf)
elif dist_type == "exponential":
Dfunc = lambda d: Nw * np.exp(-Lambda*d)
D_func_int, err = scipy.integrate.quad(Dfunc, 0.0001, D_max) # # using 0.0001 as lower limit to avoid division by 0
elif dist_type == "ungamma":
Dfunc = lambda d: Nw * np.exp(mu*np.log(d)-Lambda*d)
D_func_int, err = scipy.integrate.quad(Dfunc, 0.0001, np.inf) # # using 0.0001 as lower limit to avoid division by 0
return D_func_int
def Tmatrixcalc(self, wavelength, radius, rindex_tup, volfrac_tup, axis_ratio, alpha, beta, **psdargs):
"""
Initializes a scatterer object of class pytmatrix.Scatterer
Make sure particle size and wavelength have the same unit
Current units: meters
Parameters:
wavelength (float): incident wavelength in meter
radius (float): radius of inclusions in meters
rindex_tup (tuple): a tuple containing the real and imaginary part of the
refractive index of the background medium
volfrac_tup (tuple): a tuple containing the real and imaginary part of the
refractive index of the inclusions
axis_ratio (float): axis ratio of spheroidal inclusions
alpha (float): orientation angle in radians
beta (float): orientation angle in radians
Returs:
scatterer: Instance of class pytmatrix.Scatterer initialized with values from input args
n0 (float): Total number concentration of particles per unit volume
"""
ri = refractive.mg_refractive(rindex_tup, volfrac_tup)
scatterer = tmatrix.Scatterer(wavelength = wavelength,
m = ri,
axis_ratio = axis_ratio,
alpha = alpha,
beta = beta)
# # orientation averaging
scatterer.orient = orientation.orient_averaged_fixed
scatterer.or_pdf = orientation.uniform_pdf()
n0=0
# particle size distribution
if psdargs is not None:
scatterer.psd_integrator = PSDIntegrator(D_max = psdargs["D_max"], num_points=100)
if psdargs["psdfunc"] == "exponential":
scatterer.psd = ExponentialPSD(N0=psdargs["N"], Lambda=psdargs["Lambda"], D_max = psdargs["D_max"])
elif psdargs["psdfunc"] == "gamma":
scatterer.psd = GammaPSD(D0 = psdargs["D_max"]/2, Nw = psdargs["N"], mu=psdargs["mu"])
elif psdargs["psdfunc"] == "ungamma":
scatterer.psd = UnnormalizedGammaPSD(N0 = psdargs["N"], Lambda=psdargs["Lambda"], mu=psdargs["mu"], D_max = psdargs["D_max"])
n0 = self.numscatterers_pervolume(scatterer.psd, dist_type=psdargs["psdfunc"], Nw=psdargs["N"], Lambda=psdargs["Lambda"], mu=psdargs["mu"], D_max = psdargs["D_max"], D_med = psdargs["D_med"])
return scatterer, n0
def PhaseMatrix(self, scatterer, val, n0, theta_i, phi_i, theta_s, phi_s):
"""
Calculates Phase matrix for randomly oriented spheroids averaged over orientation
(and size distribution hopefully) in the back scattering direction
Parameters:
scatterer: instance of class pytmatrix.Scatterer
val (dict): dictionary containing input parameters that was passed to the VRT_module class
n0 (float): Total number concentration of particles per unit volume
theta_i (float): incidence angle in radians
phi_i (float): azimuth angle for incident direction in radians
theta_s (float): backscatter angle in radians
phi_s (float): azimuth angle for incident direction in radians
Returns:
P (2D array): A 4x4 phase matrix for the spheroidal inclusions/scatterers in the upper medium
"""
geom = (np.rad2deg(theta_i), np.rad2deg(theta_s), np.rad2deg(phi_i), np.rad2deg(phi_s),
val["alpha"], val["beta"])
scatterer.set_geometry(geom)
if scatterer.psd_integrator != None:
scatterer.psd_integrator.geometries = (geom,)
scatterer.psd_integrator.init_scatter_table(scatterer, angular_integration=False)
P = n0 * scatterer.get_Z()
# Modified Mueller materix form
B = np.matrix([[0.5, 0.5, 0., 0.], [0.5, -0.5, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
P = B*P*np.linalg.inv(B)
return P
def ExtinctionMatrixMish(self, scatterer, val, n0, theta, phi):
"""
Calculates Extinction matrix for layer with randomly oriented spheroids averaged over orientation
(and size distribution hopefully) in the forward scattering direction using Mischenko 2000. Pag x - equation xx
Parameters:
scatterer: instance of class pytmatrix.Scatterer
val (dict): dictionary containing input parameters that was passed to the VRT_module class
n0 (float): Total number concentration of particles per unit volume
theta (float): incidence angle in radians
phi (float): azimuth angle for incident direction in radians
Returns:
beta (1D array): An array containing the 4 eigenvalues of the extinction matrix K_e
E (2D array): A 2D array in which the columns are the 4 eigenvectors of the extinction matrix K_e
Einv (2D array): Inverse of the 2D array containg the 4 eigenvectors of the extinction matrix K_e
"""
geom = (np.rad2deg(theta), np.rad2deg(theta), np.rad2deg(phi), np.rad2deg(phi),
val["alpha"], val["beta"])
scatterer.set_geometry(geom)
if scatterer.psd_integrator != None:
scatterer.psd_integrator.geometries = (geom,)
scatterer.psd_integrator.init_scatter_table(scatterer, angular_integration=False)
SA_dyad = S = scatterer.get_S() # # fvv and fhh appear equal?
# # Tsang (1985) - eq. 5 in page 139 - attenuation rate matrix
M = (1j * 2 * np.pi * n0 / self.k) * S
# # Mischenko 2002 formula
K11 = K22 = K33 = K44 = 2 * np.pi * n0 / self.k * (S[0,0] + S[1,1]).imag
K12 = K21 = 2 * np.pi * n0 / self.k * (S[0,0] - S[1,1]).imag
K13 = K31 = -2 * np.pi * n0 / self.k * (S[0,1] + S[1,0]).imag
K14 = K41 = 2 * np.pi * n0 / self.k * (-S[0,1] + S[1,0]).real
K23 = 2 * np.pi * n0 / self.k * (-S[0,1] + S[1,0]).imag
K32 = -K23
K24 = -2 * np.pi * n0 / self.k * (S[0,1] + S[1,0]).real
K42 = -K24
K34 = 2 * np.pi * n0 / self.k * (S[1,1] - S[0,0]).real
K43 = -K34
K_e = np.matrix([[K11, K12, K13, K14], [K21, K22, K23, K24], [K31, K32, K33, K34], [K41, K42, K43, K44]])
# # # # Tsang 1985 formula
# K_e = np.matrix([[-2*M[0,0].real, 0, -M[0,1].real, -M[0,1].imag], \
# [0, -2*M[1,1].real, -M[1,0].real, M[1,0].imag], \
# [-2*M[1,0].real, -2*M[0,1].real, -M[0,0].real-M[1,1].real, M[0,0].imag-M[1,1].imag], \
# [2*M[1,0].imag, -2*M[0,1].imag, -M[0,0].imag+M[1,1].imag, -M[0,0].real-M[1,1].real]])
B = np.matrix([[0.5, 0.5, 0., 0.], [0.5, -0.5, 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
K_e = B*K_e*np.linalg.inv(B)
beta, E = np.linalg.eig(K_e)
Einv = np.linalg.inv(E)
return beta, E, Einv
def ExtinctionMatrix(self, scatterer, val, n0, theta, phi):
"""
Calculates Extinction matrix for layer with randomly oriented spheroids averaged over orientation
(and size distribution hopefully) in the forward scattering direction using Foldy's approximation
Parameters:
scatterer: instance of class pytmatrix.Scatterer
val (dict): dictionary containing input parameters that was passed to the VRT_module class
n0 (float): Total number concentration of particles per unit volume
theta (float): incidence angle in radians
phi (float): azimuth angle for incident direction in radians
Returns:
beta (1D array): An array containing the 4 eigenvalues of the extinction matrix K_e
E (2D array): A 2D array in which the columns are the 4 eigenvectors of the extinction matrix K_e
Einv (2D array): Inverse of the 2D array containg the 4 eigenvectors of the extinction matrix K_e
"""
geom = (np.rad2deg(theta), np.rad2deg(theta), np.rad2deg(phi), np.rad2deg(phi),
val["alpha"], val["beta"])
scatterer.set_geometry(geom)
if scatterer.psd_integrator != None:
scatterer.psd_integrator.geometries = (geom,)
scatterer.psd_integrator.init_scatter_table(scatterer, angular_integration=False)
SA_dyad = scatterer.get_S() # # fvv and fhh appear equal?
# # Tsang (1985) - eq. 5 in page 139 - attenuation rate matrix
M = (1j * 2 * np.pi * n0 / self.k) * SA_dyad
# M = n0 * SA_dyad
# M = 1j * 2 * np.pi / self.l1.k * SA_dyad
beta = self.StokesEigenValues(M)
E = self.StokesEigenVectors(M)
Einv = np.linalg.inv(E)
return beta, E, Einv
def StokesEigenValues(self, M):
"""
Calculate the eigenvalues for the Extinction matrix
Parameters:
M (2D array): A 2x2 matrix computed from the scattering matrix
Returns:
beta (1D array): A numpy array of eigenvalues for the extinction matrix.
"""
r = np.sqrt((M[0,0] - M[1,1])**2 + 4*M[1,0]*M[0,1])
K1 = self.k - ((1j/2) * (M[0,0] + M[1,1] + r))
K2 = self.k - ((1j/2) * (M[0,0] + M[1,1] - r))
beta = np.array([2*K1.imag, 1j*np.conj(K2) - 1j*K1, 1j*np.conj(K1) - 1j*K2, 2*K2.imag])
return beta
def StokesEigenVectors(self, M):
"""
Calculate the eigenvectors for the Extinction matrix
Parameters:
M (2D array): A 2x2 matrix computed from the scattering matrix
Returns:
E (1D array): A numpy array the columns of which are the eigenvectors of the extinction matrix.
"""
# # b1, b2, Mhv, Mvh should be 0 for high frequencies and non-polarizing components
r = np.sqrt((M[0,0] - M[1,1])**2 + 4*M[0,1]*M[1,0])
b1 = 2 * M[1,0] / (M[0,0] - M[1,1] + r)
b2 = 2 * M[0,1] / (-M[0,0] + M[1,1] - r)
E = np.matrix([[1, np.conj(b2), b2, np.abs(b2)**2],\
[np.abs(b1)**2, b1, np.conj(b1), 1],\
[2*b1.real, 1+(b1*np.conj(b2)), 1+(b2*np.conj(b1)), 2*b2.real], \
[-2*b1.imag, -1j * (1-(b1*np.conj(b2))), 1j * (1-(b2*np.conj(b1))), 2*b2.imag]])
return E
def D(self, beta, theta, d, kab = 0):
"""
Calculate a diagonal matrix in which the diagonal elements correspond to the
extincction cross section calculated using the eigen values of the extinction matrix
Parameters:
beta (1D array): Eigen values of the extinction matrix
theta (float): Incidence angle in radians
d (float): thickness of the layer containing the inclusions
Returns:
D (2D array): A diagonal matrix comprising extinction cross sections.
"""
D = np.diag(np.exp((beta+kab) * - d/ np.cos(theta)))
return D
def ExtinctionCS(self, scatterer, val, n0, theta, phi, pol=None):
if pol == None: pol = self.polH
# geom = (np.rad2deg(theta), np.rad2deg(theta), np.rad2deg(phi), np.rad2deg(phi),
# val["alpha"], val["beta"])
# scatterer.set_geometry(geom)
# if scatterer.psd_integrator != None:
# scatterer.psd_integrator.geometries = (geom,)
# scatterer.psd_integrator.init_scatter_table(scatterer, angular_integration=False)
SA_dyad = scatterer.get_S()
# # use S_vv for vertical pol and S_hh for horizontal pol
if pol == self.polV:
ext_cs = n0 * (4 * np.pi / self.k) * SA_dyad[0,0].imag
# sca_cs = self.l1.inclusions.n0 * 4 * np.pi * (np.abs(SA_dyad[0,0]) ** 2 + np.abs(SA_dyad[0,1]) ** 2)
elif pol == self.polH:
ext_cs = n0 * (4 * np.pi / self.k) * SA_dyad[1,1].imag
# sca_cs = self.l1.inclusions.n0 * 4 * np.pi * (np.abs(SA_dyad[1,0]) ** 2 + np.abs(SA_dyad[1,1]) ** 2)
# # make a diagonal matrix
K_e = np.zeros((4,4))
np.fill_diagonal(K_e, ext_cs)
return ext_cs, K_e
def ScatterCS(self, scatterer, theta, phi, pol):
geom = (np.rad2deg(theta), np.rad2deg(theta), np.rad2deg(phi), np.rad2deg(phi),
np.rad2deg(self.l1.inclusions.alpha), np.rad2deg(self.l1.inclusions.beta))
scatterer.set_geometry(geom,)
if scatterer.psd_integrator != None:
scatterer.psd_integrator.geometries = (geom,)
scatterer.psd_integrator.init_scatter_table(scatterer, angular_integration=True)
ssa = scatter.ssa(scatterer, True)
def ScatterInt(th, ph):
# (scatterer.phi, scatterer.thet) = (np.rad2deg(ph), np.rad2deg(th))
geom = (np.rad2deg(theta), np.rad2deg(th), np.rad2deg(phi), np.rad2deg(ph),
np.rad2deg(self.l1.inclusions.alpha), np.rad2deg(self.l1.inclusions.beta))
scatterer.set_geometry(geom,)
if scatterer.psd_integrator != None:
scatterer.psd_integrator.geometries = (geom,)
scatterer.psd_integrator.init_scatter_table(scatterer, angular_integration=False)
Z = scatterer.get_Z()
if pol == self.polV:
scat_int = Z[0,0] + Z[0,1]
elif pol == self.polH:
scat_int = Z[0,0] - Z[0,1]
return scat_int * np.sin(theta)
scat_cs, err = scipy.integrate.dblquad(ScatterInt, 0, 2*np.pi, lambda x: 0.0, lambda x: np.pi) # first set of limits for phi and second set of limits for theta
return scat_cs, err
def integrate_withM(self, beta, theta, matrix, limits=[0.,0.], depth=0., kab=0.):
n = len(beta)
matrix_int = np.zeros_like(matrix)
for i in range(n):
for j in range(n):
d = lambda z: np.exp(((beta[i]+kab) * (z + depth) / np.cos(theta))) * matrix[i,j]
matrix_int[i,j], err = scipy.integrate.quad(d, limits[0], limits[1])
return matrix_int
def integrate(self, beta, theta, limits=[0.,0.], depth=0., kab=0.):
"""
Integrates extinction matrix elements with resoect to 'd' / thickness.
Parameters:
beta (1D array): Eigen values of the extinction matrix
theta (float): Incidence angle in radians
limits (list): the lower and upper limits for integration
depth (float): thickness of the layer containing the inclusions
kab (float): absorption cross section of background medium
Returns:
D (2D array): A diagonal matrix with elements that are integrated in z direction
"""
n = len(beta)
D = np.zeros((n,n))
for i in range(n):
for j in range(n):
if i == j:
d = lambda z: np.exp(((beta[i]+kab) * (z + depth) / np.cos(theta)))
D[i,j], err = scipy.integrate.quad(d, limits[0], limits[1])
return D
def Mueller_bed(self, val, thetai_rad, thetat_rad, k_a_medium, T01_coh, T10_coh, R12, scat, n0=0):
"""
Compute the real-valued 4x4 Mueller matrix for scattering
from the first layer - substrate interface.
Parameters:
val (dict): dictionary containing input parameters that was passed to the VRT_module class
thetai_rad (float): Incidence angle in radians
thetat_rad (float): Transmission angle in radians
k_a_medium (float): absorption cross section of background medium
T01_coh (2D array): Transmission matrix describing coherent transmission from medium 0 to medium 1
T10_coh (2D array): Transmission matrix describing coherent transmission from medium 1 to medium 0
R12 (2D array): Reflection matrix describing coherent reflection at the boundary between medium 1 and medium 2
scat: instance of class pytmatrix.Scatterer
n0 (int or none): number of scatterers per unit volume
Returns:
M_bed (2D array): A 4x4 real-valued Mueller matrix describing total backscatterin from the bed
"""
# # keeping only the term with the the two coherent transmission matrice (term 2 of C2 in Fa et al. 2011)
beta_minus, E_minus, Einv_minus = self.ExtinctionMatrix(scat, val, n0, np.pi - thetat_rad, self.phi_i)
beta_plus, E_plus, Einv_plus = self.ExtinctionMatrix(scat, val, n0, thetat_rad, self.phi_s)
M_bed = np.linalg.multi_dot([T10_coh, E_plus, self.D(beta_plus, thetat_rad, val["d"], k_a_medium), Einv_plus, R12, E_minus, self.D(beta_minus, thetat_rad, val["d"], k_a_medium), Einv_minus, T01_coh])
return M_bed
def Mueller_bedvol(self, val, thetai_rad, thetat_rad, k_a_medium, T01_coh, T10_coh, R12_coh, scat, n0):
"""
Compute the real-valued 4x4 Mueller matrix for scattering from the
inclusions in the first layer (2) and first layer - substrate interface (1).
Parameters:
val (dict): dictionary containing input parameters that was passed to the VRT_module class
thetai_rad (float): Incidence angle in radians
thetat_rad (float): Transmission angle in radians
k_a_medium (float): absorption cross section of background medium
T01_coh (2D array): Transmission matrix describing coherent transmission from medium 0 to medium 1
T10_coh (2D array): Transmission matrix describing coherent transmission from medium 1 to medium 0
R12 (2D array): Reflection matrix describing coherent reflection at the boundary between medium 1 and medium 2
scat: instance of class pytmatrix.Scatterer
n0 (int or none): number of scatterers per unit volume
Returns:
M_bedvol (2D array): A 4x4 real-valued Mueller matrix
"""
# # Extinction due to scatterers
beta_minus, E_minus, Einv_minus = self.ExtinctionMatrix(scat, val, n0, np.pi - thetat_rad, self.phi_i)
beta_plus1, E_plus1, Einv_plus1 = self.ExtinctionMatrix(scat, val, n0, thetat_rad, self.phi_i)
beta_plus2, E_plus2, Einv_plus2 = self.ExtinctionMatrix(scat, val, n0, thetat_rad, self.phi_s)
Dminus = self.D(beta_minus, thetat_rad, val["d"], kab = k_a_medium)
Dplus1 = self.integrate(beta_plus1, thetat_rad, [-val["d"], 0], kab = k_a_medium, depth = -val["d"])
Dplus2 = self.integrate(beta_plus2, thetat_rad, [-val["d"], 0], kab = k_a_medium)
EDEminus = np.linalg.multi_dot([E_minus, Dminus, Einv_minus])
EDEplus1 = np.linalg.multi_dot([E_plus1, Dplus1, Einv_plus1])
EDEplus2 = np.linalg.multi_dot([E_plus2, Dplus2, Einv_plus2])
# # Phase matrix for scatterers
P = self.PhaseMatrix(scat, val, n0, np.pi/2 - thetat_rad, self.phi_i, np.pi/2 - thetat_rad, self.phi_s)
M_bedvol = np.linalg.multi_dot([T10_coh / np.cos(thetai_rad), EDEplus2, P, EDEplus1, R12_coh, EDEminus, T01_coh])
# Tsang formulation
# ext_term = (np.exp(-beta_plus2*val["d"]/np.cos(thetat_rad)) + np.exp(-beta_plus1*val["d"]/np.cos(thetat_rad))) \
# / ((beta_plus1/np.cos(thetat_rad)) + (beta_plus2/np.cos(thetat_rad)))
# M_bedvol = np.linalg.multi_dot([T10_coh / np.cos(thetai_rad), E_plus2, Einv_plus2, P, E_plus1, np.diag(ext_term), Einv_plus1, R12_coh, EDEminus, T01_coh])
return M_bedvol
def Mueller_volbed(self, val, thetai_rad, thetat_rad, k_a_medium, T01_coh, T10_coh, R12_coh, scat, n0):
"""
Compute the real-valued 4x4 Mueller matrix for scattering from the
inclusions in the first layer (1) and first layer - substrate interface (2).
Parameters:
val (dict): dictionary containing input parameters that was passed to the VRT_module class
thetai_rad (float): Incidence angle in radians
thetat_rad (float): Transmission angle in radians
k_a_medium (float): absorption cross section of background medium
T01_coh (2D array): Transmission matrix describing coherent transmission from medium 0 to medium 1
T10_coh (2D array): Transmission matrix describing coherent transmission from medium 1 to medium 0
R12 (2D array): Reflection matrix describing coherent reflection at the boundary between medium 1 and medium 2
scat: instance of class pytmatrix.Scatterer
n0 (int or none): number of scatterers per unit volume
Returns:
M_volbed (2D array): A 4x4 real-valued Mueller matrix
"""
# # Extinction due to scatterers
beta_plus, E_plus, Einv_plus = self.ExtinctionMatrix(scat, val, n0, thetat_rad, self.phi_s)
beta_minus1, E_minus1, Einv_minus1 = self.ExtinctionMatrix(scat, val, n0, np.pi - thetat_rad, self.phi_i)
beta_minus2, E_minus2, Einv_minus2 = self.ExtinctionMatrix(scat, val, n0, np.pi - thetat_rad, self.phi_s)
Dminus1 = self.integrate(beta_minus1, thetat_rad, [-val["d"], 0], kab = k_a_medium)
Dminus2 = self.integrate(beta_minus2, thetat_rad, [-val["d"], 0], kab = k_a_medium, depth = -val["d"])
Dplus = self.D(beta_plus, thetat_rad, val["d"], kab = k_a_medium)
EDEminus1 = np.linalg.multi_dot([E_minus1, Dminus1, Einv_minus1])
EDEminus2 = np.linalg.multi_dot([E_minus2, Dminus2, Einv_minus2])
EDEplus = np.linalg.multi_dot([E_plus, Dplus, Einv_plus])
# # Phase matrix for scatterers
P = self.PhaseMatrix(scat, val, n0, np.pi - (np.pi/2 - thetat_rad), self.phi_i, np.pi - (np.pi/2 - thetat_rad), self.phi_s)
M_volbed = np.linalg.multi_dot([T10_coh / np.cos(thetai_rad), EDEplus, R12_coh, EDEminus2, P, EDEminus1, T01_coh])
# # Tsang formulation
# ext_term = (np.exp(-beta_minus2*val["d"]/np.cos(thetat_rad)) + np.exp(-beta_minus1*val["d"]/np.cos(thetat_rad))) \
# / ((beta_minus1/np.cos(thetat_rad)) + (beta_minus2/np.cos(thetat_rad)))
# M_volbed = np.linalg.multi_dot([T10_coh / np.cos(thetai_rad), EDEplus, R12_coh, E_minus2, Einv_minus2, P, E_minus1, np.diag(ext_term), Einv_minus1, T01_coh])
return M_volbed
def Mueller_vol(self, val, thetai_rad, thetat_rad, k_a_medium, T01_coh, T10_coh, scat, n0=0):
"""
Compute the real-valued 4x4 Mueller matrix for scattering
from the inclusions in the first layer.
Parameters:
val (dict): dictionary containing input parameters that was passed to the VRT_module class
thetai_rad (float): Incidence angle in radians
thetat_rad (float): Transmission angle in radians
k_a_medium (float): absorption cross section of background medium
T01_coh (2D array): Transmission matrix describing coherent transmission from medium 0 to medium 1
T10_coh (2D array): Transmission matrix describing coherent transmission from medium 1 to medium 0
scat: instance of class pytmatrix.Scatterer
n0 (int or none): number of scatterers per unit volume
Returns:
M_vol (2D array): A 4x4 real-valued Mueller matrix describing scattering from discrete heterogeneities
"""
# # Extinction due to scatterers
beta_minus, E_minus, Einv_minus = self.ExtinctionMatrix(scat, val, n0, np.pi - thetat_rad, self.phi_i)
beta_plus, E_plus, Einv_plus = self.ExtinctionMatrix(scat, val, n0, thetat_rad, self.phi_s)
# if val["abyc"] == 1:
# k_eminus, K_Eminus = self.ExtinctionCS(scat, n0, np.pi - thetat_rad, self.phi_i)
# k_eplus, K_Eplus = self.ExtinctionCS(scat, n0, thetat_rad, self.phi_s)
# EDEminus = self.integrate(np.diagonal(K_Eminus), thetat_rad, [-val["d"], 0], k_a_medium)
# EDEplus = self.integrate(np.diagonal(K_Eplus), thetat_rad, [-val["d"], 0], k_a_medium)
# else:
Dminus = self.integrate(beta_minus, thetat_rad, [-val["d"], 0], kab = k_a_medium)
Dplus = self.integrate(beta_plus, thetat_rad, [-val["d"], 0], kab = k_a_medium)
EDEminus = np.linalg.multi_dot([E_minus, Dminus, Einv_minus])
EDEplus = np.linalg.multi_dot([E_plus, Dplus, Einv_plus])
# # Phase matrix for scatterers
P = self.PhaseMatrix(scat, val, n0, np.pi - thetat_rad, self.phi_i, thetat_rad, self.phi_s)
M_vol = np.linalg.multi_dot([T10_coh / np.cos(thetai_rad), EDEplus, P, EDEminus,T01_coh])
# # Tsang formulation
ext_term = (1 - np.exp(-(beta_plus*val["d"]/np.cos(thetat_rad))-(beta_minus*val["d"]/np.cos(thetat_rad))))/((beta_plus/np.cos(thetat_rad)) + (beta_minus/np.cos(thetat_rad)))
M_vol = np.linalg.multi_dot([T10_coh / np.cos(thetai_rad), E_plus,Einv_plus,P,E_minus,np.diag(ext_term),Einv_minus,T01_coh])
return M_vol
def intensity_breakdown(self, M, pol):
if pol == self.polH:
I_i = np.array([0, 1., 0., 0.]).reshape((4,1))
I_s = M * I_i
sigma = 4 * np.pi * np.cos(self.theta_s) * (I_s[1,0]/ I_i[1,0])
elif pol == self.polV:
I_i = np.array([1., 0, 0., 0.]).reshape((4,1))
I_s = M * I_i
sigma = 4 * np.pi * np.cos(self.theta_s) * (I_s[0,0]/ I_i[0,0])
return sigma
def I2EM_emissivity(self, thetai, eps1, eps2, s, cl):
"""
Compute the H and V rough surface emissivity using rough surface
reflectivity computed by the Improved Integral Equation Method.
Parameters:
thetai (float): Incidence angle (in radians)
eps1 (complex): complex permittivity of the upper medium
eps2 (complex): complex permittivity of the lower medium
s (float): RMS height for the interface between medium 1 and medium 2
cl (float): correlation length for the interface between medium 1 and medium 2
Returns:
e_v: Rough surface emissivity in V polarization
e_h: Rough surface emissivity in H polarization
"""
try:
thetai = thetai.item()
except:
pass
try:
s = s.item()
except:
pass
try:
cl = cl.item()
except:
pass
try:
eps = (eps2/eps1).item()
except:
eps = eps2/eps1
# # if float is not iterable error rises, check if the matlab function returns are separated by commas
eng = matlab.engine.start_matlab()
e_v, e_h = eng.I2EM_Emissivity_model(self.nu/1e9, s, cl, thetai, eps, 1, nargout=2) # using .item() to convert from numpy float64 to python scalar