-
Notifications
You must be signed in to change notification settings - Fork 2
/
locate.c
138 lines (130 loc) · 3.84 KB
/
locate.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
/*
Routines to solve for eq locations.
eik dec 2016
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "ray.h"
VelModel mp,ms ;
void printModel( char *text, double *x )
{
/* printf("Model %s : %9.4f %8.4f %8.4f %8.4f\n",text,x[0],x[1]*100.0,x[2]*100.0,x[3]) ; */
printf("Model %s : %9.5f %9.5f %9.5f %9.5f\n",text,x[0],x[1],x[2],x[3]) ;
}
void insertRow( double *x, double *a, int i, int m, int n, double weight )
{
while(n--) { a[i] = weight * *x++ ; i += m ; }
}
int locate( Solution *sol, Phase *pp )
{
#define MAXPHASES 30
double x[4],x0[4],b[MAXPHASES],a[4*MAXPHASES] ;
double dx[4], dxtest[4] ;
double tt,dlon,distance,residual,sum ;
double rayp,dtdx,dxdp,km2lat,km2lon ;
double dz,ttz,dtdz,dtdla,dtdlo ;
double dl,dtdlax,dtdlox,damp, weight ;
VelModel *vm ;
Phase *p ;
Station *s ;
int np,i,j,iter ;
if (sol->index != pp->index ) {
rLog(2,"Index in locate does not match",NULL) ;
return 0 ;
}
p = pp ;
while( p->index == sol->index ) p++ ;
np = p-pp ;
if( np > MAXPHASES ) rLog(1,"locate: more than %d phases", (void*) MAXPHASES ) ;
printf("locate: %d phases\n",np) ;
x0[0] = 0.0 ; /* origin time, sec */
x0[1] = sol->lat ; /* latitude, degrees */
x0[2] = sol->lon ; /* longitude, degrees */
x0[3] = sol->depth ; /* depth, km */
dz = 0.01 ;
km2lat = 6391*M_PI/180.0 ;
km2lon = km2lat * cos(sol->lat * M_PI/180.0 ) ;
printModel("x0",x0) ;
for ( iter = 0 ; iter < 12 ; iter++) {
sum = 0.0 ;
for( i = 0 ; i < np ; i++ ) {
p = pp+i ;
s = p->statP ;
dlon = x0[2] - s->lon ;
if( p->type == 'P' ) vm = &mp ; else vm = &ms ;
if( p->type == 'P' ) weight = 1.0 ; else weight = 2.7 ;
weight = 1.0 ;
distance = gDistance(x0[1],s->lat,dlon) ;
tt = timeFromDist(vm,distance,x0[3],&rayp,&dtdx,&dxdp) + x0[0] ;
ttz = timeFromDist(vm,distance,x0[3]+dz,&rayp,&dtdx,&dxdp) +x0[0] ;
dtdz = (ttz-tt)/dz ;
dtdla = dtdx * ( x0[1] - s->lat )*km2lat*km2lat / distance ;
dtdlo = dtdx * ( x0[2] - s->lon )*km2lon*km2lon / distance ;
dx[0] = 1.0 ; dx[1] = dtdla ; dx[2] = dtdlo ; dx[3] = dtdz ;
insertRow(dx,a,i,np,4,weight) ;
b[i] = weight * ( p->pTime - tt ) ;
dl = 0.0001 ;
distance = gDistance(x0[1]+dl,s->lat,dlon) ;
ttz = timeFromDist(vm,distance,x0[3],&rayp,&dtdx,&dxdp) +x0[0] ;
dtdlax = (ttz-tt)/dl ;
distance = gDistance(x0[1],s->lat,dlon+dl) ;
ttz = timeFromDist(vm,distance,x0[3],&rayp,&dtdx,&dxdp) +x0[0] ;
dtdlox = (ttz-tt)/dl ;
residual = tt - p->pTime ;
sum += residual*residual ;
printf("%s %c %10.6f %10.6f %10.6f %10.6f %10.6f ",s->name,p->type,p->pTime,tt,residual,distance,x0[3]) ;
printf("%8.4f %8.4f %8.4f ",dtdlax,dtdlox,1.0/dtdx) ;
printModel(" dx ",dx) ;
}
golubC(a,x,b,np,4) ;
printModel(" x ",x) ;
damp = 0.55 ;
if( iter > 7 ) damp = 1.0 ;
for( j = 0 ; j < 4 ; j++) x0[j] += damp*x[j] ;
printModel(" x0",x0) ;
printf("std_dev =%10.6f damp=%7.2f\n",sqrt(sum/np),damp) ;
}
}
#ifdef TEST
char *pModel = "silp.vel" ;
char *sModel = "sils.vel" ;
char *phaseFile = "../geysir/phase.dat" ;
char *solFile = "../geysir/ctloc2" ;
doit()
{
Phase *phases, *ip ;
Solution *location, *lp ;
int nPhases,nLoc ,i,j ;
long long index,i2 ;
Station *sp ;
initVelModel(20,&mp ) ;
initVelModel(20,&ms ) ;
readVelModel(pModel,&mp) ;
readVelModel(sModel,&ms) ;
nPhases = readPhases(phaseFile,&phases ) ;
nLoc = readCtloc(solFile,&location) ;
lp = location + 6 ;
index = lp->index ;
printf("index=%ld\n",index) ;
ip = phases ;
while( ip->index < index ) ip++ ;
j = locate( lp, ip ) ;
while( ip->index == index ) {
sp = ip->statP ;
printf("%ld %s %10.6f %10.6f\n",ip->index,sp->name,ip->pTime,ip->weight ) ;
ip++ ;
}
printf("%d phases\n",ip-phases ) ;
}
int main(int ac, char **av) {
int cc,n ;
/* feenableexcept(FE_INVALID) ; */
shLogLevel = 2 ;
while( EOF != ( cc = getopt(ac,av,"tpcr"))) {
switch(cc) {
}}
doit() ;
return 0 ;
}
#endif