forked from NordicESMhub/OsloCTM3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectral_routines.f
1522 lines (1423 loc) · 48.7 KB
/
spectral_routines.f
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
!//=========================================================================
!// Oslo CTM3
!//=========================================================================
!// Collection of routines used for converting spectral fields to
!// grid point data.
!//
!// Ole Amund Sovde, April 2015
!//=========================================================================
C-----------------------------------------------------------------------
C Routines for converting spectral data to gridpoint data.
C These are not put in f90 module, because their arguments typically
C have different array structure than the calling routine.
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
Subroutine SPE2GP ( SPE,NRNM,LUV,GPT,ID,JD,LD, ALPGAU,NMMAX,
& FFTAR,WORK,TRIG,IFAX,NTP1,FFTPTS,NRFFT )
C-----------------------------------------------------------------------
C Do the spectral to gridpoint transform. First do an inverse Legendre
C transform (ILT) then do a Fast Fourier Transform (FFT).
C
C Original version J.K.Sundet January 1995
C-----------------------------------------------------------------------
use cmn_precision, only: r8, r4
use utilities, only: ctmExitC
Implicit NONE
C-----------------------------------------------------------------------
C I/O parameters
Integer
& IFAX(10),
& NMMAX,NRNM,NTP1,
& FFTPTS,NRFFT,
& ID,JD,LD
real(r8)
& GPT(ID,JD,LD), ! gaussian gridpt values or fluxes
& FFTAR(FFTPTS,NRFFT), ! fourier to gridpoint transf. array
& WORK(FFTPTS*NRFFT), ! work area for FFT
& ALPGAU(NMMAX,JD/2),
& TRIG(ID)
Real(r4)
& SPE(2,NRNM/2,LD) ! spectral coeffs
Logical
& LUV ! .TRUE. if U or V
C Local parameters
real(r8)
& SPEWRK(2,51680), ! 51680 = T319 size (NRNM/2) Obs:NRNM is 2*NMMAX
& SUMRES,SUMIMS, ! symetric real and img sums
& SUMREA,SUMIMA ! anti-symetric real and img sums
Integer
& NGROUP, ! number of *(32) latitudes
& NRES, ! residual number of latitudes
& IGRP, ! NGROUP counter
& ILIM, ! NTP1
& IMLIM, ! NTP1
& IN, ! n Max(dimension) for given m
& JBACK, ! counter keeping track of where in NGROUP
& JNH,JSH,JLAT, ! latitude counter
& I,J,ILONG,JF,L,INL, ! loop counters
& IMN,IMP, ! m,n counters
& IXTRA ! an extra counter if U or V
C-----------------------------------------------------------------------
C Initialize
ILIM = NTP1
IMLIM = NTP1
If (.NOT.LUV) Then
IXTRA = 0
Else
IXTRA = 1
Endif
NRES = Mod(JD,NRFFT)
NGROUP = JD/NRFFT
If (NRES.NE.0) Then
Write(*,*) ' * Only programed for multiples of 32 ! *'
WRITE(*,*) NGROUP, NRES
Call ctmExitC('=================SP2GP================')
Endif
C Do the transform in two operations, multiply the spec coeffs
C with appropriate functions. Then sum the ones (odd and even) to
C find the symetric and anti-symetric functions (rel. to equator).
Do 109 L=1,LD
C For all pairs of NRFFT latitudes
Do 105 IGRP=1,NGROUP
Do J=1,NRFFT
Do I=1,FFTPTS
FFTAR(I,J) = 0._r8
WORK(I + (J-1)*FFTPTS) = 0._r8
End Do
End Do
If (IGRP.EQ.1) Then
JBACK = 0
Else
JBACK = ((IGRP-1)*NRFFT)/2
Endif
C Real(r4) --> real(r8)
Do I=1,NRNM/2
SPEWRK(1,I) = SPE(1,I,L)
SPEWRK(2,I) = SPE(2,I,L)
End Do
Do J=1,NRFFT/2
IMP = 0
IMN = 0
C For each zonal wavenumber sum the coeffs*A-Leg coeffs.
Do ILONG=1,IMLIM
SUMRES = 0._r8
SUMIMS = 0._r8
SUMREA = 0._r8
SUMIMA = 0._r8
IN = ILIM - ILONG + (1+IXTRA)
C Find even (symetric) part
Do JF=1,IN,2
SUMRES = SUMRES +ALPGAU(IMP+JF,J+JBACK)*SPEWRK(1,IMN+JF)
SUMIMS = SUMIMS +ALPGAU(IMP+JF,J+JBACK)*SPEWRK(2,IMN+JF)
Enddo
C Find odd (anti-symetric) part
Do JF=2,IN,2
SUMREA = SUMREA +ALPGAU(IMP+JF,J+JBACK)*SPEWRK(1,IMN+JF)
SUMIMA = SUMIMA +ALPGAU(IMP+JF,J+JBACK)*SPEWRK(2,IMN+JF)
Enddo
C For the southern hemisphere row, the legendre functions are
C the complex conjugates of the corresponding northern row -
C hence the juggling with the signs.
FFTAR(2*ILONG-1,J*2-1) = SUMRES + SUMREA
FFTAR(2*ILONG, J*2-1) = SUMIMS + SUMIMA
FFTAR(2*ILONG-1,J*2 ) = SUMRES - SUMREA
FFTAR(2*ILONG, J*2) = SUMIMS - SUMIMA
C
IMP = IMP + IN + (1-IXTRA)
IMN = IMN + IN
Enddo
Enddo
C Do multple FFT tranforms - currently 32,
CALL FFT_99(FFTAR,WORK,TRIG,IFAX,1,FFTPTS,ID,NRFFT)
C The latitudes are arranged: odd NH, even SH; rearrange
Do J=1,NRFFT/2
JNH = J*2 - 1
JSH = J*2
JLAT = JD - (J+JBACK) + 1
Do INL=1,ID
GPT(INL,J+JBACK,L) = FFTAR(INL,JNH)
GPT(INL,JLAT,L) = FFTAR(INL,JSH)
Enddo
Enddo
105 Continue
109 Continue
C
Return
End
C
C----------------------------------------------------------------------
Subroutine ZD2UV(ZSPE,DSPE,U,V,DD,SS,ID,JD,LD,ALP,ALPV,NMMAX,NRNM,
& FFTAR,WORK,TRIG, IFAX,NTRUNW,NRFFT,FFTPTS)
C----------------------------------------------------------------------
C Take care of the transform to horizontal wind from vorticity and
C divergence fields.
C----------------------------------------------------------------------
use cmn_precision, only: r8, r4
Implicit NONE
C----------------------------------------------------------------------
C I/O parameters
Integer
& IFAX(10),
& ID,JD,LD,
& NRNM,NMMAX,NTRUNW,
& NRFFT,FFTPTS
real(r8)
& U(ID,JD,LD),
& V(ID,JD,LD),
& FFTAR(FFTPTS,NRFFT),
& WORK(FFTPTS*NRFFT),
& ALP(NMMAX,JD/2),
& ALPV(NMMAX,JD/2),
& DD(NMMAX),
& SS(NMMAX),
& TRIG(ID)
cccccccccccccccccc , & TRIGU(ID)
Real(r4)
& ZSPE(2,NRNM/2,LD),
& DSPE(2,NRNM/2,LD)
C Local parameters
Real(r4), Allocatable ::
& USPE(:,:,:), VSPE(:,:,:)
Logical
& LUV
C-------------------------------------------------------------------
Allocate (
& VSPE(2,51680,LD), ! 51680 = T319 size (NRNM/2) Obs:NRNM is 2*NMMAX
& USPE(2,51680,LD) ) ! 5885 = T106 NMMAX=(106+1)*(106+4)/2
C Initialize
LUV = .TRUE.
C Find the spectral coeffs for U and V from the vorticity
C and divergence coeffs
Call UVCOEF(ZSPE,DSPE,DD,SS,NRNM,NMMAX,LD,NTRUNW,USPE,VSPE)
C Transform the fields to GridPoints
Call SPE2GP(USPE,NMMAX*2,LUV, U,ID,JD,LD, ALP,NMMAX,
& FFTAR,WORK,TRIG,IFAX,(NTRUNW+1),FFTPTS,NRFFT)
Call SPE2GP(VSPE,NMMAX*2,LUV, V,ID,JD,LD, ALPV,NMMAX,
& FFTAR,WORK,TRIG,IFAX,(NTRUNW+1),FFTPTS,NRFFT)
C
Deallocate ( VSPE, USPE )
c
Return
End
C
C
C----------------------------------------------------------------------
Subroutine UVCOEF(ZSPE,DSPE,DD,SS,NRNM,NMMAX,LD,NTRUNW,USPE,VSPE)
C----------------------------------------------------------------------
C Instead of using the vorticity and divergence we use the spectral
C coeffisients for u and v when the fluxes are calculated. It's also
C possible to derive the transport directly from the divergence and
C vorticity, but the approach chosen is more accessible and cleaner.
C
C Method: The spectral coeffisients for u and v are related to vorticity
C and divergence in the following way;
C
C u(n,m)=+dd(n,m)*vor(n-1,m)+i*ss(n,m)*div(n,m)-dd(n+1,m)*vor(n+1,m)
C and
C v(n,m)=-dd(n,m)*div(n-1,m)+i*ss(n,m)*vor(n,m)-dd(n+1,m)*div(n+1,m)
C
C Original version: J. K. Sundet, November 1994
C----------------------------------------------------------------------
use cmn_precision, only: r8, r4
Implicit NONE
C----------------------------------------------------------------------
C I/O parameters
Integer
& NRNM,NMMAX,
& LD,NTRUNW
real(r8)
& DD(NMMAX),
& SS(NMMAX)
Real(r4)
& USPE(2,NMMAX,LD),
& VSPE(2,NMMAX,LD),
& ZSPE(2,NRNM/2,LD),
& DSPE(2,NRNM/2,LD)
C Local parameters
real(r8), Allocatable ::
& ZWRK(:,:), DWRK(:,:)
Integer
& JL,JN,I,L, ! loop counters
& INU,INM, ! coeffs counters
& NTP1
C----------------------------------------------------------------------
Allocate (
& DWRK(2,51680), ! 51680 = T319 NMMAX
& ZWRK(2,51680) ) ! 5885 = T106 NMMAX=(106+1)*(106+4)/2
C Initialize
NTP1 = NTRUNW + 1
ZWRK(:,:) = 0._r8
DWRK(:,:) = 0._r8
C For all levels
Do L=1,LD
C Real(r4) --> real(r8)
Do I=1,NRNM/2
ZWRK(1,I) = ZSPE(1,I,L)
ZWRK(2,I) = ZSPE(2,I,L)
DWRK(1,I) = DSPE(1,I,L)
DWRK(2,I) = DSPE(2,I,L)
End Do
INU = 1
INM = 1
Do JL=1,NTP1
C For n = m
If (JL.EQ.1) Then
USPE(1,INU,L) = -DD(INU+1)*ZWRK(1,INM+1)
USPE(2,INU,L) = -DD(INU+1)*ZWRK(2,INM+1)
VSPE(1,INU,L) = +DD(INU+1)*DWRK(1,INM+1)
VSPE(2,INU,L) = +DD(INU+1)*DWRK(2,INM+1)
Elseif (JL.EQ.NTP1) Then
USPE(1,INU,L) = -SS(INM)*DWRK(2,INM)
USPE(2,INU,L) = +SS(INM)*DWRK(1,INM)
VSPE(1,INU,L) = -SS(INM)*ZWRK(2,INM)
VSPE(2,INU,L) = +SS(INM)*ZWRK(1,INM)
Else
USPE(1,INU,L) = -SS(INM)*DWRK(2,INM)-DD(INU+1)*ZWRK(1,INM+1)
USPE(2,INU,L) = +SS(INM)*DWRK(1,INM)-DD(INU+1)*ZWRK(2,INM+1)
VSPE(1,INU,L) = -SS(INM)*ZWRK(2,INM)+DD(INU+1)*DWRK(1,INM+1)
VSPE(2,INU,L) = +SS(INM)*ZWRK(1,INM)+DD(INU+1)*DWRK(2,INM+1)
Endif
INM = INM + 1
INU = INU + 1
C For m < n < NT
Do JN=(JL+1),NTRUNW
USPE(1,INU,L) = +DD(INU)*ZWRK(1,INM-1)
& -SS(INM)*DWRK(2,INM)
& -DD(INU+1)*ZWRK(1,INM+1)
USPE(2,INU,L) = +DD(INU)*ZWRK(2,INM-1)
& +SS(INM)*DWRK(1,INM)
& -DD(INU+1)*ZWRK(2,INM+1)
VSPE(1,INU,L) = -DD(INU)*DWRK(1,INM-1)
& -SS(INM)*ZWRK(2,INM)
& +DD(INU+1)*DWRK(1,INM+1)
VSPE(2,INU,L) = -DD(INU)*DWRK(2,INM-1)
& +SS(INM)*ZWRK(1,INM)
& +DD(INU+1)*DWRK(2,INM+1)
INM = INM + 1
INU = INU + 1
Enddo
C For n = NT
If (JL.LT.NTP1) Then
USPE(1,INU,L) = +DD(INU)*ZWRK(1,INM-1)
& -SS(INM)*DWRK(2,INM)
USPE(2,INU,L) = +DD(INU)*ZWRK(2,INM-1)
& +SS(INM)*DWRK(1,INM)
VSPE(1,INU,L) = -DD(INU)*DWRK(1,INM-1)
& -SS(INM)*ZWRK(2,INM)
VSPE(2,INU,L) = -DD(INU)*DWRK(2,INM-1)
& +SS(INM)*ZWRK(1,INM)
INU = INU + 1
INM = INM + 1
Endif
C For n = NTP1
USPE(1,INU,L) = +DD(INU)*ZWRK(1,INM-1)
USPE(2,INU,L) = +DD(INU)*ZWRK(2,INM-1)
VSPE(1,INU,L) = -DD(INU)*DWRK(1,INM-1)
VSPE(2,INU,L) = -DD(INU)*DWRK(2,INM-1)
INU = INU + 1
Enddo
Enddo
c
Deallocate ( ZWRK, DWRK )
C
Return
c-----------------------------------------------------------------------
End
c-----------------------------------------------------------------------
c-----------------------------------------------------------------------
subroutine FFT_99(A,WORK,TRIGS,IFAX,INC,JUMP,N,LOT)
c-----------------------------------------------------------------------
c MULTIPLE FAST REAL PERIODIC TRANSFORM OF LENGTH N PERFORMED BY
c REMOVING REDUNDANT OPERATIONS FROM COMPLEX TRANSFORM LENGTH N
c
c A = ARRAY CONTAINING INPUT & OUTPUT DATA
c WORK = WORK AREA OF SIZE (N+1)*MIN(LOT,64)
c TRIGS = PREVIOUSLY PREPARED LIST OF TRIG FUNCTION VALUES
c IFAX = PREVIOUSLY PREPARED LIST OF FACTORS OF N
c INC = INCREMENT WITHIN EACH DATA VECTOR (INC=1 = CONSEC STORED DATA)
c JUMP = INCREMENT BETWEEN THE START OF EACH DATA VECTOR
c N = LENGTH OF THE DATA VECTORS
c LOT = NUMBER OF DATA VECTORS
c assumed for this version: ISIGN = +1 TRANSFORM SPECTRAL=>GRIDPOINT
c ordering of coeff's
c A(0),B(0),A(1),B(1),A(2),B(2),...,A(N/2),B(N/2)
c WHERE B(0)=B(N/2)=0; (N+2) LOCATIONS ReqUIRED
c ordering of data
c X(0),X(1),X(2),...,X(N-1), 0 , 0 ; (N+2)
c N must be factor into 2 x 3 x 5 's (odd OK)
c
c transforms:
c ISIGN=+1: X(J)=SUM(K=0,...,N-1)(C(K)*EXP(2*I*J*K*PI/N))
c where C(K)=A(K)+I*B(K) AND C(N-K)=A(K)-I*B(K)
c ISIGN=-1: A(K)=(1/N)*SUM(J=0,...,N-1)(X(J)*COS(2*J*K*PI/N))
c B(K)=-(1/N)*SUM(J=0,...,N-1)(X(J)*SIN(2*J*K*PI/N))
c
c Adapted to CTM J.K. Sundet Jan. 1996
c-----------------------------------------------------------------------
use cmn_precision, only: r8, r4
use utilities, only: ctmExitIJL
implicit none
integer, intent(inout) :: INC,JUMP,N,LOT
integer, dimension(10), intent(inout) :: IFAX
real(r8), dimension(JUMP*LOT), intent(inout) :: A,WORK
real(r8), dimension(N), intent(inout) :: TRIGS
integer
& IOC, ! = JUMP
& NFAX, ! number of FFT factors
& NX,NBLOX,NVEX, ! loop limits
& ISTART,IA,LA, ! counters
& I,J,K,NB,II,JJ, ! counters
& IGO, ! determines what half-plane to sample points
& IFAC,IERR, ! = IFAX(K) and error check
& IBASE,JBASE,IX ! counters
logical LNODD, LFAXODD
c-----------------------------------------------------------------------
c--Check that SET_FFT is done:
if (IFAX(10).ne.N) call FFT_SET(TRIGS,IFAX,N)
IOC = JUMP
NFAX = IFAX(1)
LFAXODD = mod(NFAX,2).ne.0
if (mod(N,2).eq.1) then
NX = N
LNODD = .TRUE.
else
NX = N+1
LNODD = .FALSE.
endif
NBLOX = (LOT-1)/64 + 1
NVEX = LOT - (NBLOX-1)*64
ISTART = 1
c---Spectral-to-gridpoint transform (ISIGN=+1 in old notation)
do NB = 1,NBLOX
IA = ISTART
I = ISTART
do J=1,NVEX
A(I+INC) = 0.5_r8*A(I)
I = I + JUMP
enddo
if (.not.LNODD) then
I = ISTART + N*INC
do J=1,NVEX
A(I) = 0.5_r8*A(I)
I = I + JUMP
enddo
endif
IA = ISTART + INC
LA = 1
IGO = +1
c---Sample gridpoints on both half-planes
do K=1,NFAX
IFAC = IFAX(K+1)
IERR = -1
if (IGO.eq.+1) then
CALL FFT_RPASS(A(IA),A(IA+LA*INC),WORK(1),WORK(IFAC*LA+1),
& TRIGS,INC,1,JUMP,NX,NVEX,N,IFAC,IOC,LA,IERR)
IGO = -1
else
CALL FFT_RPASS(WORK(1),WORK(LA+1),A(IA),A(IA+IFAC*LA*INC),
& TRIGS,1,INC,NX,JUMP,NVEX,N,IFAC,IOC,LA,IERR)
IGO = +1
endif
LA = IFAC*LA
IA = ISTART
if (IERR.ne.0) then
call ctmEXITIJL('>>>>error FFT_RPASS: IERR,NVEX,IFAC',
& IERR,NVEX,IFAC)
endif
enddo
c---If necessary, copy results back to A()
if (LFAXODD) then
IBASE = 1
JBASE = IA
do JJ=1,NVEX
I = IBASE
J = JBASE
do II=1,N
A(J) = WORK(I)
I = I + 1
J = J + INC
enddo
IBASE = IBASE + NX
JBASE = JBASE + JUMP
enddo
endif
c---Fill in zero's at end
IX = ISTART + N*INC
do J=1,NVEX
A(IX) = 0._r8
A(IX+INC) = 0._r8
IX = IX + JUMP
enddo
ISTART = ISTART + NVEX*JUMP
NVEX = 64
enddo
return
end
c-----------------------------------------------------------------------
subroutine FFT_RPASS(A,B,C,D,TRIGS,INC1,INC2,INC3,INC4,LOT,N,IFAC,
& IOC,LA,IERR)
c-----------------------------------------------------------------------
c---performs one pass thru data as part of multiple real FFT
c A = FIRST REAL INPUT VECTOR eqUIVALENCE B(1) WITH A (LA*INC1+1)
c C = FIRST REAL OUTPUT VECTOR eqUIVALENCE D(1) WITH C(IFAC*LA*INC2+1)
c TRIGS= ECALCULATED LIST OF SIneS & COSIneS
c INC1 = ADDRESSING INCREMENT FOR A
c INC2 = ADDRESSING INCREMENT FOR C
c INC3 = INCREMENT BETWEEN INPUT VECTORS A
c INC4 = INCREMENT BETWEEN OUTPUT VECTORS C
c LOT = NUMBER OF VECTORS
c N = LENGTH OF THE VECTORS
c IFAC = CURRENT FACTOR OF N
c LA = PRODUCT OF PREVIOUS FACTORS
c IERR = ERROR INDICATOR:
c 0 - PASS COMPLETED WITHOUT ERROR
c 1 - LOT GREATER THAN 64
c 2 - IFAC NOT CATERED FOR
c 3 - IFAC ONLY CATERED FOR IF LA=N/IFAC
c Adapted to CTM: J. K. Sundet July 1994
c------------------------------------------------------------------------
use cmn_precision, only: r8, r4
implicit none
integer, intent(in) :: INC1,INC2,INC3,INC4,LOT,N,IFAC,IOC,LA
integer, intent(out) :: IERR
real(r8), dimension(IOC*LOT), intent(in) :: A,B
real(r8), dimension(IOC*LOT), intent(out) :: C,D
real(r8), dimension(N), intent(in) :: TRIGS
real(r8), dimension(64) :: A10,A11,A20,A21,B10,B11,B20,B21
real(r8) C1,C2,C3,C4,C5,S1,S2,S3,S4,S5,
& FN,FIFAC,FLA,FINC1,FINC2,FM
integer I,J,K,L,M,IJK, KB,KC,KD,KE,KF
integer IA,IB,IC,ID,IE,IG, JA,JB,JC,JD,JE,JF,JG,JH
integer IBASE,JBASE,IINK,JINK,JUMP,KSTOP,IHLP
logical LAEQM
real(r8),parameter:: SIN36=0.587785252292473137_r8
real(r8),parameter:: SSIN36=2._r8*SIN36
real(r8),parameter:: SIN45=0.707106781186547524_r8
real(r8),parameter:: SSIN45=2._r8*SIN45
real(r8),parameter:: SIN60=0.866025403784438597_r8
real(r8),parameter:: SSIN60=2._r8*SIN60
real(r8),parameter:: SIN72=0.951056516295153531_r8
real(r8),parameter:: SSIN72=2._r8*SIN72
real(r8),parameter:: QRT5 =0.559016994374947451_r8
real(r8),parameter:: QQRT5 =2._r8*QRT5
C---------------------------------------------------------------------
if (LOT.GT.64) then
IERR = 1
return
endif
IBASE= 0
JBASE= 0
IERR = 0
FN = real(N,r8)
FIFAC= real(IFAC,r8)
FLA = real(LA,r8)
FINC1= real(INC1,r8)
FINC2= real(INC2,r8)
FM = FN/FIFAC
M = int(FM)
IINK = int(FLA*FINC1)
JINK = int(FLA*FINC2)
JUMP = int((FIFAC-1._r8)*real(JINK,r8))
KSTOP= int((FN-FIFAC)/(2._r8*FIFAC))
LAEQM= LA.eq.M
c---defactorize the FFT coeff's, determined by IFAC
c---IFAC=2
if (IFAC.eq.2) then
IA = 1
IB = IA + int((2._r8*FM - FLA)*FINC1)
JA = 1
JB = JA + JINK
if (.not.LAEQM) then
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+A(IB+I)
C(JB+J)=A(IA+I)-A(IB+I)
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA = IA+IINK
IINK = 2*IINK
IB = IB-IINK
IBASE= 0
JBASE= JBASE+JUMP
JUMP = 2*JUMP+JINK
if (IA.ne.IB) then
do K=LA,KSTOP,LA
KB=K+K
C1=TRIGS(KB+1)
S1=TRIGS(KB+2)
IBASE=0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+A(IB+I)
D(JA+J)=B(IA+I)-B(IB+I)
C(JB+J)=C1*(A(IA+I)-A(IB+I))-S1*(B(IA+I)+B(IB+I))
D(JB+J)=S1*(A(IA+I)-A(IB+I))+C1*(B(IA+I)+B(IB+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA=IA+IINK
IB=IB-IINK
JBASE=JBASE+JUMP
enddo
if(IA.GT.IB) then
return
endif
endif
IBASE = 0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)
C(JB+J)=-B(IA+I)
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
else
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=2._r8*(A(IA+I)+A(IB+I))
C(JB+J)=2._r8*(A(IA+I)-A(IB+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
endif
c---IFAC=3
elseif(IFAC.eq.3) then
IA = 1
IB = IA+int((2._r8*FM-FLA)*FINC1)
IC = IB
JA = 1
JB = JA+JINK
JC = JB+JINK
if (.not.LAEQM) then
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+A(IB+I)
C(JB+J)=(A(IA+I)-0.5*A(IB+I))-(SIN60*(B(IB+I)))
C(JC+J)=(A(IA+I)-0.5*A(IB+I))+(SIN60*(B(IB+I)))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA = IA+IINK
IINK = 2*IINK
IB = IB+IINK
IC = IC-IINK
JBASE= JBASE+JUMP
JUMP = 2*JUMP+JINK
if (IA.ne.IC) then
do K=LA,KSTOP,LA
KB=K+K
KC=KB+KB
C1=TRIGS(KB+1)
S1=TRIGS(KB+2)
C2=TRIGS(KC+1)
S2=TRIGS(KC+2)
IBASE=0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+(A(IB+I)+A(IC+I))
D(JA+J)=B(IA+I)+(B(IB+I)-B(IC+I))
C(JB+J)= C1*((A(IA+I)-0.5_r8*(A(IB+I)+A(IC+I))) -
& (SIN60*(B(IB+I)+B(IC+I))))
& -S1*((B(IA+I)-0.5_r8*(B(IB+I)-B(IC+I))) +
& (SIN60*(A(IB+I)-A(IC+I))))
D(JB+J)= S1*((A(IA+I)-0.5_r8*(A(IB+I)+A(IC+I))) -
& (SIN60*(B(IB+I)+B(IC+I))))
& +C1*((B(IA+I)-0.5_r8*(B(IB+I)-B(IC+I))) +
& (SIN60*(A(IB+I)-A(IC+I))))
C(JC+J)= C2*((A(IA+I)-0.5_r8*(A(IB+I)+A(IC+I))) +
& (SIN60*(B(IB+I)+B(IC+I))))
& -S2*((B(IA+I)-0.5_r8*(B(IB+I)-B(IC+I))) -
& (SIN60*(A(IB+I)-A(IC+I))))
D(JC+J)= S2*((A(IA+I)-0.5_r8*(A(IB+I)+A(IC+I))) +
& (SIN60*(B(IB+I)+B(IC+I))))
& +C2*((B(IA+I)-0.5_r8*(B(IB+I)-B(IC+I))) -
& (SIN60*(A(IB+I)-A(IC+I))))
I = I + INC3
J = J + INC4
enddo
IBASE = IBASE + INC1
JBASE = JBASE + INC2
enddo
IA = IA + IINK
IB = IB + IINK
IC = IC - IINK
JBASE = JBASE + JUMP
enddo
if (IA.GT.IC) then
return
endif
endif
IBASE = 0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+A(IB+I)
C(JB+J)=(0.5_r8*A(IA+I)-A(IB+I))-(SIN60*B(IA+I))
C(JC+J)=-(0.5_r8*A(IA+I)-A(IB+I))-(SIN60*B(IA+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
else
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=2._r8*(A(IA+I)+A(IB+I))
C(JB+J)=(2._r8*A(IA+I)-A(IB+I))-(SSIN60*B(IB+I))
C(JC+J)=(2._r8*A(IA+I)-A(IB+I))+(SSIN60*B(IB+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
endif
c---IFAC=4
elseif(IFAC.eq.4) then
IA = 1
IB = IA+int((2._r8*FM-FLA)*FINC1)
IC = IB+int(2._r8*FM*FINC1)
ID = IB
JA = 1
JB = JA+JINK
JC = JB+JINK
JD = JC+JINK
if (.not.LAEQM) then
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=(A(IA+I)+A(IC+I))+A(IB+I)
C(JB+J)=(A(IA+I)-A(IC+I))-B(IB+I)
C(JC+J)=(A(IA+I)+A(IC+I))-A(IB+I)
C(JD+J)=(A(IA+I)-A(IC+I))+B(IB+I)
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA = IA+IINK
IINK = 2*IINK
IB = IB+IINK
IC = IC-IINK
ID = ID-IINK
JBASE= JBASE+JUMP
JUMP = 2*JUMP+JINK
if(IB.ne.IC) then
do K=LA,KSTOP,LA
KB=K+K
KC=KB+KB
KD=KC+KB
C1=TRIGS(KB+1)
S1=TRIGS(KB+2)
C2=TRIGS(KC+1)
S2=TRIGS(KC+2)
C3=TRIGS(KD+1)
S3=TRIGS(KD+2)
IBASE=0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=(A(IA+I)+A(IC+I))+(A(IB+I)+A(ID+I))
D(JA+J)=(B(IA+I)-B(IC+I))+(B(IB+I)-B(ID+I))
C(JC+J)= C2*((A(IA+I)+A(IC+I))-(A(IB+I)+A(ID+I)))
& -S2*((B(IA+I)-B(IC+I))-(B(IB+I)-B(ID+I)))
D(JC+J)= S2*((A(IA+I)+A(IC+I))-(A(IB+I)+A(ID+I)))
& +C2*((B(IA+I)-B(IC+I))-(B(IB+I)-B(ID+I)))
C(JB+J)= C1*((A(IA+I)-A(IC+I))-(B(IB+I)+B(ID+I)))
& -S1*((B(IA+I)+B(IC+I))+(A(IB+I)-A(ID+I)))
D(JB+J)= S1*((A(IA+I)-A(IC+I))-(B(IB+I)+B(ID+I)))
& +C1*((B(IA+I)+B(IC+I))+(A(IB+I)-A(ID+I)))
C(JD+J)= C3*((A(IA+I)-A(IC+I))+(B(IB+I)+B(ID+I)))
& -S3*((B(IA+I)+B(IC+I))-(A(IB+I)-A(ID+I)))
D(JD+J)= S3*((A(IA+I)-A(IC+I))+(B(IB+I)+B(ID+I)))
& +C3*((B(IA+I)+B(IC+I))-(A(IB+I)-A(ID+I)))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA=IA+IINK
IB=IB+IINK
IC=IC-IINK
ID=ID-IINK
JBASE=JBASE+JUMP
enddo
if(IB.GT.IC) then
return
endif
endif
IBASE = 0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+A(IB+I)
C(JB+J)=SIN45*((A(IA+I)-A(IB+I))-(B(IA+I)+B(IB+I)))
C(JC+J)=B(IB+I)-B(IA+I)
C(JD+J)=-SIN45*((A(IA+I)-A(IB+I))+(B(IA+I)+B(IB+I)))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
else
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=2._r8*((A(IA+I)+A(IC+I))+A(IB+I))
C(JB+J)=2._r8*((A(IA+I)-A(IC+I))-B(IB+I))
C(JC+J)=2._r8*((A(IA+I)+A(IC+I))-A(IB+I))
C(JD+J)=2._r8*((A(IA+I)-A(IC+I))+B(IB+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
endif
c---IFAC=5
elseif(IFAC.eq.5) then
IA = 1
IB = IA+int((2._r8*FM-FLA)*FINC1)
IC = IB+int(2._r8*FM*FINC1)
ID = IC
IE = IB
JA = 1
JB = JA+JINK
JC = JB+JINK
JD = JC+JINK
JE = JD+JINK
if(.not.LAEQM) then
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=A(IA+I)+(A(IB+I)+A(IC+I))
C(JB+J)=((A(IA+I)-0.25*(A(IB+I)+A(IC+I))) +
& QRT5*(A(IB+I)-A(IC+I)))-(SIN72*B(IB+I)+SIN36*B(IC+I))
C(JC+J)=((A(IA+I)-0.25*(A(IB+I)+A(IC+I))) -
& QRT5*(A(IB+I)-A(IC+I)))-(SIN36*B(IB+I)-SIN72*B(IC+I))
C(JD+J)=((A(IA+I)-0.25*(A(IB+I)+A(IC+I))) -
& QRT5*(A(IB+I)-A(IC+I)))+(SIN36*B(IB+I)-SIN72*B(IC+I))
C(JE+J)=((A(IA+I)-0.25*(A(IB+I)+A(IC+I))) +
& QRT5*(A(IB+I)-A(IC+I)))+(SIN72*B(IB+I)+SIN36*B(IC+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA = IA+IINK
IINK = 2*IINK
IB = IB+IINK
IC = IC+IINK
ID = ID-IINK
IE = IE-IINK
JBASE= JBASE+JUMP
JUMP = 2*JUMP+JINK
if(IB.ne.ID) then
do K=LA,KSTOP,LA
KB=K+K
KC=KB+KB
KD=KC+KB
KE=KD+KB
C1=TRIGS(KB+1)
S1=TRIGS(KB+2)
C2=TRIGS(KC+1)
S2=TRIGS(KC+2)
C3=TRIGS(KD+1)
S3=TRIGS(KD+2)
C4=TRIGS(KE+1)
S4=TRIGS(KE+2)
IBASE=0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
A10(IJK)= +QRT5*((A(IB+I)+A(IE+I))-(A(IC+I)+A(ID+I)))+
& (A(IA+I)-0.25*((A(IB+I)+A(IE+I))+(A(IC+I)+A(ID+I))))
A20(IJK)= -QRT5*((A(IB+I)+A(IE+I))-(A(IC+I)+A(ID+I)))+
& (A(IA+I)-0.25*((A(IB+I)+A(IE+I))+(A(IC+I)+A(ID+I))))
B10(IJK)= +QRT5*((B(IB+I)-B(IE+I))-(B(IC+I)-B(ID+I)))+
& (B(IA+I)-0.25*((B(IB+I)-B(IE+I))+(B(IC+I)-B(ID+I))))
B20(IJK)= -QRT5*((B(IB+I)-B(IE+I))-(B(IC+I)-B(ID+I)))+
& (B(IA+I)-0.25*((B(IB+I)-B(IE+I))+(B(IC+I)-B(ID+I))))
A11(IJK)=SIN72*(B(IB+I)+B(IE+I))+SIN36*(B(IC+I)+B(ID+I))
A21(IJK)=SIN36*(B(IB+I)+B(IE+I))-SIN72*(B(IC+I)+B(ID+I))
B11(IJK)=SIN72*(A(IB+I)-A(IE+I))+SIN36*(A(IC+I)-A(ID+I))
B21(IJK)=SIN36*(A(IB+I)-A(IE+I))-SIN72*(A(IC+I)-A(ID+I))
C(JA+J)=A(IA+I)+((A(IB+I)+A(IE+I))+(A(IC+I)+A(ID+I)))
D(JA+J)=B(IA+I)+((B(IB+I)-B(IE+I))+(B(IC+I)-B(ID+I)))
C(JB+J)=C1*(A10(IJK)-A11(IJK))-S1*(B10(IJK)+B11(IJK))
D(JB+J)=S1*(A10(IJK)-A11(IJK))+C1*(B10(IJK)+B11(IJK))
C(JE+J)=C4*(A10(IJK)+A11(IJK))-S4*(B10(IJK)-B11(IJK))
D(JE+J)=S4*(A10(IJK)+A11(IJK))+C4*(B10(IJK)-B11(IJK))
C(JC+J)=C2*(A20(IJK)-A21(IJK))-S2*(B20(IJK)+B21(IJK))
D(JC+J)=S2*(A20(IJK)-A21(IJK))+C2*(B20(IJK)+B21(IJK))
C(JD+J)=C3*(A20(IJK)+A21(IJK))-S3*(B20(IJK)-B21(IJK))
D(JD+J)=S3*(A20(IJK)+A21(IJK))+C3*(B20(IJK)-B21(IJK))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
IA=IA+IINK
IB=IB+IINK
IC=IC+IINK
ID=ID-IINK
IE=IE-IINK
JBASE=JBASE+JUMP
enddo
if(IB.GT.ID) then
return
endif
endif
IBASE = 0
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=(A(IA+I)+A(IB+I))+A(IC+I)
C(JB+J)=(QRT5*(A(IA+I)-A(IB+I)) +
& (0.25*(A(IA+I)+A(IB+I))-A(IC+I)))
& -(SIN36*B(IA+I)+SIN72*B(IB+I))
C(JE+J)=-(QRT5*(A(IA+I)-A(IB+I)) +
& (0.25*(A(IA+I)+A(IB+I))-A(IC+I)))
& -(SIN36*B(IA+I)+SIN72*B(IB+I))
C(JC+J)=(QRT5*(A(IA+I)-A(IB+I)) -
& (0.25*(A(IA+I)+A(IB+I))-A(IC+I)))
& -(SIN72*B(IA+I)-SIN36*B(IB+I))
C(JD+J)=-(QRT5*(A(IA+I)-A(IB+I)) -
& (0.25*(A(IA+I)+A(IB+I))-A(IC+I)))
& -(SIN72*B(IA+I)-SIN36*B(IB+I))
I=I+INC3
J=J+INC4
enddo
IBASE=IBASE+INC1
JBASE=JBASE+INC2
enddo
else
do L=1,LA
I=IBASE
J=JBASE
do IJK=1,LOT
C(JA+J)=2._r8*(A(IA+I)+(A(IB+I)+A(IC+I)))
C(JB+J)=(2._r8*(A(IA+I)-0.25*(A(IB+I)+A(IC+I)))
& +QQRT5*(A(IB+I)-A(IC+I))) -
& (SSIN72*B(IB+I)+SSIN36*B(IC+I))
C(JC+J)=(2._r8*(A(IA+I)-0.25*(A(IB+I)+A(IC+I)))
& -QQRT5*(A(IB+I)-A(IC+I))) -
& (SSIN36*B(IB+I)-SSIN72*B(IC+I))