-
Notifications
You must be signed in to change notification settings - Fork 7
/
ADA_Module.f90
2434 lines (2091 loc) · 100 KB
/
ADA_Module.f90
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
!
! ADA_Module
!
! Module containing the Adding Doubling Adding (ADA) radiative
! transfer solution procedures used in the CRTM.
!
!
! CREATION HISTORY:
! Written by: Quanhua Liu, QSS at JCSDA; [email protected]
! Yong Han, NOAA/NESDIS; [email protected]
! Paul van Delst; CIMMS/SSEC; [email protected]
! 08-Jun-2004
! Updated by: Quanhua Liu, NOAA/STAR: [email protected]
! 18-Dec-2021
! Updated by: Cheng Dang, UCAR, [email protected], Aug-2024
MODULE ADA_Module
! ------------------
! Environemnt set up
! ------------------
! Module use statements
USE RTV_Define
USE CRTM_Parameters
USE Type_Kinds
USE Message_Handler
USE CRTM_Utility
IMPLICIT NONE
! --------------------
! Default visibilities
! --------------------
! Everything private by default
PRIVATE
PUBLIC CRTM_ADA
PUBLIC CRTM_ADA_TL
PUBLIC CRTM_ADA_AD
PUBLIC CRTM_SurfRef
! -----------------
! Module parameters
! -----------------
CONTAINS
!################################################################################
!################################################################################
!## ##
!## ## PUBLIC MODULE ROUTINES ## ##
!## ##
!################################################################################
!################################################################################
!
SUBROUTINE CRTM_SurfRef(n_Layers,total_od,dire,Index_Sat_Angle,Rsphere,Ref_0,Ref_1,RTV, Error_Status)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n_Layers
TYPE(RTV_type), INTENT( INOUT ) :: RTV
REAL (fp), INTENT(IN) :: total_od
REAL (fp) :: Rsphere, Ref_0, Ref_1, Rfac, dire, SRef(3), S0a(3)
REAL (fp), DIMENSION(RTV%n_Angles*RTV%n_Stokes, RTV%n_Angles*RTV%n_Stokes) :: temporal_matrix,R0, T0, R2, R1
REAL (fp), DIMENSION( RTV%n_Angles*RTV%n_Stokes) :: refl_down, S0, Sun0
INTEGER :: i, j, k, L, Error_Status, nZ,Index_Sat_Angle
REAL(fp), PARAMETER :: rFactor(3) = (/ 0.0_fp, 0.5_fp, 1.0_fp/)
Sun0(:) = ZERO
DO i = 1, RTV%n_Angles
j = (i-1)*RTV%n_Stokes + 1
Sun0(j) = ONE
END DO
nZ = RTV%n_Angles * RTV%n_Stokes
j = (Index_Sat_Angle-1)*RTV%n_Stokes + 1
DO 101 L = 1, 2
R0(:,:) = RTV%s_Level_Refl_UP(1:nZ,1:nZ,n_Layers)*rFactor(L)
SRef(L) = dire*rFactor(L)
S0(:)=(ONE-dire)*RTV%Planck_Surface+SRef(L)*RTV%COS_SUN*RTV%Solar_irradiance/PI*exp(-total_od/RTV%COS_SUN)*Sun0(:)
T0(:,:) = RTV%s_Layer_Trans(1:nZ,1:nZ,n_Layers)
DO k = n_Layers, 1, -1
temporal_matrix = -matmul(R0,RTV%s_Layer_Refl(1:nZ,1:nZ,k))
DO i = 1, nZ
temporal_matrix(i,i) = ONE + temporal_matrix(i,i)
END DO
R1(1:nZ,1:nZ) = matinv(temporal_matrix, Error_Status)
R2 = matmul(RTV%s_Layer_Trans(1:nZ,1:nZ,k),R1)
IF(k < n_layers) T0 = matmul(R2,T0)
refl_down(1:nZ) = matmul(R0, RTV%s_Layer_Source_DOWN(1:nZ,k))
S0 = RTV%s_Layer_Source_UP(1:nZ,k)+ matmul(R2,refl_down(1:nZ) + S0)
R0=RTV%s_Layer_Refl(1:nZ,1:nZ,k)+matmul(matmul(R2,R0),RTV%s_Layer_Trans(1:nZ,1:nZ,k))
END DO
S0a(L) = S0(j)
101 CONTINUE
S0a(3) = RTV%s_Level_Rad_UP(j,0)
Ref_0 = S0a(1) - RTV%s_Level_Rad_UP(j,0)
Rfac = (S0a(2) - S0a(1))/(S0a(3) - S0a(1))
Rsphere = (Rfac*dire-SRef(2))/(SRef(2)*dire*(Rfac-ONE))
Ref_1 = (Rsphere - ONE/dire) * Ref_0
RETURN
END SUBROUTINE CRTM_SurfRef
!
SUBROUTINE CRTM_ADA(n_Layers, & ! Input number of atmospheric layers
w, & ! Input layer scattering albedo
T_OD, & ! Input layer optical depth
cosmic_background, & ! Input cosmic background radiance
emissivity, & ! Input surface emissivity
reflectivity, & ! Input surface reflectivity matrix
direct_reflectivity, & ! Input surface direct reflectivity
RTV, Error_Status) ! IN/Output upward radiance and others
! ------------------------------------------------------------------------- !
! FUNCTION: !
! This subroutine calculates IR/MW radiance at the top of the atmosphere !
! including atmospheric scattering. The scheme will include solar part. !
! The ADA algorithm computes layer reflectance and transmittance as well !
! as source function by the subroutine CRTM_Doubling_layer, then uses !
! an adding method to integrate the layer and surface components. !
! !
! Quanhua Liu [email protected] !
! ------------------------------------------------------------------------- !
IMPLICIT NONE
INTEGER, INTENT(IN) :: n_Layers
INTEGER nZ
TYPE(RTV_type), INTENT( INOUT ) :: RTV
REAL (fp), INTENT(IN), DIMENSION( : ) :: w,T_OD
REAL (fp), INTENT(IN), DIMENSION( : ) :: emissivity, direct_reflectivity
REAL (fp), INTENT(IN), DIMENSION( :,: ) :: reflectivity
REAL (fp), INTENT(IN) :: cosmic_background
! -------------- internal variables --------------------------------- !
! Abbreviations: !
! s: scattering, rad: radiance, trans: transmission, !
! refl: reflection, up: upward, down: downward !
! --------------------------------------------------------------------!
REAL (fp), DIMENSION(RTV%n_Angles*RTV%n_Stokes, RTV%n_Angles*RTV%n_Stokes) :: temporal_matrix
REAL (fp), DIMENSION( RTV%n_Angles*RTV%n_Stokes, n_Layers) :: refl_down
REAL (fp), DIMENSION(RTV%n_Angles*RTV%n_Stokes) :: temporal_vector
REAL (fp), DIMENSION(0:n_Layers) :: total_opt
INTEGER :: i, j, k, Error_Status
CHARACTER(*), PARAMETER :: ROUTINE_NAME = 'CRTM_ADA'
CHARACTER(256) :: Message
! Total optical depth from the layer to TOA
! ... Zero at TOA
total_opt(0) = ZERO
! ... Below TOA
DO k = 1, n_Layers
total_opt(k) = total_opt(k-1) + T_OD(k)
END DO
! Variable initialization
nZ = RTV%n_Angles * RTV%n_Stokes
RTV%s_Layer_Trans = ZERO
RTV%s_Layer_Refl = ZERO
RTV%s_Level_Refl_UP = ZERO
RTV%s_Level_Rad_UP = ZERO
RTV%s_Layer_Source_UP = ZERO
RTV%s_Layer_Source_DOWN = ZERO
refl_down = ZERO
temporal_matrix = ZERO
! Boundary conditions for the bottom layer
! ... Surface reflectivity
RTV%s_Level_Refl_UP(1:nZ,1:nZ,n_Layers)=reflectivity(1:nZ,1:nZ)
! ... Upwelling/surface-leaving radiance
! ... (a) Radiance emitted by the surface
IF( RTV%mth_Azi == 0 ) THEN
RTV%s_Level_Rad_UP(1:nZ,n_Layers ) = emissivity(1:nZ)*RTV%Planck_Surface
END IF
! ... (b) Direct solar radiance reflected by the surface
IF( RTV%Solar_Flag_true ) THEN
RTV%s_Level_Rad_UP(1:nZ,n_Layers ) = RTV%s_Level_Rad_UP(1:nZ,n_Layers )+direct_reflectivity(1:nZ)* &
RTV%COS_SUN*RTV%Solar_irradiance/PI*exp(-total_opt(n_Layers)/RTV%COS_SUN)
END IF
! 1. CRTM DEFAULT OUTPUT: Top-of-Atmosphere leaving radiance
! UPWARD ADDING LOOP STARTS FROM BOTTOM LAYER TO ATMOSPHERIC TOP LAYER.
DO 10 k = n_Layers, 1, -1
! Compute tranmission and reflection matrices for a layer
IF(w(k) > SCATTERING_ALBEDO_THRESHOLD .and. maxval(abs(RTV%Pff(1:nZ,1:nZ,k))) > ZERO) THEN
! ... Case 1, with solar scattering
! ----------------------------------------------------------- !
! CALL multiple-stream algorithm for computing layer !
! transmission, reflection, and source functions. !
! ----------------------------------------------------------- !
CALL CRTM_AMOM_layer( &
RTV%n_Streams, &
nZ,k,w(k), &
T_OD(k), &
total_opt(k-1), &
RTV%COS_AngleS(1:nZ), & ! Input
RTV%COS_WeightS(1:nZ), &
RTV%Pff(:,:,k), &
RTV%Pbb(:,:,k), & ! Input
RTV%Planck_Atmosphere(k), & ! Input
RTV, Error_Status ) ! Internal variable
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in CALL CRTM_AMOM_layer ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
! ----------------------------------------------------------- !
! Adding method to add the layer to the present level !
! to compute upward radiances and reflection matrix !
! at new level. !
! ----------------------------------------------------------- !
! Reference Liu and Lu, 2016, book chapter,
! "Community Radiative Transfer Model for Air Quality Studies"
! Equation 16(a-b)
! - R_k * r_k
temporal_matrix = -matmul(RTV%s_Level_Refl_UP(1:nZ,1:nZ,k), &
RTV%s_Layer_Refl(1:nZ,1:nZ,k))
! E - R_k * r_k
DO i = 1, nZ
temporal_matrix(i,i) = ONE + temporal_matrix(i,i)
END DO
! matinv(E - R_k * r_k)
RTV%Inv_Gamma(1:nZ,1:nZ,k) = matinv(temporal_matrix, Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv(temporal_matrix, Error_Status) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
! t_k * matinv(E - R_k * r_k)
RTV%Inv_GammaT(1:nZ,1:nZ,k) = &
matmul(RTV%s_Layer_Trans(1:nZ,1:nZ,k), RTV%Inv_Gamma(1:nZ,1:nZ,k))
! R_k * Sd_k
refl_down(1:nZ,k) = matmul(RTV%s_Level_Refl_UP(1:nZ,1:nZ,k), &
RTV%s_Layer_Source_DOWN(1:nZ,k))
! 16b: I_k-1 = Su_k + [t_k * matinv(E - R_k * r_k)] * (R_k * Sd_k + I_k)
RTV%s_Level_Rad_UP(1:nZ,k-1 )=RTV%s_Layer_Source_UP(1:nZ,k)+ &
matmul(RTV%Inv_GammaT(1:nZ,1:nZ,k),refl_down(1:nZ,k) &
+RTV%s_Level_Rad_UP(1:nZ,k ))
! R_k * t_k
RTV%Refl_Trans(1:nZ,1:nZ,k) = matmul(RTV%s_Level_Refl_UP(1:nZ,1:nZ,k), &
RTV%s_Layer_Trans(1:nZ,1:nZ,k))
! 16a: r_k-1 = r_k + [t_k * matinv(E - R_k * r_k) * (R_k * t_k)]
RTV%s_Level_Refl_UP(1:nZ,1:nZ,k-1)=RTV%s_Layer_Refl(1:nZ,1:nZ,k) + &
matmul(RTV%Inv_GammaT(1:nZ,1:nZ,k),RTV%Refl_Trans(1:nZ,1:nZ,k))
ELSE
! ... case 2, absorption/emission only
DO i = 1, nZ
RTV%s_Layer_Trans(i,i,k) = exp(-T_OD(k)/RTV%COS_AngleS(i))
END DO
DO i = 1, nZ, RTV%n_Stokes
RTV%s_Layer_Source_UP(i,k) = RTV%Planck_Atmosphere(k) * (ONE - RTV%s_Layer_Trans(i,i,k) )
RTV%s_Layer_Source_DOWN(i,k) = RTV%s_Layer_Source_UP(i,k)
END DO
! Adding method
DO i = 1, nZ
RTV%s_Level_Rad_UP(i,k-1 )=RTV%s_Layer_Source_UP(i,k)+ &
RTV%s_Layer_Trans(i,i,k)*(sum(RTV%s_Level_Refl_UP(i,1:nZ,k)*RTV%s_Layer_Source_DOWN(1:nZ,k)) &
+RTV%s_Level_Rad_UP(i,k ))
END DO
DO i = 1, nZ
DO j = 1, nZ
RTV%s_Level_Refl_UP(i,j,k-1)=RTV%s_Layer_Trans(i,i,k)*RTV%s_Level_Refl_UP(i,j,k)*RTV%s_Layer_Trans(j,j,k)
END DO
END DO
ENDIF
10 CONTINUE
! Adding reflected cosmic background radiation
IF( RTV%mth_Azi == 0 ) THEN
DO i = 1, nZ, RTV%n_Stokes
RTV%s_Level_Rad_UP(i,0)=RTV%s_Level_Rad_UP(i,0)+sum(RTV%s_Level_Refl_UP(i,1:nZ,0))*cosmic_background
ENDDO
END IF
! 2. USER-DEFINED
! Upwelling radiance at aircraft-level, flag RTV%aircraft%rt
! OR downwelling radiance at user-defined level, flag RTV%obs_4_downward%rt
IF(RTV%aircraft%rt.or.RTV%obs_4_downward%rt) THEN
! --- note by Mark ----
! Added, May 20, 2024
! Except at TOA, RTV%s_Level_Rad_UP is "intermediate" value, the following part for final vertical profiles of radiance
! Downward, finalize s_Level_Rad_UPT and s_Level_Rad_DOWNT
! --------------------
! Boundary conditions for the top layer
! ... Upwelling radiance at TOA
RTV%s_Level_Rad_UPT(:,0) = RTV%s_Level_Rad_UP(:,0)
! ... Downwelling radiance at TOA
IF( RTV%mth_Azi == 0 ) THEN
DO i = 1, nZ, RTV%n_Stokes
RTV%s_Level_Rad_DOWN(i,0)=cosmic_background
RTV%s_Level_Rad_DOWNT(i,0)=cosmic_background
ENDDO
END IF
! ... Downward reflectivity from the space
RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,0) = ZERO
! DOWNWARD ADDING LOOP STARTS FROM ATMOSPHER TOP TO BOTTOM LAYER
DO 20 k = 1, n_Layers
! ... Case 1, with solar scattering
! Compute tranmission and reflection matrices for a layer
IF(w(k) > SCATTERING_ALBEDO_THRESHOLD .and. maxval(abs(RTV%Pff(1:nZ,1:nZ,k))) > ZERO) THEN
! ----------------------------------------------------------- !
! Adding method to add the layer to the present level !
! to compute upward radiances and reflection matrix !
! at new level. !
! ----------------------------------------------------------- !
! - R_k-1 * r_k
temporal_matrix = -matmul(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k-1), &
RTV%s_Layer_Refl(1:nZ,1:nZ,k) )
! E - R_k-1 * r_k
DO i = 1, nZ
temporal_matrix(i,i) = ONE + temporal_matrix(i,i)
END DO
! matinv(E - R_k-1 * r_k)
RTV%Inv_Gamma2(1:nZ,1:nZ,k) = matinv(temporal_matrix, Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv in Inv_Gamma2 ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
! t(k) * matinv(E - R_k-1 * r_k)
RTV%Inv_Gamma2T(1:nZ,1:nZ,k) = matmul(RTV%s_Layer_Trans(1:nZ,1:nZ,k), &
RTV%Inv_Gamma2(1:nZ,1:nZ,k))
! R_k-1 * Sd_k
refl_down(1:nZ,k) = matmul(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k-1), &
RTV%s_Layer_Source_UP(1:nZ,k))
! Radiance: I_k = Su_k + [t(k) * matinv(E - R_k-1 * r_k)] * [R_k-1 * Sd_k + I_k-1]
RTV%s_Level_Rad_DOWN(1:nZ,k) = RTV%s_Layer_Source_DOWN(1:nZ,k) &
+ matmul(RTV%Inv_Gamma2T(1:nZ,1:nZ,k), &
refl_down(1:nZ,k) + RTV%s_Level_Rad_DOWN(1:nZ,k-1))
! R_k-1 * t_k
RTV%Refl_Trans_DOWN(1:nZ,1:nZ,k) = &
matmul(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k-1), &
RTV%s_Layer_Trans(1:nZ,1:nZ,k))
! R_k = r_k + [t(k) * matinv(E - R_k-1 * r_k)] * [R_k-1 * t_k]
RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k)= RTV%s_Layer_Refl(1:nZ,1:nZ,k) &
+ matmul(RTV%Inv_Gamma2T(1:nZ,1:nZ,k),&
RTV%Refl_Trans_DOWN(1:nZ,1:nZ,k))
ELSE
! Adding method for absorption layer, no solar diffuse radiation for no scattering layer
DO i = 1, nZ
RTV%s_Level_Rad_DOWN(i,k)=RTV%s_Layer_Source_DOWN(i,k)+ &
RTV%s_Layer_Trans(i,i,k)*(sum(RTV%s_Level_Refl_DOWN(i,1:nZ,k-1)*RTV%s_Layer_Source_UP(1:nZ,k)) &
+RTV%s_Level_Rad_DOWN(i,k-1))
END DO
DO i = 1, nZ
DO j = 1, nZ
RTV%s_Level_Refl_DOWN(i,j,k)=RTV%s_Layer_Trans(i,i,k)*RTV%s_Level_Refl_DOWN(i,j,k-1)*RTV%s_Layer_Trans(j,j,k)
END DO
END DO
temporal_vector = matmul(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k-1),RTV%s_Level_Rad_UP(1:nZ,k) )
RTV%s_Level_Rad_UPT(1:nZ,k)= temporal_vector + RTV%s_Level_Rad_UP(1:nZ,k)
temporal_vector = matmul(RTV%s_Level_Refl_UP(1:nZ,1:nZ,k), &
RTV%s_Level_Rad_DOWN(1:nZ,k))
RTV%s_Level_Rad_UPT(1:nZ,k) = temporal_vector + RTV%s_Level_Rad_UP(1:nZ,k)
END IF
!
! finalize upward and downward radiance s_Level_Rad_UPT, s_Level_Rad_DOWNT
IF (maxval(abs(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k))) > ZERO) THEN
temporal_matrix = -matmul(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k), &
RTV%s_Level_Refl_UP(1:nZ,1:nZ,k))
DO i = 1, nZ
temporal_matrix(i,i) = ONE + temporal_matrix(i,i)
END DO
RTV%Inv_Gamma3(1:nZ,1:nZ,k) = matinv(temporal_matrix, Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv in Inv_Gamma3 ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
RTV%s_Level_Rad_DOWNT(1:nZ,k)= matmul( RTV%Inv_Gamma3(1:nZ,1:nZ,k), &
matmul(RTV%s_Level_Refl_DOWN(1:nZ,1:nZ,k), RTV%s_Level_Rad_UP(1:nZ,k)) &
+ RTV%s_Level_Rad_DOWN(1:nZ,k) )
temporal_vector = matmul(RTV%Inv_Gamma3(1:nZ,1:nZ,k),RTV%s_Level_Rad_DOWN(1:nZ,k))
RTV%s_Level_Rad_UPT(1:nZ,k)= matmul(RTV%s_Level_Refl_UP(1:nZ,1:nZ,k),temporal_vector) &
+ matmul(RTV%Inv_Gamma3(1:nZ,1:nZ,k),RTV%s_Level_Rad_UP(1:nZ,k))
ELSE
RTV%s_Level_Rad_DOWNT(1:nZ,k)= RTV%s_Level_Rad_DOWN(1:nZ,k)
RTV%s_Level_Rad_UPT(1:nZ,k) = &
matmul(RTV%s_Level_Refl_UP(1:nZ,1:nZ,k),RTV%s_Level_Rad_DOWN(1:nZ,k)) &
+ RTV%s_Level_Rad_UP(1:nZ,k)
END IF
20 CONTINUE
RTV%s_Level_Rad_DOWN = RTV%s_Level_Rad_DOWNT
RTV%s_Level_Rad_UP = RTV%s_Level_Rad_UPT
END IF !IF(RTV%aircraft%rt.or.RTV%obs_4_downward%rt)
RETURN
END SUBROUTINE CRTM_ADA
!
!
!
SUBROUTINE MOM(KL,nZ,optical_depth,trans,refl,RTV, Error_Status)
IMPLICIT NONE
TYPE(RTV_type), INTENT( INOUT ) :: RTV
INTEGER, INTENT(IN) :: nZ, KL
INTEGER :: Error_Status
REAL(fp), INTENT(IN) :: optical_depth
REAL(fp), INTENT(OUT), DIMENSION(:,:) :: trans, refl
! local variables
INTEGER :: i, j
CHARACTER(256) :: Message
CHARACTER(*), PARAMETER :: ROUTINE_NAME = 'MOM'
REAL(fp) :: xx
REAL(fp), DIMENSION(nZ,nZ) :: tempo
RTV%PPM(1:nZ,1:nZ,KL) = RTV%PP(1:nZ,1:nZ,KL) - RTV%PM(1:nZ,1:nZ,KL)
RTV%i_PPM(1:nZ,1:nZ,KL) = matinv( RTV%PPM(1:nZ,1:nZ,KL), Error_Status )
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv( RTV%PPM(1:nZ,1:nZ,KL), Error_Status ) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
RTV%PPP(1:nZ,1:nZ,KL) = RTV%PP(1:nZ,1:nZ,KL) + RTV%PM(1:nZ,1:nZ,KL)
RTV%HH(1:nZ,1:nZ,KL) = matmul( RTV%PPM(1:nZ,1:nZ,KL), RTV%PPP(1:nZ,1:nZ,KL) )
!
! save phase element RTV%HH, call ASYMTX for calculating eigenvalue and vectors.
tempo = RTV%HH(1:nZ,1:nZ,KL)
CALL ASYMTX(tempo,nZ,nZ,nZ,RTV%EigVe(1:nZ,1:nZ,KL),RTV%EigVa(1:nZ,KL),Error_Status)
DO i = 1, nZ
IF( RTV%EigVa(i,KL) > ZERO ) THEN
RTV%EigValue(i,KL) = sqrt( RTV%EigVa(i,KL) )
ELSE
RTV%EigValue(i,KL) = ZERO
END IF
END DO
DO i = 1, nZ
DO j = 1, nZ
RTV%EigVeVa(i,j,KL) = RTV%EigVe(i,j,KL) * RTV%EigValue(j,KL)
END DO
END DO
RTV%EigVeF(1:nZ,1:nZ,KL) = matmul( RTV%i_PPM(1:nZ,1:nZ,KL), RTV%EigVeVa(1:nZ,1:nZ,KL) )
! compute layer reflection, transmission and source function
RTV%Gp(1:nZ,1:nZ,KL) = ( RTV%EigVe(1:nZ,1:nZ,KL) + RTV%EigVeF(1:nZ,1:nZ,KL) )/2.0_fp
RTV%Gm(1:nZ,1:nZ,KL) = ( RTV%EigVe(1:nZ,1:nZ,KL) - RTV%EigVeF(1:nZ,1:nZ,KL) )/2.0_fp
RTV%i_Gm(1:nZ,1:nZ,KL) = matinv( RTV%Gm(1:nZ,1:nZ,KL), Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv( RTV%Gm(1:nZ,1:nZ,KL), Error_Status) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
DO i = 1, nZ
xx = RTV%EigValue(i,KL)*optical_depth
RTV%Exp_x(i,KL) = exp(-xx)
END DO
DO i = 1, nZ
DO j = 1, nZ
RTV%A1(i,j,KL) = RTV%Gp(i,j,KL) * RTV%Exp_x(j,KL)
RTV%A4(i,j,KL) = RTV%Gm(i,j,KL) * RTV%Exp_x(j,KL)
END DO
END DO
RTV%A2(1:nZ,1:nZ,KL) = matmul( RTV%i_Gm(1:nZ,1:nZ,KL), RTV%A1(1:nZ,1:nZ,KL) )
RTV%A3(1:nZ,1:nZ,KL) = matmul( RTV%Gp(1:nZ,1:nZ,KL), RTV%A2(1:nZ,1:nZ,KL) )
RTV%A5(1:nZ,1:nZ,KL) = matmul( RTV%A1(1:nZ,1:nZ,KL), RTV%A2(1:nZ,1:nZ,KL) )
RTV%A6(1:nZ,1:nZ,KL) = matmul( RTV%A4(1:nZ,1:nZ,KL), RTV%A2(1:nZ,1:nZ,KL) )
RTV%Gm_A5(1:nZ,1:nZ,KL) = RTV%Gm(1:nZ,1:nZ,KL) - RTV%A5(1:nZ,1:nZ,KL)
RTV%i_Gm_A5(1:nZ,1:nZ,KL) = matinv(RTV%Gm_A5(1:nZ,1:nZ,KL), Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv(RTV%Gm_A5(1:nZ,1:nZ,KL), Error_Status) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
trans = matmul( RTV%A4(1:nZ,1:nZ,KL) - RTV%A3(1:nZ,1:nZ,KL), RTV%i_Gm_A5(1:nZ,1:nZ,KL) )
refl = matmul( RTV%Gp(1:nZ,1:nZ,KL) - RTV%A6(1:nZ,1:nZ,KL), RTV%i_Gm_A5(1:nZ,1:nZ,KL) )
RETURN
END SUBROUTINE MOM
!
!
SUBROUTINE MOM_TL(KL,nZ,optical_depth,RTV,optical_depth_TL,PP_TL,PM_TL,trans_TL,refl_TL,Error_Status)
IMPLICIT NONE
TYPE(RTV_type), INTENT( IN ) :: RTV
INTEGER, INTENT(IN) :: nZ, KL
INTEGER :: Error_Status
REAL(fp), INTENT(IN) :: optical_depth, optical_depth_TL
REAL(fp), INTENT(IN), DIMENSION(:,:) :: PP_TL, PM_TL
REAL(fp), INTENT(OUT), DIMENSION(:,:) :: trans_TL,refl_TL
! local variables
INTEGER :: i, j
CHARACTER(*), PARAMETER :: ROUTINE_NAME = 'MOM_TL'
REAL(fp) :: xx_TL
REAL(fp), DIMENSION(nZ,nZ) :: PPM_TL, i_PPM_TL, PPP_TL, HH_TL
REAL(fp), DIMENSION(nZ,nZ) :: EigVeF_TL,Gp_TL,Gm_TL,A1_TL,A2_TL,A3_TL,A4_TL,A5_TL,A6_TL
REAL(fp), DIMENSION(nZ,nZ) :: Gm_A5_TL,i_Gm_A5_TL,EigVe_TL,EigVeVa_TL,i_Gm_TL
REAL(fp), DIMENSION(nZ) :: Exp_x_TL, EigVa_TL, EigValue_TL
PPM_TL(:,:) = PP_TL(:,:) - PM_TL(:,:)
i_PPM_TL(:,:) = - matmul( RTV%i_PPM(1:nZ,1:nZ,KL), matmul(PPM_TL(:,:),RTV%i_PPM(1:nZ,1:nZ,KL)) )
PPP_TL(:,:) = PP_TL(:,:) + PM_TL(:,:)
HH_TL(:,:) = matmul( PPM_TL(:,:), RTV%PPP(1:nZ,1:nZ,KL) )+matmul( RTV%PPM(1:nZ,1:nZ,KL), PPP_TL(:,:) )
!
! compute TL eigenvectors EigVe, and eigenvalues EigVa
CALL ASYMTX_TL(nZ,RTV%EigVe(1:nZ,1:nZ,KL),RTV%EigVa(1:nZ,KL),HH_TL, &
EigVe_TL,EigVa_TL,Error_Status)
DO i = 1, nZ
IF( RTV%EigVa(i,KL) > ZERO ) THEN
EigValue_TL(i) = 0.5_fp*EigVa_TL(i)/RTV%EigValue(i,KL)
ELSE
EigValue_TL(i) = ZERO
END IF
END DO
EigVeVa_TL = ZERO
DO i = 1, nZ
DO j = 1, nZ
EigVeVa_TL(i,j) = EigVe_TL(i,j) * RTV%EigValue(j,KL)+RTV%EigVe(i,j,KL) * EigValue_TL(j)
END DO
END DO
EigVeF_TL(:,:) = matmul( i_PPM_TL(:,:), RTV%EigVeVa(1:nZ,1:nZ,KL) ) &
+ matmul( RTV%i_PPM(1:nZ,1:nZ,KL), EigVeVa_TL(:,:) )
!
! compute TL reflection and transmission matrices, TL source function
Gp_TL(:,:) = ( EigVe_TL(:,:) + EigVeF_TL(:,:) )/2.0_fp
Gm_TL(:,:) = ( EigVe_TL(:,:) - EigVeF_TL(:,:) )/2.0_fp
i_Gm_TL = -matmul( RTV%i_Gm(1:nZ,1:nZ,KL), matmul(Gm_TL,RTV%i_Gm(1:nZ,1:nZ,KL)) )
DO i = 1, nZ
xx_TL = EigValue_TL(i)*optical_depth+RTV%EigValue(i,KL)*optical_depth_TL
Exp_x_TL(i) = -xx_TL*RTV%Exp_x(i,KL)
END DO
DO i = 1, nZ
DO j = 1, nZ
A1_TL(i,j) = Gp_TL(i,j)* RTV%Exp_x(j,KL)+ RTV%Gp(i,j,KL)* Exp_x_TL(j)
A4_TL(i,j) = Gm_TL(i,j)* RTV%Exp_x(j,KL)+ RTV%Gm(i,j,KL)* Exp_x_TL(j)
END DO
END DO
A2_TL(:,:) = matmul(i_Gm_TL(:,:),RTV%A1(1:nZ,1:nZ,KL))+matmul(RTV%i_Gm(1:nZ,1:nZ,KL),A1_TL(:,:))
A3_TL(:,:) = matmul(Gp_TL(:,:),RTV%A2(1:nZ,1:nZ,KL))+matmul(RTV%Gp(1:nZ,1:nZ,KL),A2_TL(:,:))
A5_TL(:,:) = matmul(A1_TL(:,:),RTV%A2(1:nZ,1:nZ,KL))+matmul(RTV%A1(1:nZ,1:nZ,KL),A2_TL(:,:))
A6_TL(:,:) = matmul(A4_TL(:,:),RTV%A2(1:nZ,1:nZ,KL))+matmul(RTV%A4(1:nZ,1:nZ,KL),A2_TL(:,:))
Gm_A5_TL(:,:) = Gm_TL(:,:) - A5_TL(:,:)
i_Gm_A5_TL(:,:) = -matmul( RTV%i_Gm_A5(1:nZ,1:nZ,KL),matmul(Gm_A5_TL,RTV%i_Gm_A5(1:nZ,1:nZ,KL)))
!
! T = matmul( RTV%A4(:,:,KL) - RTV%A3(:,:,KL), RTV%i_Gm_A5(:,:,KL) )
trans_TL = matmul( A4_TL(:,:) - A3_TL(:,:), RTV%i_Gm_A5(1:nZ,1:nZ,KL) ) &
+ matmul( RTV%A4(1:nZ,1:nZ,KL) - RTV%A3(1:nZ,1:nZ,KL), i_Gm_A5_TL(:,:) )
refl_TL = matmul( Gp_TL(:,:) - A6_TL(:,:), RTV%i_Gm_A5(1:nZ,1:nZ,KL) ) &
+ matmul( RTV%Gp(1:nZ,1:nZ,KL) - RTV%A6(1:nZ,1:nZ,KL), i_Gm_A5_TL(:,:) )
RETURN
END SUBROUTINE MOM_TL
!
!
SUBROUTINE MOM_AD(KL,nZ,optical_depth,RTV, optical_depth_AD,PP_AD,PM_AD,trans_AD,refl_AD )
IMPLICIT NONE
TYPE(RTV_type), INTENT( IN ) :: RTV
INTEGER, INTENT(IN) :: nZ, KL
INTEGER :: Error_Status
REAL(fp), INTENT(IN) :: optical_depth
REAL(fp), INTENT(IN), DIMENSION(:,:) :: trans_AD,refl_AD
REAL(fp), INTENT(INOUT) :: optical_depth_AD
REAL(fp), INTENT(OUT), DIMENSION(:,:) :: PP_AD, PM_AD
! local variables
INTEGER :: i, j
CHARACTER(*), PARAMETER :: ROUTINE_NAME = 'MOM_TL'
REAL(fp) :: xx_AD
REAL(fp), DIMENSION(nZ) :: Exp_x_AD,EigValue_AD,EigVa_AD
REAL(fp), DIMENSION(nZ,nZ) :: i_Gm_A5_AD,Gp_AD,A6_AD,A3_AD,A5_AD,Gm_A5_AD,Gm_AD,A2_AD,HH_AD,PPP_AD
REAL(fp), DIMENSION(nZ,nZ) :: i_Gm_AD,A1_AD,A4_AD,EigVe_AD, EigVeF_AD,i_PPM_AD,EigVeVa_AD,PPM_AD
i_Gm_A5_AD = matmul( transpose(RTV%Gp(1:nZ,1:nZ,KL)-RTV%A6(1:nZ,1:nZ,KL)),refl_AD )
Gp_AD(:,:) = matmul( refl_AD, transpose(RTV%i_Gm_A5(1:nZ,1:nZ,KL)) )
A6_AD = - GP_AD
i_Gm_A5_AD = i_Gm_A5_AD + matmul( transpose(RTV%A4(1:nZ,1:nZ,KL)-RTV%A3(1:nZ,1:nZ,KL)),trans_AD )
A4_AD(:,:) = matmul( trans_AD, transpose(RTV%i_Gm_A5(1:nZ,1:nZ,KL)) )
A3_AD = - A4_AD
Gm_A5_AD = -matmul( transpose(RTV%i_Gm_A5(1:nZ,1:nZ,KL)) ,matmul( i_Gm_A5_AD,transpose(RTV%i_Gm_A5(1:nZ,1:nZ,KL)) ) )
Gm_AD = Gm_A5_AD
A5_AD = - Gm_A5_AD
A4_AD = A4_AD + matmul( A6_AD(:,:), transpose(RTV%A2(1:nZ,1:nZ,KL)) )
A2_AD = matmul( transpose(RTV%A4(1:nZ,1:nZ,KL)),A6_AD(:,:) )
A1_AD = matmul( A5_AD(:,:), transpose(RTV%A2(1:nZ,1:nZ,KL)) )
A2_AD = A2_AD + matmul( transpose(RTV%A1(1:nZ,1:nZ,KL)), A5_AD(:,:) )
Gp_AD = Gp_AD + matmul( A3_AD(:,:), transpose(RTV%A2(1:nZ,1:nZ,KL)) )
A2_AD = A2_AD + matmul( transpose(RTV%Gp(1:nZ,1:nZ,KL)),A3_AD(:,:) )
i_Gm_AD = matmul( A2_AD(:,:), transpose(RTV%A1(1:nZ,1:nZ,KL)) )
A1_AD = A1_AD + matmul( transpose(RTV%i_Gm(1:nZ,1:nZ,KL)), A2_AD(:,:) )
Exp_x_AD = ZERO
DO i = nZ, 1, -1
DO j = nZ, 1, -1
Gm_AD(i,j) = Gm_AD(i,j) + A4_AD(i,j)* RTV%Exp_x(j,KL)
Exp_x_AD(j) = Exp_x_AD(j) + RTV%Gm(i,j,KL)*A4_AD(i,j)
Gp_AD(i,j) = Gp_AD(i,j) + A1_AD(i,j)* RTV%Exp_x(j,KL)
Exp_x_AD(j) = Exp_x_AD(j) + RTV%Gp(i,j,KL)*A1_AD(i,j)
END DO
END DO
DO i = nZ, 1, -1
xx_AD = -Exp_x_AD(i)*RTV%Exp_x(i,KL)
Exp_x_AD(i) = ZERO
EigValue_AD(i) = xx_AD*optical_depth
optical_depth_AD = optical_depth_AD + RTV%EigValue(i,KL)*xx_AD
END DO
Gm_AD = Gm_AD -matmul( transpose(RTV%i_Gm(1:nZ,1:nZ,KL)), matmul( i_Gm_AD, transpose(RTV%i_Gm(1:nZ,1:nZ,KL)) ) )
EigVe_AD(:,:) = Gm_AD(:,:)/2.0_fp
EigVeF_AD(:,:) = - Gm_AD(:,:)/2.0_fp
EigVe_AD = EigVe_AD + Gp_AD(:,:)/2.0_fp
EigVeF_AD = EigVeF_AD + Gp_AD(:,:)/2.0_fp
i_PPM_AD(:,:) = matmul( EigVeF_AD(:,:), transpose(RTV%EigVeVa(1:nZ,1:nZ,KL)) )
EigVeVa_AD(:,:) = matmul( transpose(RTV%i_PPM(1:nZ,1:nZ,KL)), EigVeF_AD(:,:) )
DO i = nZ, 1, -1
DO j = nZ, 1, -1
EigVe_AD(i,j)=EigVe_AD(i,j)+EigVeVa_AD(i,j)* RTV%EigValue(j,KL)
EigValue_AD(j) = EigValue_AD(j)+RTV%EigVe(i,j,KL)*EigVeVa_AD(i,j)
END DO
END DO
DO i = nZ, 1, -1
IF( RTV%EigVa(i,KL) > ZERO ) THEN
EigVa_AD(i) = 0.5_fp*EigValue_AD(i)/RTV%EigValue(i,KL)
ELSE
EigValue_AD(i) = ZERO
EigVa_AD(i) = ZERO
END IF
END DO
! compute eigenvectors EigVe, and eigenvalues EigVa
CALL ASYMTX_AD(nZ,RTV%EigVe(1:nZ,1:nZ,KL),RTV%EigVa(1:nZ,KL), &
EigVe_AD,EigVa_AD,HH_AD,Error_Status)
PPM_AD(:,:) = matmul( HH_AD(:,:), transpose(RTV%PPP(1:nZ,1:nZ,KL)) )
PPP_AD(:,:) = matmul( transpose(RTV%PPM(1:nZ,1:nZ,KL)), HH_AD(:,:) )
PP_AD = PPP_AD
PM_AD = PPP_AD
PPM_AD(:,:) = PPM_AD(:,:)-matmul( transpose(RTV%i_PPM(1:nZ,1:nZ,KL)),matmul(i_PPM_AD(:,:),transpose(RTV%i_PPM(1:nZ,1:nZ,KL))) )
PP_AD = PP_AD + PPM_AD
PM_AD = PM_AD - PPM_AD
RETURN
END SUBROUTINE MOM_AD
!
!
SUBROUTINE CRTM_AMOM_layer( n_streams, & ! Input, number of streams
nZ, & ! Input, number of angles
KL, & ! Input, KL-th layer
single_albedo, & ! Input, single scattering albedo
optical_depth, & ! Input, layer optical depth
total_opt, & ! Input, accumulated optical depth from the top to current layer top
COS_Angle, & ! Input, COSINE of ANGLES
COS_Weight, & ! Input, GAUSSIAN Weights
ff, & ! Input, Phase matrix (forward part)
bb, & ! Input, Phase matrix (backward part)
Planck_Func, & ! Input, Planck for layer temperature
RTV, Error_Status) ! Output, layer transmittance, reflectance, and source
! ---------------------------------------------------------------------------------------
! FUNCTION
! Compute layer transmission, reflection matrices and source function
! at the top and bottom of the layer.
!
! Method and References
! The transmittance and reflectance matrices is further derived from
! matrix operator method. The matrix operator method is referred to the paper by
!
! Weng, F., and Q. Liu, 2003: Satellite Data Assimilation in Numerical Weather Prediction
! Model: Part 1: Forward Radiative Transfer and Jacobian Modeling in Cloudy Atmospheres,
! J. Atmos. Sci., 60, 2633-2646.
!
! see also ADA method.
! Quanhua Liu
! ----------------------------------------------------------------------------------------
IMPLICIT NONE
INTEGER, INTENT(IN) :: n_streams,nZ,KL
TYPE(RTV_type), INTENT( INOUT ) :: RTV
REAL(fp), INTENT(IN), DIMENSION(:,:) :: ff,bb
REAL(fp), INTENT(IN), DIMENSION(:) :: COS_Angle, COS_Weight
REAL(fp) :: single_albedo,optical_depth,Planck_Func,total_opt
! internal variables
REAL(fp), DIMENSION(nZ,nZ) :: trans, refl
REAL(fp) :: s, c
INTEGER :: i,j,N2,N2_1
INTEGER :: Error_Status
REAL(fp) :: EXPfactor,Sfactor,s_transmittance,Solar(2*nZ),V0(2*nZ,2*nZ),Solar1(2*nZ)
REAL(fp) :: V1(2*nZ,2*nZ),Sfac2,source_up(nZ),source_down(nZ)
CHARACTER(*), PARAMETER :: ROUTINE_NAME = 'CRTM_AMOM_layer'
CHARACTER(256) :: Message
! for small layer optical depth, single scattering is applied.
IF( optical_depth < DELTA_OPTICAL_DEPTH ) THEN
s = optical_depth * single_albedo
DO i = 1, nZ
RTV%Thermal_C(i,KL) = ZERO
c = s/COS_Angle(i)
DO j = 1, nZ
RTV%s_Layer_Refl(i,j,KL) = c * bb(i,j) * COS_Weight(j)
RTV%s_Layer_Trans(i,j,KL) = c * ff(i,j) * COS_Weight(j)
IF( i == j ) THEN
RTV%s_Layer_Trans(i,i,KL) = RTV%s_Layer_Trans(i,i,KL) + &
ONE - optical_depth/COS_Angle(i)
END IF
IF( RTV%mth_Azi == 0 ) THEN
RTV%Thermal_C(i,KL) = RTV%Thermal_C(i,KL) + &
( RTV%s_Layer_Refl(i,j,KL) + RTV%s_Layer_Trans(i,j,KL) )
END IF
ENDDO
IF( RTV%mth_Azi == 0 ) THEN
RTV%s_Layer_Source_UP(i,KL) = ( ONE - RTV%Thermal_C(i,KL) ) * Planck_Func
RTV%s_Layer_Source_DOWN(i,KL) = RTV%s_Layer_Source_UP(i,KL)
END IF
ENDDO
RETURN
END IF
!
! for numerical stability,
IF( single_albedo < max_albedo ) THEN
s = single_albedo
ELSE
s = max_albedo
END IF
!
! building phase matrices
DO i = 1, nZ
c = s/COS_Angle(i)
DO j = 1, nZ
RTV%PM(i,j,KL) = c * bb(i,j) * COS_Weight(j)
RTV%PP(i,j,KL) = c * ff(i,j) * COS_Weight(j)
ENDDO
RTV%PP(i,i,KL) = RTV%PP(i,i,KL) - ONE/COS_Angle(i)
ENDDO
IF( RTV%RT_Algorithm_Id == RT_AMOM ) THEN
CALL MOM(KL,nZ,optical_depth,trans,refl,RTV, Error_Status)
ELSE
IF( ff(2,4) == ZERO ) THEN
CALL MOM(KL,nZ,optical_depth,trans,refl,RTV, Error_Status)
ELSE
CALL Matrix_Exp(KL,nZ,optical_depth,RTV%PP(1:nZ,1:nZ,KL),RTV%PM(1:nZ,1:nZ,KL),trans,refl,RTV,Error_Status)
IF( Error_Status /= SUCCESS ) RETURN
END IF
END IF
! post processing
RTV%s_Layer_Trans(1:nZ,1:nZ,KL) = trans(:,:)
RTV%s_Layer_Refl(1:nZ,1:nZ,KL) = refl(:,:)
RTV%s_Layer_Source_UP(:,KL) = ZERO
IF( RTV%mth_Azi == 0 ) THEN
DO i = 1, nZ
RTV%Thermal_C(i,KL) = ZERO
DO j = 1, n_Streams, RTV%n_Stokes
RTV%Thermal_C(i,KL) = RTV%Thermal_C(i,KL) + (trans(i,j) + refl(i,j) )
END DO
IF ( i == nZ .AND. nZ == (n_Streams+1) ) THEN
RTV%Thermal_C(i,KL) = RTV%Thermal_C(i,KL) + trans(nZ,nZ)
END IF
RTV%s_Layer_Source_UP(i,KL) = ( ONE - RTV%Thermal_C(i,KL) ) * Planck_Func
RTV%s_Layer_Source_DOWN(i,KL) = RTV%s_Layer_Source_UP(i,KL)
END DO
END IF
! compute visible part for visible channels during daytime
IF( RTV%Solar_Flag_true ) THEN
N2 = 2 * nZ
N2_1 = N2 - RTV%n_Stokes
source_up = ZERO
source_down = ZERO
!
! Solar source
Sfactor = single_albedo*RTV%Solar_irradiance/PI
IF( RTV%mth_Azi == 0 ) Sfactor = Sfactor/TWO
EXPfactor = exp(-optical_depth/RTV%COS_SUN)
s_transmittance = exp(-total_opt/RTV%COS_SUN)
DO i = 1, nZ
Solar(i) = -bb(i,nZ+1)*Sfactor
Solar(i+nZ) = -ff(i,nZ+1)*Sfactor
DO j = 1, nZ
V0(i,j) = single_albedo * ff(i,j) * COS_Weight(j)
V0(i+nZ,j) = single_albedo * bb(i,j) * COS_Weight(j)
V0(i,j+nZ) = V0(i+nZ,j)
V0(nZ+i,j+nZ) = V0(i,j)
ENDDO
V0(i,i) = V0(i,i) - ONE - COS_Angle(i)/RTV%COS_SUN
V0(i+nZ,i+nZ) = V0(i+nZ,i+nZ) - ONE + COS_Angle(i)/RTV%COS_SUN
ENDDO
V1(1:N2_1,1:N2_1) = matinv(V0(1:N2_1,1:N2_1), Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion matinv(V0(1:N2_1,1:N2_1), Error_Status) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
Solar1(1:N2_1) = matmul( V1(1:N2_1,1:N2_1), Solar(1:N2_1) )
Solar1(N2) = ZERO
Sfac2 = Solar(N2) - sum( V0(N2,1:N2_1)*Solar1(1:N2_1) )
DO i = 1, nZ
source_up(i) = Solar1(i)
source_down(i) = EXPfactor*Solar1(i+nZ)
DO j = 1, nZ
source_up(i) =source_up(i)-refl(i,j)*Solar1(j+nZ)-trans(i,j)*EXPfactor*Solar1(j)
source_down(i) =source_down(i) -trans(i,j)*Solar1(j+nZ) -refl(i,j)*EXPfactor*Solar1(j)
END DO
END DO
! specific treatment for downeward source function
IF( abs( V0(N2,N2) ) > 0.0001_fp ) THEN
source_down(nZ) =source_down(nZ) +(EXPfactor-trans(nZ,nZ))*Sfac2/V0(N2,N2)
ELSE
source_down(nZ) =source_down(nZ) -EXPfactor*Sfac2*optical_depth/COS_Angle(nZ)
END IF
source_up(1:nZ) = source_up(1:nZ)*s_transmittance
source_down(1:nZ) = source_down(1:nZ)*s_transmittance
RTV%s_Layer_Source_UP(1:nZ,KL) = RTV%s_Layer_Source_UP(1:nZ,KL)+source_up(1:nZ)
RTV%s_Layer_Source_DOWN(1:nZ,KL) = RTV%s_Layer_Source_DOWN(1:nZ,KL)+source_down(1:nZ)
END IF
RETURN
END SUBROUTINE CRTM_AMOM_layer
!
!
SUBROUTINE Matrix_Exp(KL,nZ,optical_depth2,A,B,trans,refl,RTV,Error_Status)
IMPLICIT NONE
TYPE(RTV_type), INTENT( INOUT ) :: RTV
INTEGER, INTENT(IN) :: nZ, KL
REAL(fp), INTENT(IN) :: optical_depth2
REAL(fp), INTENT(IN), DIMENSION(:,:) :: A, B
REAL(fp), DIMENSION(:,:) :: trans, refl
REAL(fp), DIMENSION(nZ,nZ) :: HH, FF, EE, S, A1, A2, B1, B2, S1, S2, S3, S4
REAL(fp), DIMENSION(nZ,nZ) :: term1, term2, inv_M
INTEGER :: i,N,Error_Status
REAL(fp) :: Factor, Fac2, max_H,optical_depth
CHARACTER(256) :: Message
CHARACTER(*), PARAMETER :: ROUTINE_NAME = 'Matrix_Exp'
Error_Status = SUCCESS
EE(:,:) = ZERO
DO i = 1, nZ
EE(i,i) = ONE
END DO
!HH = matmul(A-B,A+B)
HH = matmul(A(1:nZ,1:nZ)-B(1:nZ,1:nZ),A(1:nZ,1:nZ)+B(1:nZ,1:nZ))
RTV%HH(1:nZ,1:nZ,KL) = HH(:,:)
max_H = maxval(abs(RTV%HH(1:nZ,1:nZ,KL)))
max_H =sqrt(max_H) * optical_depth2
RTV%Number_Doubling(KL) = INT( log( max_H/0.5_fp ) / log( TWO ) ) + 1
! RTV%Number_Doubling(KL) = INT( log( optical_depth2/DELTA_OPTICAL_DEPTH ) / log( TWO ) ) + 1
! above line is for testing purpose. If you use it, make sure the value in following line is
! not too small in numerical precision.
IF( RTV%Number_Doubling(KL) < 0 ) RTV%Number_Doubling(KL) = 0
optical_depth = optical_depth2/(2**RTV%Number_Doubling(KL))
HH = HH * optical_depth*optical_depth
FF = matmul(A+B,A-B)*optical_depth*optical_depth
N = RTV%MAX_N_AMOM !+ log(ONE+maxval(abs(HH)))/log(TWO) * 100
S1 = EE
S2 = EE
S3 = EE
S4 = EE
DO i = N, 1, -1
RTV%ADS1(1:nZ,1:nZ,i,KL) = S1
RTV%ADS2(1:nZ,1:nZ,i,KL) = S2
RTV%ADS3(1:nZ,1:nZ,i,KL) = S3
RTV%ADS4(1:nZ,1:nZ,i,KL) = S4
A1 = matmul(HH,S1)
A2 = matmul(FF,S2)
B1 = matmul(HH,S3)
B2 = matmul(FF,S4)
Factor = (TWO*float(i))*(TWO*float(i)-ONE)
Fac2 = (TWO*float(i))*(TWO*float(i)+ONE)
S1 = EE + A1/Factor
S2 = EE + A2/Factor
S3 = EE + B1/Fac2
S4 = EE + B2/Fac2
END DO
RTV%ApBS3(1:nZ,1:nZ,KL) = S3
S3 = optical_depth*(matmul(A+B,S3))
RTV%AmBS4(1:nZ,1:nZ,KL) = S4
S4 = optical_depth*(matmul(A-B,S4))
S = S1 + S2 -S3 - S4
RTV%ADS(1:nZ,1:nZ,KL) = S
RTV%ADSr(1:nZ,1:nZ,KL) = S1 + S3 - S2 - S4
trans = matinv(S, Error_Status) * TWO
refl = matmul(RTV%ADSr(1:nZ,1:nZ,KL), trans)/TWO
RTV%Refl(1:nZ,1:nZ,0,KL) = Refl(1:nZ,1:nZ)
RTV%Trans(1:nZ,1:nZ,0,KL) = Trans(1:nZ,1:nZ)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion in Matrix_Exp) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN
END IF
IF( RTV%Number_Doubling(KL) > 0 ) THEN
RTV%Refl(1:nZ,1:nZ,0,KL) = Refl(1:nZ,1:nZ)
RTV%Trans(1:nZ,1:nZ,0,KL) = Trans(1:nZ,1:nZ)
DO i = 1, RTV%Number_Doubling(KL)
inv_M = EE - matmul(RTV%Refl(1:nZ,1:nZ,i-1,KL),RTV%Refl(1:nZ,1:nZ,i-1,KL))
RTV%Inv_BeT(1:nZ,1:nZ,i,KL) = matinv(inv_M, Error_Status)
IF( Error_Status /= SUCCESS ) THEN
WRITE( Message,'("Error in matrix inversion in Matrix_Exp Ndouble) ")' )
CALL Display_Message( ROUTINE_NAME, &
TRIM(Message), &
Error_Status )
RETURN