-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaitistuff.py
executable file
·2539 lines (2304 loc) · 99 KB
/
haitistuff.py
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
from math import *
from scipy import *
import scipy
from pylab import *
from matplotlib import *
import numpy.fft as nft
import scipy.optimize as spo
#from matplotlib import pyplot as plt
import pylab as plt
from matplotlib import rc
from matplotlib.patches import Ellipse
import string
import sys
#from matplotlib import *
#from pylab import *
import os
import random
import time
#
# gamma function lives here:
#import scipy.special
from scipy.special import gamma
#from scipy.optimize import leastsq
from matplotlib import axis as aa
from threading import Thread
#
#
import datetime as dtm
import calendar
import operator
import urllib
import MySQLdb
import yodapy as yp
import rbIntervals as rbi
def updateDB(catID=523):
y.updateANSS2SQL(catID)
############################################
def freshHaitiRatios(wLens=[10, 15, 20, 30], bigShock=5.0, haitiCat='hati.cat', doShow=False):
# full set of Haiti rb ratios from a fresh catalog...
# update catalog:
print "update MySQL catalog..."
y.updateANSS2SQL()
print "update local catalog, %s" % haitiCat
getHaitiCatFromSQL(haitiCat)
#
return haitiRatios(wLens, bigShock, haitiCat, doShow)
def haitiRatios(wLens=[10, 15, 20, 30], bigShock=5.2, haitiCat='hati.cat', doShow=False):
# full set of Haiti rb ratios
# plot GR distributions:
rbHaiti=getStandardHaitiRB()
rbHaiti.GRshock(False, fname='images/haitiShock-GRdist.png')
rbHaiti.GRfullcat(False, fname='images/haitiFull-GRdist.png')
rbHaiti=None
# calc ratios for wLens:
print "calc ratios for wLens"
for wLen in wLens:
print "rb ratios for wLen=%d" % wLen
getHaitiRBratio(wLen, bigShock, haitiCat, doShow, dtm.datetime.now())
#
return 0
#########################################
#######################################
def getHaitiCatFromSQL(outFile='haiti.cat', startDate=dtm.datetime(2000, 01, 01), endDate=dtm.datetime.now(), minmag=2.5, lats=[16.5, 20.5], lons=[-74.0, -70.0], catID=523 ):
# haiti catalog is in flux, so let's use the catalog. we could go directly from ANSS if we like, but i think this will be easier.
# note that the default prams are a bit broad, but we seem to get a signal anyway.
#
sqlSelect = "select eventDateTime, lat, lon, mag from Earthquakes where catalogID=%d and eventDateTime between '%s' and '%s' and lat between %f and %f and lon between %f and %f and mag>=%f order by eventDateTime asc" % (catID, str(startDate), str(endDate), lats[0], lats[1], lons[0], lons[1], minmag)
sqlHost = 'localhost'
sqlUser = 'myoder'
sqlPassword = 'yoda'
sqlPort = 3306
sqlDB = 'QuakeData'
myConn = MySQLdb.connect(host=sqlHost, user=sqlUser, passwd=sqlPassword, port=sqlPort, db=sqlDB)
r1=myConn.cursor()
r1.execute(sqlSelect)
#
fout=open(outFile, 'w')
fout.write("# haiti catalog from hatistuff.py\n#prams: outfile, startDate, endDate, minmag, lats, lons, catID\n")
fout.write("#%s\t%s\t%s\t%s\t%s\t%s\t%s\d\n" % (outFile, startDate, endDate, minmag, lats, lons, catID))
# catalog format (tab delimited):
# 2004/09/27 01:53:42.78 35.5277 -120.8433 1.42
catlen=0
for rw in r1:
fout.write("%d/%d/%d\t%d:%d:%d.%d\t%f\t%f\t%f\n" % (rw[0].year, rw[0].month, rw[0].day, rw[0].hour, rw[0].minute, rw[0].second, rw[0].microsecond, rw[1], rw[2], rw[3]))
catlen+=1
fout.close()
r1.close()
myConn.close()
return catlen
def getStandardHaitiRB(hlat=18.46, hlon=-72.75, haitiTheta=-15, ra=1.25, rb=.25, haitiCat='hati.cat'):
#reload(rbi)
rbHaiti=rbi.intervalRecordBreaker(haitiCat, haitiTheta, hlat, hlon, ra, rb)
rbHaiti.setAftershockCatalog(haitiCat, haitiTheta, hlat, hlon, ra, rb, None, dtm.datetime.now(), 0)
return rbHaiti
def getHaitiCatPlot(doShow=False, doSave=True, rbh=None, saveName='images/catalogPlot.png'):
# rbh is a Haiti specific intervalRecordBreaker() object.
# note: this functionality has been integrated into the intervalRecordBreaker() object as obh.xyPlotCatalogs(doShow, doSave, saveName, ellipseCenter=[x,y])
if rbh==None: rbh=getStandardHaitiRB()
fcat=[]
scat=[]
for rw in rbh.shockCat:
scat+=[rw[0:4]]
for rw in rbh.fullCat:
if rw not in scat: fcat+=[rw]
#return [scat, fcat]
plt.figure(0)
plt.clf()
#
ax=plt.gca()
#el = Ellipse((-72.533,18.457), 1.25, .25, 15, facecolor='r', alpha=0.5)
el = Ellipse((-72.75,18.46), 2.0*1.25, 2.0*.25, 15.0, facecolor='b', alpha=0.4)
ax.add_artist(el)
#
plt.plot(map(operator.itemgetter(2), rbh.fullCat), map(operator.itemgetter(1), rbh.fullCat), '+')
plt.plot(map(operator.itemgetter(2), rbh.shockCat), map(operator.itemgetter(1), rbh.shockCat), '.')
#plt.plot(map(operator.itemgetter(2), fcat), map(operator.itemgetter(1), fcat), '+', label='Full Catalog')
#plt.plot(map(operator.itemgetter(2), scat), map(operator.itemgetter(1), scat), '.', label='Aftershock zone')
plt.plot([-72.533], [18.457], 'ro', label='M7.0 epicenter')
plt.legend(loc='upper left', numpoints=1)
if doSave: plt.savefig(saveName)
if doShow: plt.show()
def getHaitiRBratio(wLen=10, bigShock=5.2, haitiCat='haiti.cat', doShow=True, maxDt=None):
# plotting note: #lats=map(operator.itemgetter(1), rbi.shockCat)
# catFname='parkcat.cat', theta=tTheta, clat=tLat, clon=tLon, ra=tA, rb=tB
# haiti epicenter bits (so far, very approximate):
if haitiCat==None: haitiCat='haiti.cat'
hlat=18.46
#hlon=-72.53 # actual epicenter
hlon=-72.75 # approximate median position of events.
haitiTheta=-15.0
ra=1.25
rb=.25
#bigShock=5.7 # magnitude of a big aftershock.
bigShockShocks=[]
bigFullShocks=[]
#
#
reload(rbi)
rbHaiti=rbi.intervalRecordBreaker(haitiCat, haitiTheta, hlat, hlon, ra, rb)
rbHaiti.setAftershockCatalog(haitiCat, haitiTheta, hlat, hlon, ra, rb, None, dtm.datetime.now(), 0)
#
#rbHaiti.GRshock(False, fname='images/haitiShock-GRdist.png')
#rbHaiti.GRfullcat(False, fname='images/haitiFull-GRdist.png')
#
# get lists of bigshocks:
rnum=0
for rw in rbHaiti.shockCat:
if rw[3]>=bigShock: bigShockShocks+=[[rnum] + rw]
rnum+=1
rnum=0
for rw in rbHaiti.fullCat:
if rw[3]>=bigShock: bigFullShocks+=[[rnum] + rw]
rnum+=1
#
#rbHaitiSquare=rbi.intervalRecordBreaker(haitiCat, haitiTheta, hlat, hlon, ra, rb) # a square catalog. we'll probably just impose a square catalog...
# find mainshock(s):
shockMainshockEvNum=0
fullMainshockEvNum=0
maxMag1=rbHaiti.shockCat[0][3]
maxMag2=rbHaiti.fullCat[0][3]
rwnum1=0
rwnum2=0
for rw in rbHaiti.shockCat:
if rw[3]>maxMag1:
maxMag1=rw[3]
shockMainshockEvNum=rwnum1
rwnum1+=1
for rw in rbHaiti.fullCat:
if rw[3]>maxMag2:
maxMag2=rw[3]
fullMainshockEvNum=rwnum2
rwnum2+=1
#
# now, get ratios:
#wLen=10
hratios=rbHaiti.getIntervalRatios(3.5, wLen, rbHaiti.shockCat)
if maxDt!=None:
if maxDt>hratios[-1][1]: hratios+=[[hratios[-1][0]+1, maxDt, hratios[-1][2]]]
fdts=[]
for rw in hratios:
fdts+=[rw[1].toordinal() + float(rw[1].hour)/24 + rw[1].minute/(24*60) + rw[1].second/(24*3600) + rw[1].microsecond/(24*3600000000)]
hratiosBoxy=rbHaiti.getIntervalRatios(3.5,wLen,rbHaiti.fullCat)
if maxDt!=None:
if maxDt>hratiosBoxy[-1][1]: hratiosBoxy+=[[hratiosBoxy[-1][0]+1, maxDt, hratiosBoxy[-1][2]]]
fdtsBoxy=[]
for rw in hratiosBoxy:
fdtsBoxy+=[rw[1].toordinal() + float(rw[1].hour)/24 + rw[1].minute/(24*60) + rw[1].second/(24*3600) + rw[1].microsecond/(24*3600000000)]
#
eventFloatDt=float(dtm.datetime(2010, 1,12).toordinal()) + 21.0/24.0 + 53.0/(24*60) + 10.0/(24*3600)
plt.figure(0)
plt.clf()
plt.semilogy(fdts, map(operator.itemgetter(2), hratios), 'k.-')
#plt.fill_between(fdts, map(operator.itemgetter(2), hratios), y2=1, color='b')
plt.fill([fdts[0]]+ fdts + [fdts[-1], fdts[0]], [1] + yp.getValsAbove(map(operator.itemgetter(2), hratios), 1) + [1, 1], 'b')
plt.fill([fdts[0]]+ fdts + [fdts[-1], fdts[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratios), 1) + [1, 1], 'r')
# note: we don't set the y-log scale in these "fill()" commands. we can do that with axis.set_yscale('log') i think.
# we achieve this by doing semilogy() plots below.
plt.title("Haiti rupture area, time-time, wLen=%d" % wLen)
plt.xlabel('time')
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
#plt.axvline(x=eventFloatDt)
nbigshocks=0
for rw in bigShockShocks:
if nbigshocks==0:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
plt.semilogy([eventFloatDt], [1], 'r^', ms=10)
plt.axvline(x=eventFloatDt, color='r', lw=3, label='mainshock' )
#
#plt.semilogy([y.datetimeToFloat(rbHaiti.shockCat[25][0])], [1], 'o')
#plt.axvline(x=yp.datetimeToFloat(rbHaiti.shockCat[25][0]))
plt.axhline(y=1, color='k')
plt.legend(loc='upper left', numpoints=2)
ax=plt.gca()
fg=plt.gcf()
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
plt.savefig('images/rbHaitiRuptureTimeTime-Wlen%d.png' % wLen)
#
plt.figure(1)
plt.clf()
#plt.semilogy(fdtsBoxy, map(operator.itemgetter(2), hratiosBoxy), '.-')
boxyShocks=[[],[]]
for i in xrange(len(fdtsBoxy)):
if fdtsBoxy[i]>=eventFloatDt:
boxyShocks[0]+=[fdtsBoxy[i]]
boxyShocks[1]+=[hratiosBoxy[i][2]]
#plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsAbove(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'b')
#plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'r')
plt.fill([boxyShocks[0][0]] + boxyShocks[0] + [boxyShocks[0][-1], boxyShocks[0][0]], [1]+ yp.getValsAbove(boxyShocks[1], 1) + [1,1], 'b')
plt.fill([boxyShocks[0][0]] + boxyShocks[0] + [boxyShocks[0][-1], boxyShocks[0][0]], [1]+ yp.getValsBelow(boxyShocks[1], 1) + [1,1], 'r')
#
#print "fdts: %s" % str([fdts[0]] + fdtsBoxy + [fdts[-1], fdts[0]])
#print "vals: %s" % str([1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1])
#plt.semilogy(fdtsBoxy, yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1), '.-')
# mainshock:
#plt.axvline(x=eventFloatDt)
nbigshocks=0
for rw in bigFullShocks:
# print "shock events: %s (%f), %f" % (str(rw[1]), yp.datetimeToFloat(rw[1]), eventFloatDt)
if nbigshocks==0:
if yp.datetimeToFloat(rw[1])>eventFloatDt:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
if yp.datetimeToFloat(rw[1])>eventFloatDt: plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
plt.semilogy([eventFloatDt], [1], 'r^', ms=10)
plt.axvline(x=eventFloatDt, color='r', lw=3, label='mainshock' )
#nbigshokcs=0
#for rw in bigShockShocks:
# if nbigshocks==0:
# plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
# else:
# plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
# nbigshocks+=1
plt.axhline(y=1, color='k')
plt.title("4x4 box around Haiti, time-time Aftershocks, wLen=%d" % wLen)
plt.xlabel('time')
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.legend(loc='upper left', numpoints=2)
ax=plt.gca()
fg=plt.gcf()
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
#plt.semilogy([eventFloatDt], [1], 'r^')
plt.savefig('images/rbHaitiBoxyTimeTimeAftershocks-Wlen%d.png' % wLen)
#
plt.figure(2)
plt.clf()
plt.semilogy(fdtsBoxy, map(operator.itemgetter(2), hratiosBoxy), 'k.-')
plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsAbove(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'b')
plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'r')
#print "fdts: %s" % str([fdts[0]] + fdtsBoxy + [fdts[-1], fdts[0]])
#print "vals: %s" % str([1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1])
#plt.semilogy(fdtsBoxy, yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1), '.-')
#plt.axvline(x=eventFloatDt)
nbigshocks=0
for rw in bigFullShocks:
if nbigshocks==0:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
plt.semilogy([eventFloatDt], [1], 'r^', ms=10)
plt.axvline(x=eventFloatDt, color='r', lw=3, label='mainshock' )
plt.axhline(y=1, color='k')
plt.title("4x4 box around Haiti, time-time, wLen=%d" % wLen)
plt.xlabel('time')
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.legend(loc='upper left', numpoints=2)
ax=plt.gca()
fg=plt.gcf()
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
#plt.semilogy([eventFloatDt], [1], 'r^')
plt.savefig('images/rbHaitiBoxyTimeTime-Wlen%d.png' % wLen)
plt.figure(3)
plt.clf()
plt.axhline(y=1, color='k')
plt.semilogy(map(operator.itemgetter(0), hratiosBoxy), map(operator.itemgetter(2), hratiosBoxy), 'k.-')
X=map(operator.itemgetter(0), hratiosBoxy)
plt.fill([X[0]] + X + [X[-1], X[0]], [1] + yp.getValsAbove(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'b')
plt.fill([X[0]] + X + [X[-1], X[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'r')
#
#plt.axvline(x=fullMainshockEvNum)
nbigshocks=0
for rw in bigFullShocks:
if nbigshocks==0:
plt.axvline(x=rw[0], color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=rw[0], color='g')
#
plt.semilogy([fullMainshockEvNum], [1], 'r^', ms=10)
plt.axvline(x=fullMainshockEvNum, color='r', lw=3, label='mainshock' )
#
plt.title("4x4 box around Haiti, natural-time, wLen=%d" % wLen)
plt.xlabel("Number of Events, n")
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.legend(loc='upper left', numpoints=2)
# don't over-crowd labels...
xtks0=map(operator.itemgetter(0), hratiosBoxy)
xlbls0=map(operator.itemgetter(1), hratiosBoxy)
nskip=len(xtks0)/15 #15 labels seem to fit pretty well.
if nskip<1:nskip=1
xlbls=[]
for i in xrange(len(xlbls0)):
lbl=''
if i%nskip==0: lbl=str(xlbls0[i]) + '(%d)' % i
xlbls+=[lbl]
plt.xticks(xtks0, xlbls)
#ax=plt.gca()
fg=plt.gcf()
#ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
plt.axis('auto')
plt.savefig('images/rbHaitiBoxyNaturalTime-Wlen%d.png' % wLen)
plt.figure(4)
plt.clf()
plt.axhline(y=1, color='k')
plt.semilogy(map(operator.itemgetter(0), hratios), map(operator.itemgetter(2), hratios), 'k.-')
X=map(operator.itemgetter(0), hratios)
plt.fill([X[0]] + X+[X[-1], X[0]], [1] + yp.getValsAbove(map(operator.itemgetter(2), hratios), 1) + [1,1], 'b')
plt.fill([X[0]] + X+[X[-1], X[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratios), 1) + [1,1], 'r')
#plt.semilogy([25], [1], 'o')
#plt.axvline(x=25)
nbigshocks=0
for rw in bigShockShocks:
if nbigshocks==0:
plt.axvline(x=rw[0], color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=rw[0], color='g')
plt.semilogy([shockMainshockEvNum], [1], 'r^', ms=10)
plt.axvline(x=shockMainshockEvNum, color='r', lw=3, label='mainshock')
plt.title("Haiti rupture area, natural-time, wLen=%d" % wLen)
plt.xlabel("Number of Events, n")
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
# don't over-crowd labels...
xtks0=map(operator.itemgetter(0), hratios)
xlbls0=map(operator.itemgetter(1), hratios)
nskip=len(xtks0)/9 #15 labels seem to fit pretty well.
if nskip<1:nskip=1
xlbls=[]
for i in xrange(len(xlbls0)):
lbl=''
if i%nskip==0: lbl=str(xlbls0[i]) + '(%d)' % i
xlbls+=[lbl]
plt.xticks(xtks0, xlbls)
#ax=plt.gca()
fg=plt.gcf()
#ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
plt.legend(loc='upper left', numpoints=2)
plt.savefig('images/rbHaitiRuptureNaturalTime-Wlen%d.png' % wLen)
if doShow: plt.show()
# note: look at the shockCat record-breaking intervals plot; we pretty well forecast the 5.75 aftershock (event 25)
return [rbHaiti, hratios]
##################################################################
# chile stuff:
def getChileCatFromSQL(outFile='chile.cat', startDate=dtm.datetime(1980, 01, 01), endDate=dtm.datetime.now(), minmag=5.0, lats=[-40, -30], lons=[-80.0, -65.0], catID=523 ):
# haiti catalog is in flux, so let's use the catalog. we could go directly from ANSS if we like, but i think this will be easier.
# note that the default prams are a bit broad, but we seem to get a signal anyway.
#
sqlSelect = "select eventDateTime, lat, lon, mag from Earthquakes where catalogID=%d and eventDateTime between '%s' and '%s' and lat between %f and %f and lon between %f and %f and mag>=%f order by eventDateTime asc" % (catID, str(startDate), str(endDate), lats[0], lats[1], lons[0], lons[1], minmag)
sqlHost = 'localhost'
sqlUser = 'myoder'
sqlPassword = 'yoda'
sqlPort = 3306
sqlDB = 'QuakeData'
myConn = MySQLdb.connect(host=sqlHost, user=sqlUser, passwd=sqlPassword, port=sqlPort, db=sqlDB)
r1=myConn.cursor()
r1.execute(sqlSelect)
#
fout=open(outFile, 'w')
fout.write("# chile catalog from hatistuff.py\n#prams: outfile, startDate, endDate, minmag, lats, lons, catID\n")
fout.write("#%s\t%s\t%s\t%s\t%s\t%s\t%s\d\n" % (outFile, startDate, endDate, minmag, lats, lons, catID))
# catalog format (tab delimited):
# 2004/09/27 01:53:42.78 35.5277 -120.8433 1.42
catlen=0
for rw in r1:
fout.write("%d/%d/%d\t%d:%d:%d.%d\t%f\t%f\t%f\n" % (rw[0].year, rw[0].month, rw[0].day, rw[0].hour, rw[0].minute, rw[0].second, rw[0].microsecond, rw[1], rw[2], rw[3]))
catlen+=1
fout.close()
r1.close()
myConn.close()
return catlen
def getStandardchileRB(hlat=-35.846, hlon=-72.719, chileTheta=-55, ra=4.5, rb=3.75, chileCat='chile.cat'):
reload(rbi)
rbchile=rbi.intervalRecordBreaker(chileCat, chileTheta, hlat, hlon, ra, rb)
rbchile.setAftershockCatalog(chileCat, chileTheta, hlat, hlon, ra, rb, None, dtm.datetime.now(), 0)
return rbchile
def freshChileRatios(wLens=[15, 20, 30, 40, 50], bigShock=6.0, haitiCat='chile.cat', doShow=False, minMag=5.0):
# full set of Haiti rb ratios from a fresh catalog...
# update catalog:
print "update MySQL catalog..."
yp.updateANSS2SQL()
print "update local catalog, %s" % haitiCat
getChileCatFromSQL(chileCat)
#
return chileRatios(wLens, bigShock, haitiCat, doShow, minMag)
def chileRatios(wLens=[15, 20, 30, 40, 50], bigShock=6.0, chileCat='chile.cat', doShow=False, minMag=5.0):
# full set of Haiti rb ratios
# plot GR distributions:
rbChile=getStandardchileRB()
rbChile.GRshock(False, fname='images/chileShock-GRdist.png')
rbChile.GRfullcat(False, fname='images/chileFull-GRdist.png')
rbChile=None
# calc ratios for wLens:
print "calc ratios for wLens"
for wLen in wLens:
print "rb ratios for wLen=%d" % wLen
getChileRBratios(wLen, bigShock, chileCat, doShow, dtm.datetime.now(), minMag)
#
return 0
def getChileCatPlot(doShow=False, doSave=True, rbh=None, saveName='images/chileCatalogPlot.png'):
# rbh is a Chile specific (derived from haiti specific) intervalRecordBreaker() object.
# note: this functionality has been integrated into the intervalRecordBreaker() object as obh.xyPlotCatalogs(doShow, doSave, saveName, ellipseCenter=[x,y])
if rbh==None: rbh=getStandardchileRB()
bangDate=dtm.datetime(2010, 2, 27, 6, 34, 14)
fcat=[]
scat=[]
aftershocks=[]
for rw in rbh.shockCat:
scat+=[rw[0:4]]
for rw in rbh.fullCat:
if rw not in scat: fcat+=[rw]
if rw[0]>=bangDate: aftershocks+=[rw]
#print "aftershocks: %d" % len(aftershocks)
#return [scat, fcat]
plt.figure(0)
plt.clf()
#
ax=plt.gca()
tLat=None # 35.9
tLon=None # -120.5
tTheta=None # 40.0 #47? note: tTheta is the angle CCW of the x' (transformed) axis from the x axis.
tA=None # .4 # ellipse axes
tB=None # .15
#el = Ellipse((-72.533,18.457), 1.25, .25, 15, facecolor='r', alpha=0.5)
#el = Ellipse((-72.719,-35.846), 2.0*rbh.tA, 2.0*rbh.tB, rbh.tTheta, facecolor='b', alpha=0.4)
el = Ellipse((rbh.tLon, rbh.tLat), 2.0*rbh.tA, 2.0*rbh.tB, -rbh.tTheta, facecolor='b', alpha=0.3)
ax.add_artist(el)
#
#plt.plot(map(operator.itemgetter(2), rbh.fullCat), map(operator.itemgetter(1), rbh.fullCat), '.')
plt.plot(map(operator.itemgetter(2), fcat), map(operator.itemgetter(1), fcat), 'b.')
plt.plot(map(operator.itemgetter(2), scat), map(operator.itemgetter(1), scat), 'g.')
#plt.plot(map(operator.itemgetter(2), rbh.shockCat), map(operator.itemgetter(1), rbh.shockCat), '.')
plt.plot(map(operator.itemgetter(2), aftershocks), map(operator.itemgetter(1), aftershocks), 'r.')
#plt.plot(map(operator.itemgetter(2), fcat), map(operator.itemgetter(1), fcat), '+', label='Full Catalog')
#plt.plot(map(operator.itemgetter(2), scat), map(operator.itemgetter(1), scat), '.', label='Aftershock zone')
plt.plot([rbh.tLon], [rbh.tLat], 'r*', ms=15, label='M8.8 epicenter')
plt.legend(loc='upper left', numpoints=1)
if doSave: plt.savefig(saveName)
if doShow: plt.show()
return rbh
def getChileRBratios(wLen=25, bigShock=5.2, chileCat='chile.cat', doShow=True, maxDt=None, minMag=5.0):
# plotting note: #lats=map(operator.itemgetter(1), rbi.shockCat)
# catFname='parkcat.cat', theta=tTheta, clat=tLat, clon=tLon, ra=tA, rb=tB
# haiti epicenter bits (so far, very approximate):
if chileCat==None: chileCat='chile.cat'
#bigShock=5.7 # magnitude of a big aftershock.
bigShockShocks=[]
bigFullShocks=[]
#
#
reload(rbi)
rbchile=getStandardchileRB()
hlat=rbchile.tLat
#hlon=-72.53 # actual epicenter
hlon=rbchile.tLon
chileTheta=rbchile.tTheta
ra=rbchile.tA
rb=rbchile.tB
print "prams: %f, %f, %f, %f, %f" % (hlat, hlon, chileTheta, ra, rb)
# hlat=-35.846, hlon=-72.719, chileTheta=-55, ra=4.5, rb=3.75
rbchile.setAftershockCatalog(chileCat, chileTheta, hlat, hlon, ra, rb, None, dtm.datetime.now(), 0)
#
rbchile.GRshock(False, fname='images/chileShock-GRdist.png')
rbchile.GRfullcat(False, fname='images/chileFull-GRdist.png')
#
# get lists of bigshocks:
rnum=0
for rw in rbchile.shockCat:
if rw[3]<minMag: continue
if rw[3]>=bigShock: bigShockShocks+=[[rnum] + rw]
rnum+=1
rnum=0
for rw in rbchile.fullCat:
if rw[3]<minMag: continue
if rw[3]>=bigShock: bigFullShocks+=[[rnum] + rw]
rnum+=1
#
#rbchileSquare=rbi.intervalRecordBreaker(chileCat, chileTheta, hlat, hlon, ra, rb) # a square catalog. we'll probably just impose a square catalog...
# find mainshock(s):
shockMainshockEvNum=0
fullMainshockEvNum=0
maxMag1=rbchile.shockCat[0][3]
maxMag2=rbchile.fullCat[0][3]
rwnum1=0
rwnum2=0
for rw in rbchile.shockCat:
if rw[3]<minMag: continue
if rw[3]>maxMag1:
maxMag1=rw[3]
shockMainshockEvNum=rwnum1
eventFloatDt=yp.datetimeToFloat(rw[0])
rwnum1+=1
#print "event date: %f" % (eventFloatDt)
for rw in rbchile.fullCat:
if rw[3]<minMag: continue
if rw[3]>maxMag2:
maxMag2=rw[3]
fullMainshockEvNum=rwnum2
rwnum2+=1
print "mainshock data: mag=%f/%f, evNum1=%d, evNum2=%d" % (maxMag1, maxMag2, shockMainshockEvNum, fullMainshockEvNum)
#
# now, get ratios:
#wLen=10
hratios=rbchile.getIntervalRatios(minMag, wLen, rbchile.shockCat)
if maxDt!=None:
if maxDt>hratios[-1][1]: hratios+=[[hratios[-1][0]+1, maxDt, hratios[-1][2]]]
fdts=[]
for rw in hratios:
fdts+=[rw[1].toordinal() + float(rw[1].hour)/24 + rw[1].minute/(24*60) + rw[1].second/(24*3600) + rw[1].microsecond/(24*3600000000)]
hratiosBoxy=rbchile.getIntervalRatios(minMag,wLen,rbchile.fullCat)
if maxDt!=None:
if maxDt>hratiosBoxy[-1][1]: hratiosBoxy+=[[hratiosBoxy[-1][0]+1, maxDt, hratiosBoxy[-1][2]]]
fdtsBoxy=[]
for rw in hratiosBoxy:
fdtsBoxy+=[rw[1].toordinal() + float(rw[1].hour)/24 + rw[1].minute/(24*60) + rw[1].second/(24*3600) + rw[1].microsecond/(24*3600000000)]
plt.figure(0)
plt.clf()
#plt.semilogy(fdts, map(operator.itemgetter(2), hratios), 'k.')
#plt.fill_between(fdts, map(operator.itemgetter(2), hratios), y2=1, color='b')
#plt.fill([fdts[0]]+ fdts + [fdts[-1], fdts[0]], [1] + yp.getValsAbove(map(operator.itemgetter(2), hratios), 1) + [1, 1], 'b')
#plt.fill([fdts[0]]+ fdts + [fdts[-1], fdts[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratios), 1) + [1, 1], 'r')
plt.axvline(x=eventFloatDt, color='c', lw=3, label='mainshock' )
plt.fill_between(fdts, scipy.ones(len(fdts),int), map(operator.itemgetter(2), hratios), color='b', where=scipy.array([val>=1 for val in map(operator.itemgetter(2), hratios)]))
plt.fill_between(fdts, scipy.ones(len(fdts),int), map(operator.itemgetter(2), hratios), color='r', where=scipy.array([val<=1 for val in map(operator.itemgetter(2), hratios)]))
# note: we don't set the y-log scale in these "fill()" commands. we can do that with axis.set_yscale('log') i think.
# we achieve this by doing semilogy() plots below.
plt.title("Chile rupture area, time-time, wLen=%d" % wLen)
plt.xlabel('time')
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.axvline(x=eventFloatDt)
nbigshocks=0
for rw in bigShockShocks:
if nbigshocks==0:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
plt.semilogy([eventFloatDt], [1], 'r^', ms=10)
#plt.axvline(x=eventFloatDt, color='r', lw=3, label='mainshock' )
#
#plt.semilogy([y.datetimeToFloat(rbchile.shockCat[25][0])], [1], 'o')
#plt.axvline(x=yp.datetimeToFloat(rbchile.shockCat[25][0]))
plt.axhline(y=1, color='k')
plt.legend(loc='lower right', numpoints=2)
ax=plt.gca()
fg=plt.gcf()
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
ax.set_ylim([.1,10])
fg.autofmt_xdate()
plt.savefig('images/rbchileRuptureTimeTime-Wlen%d.png' % wLen)
#
plt.figure(1)
plt.clf()
#plt.semilogy(fdtsBoxy, map(operator.itemgetter(2), hratiosBoxy), '.-')
boxyShocks=[[],[]]
for i in xrange(len(fdtsBoxy)):
if fdtsBoxy[i]>=eventFloatDt:
boxyShocks[0]+=[fdtsBoxy[i]]
boxyShocks[1]+=[hratiosBoxy[i][2]]
#plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsAbove(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'b')
#plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'r')
#plt.fill([boxyShocks[0][0]] + boxyShocks[0] + [boxyShocks[0][-1], boxyShocks[0][0]], [1]+ yp.getValsAbove(boxyShocks[1], 1) + [1,1], 'b')
#plt.fill([boxyShocks[0][0]] + boxyShocks[0] + [boxyShocks[0][-1], boxyShocks[0][0]], [1]+ yp.getValsBelow(boxyShocks[1], 1) + [1,1], 'r')
plt.fill_between(boxyShocks[0], scipy.ones(len(boxyShocks[0]),int), boxyShocks[1], color='b', where=scipy.array([val>=1 for val in boxyShocks[1]]))
plt.fill_between(boxyShocks[0], scipy.ones(len(boxyShocks[0]),int), boxyShocks[1], color='r', where=scipy.array([val<=1 for val in boxyShocks[1]]))
#plt.bar([boxyShocks[0][0]] + boxyShocks[0] + [boxyShocks[0][-1], boxyShocks[0][0]], [1]+ yp.getValsAbove(boxyShocks[1], 1) + [1,1], width=1, color='b', orientation='vertical')
#plt.bar([boxyShocks[0][0]] + boxyShocks[0] + [boxyShocks[0][-1], boxyShocks[0][0]], [1]+ yp.getValsBelow(boxyShocks[1], 1) + [1,1], width=1, color='r', orientation='vertical')
#
#print "fdts: %s" % str([fdts[0]] + fdtsBoxy + [fdts[-1], fdts[0]])
#print "vals: %s" % str([1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1])
#plt.semilogy(fdtsBoxy, yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1), '.-')
# mainshock:
#plt.axvline(x=eventFloatDt)
plt.axvline(x=eventFloatDt, color='c', lw=3, label='mainshock')
nbigshocks=0
for rw in bigFullShocks:
# print "shock events: %s (%f), %f" % (str(rw[1]), yp.datetimeToFloat(rw[1]), eventFloatDt)
if nbigshocks==0:
if yp.datetimeToFloat(rw[1])>eventFloatDt:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
if yp.datetimeToFloat(rw[1])>eventFloatDt: plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
plt.semilogy([eventFloatDt], [1], 'r^', ms=10)
#plt.axvline(x=eventFloatDt, color='r', lw=3, label='mainshock' )
#nbigshokcs=0
#for rw in bigShockShocks:
# if nbigshocks==0:
# plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
# else:
# plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
# nbigshocks+=1
plt.axhline(y=1, color='k')
plt.title("Box around Chile, time-time Aftershocks, wLen=%d" % wLen)
plt.xlabel('time')
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.legend(loc='lower right', numpoints=2)
ax=plt.gca()
fg=plt.gcf()
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
ax.set_ylim([.1,10])
fg.autofmt_xdate()
#plt.semilogy([eventFloatDt], [1], 'r^')
plt.savefig('images/rbchileBoxyTimeTimeAftershocks-Wlen%d.png' % wLen)
#
plt.figure(2)
plt.clf()
#plt.semilogy(fdtsBoxy, map(operator.itemgetter(2), hratiosBoxy), 'k.')
#plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsAbove(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'b')
#plt.fill([fdtsBoxy[0]] + fdtsBoxy + [fdts[-1], fdtsBoxy[0]], [1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'r')
plt.fill_between(fdtsBoxy, scipy.ones(len(fdtsBoxy),int), map(operator.itemgetter(2), hratiosBoxy), color='b', where=scipy.array([val>=1 for val in map(operator.itemgetter(2), hratiosBoxy)]))
plt.fill_between(fdtsBoxy, scipy.ones(len(fdtsBoxy),int), map(operator.itemgetter(2), hratiosBoxy), color='r', where=scipy.array([val<=1 for val in map(operator.itemgetter(2), hratiosBoxy)]))
#print "fdts: %s" % str([fdts[0]] + fdtsBoxy + [fdts[-1], fdts[0]])
#print "vals: %s" % str([1]+ yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1])
#plt.semilogy(fdtsBoxy, yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1), '.-')
#plt.axvline(x=eventFloatDt)
plt.axvline(x=eventFloatDt, color='c', lw=3, label='mainshock' )
nbigshocks=0
for rw in bigFullShocks:
if nbigshocks==0:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=yp.datetimeToFloat(rw[1]), color='g')
plt.semilogy([eventFloatDt], [1], 'r^', ms=10)
#plt.axvline(x=eventFloatDt, color='r', lw=3, label='mainshock' )
plt.axhline(y=1, color='k')
plt.title("Box around Chile, time-time, wLen=%d" % wLen)
plt.xlabel('time')
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.legend(loc='upper left', numpoints=2)
ax=plt.gca()
fg=plt.gcf()
ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
ax.set_ylim([.1,10])
fg.autofmt_xdate()
#plt.semilogy([eventFloatDt], [1], 'r^')
plt.savefig('images/rbchileBoxyTimeTime-Wlen%d.png' % wLen)
plt.figure(3)
plt.clf()
plt.axhline(y=1, color='k')
#
plt.axvline(x=fullMainshockEvNum, color='c', lw=3, label='mainshock' )
#
#plt.semilogy(map(operator.itemgetter(0), hratiosBoxy), map(operator.itemgetter(2), hratiosBoxy), 'k.')
X=map(operator.itemgetter(0), hratiosBoxy)
#plt.fill([X[0]] + X + [X[-1], X[0]], [1] + yp.getValsAbove(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'b')
#plt.fill([X[0]] + X + [X[-1], X[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratiosBoxy), 1) + [1,1], 'r')
plt.fill_between(X, scipy.ones(len(X),int), map(operator.itemgetter(2), hratiosBoxy), color='b', where=scipy.array([val>=1 for val in map(operator.itemgetter(2), hratiosBoxy)]))
plt.fill_between(X, scipy.ones(len(X),int), map(operator.itemgetter(2), hratiosBoxy), color='r', where=scipy.array([val<=1 for val in map(operator.itemgetter(2), hratiosBoxy)]))
#
#plt.axvline(x=fullMainshockEvNum)
nbigshocks=0
for rw in bigFullShocks:
if nbigshocks==0:
plt.axvline(x=rw[0], color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=rw[0], color='g')
#
plt.semilogy([fullMainshockEvNum], [1], 'r^', ms=10)
#plt.axvline(x=fullMainshockEvNum, color='r', lw=3, label='mainshock' )
#
plt.title("Box around Chile, natural-time, wLen=%d" % wLen)
plt.xlabel("Number of Events, n")
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
plt.legend(loc='upper left', numpoints=2)
# don't over-crowd labels...
xtks0=map(operator.itemgetter(0), hratiosBoxy)
xlbls0=map(operator.itemgetter(1), hratiosBoxy)
nskip=len(xtks0)/15 #15 labels seem to fit pretty well.
if nskip<1:nskip=1
xlbls=[]
for i in xrange(len(xlbls0)):
lbl=''
if i%nskip==0: lbl=str(xlbls0[i]) + '(%d)' % i
xlbls+=[lbl]
plt.xticks(xtks0, xlbls)
ax=plt.gca()
fg=plt.gcf()
ax.set_ylim([.1,10])
#ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
plt.axis('auto')
plt.savefig('images/rbchileBoxyNaturalTime-Wlen%d.png' % wLen)
plt.figure(4)
plt.clf()
plt.axhline(y=1, color='k')
#plt.semilogy(map(operator.itemgetter(0), hratios), map(operator.itemgetter(2), hratios), 'k.')
X=map(operator.itemgetter(0), hratios)
#plt.fill([X[0]] + X+[X[-1], X[0]], [1] + yp.getValsAbove(map(operator.itemgetter(2), hratios), 1) + [1,1], 'b')
#plt.fill([X[0]] + X+[X[-1], X[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratios), 1) + [1,1], 'r')
#plt.bar(X, map(operator.itemgetter(2), hratios), width=1, bottom=1, color='b', align='edge')
#plt.bar([X[0]] + X+[X[-1], X[0]], [1] + yp.getValsBelow(map(operator.itemgetter(2), hratios), 1) + [1,1], width=1, color='r', orientation='vertical', align='edge')
plt.axvline(x=shockMainshockEvNum, color='c', lw=3, label='mainshock')
plt.fill_between(X, scipy.ones(len(X),int), map(operator.itemgetter(2), hratios), color='b', where=scipy.array([val>=1 for val in map(operator.itemgetter(2), hratios)]))
plt.fill_between(X, scipy.ones(len(X),int), map(operator.itemgetter(2), hratios), color='r', where=scipy.array([val<1 for val in map(operator.itemgetter(2), hratios)]))
#plt.semilogy([25], [1], 'o')
#plt.axvline(x=25)
nbigshocks=0
for rw in bigShockShocks:
if nbigshocks==0:
plt.axvline(x=rw[0], color='g', label='m > %f' % bigShock)
nbigshocks+=1
else:
plt.axvline(x=rw[0], color='g')
plt.semilogy([shockMainshockEvNum], [1], 'r^', ms=10)
#plt.axvline(x=shockMainshockEvNum, color='r', lw=3, label='mainshock')
plt.title("Chile rupture area, natural-time, wLen=%d" % wLen)
plt.xlabel("Number of Events, n")
plt.ylabel('$r=N_{rb-long} / N_{rb-short}$')
# don't over-crowd labels...
xtks0=map(operator.itemgetter(0), hratios)
xlbls0=map(operator.itemgetter(1), hratios)
nskip=len(xtks0)/9 #15 labels seem to fit pretty well.
if nskip<1:nskip=1
xlbls=[]
for i in xrange(len(xlbls0)):
lbl=''
if i%nskip==0: lbl=str(xlbls0[i]) + '(%d)' % i
xlbls+=[lbl]
plt.xticks(xtks0, xlbls)
ax=plt.gca()
ax.set_ylim([.1,10])
fg=plt.gcf()
#ax.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d'))
fg.autofmt_xdate()
plt.legend(loc='upper left', numpoints=2)
plt.savefig('images/rbchileRuptureNaturalTime-Wlen%d.png' % wLen)
if doShow: plt.show()
# note: look at the shockCat record-breaking intervals plot; we pretty well forecast the 5.75 aftershock (event 25)
return [rbchile, hratios]
##################################################################
# mexicali 2010:
def getMexicaliCat(mc=1.5):
# hlat=32.258, hlon=-115.287, Theta=50.0, ra=2.0, rb=.5, thisCat='mexicali.cat'
eqm=yp.eqcatalog([])
eqm.rb=rbi.intervalRecordBreaker(None)
xcenter=32.258
ycenter=-115.287
xsize=2.5
ysize=2.5
#startDt=dtm.datetime(2000,1,1)
startDt=dtm.datetime(2000,1,1)
eqm.setCatFromSQL(startDt, dtm.datetime.today(), lats=[xcenter-xsize, xcenter+xsize], lons=[ycenter-ysize, ycenter+ysize], minmag=mc, catalogName='Earthquakes', catalogID=523, ordering='asc')
#
#subcats: start with an xy limited cat:
# addLatLonSubcat(self, subcatname='xysubcat', fullcat=None, lats=[], lons=[], llcols=[1,2]):
# getLatLonSubcat(fullcat, lats, lons, llcols)
eqm.addLatLonSubcat('xysubcat1', eqm.cat, [31.5, 33], [-116.25, -114.5], [1,2])
eqm.addLatLonSubcat('xysubcat2', eqm.cat, [32, 32.5], [-115.5, -115])
# eqm.addTimeRangeCat('postMS', eqm.getcat(0), dtm.datetime(2010, 01, 01), dtm.datetime.today())
#eqm.addEllipCat('PFshock (.8 x .15)', eqm.cat, 40.0, 35.9, -120.5, 0.8, 0.15) # (parkfield reff.)
eqm.addEllipCat('ellip1', eqm.getcat(0), 40.0, 32.258, -115.287, 1.0, .25)
#
return eqm
def getSocalCat(mc=1.5):
# hlat=32.258, hlon=-115.287, Theta=50.0, ra=2.0, rb=.5, thisCat='mexicali.cat'
eqm=yp.eqcatalog([])
eqm.rb=rbi.intervalRecordBreaker(None)
#xcenter=32.258
#ycenter=-115.287
#xsize=2.5
#ysize=2.5
lats=[31.5, 36.5]
lons=[-122.5, -114.5]
#
#startDt=dtm.datetime(2000,1,1)
startDt=dtm.datetime(1995,1,1)
eqm.setCatFromSQL(startDt, dtm.datetime.today(), lats, lons, minmag=mc, catalogName='Earthquakes', catalogID=523, ordering='asc')
#
#subcats: start with an xy limited cat:
# addLatLonSubcat(self, subcatname='xysubcat', fullcat=None, lats=[], lons=[], llcols=[1,2]):
# getLatLonSubcat(fullcat, lats, lons, llcols)
#
# mexicali equakes:
eqm.addLatLonSubcat('xysubcat1', eqm.cat, [31.5, 33], [-116.25, -114.5], [1,2])
eqm.addLatLonSubcat('xysubcat2', eqm.cat, [32, 32.5], [-115.5, -115])
# eqm.addTimeRangeCat('postMS', eqm.getcat(0), dtm.datetime(2010, 01, 01), dtm.datetime.today())
#eqm.addEllipCat('PFshock (.8 x .15)', eqm.cat, 40.0, 35.9, -120.5, 0.8, 0.15) # (parkfield reff.)
eqm.addEllipCat('ellip1', eqm.getcat(0), 40.0, 32.258, -115.287, 1.0, .25)
#
return eqm
def rbHazardMap(catalog=None, catnum=0, mc=2.5, gridsize=.5, phi=[0,0], winlen=128, bigmag=5.0, fignum=0):
# make a hazard map from rb-sequence.
# divide cat. into gridsize size square subcats, do RB over last winlen events in each square, draw on a map.
#
# phi=[xoffset, yoffset]. we'll accomplish this by simply adding phi to the LL x,y values. nominally this changes the area of the catalog a little bit,
# but we don't throw away any data and we sill shift the bin centers.
if catalog==None:
#catalog=getMexicaliCat(mc)
catalog=getSocalCat(mc)
while len(catalog.subcats)>0: catalog.subcats.pop()
avlen=10
#
thiscat0=catalog.getcat(catnum)
mev=catalog.getMainEvent(thiscat0)
thiscat=thiscat0[0:mev[4]-1]
print "mainEvent: %s" % mev
#
llrange=catalog.getLatLonRange(thiscat)
llrange[0]=[llrange[0][0]+phi[0], llrange[0][1]+phi[1]]
deltaLat=abs(float(llrange[1][0])-llrange[0][0])
deltaLon=abs(float(llrange[1][1])-llrange[0][1])
Nlat=1+int(deltaLat/gridsize)
Nlon=1+int(deltaLon/gridsize) #number of cells in row/col
ilat=0
ilon=0 # index counters
# add empty sub-cats:
for i in xrange(Nlat*Nlon):
catalog.subcats+=[[i, []]]
#
#return catalog
lat0=llrange[0][0]
lon0=llrange[0][1]
#imax=len(thiscat)-1
# now, skip through main-catalog, get cat-index for each event and append...
#for icat in xrange(imax):
#print "stats: %d, %d, (%f), %f, %f" % (Nlat, Nlon, gridsize, lat0, lon0)
for rw in thiscat:
#rw=thiscat[imax-icat] # move backwards, from most recent to past.
# rw=thiscat[icat]
#
ilat=(rw[1]-lat0)/gridsize
ilat=int(ilat)
ilon=(rw[2]-lon0)/gridsize
ilon=int(ilon)
scindex=ilat*Nlon + ilon # sub-catalog index
#
#print "lat, lon, ilat, ilon: [%d]: %f, %f, %d, %d (%d, %d, (%f), %f, %f)" % (scindex, rw[1], rw[2], ilat, ilon, Nlat, Nlon, gridsize, lat0, lon0)
#
#print "index: %d of %d/%d (%s)" % (scindex, len(catalog.subcats), imax, rw)
#if len(catalog.subcats[scindex][1])<=(winlen+1): catalog.subcats[scindex][1].insert(0, rw)
#catalog.subcats[scindex][1].insert(0, rw)
catalog.subcats[scindex][1]+=[rw]
#
# now, calc. statistics from each square.
# [index, totalInterval, r_nrb]
gridStats=[]
#return catalog
for ct in catalog.subcats:
if len(ct[1])<=(winlen+avlen): continue
#print ct[0]
#print ct[1][-1][0]
#print ct[1][0][0]
#
thisratios=catalog.rb.getIntervalRatios(mc, winlen, ct[1])
rs=map(operator.itemgetter(-1), thisratios)
meanr=sum(rs[-avlen:])/float(avlen)
#
#gridStats+=[[ct[0], pylab.date2num(ct[1][-1][0])-pylab.date2num(ct[1][0][0]), catalog.rb.getIntervalRatios(mc, winlen, ct[1])[-1][-1]]]
gridStats+=[[ct[0], pylab.date2num(ct[1][-1][0])-pylab.date2num(ct[1][0][0]), meanr]]
#
#plt.figure(5)
reds=[]
blues=[]
print "make grid elements:"
print llrange
print lat0, lon0
#plt.title('as of %s' % str(thiscat[-1][0]))
plt.text(.4, .8, 'as of %s' % str(thiscat[-1][0]))
print 'as of %s' % str(thiscat[-1][0])
cm=catalog.plotCatMap(thiscat, True, False, None, None, 'best', True, 'g,', None, 1)
for rw in gridStats:
#
clat=int(rw[0]/Nlon)*gridsize+lat0
clon=int(rw[0]%Nlon)*gridsize+lon0
#print rw, clat, clon, Nlon, gridsize
#
if rw[2]<=1:
reds+=[[clat, clon, rw[1], rw[2]]]
thispoly=getSquare([clon, clat], gridsize)
rx, ry=cm(x=map(operator.itemgetter(0), thispoly), y=map(operator.itemgetter(1), thispoly))
cm.plot(rx, ry, 'r-', lw=2)
plt.fill(rx, ry, fc='r', ec='r', alpha=.4, lw=2)