-
Notifications
You must be signed in to change notification settings - Fork 7
/
ggrd_grdtrack_util.c
1550 lines (1387 loc) · 42 KB
/
ggrd_grdtrack_util.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
/*
subroutines for 3-D interpolation of scalar data in GMT grd
filesbased on GMT3.4.3 grdtrack
$Id: ggrd_grdtrack_util.c,v 1.6 2006/02/16 02:18:03 twb Exp twb $
original comments for grdtrack from GMT at bottom of file
*/
#include "hc_ggrd.h"
#ifndef ONEEIGHTYOVERPI
#define ONEEIGHTYOVERPI 57.295779513082320876798154814105
#endif
#include <math.h>
#include <string.h>
#include <math.h>
#ifndef irint
#define irint(x) ((int)rint(x))
#endif
void ggrd_init_master(struct ggrd_master *ggrd)
{
ggrd->mat_control = ggrd->mat_control_init = FALSE;
ggrd->ray_control = ggrd->ray_control_init = FALSE;
ggrd->vtop_control = ggrd->vtop_control_init = FALSE;
ggrd->age_control = ggrd->age_control_init = FALSE;
ggrd->nage = 0;
ggrd->age_bandlim = 200.;
ggrd->sf_init = FALSE;
ggrd->time_hist.init = FALSE;
ggrd->temp.init = ggrd->use_temp = FALSE;
ggrd->comp.init = ggrd->use_comp = FALSE;
ggrd->time_hist.vstage_transition = 0.1; /* in Ma, transition */
ggrd->time_hist.interpol_time_lin = FALSE;
ggrd->time_hist.called = FALSE;
/* 3-D velocity settings */
ggrd_init_vstruc(ggrd);
}
/*
wrapper
*/
/* for debugging, mostly */
void ggrd_grdinfo(char *filename)
{
struct ggrd_gt g[1];
char cdummy='c';
ggrd_grdtrack_init_general(FALSE,filename,&cdummy,
"",g,2,FALSE,FALSE);
fprintf(stderr,"ggrd_grdinfo: %s W: %g E: %g S: %g N: %g\n",
filename,g->grd->x_min,g->grd->x_max,g->grd->y_min,g->grd->y_max);
}
/*
init structure and files for the general case, this is a wrapper
for what's below
is_three: TRUE/FALSE for 3-D/2-D
grdfile: filename for 2-D and prefix for 3-D
depth_file: file with depth layers for 3-D
edge_info_string: as in GMT
g: ggrd_gt structure, pass allocated
use_nearneighbor: FALSE, will use bilinear interpolation TRUE: use nearneighbor
returns error code, 0 for regular execution
*/
int ggrd_grdtrack_init_general(ggrd_boolean is_three,
char *grdfile, char *depth_file,
char *gmt_edgeinfo_string,
struct ggrd_gt *g,
ggrd_boolean verbose,
ggrd_boolean change_z_sign,
ggrd_boolean use_nearneighbor)
{
/* this is a switch and can be left in */
//int pad[4]; /* GMT < 4.5.1 */
GMT_LONG pad[4]; /* GMT >= 4.5.1 */
int i,j;
float zavg,tmp;
GMT_LONG interpolant;
if(use_nearneighbor)
interpolant = BCR_NEARNEIGHBOR; /* no interpolation */
else
interpolant = BCR_BILINEAR; /* bilinear is default for
interpolation */
/* clear all entries */
g->east = g->west = g->south = g->north = 0.0;
g->is_three = is_three;
g->init = FALSE;
if(ggrd_grdtrack_init(&g->west,&g->east,
&g->south,&g->north,&g->f,&g->mm,
grdfile,&g->grd,&g->edgeinfo,
gmt_edgeinfo_string,&g->geographic_in,
pad,is_three,depth_file,&g->z,&g->nz,
interpolant,verbose,change_z_sign,
g->loc_bcr))
return 2;
/*
check bandlimited maximum with positivity constraint
*/
g->fmaxlim = (float *)malloc(sizeof(float)*g->nz);
for(i=0;i < g->nz;i++){ /* loop through layers */
//g->fmaxlim[i] = g->grd[i].z_min;
g->fmaxlim[i] = 0.0;
for(j=0;j < g->mm;j++){ /* loop trough entries */
tmp = fabs(g->f[i*g->mm+j]);
//if((g->f[i*g->mm+j] < g->bandlim) &&(g->f[i*g->mm+j] > g->fmaxlim[i]))
if((tmp < g->bandlim) && tmp > g->fmaxlim[i])
g->fmaxlim[i] = tmp;
}
/* min: g->grd[i].z_min
max: g->grd[i].z_max,
bandlim_max: g->fmaxlim[i] */
//fprintf(stderr,"%g %g %g %g\n", g->grd[i].z_min,g->grd[i].z_max,g->bandlim,g->fmaxlim[i]);
}
if(is_three){
/*
check how the depth levels are specified for debugging
*/
zavg = 0.0;
for(i=0;i < g->nz;i++)
zavg += g->z[i];
if(zavg > 0)
g->zlevels_are_negative = FALSE;
else
g->zlevels_are_negative = TRUE;
}else{
g->zlevels_are_negative = FALSE;
}
// if(change_z_sign) /* reverse logic */
//g->zlevels_are_negative = (g->zlevels_are_negative)?(FALSE):(TRUE);
if(verbose){
fprintf(stderr,"ggrd_grdtrack_init_general: initialized from %s, %s, bcflag: %s.\n",
grdfile,(is_three)?("3-D"):("1-D"),
gmt_edgeinfo_string);
if(is_three){
fprintf(stderr,"ggrd_grdtrack_init_general: depth file %s, %i levels, %s.\n",
depth_file,g->nz,
(g->zlevels_are_negative)?("negative z levels"):("positive z levels"));
}
}
g->init = TRUE;
return 0;
}
/*
take log10, 10^x and/or scale complete grid. log10 and 10^x applies first
*/
int ggrd_grdtrack_rescale(struct ggrd_gt *g,
ggrd_boolean take_log10, /* take log10() */
ggrd_boolean take_power10, /* take 10^() */
ggrd_boolean rescale, /* rescale? */
double scale /* factor for rescaling */)
{
int i,j,k;
if(!g->init){
fprintf(stderr,"ggrd_grdtrack_rescale: error: ggrd not initialized\n");
return 1;
}
for(i=0;i < g->nz;i++){ /* loop through depths */
k = i*g->mm;
for(j=0;j < g->mm;j++,k++){
if(take_log10){
g->f[k] = log10(g->f[k]);
}
if(take_power10){
g->f[k] = pow(10.0,g->f[k]);
}
if(rescale)
g->f[k] *= scale;
}
}
return 0;
}
/*
for 3-D spherical
interpolation wrapper, uses r, theta, phi input. return value and TRUE if success,
undefined and FALSE else
*/
ggrd_boolean ggrd_grdtrack_interpolate_rtp(double r,double t,double p,
struct ggrd_gt *g,
double *value,
ggrd_boolean verbose,
ggrd_boolean shift_to_pos_lon,
double radius_planet_in_km)
{
double x[3];
ggrd_boolean result;
if(!g->init){ /* this will not necessarily work */
fprintf(stderr,"ggrd_grdtrack_interpolate_rtp: error, g structure not initialized\n");
return FALSE;
}
if(!g->is_three){
fprintf(stderr,"ggrd_grdtrack_interpolate_rtp: error, g structure is not 3-D\n");
return FALSE;
}
/*
convert coordinates to lon / lat / z
*/
x[0] = p * ONEEIGHTYOVERPI; /* lon */
if(shift_to_pos_lon){
/* make sure we are in 0 ... 360 system ? */
if(x[0]<0)
x[0]+=360.0;
if(x[0]>=360)
x[0]-=360.0;
}
x[1] = 90.0 - t * ONEEIGHTYOVERPI; /* lat */
x[2] = (1.0-r) * radius_planet_in_km; /* depth in [km] */
if(g->zlevels_are_negative) /* adjust for depth */
x[2] = -x[2];
result = ggrd_grdtrack_interpolate(x,TRUE,g->grd,g->f,
g->edgeinfo,g->mm,g->z,
g->nz,value,verbose,
g->loc_bcr);
return result;
}
/*
this is almost redundant, use lon lat in degrees and z in [km] depth
*/
ggrd_boolean ggrd_grdtrack_interpolate_lonlatz(double lon,double lat,double z,
struct ggrd_gt *g,
double *value,
ggrd_boolean verbose)
{
double x[3];
ggrd_boolean result;
if(!g->init){ /* this will not necessarily work */
fprintf(stderr,"ggrd_grdtrack_interpolate_rtp: error, g structure not initialized\n");
return FALSE;
}
if(!g->is_three){
fprintf(stderr,"ggrd_grdtrack_interpolate_rtp: error, g structure is not 3-D\n");
return FALSE;
}
/*
convert coordinates to lon / lat / z
*/
x[0] = lon;
x[1] = lat;
x[2] = z;
if(g->zlevels_are_negative) /* adjust for depth */
x[2] = -x[2];
result = ggrd_grdtrack_interpolate(x,TRUE,g->grd,g->f,
g->edgeinfo,g->mm,g->z,
g->nz,value,verbose,
g->loc_bcr);
return result;
}
/*
for 3-D cartesian
interpolation wrapper, uses x, y, z input. return value and TRUE if success,
undefined and FALSE else
this mean lon lat z
*/
ggrd_boolean ggrd_grdtrack_interpolate_xyz(double x,double y,
double z,
struct ggrd_gt *g,
double *value,
ggrd_boolean verbose)
{
double xloc[3];
ggrd_boolean result;
if(!g->init){ /* this will not necessarily work */
fprintf(stderr,"ggrd_grdtrack_interpolate_xyz: error, g structure not initialized\n");
return FALSE;
}
if(!g->is_three){
fprintf(stderr,"ggrd_grdtrack_interpolate_xyz: error, g structure is not 3-D\n");
return FALSE;
}
/*
convert coordinates
*/
xloc[0] = x; /* lon, x */
xloc[1] = y; /* lat, y */
xloc[2] = z; /* depth, z*/
if(g->zlevels_are_negative) /* adjust for depth */
xloc[2] = -xloc[2];
result = ggrd_grdtrack_interpolate(xloc,TRUE,g->grd,g->f,
g->edgeinfo,g->mm,g->z,
g->nz,value,verbose,
g->loc_bcr);
return result;
}
/*
for 2-D spherical
interpolation wrapper, uses theta, phi input.
return value and TRUE if success,
undefined and FALSE else
*/
ggrd_boolean ggrd_grdtrack_interpolate_tp(double t,double p,
struct ggrd_gt *g,
double *value,
ggrd_boolean verbose,
ggrd_boolean shift_to_pos_lon)
{
double x[3];
ggrd_boolean result;
if(!g->init){ /* this will not necessarily work */
fprintf(stderr,"ggrd_grdtrack_interpolate_tp: error, g structure not initialized\n");
return FALSE;
}
if(g->is_three){
fprintf(stderr,"ggrd_grdtrack_interpolate_tp: error, g structure is not 2-D\n");
return FALSE;
}
/*
convert coordinates
*/
x[0] = p * ONEEIGHTYOVERPI; /* lon */
if(shift_to_pos_lon){
if(x[0] < 0)
x[0] += 360.0;
if(x[0] >= 360.0)
x[0]-=360.0;
}
x[1] = 90.0 - t * ONEEIGHTYOVERPI; /* lat */
x[2] = 1.0;
result = ggrd_grdtrack_interpolate(x,FALSE,g->grd,g->f,
g->edgeinfo,g->mm,g->z,g->nz,
value,verbose,g->loc_bcr);
return result;
}
/*
for 2-D cartesian
interpolation wrapper, uses x,y input
return value and TRUE if success,
undefined and FALSE else
*/
ggrd_boolean ggrd_grdtrack_interpolate_xy(double xin,double yin,
struct ggrd_gt *g,
double *value,
ggrd_boolean verbose)
{
double x[3];
ggrd_boolean result;
if(!g->init){ /* this will not necessarily work */
fprintf(stderr,"ggrd_grdtrack_interpolate_xy: error, g structure not initialized\n");
return FALSE;
}
if(g->is_three){
fprintf(stderr,"ggrd_grdtrack_interpolate_xy: error, g structure is not 2-D\n");
return FALSE;
}
x[0] = xin;
x[1] = yin;
x[2] = 0.0;
result = ggrd_grdtrack_interpolate(x,FALSE,g->grd,g->f,g->edgeinfo,
g->mm,g->z,g->nz,value,verbose,
g->loc_bcr);
//fprintf(stderr,"%g %g %g\n",x[0],x[1],*value);
return result;
}
/*
free structure
*/
void ggrd_grdtrack_free_gstruc(struct ggrd_gt *g)
{
free(g->grd);
free(g->edgeinfo);
free(g->f);
if(g->is_three)
free(g->z);
}
/*
given a location vector in spherical theta, phi system (xp[3]) and a
Cartesian rotation vector wx, wy, wz (omega[3]), find the spherical
velocities vr, vtheta,vphi
*/
void ggrd_find_spherical_vel_from_rigid_cart_rot(double *vr,
double *vtheta,
double *vphi,
double *xp,
double *omega)
{
double vp[3],polar_base[3][3],ct,cp,st,sp,xc[3],tmp,vc[3];
int i;
/* cos and sin theta and phi */
ct=cos(xp[1]);cp=cos(xp[2]);
st=sin(xp[1]);sp=sin(xp[2]);
/* convert location to Cartesian */
tmp = st * xp[0];
xc[0]= tmp * cos(xp[2]); /* x */
xc[1]= tmp * sin(xp[2]); /* y */
xc[2]= ct * xp[0]; /* z */
/* v = omega \cross r */
vc[0] = omega[1]*xc[2] - omega[2]*xc[1];
vc[1] = omega[2]*xc[0] - omega[0]*xc[2];
vc[2] = omega[0]*xc[1] - omega[1]*xc[0];
/* get basis */
polar_base[0][0]= st * cp;polar_base[0][1]= st * sp;polar_base[0][2]= ct;
polar_base[1][0]= ct * cp;polar_base[1][1]= ct * sp;polar_base[1][2]= -st;
polar_base[2][0]= -sp;polar_base[2][1]= cp;polar_base[2][2]= 0.0;
/* convert */
for(i=0;i<3;i++){
vp[i] = polar_base[i][0] * vc[0];
vp[i] += polar_base[i][1] * vc[1];
vp[i] += polar_base[i][2] * vc[2];
}
vr[0] = vp[0];
vtheta[0] = vp[1];
vphi[0]=vp[2];
}
/*
initialize
*/
int ggrd_grdtrack_init(double *west, double *east,double *south, double *north,
/* geographic bounds,
set all to zero to
get the whole range from the
input grid files
*/
float **f, /* data, pass as empty */
int *mm, /* size of data */
char *grdfile, /* name, or prefix, of grd file with scalars */
struct GRD_HEADER **grd, /* pass as empty */
struct GMT_EDGEINFO **edgeinfo, /* pass as empty */
char *edgeinfo_string, /* -fg/ -L type flags from GMT, can be empty */
ggrd_boolean *geographic_in, /* this is normally TRUE */
//int *pad, /* [4] array with padding (output) GMT<4.5.1*/
GMT_LONG *pad,
ggrd_boolean three_d, char *dfile, /* depth file name */
float **z, /* layers, pass as NULL */
int *nz, /* number of layers */
GMT_LONG interpolant, /* linear/cubic? */
ggrd_boolean verbose,
ggrd_boolean change_depth_sign, /* change the
sign of the
depth
levels to go from depth (>0) to z (<0) */
struct GMT_BCR *loc_bcr)
{
FILE *din;
float dz1,dz2;
struct GRD_HEADER ogrd;
int i,one_or_zero,nx,ny,mx,my;
char filename[BUFSIZ*2],*cdummy;
static int gmt_init = FALSE;
/*
deal with edgeinfo
*/
*edgeinfo = (struct GMT_EDGEINFO *)
GMT_memory (VNULL, (size_t)1, sizeof(struct GMT_EDGEINFO), "ggrd_grdtrack_init");
/* init with nonsense to avoid compiler warning */
ogrd.x_min = ogrd.y_min =ogrd.x_max = ogrd.y_max = -100;
ogrd.x_inc = ogrd.y_inc = -1;
ogrd.node_offset = 0;ogrd.nx = ogrd.ny = -1;
if(!gmt_init){
/* this should be OK as is. init only once globally */
GMT_program = "ggrd";
GMT_make_fnan (GMT_f_NaN);
GMT_make_dnan (GMT_d_NaN);
GMT_io_init ();/* Init the table i/o structure */
GMT_grdio_init();
if(strcmp(edgeinfo_string,"-fg")==0){
GMT_io.in_col_type[GMT_X] = GMT_io.out_col_type[GMT_X] = GMT_IS_LON;
GMT_io.in_col_type[GMT_Y] = GMT_io.out_col_type[GMT_Y] = GMT_IS_LAT;
}
if(strcmp(edgeinfo_string,"-fx")==0){
GMT_io.in_col_type[GMT_X] = GMT_io.out_col_type[GMT_X] = GMT_IS_LON;
}
if(strcmp(edgeinfo_string,"-fy")==0){
GMT_io.in_col_type[GMT_Y] = GMT_io.out_col_type[GMT_Y] = GMT_IS_LAT;
}
gmt_init = TRUE;
}
/*
init first edgeinfo (period/global?)
*/
GMT_boundcond_init (*edgeinfo);
/* check if geographic */
if (strlen(edgeinfo_string)>2){ /* the boundary flag was set */
/* parse */
GMT_boundcond_parse (*edgeinfo, (edgeinfo_string+2));
if ((*edgeinfo)->gn)
*geographic_in = 1;
else if((*edgeinfo)->nxp == -1)
*geographic_in = 2;
else
*geographic_in = 0;
}else{
*geographic_in = 0;
}
if(verbose >= 2)
if(*geographic_in)
fprintf(stderr,"ggrd_grdtrack_init: detected geographic region from geostring: %s\n",
edgeinfo_string);
*z = (float *) GMT_memory
(VNULL, (size_t)1, sizeof(float), "ggrd_grdtrack_init");
if(three_d){
/*
three D part first
*/
/*
init the layers
*/
din = fopen(dfile,"r");
if(!din){
fprintf(stderr,"ggrd_grdtrack_init: could not open depth file %s\n",
dfile);
return 1;
}
/* read in the layers */
*nz = 0;
dz1 = -1;
while(fscanf(din,"%f",(*z+ (*nz))) == 1){
if(change_depth_sign)
*(*z+ (*nz)) = -(*(*z+ (*nz)));
/* read in each depth layer */
*z = (float *) GMT_memory ((void *)(*z), (size_t)((*nz)+2), sizeof(float), "ggrd_grdtrack_init");
if(*nz > 0){ /* check for increasing layers */
if(dz1 < 0){
/* init first interval */
dz1 = *(*z+(*nz)) - *(*z+(*nz)-1);
dz2 = dz1;
}else{
/* later intervals */
dz2 = *(*z+(*nz)) - *(*z+(*nz)-1);
}
if(dz2 <= 0.0){ /* check for monotonic increase */
fprintf(stderr,"%s: error: levels in %s have to increase monotonically: n: %i dz; %g\n",
"ggrd_grdtrack_init",dfile,*nz,dz2);
return 2;
}
}
*nz += 1;
}
fclose(din);
/* end layer init"ggrd_grdtrack_initialization */
if(*nz < 2){
fprintf(stderr,"%s: error: need at least two layers in %s\n",
"ggrd_grdtrack_init", dfile);
return 3;
}
if(verbose)
fprintf(stderr,"%s: read %i levels from %s between zmin: %g and zmax: %g\n",
"ggrd_grdtrack_init",*nz,dfile,*(*z+0),*(*z+(*nz)-1));
}else{
*nz = 1;
*(*z) = 0.0;
if(verbose >= 2)
fprintf(stderr,"ggrd_grdtrack_init: single level at z: %g\n",*(*z));
}
/*
get nz grd and edgeinfo structures
*/
*grd = (struct GRD_HEADER *)
GMT_memory (NULL, (size_t)(*nz), sizeof(struct GRD_HEADER), "ggrd_grdtrack_init");
*edgeinfo = (struct GMT_EDGEINFO *)
GMT_memory (*edgeinfo, (size_t)(*nz), sizeof(struct GMT_EDGEINFO), "ggrd_grdtrack_init");
if(verbose >= 2)
fprintf(stderr,"ggrd_grdtrack_init: mem alloc ok\n");
/* init the header */
GMT_grd_init (*grd,0,&cdummy,FALSE);
if(*nz == 1){
if(verbose >= 2)
fprintf(stderr,"ggrd_grdtrack_init: opening single file %s, GMT4 mode\n",grdfile);
if(GMT_read_grd_info (grdfile,*grd)){
fprintf (stderr, "%s: error opening file %s for header\n",
"ggrd_grdtrack_init", grdfile);
return 4;
}
}else{
/* loop through headers for testing purposess */
for(i=0;i<(*nz);i++){
sprintf(filename,"%s.%i.grd",grdfile,i+1);
if (GMT_read_grd_info (filename,(*grd+i))) {
fprintf (stderr, "%s: error opening file %s (-D option was used)\n",
"ggrd_grdtrack_init", filename);
return 6;
}
if(i == 0){
/* save the first grid parameters */
ogrd.x_min = (*grd)[0].x_min;
ogrd.y_min = (*grd)[0].y_min;
ogrd.x_max = (*grd)[0].x_max;
ogrd.y_max = (*grd)[0].y_max;
ogrd.x_inc = (*grd)[0].x_inc;
ogrd.y_inc = (*grd)[0].y_inc;
ogrd.node_offset = (*grd)[0].node_offset;
ogrd.nx = (*grd)[0].nx;
ogrd.ny = (*grd)[0].ny;
/*
make sure we are in 0 ... 360 system
*/
if((ogrd.x_min < 0)||(ogrd.x_max<0)){
fprintf(stderr,"%s: WARNING: geographic grids should be in 0..360 lon system (found %g - %g)\n",
"ggrd_grdtrack_init",ogrd.x_min,ogrd.x_max);
}
}else{
/* test */
if((fabs(ogrd.x_min - (*grd)[i].x_min)>5e-7)||
(fabs(ogrd.y_min - (*grd)[i].y_min)>5e-7)||
(fabs(ogrd.x_max - (*grd)[i].x_max)>5e-7)||
(fabs(ogrd.y_max - (*grd)[i].y_max)>5e-7)||
(fabs(ogrd.x_inc - (*grd)[i].x_inc)>5e-7)||
(fabs(ogrd.y_inc - (*grd)[i].y_inc)>5e-7)||
(fabs(ogrd.nx - (*grd)[i].nx)>5e-7)||
(fabs(ogrd.ny - (*grd)[i].ny)>5e-7)||
(fabs(ogrd.node_offset - (*grd)[i].node_offset)>5e-7)){
fprintf(stderr,"%s: error: grid %i out of %i has different dimensions or setting from first\n",
"ggrd_grdtrack_init",i+1,(*nz));
return 8;
}
}
}
}
if(verbose > 2)
fprintf(stderr,"ggrd_grdtrack_init: read %i headers OK, grids appear to be same size\n",*nz);
if (fabs(*west - (*east)) < 5e-7) { /* No subset asked for , west same as east*/
*west = (*grd)[0].x_min;
*east = (*grd)[0].x_max;
*south = (*grd)[0].y_min;
*north = (*grd)[0].y_max;
}
one_or_zero = ((*grd)[0].node_offset) ? 0 : 1;
nx = irint ( (*east - *west) / (*grd)[0].x_inc) + one_or_zero;
ny = irint ( (*north - *south) / (*grd)[0].y_inc) + one_or_zero;
/* real size of data */
//nn = nx * ny;
/* padded */
mx = nx + 4;
my = ny + 4;
/*
get space for all layers
*/
*mm = mx * my;
*f = (float *) calloc((*mm) * (*nz) ,sizeof (float));
if(!(*f)){
fprintf(stderr,"ggrd_grdtrack_init: f memory error, mm: %i (%i by %i) by nz: %i \n",*mm,mx,my, *nz);
return 9;
}
if(verbose >= 2){
fprintf(stderr,"ggrd_grdtrack_init: mem alloc 2 ok, %g %g %g %g %i %i\n",
*west,*east,*south,*north,nx,ny);
}
/*
pad on sides
*/
pad[0] = pad[1] = pad[2] = pad[3] = 2;
for(i=0;i < (*nz);i++){
/*
loop through layers
*/
if(i != 0) /* copy first edgeinfo over */
memcpy((*edgeinfo+i),(*edgeinfo),sizeof(struct GMT_EDGEINFO));
if((*nz) == 1){
sprintf(filename,"%s",grdfile);
}else{ /* construct full filename */
sprintf(filename,"%s.%i.grd",grdfile,i+1);
}
if (verbose)
fprintf(stderr,"ggrd_grdtrack_init: reading grd file %s (%g - %g (%i) %g - %g (%i); geo: %i flag: %s\n",
filename,*west,*east,nx,*south,*north,ny,
*geographic_in,edgeinfo_string);
/*
read the grd files
*/
if (GMT_read_grd (filename,(*grd+i), (*f+i* (*mm)),
*west, *east, *south, *north,
pad, FALSE)) {
fprintf (stderr, "%s: error reading file %s\n", "ggrd_grdtrack_init", grdfile);
return 10;
}
//fprintf(stderr,"%g %g %i %i %i %i\n",(*grd)->z_scale_factor,(*grd)->z_add_offset,nx,ny,mx,my);
/*
prepare the boundaries
*/
GMT_boundcond_param_prep ((*grd+i), (*edgeinfo+i));
if(i == 0){
/*
Initialize bcr structure, this can be the same for
all grids as long as they have the same dimensions
*/
GMT_bcr_init ((*grd+i), pad, interpolant,1.0,loc_bcr);
}
/* Set boundary conditions */
GMT_boundcond_set ((*grd+i), (*edgeinfo+i), pad,
(*f+i*(*mm)));
} /* end layer loop */
if(verbose){
ggrd_print_layer_avg(*f,*z,mx,my,*nz,stderr,pad);
}
return 0;
}
void ggrd_print_layer_avg(float *x,float *z,int nx, int ny,
int m,FILE *out,
GMT_LONG *pad) /* >= 4.5.1 */
//int *pad)
{
int i,j,k,yl,xl,l,nxny,nxnyr;
float *tmp;
nxny = nx*ny; /* size with padding */
if(pad[0]+pad[1]+pad[2]+pad[3] == 0){
for(i=0;i < m;i++){
fprintf(stderr,"ggrd_grdtrack_init: layer %3i at depth %11g, mean: %11g rms: %11g\n",
i+1,z[i],ggrd_gt_mean((x+i*nxny),nxny),
ggrd_gt_rms((x+i*nxny),nxny));
}
}else{
nxnyr = (nx-pad[0]-pad[1]) * (ny-pad[2]-pad[3]); /* actual data */
tmp = (float *)malloc(sizeof(float)*nxnyr);
if(!tmp)GGRD_MEMERROR("ggrd_print_layer_avg");
xl = nx - pad[1];
yl = ny - pad[2];
for(i=0;i < m;i++){ /* loop through depths */
for(l=0,j=pad[3];j < yl;j++)
for(k=pad[0];k < xl;k++,l++)
tmp[l] = x[i*nxny + j * nx + k];
fprintf(stderr,"ggrd_grdtrack_init: layer %3i at depth %11g, mean: %11g rms: %11g\n",
i+1,z[i],ggrd_gt_mean(tmp,nxnyr),
ggrd_gt_rms(tmp,nxnyr));
}
free(tmp);
}
}
/*
interpolate value
*/
ggrd_boolean ggrd_grdtrack_interpolate(double *in, /* lon/lat/z [2/3] in degrees/km */
ggrd_boolean three_d, /* use 3-D inetrpolation or 2-D? */
struct GRD_HEADER *grd, /* grd information */
float *f, /* data array */
struct GMT_EDGEINFO *edgeinfo, /* edge information */
int mm, /* nx * ny */
float *z, /* depth layers */
int nz, /* number of depth layers */
double *value, /* output value */
ggrd_boolean verbose,
struct GMT_BCR *loc_bcr)
{
int i1,i2;
double fac1,fac2,val1,val2;
static ggrd_boolean zwarned; /* this should move, leave for now */
/* If point is outside grd area,
shift it using periodicity or skip if not periodic. */
*value = NAN; /* use NAN as default so that error
returns don't have some number in
case user forgets to check */
/* check if in bounds */
while ( (in[1] < grd[0].y_min) && (edgeinfo[0].nyp > 0) )
in[1] += (grd[0].y_inc * edgeinfo[0].nyp);
if (in[1] < grd[0].y_min){
return FALSE;
}
while ( (in[1] > grd[0].y_max) && (edgeinfo[0].nyp > 0) )
in[1] -= (grd[0].y_inc * edgeinfo[0].nyp);
if (in[1] > grd[0].y_max) {
return FALSE;
}
while ( (in[0] < grd[0].x_min) && (edgeinfo[0].nxp > 0) )
in[0] += (grd[0].x_inc * edgeinfo[0].nxp);
if (in[0] < grd[0].x_min) {
return FALSE;
}
while ( (in[0] > grd[0].x_max) && (edgeinfo[0].nxp > 0) )
in[0] -= (grd[0].x_inc * edgeinfo[0].nxp);
if (in[0] > grd[0].x_max) {
return FALSE;
}
/*
interpolate
*/
if(three_d){
ggrd_gt_interpolate_z(in[2],z,nz,&i1,&i2,&fac1,&fac2,verbose,&zwarned);
/*
we need these calls to reset the bcr.i and bcr.j counters
otherwise the interpolation routine would assume we have the same
grid
TO DO:
now, we still need the same grid dimensions, else a separate bcr
variable has to be introduced
*/
val1 = GMT_get_bcr_z((grd+i1), in[0], in[1], (f+i1*mm), (edgeinfo+i1),loc_bcr);
val2 = GMT_get_bcr_z((grd+i2), in[0], in[1], (f+i2*mm), (edgeinfo+i2),loc_bcr);
/* fprintf(stderr,"z(%3i/%3i): %11g z: %11g z(%3i/%3i): %11g f1: %11g f2: %11g v1: %11g v2: %11g rms: %11g %11g\n", */
/* i1+1,nz,z[i1],in[2],i2+1,nz,z[i2],fac1,fac2, */
/* val1,val2,rms((f+i1*mm),mm),rms((f+i2*mm),mm)); */
*value = fac1 * val1;
*value += fac2 * val2;
}else{
/* single layer */
*value = GMT_get_bcr_z(grd, in[0], in[1], f, edgeinfo,loc_bcr);
}
if(verbose)
fprintf(stderr,"ggrd_interpolate: lon: %g lat: %g val: %g\n",in[0],in[1],*value);
return TRUE;
}
/*
read in times for time history of velocities, if needed
if read_thistory is TRUE:
the input file for the tectonic stages is in format
t_start^1 t_stop^1
....
t_start^nvtimes t_stop^nvtimes
expecting ascending time intervals, which should have smaller time first and have no gaps
on return, the vtimes vector is in the format
t_left^1 t_mid^1 t_right^1
t_left^2 ...
...
t_left^nvtimes t_mid^nvtimes t_right^nvtimes
else, will only init for one step
returns error code
*/
int ggrd_init_thist_from_file(struct ggrd_t *thist,
char *input_file,
ggrd_boolean read_thistory,
ggrd_boolean verbose)
{
FILE *in;
double ta,tb;
ggrd_boolean opened_file = FALSE;
if(thist->init){
fprintf(stderr,"ggrd_read_time_intervals: error: already initialized\n");
return 1;
}
ggrd_vecalloc(&thist->vtimes,3,"rti: 1");
if(read_thistory){
in = fopen(input_file,"r");
if(!in){
if(verbose)
fprintf(stderr,"ggrd_read_time_intervals: WARNING: could not open file %s\n",
input_file);
opened_file = FALSE;
}else{
opened_file = TRUE;
}
}
if(opened_file){
thist->nvtimes = thist->nvtimes3 = 0;
while(fscanf(in,"%lf %lf",&ta,&tb) == 2){
thist->vtimes[thist->nvtimes3] = ta;
thist->vtimes[thist->nvtimes3+2] = tb;
if(thist->nvtimes > 0){
if((*(thist->vtimes+thist->nvtimes3+2) < *(thist->vtimes+thist->nvtimes3))||
(*(thist->vtimes+thist->nvtimes3) < *(thist->vtimes+(thist->nvtimes-1)*3))||
(*(thist->vtimes+thist->nvtimes3+2) < *(thist->vtimes+(thist->nvtimes-1)*3+2))||
(fabs(*(thist->vtimes+(thist->nvtimes - 1)*3+2) - *(thist->vtimes+thist->nvtimes3))>5e-7)){
GGRD_PE("ggrd_read_time_intervals: error, expecting ascending time intervals");
GGRD_PE("ggrd_read_time_intervals: which should have smaller time first and have no gaps");
}
}
// compute mid point
*(thist->vtimes + thist->nvtimes3+1) =
(*(thist->vtimes+thist->nvtimes3) + *(thist->vtimes + thist->nvtimes3+2))/2.0;
thist->nvtimes += 1;
thist->nvtimes3 += 3;
ggrd_vecrealloc(&thist->vtimes,thist->nvtimes3+3,"rti: 2");
}
thist->tmin = *(thist->vtimes+0);
thist->tmax = *(thist->vtimes+ (thist->nvtimes-1) * 3 +2);
if(!(thist->nvtimes)){
fprintf(stderr,"ggrd_read_time_intervals: error, no times read from %s\n",
input_file);
return 3;
}else{
if(verbose){
fprintf(stderr,"ggrd_read_time_intervals: read %i time intervals from %s\n",
thist->nvtimes,input_file);
fprintf(stderr,"ggrd_read_time_intervals: t_min: %g t_max: %g\n",
thist->tmin,thist->tmax);
}
}
fclose(in);
}else{
/*
only one time step, or no file
*/
thist->nvtimes = 1;
thist->nvtimes3 = thist->nvtimes * 3;
*(thist->vtimes+0) = *(thist->vtimes+1) =
*(thist->vtimes+2) = thist->tmin = thist->tmax= 0.0;
if(verbose)
fprintf(stderr,"ggrd_read_time_intervals: only one timestep / constant fields\n");
}
thist->called = FALSE;
thist->init = TRUE;
return 0;
}
/*
use linear interpolation for z direction
*/
void ggrd_gt_interpolate_z(double z,float *za,int nz,
int *i1, int *i2,
double *fac1, double *fac2,
ggrd_boolean verbose,
ggrd_boolean *zwarned)
{
int nzm1;
if((!(*zwarned))&&verbose){
if((z<za[0])||(z>za[nz-1])){
fprintf(stderr,"interpolate_z: WARNING: at least one z value extrapolated\n");
fprintf(stderr,"interpolate_z: zmin: %g z: %g zmax: %g\n",
za[0],z,za[nz-1]);
*zwarned = TRUE;
}