-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrosettaCycle.R
624 lines (557 loc) · 22.9 KB
/
rosettaCycle.R
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
# rosetta cycle function
#
exe.rosetta.cycle <- function(eqFile) {
# first things first: check if there are enough MDPs in the data file ----
eqraw <- read.csv(eqFile,
header = TRUE,
sep = "|")
eqraw2 <-
select(eqraw,
ReferenceLongitude,
ReferenceLatitude,
ExpectedIntensity)
eqNMO <- nrow(eqraw) # MDP number
if (eqNMO >= eqIntNumb55Min) {
# loading and parsing header to obtain eq metadata ---------------------
inputID <-
unlist(strsplit(read.csv(
eqFile, header = T, sep = "|"
)$X.EventID[1], split = "/"))[3]
eqheadfile <-
paste (downloadPath, inputID, "_dbmihead.txt", sep = "")
download.file(paste(asmiServUrl, "?eventid=", inputID, "&format=text", sep = ""),
destfile = eqheadfile)
eqheadraw <- read.csv(eqheadfile,
header = F,
sep = "|")
eqName <- inputID
eqID <- inputID
eqDate <- eqheadraw$V2[2]
eqEpiLon <- as.numeric(eqheadraw$V4[2])
eqEpiLat <- as.numeric(eqheadraw$V3[2])
eqDepDef <- as.numeric(eqheadraw$V5[2])
eqMw <- as.numeric(eqheadraw$V11[2]) # Registered/estimanted Mw
eqEpiArea <- eqheadraw$V13[2]
# non-numeric to numeric transformation for MDPs -----------------------
eqraw2$MCS <-
with(eqraw2, ifelse(
ExpectedIntensity == "11-12",
11.5,
ifelse(
ExpectedIntensity == "10-11",
10.5,
ifelse(
ExpectedIntensity == "9-10",
9.5,
ifelse(
ExpectedIntensity == "8-9",
8.5,
ifelse(
ExpectedIntensity == "7-8",
7.5,
ifelse(
ExpectedIntensity == "6-7",
6.5,
ifelse(
ExpectedIntensity == "5-6",
5.5,
ifelse(
ExpectedIntensity == "4-5",
4.5,
ifelse(
ExpectedIntensity == "3-4",
3.5,
ifelse(
ExpectedIntensity == "2-3",
2.5,
ifelse(
ExpectedIntensity == "F",
3.9,
ifelse(
ExpectedIntensity == "SF",
2.9,
ifelse(
ExpectedIntensity == "NF",
1,
ifelse(
ExpectedIntensity == "HF",
5.1,
ifelse(
ExpectedIntensity == "SD",
5.6,
ifelse(
ExpectedIntensity == "D",
6.4,
ifelse(ExpectedIntensity == "HD", 8.6, ExpectedIntensity)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
))
eqraw3 <-
select(eqraw2, ReferenceLongitude, ReferenceLatitude, MCS)
colnames(eqraw3) <- c("Lon", "Lat", "MCS")
# MCS are still recognized as "char"
eqraw3 <- transform(eqraw3, MCS = as.numeric(MCS))
# calculating distances of municipalities from epicenter in km
distances <-
sapply(1:nrow(eqraw2), function(i)
round(distm(eqraw2[i, 1:2], c(
eqEpiLon , eqEpiLat
)) / 1000, digits = 1))
eqraw3$Dist_epi <- distances
# set a variable to enable/disable subsequent calculations
doNotCalc <- FALSE
## Magnitude filter function ----------------------------------------
# Establishing base filter parameters to select earthquakes
eq <- select(eqraw3, MCS, Dist_epi)
eqUnder55 <-
nrow(subset(eq, eq$Dist_epi <= 55)) # number of MDP between epicenter
# and 55 km
if (eqUnder55 > 0) {
## rosetta algorithm calculations ---------------------------------
# renaming header columns to something more practical
names(eq)[1] <- "Int"
names(eq)[2] <- "Dist"
# adding nMDP vs mobile windows histogram plots
eqMDPs <- paste(eqID, "MDPs", sep = "")
# cleaning data file from fake (0) values
eq <- eq[!(eq$Int == 0),]
# creating subset of intensities within ranges of 10 km
# moving away from epicenter
v0_10 <- subset(eq[1], eq[2] >= 0 & eq[2] < 10)
v5_15 <- subset(eq[1], eq[2] >= 5 & eq[2] < 15)
v10_20 <- subset(eq[1], eq[2] >= 10 & eq[2] < 20)
v15_25 <- subset(eq[1], eq[2] >= 15 & eq[2] < 25)
v20_30 <- subset(eq[1], eq[2] >= 20 & eq[2] < 30)
v25_35 <- subset(eq[1], eq[2] >= 25 & eq[2] < 35)
v30_40 <- subset(eq[1], eq[2] >= 30 & eq[2] < 40)
v35_45 <- subset(eq[1], eq[2] >= 35 & eq[2] < 45)
v40_50 <- subset(eq[1], eq[2] >= 40 & eq[2] < 50)
v45_55 <- subset(eq[1], eq[2] >= 45 & eq[2] < 55)
# create dataframe with mean distance and MDP number to plot in histogram
eq_MDPs_number <- cbind.data.frame(
AvgDist = seq(5, 50, by = 5),
nMDP = c(
nrow(v0_10),
nrow(v5_15),
nrow(v10_20),
nrow(v15_25),
nrow(v20_30),
nrow(v25_35),
nrow(v30_40),
nrow(v35_45),
nrow(v40_50),
nrow(v45_55)
)
)
# calculating averages for distance intervals
eq_avgs <- cbind.data.frame(
Dist = seq(5, 50, by = 5),
Int = c(
mean(v0_10$Int),
mean(v5_15$Int),
mean(v10_20$Int),
mean(v15_25$Int),
mean(v20_30$Int),
mean(v25_35$Int),
mean(v30_40$Int),
mean(v35_45$Int),
mean(v40_50$Int),
mean(v45_55$Int)
)
)
# Consider only earthquakes with minimum mobile avgs and minimum avg
# between 5 and 15 km from epicenter as established
mobAvgNumb <- nrow(subset(eq_avgs, !is.nan(eq_avgs$Int)))
if (!is.na(mean(v0_10$Int)) &&
mean(v0_10$Int) > meanv0_10IntMin &&
mobAvgNumb >= mobAvgNumbMin) {
eq_IntNumbTot <- length(eq[[1]]) # MDPs total
eq_IntNumb55 <-
length(subset(eq[1], eq[2] < 55)[[1]]) # MDPs within 55 km
# calculating earthquake depth using Rosetta algorithm
cAng <- lm(eq_avgs$Int ~ eq_avgs$Dist)$coefficient[2]
slope <- abs(as.numeric(cAng))
eq_Dnext <- exp((slope - 0.087) / (-0.018))
# Calculating intercept
slopeInt <-
summary(lm(eq_avgs$Int ~ eq_avgs$Dist))$coefficient[1]
# calculating intercept Mw
MwI <- 0.18 * log(eq_Dnext) + 0.56 * slopeInt + 1.44
# set to NA parameters that might not be created if the following
# "if" statement will not be performed (MwI < 6.75)
slopeIntCorr <- NA
MwICorr <- NA
Rwc <- NA
## W & C recalculation for MwI >= 6.75 ---------------------------
# If MwI is >= of 6.75 re-calculate everything taking into account
# fault radius as for Wells and Coppersmith (1994)
if (MwI >= 6.75) {
# source R script for fault parameters calculation through W&C1994
source("wcCalcFun.R")
wcCalc(MwI)
load("faultMetaWC.RData")
Rwc <- round(faultRadius, digits = 0)
### recalculate earthquake parameters ---------------------------
startRange <- Rwc
endRange <- Rwc + 10
# create vector for number of MDPs to be used to create histograms of
# distance vs number of MDPs
number_of_mdps <- c()
# first two ranges (fixed values)
vRwc <- subset(eq, eq$Dist >= 0 & eq$Dist <= Rwc)
vRwcplus <-
subset(eq, eq$Dist > startRange & eq$Dist <= endRange)
# calculates the average intensity between epicenter and Rwc and add the first
# row to the dataframe
eq_avgs <-
data.frame(Rwc / 2, round(mean(vRwc$Int), digits = 1))
# add number of MDPs to vector
number_of_mdps <- append(number_of_mdps, nrow(vRwc))
# gives a name to columns
names(eq_avgs) <- c("Dist", "Int")
# calculates the average intensity between Rwc and Rwc+10km (2nd window) and
# add it to the dataframe
eq_avgs[nrow(eq_avgs) + 1, ] = c((Rwc + 5),
round(mean(vRwcplus$Int),
digits = 1))
number_of_mdps <- append(number_of_mdps, nrow(vRwcplus))
# move forward
startRange <- startRange + 5
endRange <- startRange + 10
# subsetting remnant eq intensities following above directions; calculating
# average intensity for every subset and storing results in the dataframe
while (endRange <= 55) {
nam <- paste("v", startRange, endRange, sep = "")
assign(nam, subset(eq, eq[2] > startRange &
eq[2] <= endRange))
#transform nam character in variable name
namX <- eval(parse(text = nam))
# add numbero of MDPs to vector to plot histograms
number_of_mdps <- append(nrow(namX), number_of_mdps)
# insert mean in eq_avgs dataframe
eq_avgs[nrow(eq_avgs) + 1, ] = c(startRange + 5,
round(mean(namX$Int), digits = 1))
# move forward
startRange <- startRange + 5
endRange <- startRange + 10
}
eq_MDPs_number <-
cbind.data.frame(eq_avgs$Dist, number_of_mdps)
names(eq_MDPs_number)[1] <- "AvgDist"
names(eq_MDPs_number)[2] <- "nMDP"
# plot of histograms
if (histYES) {
hist <- ggplot(eq_MDPs_number,
aes(x = AvgDist, y = nMDP)) +
geom_bar(stat = "identity",
color = "blue",
fill = "transparent") +
scale_x_continuous(
name = "Epicentral distance (km)",
limits = c(0, 55),
breaks = round(eq_MDPs_number$AvgDist, digits = 0)
) +
scale_y_continuous(name = "Number of MDPs") +
labs(title = paste(
eqID,
" - ",
eqEpiArea,
"\n# MDP vs epicentral distance",
sep = ""
)) +
geom_text(aes(y = nMDP - nMDP * 10 / 100, label = nMDP))
ggsave(
filename = paste(outdir, "/", eqID, "WC_hist.png", sep = ""),
hist,
device = "png"
)
}
# Consider only earthquakes with minimum mobile avgs and minimum avg
# between 5 and 15 km from epicenter as established
mobAvgNumb <- nrow(subset(eq_avgs, !is.nan(eq_avgs$Int)))
if (!is.na(eq_avgs$Int[1]) &&
eq_avgs$Int[1] > meanv0_10IntMin &&
mobAvgNumb >= mobAvgNumbMin) {
eq_IntNumbTot <- nrow(eq) # MDPs total
eq_IntNumb55 <-
nrow(subset(eq, eq$Dist <= 55)) # MDPs within 55 km
# calculating earthquake depth using Rosetta algorithm
cAng <- lm(eq_avgs$Int ~ eq_avgs$Dist)$coefficient[2]
slope <- abs(as.numeric(cAng))
eq_Dnext <- exp((slope - 0.087) / (-0.018))
# If eq_Dnext is shallower than 5 km it will be set to 5 km
if (eq_Dnext < 5) {
eq_Dnext <- 5
}
# if eq_Dnext is deeper than 73 km it will be set to 73 km
if (eq_Dnext > 73) {
eq_Dnext <- 73
}
# Calculating intercept
slopeInt <-
summary(lm(eq_avgs$Int ~ eq_avgs$Dist))$coefficient[1]
# Intercept correction due to W&C fault radius
slopeIntCorr <- slopeInt - slope * Rwc
# (re-)calculating intercept Mw and corrected one
MwI <- 0.18 * log(eq_Dnext) + 0.56 * slopeInt + 1.44
MwICorr <-
0.18 * log(eq_Dnext) + 0.56 * slopeIntCorr + 1.44
} else {
doNotCalc <- TRUE
}
}
if (!doNotCalc) {
# calculating stdError
stdError <-
summary(lm(eq_avgs$Int ~ eq_avgs$Dist))$coefficients[4]
roundStdError <- round(stdError, digits = 3)
# considerate earthquake with established conditions only
if (coef(lm(eq_avgs$Int ~ eq_avgs$Dist))[[2]] < 0 &&
eq_IntNumb55 >= eqIntNumb55Min &&
summary(lm(eq_avgs$Int ~ eq_avgs$Dist))$coefficients[4] <= maxStdError) {
## analysis on macroseismic field homogeneity on the basis of ----
# intensities distribution along azimuth section (azEqInt algorithm)
# create two dataframes with MDPs and their azimuthal direction
# with respect to epicenter
dut <- 1
outPut <-
data.frame(
intensity = integer(),
azimuth = double(),
distance = double()
)
outPutDeg <-
data.frame(
intensity = integer(),
azimuth = double(),
distance = double()
)
while (dut <= nrow(eqraw3)) {
outPut[nrow(outPut) + 1,] = c(eqraw3$MCS[dut],
deg2rad(bearing(
c(eqEpiLon, eqEpiLat),
c(eqraw3$Lon[dut], eqraw3$Lat[dut])
)),
eqraw3$Dist_epi[dut])
outPutDeg[nrow(outPutDeg) + 1,] = c(eqraw3$MCS[dut],
bearing(
c(eqEpiLon, eqEpiLat),
c(eqraw3$Lon[dut], eqraw3$Lat[dut])
),
eqraw3$Dist_epi[dut])
dut <- dut + 1
}
# distinguish MDPs in relation to their azimuth
outPutDeg3 <- subset(outPutDeg, intensity <= 3)
outPutDeg5 <-
subset(outPutDeg, intensity > 3 & intensity <= 5)
outPutDeg7 <-
subset(outPutDeg, intensity > 5 & intensity <= 7)
outPutDeg9 <-
subset(outPutDeg, intensity > 7 & intensity <= 9)
outPutDeg11 <- subset(outPutDeg, intensity > 9)
outPutDegSel <-
subset(outPutDeg, distance >= 10 & distance <= 55)
outPutDegSel3 <- subset(outPutDegSel, intensity <= 3)
outPutDegSel5 <-
subset(outPutDegSel, intensity > 3 & intensity <= 5)
outPutDegSel7 <-
subset(outPutDegSel, intensity > 5 & intensity <= 7)
outPutDegSel9 <-
subset(outPutDegSel, intensity > 7 & intensity <= 9)
outPutDegSel11 <- subset(outPutDegSel, intensity > 9)
# evaluate if there is at least 1 intensity per interval
# of 10 degrees 18 times (i.e. the intensities must be distributed
# for a minimum of 180 azimuth degrees wherever located in the windrose)
outCount <-
data.frame(
azimuthInterval = character(),
azimuthCount = integer(),
stringsAsFactors = FALSE
)
det <- 0
while (det <= 170) {
outCount[nrow(outCount) + 1, ] = list(paste(det, ">", det + 10, sep = ""), length(
which(
outPutDegSel$azimuth >= det & outPutDegSel$azimuth < (det + 10)
)
))
det <- det + 10
}
detinv <- 0
while (detinv >= -170) {
outCount[nrow(outCount) + 1, ] = list(paste(detinv, "<", detinv - 10, sep = ""),
length(
which(
outPutDegSel$azimuth < detinv &
outPutDegSel$azimuth >= (detinv - 10)
)
))
detinv <- detinv - 10
}
resultAz <- length(which(outCount$azimuthCount > 0))
# plotting ---------------------------------------------------
# plot histogram of number of MDPs vs distance from epicenter
if (histYES) {
hist <- ggplot(eq_MDPs_number,
aes(x = AvgDist, y = nMDP)) +
geom_bar(stat = "identity",
color = "blue",
fill = "transparent") +
scale_x_continuous(
name = "Epicentral distance (kms)",
limits = c(0, 55),
breaks = seq(5, 50, by = 5)
) +
scale_y_continuous(name = "Number of MDP") +
labs(title = paste(
eqID,
" - ",
eqEpiArea,
"\n# MDP vs epicentral distance",
sep = ""
)) +
geom_text(aes(y = nMDP - nMDP * 10 / 100, label = nMDP))
ggsave(
filename = paste(outdir, "/", eqID, "_hist.png", sep = ""),
hist,
device = "png"
)
}
if (resultAz >= resultAzMin) {
if (liPlotto) {
pdfOUT0_50 <- ggplot(eq_avgs, aes(Dist, Int)) +
ggtitle(paste("DBMI_", eqID, "|", eqDate, "-", eqEpiArea)) +
geom_point(aes(y = Int, colour = "Intensities")) +
scale_x_continuous(
name = "Epicentral distance (km)",
limits = c(0, 50),
breaks = seq(0, 50, by = 5)
) +
theme(legend.position = "none") +
scale_y_continuous(
name = "Avg. intensities (MCS)",
limits = c(-10, 51),
breaks = seq(1, max(eq_avgs$Int, na.rm = TRUE) + 1)
) +
coord_cartesian(xlim = c(0, 50),
ylim = c(1, max(eq_avgs$Int, na.rm = TRUE) + 1)) +
geom_smooth(
method = "lm",
fullrange = TRUE,
se = TRUE,
color = "blue"
) +
annotate(
"rect",
xmin = 0,
xmax = 33,
ymin = 1,
ymax = 2,
alpha = 0.6,
color = "grey",
fill = "white"
) +
annotate(
geom = "text",
x = 1,
y = 1.6,
hjust = 0,
size = 3.5,
label = paste0(
"\nSteepness: ",
abs(round(cAng, digits = 3)),
"\nExpected depth: ",
round(eq_Dnext, digits = 1),
"\nSteepness std. error: ",
roundStdError,
"\nMDPs within 55 km: ",
eq_IntNumb55
)
) +
annotate(
geom = "text",
x = 16,
y = 1.6,
hjust = 0,
size = 3.5,
label = paste0(
"\nIntercept value: ",
round(slopeInt, digits = 2),
"\nCorrected Intercept value: ",
round(slopeIntCorr, digits = 2),
"\nMw y-intercept (this work): ",
round(MwI, digits = 2),
"\nCorrected Mw y-intercept (this work): ",
round(MwICorr, digits = 2)
)
)
}
if (liSalvo) {
ggsave(
filename = paste(eqID,
"-0_50",
".pdf",
sep = ""),
plot = pdfOUT0_50,
device = "pdf",
path = outdir
)
}
# exporting eq parameters to ouput csv file -------------------
newline <-
list(
eqID,
eqEpiArea,
eqDate,
eqMw,
eqDepDef,
eq_IntNumbTot,
eq_IntNumb55,
mobAvgNumb,
eqEpiLon,
eqEpiLat,
round(coef(
lm(eq_avgs$Int ~ eq_avgs$Dist)
)[[2]], digits = 3),
round(eq_Dnext, digits = 2),
round(summary(
lm(eq_avgs$Int ~ eq_avgs$Dist)
)$coefficients[4], digits = 4),
round(slopeInt, digits = 3),
round(slopeIntCorr, digits = 3),
round(MwI, digits = 2),
round(MwICorr, digits = 2),
resultAz,
Rwc,
paste("file://", outdir, "/", eqID, "-0_50.pdf", sep = "")
)
write.table(
newline,
file = outFileName,
append = TRUE,
sep = ",",
quote = FALSE,
col.names = FALSE,
row.names = FALSE
)
}
}
}
}
}
}
}