-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothers.f90
1371 lines (1108 loc) · 39.7 KB
/
others.f90
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
!
! others.f90
! MarsInversion
!
! Created by fuji on 04/12/2019.
! Copyright 2019 nfuji. All rights reserved.
!
subroutine pinput
use parameters
use angles
implicit none
character(200) :: argv
integer :: argc
character(200) :: tmpfile,metafile
character(200) :: obsfile
character(200) :: dummy
real(kind(0d0)) :: fdummy,fdummy2,phirq
!integer, external :: getpid
integer :: iloop,it,icheck,jloop,iFind
!character(200) :: commandline
character(200) :: paramName
call getarg(1,argv)
metafile=argv
call getarg(2,argv)
workingDir=argv
call getarg(3,argv)
resultDir=argv
call getarg(4,argv)
inversionName=argv
argc=iargc()
dummy = 'mkdir -p '//trim(resultDir)
call system(dummy)
if(argc.ne.4) then
print *, "you need <metafile> <working directory> <result directory> <inversion name>"
print *, "cheers"
stop
endif
!write(tmpfile,"(Z5.5)") getpid()
!tmpfile='tmpfileMarsInversion'//tmpfile
tmpfile='tmpfileMarsInversion'
open(unit=5,file=metafile,status='unknown')
open(unit=1,file=tmpfile,status='unknown')
100 continue
read(5,110) dummy
110 format(a200)
if(dummy(1:1).eq.'#') goto 100
if(dummy(1:3).eq.'end') goto 120
write(1,110) dummy
goto 100
120 continue
close(1)
close(5)
!open(unit=1,file=tmpfile,status='unknown')
calculMode=0
nmt=6
!read(1,110) dummy ! This dummy string determines i) test mode, ii) Alice normal, or iii) versionSGT mode
paramName="calculMode"
call searchForParams(tmpfile,paramName,dummy,0)
print *, "calculMode is ", dummy
if((dummy(1:10).eq.'versionSGT').or.(dummy(1:10).eq.'monitoring') &
.or.(dummy(1:12).eq.'lightMonitor').or.(dummy(1:12).eq.'heavyMonitor').or.(dummy(1:9).eq.'synthetic')) then
!close(1)
if(dummy(1:10).eq.'versionSGT') calculMode=2
if(dummy(1:10).eq.'monitoring') calculMode=3
if(dummy(1:12).eq.'lightMonitor') calculMode=4
if(dummy(1:12).eq.'heavyMonitor') calculMode=5
if(dummy(1:9).eq.'synthetic') calculMode=10
paramName="inversionMode"
inversionMode="6components"
call searchForParamsOption(tmpfile,paramName,inversionMode,0,iFind)
print *, "inversionMode = ", inversionMode
if(calculMode.eq.10) then
Mij_synthetic=0.d0
Mij_synthetic(1)=1.d0 ! Mrr activated
paramName="momentTensor"
call searchForParamsOption(tmpfile,paramName,dummy,1,iFind)
if(iFind.eq.1) read(dummy,*) Mij_synthetic(1:6)
if(iFind.ne.1) print *, "Since you did not precise Mij, we chose 1 0 0 0 0 0"
print *, "momentTensor", Mij_synthetic
dummy = 'mkdir -p '//trim(workingDir)
call system(dummy)
endif
nmt=6
if(calculMode.eq.5) then
print *, "are you sure that you are using MPI version?"
paramName='toleranceDistance'
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) toleranceDistance
endif
ZRTorZNE="ZRT" ! ZRT is used unless ZNE is explicitly defined
paramName="ZRTorZNE"
call searchForParamsOption(tmpfile,paramName,ZRTorZNE,0,iFind)
print *, "ZRTorZNE is ", ZRTorZNE
paramName="SGTinfo"
call searchForParams(tmpfile,paramName,SGTinfo,0)
print *, "SGTinfo is ", SGTinfo
paramName="parentDir"
call searchForParams(tmpfile,paramName,parentDir,0)
print *, "parentDir is ", parentDir
paramName="eventName"
call searchForParams(tmpfile,paramName,eventName,0)
print *, "eventName is ", trim(eventName)
paramName="stationName"
call searchForParams(tmpfile,paramName,stationName,0)
print *, "stationName is ", stationName
paramName="stlalo"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy, *) stla, stlo
print *, "stla is ", stla, " and stlo is", stlo
rlat=stla
rlon=stlo
paramName="searchAreaDistance"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) gcarcmin,gcarcmax,dgcarc
print *, "gcarc min, max and interval are: ", gcarcmin,gcarcmax,dgcarc
ntheta = int((gcarcmax-gcarcmin)/dgcarc)+1
allocate(gcarc(1:ntheta))
allocate(ithetaD(1:ntheta))
do iloop=1,ntheta
gcarc(iloop) = gcarcmin + dble(iloop-1)*dgcarc
enddo
paramName="searchAreaAzimuth"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) azimuthmin,azimuthmax,dazimuth
print *, "azimuth min, max and interval are: ", azimuthmin,azimuthmax,dazimuth
nphi = int((azimuthmax-azimuthmin)/dazimuth)+1
allocate(azimuth(1:nphi))
do iloop=1,nphi
azimuth(iloop) = azimuthmin + dble(iloop-1)*dazimuth
enddo
paramName="searchAreaRadius"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) radiusmin,radiusmax,dradius
print *, "source radius min, max and interval are: ", radiusmin,radiusmax,dradius
nr = int((radiusmax-radiusmin)/dradius)+1
allocate(radius(1:nr))
allocate(iradiusD(1:nr))
do iloop=1,nr
radius(iloop) = radiusmin + dble(iloop-1)*dradius
enddo
paramName="dt"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) dt
print *, "dt is ", dt, "(s)"
samplingHz=1.d0/dt
!paramName="tlenDSM"
!call searchForParams(tmpfile,paramName,dummy,1)
!read(dummy,*) tlenDSM
!print *, "tlen in DSM synthetic is ", tlenDSM, " (s)"
!npDSM=int(tlenDSM/dt)
!print *, " thus npDSM is ", npDSM
paramName="tlenData"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) tlenData
npData = int(tlenData/dt)
print *, "tlenData is ", tlenData, " (s) and thus npData is ", npData
paramName="movingWindowStep"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) movingWindowStep
print *, "movingWindowStep for each MT inversion is ", movingWindowStep, " (s)"
ntStep=int(movingWindowStep/dt)
print *, " thus npStep is ", ntStep
paramName="npButterworth"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) npButterworth
paramName="fmin"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) fmin
paramName="fmax"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) fmax
print *, "Butterworth parameters (npButterworth, fmin, fmax) are:"
print *, " ", npButterworth, fmin, fmax
paramName="effectiveSynWindow"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) start, end
print *, "effective DSM synthetic window is from ", start, " (s) to ", end, " (s)"
paramName="numberofSynWindows"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) ntwin
allocate(twin(1:4,1:ntwin))
allocate(itwin(1:4,1:ntwin))
do iloop=1,ntwin
write(paramName,*) iloop
paramName="synWindow"//trim(adjustl(paramName))
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) twin(1,iloop),twin(2,iloop),twin(3,iloop),twin(4,iloop)
print *, iloop,"-th window in syn is characterised by:", &
twin(1,iloop),twin(2,iloop),twin(3,iloop),twin(4,iloop)
enddo
itwin=int(twin/dt)
allocate(obsRaw(0:npData,1:3))
allocate(obsFilt(0:npData,1:3))
allocate(obsFiltTapered(0:npData,1:3))
if(calculMode.ne.10) then
paramName="obsZfile"
call searchForParams(tmpfile,paramName,obsfile,0)
obsfile=trim(workingDir)//"/"//trim(obsfile)
print *, "Reading obsZfile: ", obsfile
open(unit=10,file=obsfile,status='unknown')
do it=0,npData
read(10,*) obsRaw(it,1)
enddo
paramName="obsNfile"
call searchForParams(tmpfile,paramName,obsfile,0)
obsfile=trim(workingDir)//"/"//trim(obsfile)
print *, "Reading obsNfile: ", obsfile
open(unit=10,file=obsfile,status='unknown')
do it=0,npData
read(10,*) obsRaw(it,2)
enddo
paramName="obsEfile"
call searchForParams(tmpfile,paramName,obsfile,0)
obsfile=trim(workingDir)//"/"//trim(obsfile)
print *, "Reading obsEfile: ", obsfile
open(unit=10,file=obsfile,status='unknown')
do it=0,npData
read(10,*) obsRaw(it,3)
enddo
paramName="numberofObsWindows"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) ntwinObs
paramName="obsMovingWindowOption"
call searchForParams(tmpfile,paramName,dummy,0)
if(dummy(1:5).eq.'fixed') then
NmovingWindowDimension=1
elseif(dummy(1:).eq.'independent') then
NmovingWindowDimension=ntwinObs
endif
print *, "the number of Obs Windows is ", ntwinObs, " and the dimension is ", &
NmovingWindowDimension, " since you chose the ", trim(dummy), " option."
if((calculMode.eq.3).and.(NmovingWindowDimension.ne.1)) then
print *, "monitoring mode is not supporting independent window shifting"
stop
endif
if((calculMode.eq.4).and.(NmovingWindowDimension.ne.1)) then
print *, "light monitoring mode is not supporting independent window shifting"
stop
endif
if((calculMode.eq.5).and.(NmovingWindowDimension.ne.1)) then
print *, "heavy monitoring mode is not supporting independent window shifting"
stop
endif
allocate(twinObs(1:4,1:ntwinObs))
allocate(itwinObs(1:4,1:ntwinObs))
do iloop=1,ntwinObs
write(paramName,*) iloop
paramName="obsWindow"//trim(adjustl(paramName))
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) twinObs(1,iloop),twinObs(2,iloop),twinObs(3,iloop),twinObs(4,iloop)
print *, iloop,"-th window in obs is characterised by:", &
twinObs(1,iloop),twinObs(2,iloop),twinObs(3,iloop),twinObs(4,iloop)
enddo
itwinObs=int(twinObs/dt)
endif
!commandline = 'mkdir -p '//trim(parentDir)
!call system(commandline)
call pinputDSM(DSMconfFile,PoutputDir,psvmodel,&
modelname,tlenFull,rmin_,rmax_,rdelta_, &
r0min,r0max,r0delta,thetamin,thetamax,thetadelta, &
imin,imax,rsgtswitch,tsgtswitch, &
synnswitch,SGTinfo)
call readDSMconf(DSMconfFile,re,ratc,ratl,omegai,maxlmax)
! lsmoothfinder for FFT
np0=imax
call lsmoothfinder(tlenFull,np0,samplingHz,lsmooth)
iloop=1
do while (iloop<lsmooth)
iloop = iloop*2
enddo
lsmooth = iloop
iloop = 0
np1 = 1
do while (np1<np0)
np1 = np1*2
enddo
np1 = np1*lsmooth
! redefinition of samplingHz
samplingHz = dble(2*np1)/tlenFull
dtn = 1.d0/samplingHz
iWindowStart = int(start*samplingHz)
iWindowEnd = int(end*samplingHz)
call makingIndependentWindow
if(tlenFull<end) then
print *, "DSM tlen is ", tlenFull, "and you asked to take window up to ", end
print *, "it is impossible, sorry."
stop
endif
tmpfile='tmpReadPSVmodel'
call readpsvmodel(psvmodel,tmpfile)
INFO_TSGT = trim(parentDir)//"/INFO_TSGT.TXT"
INFO_RSGT = trim(parentDir)//"/INFO_RSGT.TXT"
rsampletxt = trim(parentDir)//"/rsample.txt"
modelcard = trim(parentDir)//"/"//trim(modelname)//".card"
!synnfile = trim(parentDir)//"/"//trim(stationName)//"."//trim(eventName)//"."//trim(compo)//"s.dat"
r_n = int((rmax_-rmin_)/rdelta_)+1
allocate(r_(1:r_n))
do iloop=1,r_n
r_(iloop) = rmin_ + dble(iloop-1)*rdelta_
enddo
theta_n = int((thetamax-thetamin)/thetadelta)+1
allocate(thetaD(1:theta_n))
do iloop=1,theta_n
thetaD(iloop) = thetamin + dble(iloop-1)*thetadelta
enddo
print *, "r, theta are discretised in ", r_n, theta_n, "pieces in DSM pre-calculation"
! check r(:) and r_(:) because we don't interpolate for depth
iradiusD=0
do iloop=1,nr
icheck=0
do jloop=1,r_n
if(dabs(r_(jloop)-radius(iloop))< 1.d-5*radius(iloop)) then
icheck=1
iradiusD(iloop)=jloop
endif
enddo
if(icheck.eq.0) then
print *, radius(iloop), "is not in the catalogue, sorry"
stop
endif
enddo
! check gcarc(:) and thetaD(:) because we don't interpolate for depth
ithetaD=0
do iloop=1,ntheta
icheck=0
do jloop=1,theta_n
if(dabs(thetaD(jloop)-gcarc(iloop))<1.d-5*gcarc(iloop)) then
icheck=1
ithetaD(iloop)=jloop
endif
enddo
if(icheck.eq.0) then
print *, gcarc(iloop), "is not in the catalogue, sorry"
stop
endif
enddo
allocate(latgeo(1:nphi,1:ntheta),longeo(1:nphi,1:ntheta))
allocate(crq(1:nphi,1:ntheta),crq2(1:nphi,1:ntheta))
allocate(srq(1:nphi,1:ntheta),srq2(1:nphi,1:ntheta))
allocate(cqr(1:nphi,1:ntheta),sqr(1:nphi,1:ntheta))
latgeo=0.d0
longeo=0.d0
do iloop=1,ntheta
do jloop=1,nphi
call geoCoordinates(stla*degree2radian,stlo*degree2radian, &
latgeo(jloop,iloop),longeo(jloop,iloop),azimuth(jloop)*degree2radian, &
fdummy,gcarc(iloop)*degree2radian) !!! geoCooridnates uses radian
enddo
enddo
latgeo=latgeo*radian2degree
longeo=longeo*radian2degree
print *, "InSight: ", stla, stlo
do iloop=1,ntheta
do jloop=1,nphi
!print *, gcarc(iloop),azimuth(jloop), latgeo(jloop,iloop),longeo(jloop,iloop)
!azimuth(jloop)=-azimuth(jloop)
call azimth(0,latgeo(jloop,iloop),longeo(jloop,iloop),stla,stlo,fdummy2,phirq,fdummy)
!print *, " inverse: ",fdummy2,phirq,fdummy
!print *, phirq,fdummy
phirq=pi-phirq*degree2radian
fdummy=fdummy*degree2radian
if(phirq.lt.0.d0) phirq=phirq+2.d0*pi
if(phirq.gt.(2.d0*pi)) phirq=phirq-2.d0*pi
crq(jloop,iloop)=dcos(phirq)
srq(jloop,iloop)=dsin(phirq)
crq2(jloop,iloop)=dcos(2.d0*phirq)
srq2(jloop,iloop)=dsin(2.d0*phirq)
cqr(jloop,iloop)=dcos(fdummy)
sqr(jloop,iloop)=dsin(fdummy)
enddo
enddo
! allocate SGTs, synthetics in frequency
allocate(omega(imin:imax))
do iloop = imin, imax
omega(iloop) = 2.d0*pi*dble(iloop)/tlenFull
enddo
!nConfiguration=r_n*theta_n*phi_n
nConfiguration=nr*ntheta*nphi
print *, "source locations to be tested (in r, theta, phi, total): "
print *, " ", nr,ntheta,nphi,nConfiguration
!elseif((dummy(1:6).eq.'normal').or.(dummy(1:4).eq.'test')) then
else
print *, "no more support for non RSGT methods. Sorry."
stop
endif
end subroutine pinput
subroutine azimth(ellips,slat,slon,rlat,rlon,delta,azim,bazim)
! This routine uses Euler angles to find the geocentric distance,
! azimuth, and back azimuth for a source-reciever pair.
!
! Input
!
! slat - source geographic latitude in decimal degrees
! slon - source longitude in decimal degrees
! rlat - reciever geographic latitude in decimal degrees
! rlon - reciever longitude in decimal degrees
!
! Output
!
! delta - geocentric source-reciever distance in decimal degrees of arc
! azim - geocentric azimuth from the source to the reciever
! bazim - geocentric back azimuth from the reciever to the source
!
! The distance calculated here delta is always between 0 and 180 degrees.
! Accordingly, the azimuth and back azimuth are defined for the minor
! arc between (slat,slon) and (rlat,rlon).
!
! if ellips = 0 then geocentric = geocentric
! because in NF version it is already taken into account so ellips should be 0 always
implicit none
real(kind(0d0)) :: dtor,e,slatra,slat,w,s,scolat,rlatra,rlat,rcolat,slonra,rlon,c2,s2,c1,s1,slatrc,x0,y0,z0,x1,y1,z1,x2,y2,z2,slon,rlonra,delta,azim,bazim,pi
real(kind(0d0)), parameter :: flt = 298.25d0
integer :: ellips
dtor=4.d0*datan(1.d0)/180.d0
pi=4.d0*datan(1.d0)
if(ellips.ne.0) then
e=1.d0/flt
else
e=0.d0
endif
! Convert to geocentric coordinates and from latitude to colatitude.
slatra=dtor*slat
w=dsin(slatra)
s=((2.d0-e)*w+4.d0*e*(w**3))*e*dcos(slatra)
!scolat=1.5707963d0-slatra+s
scolat=pi*5.d-1-slatra+s
rlatra=dtor*rlat
w=dsin(rlatra)
s=((2.d0-e)*w+4.d0*e*(w**3))*e*dcos(rlatra)
!rcolat=1.5707963d0-rlatra+s
rcolat=pi*5.d-1-rlatra+s
slonra=slon*dtor
rlonra=rlon*dtor
c2=dcos(scolat)
s2=dsin(scolat)
c1=dcos(slonra)
s1=dsin(slonra)
slatrc=dsin(rcolat)
! Find the azimuth and distance by rotating the source to the north pole.
x0=slatrc*dcos(rlonra)
y0=slatrc*dsin(rlonra)
z0=dcos(rcolat)
x1=c1*x0+s1*y0
z0=dcos(rcolat)
x1=c1*x0+s1*y0
y1=-s1*x0+c1*y0
z1=z0
x2=c2*x1-s2*z1
y2=y1
z2=c2*z1+s2*x1
call angles(x2,y2,z2,delta,azim)
azim=180.d0-azim
! Find the back azimuth by rotating the receiver to the north pole.
c2=dcos(rcolat)
s2=dsin(rcolat)
c1=dcos(rlonra)
s1=dsin(rlonra)
slatrc=dsin(scolat)
x0=slatrc*dcos(slonra)
y0=slatrc*dsin(slonra)
z0=dcos(scolat)
x1=c1*x0+s1*y0
y1=-s1*x0+c1*y0
z1=z0
x2=c2*x1-s2*z1
y2=y1
z2=c2*z1+s2*x1
call angles(x2,y2,z2,delta,bazim)
bazim=180.d0-bazim
return
end subroutine azimth
subroutine angles(x,y,z,theta,phi)
! Finds the angles theta and phi of a spherical polar coordinate
! system from the cartesion coordinates x, y, and z.
implicit none
real(kind(0d0)) :: pi,rtod,arg1,x,y,theta,phi,z
! real(kind(0d0)), parameter :: eps = 1.d-14
real(kind(0d0)), parameter :: eps = 0.d0
pi=4.d0*datan(1.d0)
rtod=180.d0/pi
arg1=dsqrt(x*x+y*y)
theta=datan2(arg1,z)
if(dabs(x).le.eps.and.dabs(y).le.eps) then
phi=0.d0
else
phi=datan2(y,x)
endif
phi=phi*rtod
theta=theta*rtod
return
end subroutine angles
subroutine geoCoordinates(glat1,glon1,glat2,glon2,faz,baz,s)
!
! *** solution of the geodetic direct problem after t.vincenty
! *** modified rainsford's method with helmert's elliptical terms
! *** effective in any azimuth and at any distance short of antipodal
!
! *** a is the semi-major axis of the reference ellipsoid
! *** f is the flattening of the reference ellipsoid
! *** latitudes and longitudes in radians positive north and east
! *** azimuths in radians clockwise from north
! *** geodesic distance s assumed in units of semi-major axis a
!
! *** programmed for cdc-6600 by lcdr l.pfeifer ngs rockville md 20feb75
! *** modified for system 360 by john g gergen ngs rockville md 750608
!
! Here everything is in radian!
!implicit real*8 (a-h,o-z)
implicit none
real(kind(0d0)) :: glat1,glon1,glat2,glon2,faz,baz,s
real(kind(0d0)) :: a,pi,f,eps,r,tu,sf,cf,cu,su,sa,c2a,x,c,d,y,sy,cy,cz,e
!common/const/pi,rad
!common/elipsoid/a,f
a=1
pi=4.d0*datan(1.d0)
f=0
eps = 0.5d-13
r=1.-f
tu=r*dsin(glat1)/dcos(glat1)
sf=dsin(faz)
cf=dcos(faz)
baz=0.
if(abs(cf)>eps) baz=datan2(tu,cf)*2.
cu=1./dsqrt(tu*tu+1.)
su=tu*cu
sa=cu*sf
c2a=-sa*sa+1.
x=dsqrt((1./r/r-1.)*c2a+1.)+1.
x=(x-2.)/x
c=1.-x
c=(x*x/4.+1)/c
d=(0.375d0*x*x-1.)*x
tu=s/r/a/c
y=tu
100 sy=dsin(y)
cy=dcos(y)
cz=dcos(baz+y)
e=cz*cz*2.-1.
c=y
x=e*cy
y=e+e-1.
y=(((sy*sy*4.-3.)*y*cz*d/6.+x)*d/4.-cz)*sy*d+tu
if(dabs(y-c).gt.eps)go to 100
baz=cu*cy*cf-su*sy
c=r*dsqrt(sa*sa+baz*baz)
d=su*cy+cu*sy*cf
glat2=datan2(d,c)
c=cu*cy-su*sy*cf
x=datan2(sy*sf,c)
c=((-3.*c2a+4.)*f+4.)*c2a*f/16.
d=((e*cy*c+cz)*sy*c+y)*sa
glon2=glon1+x-(1.-c)*d*f
baz=datan2(sa,baz)+pi
return
end subroutine
subroutine searchForParams(filename,ParamName,textParam,paramisText)
implicit none
character(200) :: filename,textParam,text_line
integer :: paramLength,textLength,paramisText,io
character(200) :: ParamName
integer :: iFind, jtemp, iCut
filename=trim(filename)
ParamName=trim(ParamName)
paramLength=len_trim(ParamName)
!print *, paramLength, ParamName
iFind=0
iCut=0
open(20,file=filename,status='unknown')
do while(iFind.eq.0)
read(20,'(a)',IOSTAT=io) text_line
if(io>0) then
print *, "oh, no"
print *, trim(ParamName), " is not found."
stop
endif
textLength=len_trim(text_line)
!print *, text_line(1:textLength)
if(text_line(1:paramLength).eq.ParamName(1:paramLength)) then
do jtemp = 1,textLength
if(text_line(jtemp:jtemp).eq.'=') iCut = jtemp
enddo
!print *, iCut, textLength
iFind=1
!print *,text_line(iCut+1:textLength)
textParam=text_line(iCut+1:textLength)
if(paramisText.eq.0) then
textParam=trim(textParam)
textParam=adjustl(textParam)
endif
!print *, textParam
endif
enddo
close(20)
if(iFind.eq.0) then
print *, ParamName, "is not found."
stop
endif
end subroutine
subroutine searchForParamsOption(filename,ParamName,textParam,paramisText,iFind)
implicit none
character(200) :: filename,textParam,text_line
integer :: paramLength,textLength,paramisText,io
character(200) :: ParamName
integer :: iFind, jtemp, iCut
filename=trim(filename)
ParamName=trim(ParamName)
paramLength=len_trim(ParamName)
!print *, paramLength, ParamName
iFind=0
iCut=0
open(20,file=filename,status='unknown')
do while(iFind.eq.0)
read(20,'(a)',IOSTAT=io) text_line
if(io>0) then
!print *, "oh, no"
!print *, trim(ParamName), " is not found. But maybe it's alright (option parameters)."
!stop
iFind=2
endif
textLength=len_trim(text_line)
!print *, text_line(1:textLength)
if(text_line(1:paramLength).eq.ParamName(1:paramLength)) then
do jtemp = 1,textLength
if(text_line(jtemp:jtemp).eq.'=') iCut = jtemp
enddo
!print *, iCut, textLength
iFind=1
!print *,text_line(iCut+1:textLength)
textParam=text_line(iCut+1:textLength)
if(paramisText.eq.0) then
textParam=trim(textParam)
textParam=adjustl(textParam)
endif
!print *, textParam
endif
enddo
close(20)
if(iFind.eq.2) iFind=0
end subroutine
subroutine makingIndependentWindow
use parameters
implicit none
integer :: iloop,jloop,tmpinteger
integer, allocatable :: indexInWindow(:)
character(200) :: dummy, paramName, tmpfile
! Moving windows
allocate(fMovingWindowStart(1:NmovingWindowDimension))
allocate(fMovingWindowEnd(1:NmovingWindowDimension))
allocate(iMovingWindowStart(1:NmovingWindowDimension))
allocate(iMovingWindowEnd(1:NmovingWindowDimension))
allocate(indexInWindow(1:ntwinObs))
allocate(totalNumberInWindowDimension(1:NmovingWindowDimension))
tmpfile='tmpfileMarsInversion'
if((calculMode.eq.2).or.(calculMode.eq.10)) then
do iloop=1,NmovingWindowDimension
write(paramName,*) iloop
paramName="movingWindowRange"//trim(adjustl(paramName))
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*) fMovingWindowStart(iloop), fMovingWindowEnd(iloop)
print *, iloop,"-th moving range in obs is characterised by:", &
fMovingWindowStart(iloop), fMovingWindowEnd(iloop)
enddo
elseif((calculMode.eq.3).or.(calculMode.eq.4).or.(calculMode.eq.5)) then
paramName="movingWindowRangeMonitoring"
call searchForParams(tmpfile,paramName,dummy,1)
read(dummy,*)fMovingWindowStart(1), fMovingWindowEnd(1)
endif
iMovingWindowStart=int(fMovingWindowStart/dt)
iMovingWindowEnd=int(fMovingWindowEnd/dt)
nTimeCombination = 1
if((calculMode.eq.2).or.(calculMode.eq.10)) then
do iloop=1,NmovingWindowDimension
iMovingWindowStart(iloop)=itwinObs(1,iloop)+iMovingWindowStart(iloop)
iMovingWindowEnd(iloop)=itwinObs(1,iloop)+iMovingWindowEnd(iloop)
totalNumberInWindowDimension(iloop)=(iMovingWindowEnd(iloop)-iMovingWindowStart(iloop))/ntStep+1
nTimeCombination = nTimeCombination*totalNumberInWindowDimension(iloop)
!print *, iloop, totalNumberInWindowDimension(iloop)
enddo
elseif((calculMode.eq.3).or.(calculMode.eq.4).or.(calculMode.eq.5)) then
totalNumberInWindowDimension(1)=(iMovingWindowEnd(1)-iMovingWindowStart(1))/ntStep+1
nTimeCombination = totalNumberInWindowDimension(1)
!print *, "The number of time window shift steps for monitoring:", nTimeCombination
endif
allocate(fEachShift(1:nTimeCombination,1:ntwinObs))
allocate(iEachWindowStart(1:nTimeCombination,1:ntwinObs))
allocate(iEachWindowEnd(1:nTimeCombination,1:ntwinObs))
if((calculMode.eq.2).or.(calculMode.eq.10)) then
do jloop=1,nTimeCombination
tmpinteger=jloop-1
if(NmovingWindowDimension.eq.ntwinObs) then ! independent time window
do iloop=1,NmovingWindowDimension
indexInWindow(iloop)=mod(tmpinteger,totalNumberInWindowDimension(iloop))+1
tmpinteger=tmpinteger/totalNumberInWindowDimension(iloop)
fEachShift(jloop,iloop)=fMovingWindowStart(iloop)+dt*dble(ntStep)*dble(indexInWindow(iloop)-1)
iEachWindowStart(jloop,iloop)=iMovingWindowStart(iloop)+ntStep*(indexInWindow(iloop)-1)
iEachWindowEnd(jloop,iloop)=iEachWindowStart(jloop,iloop)+itwinObs(4,iloop)-itwinObs(1,iloop)
! check whether syn and obs are available for these indices
if(iEachWindowEnd(jloop,iloop)<iWindowStart) then
print *, "no sufficient data points in syn data for the window", iloop
stop
endif
if(iEachWindowStart(jloop,iloop)>iWindowEnd) then
print *, "no sufficient data points in syn data for the window", iloop
stop
endif
enddo
else
do iloop=1,ntwinObs ! fixed time window
indexInWindow(iloop)=jloop
fEachShift(jloop,iloop)=fMovingWindowStart(iloop)+dt*dble(ntStep)*dble(indexInWindow(iloop)-1)
iEachWindowStart(jloop,iloop)=iMovingWindowStart(iloop)+ntStep*(indexInWindow(iloop)-1)
iEachWindowEnd(jloop,iloop)=iEachWindowStart(jloop,iloop)+itwinObs(4,iloop)-itwinObs(1,iloop)+1
! check whether syn and obs are available for these indices
if(iEachWindowEnd(jloop,iloop)<iWindowStart) then
print *, "no sufficient data points in syn data for the window", iloop
stop
endif
if(iEachWindowStart(jloop,iloop)>iWindowEnd) then
print *, "no sufficient data points in syn data for the window", iloop
stop
endif
enddo
endif
!print *, jloop,indexInWindow(:),iEachWindowStart(jloop,:),iEachWindowEnd(jloop,:)
!print *, fEachShift(jloop,:)
enddo
elseif((calculMode.eq.3).or.(calculMode.eq.4).or.(calculMode.eq.5)) then ! here the syn is normally shorter than the obs
if(iMovingWindowStart(1)+iWindowStart<iWindowStart) then
print *, "no sufficient data points in obs data for the window on the left side"
stop
endif
if(iMovingWindowStart(1)+ntStep*(totalNumberInWindowDimension(1)-1)+iWindowEnd>npData) then
print *, "no sufficient data points in obs data for the window on the right side"
!print *, iMovingWindowStart(1)+ntStep*(totalNumberInWindowDimension(1)-1)+iWindowEnd
!print *, npData
stop
endif
endif
!stop
end subroutine makingIndependentWindow
subroutine pinputDSM(DSMconfFile,outputDir,psvmodel, &
modelname,tlen,rmin_,rmax_,rdelta_,r0min,r0max,r0delta, &
thetamin,thetamax,thetadelta,imin,imax,rsgtswitch,tsgtswitch,synnswitch,SGTinfo)
implicit none
!character(120), parameter :: tmpfile='tmpworkingfile_for_SGTcalcul'
character(200) :: dummy,outputDir,psvmodel,modelname,DSMconfFile,SGTinfo
real(kind(0d0)) :: tlen,rmin_,rmax_,rdelta_,r0min,r0max,r0delta
real(kind(0d0)) :: thetamin,thetamax,thetadelta
integer :: imin,imax,rsgtswitch,tsgtswitch,synnswitch
integer, external :: getpid
character(120) :: tmpfile
!write(tmpfile, "(Z5.5)") getpid()
!tmpfile='tmpworkingfile_for_SGTcalcul'//tmpfile
tmpfile='tmpworkingfile_for_SGTcalcul'
open(unit=2, file=SGTinfo)
open(unit=1, file=tmpfile,status='unknown')
100 continue
read(2,110) dummy
110 format(a120)
if(dummy(1:1).eq.'#') goto 100
if(dummy(1:3).eq.'end') goto 120
write(1,110) dummy
goto 100
120 continue
close(1)
close(2)
open(unit=1,file=tmpfile,status='unknown')
read(1,110) DSMconfFile
read(1,110) outputDir
read(1,110) psvmodel
read(1,110) modelname
outputDir=trim(outputDir)
psvmodel=trim(psvmodel)
modelname=trim(modelname)
modelname=adjustl(modelname)
outputDir=adjustl(outputDir)
psvmodel=adjustl(psvmodel)
read(1,*) tlen
read(1,*) rmin_,rmax_,rdelta_
read(1,*) r0min
r0max=r0min
r0delta=20.d0
read(1,*) thetamin,thetamax,thetadelta
read(1,*) imin,imax
read(1,*) rsgtswitch,tsgtswitch,synnswitch
close(1,status='delete')
end subroutine pinputDSM
subroutine readDSMconf(DSMconfFile,re,ratc,ratl,omegai,maxlmax)
implicit none
!character(120), parameter :: tmpfile='tmpworkingfile_for_DSMconf'
character(120) :: dummy,DSMconfFile
real(kind(0d0)) :: re,ratc,ratl,omegai
integer :: maxlmax
integer, external :: getpid
character(120) :: tmpfile
!write(tmpfile,"(Z5.5)") getpid()
!tmpfile='tmpworkingfile_for_DSMconf'//tmpfile
tmpfile='tmpworkingfile_for_DSMconf'
open(unit=2, file=DSMconfFile, status='old',action='read',position='rewind')
open(unit=1, file=tmpfile,status='unknown')
100 continue
read(2,110) dummy
110 format(a120)
if(dummy(1:1).eq.'#') goto 100