def analyse_regions(self, array_path): """ receives a path to an array of quakes object prints how many quakes there are outside a region that caused shocks on that region """ # auxiliary object catalog = Catalog() # get earthquake array quakes = pickle.load(open(array_path, 'rb')) # analyse kanto kanto_bounds = catalog.get_kanto_bounds() self.analyse_region(quakes, kanto_bounds, 'kanto') # analyse kansai kansai_bounds = catalog.get_kansai_bounds() self.analyse_region(quakes, kansai_bounds, 'kansai') # analyse tohoku tohoku_bounds = catalog.get_tohoku_bounds() self.analyse_region(quakes, tohoku_bounds, 'tohoku') # analyse east_japan east_japan_bounds = catalog.get_east_japan_bounds() self.analyse_region(quakes, east_japan_bounds, 'east_japan')
def study_slc(self): """ receives nothing apply single link to all regions of japan saves the results on pickle objects """ #defines COMPLETE_CATALOG = "../catalogs/new_jma.txt" TEMP_CATALOG1 = '../catalogs/temp1.txt' TEMP_CATALOG2 = '../catalogs/temp2.txt' DEBUG = '../catalogs/reduced_jma.txt' BOUND = 0 MAG_THRESH = 1 # auxiliary classes needed cat = Catalog() slc1 = Single_Link() # distance threshold, the distance to use when the distance calculated # by the single link cluster returns negative dist_thresh = [12.0] # dictionary containing information of the quakes and region names regions = {} regions["kanto"] = [cat.get_kanto_bounds(), 0.0] #regions["kansai"] = [cat.get_kansai_bounds(), 0.0] regions["tohoku"] = [cat.get_tohoku_bounds(), 0.0] regions["east_japan"] = [cat.get_east_japan_bounds(), 0.0] ## try to run for the whole japan catalog #japan_bound = [0.0, 400.0, 0.0, 400.0] #regions["japan"] = [japan_bound, 0.0] # iterate throgh every region for region, info in regions.items(): for thresh in dist_thresh: # obtain catalog for the region cat.select_geographically(info[BOUND], COMPLETE_CATALOG, TEMP_CATALOG1) cat.get_mag_threshold(TEMP_CATALOG1, TEMP_CATALOG2, info[MAG_THRESH], index = 2) # apply single link to the catalog and get quake #quakes = slc1.apply_single_link(TEMP_CATALOG2, region, thresh) # save array as a pickle object in the correct location # save declustered quake array using pickle #path = 'data_persistence/declustered_array_slc_' + region + '_' + str(round(thresh, 3)) #file_aux1 = open(path, 'wb') #pickle.dump(quakes, file_aux1) # classify quakes by making the centroid the medoid of the cluster slc1.classify_quakes(path, 'closest') quake_path = path + '_classified_medoid' # clean up call(["rm", TEMP_CATALOG1, TEMP_CATALOG2])
def plot_histograms(self, folder): """ receives a folder, corresponding to the path of the method we're using ex: folder = '../results/window_method/' plot histograms showing how much quakes have occurred on each year for the regions of kanto, kansai, tohoku and east japan """ # auxiliar variable catalog = Catalog() # get bounds so to include all the japan bounds = [0.0, 360.0, 0.0, 360.0] # plot histogram for all japan, considering only mainshocks self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'japan_mainshocks.jpg', 'Mainshocks by year on japan region', mainshocks=True) # plot histogram for japan, considering everything self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'japan_all.jpg', 'Quakes by year on japan region', mainshocks=False) # get bounds of kanto bounds = catalog.get_kanto_bounds() # plot histogram for kanto, considering only mainshocks self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'kanto_mainshocks.jpg', 'Mainshocks by year on kanto region', mainshocks=True) # plot histogram for kanto, considering everything self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'kanto_all.jpg', 'Quakes by year on kanto region', mainshocks=False) # get bounds of kansai bounds = catalog.get_kansai_bounds() # plot histogram for kansai, considering only mainshocks self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'kansai_mainshocks.jpg', 'Mainshocks by year on kansai region', mainshocks=True) # plot histogram for kansai, considering everything self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'kansai_all.jpg', 'Quakes by year on kansai region', mainshocks=False) # get bounds of tohoku bounds = catalog.get_tohoku_bounds() # plot histogram for tohoku, considering only mainshocks self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'tohoku_mainshocks.jpg', 'Mainshocks by year on tohoku region', mainshocks=True) # plot histogram for tohoku, considering everything self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'tohoku_all.jpg', 'Quakes by year on tohoku region', mainshocks=False) # get bounds of east_japan bounds = catalog.get_east_japan_bounds() # plot histogram for east_japan, considering only mainshocks self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'east_japan_mainshocks.jpg', 'Mainshocks by year on east_japan region', mainshocks=True) # plot histogram for east_japan, considering everything self.show_quakes_by_year(folder + 'classified_quakes.txt', bounds, folder + 'east_japan_all.jpg', 'Quakes by year on east_japan region', mainshocks=False)
def get_region_bounds(region): cat = Catalog() if region == "kanto": return cat.get_kanto_bounds() elif region == "kansai": return cat.get_kansai_bounds() elif region == "tohoku": return cat.get_tohoku_bounds() elif region == "east_japan": return cat.get_east_japan_bounds() else: exit("region not known")
def get_catalogs(self): """ reads each quake array, obtain correspondent catalog for each quake array in the end, unite all catalogs in one big catalog - jma style - """ # defines CATALOG_READ = '../catalogs/jma.txt' # auxiliary classes needed cat = Catalog() slc = Single_Link() # get dictionary, containing for a given region the region bounds bounds = {} bounds["kanto"] = cat.get_kanto_bounds() bounds["kansai"] = cat.get_kansai_bounds() bounds["tohoku"] = cat.get_tohoku_bounds() bounds["east_japan"] = cat.get_east_japan_bounds() # obtain catalog for each region for region, bound in bounds.items(): catalog_write = '../results/single_link/' + region + '_allshocks.txt' cat.select_geographically(bound, CATALOG_READ, catalog_write) # for each region, classify quakes for region, bound in bounds.items(): quakes_path = 'data_persistence/declustered_array_slc_' + region slc.classify_quakes(quakes_path, 'closest') # for each region for region, bound in bounds.items(): # obtain quakes path, catalog to read from and catalog to write quakes_path = 'data_persistence/declustered_array_slc_' + region + '_classified_medoid' catalog_read = '../results/single_link/' + region + '_allshocks.txt' catalog_write = '../results/single_link/' + region + '_classified.txt' slc.get_catalog_mainshocks(quakes_path, catalog_read, catalog_write) # merge catalog in a big one call(["cat ../results/single_link/*classified.txt > ../results/single_link/temp.txt"], shell = True) # sort file by columns - firts the year, then the month and so it goes call(['sort -n -k 3,3 -n -k 4,4 -n -k 5,5 -n -k 8,8 -n -k 9,9 -n -k 10,10 ../results/single_link/temp.txt \ > ../results/single_link/temp2.txt'], shell = True) # remove duplicate lines call(['uniq -u ../results/single_link/temp2.txt > ../results/single_link/regions_classified.txt'], shell = True) # remove temporary files call(['rm ../results/single_link/temp*.txt'], shell=True)
def study_chisquare(self, folder, array_path, days, alpha, mag_threshold=None): """ receives a folder to look for the quakes, the path where the earthquake array is located, the number of days, a significance level and optionally a magnitude threshold performs a chisquare test for the 4 regions of japan and all the japan by taking the observed frequencies in that number of days """ # auxiliar classes needed catalog = Catalog() stat = Statistic() # calculate the number of seconds to be done on the interval time_interval = days * 24.0 * 360.0 # generate catalog in a good format to work with quakes = pickle.load(open(array_path, 'rb')) catalog.record_mainshocks(quakes, folder + 'quakes.txt', '../catalogs/new_jma.txt') # get the core catalog name, and obtain it if mag_threshold == None: CATALOG_EXTENSION = 'quakes.txt' else: CATALOG_EXTENSION = 'quakes_' + str(mag_threshold) + '.txt' catalog.get_mag_threshold(folder + 'quakes.txt', folder + CATALOG_EXTENSION ,mag_threshold) # do chi square test for all japan, with aftershocks print("Doing chi-square test for all japan, including aftershocks on analysis:") stat.do_chi_square(folder + CATALOG_EXTENSION, 0.05, time_interval) # do chi square test for all japan, without aftershocks print("\nDoing chi-square test for all japan, excluding aftershocks on analysis") catalog.get_mainshocks(folder + 'japan_mainshocks.txt', folder + CATALOG_EXTENSION, 5) stat.do_chi_square(folder + 'japan_mainshocks.txt', 0.05, time_interval) # get bounds for kanto bounds = catalog.get_kanto_bounds() # construct a catalog for quakes that happened in kanto catalog.select_geographically(bounds, folder + CATALOG_EXTENSION, folder + 'quakes_kanto.txt') # do chi_squared test for kanto, with aftershocks print("\nDoing chi-square test for kanto, including aftershocks on analysis:") stat.do_chi_square(folder + 'quakes_kanto.txt', 0.05, time_interval) # do chi_squared test for kanto, without aftershocks print("\nDoing chi-square test for kanto, excluding aftershocks on analysis") catalog.get_mainshocks(folder + 'mainshocks_kanto.txt', folder + 'quakes_kanto.txt', 5) stat.do_chi_square(folder + 'mainshocks_kanto.txt', 0.05, time_interval) # get bounds for kansai bounds = catalog.get_kansai_bounds() # construct a catalog for quakes that happened in kansai catalog.select_geographically(bounds, folder + CATALOG_EXTENSION, folder + 'quakes_kansai.txt') # do chi_squared test for kansai, with aftershocks print("\nDoing chi-square test for kansai, including aftershocks on analysis:") stat.do_chi_square(folder + 'quakes_kansai.txt', 0.05, time_interval) # do chi_squared test for kansai, without aftershocks print("\nDoing chi-square test for kansai, excluding aftershocks on analysis") catalog.get_mainshocks(folder + 'mainshocks_kansai.txt', folder + 'quakes_kansai.txt', 5) stat.do_chi_square(folder + 'mainshocks_kansai.txt', 0.05, time_interval) # get bounds for tohoku bounds = catalog.get_tohoku_bounds() # construct a catalog for quakes that happened in tohoku catalog.select_geographically(bounds, folder + CATALOG_EXTENSION, folder + 'quakes_tohoku.txt') # do chi_squared test for tohoku, with aftershocks print("\nDoing chi-square test for tohoku, including aftershocks on analysis:") stat.do_chi_square(folder + 'quakes_tohoku.txt', 0.05, time_interval) # do chi_squared test for tohoku, without aftershocks print("\nDoing chi-square test for tohoku, excluding aftershocks on analysis") catalog.get_mainshocks(folder + 'mainshocks_tohoku.txt', folder + 'quakes_tohoku.txt', 5) stat.do_chi_square(folder + 'mainshocks_tohoku.txt', 0.05, time_interval) # get bounds for east_japan bounds = catalog.get_east_japan_bounds() # construct a catalog for quakes that happened in east_japan catalog.select_geographically(bounds, folder + CATALOG_EXTENSION, folder + 'quakes_east_japan.txt') # do chi_squared test for east_japan, with aftershocks print("\nDoing chi-square test for east_japan, including aftershocks on analysis:") stat.do_chi_square(folder + 'quakes_east_japan.txt', 0.05, time_interval) # do chi_squared test for east_japan, without aftershocks print("\nDoing chi-square test for east_japan, excluding aftershocks on analysis") catalog.get_mainshocks(folder + 'mainshocks_east_japan.txt', folder + 'quakes_east_japan.txt', 5) stat.do_chi_square(folder + 'mainshocks_east_japan.txt', 0.05, time_interval)
def show_divide_quakes(self, array_path): """ receives a path to an array of quakes counts how many quakes happened in each of the sets: set 1 - quakes that happened in kanto set 2 - quakes that happened in kansai but not in set 1 set 3 - quakes that happened in tohoku but not in set 1 and 2 set 4 - quakes that happened in east japan but not in set 1, 2 and 3 set 5 - quakes that didn't happen in sets 1, 2, 3 and 4 """ # obtain earthquake array quakes = pickle.load(open(array_path, 'rb')) # get bounds for all regions catalog = Catalog() kanto_bounds = catalog.get_kanto_bounds() kansai_bounds = catalog.get_kansai_bounds() tohoku_bounds = catalog.get_tohoku_bounds() east_japan_bounds = catalog.get_east_japan_bounds() # set variables to zero num_set1 = 0 num_set2 = 0 num_set3 = 0 num_set4 = 0 num_set5 = 0 # iterate through the array for index in range(len(quakes)): # get current quake cur_quake = quakes[index] # if the current quake happened in kanto if cur_quake.is_on_region(kanto_bounds): # increment appropriate variable num_set1 += 1 # go to the next quake continue # if the current quake happened in kansai if cur_quake.is_on_region(kansai_bounds): # increment appropriate variable num_set2 += 1 # go to the next quake continue # if the current quake happened in tohoku if cur_quake.is_on_region(tohoku_bounds): # increment appropriate variable num_set3 += 1 # go to the next quake continue # if the current quake happened in east_japan if cur_quake.is_on_region(east_japan_bounds): # increment appropriate variable num_set4 += 1 # go to the next quake continue # finally, increment set that does not contain every quake else: num_set5 += 1 # print results print("quakes that happened in kanto: ", num_set1) print("quakes that happened in kansai but not in kanto: ", num_set2) print("quakes that happened in tohoku, but not in kanto/kansai: ", num_set3) print("quakes that happened in east japan, but not in tohoku/kanto/kansai: ", num_set4) print("quakes that didn't happen in any of the previous for regions", num_set5)
def plot_heat_all(self, array_path, dep_lim = None, map_background = True): """ receives the path to a serialized array object optionally receives a depth limit and a map background boolean, to indicate wheter we use the map as background or the coast as background in order to study the mainshocks plot heat map for all regions """ # obtain array of shocks within the depth limit all_quakes = pickle.load(open(array_path, 'rb')) useful_quakes = [] for index in range(len(all_quakes)): current_quake = all_quakes[index] if dep_lim != None and current_quake.depth > dep_lim: continue useful_quakes.append(current_quake) # get instance of classes that we'll need methods draw = Draw() cat = Catalog() # get dictionary that for every region name gives bound and zoom for all regions regions = {} regions["japan"] = [cat.get_catalog_bounds('../catalogs/new_jma.txt'), 5, 100] regions["kanto"] = [cat.get_kanto_bounds(), 9, 25] regions["kansai"] = [cat.get_kansai_bounds(), 9, 25] regions["tohoku"] = [cat.get_tohoku_bounds(), 9, 25] regions["east_japan"] = [cat.get_east_japan_bounds(), 9, 25] # index to add legibility to the code BOUNDS = 0 ZOOM = 1 BINS = 2 # set time bound on which to plot and which years to plot year_bound = [[0.0, 12*366*24.0*3600]] # this way all years are considered # iterate through the years for time in year_bound: # iterate through the regions for region, info in regions.items(): # if we are using a map as background if map_background: # plot image for the region draw.plot_image_quakes(useful_quakes, time, info[BOUNDS], info[ZOOM]) call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", region + "_back_heat"]) call(["mv", region + "_back_heat-1.png", "../images/"]) call(["rm", "Rplots.pdf"]) input("Edit the image and then press Enter") # plot heat map for the region in the given year self.plot_heat(useful_quakes, time, info[BOUNDS], info[BINS], "../images/" + region + "_back_heat-1.png",\ only_main = False) # save it in the correct format, and do cleaning call(["mv", "temp.png", "../images/" + region + "_heat.png"]) else: # plot image for the current region draw.plot_quakes_coast(useful_quakes, time, info[BOUNDS], dep_lim = dep_lim) call(["mv", "temp.png", "../images/" + region + "_back_heat_coast-1.png"]) input("Edit the image and then press Enter") # plot heat map for the region in the given year self.plot_heat(useful_quakes, time, info[BOUNDS], info[BINS], "../images/" + region + "_back_heat_coast-1.png",\ only_main = False) # save it in the correct format, and do cleaning call(["mv", "temp.png", "../images/" + region + "_heat_coast.png"])
def plot_clusters_all(self, array_path, dep_lim = None, map_background = True): """ receives a path to the array of quakes. optionally it receives a depth limit and a map_background boolean if map_background = True, then the cluster is plot with a background map if map_background = False, then the cluster is plot with the coast of japan as background in order to plot study the mainshocks and it's associated clusters, plot some clusters on the map """ # obtain array of shocks within the depth limit all_quakes = pickle.load(open(array_path, 'rb')) useful_quakes = [] for index in range(len(all_quakes)): current_quake = all_quakes[index] if dep_lim != None and current_quake.depth > dep_lim: continue useful_quakes.append(current_quake) # get instance of classes so we can access methods draw = Draw() cat = Catalog() # get dictionary that for every region name gives bound and zoom for all regions regions = {} regions["japan"] = [cat.get_catalog_bounds('../catalogs/new_jma.txt'), 5] regions["kanto"] = [cat.get_kanto_bounds(), 9] regions["kansai"] = [cat.get_kansai_bounds(), 9] regions["tohoku"] = [cat.get_tohoku_bounds(), 9] regions["east_japan"] = [cat.get_east_japan_bounds(), 9] # index to add legibility to the code BOUNDS = 0 ZOOM = 1 # set list of how many clusters to plot, and which years to plot num_clusters = [10] year_bound = [[0.0, 12*366*24.0*3600]] # this way we consider all years # iterate through the list containing how many clusters we plot for num in num_clusters: # iterate through the years for time in year_bound: # iterate through the regions for region, info in regions.items(): # if we need to plot a map in the background if map_background: # plot image for the current region draw.plot_image_quakes(useful_quakes, time, info[BOUNDS], info[ZOOM], num_clusters = num, dep_lim = dep_lim) call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", region + "_back"]) call(["mv", region + "_back-1.png", "../images/"]) call(["rm", "Rplots.pdf"]) input("Edit the image and then press Enter") # plot cluster for current region in the given year, for the current number of clusters self.plot_clusters(useful_quakes, num, time, info[BOUNDS], "../images/" + region +"_back-1.png") ## save it in the correct format, and do cleaning call(["mv", "temp.png", "../images/" + region + "_clusters_map.png"]) # if we dont want to plot a japan map in the background else: # plot image for the current region draw.plot_quakes_coast(useful_quakes, time, info[BOUNDS], num_clusters = num, dep_lim = dep_lim) call(["mv", "temp.png", "../images/" + region + "_back_coast-1.png"]) input("Edit the image and then press Enter") # plot cluster for current region in the given year, for the current number of clusters self.plot_clusters(useful_quakes, num, time, info[BOUNDS], "../images/" + region +"_back_coast-1.png") ## save it in the correct format, and do cleaning call(["mv", "temp.png", "../images/" + region + "_clusters_coast.png"])
def draw_all_maps(self): """ draw the maps for the japan catalog, kanto region kansai region, tohoku region and east japan region """ # get a catalog cat = Catalog(); # get japan bounds bounds = cat.get_catalog_bounds("../catalogs/new_jma.txt") # draw map for japan self.draw_map(bounds, 5) # convert image to png, save it on images folder and removes the pdf call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", "japan"]) call(["cp", "japan-1.png", "../images/"]) call(["rm", "japan-1.png"]) call(["rm", "Rplots.pdf"]) # get kanto bounds bounds = cat.get_kanto_bounds() # draw map for kanto self.draw_map(bounds, 9) # convert image to png, save it on images folder and removes the pdf call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", "kanto"]) call(["cp", "kanto-1.png", "../images/"]) call(["rm", "kanto-1.png"]) call(["rm", "Rplots.pdf"]) # get kansai bounds bounds = cat.get_kansai_bounds() # draw map for kansai self.draw_map(bounds, 9) # convert image to png, save it on images folder and removes the pdf call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", "kansai"]) call(["cp", "kansai-1.png", "../images/"]) call(["rm", "kansai-1.png"]) call(["rm", "Rplots.pdf"]) # get tohoku bounds bounds = cat.get_tohoku_bounds() # draw map for tohoku self.draw_map(bounds, 8) # convert image to png, save it on images folder and removes the pdf call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", "tohoku"]) call(["cp", "tohoku-1.png", "../images/"]) call(["rm", "tohoku-1.png"]) call(["rm", "Rplots.pdf"]) # get east_japan bounds bounds = cat.get_east_japan_bounds() # draw map for east_japan self.draw_map(bounds, 8) # convert image to png, save it on images folder and removes the pdf call(["pdftoppm", "-rx", "300", "-ry", "300", "-png", "Rplots.pdf", "east_japan"]) call(["cp", "east_japan-1.png", "../images/"]) call(["rm", "east_japan-1.png"]) call(["rm", "Rplots.pdf"])