-
Notifications
You must be signed in to change notification settings - Fork 7
/
rick_sh_c.c
1322 lines (1242 loc) · 35.5 KB
/
rick_sh_c.c
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
#include "hc.h"
/* //
// compute Legendre function (l,m) evaluated on nlat points
// in latitude and their derviatives with respect to theta, if
// ivec is set to 1
// */
void rick_compute_allplm(int lmax,int ivec,SH_RICK_PREC *plm,
SH_RICK_PREC *dplm, struct rick_module *rick)
{
int i,os;
if (lmax != rick->nlat-1) {
fprintf(stderr,"rick_compute_allplm: error: lmax mismatch: nlat/lmax %i %i \n",rick->nlat,lmax);
/* print *,nlat,lmax */
exit(-1);
}
os=0; /* changed this to 0 TWB */
for (i=0;i < rick->nlat;i++) { /*changed from 1->nlat to 0->nlat-1 - need change in plmbar1 also */
rick_plmbar1((plm+os),(dplm+os),ivec,lmax,rick->gauss_z[i],rick); /*note change in gauss_z[i] */
os += rick->lmsize;
}
}
/* // compute Legendre function (l,m) evaluated on npoints points in
// theta array their derviatives with respect to theta, if ivec is set
// to 1 */
void
rick_compute_allplm_reg (lmax, ivec, plm, dplm, rick, theta, ntheta)
int lmax;
int ivec;
SH_RICK_PREC *plm;
SH_RICK_PREC *dplm;
struct rick_module *rick;
SH_RICK_PREC *theta;
int ntheta;
{
int i,os;
os=0; /* changed this to 0 TWB */
for (i=0;i < ntheta;i++) { /*changed from 1->nlat to 0->nlat-1 - need change in plmbar1 also */
rick_plmbar1((plm+os),(dplm+os),ivec,lmax,cos(theta[i]),rick); /*note change in gauss_z[i] */
os += rick->lmsize;
}
}
/* //
// detemine the colatidude and longitude of PIxel index
// where index goes from 0 ... nlon * nlat-1
// */
void
rick_pix2ang (index, lmax, theta, phi, rick)
int index;
int lmax;
SH_RICK_PREC *theta;
SH_RICK_PREC *phi;
struct rick_module *rick; {
int i,j;
if(!rick->initialized){
fprintf(stderr,"rick_pix2ang: error: rick module not initialized\n");
exit(-1);
}
j = index;
i=0;
while(j > rick->nlonm1) { /* */
j -= rick->nlon;
i++;
}
*theta = rick->gauss_theta[i];
*phi = rick->dphi * (SH_RICK_PREC)(j);
}
void
rick_shc2d (cslm, dslm, lmax, ivec, rdatax, rdatay, rick)
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
int lmax;
int ivec;
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
struct rick_module *rick;
{
//
// Transforms spherical harmonic coefficients of a scalar (ivec = 0)
// or a vector (ivec=1) field
// to data points on a grid. Reverse transform of subroutine
// shd2c.f so long as the same degree is used and the points
// in latitude are Gaussian integration points. Maximum degree
// must be 2**n-1 in order for the FFT to work; this determines
// the grid spacing in longitude. nlat = lmax+1, nlon = 2*nlat
//
// INPUT:
//
// lmax spherical harmonic degree used in expansion
// cslm (lmsize2) spherical harmonic coefficients
// dslm (lmsize2)
// if ivec, will hold poloidal and toroidal coeff,else
// dslm will not be referenced
//
// ivec 0: scalar 1: vector field
// OUTPUT:
// rdatax (nlon * nlat) values on grid points
// rdatay (nlon * nlat). x and y will be theta and phi for
// ivec = 1, else rdatay will not be referenced
//
// if Ivec is set, assume velocities to be expanded instead
//
// input: lmax,ivec
// local
SH_RICK_PREC *plm,*dplm;
if(!rick->initialized){
fprintf(stderr,"rick_shc2d: error: initialize first\n");
exit(-1);
}
// compute the Plm first
if(lmax != rick->nlat-1){
fprintf(stderr,"rick_shc2d: error: lmax mismatch: nlat: %i lmax: %i\n",
rick->nlat,lmax);
exit(-1);
}
/* allocate memory */
rick_vecalloc(&plm,rick->nlat*rick->lmsize,"rick_shc2d: mem 1");
if(ivec)
rick_vecalloc(&dplm,rick->nlat*rick->lmsize,"rick_shc2d: mem 2");
//
// compute the Plm first
rick_compute_allplm(lmax,ivec,plm,dplm,rick);
//
// call the precomputed subroutine
rick_shc2d_pre(cslm,dslm,lmax,plm,dplm,ivec,rdatax,rdatay,rick);
/* free legendre functions if not needed */
free(plm);
if(ivec)
free(dplm);
}
/*
converts on regular basis with locations cos(theta)[], phi[] long
*/
void
rick_shc2d_reg (cslm, dslm, lmax, ivec, rdatax, rdatay, rick, theta, ntheta, phi, nphi, save_sincos_fac)
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
int lmax;
int ivec;
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
struct rick_module *rick;
SH_RICK_PREC *theta;
int ntheta;
SH_RICK_PREC *phi;
int nphi;
hc_boolean save_sincos_fac;
{
SH_RICK_PREC *plm,*dplm;
if(!rick->initialized){
fprintf(stderr,"rick_shc2d: error: initialize first\n");
exit(-1);
}
/* allocate memory */
rick_vecalloc(&plm,ntheta*rick->lmsize,"rick_shc2d_reg: mem 1");
if(ivec)
rick_vecalloc(&dplm,ntheta*rick->lmsize,"rick_shc2d_reg: mem 2");
//
// compute the Plm first for all theta values
rick_compute_allplm_reg(lmax,ivec,plm,dplm,rick,theta,ntheta);
//
// call the precomputed subroutine
rick_shc2d_pre_reg(cslm,dslm,lmax,plm,dplm,ivec,rdatax,rdatay,rick,theta,ntheta,
phi,nphi,save_sincos_fac);
/* free legendre functions if not needed */
free(plm);
if(ivec)
free(dplm);
}
/* //
// the actual routine to go from spectral to spatial: added structure rick
// */
void
rick_shc2d_pre (cslm, dslm, lmax, plm, dplm, ivec, rdatax, rdatay, rick)
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
int lmax;
SH_RICK_PREC *plm;
SH_RICK_PREC *dplm;
int ivec;
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
struct rick_module *rick;
{
/* //
// Legendre functions are precomputed
// */
SH_RICK_PREC *valuex, *valuey;
SH_RICK_PREC dpdt,dpdp;
static int negunity = -1; /* an actual constant */
int i,j,m,m2,j2,ios1,l,lmaxp1,lmaxp1t2,oplm,nlon2,lm1;
if(!rick->initialized){
fprintf(stderr,"rick_shc2d_pre: error: initialize modules first\n");
exit(-1);
}
// check bounds
lmaxp1 = lmax + 1; // this is nlat
lmaxp1t2 = 2 * lmaxp1; // this is nlon
if((rick->nlat != lmaxp1)||(rick->nlon != lmaxp1t2)){
fprintf(stderr,"rick_shc2d_pre: dimension mismatch: lmax: %i nlon: %i nlat:%i\n",
lmax,rick->nlon,rick->nlat);
exit(-1);
}
if(ivec){
if(!rick->vector_sh_fac_init){
fprintf(stderr,"rick_shc2d_pre: error: vector harmonics factors not initialized\n");
exit(-1);
}
}
nlon2 = rick->nlon + 2;
/* allocate value arrays */
rick_vecalloc(&valuex,nlon2,"rick_shc2d_pre 1");
if(ivec)
rick_vecalloc(&valuey,nlon2,"rick_shc2d_pre 2");
for (i=0;i < rick->nlat; i++) {
/*
loop through latitudes
*/
oplm = i * rick->lmsize; /* offset for Plm array. (changed indices TWB) */
ios1 = i * rick->nlon; /* offset for data array */
if(!ivec){
/*
scalar
*/
for(j=0;j < nlon2;j++)
valuex[j] = 0.0; /* init with 0.0es */
l = 0; m = -1;
for (j=j2=0; j < rick->lmsize; j++,j2+=2) { /* loop through l,m */
m++;
if (m > l) {
m=0;
l++;
}
m2 = 2*m;
//fprintf(stderr,"%11i %11i %11g %11g\n",l,m,cslm[j2],cslm[j2+1]);
/* add up contributions from all l,m */
valuex[m2] += /* cos term */
plm[oplm+j] * (SH_RICK_PREC)cslm[j2]; /* A coeff */
valuex[m2+1] += /* sin term */
plm[oplm+j] * (SH_RICK_PREC)cslm[j2+1]; /* B coeff */
}
/* compute inverse FFT */
#ifdef NO_RICK_FORTRAN
rick_cs2ab(valuex,rick->nlon);
rick_realft_nr((valuex-1),rick->nlat,negunity);
#else
rick_f90_cs2ab(valuex,&rick->nlon);
rick_f90_realft(valuex,&rick->nlat,&negunity);
#endif
for (j=0; j < rick->nlon; j++) { /* can't vectorize */
rdatax[ios1 + j] = valuex[j]/(SH_RICK_PREC)(rick->nlat);
}
/* end scalar part */
} else {
/*
vector harmonics
*/
for(j=0;j < nlon2;j++){
valuex[j] = valuey[j] = 0.0;
}
l = 1;
m = -1;
/* start at l = 1 */
for (j=1,j2=2; j < rick->lmsize; j++,j2+=2) {
/* loop through l,m */
m++;
if (m > l) {
m=0;
l++;
}
m2 = 2*m;
/* derivative factors */
lm1 = l - 1;
dpdt = dplm[oplm+j] * (SH_RICK_PREC)rick->ell_factor[lm1]; /* d_theta(P_lm) factor */
dpdp = ((SH_RICK_PREC)m) * plm[oplm+j]/ (SH_RICK_PREC)rick->sin_theta[i];
dpdp *= (SH_RICK_PREC)rick->ell_factor[lm1]; /* d_phi (P_lm) factor */
/* add up contributions from all l,m
u_theta
*/
valuex[m2] += /* cos term */
cslm[j2] * dpdt + dslm[j2+1] * dpdp;
valuex[m2+1] += /* sin term */
cslm[j2+1] * dpdt - dslm[j2] * dpdp;
/*
u_phi
*/
valuey[m2] += // cos term
cslm[j2+1] * dpdp - dslm[j2] * dpdt;
valuey[m2+1] += // sin term
- cslm[j2] * dpdp - dslm[j2+1] * dpdt;
} /* end l,m loop */
/* do inverse FFTs */
#ifdef NO_RICK_FORTRAN
rick_cs2ab(valuex,rick->nlon);
rick_cs2ab(valuey,rick->nlon);
rick_realft_nr((valuex-1),rick->nlat,negunity);
rick_realft_nr((valuey-1),rick->nlat,negunity);
#else
rick_f90_cs2ab(valuex,&rick->nlon);
rick_f90_cs2ab(valuey,&rick->nlon);
rick_f90_realft(valuex,&rick->nlat,&negunity);
rick_f90_realft(valuey,&rick->nlat,&negunity);
#endif
/* assign to output array */
for (j=0; j < rick->nlon; j++) {
rdatax[ios1 + j] = valuex[j]/(SH_RICK_PREC)(rick->nlat);
rdatay[ios1 + j] = valuey[j]/(SH_RICK_PREC)(rick->nlat);
}
}
} /* end latitude loop */
/* free temporary arrays */
free(valuex);
if(ivec)
free(valuey);
}
/*
regularly spaced version, data are requested on a ntheta by nphi grid,
with nphi values in each column, located at phi[], and ntheta values
in each row at theta[]
*/
void
rick_shc2d_pre_reg (cslm, dslm, lmax, plm, dplm, ivec, rdatax, rdatay, rick, theta, ntheta, phi, nphi, save_sincos_fac)
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
int lmax;
SH_RICK_PREC *plm;
SH_RICK_PREC *dplm;
int ivec;
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
struct rick_module *rick;
SH_RICK_PREC *theta;
int ntheta;
SH_RICK_PREC *phi;
int nphi;
my_boolean save_sincos_fac;
{
/* //
// Legendre functions are precomputed
// */
SH_RICK_HIGH_PREC dpdt,dpdp,mphi,sin_theta;
SH_RICK_PREC *loc_plma=NULL,*loc_plmb=NULL;
int i,j,k,k2,m,ios1,ios2,ios3,l,lmaxp1,lm1,idata;
if(!rick->initialized){
fprintf(stderr,"rick_shc2d_pre_reg: error: initialize modules first\n");
exit(-1);
}
lmaxp1 = lmax + 1;
if(ivec){
if(!rick->vector_sh_fac_init){
fprintf(stderr,"rick_shc2d_pre_reg: error: vector harmonics factors not initialized\n");
exit(-1);
}
}
if((lmax+1)*(lmax+2)/2 > rick->lmsize){
fprintf(stderr,"rick_shc2d_pre_reg: error: lmax %i out of bounds\n",lmax);
exit(-1);
}
/*
compute sin/cos factors
*/
if((!save_sincos_fac)||(!rick->sin_cos_saved)){
rick_vecrealloc(&rick->sfac,nphi*lmaxp1,"rick_shc2d_pre_reg");
rick_vecrealloc(&rick->cfac,nphi*lmaxp1,"rick_shc2d_pre_reg");
for(ios1=i=0;i < nphi;i++){
for(m=0;m <= lmax;m++,ios1++){
mphi = (SH_RICK_HIGH_PREC)m*(SH_RICK_HIGH_PREC)phi[i];
rick->sfac[ios1] = (SH_RICK_PREC)sin(mphi);
rick->cfac[ios1] = (SH_RICK_PREC)cos(mphi);
}
}
if(save_sincos_fac)
rick->sin_cos_saved = TRUE;
}
if(ivec == 0){
/*
scalar
*/
rick_vecrealloc(&loc_plma,ntheta*rick->lmsize,"rick_shc2d_pre_reg 3");
rick_vecrealloc(&loc_plmb,ntheta*rick->lmsize,"rick_shc2d_pre_reg 4");
for(i=ios1=0;i < ntheta;i++){ /* theta dependent array */
for(k=k2=0;k < rick->lmsize;k++,k2+=2,ios1++){
loc_plma[ios1] = cslm[k2 ] * plm[ios1];
loc_plmb[ios1] = cslm[k2+1] * plm[ios1];
}
}
for (idata=i=ios2=0;i < ntheta; i++,ios2 += rick->lmsize) { /* theta loop */
for(ios3=j=0;j < nphi;j++,idata++,ios3 += lmaxp1){ /* phi loop */
/* m = 0 , l = 0*/
l=0;m=0;
rdatax[idata] = loc_plma[ios2];
for (k=1; k < rick->lmsize; k++) {
m++;
if (m > l) {
m=0;l++;
}
rdatax[idata] += loc_plma[ios2+k] * rick->cfac[ios3+m];
if(m != 0)
rdatax[idata] += loc_plmb[ios2+k] * rick->sfac[ios3+m];
}
}
}
free(loc_plma);free(loc_plmb);
/* end scalar part */
} else {
/*
vector harmonics
*/
for (idata=i=ios2=0;i < ntheta; i++,ios2 += rick->lmsize) { /* theta
loop */
sin_theta = sin(theta[i]);
for(ios3=j=0;j < nphi;j++,idata++,ios3 += lmaxp1){ /* phi loop */
rdatax[idata] = rdatay[idata] = 0.0;
l = 0;m = 0;
/* start at l = 1 */
for (k=1,k2=2; k < rick->lmsize; k++,k2+=2) {
/* loop through l,m */
m++;
if (m > l) {
m=0;l++;
}
lm1 = l - 1;
// fprintf(stderr,"%5i %5i\t %11g %11g\tPA: %11g PB: %11g\tTA: %11g TB:%11g\n",
// l,m,rick->ell_factor[lm1],plm[ios2+k],
// SH_RICK_FACTOR(l, m) * cslm[k2] ,SH_RICK_FACTOR(l, m) *cslm[k2+1],
// SH_RICK_FACTOR(l, m) * dslm[k2], SH_RICK_FACTOR(l, m) *dslm[k2+1]);
dpdt = dplm[ios2+k] * rick->ell_factor[lm1]; /* d_theta(P_lm) factor */
dpdp = ((SH_RICK_PREC)m) * plm[ios2+k]/ sin_theta;
dpdp *= (SH_RICK_PREC)rick->ell_factor[lm1]; /* d_phi (P_lm) factor */
/*
add up contributions from all l,m
u_theta
*/
rdatax[idata] += /* cos term */
(cslm[k2] * dpdt + dslm[k2+1] * dpdp) * rick->cfac[ios3+m];
rdatax[idata] += /* sin term */
(cslm[k2+1] * dpdt - dslm[k2] * dpdp) * rick->sfac[ios3+m];
/*
u_phi
*/
rdatay[idata] += // cos term
(cslm[k2+1] * dpdp - dslm[k2] * dpdt) * rick->cfac[ios3+m];
rdatay[idata] += // sin term
(- cslm[k2] * dpdp - dslm[k2+1] * dpdt) * rick->sfac[ios3+m];
}
//fprintf(stderr,"%11g %11g %11g %11g\n",theta[j],phi[i],rdatax[i],rdatay[i]);
} /* end phi loop */
} /* end theta loop */
}
if(!save_sincos_fac){
rick_vecrealloc(&rick->sfac,1,"");
rick_vecrealloc(&rick->cfac,1,"");
}
}
/* completely irregular output */
void
rick_shc2d_irreg (cslm, dslm, lmax, ivec, rdatax, rdatay, rick, theta, phi, npoints)
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
int lmax;
int ivec;
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
struct rick_module *rick;
SH_RICK_PREC *theta;
SH_RICK_PREC *phi;
int npoints;
{
/* //
// Legendre functions are precomputed
// */
SH_RICK_HIGH_PREC dpdt,dpdp,mphi,sin_theta,sfac,cfac;
SH_RICK_PREC *plm=NULL,*dplm=NULL;
int i,k,k2,m,l,lm1;
if(!rick->initialized){
fprintf(stderr,"rick_shc2d_pre_reg: error: initialize modules first\n");
exit(-1);
}
if((lmax+1)*(lmax+2)/2 > rick->lmsize){
fprintf(stderr,"rick_shc2d_pre_reg: error: lmax %i out of bounds\n",lmax);
exit(-1);
}
if(ivec == 0){
/*
scalar
*/
rick_vecrealloc(&plm,rick->lmsize,"rick_shc2d_irreg: mem 1");
for(i=0;i < npoints;i++){
/* get legendre function values */
rick_plmbar1(plm,dplm,ivec,lmax,cos(theta[i]),rick);
/* m = 0 , l = 0*/
l=0;m=0;
rdatax[i] = cslm[0] * plm[0];
for (k=1,k2=2; k < rick->lmsize; k++,k2+=2) {
m++;
if (m > l) {
m=0;l++;
}
//fprintf(stderr,"%5i %5i %11g %11g\n",l,m,cslm[k2],cslm[k2+1]);
if(m != 0){
mphi = (SH_RICK_HIGH_PREC)m*(SH_RICK_HIGH_PREC)phi[i];
rdatax[i] += cslm[k2] * plm[k] * cos(mphi);
rdatax[i] += cslm[k2+1] * plm[k] * sin(mphi);
}else{
rdatax[i] += cslm[k2] * plm[k] ;
}
}
} /* end data loop */
free(plm);
/* end scalar part */
} else {
/*
vector harmonics
*/
rick_vecrealloc(&plm,rick->lmsize,"rick_shc2d_irreg: mem 1");
rick_vecrealloc(&dplm,rick->lmsize,"rick_shc2d_irreg: mem 2");
for(i=0;i < npoints;i++){
/* get legendre function values */
rick_plmbar1(plm,dplm,ivec,lmax,cos(theta[i]),rick);
sin_theta = sin(theta[i]);
rdatax[i] = rdatay[i] = 0.0;
l = 0;m = 0;
/* start at l = 1 */
for (k=1,k2=2; k < rick->lmsize; k++,k2+=2) {
/* loop through l,m */
m++;
if (m > l) {
m=0;l++;
}
lm1 = l - 1;
// fprintf(stderr,"%5i %5i\t %11g %11g\tPA: %11g PB: %11g\tTA: %11g TB:%11g\n",
// l,m,rick->ell_factor[lm1],plm[ios2+k],
// SH_RICK_FACTOR(l, m) * cslm[k2] ,SH_RICK_FACTOR(l, m) *cslm[k2+1],
// SH_RICK_FACTOR(l, m) * dslm[k2], SH_RICK_FACTOR(l, m) *dslm[k2+1]);
dpdt = dplm[k] * rick->ell_factor[lm1]; /* d_theta(P_lm) factor */
dpdp = ((SH_RICK_PREC)m) * plm[k]/ sin_theta;
dpdp *= (SH_RICK_PREC)rick->ell_factor[lm1]; /* d_phi (P_lm) factor */
if(m){
mphi = (SH_RICK_HIGH_PREC)m*(SH_RICK_HIGH_PREC)phi[i];
sfac = (SH_RICK_PREC)sin(mphi);
cfac = (SH_RICK_PREC)cos(mphi);
}else{
mphi = sfac = 0.0;
cfac = 1.0;
}
/*
add up contributions from all l,m
u_theta
*/
rdatax[i] += /* cos term */
(cslm[k2] * dpdt + dslm[k2+1] * dpdp) * cfac;
rdatax[i] += /* sin term */
(cslm[k2+1] * dpdt - dslm[k2] * dpdp) * sfac;
/*
u_phi
*/
rdatay[i] += // cos term
(cslm[k2+1] * dpdp - dslm[k2] * dpdt) * cfac;
rdatay[i] += // sin term
(- cslm[k2] * dpdp - dslm[k2+1] * dpdt) * sfac;
}
} /* end phi loop */
free(plm);free(dplm);
} /* end vector part */
}
void
rick_shd2c (rdatax, rdatay, lmax, ivec, cslm, dslm, rick)
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
int lmax;
int ivec;
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
struct rick_module *rick;
{
//
// Calculates spherical harmonic coefficients cslm(l,m) of
// a scalar (ivec = 0) or vector (ivec=1) function on a sphere.
//
// if ivec == 1, then cslm will be poloidal, and dslm toroidal
// rdata will be nlon*nlat
//
// Coefficients stored with
// cosine term and sine term alternating, starting at l=0
// and m=0 with m ranging from 0 to l before l is incremented.
//
// Harmonics normalized with mean square = 1.0. Expansion is:
// SUM( Plm(cos(theta))*(cp(l,m)*cos(m*phi)+sp(l,m)*sin(m*phi))
//
// Uses FFT in longitude and gaussian integration of spectral
// coefficients (for fixed m) times associated Legendre
// functions (of same order m) to get expansion coefficients.
//
// Uses Num Rec routine for Gaussian points and weights, which should
// be initialized first by a call to rick_init.
//
// Uses recursive routine to generate associated Legendre functions.
//
// INPUT:
//
// lmax maximum possible degree of expansion (2**n-1). There
// are nlon = 2*lmax+2 data points in longitude for each latitude
// which has nlat = lmax+1 points
//
// rdatax,rdatay data((nlon * nlat)) arrays for theta and phi
// components
//
// ic_pd: should be either 0.0_cp or 1.0_cp
// if set to 1.0_cp, will expand velocities instead
// in this case, data should hold the theta and phi components
// of a vector field and cslm will hold the poloidal and toroidal
// on output components, respectively
//
// OUTPUT:
//
// cslm,dslm coefficients, (2*lmsize)
//
// dslm and rdatay will only be referenced when ivec = 1
//
//
// local
SH_RICK_PREC *plm,*dplm;
/* allocate memory */
rick_vecalloc(&plm,rick->nlat*rick->lmsize,"rick_shd2c: mem 1");
rick_vecalloc(&dplm,rick->nlat*rick->lmsize,"rick_shd2c: mem 2");
// check
if(!rick->initialized){
fprintf(stderr,"rick_shd2c: error: initialize first\n");
exit(-1);
}
if(lmax != rick->nlat-1){
fprintf(stderr,"rick_shd2c: error: lmax mismatch: nlat: %i lmax: %i\n",
rick->nlat,lmax);
exit(-1);
}
// compute the Plm first
rick_compute_allplm(lmax,ivec,plm,dplm,rick);
//
// call the precomputed version
rick_shd2c_pre(rdatax,rdatay,lmax,plm,dplm,ivec,cslm,dslm,rick);
/* free legendre functions if not needed */
free(plm);
free(dplm);
}
//
// the actual routine to go from spatial to spectral,
// for comments, see above
//
void
rick_shd2c_pre (rdatax, rdatay, lmax, plm, dplm, ivec, cslm, dslm, rick)
SH_RICK_PREC *rdatax;
SH_RICK_PREC *rdatay;
int lmax;
SH_RICK_PREC *plm;
SH_RICK_PREC *dplm;
int ivec;
SH_RICK_PREC *cslm;
SH_RICK_PREC *dslm;
struct rick_module *rick;
{
// local
SH_RICK_PREC *valuex, *valuey;
SH_RICK_PREC dfact,dpdt,dpdp;
static int unity = 1; /* constant */
//
int lmaxp1,lmaxp1t2,i,j,l,m,ios1,m2,j2,oplm,nlon2,lm1;
// check
if(!rick->initialized){
fprintf(stderr,"rick_shd2c_pre: error: initialize first\n");
exit(-1);
}
// check some more and compute bounds
lmaxp1 = lmax + 1;
lmaxp1t2 = lmaxp1 * 2;
nlon2 = rick->nlon + 2;
if((lmaxp1 != rick->nlat)||(rick->nlon != lmaxp1t2)||
((lmax+1)*(lmax+2)/2 != rick->lmsize)){
fprintf(stderr,"rick_shd2c_pre: dimension error, lmax %i\n",lmax);
fprintf(stderr,"rick_shd2c_pre: nlon %i nlat %i\n",rick->nlon,rick->nlat);
fprintf(stderr,"rick_shd2c_pre: lmsize %i\n",rick->lmsize);
exit(-1);
}
/* allocate */
rick_vecalloc(&valuex,nlon2,"rick_shd2c_pre 1");
if(ivec)
rick_vecalloc(&valuey,nlon2,"rick_shd2c_pre 2");
//
// initialize the coefficients
//
for(i=0;i < rick->lmsize2;i++)
cslm[i] = 0.0;
if(ivec){
if(! rick->vector_sh_fac_init){
fprintf(stderr,"rick_shd2c_pre: error: vector harmonics factors not initialized\n");
exit(-1);
}
for(i=0;i < rick->lmsize2;i++)
dslm[i] = 0.0;
}
for(i=0;i < rick->nlat;i++){
//
// loop through latitudes
//
ios1 = i * rick->nlon; // offset for data array
oplm = i * rick->lmsize; // offset for Plm array
//
if(!ivec){
//
// scalar expansion
//
for(j=0;j < rick->nlon;j++){
valuex[j] = rdatax[ios1 + j];
}
//
// compute the FFT
//
#ifdef NO_RICK_FORTRAN
rick_realft_nr((valuex-1),rick->nlat,unity);
rick_ab2cs(valuex,rick->nlon);
#else
rick_f90_realft(valuex,&rick->nlat,&unity);
rick_f90_ab2cs(valuex,&rick->nlon);
#endif
// sum up for integration
l = 0;m = -1;
for(j=j2=0;j < rick->lmsize;j++,j2+=2){
m++;
if( m > l ) {
l++;m=0;
}
// we incorporate the Gauss integration weight and Plm factors here
if (m == 0) {
dfact = ((SH_RICK_PREC)rick->gauss_w[i] * plm[oplm+j])/2.0;
}else{
dfact = ((SH_RICK_PREC)rick->gauss_w[i] * plm[oplm+j])/4.0;
}
m2 = m * 2;
cslm[j2] += valuex[m2] * dfact; // A coefficient
cslm[j2+1] += valuex[m2+1] * dfact; // B coefficient
} /* end lmsize loop */
/* end scalar */
}else{
//
// vector field expansion
//
for(j=0;j < rick->nlon;j++){
valuex[j] = rdatax[ios1 + j]; // theta
valuey[j] = rdatay[ios1 + j]; // phi
}
// perform the FFTs on both components
#ifdef NO_RICK_FORTRAN
rick_realft_nr((valuex-1),rick->nlat,unity);
rick_realft_nr((valuey-1),rick->nlat,unity);
rick_ab2cs(valuex,rick->nlon);
rick_ab2cs(valuey,rick->nlon);
#else
rick_f90_realft(valuex,&rick->nlat,&unity);
rick_f90_realft(valuey,&rick->nlat,&unity);
rick_f90_ab2cs(valuex,&rick->nlon);
rick_f90_ab2cs(valuey,&rick->nlon);
#endif
//
l=1;m=-1; // there's no l=0 term
//
for(j = 1,j2 = 2;j < rick->lmsize;j++,j2+=2){
m++;
if( m >l ) {
l++;m=0;
}
lm1 = l - 1;
if (m == 0){ // ell_factor is 1/sqrt(l(l+1))
dfact = rick->gauss_w[i] * rick->ell_factor[lm1]/2.0;
}else{
dfact = rick->gauss_w[i] * rick->ell_factor[lm1]/4.0;
}
//
// some more factors
//
// d_theta(P_lm) factor
dpdt = dplm[oplm+j];
// d_phi (P_lm) factor
dpdp = ((SH_RICK_PREC)m) * plm[oplm+j]/(SH_RICK_PREC)rick->sin_theta[i];
//
m2 = m * 2;
// print *,m,l,dpdt*dfact,dpdp*dfact
/* poloidal */
cslm[j2] += // poloidal A
(dpdt * valuex[m2] - dpdp * valuey[m2+1])*dfact;
cslm[j2+1] += // poloidal B
(dpdt * valuex[m2+1] + dpdp * valuey[m2] )*dfact;
/* toroidal */
dslm[j2] += // toroidal A
(-dpdp * valuex[m2+1] - dpdt * valuey[m2] )*dfact;
dslm[j2+1] += // toroidal B
( dpdp * valuex[m2] - dpdt * valuey[m2+1])*dfact;
}
} // end vector field
} // end latitude loop
free(valuex);
if(ivec)
free(valuey);
}
//
// initialize all necessary arrays for Rick type expansion
//
// if ivec == 1, will initialize for velocities/polarizations
//
/* if regular is set, will not initialize the Gauss quadrature
points */
void
rick_init (lmax, ivec, npoints, nplm, tnplm, rick, regular)
int lmax;
int ivec;
int *npoints;
int *nplm;
int *tnplm;
struct rick_module *rick;
hc_boolean regular;
{
// input: lmax,ivec
// output: npoints,nplm,tnplm
// local
SH_RICK_PREC xtemp;
int i,l;
//fprintf(stderr,"rick_init: lmax: %i was_called: %i\n",lmax,rick->was_called);
if(!rick->was_called){
if(lmax == 0){
fprintf(stderr,"rick_init: error: lmax is zero: %i\n",lmax);
exit(-1);
}
//
// test if lmax is 2**n-1
//
xtemp = log((SH_RICK_PREC)(lmax+1))/log(2.0);
if(fabs((int)(xtemp+.5)-xtemp) > 1e-7){
fprintf(stderr,"rick_init: error: lmax has to be 2**n-1\n");
fprintf(stderr,"rick_init: maybe use lmax = %i\n",
(int)pow(2,(int)(xtemp)+1)-1);
fprintf(stderr,"rick_init: instead of %i\n",lmax);
exit(-1);
}
//
// number of longitudinal and latitudinal points
//
rick->nlat = lmax + 1;
rick->nlon = 2 * rick->nlat;
//
// number of points in one layer
//
*npoints = rick->nlat * rick->nlon;
rick->old_npoints = *npoints;
//
//
// for coordinate computations
//
rick->dphi = RICK_TWOPI / (SH_RICK_PREC)(rick->nlon);
rick->nlonm1 = rick->nlon - 1;
//
// size of tighly packed arrays with l,m indices
rick->lmsize = (lmax+1)*(lmax+2)/2;
rick->lmsize2 = rick->lmsize * 2; //for A and B
//
//
// size of the Plm array
*nplm = rick->lmsize * rick->nlat;
*tnplm = *nplm * (1+ivec); // for all layers
rick->old_tnplm = *tnplm;
rick->old_nplm = *nplm;
//
// initialize the Gauss points, at which the latitudes are
// evaluated
//
if(!regular){
rick_vecalloc(&rick->gauss_z,rick->nlat,"rick_init 1");
rick_vecalloc(&rick->gauss_w,rick->nlat,"rick_init 2");
rick_vecalloc(&rick->gauss_theta,rick->nlat,"rick_init 3");
/*
gauss weighting
*/
rick_gauleg(-1.0,1.0,rick->gauss_z,rick->gauss_w,rick->nlat);
//
// theta values of the Gauss quadrature points
//
for(i=0;i < rick->nlat;i++){
rick->gauss_theta[i] = acos(rick->gauss_z[i]);
}
}
//
// those will be used by plmbar to store some of the factors
//
rick_vecalloc(&rick->plm_f1,rick->lmsize,"rick_init 4");
rick_vecalloc(&rick->plm_f2,rick->lmsize,"rick_init 5");
rick_vecalloc(&rick->plm_fac1,rick->lmsize,"rick_init 6");
rick_vecalloc(&rick->plm_fac2,rick->lmsize,"rick_init 7");
rick_vecalloc(&rick->plm_srt,rick->nlon,"rick_init 8");
rick->sin_cos_saved = FALSE;
if(ivec){
//
// additional arrays for vector spherical harmonics
// (perform the computations in SH_RICK_PREC precision)
//
rick_vecalloc(&rick->ell_factor,rick->nlat,"rick init 9");
if(!regular)
rick_vecalloc(&rick->sin_theta,rick->nlat,"rick init 9");
// 1/(l(l+1)) factors
for(i=0,l=1;i < rick->nlat;i++,l++){
// no l=0 term, obviously
// go from l=1 to l=lmax+1
rick->ell_factor[i] = 1.0/sqrt((SH_RICK_PREC)(l*(l+1)));
if(!regular){
rick->sin_theta[i] = sqrt((1.0 - rick->gauss_z[i])*
(1.0+rick->gauss_z[i]));
}
rick->vector_sh_fac_init = TRUE;
}
}else{
rick->vector_sh_fac_init = FALSE;
}
//
// logic flags
//
rick->computed_legendre = FALSE;
rick->initialized = TRUE;
rick->was_called = TRUE;
/*
save initial call lmax and ivec settings
*/
rick->old_lmax = lmax;
rick->old_ivec = ivec;
/* for regular expansions */
rick->cfac = rick->sfac = NULL;
/* end initial branch */