#!/usr/bin/env python
"""
Copyright 2010-2019 University Of Southern California

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Python version of Ronnie Kamai Matlab scripts to generate box
plots of the GMPE comparisons. Added pre-computed NGA-West 1 and 2
data and pass ranges into this script. No longer uses Matlab and no
longer needs to mine gmpe data files from the realizations.

NGA-West 1 and 2 comparison assumes data is calculated at Vs30=863m/s
"""
from __future__ import division, print_function

# Import Python modules
import os
import sys
import glob
import optparse
import numpy as np
import matplotlib as mpl
if mpl.get_backend() != 'agg':
    mpl.use('Agg') # Disables use of Tk/X11
import matplotlib.pyplot as plt
import pylab

# Import plot config file
import plot_config

# --------------------------------------------------------------------------
# Some constants
# --------------------------------------------------------------------------
CODEBASES = ["gp", "ucsb", "sdsu", "exsim", "csm",
             "irikura_recipe_m1", "irikura_recipe_m2", "song"]
ALL_PERIODS = [0.010, 0.011, 0.012, 0.013, 0.015, 0.017, 0.020, 0.022, 0.025,
               0.029, 0.032, 0.035, 0.040, 0.045, 0.050, 0.055, 0.060, 0.065,
               0.075, 0.085, 0.100, 0.110, 0.120, 0.130, 0.150, 0.170, 0.200,
               0.220, 0.240, 0.260, 0.280, 0.300, 0.350, 0.400, 0.450, 0.500,
               0.550, 0.600, 0.650, 0.750, 0.850, 1.000, 1.100, 1.200, 1.300,
               1.500, 1.700, 2.000, 2.200, 2.400, 2.600, 2.800, 3.000, 3.500,
               4.000, 4.400, 5.000, 5.500, 6.000, 6.500, 7.500, 8.500, 10.000]

PERIODS = [0.010, 0.020, 0.029, 0.04, 0.05, 0.06, 0.075, 0.1,
           0.15, 0.2, 0.26, 0.3, 0.4, 0.5, 0.6, 0.75, 1, 1.5,
           2, 3, 4, 5, 6, 7.5, 10]

NGA1 = {}

NGA1["Tacpt"] = [0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.15, 0.2, 0.25,
                 0.3, 0.4, 0.5, 0.75, 1, 1.5, 2, 3]

NGA1["Tave"] = [0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.15, 0.2, 0.25,
                0.3, 0.4, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 10]

NGA1["Tnga"] = [0.0100, 0.0200, 0.0290, 0.0400,
                0.0500, 0.0600, 0.0750, 0.1000,
                0.1500, 0.2000, 0.2600, 0.3000,
                0.4000, 0.5000, 0.6000, 0.7500,
                1.0000, 1.5000, 2.0000, 3.0000,
                4.0000, 5.0000, 6.0000, 7.5000,
                10.0000]

NGA1["M55rvLo20"] = [0.0436221, 0.04462166, 0.04940387, 0.06279549,
                     0.08096491, 0.09132741, 0.09393127, 0.08478165,
                     0.07334698, 0.06364087, 0.04750753, 0.03639194,
                     0.02063671, 0.01321022, 0.00649865, 0.00383689,
                     0.00178624]
NGA1["M55rvHi20"] = [0.11322152, 0.1158159, 0.12822815, 0.16298622,
                     0.21014509, 0.23704106, 0.2437994, 0.22005147,
                     0.19037269, 0.16518042, 0.12330618, 0.09445558,
                     0.05356274, 0.03428723, 0.0168673, 0.00995868,
                     0.0046362]
NGA1["M55rvLo50"] = [0.01214008, 0.01237123, 0.01356571, 0.01702426,
                     0.02166736, 0.02447316, 0.02579139, 0.0237992,
                     0.02115549, 0.01878241, 0.01443256, 0.01122829,
                     0.00657776, 0.00422454, 0.00213321, 0.00126461,
                     0.0005884]
NGA1["M55rvHi50"] = [0.03233765, 0.03295336, 0.03613512, 0.04534767,
                     0.05771555, 0.06518939, 0.06870077, 0.06339414,
                     0.05635208, 0.05003088, 0.03844414, 0.02990891,
                     0.01752124, 0.01125295, 0.00568225, 0.00336855,
                     0.00156734]
NGA1["M62ssLo20"] = [0.06728196, 0.06841946, 0.072648554, 0.085038587,
                     0.109542884, 0.12853256, 0.149399064, 0.147372083,
                     0.133849228, 0.119988992, 0.097184751, 0.077936768,
                     0.049245661, 0.035129951, 0.020601618, 0.013091926,
                     0.006792951]
NGA1["M62ssHi20"] = [0.14491499, 0.14736499, 0.156473808, 0.183160034,
                     0.235938518, 0.276839361, 0.3217826, 0.317416794,
                     0.288290646, 0.258437829, 0.209321002, 0.167863809,
                     0.106067577, 0.075664509, 0.044372715, 0.028197994,
                     0.014630972]
NGA1["M62ssLo50"] = [0.02413404, 0.02448662, 0.02567124, 0.02928488,
                     0.03713192, 0.04312212, 0.05143032, 0.05321518,
                     0.05005904, 0.04628924, 0.0382897, 0.0312259,
                     0.02031178, 0.01469734, 0.00880718, 0.0056608,
                     0.00293166]
NGA1["M62ssHi50"] = [0.0553896, 0.0561988, 0.0589176, 0.0672112,
                     0.0852208, 0.0989688, 0.1180368, 0.1221332,
                     0.1148896, 0.1062376, 0.087878, 0.071666,
                     0.0466172, 0.0337316, 0.0202132, 0.012992,
                     0.0067284]
NGA1["M66ssLo20"] = [0.0790686, 0.0802334, 0.084877, 0.0980707,
                     0.1263652, 0.1487317, 0.1757457, 0.1798329,
                     0.1671254, 0.1525498, 0.1290159, 0.1065766,
                     0.0711464, 0.0529425, 0.0329342, 0.0220168,
                     0.0122733]
NGA1["M66ssHi20"] = [0.1946304, 0.1974976, 0.208928, 0.2414048,
                     0.3110528, 0.3661088, 0.4326048, 0.4426656,
                     0.4113856, 0.3755072, 0.3175776, 0.2623424,
                     0.1751296, 0.13032, 0.0810688, 0.0541952,
                     0.0302112]
NGA1["M66ssLo50"] = [0.0297132, 0.0301008, 0.0314532, 0.0354804,
                     0.04479, 0.0520152, 0.0627456, 0.0667836,
                     0.0639756, 0.0600852, 0.0519228, 0.0435852,
                     0.0299028, 0.0225792, 0.0143736, 0.009738,
                     0.0054036]
NGA1["M66ssHi50"] = [0.08121608, 0.08227552, 0.08597208, 0.09697976,
                     0.122426, 0.14217488, 0.17150464, 0.18254184,
                     0.17486664, 0.16423288, 0.14192232, 0.11913288,
                     0.08173432, 0.06171648, 0.03928784, 0.0266172,
                     0.01476984]
NGA1["M66rvLo20"] = [0.0790066, 0.08016352, 0.08480732, 0.09775168,
                     0.12570004, 0.1483164, 0.17609736, 0.18121484,
                     0.16972872, 0.15629456, 0.13195708, 0.1094796,
                     0.07360516, 0.05386436, 0.03191636, 0.0208382,
                     0.01131996]
NGA1["M66rvHi20"] = [0.2115338, 0.21463136, 0.22706476, 0.26172224,
                     0.33655172, 0.3971052, 0.47148648, 0.48518812,
                     0.45443496, 0.41846608, 0.35330444, 0.2931228,
                     0.19707188, 0.14421748, 0.08545348, 0.0557926,
                     0.03030828]
NGA1["M66rvLo50"] = [0.03178267, 0.03217359, 0.033642323, 0.037721806,
                     0.047259278, 0.054916372, 0.065808374, 0.070046763,
                     0.067213816, 0.063205181, 0.053938841, 0.045159798,
                     0.030807022, 0.022667605, 0.013586382, 0.008941526,
                     0.004847463]
NGA1["M66rvHi50"] = [0.087128354, 0.088200014, 0.092226369, 0.103409777,
                     0.129555606, 0.150546607, 0.180405716, 0.192024747,
                     0.184258565, 0.173269374, 0.147866822, 0.123800136,
                     0.084453733, 0.062140504, 0.037245426, 0.024512114,
                     0.013288734]
NGA1["number_of_models"] = 4
NGA1["lines"] = {}

# AS08
NGA1["lines"][0] = {}
NGA1["lines"][0]['M55rv20'] = [0.0728, 0.0739, 0.0774, 0.0844,
                               0.0925, 0.1028, 0.1168, 0.1403,
                               0.1669, 0.1629, 0.1454, 0.1304,
                               0.1004, 0.0760, 0.0583, 0.0422,
                               0.0268, 0.0138, 0.0080, 0.0036,
                               0.0020, 0.0013, 0.0009, 0.0006,
                               0.0003]
NGA1["lines"][0]['M55rv50'] = [0.0228, 0.0230, 0.0236, 0.0252,
                               0.0272, 0.0299, 0.0335, 0.0401,
                               0.0493, 0.0510, 0.0478, 0.0436,
                               0.0345, 0.0266, 0.0206, 0.0151,
                               0.0097, 0.0050, 0.0029, 0.0013,
                               0.0007, 0.0005, 0.0003, 0.0002,
                               0.0001]
NGA1["lines"][0]['M62ss20'] = [0.1045, 0.1061, 0.1110, 0.1183,
                               0.1274, 0.1395, 0.1559, 0.1857,
                               0.2251, 0.2288, 0.2147, 0.1980,
                               0.1610, 0.1284, 0.1034, 0.0793,
                               0.0546, 0.0319, 0.0201, 0.0103,
                               0.0062, 0.0040, 0.0028, 0.0018,
                               0.0010]
NGA1["lines"][0]['M62ss50'] = [0.0384, 0.0386, 0.0397, 0.0414,
                               0.0439, 0.0475, 0.0524, 0.0622,
                               0.0779, 0.0839, 0.0827, 0.0775,
                               0.0647, 0.0526, 0.0428, 0.0333,
                               0.0231, 0.0136, 0.0086, 0.0044,
                               0.0027, 0.0017, 0.0012, 0.0008,
                               0.0004]
NGA1["lines"][0]['M66ss20'] = [0.0977, 0.0992, 0.1039, 0.1097,
                               0.1173, 0.1277, 0.1417, 0.1680,
                               0.2065, 0.2149, 0.2074, 0.1942,
                               0.1658, 0.1401, 0.1183, 0.0962,
                               0.0714, 0.0467, 0.0318, 0.0182,
                               0.0119, 0.0086, 0.0061, 0.0040,
                               0.0022]
NGA1["lines"][0]['M66ss50'] = [0.0394, 0.0397, 0.0408, 0.0422,
                               0.0443, 0.0477, 0.0522, 0.0617,
                               0.0785, 0.0865, 0.0878, 0.0835,
                               0.0732, 0.0630, 0.0538, 0.0443,
                               0.0332, 0.0219, 0.0150, 0.0086,
                               0.0056, 0.0041, 0.0029, 0.0019,
                               0.0011]
NGA1["lines"][0]['M66rv20'] = [0.1281, 0.1301, 0.1361, 0.1434,
                               0.1531, 0.1666, 0.1848, 0.2193,
                               0.2757, 0.2906, 0.2838, 0.2674,
                               0.2273, 0.1885, 0.1560, 0.1238,
                               0.0885, 0.0549, 0.0361, 0.0195,
                               0.0123, 0.0087, 0.0061, 0.0040,
                               0.0023]
NGA1["lines"][0]['M66rv50'] = [0.0516, 0.0520, 0.0534, 0.0551,
                               0.0579, 0.0623, 0.0682, 0.0806,
                               0.1046, 0.1169, 0.1199, 0.1148,
                               0.1002, 0.0847, 0.0708, 0.0570,
                               0.0412, 0.0257, 0.0170, 0.0092,
                               0.0058, 0.0041, 0.0029, 0.0019,
                               0.0011]

# BA08
NGA1["lines"][1] = {}
NGA1["lines"][1]['M55rv20'] = [0.0568, 0.0591, 0.0633, 0.0714,
                               0.0781, 0.0900, 0.1072, 0.1249,
                               0.1396, 0.1251, 0.1026, 0.0926,
                               0.0705, 0.0527, 0.0413, 0.0306,
                               0.0216, 0.0124, 0.0074, 0.0035,
                               0.0018, 0.0010, 0.0007, 0.0005,
                               0.0002]
NGA1["lines"][1]['M55rv50'] = [0.0239, 0.0249, 0.0262, 0.0291,
                               0.0315, 0.0362, 0.0429, 0.0500,
                               0.0565, 0.0561, 0.0479, 0.0444,
                               0.0330, 0.0246, 0.0194, 0.0144,
                               0.0099, 0.0057, 0.0035, 0.0017,
                               0.0009, 0.0006, 0.0004, 0.0003,
                               0.0001]
NGA1["lines"][1]['M62ss20'] = [0.0996, 0.1029, 0.1085, 0.1197,
                               0.1286, 0.1459, 0.1702, 0.1956,
                               0.2296, 0.2291, 0.1971, 0.1818,
                               0.1455, 0.1130, 0.0918, 0.0712,
                               0.0532, 0.0340, 0.0224, 0.0121,
                               0.0072, 0.0046, 0.0034, 0.0023,
                               0.0012]
NGA1["lines"][1]['M62ss50'] = [0.0458, 0.0473, 0.0493, 0.0537,
                               0.0572, 0.0644, 0.0745, 0.0851,
                               0.1001, 0.1067, 0.0946, 0.0891,
                               0.0708, 0.0555, 0.0454, 0.0356,
                               0.0264, 0.0170, 0.0113, 0.0062,
                               0.0038, 0.0026, 0.0020, 0.0014,
                               0.0008]
NGA1["lines"][1]['M66ss20'] = [0.1312, 0.1343, 0.1394, 0.1506,
                               0.1595, 0.1787, 0.2054, 0.2353,
                               0.2856, 0.3052, 0.2681, 0.2488,
                               0.2122, 0.1675, 0.1376, 0.1082,
                               0.0821, 0.0539, 0.0367, 0.0203,
                               0.0135, 0.0091, 0.0069, 0.0049,
                               0.0026]
NGA1["lines"][1]['M66ss50'] = [0.0637, 0.0653, 0.0671, 0.0716,
                               0.0752, 0.0836, 0.0952, 0.1077,
                               0.1305, 0.1458, 0.1313, 0.1241,
                               0.1060, 0.0850, 0.0705, 0.0561,
                               0.0428, 0.0282, 0.0194, 0.0107,
                               0.0073, 0.0051, 0.0040, 0.0029,
                               0.0017]
NGA1["lines"][1]['M66rv20'] = [0.1323, 0.1354, 0.1406, 0.1519,
                               0.1608, 0.1802, 0.2072, 0.2373,
                               0.2881, 0.3078, 0.2704, 0.2509,
                               0.2140, 0.1689, 0.1388, 0.1091,
                               0.0828, 0.0543, 0.0371, 0.0204,
                               0.0136, 0.0091, 0.0069, 0.0049,
                               0.0026]
NGA1["lines"][1]['M66rv50'] = [0.0637, 0.0654, 0.0672, 0.0717,
                               0.0753, 0.0837, 0.0953, 0.1079,
                               0.1307, 0.1460, 0.1314, 0.1242,
                               0.1062, 0.0851, 0.0706, 0.0562,
                               0.0428, 0.0282, 0.0194, 0.0108,
                               0.0073, 0.0051, 0.0040, 0.0029,
                               0.0017]

# CB08
NGA1["lines"][2] = {}
NGA1["lines"][2]['M55rv20'] = [0.1011, 0.1031, 0.1106, 0.1263,
                               0.1392, 0.1589, 0.1868, 0.2244,
                               0.2583, 0.2306, 0.1761, 0.1522,
                               0.1133, 0.0882, 0.0686, 0.0505,
                               0.0328, 0.0158, 0.0085, 0.0033,
                               0.0017, 0.0011, 0.0007, 0.0005,
                               0.0003]
NGA1["lines"][2]['M55rv50'] = [0.0356, 0.0362, 0.0381, 0.0424,
                               0.0458, 0.0512, 0.0586, 0.0692,
                               0.0815, 0.0767, 0.0619, 0.0551,
                               0.0417, 0.0330, 0.0260, 0.0194,
                               0.0126, 0.0061, 0.0033, 0.0013,
                               0.0006, 0.0004, 0.0003, 0.0002,
                               0.0001]
NGA1["lines"][2]['M62ss20'] = [0.1078, 0.1099, 0.1180, 0.1347,
                               0.1485, 0.1697, 0.1998, 0.2404,
                               0.2772, 0.2618, 0.2138, 0.1915,
                               0.1536, 0.1227, 0.0967, 0.0723,
                               0.0492, 0.0268, 0.0171, 0.0092,
                               0.0060, 0.0044, 0.0031, 0.0019,
                               0.0011]
NGA1["lines"][2]['M62ss50'] = [0.0419, 0.0425, 0.0448, 0.0498,
                               0.0538, 0.0602, 0.0689, 0.0814,
                               0.0958, 0.0957, 0.0827, 0.0764,
                               0.0623, 0.0506, 0.0405, 0.0308,
                               0.0209, 0.0114, 0.0073, 0.0039,
                               0.0025, 0.0019, 0.0013, 0.0008,
                               0.0005]
NGA1["lines"][2]['M66ss20'] = [0.1279, 0.1304, 0.1400, 0.1598,
                               0.1762, 0.2012, 0.2366, 0.2843,
                               0.3275, 0.3176, 0.2674, 0.2437,
                               0.2023, 0.1649, 0.1317, 0.1001,
                               0.0695, 0.0390, 0.0264, 0.0158,
                               0.0114, 0.0091, 0.0064, 0.0041,
                               0.0024]
NGA1["lines"][2]['M66ss50'] = [0.0527, 0.0535, 0.0564, 0.0627,
                               0.0677, 0.0756, 0.0864, 0.1018,
                               0.1198, 0.1229, 0.1097, 0.1030,
                               0.0871, 0.0722, 0.0585, 0.0452,
                               0.0314, 0.0176, 0.0119, 0.0071,
                               0.0051, 0.0041, 0.0029, 0.0019,
                               0.0011]
NGA1["lines"][2]['M66rv20'] = [0.1702, 0.1736, 0.1864, 0.2127,
                               0.2345, 0.2678, 0.3149, 0.3785,
                               0.4360, 0.4227, 0.3559, 0.3243,
                               0.2693, 0.2194, 0.1753, 0.1332,
                               0.0903, 0.0461, 0.0292, 0.0158,
                               0.0114, 0.0091, 0.0064, 0.0041,
                               0.0024]
NGA1["lines"][2]['M66rv50'] = [0.0698, 0.0709, 0.0747, 0.0829,
                               0.0896, 0.1000, 0.1144, 0.1348,
                               0.1585, 0.1626, 0.1452, 0.1364,
                               0.1153, 0.0956, 0.0774, 0.0598,
                               0.0406, 0.0207, 0.0131, 0.0071,
                               0.0051, 0.0041, 0.0029, 0.0019,
                               0.0011]

# CY08
NGA1["lines"][3] = {}
NGA1["lines"][3]['M55rv20'] = [0.0810, 0.0826, 0.0880, 0.0972,
                               0.1082, 0.1198, 0.1357, 0.1560,
                               0.1700, 0.1538, 0.1290, 0.1148,
                               0.0861, 0.0660, 0.0520, 0.0388,
                               0.0256, 0.0127, 0.0071, 0.0031,
                               0.0018, 0.0011, 0.0007, 0.0004,
                               0.0002]
NGA1["lines"][3]['M55rv50'] = [0.0219, 0.0223, 0.0234, 0.0255,
                               0.0279, 0.0305, 0.0341, 0.0391,
                               0.0438, 0.0410, 0.0355, 0.0321,
                               0.0249, 0.0196, 0.0157, 0.0119,
                               0.0081, 0.0041, 0.0023, 0.0010,
                               0.0006, 0.0004, 0.0002, 0.0001,
                               0.0001]
NGA1["lines"][3]['M62ss20'] = [0.1136, 0.1153, 0.1227, 0.1356,
                               0.1513, 0.1680, 0.1909, 0.2207,
                               0.2431, 0.2233, 0.1913, 0.1728,
                               0.1354, 0.1085, 0.0897, 0.0711,
                               0.0520, 0.0314, 0.0203, 0.0108,
                               0.0068, 0.0046, 0.0032, 0.0021,
                               0.0010]
NGA1["lines"][3]['M62ss50'] = [0.0360, 0.0364, 0.0384, 0.0418,
                               0.0460, 0.0506, 0.0567, 0.0653,
                               0.0734, 0.0690, 0.0604, 0.0552,
                               0.0443, 0.0361, 0.0302, 0.0243,
                               0.0181, 0.0112, 0.0073, 0.0039,
                               0.0025, 0.0017, 0.0012, 0.0008,
                               0.0004]
NGA1["lines"][3]['M66ss20'] = [0.1206, 0.1224, 0.1303, 0.1443,
                               0.1614, 0.1797, 0.2048, 0.2377,
                               0.2641, 0.2446, 0.2117, 0.1925,
                               0.1534, 0.1252, 0.1056, 0.0858,
                               0.0654, 0.0423, 0.0290, 0.0168,
                               0.0112, 0.0080, 0.0058, 0.0039,
                               0.0020]
NGA1["lines"][3]['M66ss50'] = [0.0420, 0.0425, 0.0448, 0.0489,
                               0.0540, 0.0595, 0.0670, 0.0774,
                               0.0875, 0.0826, 0.0727, 0.0668,
                               0.0542, 0.0448, 0.0382, 0.0315,
                               0.0244, 0.0161, 0.0111, 0.0065,
                               0.0044, 0.0031, 0.0023, 0.0015,
                               0.0008]
NGA1["lines"][3]['M66rv20'] = [0.1555, 0.1578, 0.1680, 0.1858,
                               0.2076, 0.2307, 0.2625, 0.3041,
                               0.3369, 0.3114, 0.2689, 0.2442,
                               0.1938, 0.1573, 0.1317, 0.1059,
                               0.0785, 0.0462, 0.0292, 0.0158,
                               0.0103, 0.0073, 0.0052, 0.0035,
                               0.0018]
NGA1["lines"][3]['M66rv50'] = [0.0540, 0.0547, 0.0577, 0.0629,
                               0.0694, 0.0764, 0.0858, 0.0989,
                               0.1115, 0.1050, 0.0923, 0.0846,
                               0.0684, 0.0563, 0.0476, 0.0388,
                               0.0292, 0.0175, 0.0112, 0.0061,
                               0.0040, 0.0028, 0.0020, 0.0014,
                               0.0007]

# Averages
NGA1["lines"]["avg"] = {}
NGA1["lines"]["avg"]['M55rv20'] = [0.066290344, 0.067809333, 0.075076612,
                                   0.095427199, 0.123038364, 0.138785753,
                                   0.142742706, 0.128838474, 0.111461771,
                                   0.096711888, 0.07219484, 0.055303032,
                                   0.031360581, 0.020074916, 0.009875678,
                                   0.005830732, 0.002714463, 0.001560327,
                                   0.001056087, 0.000454198]
NGA1["lines"]["avg"]['M55rv50'] = [0.019034259, 0.019396674, 0.021269488,
                                   0.026692088, 0.03397194, 0.038371121,
                                   0.040437953, 0.037314419, 0.033169393,
                                   0.029448671, 0.022628598, 0.017604679,
                                   0.010313173, 0.006623596, 0.003344627,
                                   0.001982765, 0.000922549, 0.000539826,
                                   0.00036732, 9.19224E-05]
NGA1["lines"]["avg"]['M62ss20'] = [0.103510707, 0.105260707, 0.111767005,
                                   0.130828596, 0.168527513, 0.197742401,
                                   0.229844714, 0.226726281, 0.20592189,
                                   0.184598449, 0.149515001, 0.119902721,
                                   0.075762555, 0.054046078, 0.031694796,
                                   0.020141424, 0.010450695, 0.006320955,
                                   0.004227879, 0.001003203]
NGA1["lines"]["avg"]['M62ss50'] = [0.039564, 0.040142, 0.042084,
                                   0.048008, 0.060872, 0.070692,
                                   0.084312, 0.087238, 0.082064,
                                   0.075884, 0.06277, 0.05119,
                                   0.033298, 0.024094, 0.014438,
                                   0.00928, 0.004806, 0.002882,
                                   0.001942, 0.000492]
NGA1["lines"]["avg"]['M66ss20'] = [0.121644, 0.123436, 0.13058,
                                   0.150878, 0.194408, 0.228818,
                                   0.270378, 0.276666, 0.257116,
                                   0.234692, 0.198486, 0.163964,
                                   0.109456, 0.08145, 0.050668,
                                   0.033872, 0.018882, 0.01232,
                                   0.008746, 0.002262]
NGA1["lines"]["avg"]['M66ss50'] = [0.049522, 0.050168, 0.052422,
                                   0.059134, 0.07465, 0.086692,
                                   0.104576, 0.111306, 0.106626,
                                   0.100142, 0.086538, 0.072642,
                                   0.049838, 0.037632, 0.023956,
                                   0.01623, 0.009006, 0.005828,
                                   0.004144, 0.001134]
NGA1["lines"]["avg"]['M66rv20'] = [0.12743, 0.129296, 0.136786,
                                   0.157664, 0.202742, 0.23922,
                                   0.284028, 0.292282, 0.273756,
                                   0.252088, 0.212834, 0.17658,
                                   0.118718, 0.086878, 0.051478,
                                   0.03361, 0.018258, 0.011778,
                                   0.00842, 0.002272]
NGA1["lines"]["avg"]['M66rv50'] = [0.054797707, 0.055471707, 0.058004005,
                                   0.065037596, 0.081481513, 0.094683401,
                                   0.113462714, 0.120770281, 0.11588589,
                                   0.108974449, 0.092998001, 0.077861721,
                                   0.053115555, 0.039082078, 0.023424796,
                                   0.015416424, 0.008357695, 0.005363955,
                                   0.003851879, 0.001110203]

NGA1["labels"] = ['AS08', 'BA08', 'CB08', 'CY08']
NGA1["acptThin"] = []

# NGA-WEST2

NGA2 = {}

NGA2["Tacpt"] = [0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.15, 0.2, 0.25,
                 0.3, 0.4, 0.5, 0.75, 1, 1.5, 2, 3]

NGA2["Tave"] = [0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.15, 0.2, 0.25,
                0.3, 0.4, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 7.5, 10]

NGA2["Tnga"] = [0.01, 0.02, 0.03, 0.05, 0.075, 0.1, 0.15, 0.2, 0.25,
                0.3, 0.4, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 7.5, 10]

NGA2["M55rvLo20"] = [0.043162828, 0.043986244, 0.050532404, 0.066308951,
                     0.082850287, 0.090492273, 0.093229471, 0.088219197,
                     0.07690558, 0.066913585, 0.049811164, 0.038094685,
                     0.021626464, 0.013399224, 0.006223801, 0.003481884,
                     0.001590148, 0.000979361, 0.000606029, 0.000222181,
                     0.00010707]
NGA2["M55rvHi20"] = [0.101461388, 0.104853367, 0.123067534, 0.162986215,
                     0.20535873, 0.221988581, 0.227886312, 0.199684117,
                     0.171597973, 0.15382306, 0.110439407, 0.082566413,
                     0.048377614, 0.030035889, 0.014174531, 0.008401558,
                     0.003936088, 0.002267193, 0.001518127, 0.000640095,
                     0.000353764]
NGA2["M55rvLo50"] = [0.011874366, 0.012092877, 0.013686828, 0.01803045,
                     0.022654288, 0.025115416, 0.027241209, 0.025956576,
                     0.023337165, 0.020780156, 0.015878191, 0.012320678,
                     0.007248839, 0.004645067, 0.002234405, 0.001259298,
                     0.000578201, 0.000351471, 0.000203256, 7.47E-05,
                     3.61E-05]
NGA2["M55rvHi50"] = [0.029726643, 0.030466799, 0.035226905, 0.045347672,
                     0.05659596, 0.062552874, 0.066022908, 0.059792354,
                     0.053539767, 0.049596244, 0.036182394, 0.027296789,
                     0.016582087, 0.010136058, 0.004879038, 0.002899413,
                     0.001356603, 0.000813832, 0.000560425, 0.000265524,
                     0.000143461]
NGA2["M62ssLo20"] = [0.068466531, 0.069695186, 0.076408093, 0.097422245,
                     0.123814297, 0.136956225, 0.146243099, 0.138883495,
                     0.125917282, 0.115074816, 0.093146032, 0.075465575,
                     0.047547496, 0.033686379, 0.019135667, 0.012666283,
                     0.007019788, 0.00412385, 0.002531575, 0.001038643,
                     0.000540379]
NGA2["M62ssHi20"] = [0.142504114, 0.145061397, 0.159033435, 0.210536846,
                     0.261667301, 0.295110986, 0.323027865, 0.30742257,
                     0.27493888, 0.243420355, 0.193871262, 0.157071708,
                     0.100027172, 0.070113785, 0.039828384, 0.026363208,
                     0.01461077, 0.009514016, 0.006874933, 0.003753873,
                     0.002164308]
NGA2["M62ssLo50"] = [0.02137448, 0.021959453, 0.024099659, 0.031448649,
                     0.040144247, 0.043917118, 0.048246515, 0.045530733,
                     0.041939743, 0.039251989, 0.032923606, 0.027287471,
                     0.018056208, 0.012726296, 0.007399963, 0.004918294,
                     0.002716006, 0.00152508, 0.00093839, 0.000385732,
                     0.000200894]
NGA2["M62ssHi50"] = [0.048745761, 0.049404324, 0.053607771, 0.067996794,
                     0.085744903, 0.096611288, 0.109675825, 0.110567163,
                     0.101388253, 0.090808185, 0.07241828, 0.058417202,
                     0.038183052, 0.026682366, 0.015402054, 0.010236785,
                     0.005674808, 0.003772374, 0.002794809, 0.00161524,
                     0.000907223]
NGA2["M66ssLo20"] = [0.085417391, 0.086968792, 0.094320954, 0.117282459,
                     0.152663489, 0.172353817, 0.188287767, 0.179195223,
                     0.161293332, 0.143989459, 0.117702522, 0.097375597,
                     0.064089178, 0.046454371, 0.028139301, 0.019587618,
                     0.011989636, 0.007364112, 0.004795379, 0.002143994,
                     0.001177935]
NGA2["M66ssHi20"] = [0.179247367, 0.18480203, 0.215915347, 0.292453367,
                     0.356440194, 0.375056915, 0.391896317, 0.372971379,
                     0.335710939, 0.299695194, 0.244982378, 0.202674547,
                     0.133393226, 0.096688686, 0.058568268, 0.040769061,
                     0.025160942, 0.01757914, 0.014688678, 0.008873136,
                     0.005018356]
NGA2["M66ssLo50"] = [0.030572971, 0.031321765, 0.034464839, 0.043768108,
                     0.054836348, 0.061081236, 0.067142045, 0.065019697,
                     0.060024542, 0.054796469, 0.045702084, 0.038262082,
                     0.025924541, 0.018722731, 0.011607744, 0.008113298,
                     0.004697369, 0.002904508, 0.001895507, 0.000848924,
                     0.000466798]
NGA2["M66ssHi50"] = [0.06661183, 0.068101077, 0.078444572, 0.103073492,
                     0.122849466, 0.129733734, 0.13974737, 0.135329981,
                     0.124933219, 0.11405167, 0.0951229, 0.079637511,
                     0.053958536, 0.038968912, 0.024159999, 0.016886766,
                     0.010399168, 0.007978404, 0.006734335, 0.004488779,
                     0.002598649]
NGA2["M66rvLo20"] = [0.091912809, 0.093697767, 0.103905415, 0.132085813,
                     0.169327928, 0.190813845, 0.209418048, 0.199794261,
                     0.180877286, 0.162285484, 0.13190601, 0.108449534,
                     0.070777948, 0.050935201, 0.030429577, 0.020755248,
                     0.012435454, 0.007913061, 0.005022391, 0.002186587,
                     0.001190133]
NGA2["M66rvHi20"] = [0.194889368, 0.200576566, 0.234480754, 0.312298198,
                     0.382137301, 0.406009288, 0.435876229, 0.415845578,
                     0.376472372, 0.337775973, 0.274545078, 0.225723496,
                     0.147315025, 0.106014948, 0.063335178, 0.04319933,
                     0.025882768, 0.01757914, 0.014688678, 0.008873136,
                     0.005018356]
NGA2["M66rvLo50"] = [0.033119304, 0.034025707, 0.037341911, 0.047530169,
                     0.059827313, 0.067125841, 0.074010372, 0.071772064,
                     0.066391977, 0.061241183, 0.050794904, 0.042260195,
                     0.028380811, 0.020347486, 0.01244476, 0.00852593,
                     0.005104347, 0.003121034, 0.00198524, 0.000865789,
                     0.000471632]
NGA2["M66rvHi50"] = [0.072425493, 0.073914951, 0.085211981, 0.110196595,
                     0.131902644, 0.140616093, 0.154042892, 0.149485142,
                     0.138869765, 0.127465499, 0.105722937, 0.087959058,
                     0.059070939, 0.042350627, 0.025902137, 0.017745606,
                     0.010624029, 0.007978404, 0.006734335, 0.004488779,
                     0.002598649]

NGA2["number_of_models"] = 5
NGA2["lines"] = {}

# ASK14
NGA2["lines"][0] = {}
NGA2["lines"][0]['M55rv20'] = [0.061578106, 0.063005254, 0.066803615,
                               0.079421302, 0.103669724, 0.122302981,
                               0.131836331, 0.125195364, 0.107642513,
                               0.088189081, 0.063743205, 0.049345676,
                               0.026926007, 0.017621022, 0.009757405,
                               0.006278258, 0.003083182, 0.001761033,
                               0.001129384, 0.000519722, 0.000278025]
NGA2["lines"][0]['M55rv50'] = [0.017973237, 0.018334702, 0.019323745,
                               0.022681496, 0.028902667, 0.033418535,
                               0.035914413, 0.03503869, 0.031230435,
                               0.026365612, 0.020296329, 0.016239192,
                               0.009049143, 0.005921975, 0.003308861,
                               0.002148285, 0.001049484, 0.000593693,
                               0.000381771, 0.000190624, 0.000109169]
NGA2["lines"][0]['M62ss20'] = [0.097260036, 0.099514156, 0.105513509,
                               0.125226914, 0.163101257, 0.192732154,
                               0.21370852, 0.209057057, 0.185161324,
                               0.156783355, 0.118907321, 0.096296497,
                               0.059678655, 0.041746868, 0.024717174,
                               0.016937065, 0.009578433, 0.006069052,
                               0.004245604, 0.002275094, 0.00137539]
NGA2["lines"][0]['M62ss50'] = [0.033729564, 0.03440791, 0.036264002,
                               0.04253211, 0.054144693, 0.062651337,
                               0.069172238, 0.069518561, 0.063829408,
                               0.055692835, 0.044985083, 0.037653221,
                               0.023830374, 0.016670005, 0.009959082,
                               0.006886004, 0.003873892, 0.002431038,
                               0.001705207, 0.000991475, 0.00064168]
NGA2["lines"][0]['M66ss20'] = [0.107214866, 0.109699701, 0.116313104,
                               0.137979363, 0.179604104, 0.212327795,
                               0.23836897, 0.236335866, 0.212154377,
                               0.184327061, 0.145327688, 0.121617233,
                               0.081844504, 0.060019599, 0.037489656,
                               0.026885609, 0.016588684, 0.011205549,
                               0.008267819, 0.004858201, 0.003157512]
NGA2["lines"][0]['M66ss50'] = [0.041031537, 0.041856735, 0.044114644,
                               0.051721186, 0.065813166, 0.076179117,
                               0.085142458, 0.086726546, 0.080706593,
                               0.072256172, 0.060672902, 0.052477512,
                               0.036065157, 0.026447912, 0.01666934,
                               0.012062449, 0.007403745, 0.004953252,
                               0.003664504, 0.002336385, 0.001625639]
NGA2["lines"][0]['M66rv20'] = [0.126448475, 0.129379072, 0.137178875,
                               0.162586178, 0.211397013, 0.250125487,
                               0.281130723, 0.278732894, 0.250213412,
                               0.215123372, 0.166832633, 0.137948258,
                               0.090633251, 0.065377042, 0.03992749,
                               0.028165312, 0.016991629, 0.011289907,
                               0.008218361, 0.004721696, 0.003018573]
NGA2["lines"][0]['M66rv50'] = [0.048392313, 0.049365545, 0.052028508,
                               0.060977747, 0.077557098, 0.089803529,
                               0.100416429, 0.102284691, 0.0951848,
                               0.08432832, 0.069651008, 0.059524306,
                               0.039937959, 0.028808694, 0.017753295,
                               0.012636598, 0.007583584, 0.004990541,
                               0.003642583, 0.002270738, 0.001554107]

# BSSA14
NGA2["lines"][1] = {}
NGA2["lines"][1]['M55rv20'] = [0.076413241, 0.077253575, 0.08184956,
                               0.102934282, 0.139186143, 0.162575284,
                               0.150573516, 0.120952464, 0.098543235,
                               0.081992934, 0.060430775, 0.046090136,
                               0.025442899, 0.015763793, 0.007322119,
                               0.004096335, 0.001870762, 0.00115219,
                               0.000846125, 0.000464796, 0.00028245]
NGA2["lines"][1]['M55rv50'] = [0.024858984, 0.024970577, 0.026149586,
                               0.032087775, 0.043458106, 0.051762526,
                               0.049593053, 0.040836822, 0.033851719,
                               0.028501079, 0.021217238, 0.016225418,
                               0.008991406, 0.005582111, 0.002628712,
                               0.001481527, 0.000680236, 0.000423299,
                               0.000313349, 0.000175406, 0.000102974]
NGA2["lines"][1]['M62ss20'] = [0.100226462, 0.101211123, 0.108598161,
                               0.137527661, 0.178408359, 0.211159905,
                               0.253176163, 0.250521243, 0.226604957,
                               0.20124361, 0.159724699, 0.12887546,
                               0.084387721, 0.059134451, 0.03242305,
                               0.020500152, 0.011317539, 0.007699554,
                               0.005945281, 0.003264237, 0.001882007]
NGA2["lines"][1]['M62ss50'] = [0.038217, 0.038306577, 0.040607634,
                               0.050157117, 0.064950615, 0.077837808,
                               0.095370282, 0.096145359, 0.088163698,
                               0.078963639, 0.062972417, 0.050797567,
                               0.033202654, 0.023202057, 0.012841278,
                               0.008173773, 0.004536355, 0.003123293,
                               0.002430269, 0.001371234, 0.000776325]
NGA2["lines"][1]['M66ss20'] = [0.118221219, 0.119111241, 0.127552723,
                               0.160340628, 0.205462127, 0.240048923,
                               0.283481833, 0.280130756, 0.254800671,
                               0.228008082, 0.184144195, 0.150825535,
                               0.100772538, 0.071938892, 0.041753699,
                               0.02757776, 0.016384654, 0.011838832,
                               0.00957189, 0.005713354, 0.003478785]
NGA2["lines"][1]['M66ss50'] = [0.048281431, 0.04825033, 0.051026245,
                               0.062543085, 0.079869247, 0.094132499,
                               0.112850488, 0.113230289, 0.104189604,
                               0.093840938, 0.075904444, 0.062040067,
                               0.041231207, 0.029266718, 0.017106648,
                               0.011372459, 0.006794132, 0.004975001,
                               0.004054219, 0.002502137, 0.00150916]
NGA2["lines"][1]['M66rv20'] = [0.11909749, 0.119666451, 0.126105188,
                               0.155395075, 0.203184171, 0.244533089,
                               0.290417208, 0.286405675, 0.266074813,
                               0.243369522, 0.198369114, 0.160476045,
                               0.104494057, 0.074359211, 0.042481821,
                               0.027178519, 0.015080032, 0.010330213,
                               0.0081176, 0.004863548, 0.002900911]
NGA2["lines"][1]['M66rv50'] = [0.047200202, 0.047025235, 0.048922531,
                               0.058776711, 0.076615375, 0.093026548,
                               0.112141029, 0.112271666, 0.105483435,
                               0.097071955, 0.079180844, 0.063885788,
                               0.041339691, 0.029232544, 0.016819048,
                               0.010833547, 0.006047472, 0.004201834,
                               0.003330127, 0.002068516, 0.001221946]

# CB14
NGA2["lines"][2] = {}
NGA2["lines"][2]['M55rv20'] = [0.088227294, 0.091176841, 0.107015247,
                               0.141727144, 0.178572809, 0.193033549,
                               0.19816201, 0.173638363, 0.149215629,
                               0.133759182, 0.096034267, 0.071796881,
                               0.04206749, 0.026118164, 0.011831608,
                               0.006783503, 0.003010526, 0.001819498,
                               0.001282769, 0.000416311, 0.000246199]
NGA2["lines"][2]['M55rv50'] = [0.025849255, 0.026492868, 0.030632091,
                               0.039432759, 0.049213878, 0.054393803,
                               0.057411225, 0.051993352, 0.04655632,
                               0.043127169, 0.031462951, 0.023736338,
                               0.014419206, 0.008813964, 0.004124255,
                               0.00230833, 0.00102465, 0.000628343,
                               0.000434391, 0.00014163, 8.23E-05]
NGA2["lines"][2]['M62ss20'] = [0.113707125, 0.117106954, 0.135995759,
                               0.183075518, 0.226667031, 0.242887067,
                               0.253579481, 0.228661454, 0.201816233,
                               0.180936926, 0.148390053, 0.119185793,
                               0.076110869, 0.052595034, 0.029010492,
                               0.019650293, 0.010665338, 0.00715859,
                               0.005076754, 0.001994784, 0.001188622]
NGA2["lines"][2]['M62ss50'] = [0.038755556, 0.039581835, 0.045291512,
                               0.059126679, 0.071895334, 0.07786539,
                               0.081979597, 0.076292242, 0.070566843,
                               0.065679689, 0.054399992, 0.044126235,
                               0.029276483, 0.019706214, 0.011149448,
                               0.007364419, 0.003972911, 0.002680179,
                               0.001862422, 0.000748463, 0.000443649]
NGA2["lines"][2]['M66ss20'] = [0.155867276, 0.160697418, 0.187752475,
                               0.254307276, 0.309947995, 0.326136448,
                               0.337602011, 0.304238415, 0.269375658,
                               0.241112458, 0.204374775, 0.166746767,
                               0.10902705, 0.078887722, 0.047355531,
                               0.033593255, 0.019975941, 0.013665746,
                               0.009815503, 0.004282007, 0.00262249]
NGA2["lines"][2]['M66ss50'] = [0.05792333, 0.059218328, 0.068212671,
                               0.089629124, 0.106825622, 0.112811943,
                               0.116228331, 0.107981711, 0.100525763,
                               0.093656666, 0.079894881, 0.065860293,
                               0.044794242, 0.031378182, 0.019244009,
                               0.013303901, 0.007835056, 0.005358235,
                               0.003769357, 0.001699136, 0.001042387]
NGA2["lines"][2]['M66rv20'] = [0.169469016, 0.174414405, 0.203896308,
                               0.271563651, 0.332293305, 0.353051555,
                               0.371985969, 0.339441624, 0.302509153,
                               0.273839683, 0.227737358, 0.184555278,
                               0.121125476, 0.087773522, 0.052085813,
                               0.035782677, 0.021192734, 0.01401949,
                               0.009899476, 0.004335988, 0.002688353]
NGA2["lines"][2]['M66rv50'] = [0.062978689, 0.064273871, 0.074097375,
                               0.095823126, 0.114697951, 0.122274863,
                               0.128082941, 0.120476197, 0.11289054,
                               0.106369086, 0.089027861, 0.072894155,
                               0.049764934, 0.034912575, 0.021166268,
                               0.014170976, 0.008312312, 0.005496935,
                               0.003801604, 0.001720556, 0.001068566]

# CY14
NGA2["lines"][3] = {}
NGA2["lines"][3]['M55rv20'] = [0.050779798, 0.051748523, 0.059449887,
                               0.080828357, 0.104725139, 0.116927762,
                               0.124724949, 0.118262962, 0.106749044,
                               0.094837167, 0.073538436, 0.05717148,
                               0.032375774, 0.020061193, 0.009692497,
                               0.005622758, 0.002415163, 0.001236062,
                               0.000712976, 0.000261389, 0.000125965]
NGA2["lines"][3]['M55rv50'] = [0.013969843, 0.014226914, 0.016102151,
                               0.021212294, 0.026652103, 0.029547549,
                               0.032048481, 0.031347835, 0.028981799,
                               0.026286737, 0.021046008, 0.016745318,
                               0.00982377, 0.006229585, 0.003110842,
                               0.00183045, 0.000802731, 0.000413495,
                               0.000239125, 8.79E-05, 4.24E-05]
NGA2["lines"][3]['M62ss20'] = [0.101415186, 0.101415186, 0.109425615,
                               0.148990467, 0.193029233, 0.214872641,
                               0.227014399, 0.213813337, 0.191145056,
                               0.169284638, 0.134258874, 0.109339519,
                               0.070552286, 0.048531253, 0.027472277,
                               0.017913705, 0.008698077, 0.004851588,
                               0.002978323, 0.001221933, 0.000635741]
NGA2["lines"][3]['M62ss50'] = [0.033091367, 0.033091367, 0.035274688,
                               0.046844613, 0.058950022, 0.064807478,
                               0.068292508, 0.065255872, 0.059159035,
                               0.05312583, 0.042993935, 0.035564299,
                               0.023562859, 0.016535517, 0.009680927,
                               0.006421574, 0.003195302, 0.001794211,
                               0.001103988, 0.000453802, 0.000236346]
NGA2["lines"][3]['M66ss20'] = [0.125746737, 0.125746737, 0.125746737,
                               0.170211789, 0.220822205, 0.24683404,
                               0.262423678, 0.248404156, 0.223487098,
                               0.199261403, 0.159963361, 0.131756459,
                               0.087391411, 0.061649539, 0.036629482,
                               0.025129052, 0.014105455, 0.008663661,
                               0.005641622, 0.002522346, 0.001385806]
NGA2["lines"][3]['M66ss50'] = [0.039050726, 0.039660812, 0.044703667,
                               0.059273672, 0.074804773, 0.08255535,
                               0.087151995, 0.083133586, 0.075460619,
                               0.067943228, 0.055238197, 0.045960828,
                               0.03112521, 0.022357126, 0.013736312,
                               0.009595068, 0.005526317, 0.003417069,
                               0.002230008, 0.000998734, 0.000549175]
NGA2["lines"][3]['M66rv20'] = [0.129802524, 0.130998636, 0.149076227,
                               0.201967882, 0.26189142, 0.292647877,
                               0.311081899, 0.294504065, 0.265325907,
                               0.236866669, 0.190398425, 0.156898193,
                               0.104096666, 0.073437264, 0.043631714,
                               0.029913998, 0.015885012, 0.009309483,
                               0.005908695, 0.002572455, 0.001400157]
NGA2["lines"][3]['M66rv50'] = [0.046341792, 0.047067794, 0.053057086,
                               0.070350027, 0.088760431, 0.097935955,
                               0.103368365, 0.098605745, 0.089618947,
                               0.080788226, 0.065760287, 0.054738377,
                               0.037077184, 0.026632865, 0.016362689,
                               0.01142299, 0.006223852, 0.003671804,
                               0.002335576, 0.001018575, 0.000554862]

# I14
NGA2["lines"][4] = {}
NGA2["lines"][4]['M55rv20'] = [0.055024189, 0.056530081, 0.0620396,
                               0.078010531, 0.097470926, 0.106461498,
                               0.109681731, 0.10378729, 0.092606074,
                               0.083901121, 0.065564911, 0.050390889,
                               0.029590832, 0.020427919, 0.010521551,
                               0.006336727, 0.003261807, 0.00188871,
                               0.001290176, 0.000556183, 0.000293414]
NGA2["lines"][4]['M55rv50'] = [0.015236776, 0.015653773, 0.017179417,
                               0.022394675, 0.027975214, 0.030215321,
                               0.03246442, 0.030537148, 0.02772179,
                               0.02561654, 0.020514408, 0.01592864,
                               0.010005795, 0.006959442, 0.003736788,
                               0.002280009, 0.001144526, 0.00070768,
                               0.000487326, 0.00023089, 0.000124749]
NGA2["lines"][4]['M62ss20'] = [0.081272815, 0.083497074, 0.091634843,
                               0.114614406, 0.145663879, 0.161124971,
                               0.172050705, 0.163392347, 0.148137978,
                               0.135382136, 0.110625647, 0.090671788,
                               0.055938231, 0.040988255, 0.024411668,
                               0.0163667, 0.010377681, 0.007194282,
                               0.00558074, 0.002983396, 0.001636698]
NGA2["lines"][4]['M62ss50'] = [0.025146447, 0.02583465, 0.02835254,
                               0.03699841, 0.047228526, 0.051667197,
                               0.056760605, 0.053565569, 0.049340874,
                               0.04617881, 0.038733654, 0.032102907,
                               0.021242597, 0.015687026, 0.009748716,
                               0.006632224, 0.004088925, 0.00304533,
                               0.002384554, 0.001404556, 0.000788889]
NGA2["lines"][4]['M66ss20'] = [0.109106135, 0.11209213, 0.123016824,
                               0.152243515, 0.196325526, 0.217922781,
                               0.236332644, 0.223511106, 0.203667649,
                               0.185955391, 0.155234204, 0.131470461,
                               0.08327019, 0.062601698, 0.039753782,
                               0.028109121, 0.020146445, 0.015286208,
                               0.012772763, 0.00771577, 0.004363788]
NGA2["lines"][4]['M66ss50'] = [0.035968201, 0.036952572, 0.040554034,
                               0.052553093, 0.068247826, 0.07492907,
                               0.082954204, 0.077945842, 0.072102963,
                               0.067575964, 0.057960425, 0.049663881,
                               0.033790218, 0.025606021, 0.016975868,
                               0.012191196, 0.00848152, 0.006937743,
                               0.005855944, 0.003903286, 0.002259695]
NGA2["lines"][4]['M66rv20'] = [0.118193265, 0.121427955, 0.133262534,
                               0.164923431, 0.212676904, 0.236072931,
                               0.256016097, 0.242126691, 0.220630531,
                               0.20144307, 0.168163205, 0.14242025,
                               0.09020552, 0.066472771, 0.041376165,
                               0.028676963, 0.020553431, 0.015286208,
                               0.012772763, 0.00771577, 0.004363788]
NGA2["lines"][4]['M66rv50'] = [0.038963887, 0.040030243, 0.04393166,
                               0.056930086, 0.073931987, 0.081169692,
                               0.089863216, 0.084437722, 0.078108208,
                               0.073204168, 0.062787779, 0.05380024,
                               0.036604506, 0.027189409, 0.017668666,
                               0.012437474, 0.008652858, 0.006937743,
                               0.005855944, 0.003903286, 0.002259695]

# Averages
NGA2["lines"]["avg"] = {}
NGA2["lines"]["avg"]['M55rv20'] = [0.066404526, 0.067942854, 0.075431582,
                                   0.096584323, 0.124724948, 0.140260215,
                                   0.142995707, 0.128367288, 0.110951299,
                                   0.096535897, 0.071862319, 0.054959013,
                                   0.0312806, 0.019998418, 0.009825036,
                                   0.005823516, 0.002728288, 0.001571499,
                                   0.001052286, 0.00044368, 0.000245211]
NGA2["lines"]["avg"]['M55rv50'] = [0.019577619, 0.019935767, 0.021877398,
                                   0.0275618, 0.035240394, 0.039867547,
                                   0.041486318, 0.037950769, 0.033668413,
                                   0.029979427, 0.022907387, 0.017774981,
                                   0.010457864, 0.006701415, 0.003381892,
                                   0.00200972, 0.000940326, 0.000553302,
                                   0.000371193, 0.000165286, 9.23E-05]
NGA2["lines"]["avg"]['M62ss20'] = [0.098776325, 0.100548899, 0.110233577,
                                   0.141886993, 0.181373952, 0.204555348,
                                   0.223905854, 0.213089088, 0.19057311,
                                   0.168726133, 0.134381319, 0.108873811,
                                   0.069333552, 0.048599172, 0.027606932,
                                   0.018273583, 0.010127414, 0.006594613,
                                   0.00476534, 0.002347889, 0.001343692]
NGA2["lines"]["avg"]['M62ss50'] = [0.033787987, 0.034244468, 0.037158075,
                                   0.047131786, 0.059433838, 0.066965842,
                                   0.074315046, 0.072155521, 0.066211972,
                                   0.059928161, 0.048817016, 0.040048846,
                                   0.026222993, 0.018360164, 0.01067589,
                                   0.007095599, 0.003933477, 0.00261481,
                                   0.001897288, 0.000993906, 0.000577378]
NGA2["lines"]["avg"]['M66ss20'] = [0.123231246, 0.125469445, 0.136076373,
                                   0.175016514, 0.222432391, 0.248653998,
                                   0.271641827, 0.25852406, 0.232697091,
                                   0.207732879, 0.169808845, 0.140483291,
                                   0.092461139, 0.06701949, 0.04059643,
                                   0.02825896, 0.017440236, 0.012132,
                                   0.00921392, 0.005018336, 0.003001676]
NGA2["lines"]["avg"]['M66ss50'] = [0.044451045, 0.045187755, 0.049722252,
                                   0.063144032, 0.079112127, 0.088121596,
                                   0.096865495, 0.093803595, 0.086597108,
                                   0.079054594, 0.06593417, 0.055200516,
                                   0.037401207, 0.027011192, 0.016746435,
                                   0.011705015, 0.007208154, 0.00512826,
                                   0.003914806, 0.002287936, 0.001397211]
NGA2["lines"]["avg"]['M66rv20'] = [0.132602154, 0.135177304, 0.149903826,
                                   0.191287243, 0.244288563, 0.275286188,
                                   0.302126379, 0.28824219, 0.260950763,
                                   0.234128463, 0.190300147, 0.156459605,
                                   0.102110994, 0.073483962, 0.0439006,
                                   0.029943494, 0.017940568, 0.01204706,
                                   0.008983379, 0.004841892, 0.002874356]
NGA2["lines"]["avg"]['M66rv50'] = [0.048775377, 0.049552538, 0.054407432,
                                   0.06857154, 0.086312568, 0.096842118,
                                   0.106774396, 0.103615204, 0.096257186,
                                   0.088352351, 0.073281556, 0.060968573,
                                   0.040944855, 0.029355217, 0.017953993,
                                   0.012300317, 0.007364016, 0.005059772,
                                   0.003793167, 0.002196334, 0.001331835]

NGA2["labels"] = ['ASK14', 'BSSA14', 'CB14', 'CY14', 'I14']
NGA2["acptThin"] = [3, 4, 5, 7.5, 10]

# 9 line colors in 'excel' colormap
RGB_XLS = [[0.96863, 0.58824, 0.27451],
           [0.50196, 0.39216, 0.77647],
           [0.60784, 0.73333, 0.34902],
           [0.75294, 0.31373, 0.30196],
           [0.58039, 0.54118, 0.32941],
           [0.29412, 0.67451, 0.77647],
           [0.30980, 0.50588, 0.74118],
           [0.12157, 0.28627, 0.49020],
           [0.45, 0.45, 0.45]]

COLOR_NGA = [0, 0, 0]

# --------------------------------------------------------------------------
# Functions
# --------------------------------------------------------------------------

def create_plot(codebase, mag, vel, model, dist, mech, sim_data, output_file):
    """
    This function creates a GMPE comparison plot
    Code was migrated from the Matlab script written by Ronnie.
    """
    # Set method name
    if codebase == 'gp':
        method_name = "Graves & Pitarka"
    elif codebase == 'sdsu':
        method_name = "SDSU"
    elif codebase == 'ucsb':
        method_name = "UCSB"
    elif codebase == 'exsim':
        method_name = "ExSIM"
    elif codebase == 'csm':
        method_name = "CSM"
    elif codebase == 'irikura_recipe_m1':
        method_name = "Irikura Recipe M1"
    elif codebase == 'irikura_recipe_m2':
        method_name = "Irikura Recipe M2"
    elif codebase == 'song':
        method_name = "Song"
    else:
        method_name = ""

    plot_title = ("%s, Scenario: M%.1f, %s, R=%d km, %s" %
                  (method_name, mag, mech, dist, vel))

    # Select model and lines
    if model == "nga-west1":
        nga = NGA1
    else:
        nga = NGA2

    nga_lines = []
    nga_labels = nga["labels"]
    nga_periods = nga["Tnga"]
    nga_ave_periods = nga["Tave"]
    nga_acpt_thin = nga["acptThin"]

    if mech == "REV" and mag == 5.5 and dist == 20:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M55rvLo20"]
        acpt_hi = nga["M55rvHi20"]
        nga_ave = nga["lines"]["avg"]["M55rv20"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M55rv20"])
    elif mech == "REV" and mag == 5.5 and dist == 50:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M55rvLo50"]
        acpt_hi = nga["M55rvHi50"]
        nga_ave = nga["lines"]["avg"]["M55rv50"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M55rv50"])
    elif mech == "SS" and mag == 6.2 and dist == 20:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M62ssLo20"]
        acpt_hi = nga["M62ssHi20"]
        nga_ave = nga["lines"]["avg"]["M62ss20"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M62ss20"])
    elif mech == "SS" and mag == 6.2 and dist == 50:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M62ssLo50"]
        acpt_hi = nga["M62ssHi50"]
        nga_ave = nga["lines"]["avg"]["M62ss50"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M62ss50"])
    elif mech == "SS" and mag == 6.6 and dist == 20:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M66ssLo20"]
        acpt_hi = nga["M66ssHi20"]
        nga_ave = nga["lines"]["avg"]["M66ss20"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M66ss20"])
    elif mech == "SS" and mag == 6.6 and dist == 50:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M66ssLo50"]
        acpt_hi = nga["M66ssHi50"]
        nga_ave = nga["lines"]["avg"]["M66ss50"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M66ss50"])
    elif mech == "REV" and mag == 6.6 and dist == 20:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M66rvLo20"]
        acpt_hi = nga["M66rvHi20"]
        nga_ave = nga["lines"]["avg"]["M66rv20"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M66rv20"])
    elif mech == "REV" and mag == 6.6 and dist == 50:
        acpt_x = nga["Tacpt"]
        acpt_lo = nga["M66rvLo50"]
        acpt_hi = nga["M66rvHi50"]
        nga_ave = nga["lines"]["avg"]["M66rv50"]
        for line in range(0, nga["number_of_models"]):
            nga_lines.append(nga["lines"][line]["M66rv50"])

    # Create arrays for sim_data mean and stddev
    sim_mean = []
    for data in sim_data:
        sim_mean.append(np.exp(np.mean(np.log(data))))

    box_positions = PERIODS
    box_width = 1.5*10.**(np.log10(PERIODS)-1)

    # y axis
    ymin = 0.00006
    ymax = 4.0

    # Initialize plot
    fig, _ = plt.subplots()
    fig.clf()

    # Array for labels
    l_handlers = []
    labels = nga_labels
    labels.append("Mean NGA Models")
    labels.append("Acceptance Criteria")

    # Plot NGA lines and average
    for line in range(0, len(nga_lines)):
        l_handlers.append(plt.plot(nga_periods, nga_lines[line],
                                   color=RGB_XLS[line], lw=1.8)[0])
    l_handlers.append(plt.plot(nga_ave_periods, nga_ave,
                               color=COLOR_NGA, lw=1.8)[0])
    # Plot acceptance criteria
    l_handlers.append(plt.plot(acpt_x, acpt_hi[0:len(acpt_x)],
                               color=COLOR_NGA, lw=2, ls='--')[0])
    plt.plot(acpt_x, acpt_lo[0:len(acpt_x)], color=COLOR_NGA, lw=2, ls='--')
    if len(nga_acpt_thin):
        plt.plot(nga_acpt_thin,
                 acpt_hi[len(acpt_hi)-len(nga_acpt_thin):len(acpt_hi)],
                 color=COLOR_NGA, lw=0.8, ls='--')
        plt.plot(nga_acpt_thin,
                 acpt_lo[len(acpt_lo)-len(nga_acpt_thin):len(acpt_lo)],
                 color=COLOR_NGA, lw=0.8, ls='--')
    # Now plot the data
    plt.boxplot(sim_data, positions=box_positions,
                widths=box_width, whis='range',
                boxprops=dict(linewidth=1.5),
                whiskerprops=dict(linewidth=1.1,
                                  linestyle='-',
                                  color='k'))
    plt.scatter(box_positions, sim_mean, s=50, color='red', marker='o')

    plt.legend(l_handlers, labels, loc='lower left')
    plt.xscale('log')
    plt.yscale('log')
    plt.ylabel('PSA (g)')
    plt.xlabel('Period (sec)')
    plt.title(plot_title)
    plt.axis([0.01, 20, ymin, ymax])
    plt.xticks([0.01, 0.1, 1, 10], ["0.01", "0.1", "1", "10"])
    plt.grid(True)
    plt.grid(b=True, which='major', linestyle='-', color='lightgray')
    plt.grid(b=True, which='minor', linewidth=0.5, color='gray')
    plt.minorticks_on()

    # All done! Save plot!
    pylab.gcf().set_size_inches(10, 7.5)
    pylab.savefig(output_file, format="png", dpi=plot_config.dpi)

def load_sim_data(input_dir):
    """
    This function reads all simulation data from the rd50 files
    """
    # Create empty arrays for simulation data
    sim_data = []
    for _ in PERIODS:
        sim_data.append([])

    realizations = sorted(os.listdir(input_dir))
    for realization in realizations:
        basedir = os.path.join(input_dir, realization)
        stations = sorted(glob.glob("%s%s*.rd50" % (basedir, os.sep)))
        for station in stations:
            input_file = open(station, 'r')
            # Start with the first period
            cur_period_idx = 0
            for line in input_file:
                cur_period = PERIODS[cur_period_idx]
                line = line.strip()
                # Skip comments
                if line.startswith("#"):
                    continue
                pieces = [float(item) for item in line.split()]
                # Check if we want this period
                if pieces[0] == cur_period:
                    sim_data[cur_period_idx].append(pieces[3])
                    cur_period_idx = cur_period_idx + 1
            # Done with this file
            input_file.close()

    # Return simulation data
    return sim_data

# --------------------------------------------------------------------------
# Main
# --------------------------------------------------------------------------

def run():
    """
    Main part of the code
    """
    parser = optparse.OptionParser()
    parser.add_option("-d", "--dir", dest="input_dir",
                      help="Input directory containing simulation results")
    parser.add_option("-o", "--output", dest="output_dir",
                      help="Output directory for the plots")
    parser.add_option("-c", "--codebase", dest="codebase",
                      help="Codebase used in the simulation")
    parser.add_option("-m", "--mag", type="float", dest="mag",
                      help="Event Magnitude")
    parser.add_option("--dist", type="int", dest="dist",
                      help="Distance of the stations in km")
    parser.add_option("--mech", dest="mech",
                      help="Fault mechanism")
    parser.add_option("--vel", dest="vel",
                      help="Velocity model")
    parser.add_option("--nga-west1", action="store_true", dest="nga1",
                      help="Uses NGA West 1 GMPEs for comparison")
    (options, _) = parser.parse_args()


    if options.input_dir is None:
        parser.error("Please specify the input directory!")
    top_input_dir = options.input_dir
    if not os.path.isdir(top_input_dir):
        parser.error("Invalid input directory!")
    dirs = os.listdir(top_input_dir)
    if not "Sims" in dirs:
        parser.error("Please provide the top-level simulation directory!\n"
                     "This is the directory given to the cluster script")
    input_outdir = os.path.join(top_input_dir, "Sims", "outdata")
    # Validate codebase to use
    codebase = options.codebase
    if codebase is None:
        print("Please specify a codebase!")
        sys.exit(1)
    codebase = codebase.lower()
    if codebase not in CODEBASES:
        print("Codebase needs to be one of: %s" % (CODEBASES))
        sys.exit(1)
    if options.mag is None:
        parser.error("Please specify magnitude!")
    mag = float(options.mag)
    if options.mech is None:
        parser.error("Please specify fault mechanism!")
    mech = options.mech.upper()
    if options.dist is None:
        parser.error("Please specify station distance!")
    dist = int(options.dist)
    if options.vel is None:
        parser.error("Please specify velocity model!")
    vel = options.vel.upper()

    # Default is to use NGA-WEST2
    if options.nga1 is None:
        model = "nga-west2"
        model_short = "w2"
    else:
        model = "nga-west1"
        model_short = "w1"

    # Output filename
    r_output_file = ("boxplot_%s-gmpe%d%s%d-%s-%s.png" % (codebase,
                                                          int(mag * 10),
                                                          mech,
                                                          dist,
                                                          vel,
                                                          model_short))
    if options.output_dir is None:
        output_file = r_output_file
    else:
        output_file = os.path.join(options.output_dir, r_output_file)

    # Load simulation data
    sim_data = load_sim_data(input_outdir)

    # Create plot
    create_plot(codebase, mag, vel, model, dist, mech, sim_data, output_file)

    print("All Done!")

### Main
run()
