-
Notifications
You must be signed in to change notification settings - Fork 7
/
sh_exp.c
2356 lines (2141 loc) · 54 KB
/
sh_exp.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
/*
lower level routines to handle spherical harmonics expansions
the higher level routines that operate on single (or triple)
sets of expansions are in sh_model.c
$Id: sh_exp.c,v 1.15 2006/03/20 05:32:48 becker Exp becker $
*/
#include "hc.h"
/*
allocates and initializes spherical harmonics structure
*/
void
sh_allocate_and_init (exp, n, lmax, type, ivec, verbose, regular)
struct sh_lms **exp;
int n;
int lmax;
int type;
int ivec;
hc_boolean verbose;
hc_boolean regular;
{
int i;
/* init as zeroes! (but this won't necessarily set the logic flags!) */
*exp = (struct sh_lms *)calloc(n,sizeof(struct sh_lms));
if(!(*exp))
HC_MEMERROR("sh_allocate_and_init");
for(i=0;i < n;i++){
sh_init_expansion((*exp+i),lmax,type,ivec,verbose,regular);
}
}
/*
compute the parameters needed for a single expansion of order lmax and
allocate initial array for spectral coefficients. the type of array
depends on the type of expansion.
if you want vector harmonics at any point during a computation, set
ivec to unity initially
coefficients are initialized as zero
if regular is set, will not use Gauss points
*/
void
sh_init_expansion (exp, lmax, type, ivec, verbose, regular)
struct sh_lms *exp;
int lmax;
int type;
int ivec;
hc_boolean verbose;
hc_boolean regular;
{
/*
initialize logic flags
*/
exp->spectral_init = FALSE;
/* type of expansion, e.g. SH_HEALPIX or SH_RICK, this will be checked later */
exp->type = type;
#ifdef HC_DEBUG
/*
l and m bounds
*/
if(lmax < 1){
fprintf(stderr,"sh_init_expansion: error: lmax out of bounds: %i\n",
lmax);
exit(-1);
}
#endif
exp->lmax = lmax;
/* same as above, plus one */
exp->lmaxp1 = exp->lmax+1;
/*
size of one set of coefficients (l,m) if stored by wasting space
(assuming we are using complex numbers)
*/
exp->lmbig = exp->lmaxp1 * exp->lmaxp1;
/*
size of coefficients when stored compactly like
(l+1)*l/2 + m, times two for A and B
*/
exp->lmsmall2 = (exp->lmaxp1)*(exp->lmaxp1+1); /* for A and B */
/*
*/
exp->plm_computed = FALSE;
/*
allocate the spectral (coefficients) storage and initialize possibly
other arrays
*/
switch(exp->type){
#ifdef HC_USE_HEALPIX
case SH_HEALPIX: /* SH_HEALPIX part */
if(regular)
HC_ERROR("regular init not implemented for healpix");
/*
get single precision complex array which holds A and B
*/
exp->n_lm = exp->lmbig;
hc_scmplx_vecalloc(&exp->alm_c,exp->n_lm,"init_expansion");
sh_clear_alm(exp);
/*
init the Healpix parameters and determine the number of points
in the spatial domain
*/
heal_init_parameters(&exp->heal,(lmax/2)+2,SH_HEALPIX_RING,
ivec,exp->lmax,&exp->npoints,
&exp->n_plm,&exp->tn_plm);
break;
#endif
case SH_RICK: /* SH_RICK PART */
exp->rick.was_called = FALSE; /* this used to work via a calloc
call, but because not int, make
sure to work */
exp->rick.computed_legendre =
exp->rick.initialized =
exp->rick.vector_sh_fac_init =
exp->rick.sin_cos_saved = FALSE;
/*
make room for the coefficients A and B in compact storage
*/
exp->n_lm = exp->lmsmall2;
/*
use single precision vector
*/
rick_vecalloc(&exp->alm,exp->n_lm,"sh_init_expansion");
sh_clear_alm(exp); /* set to zero */
/*
init the parameters for Rick subroutines
*/
#ifdef NO_RICK_FORTRAN
rick_init(exp->lmax,ivec,&exp->npoints,
&exp->n_plm,&exp->tn_plm,&exp->rick,regular);
#else
/* f90 version */
rick_f90_init(&exp->lmax,&ivec,&exp->npoints,
&exp->n_plm,&exp->tn_plm,regular);
#endif
break;
#ifdef HC_USE_SPHEREPACK
case SH_SPHEREPACK_GAUSS:
case SH_SPHEREPACK_EVEN:
HC_ERROR("init_expansion","Spherepack not implemented");
/*
make room for coefficients
*/
/*
initialize
*/
break;
#endif
default:
sh_exp_type_error("sh_init_expansion",exp);
break;
}
}
/*
free an expansion structure with n elements
*/
void
sh_free_expansion (exp, n)
struct sh_lms *exp;
int n;
{
int i;
for(i=0;i<n;i++){
switch(exp[i].type){
#ifdef HC_USE_HEALPIX
case SH_HEALPIX: /* SH_HEALPIX part */
heal_free_structure(&exp[i].heal);
free(exp[i].alm_c);
break;
#endif
case SH_RICK:
free(exp[i].alm);
break;
#ifdef HC_USE_SPHEREPACK
case SH_SPHEREPACK_GAUSS:
case SH_SPHEREPACK_EVEN:
HC_ERROR("free_expansion","Spherepack not implemented");
break;
#endif
default:
sh_exp_type_error("sh_free_expansion",(exp+i));
break;
}
}
}
/*
zero out all coefficients
*/
void
sh_clear_alm (exp)
struct sh_lms *exp;
{
int i;
switch(exp->type){
#ifdef HC_USE_HEALPIX
case SH_HEALPIX:
/* init with zeroes */
for(i=0;i<exp->n_lm;i++)
exp->alm_c[i].dr = exp->alm_c[i].di = 0.0;
break;
#endif
case SH_RICK:
for(i=0;i<exp->n_lm;i++)
exp->alm[i] = 0.0;
break;
#ifdef HC_USE_SPHEREPACK
case SH_SPHEREPACK_GAUSS:
case SH_SPHEREPACK_EVEN:
HC_ERROR("clear_alm","Spherepack not implemented");
break;
#endif
default:
sh_exp_type_error("sh_clear_alm",exp);
break;
}
}
/*
compute | \Psi | ^2 = \sum (2l+1) \sigma^2
kindof RMS^2
*/
HC_CPREC
sh_total_power (exp)
struct sh_lms *exp;
{
HC_PREC *power;
double sum;
int l;
hc_vecalloc(&power,exp->lmaxp1,"sh_total_power");
sh_compute_power_per_degree(exp,power);
for(sum=0.0,l=0;l<=exp->lmax;l++)
sum += (double)(2.0*(HC_CPREC)l+1.0) * (double)power[l];
free(power);
return (HC_CPREC)sum;
}
HC_CPREC sh_total_rms (exp)
struct sh_lms *exp;
{
HC_PREC *power;
HC_PREC sum;
int l;
hc_vecalloc(&power,exp->lmaxp1,"sh_total_power");
sh_compute_power_per_degree(exp,power);
for(sum=0.0,l=1;l<=exp->lmax;l++){
//fprintf(stderr,"%g\n",(double)power[l]);
sum += (double)(2.0*(HC_CPREC)l+1.0) * (double)power[l];
}
free(power);
return sqrt(sum);
}
/*
compute the power (sigma^2) per degree and unit area
power[lmaxp1]
*/
void
sh_compute_power_per_degree (exp, power)
struct sh_lms *exp;
HC_PREC *power;
{
int l,m;
HC_CPREC value[2];
hc_boolean need_b;
for(l=0;l<=exp->lmax;l++){
power[l] = 0.0;
for(m=0;m<=l;m++){
need_b = (hc_boolean) ((m == 0) ? (0) : (2));
sh_get_coeff(exp,l,m,need_b,TRUE,value); /* convert to DT
normalization */
power[l] += value[0] * value[0];
if(need_b)
power[l] += value[1] * value[1];
} /* end m loop */
power[l] /= 2.0*((HC_CPREC)l)+1.0;
} /* end l loop */
}
/* compute total correlation up to llim */
HC_PREC
sh_correlation (exp1, exp2, llim)
struct sh_lms *exp1;
struct sh_lms *exp2;
int llim;
{
return sh_correlation_per_degree(exp1,exp2,1,llim);
}
HC_PREC
sh_correlation_per_degree (exp1, exp2, lmin, lmax)
struct sh_lms *exp1;
struct sh_lms *exp2;
int lmin;
int lmax;
{
int l,m;
HC_CPREC sum[3],tmp,atmp,btmp,ctmp,value1[2],value2[2];
double dtmp;
hc_boolean need_b;
sum[0]=sum[1]=sum[2]=0.0;
if((lmax > exp1->lmax)||(lmax > exp2->lmax)||(lmax < 1)||(lmin < 1)){
fprintf(stderr,"sh_compute_correlation_per_degree: error: L1 %i L2 %i lmin %i lmax %i\n",
exp1->lmax,exp2->lmax,lmin,lmax);
exit(-1);
}
for(l=lmin;l <= lmax;l++){
for(m=0;m<=l;m++){
need_b = (hc_boolean) ((m == 0) ? (0) : (2));
sh_get_coeff(exp1,l,m,need_b,TRUE,value1); /* convert to DT normalization */
sh_get_coeff(exp2,l,m,need_b,TRUE,value2); /* convert to DT normalization */
atmp = value1[0];
ctmp = value2[0];
sum[0] += atmp * ctmp;
sum[1] += atmp * atmp;
sum[2] += ctmp * ctmp;
if(need_b){
btmp = value1[1];
dtmp = value2[1];
sum[0] += btmp* dtmp;
sum[1] += btmp * btmp;
sum[2] += dtmp * dtmp;
}
} /* end m loop */
} /* end l loop */
tmp = sqrt(sum[1]*sum[2]);
return sum[0]/tmp;
}
void
sh_single_par_and_exp_to_file (exp, name, binary, verbose)
struct sh_lms *exp;
char *name;
hc_boolean binary;
hc_boolean verbose;
{
FILE *out;
out = fopen(name,"w");
if(!out){
fprintf(stderr,"sh_single_par_and_exp_to_file: ERROR: problem openeing %s\n",name);
exit(-1);
}
sh_single_par_and_exp_to_stream(exp,out,binary,verbose);
fclose(out);
if(verbose)
fprintf(stderr,"sh_single_par_and_exp_to_file: written to %s\n",name);
}
void
sh_single_par_and_exp_to_stream (exp, out, binary, verbose)
struct sh_lms *exp;
FILE *out;
hc_boolean binary;
hc_boolean verbose;
{
HC_PREC fac[1]={1.0};
const hc_boolean short_format = FALSE;
sh_print_parameters_to_stream(exp,1,0,1,0,out,short_format,binary,verbose);
sh_print_coefficients_to_stream(exp,1,out,fac,binary,verbose);
}
/*
print one line with all parameters needed to identify a spherical
harmonics expansion for a scalar (shps == 1), poloidal/toroidal (shps
== 2), or a vector field (shps == 3)
exp[shps]
ilayer: the layer index (0...nset-1) for nset layers
zlabel: label for layer ilayer
if short_format is selected, will only print
lmax
*/
void
sh_print_parameters_to_stream (exp, shps, ilayer, nset, zlabel, out, short_format, binary, verbose)
struct sh_lms *exp;
int shps;
int ilayer;
int nset;
HC_CPREC zlabel;
FILE *out;
hc_boolean short_format;
hc_boolean binary;
hc_boolean verbose;
{
HC_PREC fz;
/*
print
lmax i z[i] nset shps expansion_type
*/
if(binary){
fz = (HC_PREC)zlabel;
fwrite(&exp[0].lmax,sizeof(int),1,out);
if(!short_format){
fwrite(&ilayer,sizeof(int),1,out);
hc_print_float(&fz,1,out);
fwrite(&nset,sizeof(int),1,out);
fwrite(&shps,sizeof(int),1,out);
fwrite(&exp[0].type,sizeof(int),1,out);
}
}else{
if(!short_format)
fprintf(out,"%6i %6i %.8e %6i %2i %2i ",
exp[0].lmax,ilayer,
(double)zlabel,nset,
shps,exp[0].type);
else
fprintf(out,"%6i ",
exp[0].lmax);
}
/*
additional parameters?
*/
switch(exp[0].type){
case SH_RICK:
break;
#ifdef HC_USE_HEALPIX
case SH_HEALPIX:
break;
#endif
#ifdef HC_USE_SPHEREPACK
case SH_SPHEREPACK_GAUSS:
case SH_SPHEREPACK_EVEN:
break;
#endif
default:
sh_exp_type_error("sh_print_parameters",exp);
break;
}
if(!binary)
fprintf(out,"\n"); /* finish header line */
}
/*
read parameters needed to initialize an expansion
can be used to read several layers of a model
will return TRUE, if success
default (long format) input is
type lmax shps ilayer nset zlabel ivec
short format is just
lmax
explanations:
type: type of spherical harmonics expansion
lmax: max degree
shps: 1 or 3 for different sets of expansions, scalar or scalar + vector
ilayer: 0..nset
nset: number of sets
zlabel: float label of this set
ivec: scalar/vector flag. 0 for shps==1, 1 else
*/
hc_boolean
sh_read_parameters_from_stream (type, lmax, shps, ilayer, nset, zlabel, ivec, in, short_format, binary, verbose)
int *type;
int *lmax;
int *shps;
int *ilayer;
int *nset;
HC_CPREC *zlabel;
int *ivec;
FILE *in;
hc_boolean short_format;
hc_boolean binary;
hc_boolean verbose;
{
int input1[2],input2[3];
HC_PREC fz;
double dtmp;
/*
read
lmax i+1 z[i] nset shps expansion_type
*/
if(binary){
if(short_format){
if(fread(input1,sizeof(int),1,in) != 1)
return FALSE;
*lmax = input1[0];
}else{
if(fread(input1,sizeof(int),2,in)+
hc_read_float(&fz,1,in) +
fread(input2,sizeof(int),3,in) != 6)
return FALSE;
*lmax = input1[0];
*ilayer=input1[1];
*zlabel = (HC_CPREC) fz;
*nset = input2[0];
*shps = input2[1];
*type = input2[2];
}
}else{
if(short_format){
if(fscanf(in,"%i",lmax)!=1){
return FALSE;
}
}else{
if(fscanf(in,"%i %i %lf %i %i %i",
lmax,ilayer,&dtmp,nset,shps,type)!=6){
return FALSE;
}
*zlabel = (HC_PREC)dtmp;
}
}
if(short_format){
/* default settings for short format */
*zlabel = 0.0;
*ilayer = 0;
*nset=1;
*shps = 1;
*type = HC_DEFAULT_INTERNAL_FORMAT;
}
if(*shps == 1)
*ivec = 0;
else
*ivec = 1;
/*
additional parameters?
*/
switch(*type){
case SH_RICK:
break;
#ifdef HC_USE_HEALPIX
case SH_HEALPIX:
break;
#endif
#ifdef HC_USE_SPHEREPACK
case SH_SPHEREPACK_EVEN:
case SH_SPHEREPACK_GAUSS:
break;
#endif
default:
fprintf(stderr,"sh_read_parameters: type %i undefined\n",
*type);
exit(-1);
break;
}
return TRUE;
}
/*
write the coefficients of a spherical harmonic expansion to out stream
output will be real spherical harmonics coefficients as in Dahlen and
Tromp p. 859
for shps = 1
A00 B00
A10 B10
A11 B11
A20 ...
for shps = 2
A00_p B00_p A00_t B00_t
A10_p B10_p A10_t B10_t
....
for shps = 3
A00_s B00_s A00_p B00_p A00_t B00_t
A10_s B10_s A10_p B10_p A10_t B10_t
....
where s, p, and t are the scalar (radial), poloidal, and toroidal expansions,
respectively
pass shps as unity, if scalar, as 2 if pol/tor part of field, and
as 3 if u_r pol tor components with three expansions for velocity
field
basically, all values of shps can be passed, as long as exp[shps]
fac[3] scales the coefficients
*/
void
sh_print_coefficients_to_stream (exp, shps, out, fac, binary, verbose)
struct sh_lms *exp;
int shps;
FILE *out;
HC_CPREC *fac;
hc_boolean binary;
hc_boolean verbose;
{
int j,l,m;
HC_PREC value[2];
HC_PREC fvalue[2];
/*
test other expansions this set
*/
for(j=0;j < shps;j++){ /* check the lmax */
if(exp[j].lmax != exp[0].lmax){
fprintf(stderr,"sh_print_coefficients: error: lmax(%i):%i != lmax(0):%i\n",
j+1,exp[j].lmax,exp[0].lmax);
exit(-1);
}
if(exp[j].type != exp[0].type ){
fprintf(stderr,"sh_print_coefficients: error: type(%i):%i != type(0):%i\n",
j+1,exp[j].type,exp[0].type);
exit(-1);
}
} /* end test */
if(binary){
for(l=0;l <= exp[0].lmax;l++)
for(m=0;m <= l;m++)
for(j=0;j < shps;j++){
/*
output is in physical convention, convert from whatever
we are using internally
*/
sh_get_coeff((exp+j),l,m,2,TRUE,value);
fvalue[0] = value[0]*fac[j];
fvalue[1] = value[1]*fac[j];
hc_print_float(fvalue, 2, out);
}
}else{
for(l=0;l <= exp[0].lmax;l++){
for(m=0;m <= l;m++){
for(j=0;j < shps;j++){
/* output in physical convention, convert from internal
convention */
sh_get_coeff((exp+j),l,m,2,TRUE,value);
fprintf(out,"%15.7e %15.7e\t",
(double)(value[0]*fac[j]),
(double)(value[1]*fac[j]));
}
fprintf(out,"\n");
} /* end m loop */
} /* end l loop */
//fprintf(out,"\n");
}
}
/*
read in spherical harmonic coefficients in real, physics convention of
Dahlen and Tromp p. 859, and convert to whatever internal format may
be used
shps: number of spherical harmonics sets
scale coefficients with factor fac[shps]
lmax: -1: use lmax from expansion
else: read up to lmax
*/
void
sh_read_coefficients_from_stream (exp, shps, lmax, in, binary, fac, verbose)
struct sh_lms *exp;
int shps;
int lmax;
FILE *in;
hc_boolean binary;
HC_CPREC *fac;
hc_boolean verbose;
{
int j,k,l,m,lmax_loc;
HC_CPREC value[2]={0,0};
HC_PREC fvalue[2]={0,0};
if(lmax < 0)
lmax_loc = exp[0].lmax;
else
lmax_loc = lmax;
/*
test other expansions of this set
*/
for(j=1;j < shps;j++){ /* check the lmax */
if(exp[j].lmax != exp[0].lmax){
fprintf(stderr,"sh_read_coefficients_from_stream: error: lmax(%i):%i != lmax(0):%i\n",
j+1,exp[j].lmax,exp[0].lmax);
exit(-1);
}
if(exp[j].type != exp[0].type ){
fprintf(stderr,"sh_read_coefficients_from_stream: error: type(%i):%i != type(0):%i\n",
j+1,exp[j].type,exp[0].type);
exit(-1);
}
} /* end test */
if(binary){
for(l=0;l <= lmax_loc;l++)
for(m=0;m <= l;m++)
for(j=0;j < shps;j++){
if(hc_read_float(fvalue,2,in)!=2){
fprintf(stderr,"sh_read_coefficients_from_stream: read error: set %i l %i m %i\n",
j+1,l,m);
exit(-1);
}
for(k=0;k<2;k++)
value[k] = (HC_CPREC)fvalue[k];
/* read in real, Dahlen & Tromp normalized coefficients and
convert to whatever format we are using internally */
sh_write_coeff((exp+j),l,m,(m==0)?(0):(2),TRUE,value);
}
}else{
for(l=0;l <= lmax_loc;l++)
for(m=0;m <= l;m++)
for(j=0;j < shps;j++){
if(fscanf(in,HC_TWO_FLT_FORMAT,value,(value+1))!=2){
fprintf(stderr,"sh_read_coefficients_from_stream: read error: set %i l %i m %i, last val: %g %g\n",
j+1,l,m,(double)value[0],(double)value[1]);
exit(-1);
}
/* read in real, Dahlen & Tromp normalized coefficients and
convert to whatever format we are using internally */
sh_write_coeff((exp+j),l,m,(m==0)?(0):(2),TRUE,value);
}
}
/* fill up rest with zeroes, if we are limiting to lmax_loc */
if(lmax_loc < exp[0].lmax){
value[0] = value[1] = 0.0;
for(l=lmax_loc+1;l <= exp[0].lmax;l++)
for(m=0;m <= l;m++)
for(j=0;j < shps;j++)
sh_write_coeff((exp+j),l,m,(m==0)?(0):(2),TRUE,value);
}
for(j=0;j < shps;j++){
//sh_print_nonzero_coeff((exp+j),stderr);
sh_scale_expansion((exp+j),fac[j]);
//sh_print_nonzero_coeff((exp+j),stderr);
exp[j].spectral_init = TRUE;
}
}
/* print a raw set of coefficients to out if nonzero, for
debugging */
void
sh_print_nonzero_coeff (exp, out)
struct sh_lms *exp;
FILE *out;
{
int l,m;
HC_CPREC value[2];
for(l=0;l <= exp->lmax;l++){
for(m=0;m <= l;m++){
sh_get_coeff(exp,l,m,2,FALSE,value);
if(fabs(value[0])+fabs(value[1]) > 1e-8)
fprintf(out,"%5i %5i %15.7e %15.7e\n",l,m,
(double)value[0],(double)value[1]);
}
}
}
/*
given an initializez expansion structure, read the corresponding spatial data in
lon lat data
format from FILE *in
the output is in the data array, which has to be passed
data[shps * exp->npoints]
if use_3d is set, will read in
lon lat z data
instead
*/
void
sh_read_spatial_data_from_stream (exp, in, use_3d, shps, data, z)
struct sh_lms *exp;
FILE *in;
my_boolean use_3d;
int shps;
HC_PREC *data;
HC_PREC *z;
{
sh_read_spatial_data(exp,in,use_3d,shps,data,z);
}
/*
generic function
*/
void
sh_read_spatial_data (exp, in, use_3d, shps, data, z)
struct sh_lms *exp;
FILE *in;
my_boolean use_3d;
int shps;
HC_PREC *data;
HC_PREC *z;
{
HC_PREC lon,lat,xp[3];
int j,k;
/*
read in data for each layer
*/
for(j=0;j < exp->npoints;j++){
/*
get expected coordinates to check if the input is OK
*/
switch(exp->type){
#ifdef HC_USE_HEALPIX
case SH_HEALPIX:
switch(exp->heal.ordering){
case SH_HEALPIX_RING:
pix2ang_ring((long)exp->heal.nside,(long)j,
(xp+HC_THETA),(xp+HC_PHI));
break;
case SH_HEALPIX_NEST:
pix2ang_nest((long)exp->heal.nside,(long)j,
(xp+HC_THETA),(xp+HC_PHI));
break;
default:
fprintf(stderr,"sh_read_spatial_data: error: ordering %i undefined\n",
exp->heal.ordering);
exit(-1);
break;
}
break; /* end Healpix branch */
#endif
case SH_RICK:
/* for Rick's type routine */
#ifdef NO_RICK_FORTRAN
rick_pix2ang(j,exp->lmax,(xp+HC_THETA),(xp+HC_PHI),
&exp->rick);
#else
rick_f90_pix2ang(&j,&exp->lmax,(SH_RICK_PREC)(xp+HC_THETA),
(SH_RICK_PREC)(xp+HC_PHI));
#endif
break;
#ifdef HC_USE_SPHEREPACK
case SH_SPHEREPACK_GAUSS:
case SH_SPHEREPACK_EVEN:
break;
#endif
default:
sh_exp_type_error("sh_read_model_spatial_data",exp);
break;
} /* end type branch */
/*
read coordinates
*/
if(!use_3d){
/*
read in lon lat
*/
if(fscanf(in,HC_TWO_FLT_FORMAT,&lon,&lat) != 2){
fprintf(stderr,"sh_read_spatial_data: error: lon lat format: pixel %i: read error\n",
(int)j);
exit(-1);
}
}else{
/*
read in lon lat z[i]
*/
if(fscanf(in,HC_THREE_FLT_FORMAT,&lon,&lat,z) != 3){
fprintf(stderr,"sh_read_spatial_data: error: lon lat z format: pixel %i: read error\n",
(int)j);
exit(-1);
}
}
/*
read data
*/
for(k=0;k < shps;k++){
if(fscanf(in,HC_FLT_FORMAT,(data+k*exp[0].npoints+j))!=1){
fprintf(stderr,"sh_read_spatial_data: error: scalar format: pixel %i: read error\n",
(int)j);
exit(-1);
}
}
/*
adjust longitude range
*/
if(lon < 0)
lon += 360.0;
/*
check if location is OK (we don't know about z defaults)
*/
if(((fabs(PHI2LON(xp[HC_PHI])-lon) > 1e-3)&&(fabs(fabs(PHI2LON(xp[HC_PHI])-lon)-360) > 1e-3))||
(fabs(THETA2LAT(xp[HC_THETA])-lat) > 1e-3)){
fprintf(stderr,"sh_read_model_spatial_data: error: pixel %i coordinate mismatch:\n",
(int)j);
fprintf(stderr,"sh_read_model_spatial_data: orig: %g, %g file: %g, %g\n",
(double)PHI2LON(xp[HC_PHI]),(double)THETA2LAT(xp[HC_THETA]),
(double)lon,(double)lat);
exit(-1);
}
} /* end points in layer loop */
}
/*
print the spatial basis coordinates of a single spherical
harmonics expansion
for show_z_label is FALSE, the format is
lon lat
for all the npoints of an expansion
for show_z_label is TRUE, the format is
lon lat z
as passed
if out_mode == 0, will write to file *out,
else will use x[] and store the values
*/
void
sh_compute_spatial_basis (exp, out, use_3d, z, x, out_mode, verbose)
struct sh_lms *exp;
FILE *out;
hc_boolean use_3d;
HC_PREC z;
HC_PREC **x;
int out_mode;
hc_boolean verbose;
{
int j,os,inc;
HC_PREC xp[3];
if(out_mode) /* make room for storing x,y,z */
hc_vecrealloc(x,exp->npoints*(2+((use_3d)?(1):(0))),"sh_compute_spatial_basis");
inc = (use_3d)?(3):(2);
for(j=os=0;j < exp->npoints;j++,os+=inc){
/*
get coordinates
*/
switch(exp->type){
#ifdef HC_USE_HEALPIX
case SH_HEALPIX:
switch(exp->heal.ordering){
case SH_HEALPIX_RING:
pix2ang_ring((long)exp->heal.nside,(long)j,
(xp+HC_THETA),(xp+HC_PHI));
break;
case SH_HEALPIX_NEST:
pix2ang_nest((long)exp->heal.nside,(long)j,