-
Notifications
You must be signed in to change notification settings - Fork 348
/
gmtgravmag3d.c
1419 lines (1291 loc) · 61.1 KB
/
gmtgravmag3d.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
/*--------------------------------------------------------------------
*
* Copyright (c) 1991-2024 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
* See LICENSE.TXT file for copying and redistribution conditions.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Contact info: www.generic-mapping-tools.org
*--------------------------------------------------------------------*/
/*
* API functions to support the gmtgravmag3d application.
*
* Brief synopsis:
*
*
* Author: Joaquim Luis
* Date: 06-OCT-2000 (original GMT 4)
*
*/
#include "gmt_dev.h"
#include "longopt/gmtgravmag3d_inc.h"
#include "okbfuns.h"
#define THIS_MODULE_CLASSIC_NAME "gmtgravmag3d"
#define THIS_MODULE_MODERN_NAME "gmtgravmag3d"
#define THIS_MODULE_LIB "potential"
#define THIS_MODULE_PURPOSE "Compute the gravity/magnetic anomaly of a 3-D body by the method of Okabe"
#define THIS_MODULE_KEYS "<D{,TD(,FD(,MD(,GG},>D)"
#define THIS_MODULE_NEEDS "R"
#define THIS_MODULE_OPTIONS "-:RVfhior" GMT_ADD_xg_OPT
struct GMTGRAVMAG3D_CTRL {
struct GMTGRAVMAG3D_C { /* -C */
bool active;
double rho;
} C;
struct GMTGRAVMAG3D_D { /* -D */
bool active;
double dir;
} D;
struct GMTGRAVMAG3D_E { /* -T */
bool active;
double dz;
} E;
struct GMTGRAVMAG3D_F { /* -F<grdfile> */
bool active;
char *file;
} F;
struct GMTGRAVMAG3D_G { /* -G<grdfile> */
bool active;
char *file;
} G;
struct GMTGRAVMAG3D_H { /* -H */
bool active;
double t_dec, t_dip, m_int, m_dec, m_dip;
} H;
struct GMTGRAVMAG3D_I { /* -Idx[/dy] */
bool active;
double inc[2];
} I;
struct GMTGRAVMAG3D_L { /* -L */
bool active;
double zobs;
} L;
struct GMTGRAVMAG3D_M { /* -M for model body(ies) */
bool active;
int type[7][10];
double params[7][10][9]; /* 7 bodies with at most 9 parameters */
} M;
struct GMTGRAVMAG3D_S { /* -S */
bool active;
double radius;
} S;
struct GMTGRAVMAG3D_Z { /* -Z */
bool active;
double z0;
} Z;
struct GMTGRAVMAG3D_T { /* -T */
bool active;
bool triangulate;
bool raw;
bool stl;
bool m_var, m_var1, m_var2, m_var3, m_var4;
char *xyz_file;
char *t_file;
char *raw_file;
char *stl_file;
} T;
struct GMTGRAVMAG3D_box { /* No option, just a container */
bool is_geog;
double d_to_m, *mag_int, lon_0, lat_0;
} box;
int n_triang, n_vert, n_raw_triang;
int npts_circ; /* Number of points in which a circle is descretized. */
int n_slices; /* Spheres and Ellipsoids are made by 2*n_slices. Bells are made by n_slices. */
int n_sigmas; /* Number of sigmas which will determine the bell's base width */
struct GMTGRAVMAG3D_XYZ *triang;
struct GMTGRAVMAG3D_XYZ *t_center;
struct GMTGRAVMAG3D_RAW *raw_mesh;
struct MAG_VAR2 *okabe_mag_var2;
struct MAG_VAR3 *okabe_mag_var3;
struct MAG_VAR4 *okabe_mag_var4;
struct GMTGRAVMAG3D_VERT *vert;
};
struct GMTGRAVMAG3D_XYZ {
double x, y, z;
};
struct GMTGRAVMAG3D_VERT {
unsigned int a, b, c;
};
struct GMTGRAVMAG3D_RAW {
double t1[3], t2[3], t3[3];
};
struct MAG_VAR2 {
double m, m_dip;
};
struct MAG_VAR3 {
double m, m_dec, m_dip;
};
struct MAG_VAR4 {
double t_dec, t_dip, m, m_dec, m_dip;
};
enum GMT_enum_body {
BELL = 0,
CYLINDER,
CONE,
ELLIPSOID,
PRISM,
PYRAMID,
SPHERE
};
static void *New_Ctrl (struct GMT_CTRL *GMT) { /* Allocate and initialize a new control structure */
struct GMTGRAVMAG3D_CTRL *C = gmt_M_memory (GMT, NULL, 1, struct GMTGRAVMAG3D_CTRL);
/* Initialize values whose defaults are not 0/false/NULL */
C->L.zobs = 0;
C->D.dir = -1;
C->S.radius = 50000;
C->npts_circ = 24;
C->n_slices = 5;
C->n_sigmas = 2;
return (C);
}
static void Free_Ctrl (struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *C) { /* Deallocate control structure */
if (!C) return;
gmt_M_str_free (C->F.file);
gmt_M_str_free (C->G.file);
gmt_M_str_free (C->T.xyz_file);
gmt_M_str_free (C->T.t_file);
gmt_M_str_free (C->T.raw_file);
gmt_M_str_free (C->T.stl_file);
gmt_M_free (GMT, C);
}
GMT_LOCAL int read_stl (struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl);
GMT_LOCAL void set_center (struct GMTGRAVMAG3D_CTRL *Ctrl);
GMT_LOCAL int facet_triangulate (struct GMTGRAVMAG3D_CTRL *Ctrl, struct BODY_VERTS *body_verts, unsigned int i, bool bat);
GMT_LOCAL int facet_raw (struct GMTGRAVMAG3D_CTRL *Ctrl, struct BODY_VERTS *body_verts, unsigned int i, bool geo);
GMT_LOCAL int check_triang_cw (struct GMTGRAVMAG3D_CTRL *Ctrl, unsigned int n, unsigned int type);
static int usage (struct GMTAPI_CTRL *API, int level) {
const char *name = gmt_show_name_and_purpose (API, THIS_MODULE_LIB, THIS_MODULE_CLASSIC_NAME, THIS_MODULE_PURPOSE);
if (level == GMT_MODULE_PURPOSE) return (GMT_NOERROR);
GMT_Usage (API, 0, "usage: %s [<table>] -Tv<vert_file> | -Tr|s<raw_file> | -M+s<body>/<pars> [-C<density>] [-E<thickness>] "
"[-F<xy_file>] [-G%s] [-H<f_dec>/<f_dip>/<m_int></m_dec>/<m_dip>] [%s] [-L<z_observation>] [%s] "
"[-S<radius>] [-Z<level>] [%s] [-fg] [%s] [%s] [%s] [%s]%s[%s]\n",
name, GMT_OUTGRID, GMT_I_OPT, GMT_Rgeo_OPT, GMT_V_OPT, GMT_h_OPT, GMT_i_OPT, GMT_o_OPT, GMT_r_OPT, GMT_xg_OPT, GMT_PAR_OPT);
if (level == GMT_SYNOPSIS) return (GMT_MODULE_SYNOPSIS);
GMT_Message (API, GMT_TIME_NONE, " REQUIRED ARGUMENTS:\n");
GMT_Usage (API, 1, "\n<table>");
GMT_Usage (API, -2, "One or more data files (in ASCII, binary, netCDF) with data; see -T for format. If no files are given, standard input is read");
GMT_Usage (API, 1, "\n-Tv<vert_file> | -Tr|s<raw_file>");
GMT_Usage (API, -2, "Give names of xyz and vertex (-Tv<fname>) files defining a closed surface. "
"If <xyz_file> has more then 3 columns it means variable magnetization; see docs for more details. "
"The file formats correspond to the output of the triangulate program. "
"Alternatively, use directives to indicate specific formats:");
GMT_Usage (API, 3, "r: Append <file> in raw triangle format (x1 y1 z1 x2 ... z3).");
GMT_Usage (API, 3, "s: Append <file> in STL format.");
GMT_Usage (API, 1, "\nOR");
GMT_Usage (API, 1, "\n-M+s<body>/<pars>");
GMT_Usage (API, -2, "Select among one or more of the following bodies and append <pars>, where x0 and y0 are the horizontal coordinates "
"of the body center [default to 0,0], npts is the number of points that a circle is discretized and n_slices "
"apply when bodies are made by a pile of slices. For example Spheres and Ellipsoids are made of 2*n_slices and "
"Bells have n_slices [Default 5].");
GMT_Usage (API, 3, "%s Gaussian: Append bell,height/sx/sy/z0[/x0/y0/n_sig/npts/n_slices] "
"for a Gaussian of height <height> with characteristic STDs <sx> and <sy>. The base "
"width (at depth <z0>) is controlled by the number of sigmas (<n_sig>) [Default = 2].", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Cylinder: Append cylinder,rad/height/z0[/x0/y0/npts/n_slices] for a "
"cylinder of radius <rad> height <height> and base at depth <z0>.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Cone: Append cone,semi_x/semi_y/height/z0[/x0/y0/npts] for a "
"cone of semi axes <semi_x/semi_y> height <height> and base at depth <z0>.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Ellipsoid: Append ellipsoid,semi_x/semi_y/semi_z/z_center[/x0/y0/npts/n_slices] for an "
"ellipsoid of semi axes <semi_x/semi_y/semi_z> and center depth <z_center>.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Prism: Append prism,side_x/side_y/side_z/z0[/x0/y0] for a "
"prism of sides <x/y/z> and base at depth <z0>.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Pyramid: Append pyramid,side_x/side_y/height/z0[/x0/y0] for a "
"pyramid of sides <x/y> height <height> and base at pth <z0>.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s Sphere: Append sphere,rad/z_center[/x0/y0/npts/n_slices] for a "
"sphere of radius <rad> and center at depth <z_center>.", GMT_LINE_BULLET);
GMT_Usage (API, -2, "Note: It is possible to select more than one body. For example, "
"-M+sprism,1/1/1/-5/-10/1+ssphere,1/-5.");
GMT_Message (API, GMT_TIME_NONE, "\n OPTIONAL ARGUMENTS:\n");
GMT_Usage (API, 1, "\n-C<density>");
GMT_Usage (API, -2, "Set body <density> in SI units.");
GMT_Usage (API, 1, "\n-E<thickness>");
GMT_Usage (API, -2, "Give layer thickness in m [Default = 0 m].");
GMT_Usage (API, 1, "\n-F<xy_file>");
GMT_Usage (API, -2, "Pass locations where anomaly is going to be computed.");
gmt_outgrid_syntax (API, 'G', "Set name of the output grid file");
GMT_Usage (API, 1, "\n-H<f_dec>/<f_dip>/<m_int></m_dec>/<m_dip>");
GMT_Usage (API, -2, "Append parameters for computation of magnetic anomaly:");
GMT_Usage (API, 3, "%s <f_dec>/<f_dip> -> geomagnetic declination/inclination.", GMT_LINE_BULLET);
GMT_Usage (API, 3, "%s <m_int></m_dec>/<m_dip> -> body magnetic intensity/declination/inclination.", GMT_LINE_BULLET);
GMT_Option (API, "I");
GMT_Usage (API, 1, "\n-L<z_observation>");
GMT_Usage (API, -2, "Set level of observation [Default = 0].");
GMT_Option (API, "R");
GMT_Usage (API, 1, "\n-S<radius>");
GMT_Usage (API, -2, "Set search radius in km.");
GMT_Option (API, "V");
GMT_Usage (API, 1, "\n-Z<level>");
GMT_Usage (API, -2, "Set z level of reference plane [Default = 0].");
GMT_Option (API, "bi");
GMT_Usage (API, 1, "\n-fg Converts geographic grids to meters using a \"Flat Earth\" approximation.");
GMT_Option (API, "h,i,o,r");
#ifdef HAVE_GLIB_GTHREAD
GMT_Option (API, "x");
#else
GMT_Usage (API, 1, "\n-x Not available since this binary was not build with GLIB multi-threading support.");
#endif
GMT_Option (API, ":,.");
return (GMT_MODULE_USAGE);
}
static int parse(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl, struct GMT_OPTION *options) {
/* This parses the options provided to gmtgravmag3d and sets parameters in Ctrl.
* Note Ctrl has already been initialized and non-zero default values set.
* Any GMT common options will override values set previously by other commands.
* It also replaces any file names specified as input or output with the data ID
* returned when registering these sources/destinations with the API.
*/
unsigned int pos = 0, n_errors = 0;
int n_par, err_npar = 0, nBELL = 0, nCIL = 0, nPRI = 0, nCONE = 0, nELL = 0, nPIR = 0, nSPHERE = 0;
char p[GMT_LEN256] = {""}, p2[GMT_LEN256] = {""};
struct GMT_OPTION *opt = NULL;
struct GMTAPI_CTRL *API = GMT->parent;
for (opt = options; opt; opt = opt->next) {
switch (opt->option) {
case '<': /* Input files */
if (GMT_Get_FilePath (API, GMT_IS_DATASET, GMT_IN, GMT_FILE_REMOTE, &(opt->arg))) n_errors++;
Ctrl->T.xyz_file = strdup(opt->arg);
break;
/* Processes program-specific parameters */
case 'B': /* For backward compat (Undocumented) */
case 'H':
n_errors += gmt_M_repeated_module_option (API, Ctrl->H.active);
if ((sscanf(opt->arg, "%lf/%lf/%lf/%lf/%lf",
&Ctrl->H.t_dec, &Ctrl->H.t_dip, &Ctrl->H.m_int, &Ctrl->H.m_dec, &Ctrl->H.m_dip)) != 5) {
GMT_Report (API, GMT_MSG_ERROR, "Option -H: Can't decipher values\n");
n_errors++;
}
Ctrl->C.active = false;
break;
case 'C':
n_errors += gmt_M_repeated_module_option (API, Ctrl->C.active);
n_errors += gmt_get_required_double (GMT, opt->arg, opt->option, 0, &Ctrl->C.rho);
Ctrl->C.rho *= 6.674e-6; /* Bake in Newton's constant with density contrast */
Ctrl->H.active = false;
break;
case 'D':
n_errors += gmt_M_repeated_module_option (API, Ctrl->D.active);
n_errors += gmt_get_no_argument (GMT, opt->arg, opt->option, 0);
Ctrl->D.dir = 1;
break;
case 'F':
n_errors += gmt_M_repeated_module_option (API, Ctrl->F.active);
n_errors += gmt_get_required_file (GMT, opt->arg, opt->option, 0, GMT_IS_DATASET, GMT_IN, GMT_FILE_REMOTE, &(Ctrl->F.file));
break;
case 'G':
n_errors += gmt_M_repeated_module_option (API, Ctrl->G.active);
n_errors += gmt_get_required_file (GMT, opt->arg, opt->option, 0, GMT_IS_GRID, GMT_OUT, GMT_FILE_LOCAL, &(Ctrl->G.file));
break;
case 'I':
n_errors += gmt_M_repeated_module_option (API, Ctrl->I.active);
if (gmt_getinc (GMT, opt->arg, Ctrl->I.inc)) {
gmt_inc_syntax (GMT, 'I', 1);
n_errors++;
}
break;
case 'L':
n_errors += gmt_M_repeated_module_option (API, Ctrl->L.active);
n_errors += gmt_get_required_double (GMT, opt->arg, opt->option, 0, &Ctrl->L.zobs);
break;
case 'M':
n_errors += gmt_M_repeated_module_option (API, Ctrl->M.active);
while (gmt_strtok (opt->arg, ",", &pos, p)) { /* -M+cone,a/b/c+ellipe,a/b/c/d */
if (p[0] != '+' && p[1] != 's') {
GMT_Report (API, GMT_MSG_ERROR, "Model option must start with a +s<code> and not %s\n", p);
return GMT_PARSE_ERROR;
}
gmt_strtok(opt->arg, "+", &pos, p2); /* Get the string with the model parameters */
if (pos < strlen(opt->arg)) pos--; /* Need to receed 1 due to the (p[0] != '+') test */
if (!strcmp(&p[2], "bell")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[BELL][nBELL][0], &Ctrl->M.params[BELL][nBELL][1], &Ctrl->M.params[BELL][nBELL][2], &Ctrl->M.params[BELL][nBELL][3], &Ctrl->M.params[BELL][nBELL][4], &Ctrl->M.params[BELL][nBELL][5], &Ctrl->M.params[BELL][nBELL][6], &Ctrl->M.params[BELL][nBELL][7], &Ctrl->M.params[BELL][nBELL][8]);
if (n_par < 4) err_npar = 1;
if (n_par < 7) Ctrl->M.params[BELL][nBELL][6] = Ctrl->n_sigmas;
if (n_par < 8) Ctrl->M.params[BELL][nBELL][7] = Ctrl->npts_circ;
if (n_par < 9) Ctrl->M.params[BELL][nBELL][8] = Ctrl->n_slices;
if (Ctrl->M.params[BELL][nBELL][6] || Ctrl->M.params[SPHERE][nSPHERE][7] <= 0 || Ctrl->M.params[SPHERE][nSPHERE][8] <= 0) {
GMT_Report (API, GMT_MSG_ERROR, "Bad parameters for the 'bell' body. Please, RTFM (read the manual).");
return GMT_PARSE_ERROR;
}
Ctrl->M.type[BELL][nBELL] = BELL;
nBELL++;
}
else if (!strcmp(&p[2], "cylinder")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[CYLINDER][nCIL][0], &Ctrl->M.params[CYLINDER][nCIL][1], &Ctrl->M.params[CYLINDER][nCIL][2], &Ctrl->M.params[CYLINDER][nCIL][3], &Ctrl->M.params[CYLINDER][nCIL][4], &Ctrl->M.params[CYLINDER][nCIL][5]);
if (n_par < 3) err_npar = 1;
if (n_par < 6) Ctrl->M.params[CYLINDER][nCIL][5] = Ctrl->npts_circ;
if (Ctrl->M.params[SPHERE][nSPHERE][5] <= 0) {
GMT_Report (API, GMT_MSG_ERROR, "Bad parameters for the 'cylinder' body. Please, RTFM (read the manual).");
return GMT_PARSE_ERROR;
}
Ctrl->M.type[CYLINDER][nCIL] = CYLINDER;
nCIL++;
}
else if (!strcmp(&p[2], "cone")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[CONE][nCONE][0], &Ctrl->M.params[CONE][nCONE][1], &Ctrl->M.params[CONE][nCONE][2], &Ctrl->M.params[CONE][nCONE][3], &Ctrl->M.params[CONE][nCONE][4]);
if (n_par < 4) err_npar = 1;
if (n_par == 4) Ctrl->M.params[CONE][nCONE][4] = Ctrl->npts_circ;
Ctrl->M.type[CONE][nCONE] = CONE;
nCONE++;
}
else if (!strcmp(&p[2], "ellipsoid")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[ELLIPSOID][nELL][0], &Ctrl->M.params[ELLIPSOID][nELL][1], &Ctrl->M.params[ELLIPSOID][nELL][2], &Ctrl->M.params[ELLIPSOID][nELL][3], &Ctrl->M.params[ELLIPSOID][nELL][4], &Ctrl->M.params[ELLIPSOID][nELL][5], &Ctrl->M.params[ELLIPSOID][nELL][6], &Ctrl->M.params[ELLIPSOID][nELL][7]);
if (n_par < 4) err_npar = 1;
if (n_par < 7) Ctrl->M.params[ELLIPSOID][nELL][6] = Ctrl->npts_circ;
if (n_par < 8) Ctrl->M.params[ELLIPSOID][nELL][7] = Ctrl->n_slices;
if (Ctrl->M.params[SPHERE][nSPHERE][6] <= 0 || Ctrl->M.params[SPHERE][nSPHERE][7] <= 0) {
GMT_Report (API, GMT_MSG_ERROR, "Bad parameters for the 'ellipsoid' body. Please, RTFM (read the manual).");
return GMT_PARSE_ERROR;
}
Ctrl->M.type[ELLIPSOID][nELL] = ELLIPSOID;
nELL++;
}
else if (!strcmp(&p[2], "pyramid")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[PYRAMID][nPIR][0], &Ctrl->M.params[PYRAMID][nPIR][1], &Ctrl->M.params[PYRAMID][nPIR][2], &Ctrl->M.params[PYRAMID][nPIR][3], &Ctrl->M.params[PYRAMID][nPIR][4], &Ctrl->M.params[PYRAMID][nPIR][5]);
if (n_par < 4) err_npar = 1;
Ctrl->M.type[PYRAMID][nPIR] = PYRAMID;
nPIR++;
}
else if (!strcmp(&p[2], "prism")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[PRISM][nPRI][0], &Ctrl->M.params[PRISM][nPRI][1], &Ctrl->M.params[PRISM][nPRI][2], &Ctrl->M.params[PRISM][nPRI][3], &Ctrl->M.params[PRISM][nPRI][4], &Ctrl->M.params[PRISM][nPRI][5]);
if (n_par < 4) err_npar = 1;
Ctrl->M.type[PRISM][nPRI] = PRISM;
nPRI++;
}
else if (!strcmp(&p[2], "sphere")) {
n_par = sscanf (p2, "%lg/%lg/%lg/%lg/%lg/%lg", &Ctrl->M.params[SPHERE][nSPHERE][0], &Ctrl->M.params[SPHERE][nSPHERE][1], &Ctrl->M.params[SPHERE][nSPHERE][2], &Ctrl->M.params[SPHERE][nSPHERE][3], &Ctrl->M.params[SPHERE][nSPHERE][4], &Ctrl->M.params[SPHERE][nSPHERE][5]);
if (n_par < 2) err_npar = 1;
if (n_par < 5) Ctrl->M.params[SPHERE][nSPHERE][4] = Ctrl->npts_circ;
if (n_par < 6) Ctrl->M.params[SPHERE][nSPHERE][5] = Ctrl->n_slices;
if (Ctrl->M.params[SPHERE][nSPHERE][4] <= 0 || Ctrl->M.params[SPHERE][nSPHERE][5] <= 0) {
GMT_Report (API, GMT_MSG_ERROR, "Bad parameters for the 'sphere' body. Please, RTFM (read the manual).");
return GMT_PARSE_ERROR;
}
Ctrl->M.type[SPHERE][nSPHERE] = SPHERE;
nSPHERE++;
}
else {
GMT_Report (API, GMT_MSG_ERROR, "Unknown model code (%s) in -M option\n", &p[1]);
return GMT_PARSE_ERROR;
}
if (err_npar) {
GMT_Report (API, GMT_MSG_ERROR, "Model prism option, wrong number of parameters.\n");
return GMT_PARSE_ERROR;
}
}
break;
case 'E':
n_errors += gmt_M_repeated_module_option (API, Ctrl->E.active);
n_errors += gmt_get_required_double (GMT, opt->arg, opt->option, 0, &Ctrl->E.dz);
break;
case 'S':
n_errors += gmt_M_repeated_module_option (API, Ctrl->S.active);
n_errors += gmt_get_required_double (GMT, opt->arg, opt->option, 0, &Ctrl->S.radius);
Ctrl->S.radius *= 1000; /* Expect km, convert to meters */
break;
case 'T': /* Selected input mesh format */
n_errors += gmt_M_repeated_module_option (API, Ctrl->T.active);
if (opt->arg[0] == 'p') {
char *pch;
Ctrl->T.xyz_file = strdup(&opt->arg[1]);
Ctrl->T.triangulate = true;
if ((pch = strstr(opt->arg, "+m")) != NULL) { /* Variable magnetization */
Ctrl->T.m_var = true;
Ctrl->H.active = true;
Ctrl->C.active = false;
pch += 2; /* Jump the +m chars. These are not documented and I (JL) don't remember what they do */
if (pch && pch[0] == '2') Ctrl->T.m_var2 = true;
else if (pch && pch[0] == '3') Ctrl->T.m_var3 = true;
else if (pch && pch[0] == '4') Ctrl->T.m_var4 = true;
else Ctrl->T.m_var1 = true;
Ctrl->T.xyz_file[strlen(Ctrl->T.xyz_file)-2] = '\0'; /* In any case the "+m" must go out of fname */
}
}
else if (opt->arg[0] == 'v') {
Ctrl->T.triangulate = true;
Ctrl->T.t_file = strdup(&opt->arg[1]);
}
else if (opt->arg[0] == 'r') {
Ctrl->T.raw_file = strdup(&opt->arg[1]);
Ctrl->T.raw = true;
}
else if (opt->arg[0] == 's') {
Ctrl->T.stl_file = strdup(&opt->arg[1]);
Ctrl->T.stl = true;
}
break;
case 'Z':
n_errors += gmt_M_repeated_module_option (API, Ctrl->Z.active);
n_errors += gmt_get_required_double (GMT, opt->arg, opt->option, 0, &Ctrl->Z.z0);
break;
default: /* Report bad options */
n_errors += gmt_default_option_error (GMT, opt);
break;
}
}
n_errors += gmt_M_check_condition(GMT, Ctrl->S.active && (Ctrl->S.radius <= 0.0 || gmt_M_is_dnan (Ctrl->S.radius)),
"Option -S: Radius is NaN or negative\n");
n_errors += gmt_M_check_condition(GMT, !Ctrl->T.active && !Ctrl->M.active, "Options -T or -M are mandatory\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->T.xyz_file != NULL && Ctrl->T.t_file == NULL,
"with xyz must provide also vertex (-Tv) file.\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->T.t_file != NULL && Ctrl->T.xyz_file == NULL,
"Option -T: vertex file provided (-Tv) but not xyz file.\n");
n_errors += gmt_M_check_condition(GMT, !Ctrl->G.active && !Ctrl->F.active, "Must specify either -G or -F options\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->G.active && !Ctrl->I.active, "Must specify -I option\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->G.active && !GMT->common.R.active[RSET], "Must specify -R option\n");
n_errors += gmt_M_check_condition(GMT, Ctrl->C.rho == 0.0 && !Ctrl->H.active && !Ctrl->T.m_var4 ,
"Must specify either -C<density> or -H<stuff>\n");
(void)gmt_M_check_condition(GMT, Ctrl->G.active && Ctrl->F.active, "Warning: -F overrides -G\n");
if (gmt_M_check_condition(GMT, Ctrl->T.raw && Ctrl->S.active, "Warning: -Tr overrides -S\n"))
Ctrl->S.active = false;
/*n_errors += gmt_M_check_condition (GMT, !Ctrl->In.file, "Must specify input file\n");*/
return (n_errors ? GMT_PARSE_ERROR : GMT_NOERROR);
}
/* -------------------------------------------------------------------------*/
GMT_LOCAL int read_xyz(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl, struct GMT_OPTION *options, double *lon_0, double *lat_0) {
/* read xyz[m] file with point data coordinates */
int n_cols = 0, error;
unsigned int k, n = 0;
size_t n_alloc = 10 * GMT_CHUNK;
char line[GMT_LEN256] = {""};
double x1, x2, x3, x4, x5, x6, x7, x8;
struct GMT_RECORD *In = NULL;
FILE *fp = NULL;
/* First, count number of columns */
if ((fp = gmt_fopen (GMT, Ctrl->T.xyz_file, "r")) == NULL) return -1;
while (fgets (line, GMT_LEN256, fp)) {
if (line[0] == '#') continue;
n_cols = sscanf (line, "%lg %lg %lg %lg %lg %lg %lg %lg", &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8);
break;
}
fclose(fp);
if (n_cols < 3 || n_cols == 7 || n_cols > 8) {
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Wrong number of columns (%d) in file %s\n", n_cols, Ctrl->T.xyz_file);
return -1;
}
if ((error = GMT_Set_Columns (GMT->parent, GMT_IN, (unsigned int)n_cols, GMT_COL_FIX_NO_TEXT)) != GMT_NOERROR)
return error;
if (GMT_Init_IO (GMT->parent, GMT_IS_DATASET, GMT_IS_POINT, GMT_IN, GMT_ADD_DEFAULT, 0, options) != GMT_NOERROR) /* Establishes data input */
return GMT->parent->error;
if (GMT_Begin_IO (GMT->parent, GMT_IS_DATASET, GMT_IN, GMT_HEADER_ON) != GMT_NOERROR) /* Enables data input and sets access mode */
return GMT->parent->error;
Ctrl->triang = gmt_M_memory (GMT, NULL, n_alloc, struct GMTGRAVMAG3D_XYZ);
Ctrl->T.m_var = (n_cols == 3) ? false : true; /* x,y,z */
if (n_cols == 4) {
Ctrl->T.m_var1 = true;
Ctrl->box.mag_int = gmt_M_memory (GMT, NULL, n_alloc, double);
}
else if (n_cols == 5) {
Ctrl->T.m_var2 = true;
Ctrl->okabe_mag_var2 = gmt_M_memory (GMT, NULL, n_alloc, struct MAG_VAR2);
}
else if (n_cols == 6) {
Ctrl->T.m_var3 = true;
Ctrl->okabe_mag_var3 = gmt_M_memory (GMT, NULL, n_alloc, struct MAG_VAR3);
}
else if (n_cols == 8) {
Ctrl->T.m_var4 = true;
Ctrl->okabe_mag_var4 = gmt_M_memory (GMT, NULL, n_alloc, struct MAG_VAR4);
}
if (n_cols > 3) { /* A bit ugly doing this here but only now we know enough */
Ctrl->H.active = true;
Ctrl->C.active = false;
}
do { /* Keep returning records until we reach EOF */
if ((In = GMT_Get_Record (GMT->parent, GMT_READ_DATA, NULL)) == NULL) { /* Read next record, get NULL if special case */
if (gmt_M_rec_is_error (GMT)) /* Bail if there are any read errors */
return (GMT_RUNTIME_ERROR);
else if (gmt_M_rec_is_eof (GMT)) /* Reached end of file */
break;
continue; /* Go back and read the next record */
}
if (In->data == NULL) {
gmt_quit_bad_record (GMT->parent, In);
return (GMT->parent->error);
}
if (n == n_alloc) {
n_alloc = (size_t)(n_alloc * 1.7);
Ctrl->triang = gmt_M_memory (GMT, Ctrl->triang, n_alloc, struct GMTGRAVMAG3D_XYZ);
if (Ctrl->T.m_var1)
Ctrl->box.mag_int = gmt_M_memory (GMT, Ctrl->box.mag_int, n_alloc, double);
else if (Ctrl->T.m_var2)
Ctrl->okabe_mag_var2 = gmt_M_memory (GMT, Ctrl->okabe_mag_var2, n_alloc, struct MAG_VAR2);
else if (Ctrl->T.m_var3)
Ctrl->okabe_mag_var3 = gmt_M_memory (GMT, Ctrl->okabe_mag_var3, n_alloc, struct MAG_VAR3);
else
Ctrl->okabe_mag_var4 = gmt_M_memory (GMT, Ctrl->okabe_mag_var4, n_alloc, struct MAG_VAR4);
}
Ctrl->triang[n].x = In->data[0];
Ctrl->triang[n].y = -In->data[1]; /* - because y must be positive 'south'*/
Ctrl->triang[n].z = In->data[2] * Ctrl->D.dir;
if (Ctrl->T.m_var1)
Ctrl->box.mag_int[n] = In->data[3];
else if (Ctrl->T.m_var2) {
Ctrl->okabe_mag_var2[n].m = In->data[3];
Ctrl->okabe_mag_var2[n].m_dip = In->data[4];
}
else if (Ctrl->T.m_var3) {
Ctrl->okabe_mag_var3[n].m = In->data[3];
Ctrl->okabe_mag_var3[n].m_dec = In->data[4];
Ctrl->okabe_mag_var3[n].m_dip = In->data[5];
}
else if (Ctrl->T.m_var4) {
Ctrl->okabe_mag_var4[n].t_dec = In->data[3];
Ctrl->okabe_mag_var4[n].t_dip = In->data[4];
Ctrl->okabe_mag_var4[n].m = In->data[5];
Ctrl->okabe_mag_var4[n].m_dec = In->data[6];
Ctrl->okabe_mag_var4[n].m_dip = In->data[7];
}
n++;
} while (true);
if (GMT_End_IO (GMT->parent, GMT_IN, 0) != GMT_NOERROR) /* Disables further data input */
return GMT->parent->error;
Ctrl->triang = gmt_M_memory (GMT, Ctrl->triang, (size_t)n, struct GMTGRAVMAG3D_XYZ);
if (Ctrl->T.m_var1) Ctrl->box.mag_int = gmt_M_memory (GMT, Ctrl->box.mag_int, (size_t)n, double);
else if (Ctrl->T.m_var2) Ctrl->okabe_mag_var2 = gmt_M_memory (GMT, Ctrl->okabe_mag_var2, (size_t)n, struct MAG_VAR2);
else if (Ctrl->T.m_var3) Ctrl->okabe_mag_var3 = gmt_M_memory (GMT, Ctrl->okabe_mag_var3, (size_t)n, struct MAG_VAR3);
else Ctrl->okabe_mag_var4 = gmt_M_memory (GMT, Ctrl->okabe_mag_var4, (size_t)n, struct MAG_VAR4);
*lon_0 = 0.; *lat_0 = 0.;
if (Ctrl->box.is_geog) {
double x_min = FLT_MAX, x_max = -FLT_MAX, y_min = FLT_MAX, y_max = -FLT_MAX;
/* compute the central lat lon For y_min/max we reverse MIN/MAX because we already have multiplied y by -1 above. */
for (k = 0; k < n; k++) {
x_min = MIN(Ctrl->triang[k].x, x_min); x_max = MAX(Ctrl->triang[k].x, x_max);
y_min = MAX(Ctrl->triang[n].y, y_min); y_max = MIN(Ctrl->triang[n].y, y_max);
}
*lon_0 = (x_min + x_max) / 2;
*lat_0 = (y_min + y_max) / 2;
for (k = 0; k < n; k++) {
Ctrl->triang[k].x = (Ctrl->triang[k].x - *lon_0) * Ctrl->box.d_to_m * cos(Ctrl->triang[k].y*D2R);
Ctrl->triang[k].y = (Ctrl->triang[k].y + *lat_0) * Ctrl->box.d_to_m; /* + because we multiplied by -1 above */
}
}
Ctrl->n_triang = n;
return 0;
}
/* -------------------------------------------------------------------------*/
GMT_LOCAL int read_vertices(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl) {
/* Read precalculated triangulation indices */
int n_skipped, error;
uint64_t seg, row, np, k;
unsigned int save_col_type[3];
double d_n = (double)Ctrl->n_triang - 0.5; /* So we can use > in test near line 806 */
struct GMT_DATASET *Tin = NULL;
struct GMT_DATATABLE *T = NULL;
/* Must switch to Cartesian input and save whatever original input type we have since we are reading integer triplets */
for (k = 0; k < 3; k++) {
save_col_type[k] = gmt_M_type (GMT, GMT_IN, k); /* Remember what we have */
gmt_set_column_type (GMT, GMT_IN, (unsigned int)k, GMT_IS_FLOAT); /* And temporarily set to FLOAT */
}
if ((error = GMT_Set_Columns (GMT->parent, GMT_IN, 3, GMT_COL_FIX_NO_TEXT)) != GMT_NOERROR)
return GMT_RUNTIME_ERROR;
if ((Tin = GMT_Read_Data (GMT->parent, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_NONE, GMT_READ_NORMAL, NULL, Ctrl->T.t_file, NULL)) == NULL) {
return GMT->parent->error;
}
for (k = 0; k < 3; k++) gmt_set_column_type (GMT, GMT_IN, (unsigned int)k, save_col_type[k]); /* Undo the damage above */
if (Tin->n_columns < 3) { /* Trouble */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Option -Tv: %s does not have at least 3 columns with indices\n", Ctrl->T.t_file);
if (GMT_Destroy_Data (GMT->parent, &Tin) != GMT_NOERROR) return GMT->parent->error;
return GMT_RUNTIME_ERROR;
}
T = Tin->table[0]; /* Since we only have one table here */
Ctrl->vert = gmt_M_memory (GMT, NULL, T->n_records, struct GMTGRAVMAG3D_VERT); /* Allocate the integer index array */
for (seg = np = n_skipped = 0; seg < T->n_segments; seg++) {
for (row = 0; row < T->segment[seg]->n_rows; row++) {
if (T->segment[seg]->data[0][row] > d_n || T->segment[seg]->data[1][row] > d_n || T->segment[seg]->data[2][row] > d_n)
n_skipped++; /* Outside point range */
else {
Ctrl->vert[np].a = urint (T->segment[seg]->data[0][row]);
Ctrl->vert[np].b = urint (T->segment[seg]->data[1][row]);
Ctrl->vert[np].c = urint (T->segment[seg]->data[2][row]);
np++;
}
}
}
if (GMT_Destroy_Data (GMT->parent, &Tin) != GMT_NOERROR)
return GMT->parent->error;
GMT_Report (GMT->parent, GMT_MSG_INFORMATION, "Read %d indices triplets from %s.\n", np, Ctrl->T.t_file);
if (n_skipped)
GMT_Report (GMT->parent, GMT_MSG_WARNING, "Found %d indices triplets exceeding range of known vertices - skipped.\n", n_skipped);
Ctrl->n_vert = np;
return 0;
}
/* -----------------------------------------------------------------*/
GMT_LOCAL int read_raw(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl) {
/* read a file with triangles in the raw format and returns nb of triangles */
unsigned int row, seg, nt;
struct GMT_DATASET *In = NULL;
if ((In = GMT_Read_Data (GMT->parent, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_POINT, GMT_IO_ASCII, NULL, Ctrl->T.raw_file, NULL)) == NULL)
return GMT->parent->error;
if (In->n_columns < 9) { /* Trouble */
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Option -Ts: %s does not have 9 columns with 3 triangle vertices\n", Ctrl->T.raw_file);
return -1;
}
Ctrl->raw_mesh = gmt_M_memory (GMT, NULL, In->n_records, struct GMTGRAVMAG3D_RAW);
for (seg = nt = 0; seg < In->n_segments; seg++) {
for (row = 0; row < In->table[0]->n_records; row++) {
Ctrl->raw_mesh[nt].t1[0] = In->table[0]->segment[seg]->data[0][row];
Ctrl->raw_mesh[nt].t1[1] = -In->table[0]->segment[seg]->data[1][row];
Ctrl->raw_mesh[nt].t1[2] = In->table[0]->segment[seg]->data[2][row] * Ctrl->D.dir;
Ctrl->raw_mesh[nt].t2[0] = In->table[0]->segment[seg]->data[3][row];
Ctrl->raw_mesh[nt].t2[1] = -In->table[0]->segment[seg]->data[4][row];
Ctrl->raw_mesh[nt].t2[2] = In->table[0]->segment[seg]->data[5][row] * Ctrl->D.dir;
Ctrl->raw_mesh[nt].t3[0] = In->table[0]->segment[seg]->data[6][row];
Ctrl->raw_mesh[nt].t3[1] = -In->table[0]->segment[seg]->data[7][row];
Ctrl->raw_mesh[nt].t3[2] = In->table[0]->segment[seg]->data[8][row] * Ctrl->D.dir;
nt++;
}
}
Ctrl->n_raw_triang = In->n_records;
if (GMT_Destroy_Data (GMT->parent, &In) != GMT_NOERROR) {
return GMT->parent->error;
}
return 0;
}
#include "solids.c"
/* -----------------------------------------------------------------*/
GMT_LOCAL void solids(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl) {
/* */
for (int m = 0; m < 7; m++) {
for (int n = 0; n < 10; n++) {
if (Ctrl->M.type[m][n]) {
switch (Ctrl->M.type[m][n]) {
case BELL:
five_psoid(GMT, Ctrl, BELL, n, false, false, true, false);
break;
case CYLINDER:
cilindro(GMT, Ctrl, n);
break;
case CONE:
five_psoid(GMT, Ctrl, CONE, n, true, false, false, false);
break;
case ELLIPSOID:
five_psoid(GMT, Ctrl, ELLIPSOID, n, false, false, false, false);
break;
case PYRAMID:
five_psoid(GMT, Ctrl, PYRAMID, n, false, true, false, false);
break;
case PRISM:
prism(GMT, Ctrl, n);
break;
case SPHERE:
five_psoid(GMT, Ctrl, SPHERE, n, false, false, false, false);
break;
default:
break;
}
}
}
}
}
#define bailout(code) {gmt_M_free_options (mode); return (code);}
#define Return(code) {Free_Ctrl (GMT, Ctrl); gmt_end_module (GMT, GMT_cpy); bailout (code);}
EXTERN_MSC int GMT_gmtgravmag3d (void *V_API, int mode, void *args) {
bool bat = true, DO = true;
unsigned int row, col, i, j, k, kk;
unsigned int ndata_p = 0, nx_p, ny_p, n_vert_max;
unsigned int z_th = 0, n_triang = 0, ndata_s = 0, n_swap = 0;
int error = 0;
uint64_t ij;
size_t nm;
int km, pm; /* index of current body facet (for mag only) */
gmt_grdfloat *g = NULL, one_100;
double s_rad2, x_o, y_o, t_mag, a, DX, DY;
double *x_obs = NULL, *y_obs = NULL, *z_obs = NULL, *x = NULL, *y = NULL, *cos_vec = NULL;
double cc_t, cs_t, s_t, lon_0 = 0, lat_0 = 0;
struct LOC_OR *loc_or = NULL;
struct BODY_VERTS *body_verts = NULL;
struct BODY_DESC body_desc;
struct GMTGRAVMAG3D_CTRL *Ctrl = NULL;
struct GMT_GRID *Gout = NULL;
struct GMT_DATASET *Cin = NULL;
struct GMT_DATATABLE *point = NULL;
struct GMT_CTRL *GMT = NULL, *GMT_cpy = NULL;
struct GMT_OPTION *options = NULL;
struct GMTAPI_CTRL *API = gmt_get_api_ptr (V_API); /* Cast from void to GMTAPI_CTRL pointer */
struct MAG_PARAM *okabe_mag_param = NULL;
struct MAG_VAR *okabe_mag_var = NULL;
struct GMTGRAVMAG3D_VERT *vert = NULL;
struct MAG_VAR2 *okabe_mag_var2 = NULL;
struct MAG_VAR3 *okabe_mag_var3 = NULL;
struct MAG_VAR4 *okabe_mag_var4 = NULL;
body_desc.n_v = NULL, body_desc.ind = NULL;
/*----------------------- Standard module initialization and parsing ----------------------*/
if (API == NULL) return (GMT_NOT_A_SESSION);
if (mode == GMT_MODULE_PURPOSE) return (usage (API, GMT_MODULE_PURPOSE)); /* Return the purpose of program */
options = GMT_Create_Options (API, mode, args);
if (API->error) return (API->error); /* Set or get option list */
if ((error = gmt_report_usage (API, options, 0, usage)) != GMT_NOERROR) bailout (error); /* Give usage if requested */
/* Parse the command-line arguments */
if ((GMT = gmt_init_module (API, THIS_MODULE_LIB, THIS_MODULE_CLASSIC_NAME, THIS_MODULE_KEYS, THIS_MODULE_NEEDS, module_kw, &options, &GMT_cpy)) == NULL) bailout (API->error); /* Save current state */
if (GMT_Parse_Common (API, THIS_MODULE_OPTIONS, options)) Return (API->error);
Ctrl = New_Ctrl (GMT); /* Allocate and initialize a new control structure */
if ((error = parse (GMT, Ctrl, options)) != 0) Return (error);
/*---------------------------- This is the gmtgravmag3d main code ----------------------------*/
okabe_mag_var2 = Ctrl->okabe_mag_var2; /* Aliases */
okabe_mag_var3 = Ctrl->okabe_mag_var3;
okabe_mag_var4 = Ctrl->okabe_mag_var4;
if (gmt_M_is_geographic (GMT, GMT_IN)) Ctrl->box.is_geog = true;
if (!Ctrl->box.is_geog)
Ctrl->box.d_to_m = 1;
else
Ctrl->box.d_to_m = 2 * M_PI * 6371008.7714 / 360.0;
/*Ctrl->box.d_to_m = 2.0 * M_PI * gmtdefs.ellipse[N_ELLIPSOIDS-1].eq_radius / 360.0;*/
Ctrl->Z.z0 *= Ctrl->D.dir;
/* ---- Read files section ---------------------------------------------------- */
if (Ctrl->F.active) { /* Read xy file where anomaly is to be computed */
if ((Cin = GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_POINT, GMT_IO_ASCII, NULL, Ctrl->F.file, NULL)) == NULL)
Return (API->error);
if (Cin->n_columns < 2) { /* Trouble */
GMT_Report (API, GMT_MSG_ERROR, "Option -F: %s does not have at least 2 columns with coordinates\n", Ctrl->F.file);
Return (GMT_PARSE_ERROR);
}
point = Cin->table[0]; /* Can only be one table since we read a single file */
ndata_p = (unsigned int)point->n_records;
if (point->n_segments > 1) /* case not dealt (or ignored) and should be tested here */
GMT_Report(API, GMT_MSG_WARNING, "Multi-segment files are not used in gmtgravmag3d. Using first segment only\n");
}
if (Ctrl->T.triangulate) { /* Read triangle file output from triangulate */
if ((error = read_xyz(GMT, Ctrl, options, &lon_0, &lat_0)))
Return (error);
/* read vertex file */
if ((error = read_vertices(GMT, Ctrl)))
Return (error);
vert = Ctrl->vert;
Ctrl->t_center = gmt_M_memory (GMT, NULL, Ctrl->n_vert, struct GMTGRAVMAG3D_XYZ);
/* compute approximate center of each triangle */
n_swap = check_triang_cw (Ctrl, Ctrl->n_vert, 0);
set_center (Ctrl);
}
else if (Ctrl->T.stl) { /* Read STL file defining a closed volume */
if ((ndata_s = read_stl(GMT, Ctrl)) < 0) {
GMT_Report (API, GMT_MSG_ERROR, "Cannot open file %s\n", Ctrl->T.stl_file);
Return (GMT_ERROR_ON_FOPEN);
}
/*n_swap = check_triang_cw (ndata_s, 1);*/
}
else if (Ctrl->T.raw) { /* Read RAW file defining a closed volume */
if ((error = read_raw(GMT, Ctrl)))
Return (error);
/*n_swap = check_triang_cw (Ctrl, Ctrl->n_raw_triang, 1);*/
}
else if (Ctrl->M.active) {
solids(GMT, Ctrl);
}
#if 0
for (i = 0; i < 24; i++) {
fprintf(stderr, "%.2f %.2f %.2f ", Ctrl->raw_mesh[i].t1[0], Ctrl->raw_mesh[i].t1[1], Ctrl->raw_mesh[i].t1[2]);
fprintf(stderr, "%.2f %.2f %.2f ", Ctrl->raw_mesh[i].t2[0], Ctrl->raw_mesh[i].t2[1], Ctrl->raw_mesh[i].t2[2]);
fprintf(stderr, "%.2f %.2f %.2f\n", Ctrl->raw_mesh[i].t3[0], Ctrl->raw_mesh[i].t3[1], Ctrl->raw_mesh[i].t3[2]);
}
#endif
if (n_swap > 0)
GMT_Report (API, GMT_MSG_INFORMATION, "%d triangles had ccw order\n", n_swap);
/* --------------------------------------------------------------------------------------- */
if (Ctrl->G.active) {
if ((Gout = GMT_Create_Data (API, GMT_IS_GRID, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA, NULL, NULL, Ctrl->I.inc, \
GMT_GRID_DEFAULT_REG, GMT_NOTSET, NULL)) == NULL) Return (API->error);
GMT_Report (API, GMT_MSG_INFORMATION, "Grid dimensions are n_columns = %d, n_rows = %d\n", Gout->header->n_columns, Gout->header->n_rows);
/* Build observation point vectors */
x = gmt_M_memory (GMT, NULL, Gout->header->n_columns, double);
y = gmt_M_memory (GMT, NULL, Gout->header->n_rows, double);
for (i = 0; i < Gout->header->n_columns; i++)
x[i] = (i == (Gout->header->n_columns-1)) ? Gout->header->wesn[XHI] : (Gout->header->wesn[XLO] + i * Gout->header->inc[GMT_X]);
for (j = 0; j < Gout->header->n_rows; j++)
y[j] = (j == (Gout->header->n_rows-1)) ? -Gout->header->wesn[YLO] : -(Gout->header->wesn[YHI] - j * Gout->header->inc[GMT_Y]);
}
nx_p = (!Ctrl->F.active) ? Gout->header->n_columns : ndata_p;
ny_p = (!Ctrl->F.active) ? Gout->header->n_rows : ndata_p;
nm = (!Ctrl->F.active) ? Gout->header->nm : ndata_p;
x_obs = gmt_M_memory (GMT, NULL, nx_p, double);
y_obs = gmt_M_memory (GMT, NULL, ny_p, double);
z_obs = gmt_M_memory (GMT, NULL, nx_p, double);
body_verts = gmt_M_memory (GMT, NULL, 18, struct BODY_VERTS);
if (Ctrl->F.active) { /* Need to compute observation coords only once */
size_t row;
for (row = 0; row < ndata_p; row++) {
x_obs[row] = (Ctrl->box.is_geog) ? (point->segment[0]->data[GMT_X][row] - lon_0) *
Ctrl->box.d_to_m*cos(point->segment[0]->data[GMT_Y][row] * D2R) : point->segment[0]->data[GMT_X][row];
y_obs[row] = (Ctrl->box.is_geog) ? -(point->segment[0]->data[GMT_Y][row] - lat_0) *
Ctrl->box.d_to_m : -point->segment[0]->data[GMT_Y][row]; /* - because y positive 'south' */
}
g = gmt_M_memory (GMT, NULL, nm, gmt_grdfloat);
}
if (Ctrl->T.triangulate) {
n_triang = Ctrl->n_vert;
body_desc.n_f = 5; /* Number of prism facets */
body_desc.n_v = gmt_M_memory(GMT, NULL, body_desc.n_f, unsigned int);
body_desc.n_v[0] = 3; body_desc.n_v[1] = 3;
body_desc.n_v[2] = 4; body_desc.n_v[3] = 4;
body_desc.n_v[4] = 4;
body_desc.ind = gmt_M_memory(GMT, NULL, (body_desc.n_v[0] + body_desc.n_v[1] +
body_desc.n_v[2] + body_desc.n_v[3] + body_desc.n_v[4]), unsigned int);
body_desc.ind[0] = 0; body_desc.ind[1] = 1; body_desc.ind[2] = 2; /* top triang */
body_desc.ind[3] = 3; body_desc.ind[4] = 5; body_desc.ind[5] = 4; /* bot triang */
body_desc.ind[6] = 1; body_desc.ind[7] = 4; body_desc.ind[8] = 5; body_desc.ind[9] = 2;
body_desc.ind[10] = 0; body_desc.ind[11] = 3; body_desc.ind[12] = 4; body_desc.ind[13] = 1;
body_desc.ind[14] = 0; body_desc.ind[15] = 2; body_desc.ind[16] = 5; body_desc.ind[17] = 3;
/* Actually, for the gravity case we can save lots of computations because the flux
through the vertical walls does not contribute to gravity anomaly, which is vertical */
if (Ctrl->C.active)
body_desc.n_f = 2; /* Number of prism facets that count */
}
else if (Ctrl->T.raw || Ctrl->T.stl || Ctrl->M.active) {
n_triang = (Ctrl->T.raw || Ctrl->M.active) ? Ctrl->n_raw_triang : ndata_s;
body_desc.n_f = 1;
body_desc.n_v = gmt_M_memory (GMT, NULL, body_desc.n_f, unsigned int);
body_desc.n_v[0] = 3;
body_desc.ind = gmt_M_memory (GMT, NULL, body_desc.n_v[0], unsigned int);
body_desc.ind[0] = 0; body_desc.ind[1] = 1; body_desc.ind[2] = 2;
}
else {
GMT_Report (API, GMT_MSG_ERROR, "It shouldn't pass here\n");
error = GMT_RUNTIME_ERROR;
goto END;
}
/* Allocate a structure that will be used inside okabe().
We do it here to avoid thousands of alloc/free that would result if done in okabe() */
n_vert_max = body_desc.n_v[0];
for (i = 1; i < body_desc.n_f; i++)
n_vert_max = MAX(body_desc.n_v[i], n_vert_max);
loc_or = gmt_M_memory (GMT, NULL, (n_vert_max+1), struct LOC_OR);
if (Ctrl->H.active) { /* 1e2 is a factor to obtain nT from magnetization in A/m */
cc_t = cos(Ctrl->H.m_dip*D2R)*cos((Ctrl->H.m_dec - 90.)*D2R);
cs_t = cos(Ctrl->H.m_dip*D2R)*sin((Ctrl->H.m_dec - 90.)*D2R);
s_t = sin(Ctrl->H.m_dip*D2R);
if (!Ctrl->T.m_var4) { /* In all the other cases the field parameters are constant */
okabe_mag_param = gmt_M_memory (GMT, NULL, 1, struct MAG_PARAM);
okabe_mag_param[0].rim[0] = 1e2*cos(Ctrl->H.t_dip*D2R) * cos((Ctrl->H.t_dec - 90.)*D2R);
okabe_mag_param[0].rim[1] = 1e2*cos(Ctrl->H.t_dip*D2R) * sin((Ctrl->H.t_dec - 90.)*D2R);
okabe_mag_param[0].rim[2] = 1e2*sin(Ctrl->H.t_dip*D2R);
}
if (!Ctrl->T.m_var) { /* Case of constant magnetization */
okabe_mag_var = gmt_M_memory (GMT, NULL, 1, struct MAG_VAR);
okabe_mag_var[0].rk[0] = Ctrl->H.m_int * cc_t;
okabe_mag_var[0].rk[1] = Ctrl->H.m_int * cs_t;
okabe_mag_var[0].rk[2] = Ctrl->H.m_int * s_t;
}
else { /* The triangles have a non-constant magnetization */
okabe_mag_var = gmt_M_memory (GMT, NULL, n_triang, struct MAG_VAR);
if (Ctrl->T.m_var1) { /* Only the mag intensity changes. Mag dec & dip are constant */
for (i = 0; i < n_triang; i++) {
t_mag = (Ctrl->box.mag_int[vert[i].a] + Ctrl->box.mag_int[vert[i].b] + Ctrl->box.mag_int[vert[i].c])/3.;
okabe_mag_var[i].rk[0] = t_mag * cc_t;
okabe_mag_var[i].rk[1] = t_mag * cs_t;
okabe_mag_var[i].rk[2] = t_mag * s_t;
}
}
else if (Ctrl->T.m_var2) { /* Both mag intensity & dip varies. Dec is Zero (axial dipole) */
for (i = 0; i < n_triang; i++) {
t_mag = (okabe_mag_var2[vert[i].a].m + okabe_mag_var2[vert[i].b].m + okabe_mag_var2[vert[i].c].m)/3.;
Ctrl->H.t_dip = (okabe_mag_var2[vert[i].a].m_dip + okabe_mag_var2[vert[i].b].m_dip + okabe_mag_var2[vert[i].c].m_dip)/3.;
okabe_mag_var[i].rk[0] = 0.;
okabe_mag_var[i].rk[1] = -t_mag * cos(Ctrl->H.t_dip*D2R);
okabe_mag_var[i].rk[2] = t_mag * sin(Ctrl->H.t_dip*D2R);
}
}
else if (Ctrl->T.m_var3) { /* Both mag intensity, mag_dec & mag_dip varies. */
for (i = 0; i < n_triang; i++) {
t_mag = (okabe_mag_var3[vert[i].a].m + okabe_mag_var3[vert[i].b].m + okabe_mag_var3[vert[i].c].m)/3.;
Ctrl->H.t_dec = (okabe_mag_var3[vert[i].a].m_dec + okabe_mag_var3[vert[i].b].m_dec + okabe_mag_var3[vert[i].c].m_dec)/3.;
Ctrl->H.t_dip = (okabe_mag_var3[vert[i].a].m_dip + okabe_mag_var3[vert[i].b].m_dip + okabe_mag_var3[vert[i].c].m_dip)/3.;
okabe_mag_var[i].rk[0] = t_mag * cos(Ctrl->H.t_dip*D2R) * cos((Ctrl->H.t_dec - 90)*D2R);
okabe_mag_var[i].rk[1] = t_mag * cos(Ctrl->H.t_dip*D2R) * sin((Ctrl->H.t_dec - 90)*D2R);
okabe_mag_var[i].rk[2] = t_mag * sin(Ctrl->H.t_dip*D2R);
}
}
else { /* Everything varies. */
if (okabe_mag_param == NULL) /* If not yet allocated */
okabe_mag_param = gmt_M_memory (GMT, NULL, n_triang, struct MAG_PARAM);
for (i = 0; i < n_triang; i++) {
Ctrl->H.t_dec = (okabe_mag_var4[vert[i].a].t_dec + okabe_mag_var4[vert[i].b].t_dec + okabe_mag_var4[vert[i].c].t_dec)/3.;
Ctrl->H.t_dip = (okabe_mag_var4[vert[i].a].t_dip + okabe_mag_var4[vert[i].b].t_dip + okabe_mag_var4[vert[i].c].t_dip)/3.;
okabe_mag_param[i].rim[0] = 1e2*cos(Ctrl->H.t_dip*D2R) * cos((Ctrl->H.t_dec - 90.)*D2R);
okabe_mag_param[i].rim[1] = 1e2*cos(Ctrl->H.t_dip*D2R) * sin((Ctrl->H.t_dec - 90.)*D2R);