Skip to content

Commit 2feb914

Browse files
authored
Imported a few libraries and corrected minor errors to run the mapping_GEAR1 function (#259)
1 parent c13a77e commit 2feb914

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

csep/utils/readers.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# Third-party imports
1111
import numpy
12+
import pandas as pd
1213

1314
# PyCSEP imports
1415
from csep.utils.time_utils import strptime_to_utc_datetime, \
@@ -18,6 +19,7 @@
1819
from csep.utils.iris import gcmt_search
1920
from csep.core.regions import QuadtreeGrid2D
2021
from csep.core.exceptions import CSEPIOException
22+
from csep.core.regions import CartesianGrid2D
2123

2224

2325
def ndk(filename):
@@ -901,11 +903,11 @@ def mapping_GEAR1(GEAR1_file, area_file, grid_file, b_value=False):
901903
902904
grid_file (str): Two-column, n-row array containing the centered longitude and latitude
903905
coordinates of all the cells (with spatial resolution of 0.1 x 0.1) that
904-
make up the new testing region. E.g. 25.15, 33.25
905-
25.25, 33.25
906-
25.35, 33.25
907-
Each lon lat coordinate must be a two-decimal floating number ending in 5,
908-
i.e., the cell midpoint, as seen above.
906+
make up the new testing region. E.g. 25.15 33.25
907+
25.25 33.25
908+
25.35 33.25
909+
Each lon lat coordinate, separated by single blank space, must be a two-
910+
decimal floating number ending in 5, i.e., the cell midpoint, as seen above.
909911
910912
b_value (float): If modellers wish to extroplate GEAR1 M 5.95+ earthquake rates to a lower
911913
magnitude threshold, they must provide a generic b value of the region.
@@ -915,7 +917,7 @@ def mapping_GEAR1(GEAR1_file, area_file, grid_file, b_value=False):
915917
b_value variable should remain False.
916918
917919
Returns
918-
GEAR1_region.dat: Input text file containing GEAR 1 earthquake rates in cells defined within
920+
GEAR1_region.dat: Input text file containing GEAR1 earthquake rates in cells defined within
919921
the desired geographic region. This file feeds a so-called read_GEAR1_format
920922
function, which translates the format in which the GEAR1 forecasts were
921923
originally provided into a pyCSEP-friendly format.
@@ -928,8 +930,8 @@ def mapping_GEAR1(GEAR1_file, area_file, grid_file, b_value=False):
928930
929931
"""
930932
print ('Reading data...')
931-
bulk_dataW = np.loadtxt(GEAR1_file, skiprows=1, delimiter=',')
932-
bulk_areaW = np.loadtxt(area_file, skiprows=1, delimiter=',')
933+
bulk_dataW = numpy.loadtxt(GEAR1_file, skiprows=1, delimiter=',')
934+
bulk_areaW = numpy.loadtxt(area_file, skiprows=1, delimiter=',')
933935

934936
# This part of the code is aimed to ensure that all lon and lat coordinates are two-digits floating
935937
# numbers. This is important, because the projection of GEAR1 onto a geographical region is basically
@@ -939,8 +941,8 @@ def mapping_GEAR1(GEAR1_file, area_file, grid_file, b_value=False):
939941
longitudesW = []
940942

941943
for i in range(len(bulk_dataW)):
942-
longitudesW.append(np.float('%.2f' % round(bulk_dataW[:,0][i],2)))
943-
latitudesW.append(np.float('%.2f' % round(bulk_dataW[:,1][i],2)))
944+
longitudesW.append(float('%.2f' % round(bulk_dataW[:,0][i],2)))
945+
latitudesW.append(float('%.2f' % round(bulk_dataW[:,1][i],2)))
944946

945947
# This is the first Pandas data frame when no extrapolations are needed:
946948
if not b_value:
@@ -1038,14 +1040,14 @@ def mapping_GEAR1(GEAR1_file, area_file, grid_file, b_value=False):
10381040
longitudesW = []
10391041

10401042
# This is the second Pandas data frame:
1041-
bulk_dataR = np.loadtxt(grid_file, skiprows=0, delimiter=' ')
1043+
bulk_dataR = numpy.loadtxt(grid_file, skiprows=0, delimiter=' ')
10421044

10431045
grid_longitudes = []
10441046
grid_latitudes = []
10451047

10461048
for i in range(len(bulk_dataR)):
1047-
grid_longitudes.append(np.float('%.2f' % round(bulk_dataR[:,0][i],2)))
1048-
grid_latitudes.append(np.float('%.2f' % round(bulk_dataR[:,1][i],2)))
1049+
grid_longitudes.append(float('%.2f' % round(bulk_dataR[:,0][i],2)))
1050+
grid_latitudes.append(float('%.2f' % round(bulk_dataR[:,1][i],2)))
10491051

10501052
polygon = pd.DataFrame()
10511053
polygon['longitude'] = grid_longitudes
@@ -1080,13 +1082,13 @@ def read_GEAR1_format(filename, area_filename, magnitudes):
10801082
Returns:
10811083
:class:`csep.core.forecasts.GriddedForecast`
10821084
"""
1083-
t0 = time.time()
1084-
bulk_data = np.loadtxt(filename, skiprows=1, delimiter=',')
1085+
1086+
bulk_data = numpy.loadtxt(filename, skiprows=1, delimiter=',')
10851087

10861088
# Construction of the testing region:
10871089
lons = bulk_data[:,1]
10881090
lats = bulk_data[:,2]
1089-
coords = np.column_stack([lons, lats])
1091+
coords = numpy.column_stack([lons, lats])
10901092

10911093
# Coordinates are given as midpoints origin should be in the 'lower left' corner:
10921094
r = CartesianGrid2D.from_origins(coords, magnitudes=magnitudes)
@@ -1095,16 +1097,16 @@ def read_GEAR1_format(filename, area_filename, magnitudes):
10951097
bulk_data_no_coords = bulk_data[:, 3:]
10961098

10971099
# Original GEAR1 format provides cumulative rates per meter**2
1098-
incremental_yrly_density = np.diff(np.fliplr(bulk_data_no_coords))
1100+
incremental_yrly_density = numpy.diff(numpy.fliplr(bulk_data_no_coords))
10991101

11001102
# Computing the differences, but returning array with the same shape:
1101-
incremental_yrly_density = np.column_stack([np.fliplr(incremental_yrly_density), bulk_data_no_coords[:,-1]])
1103+
incremental_yrly_density = numpy.column_stack([numpy.fliplr(incremental_yrly_density), bulk_data_no_coords[:,-1]])
11021104

11031105
# Read in area to denormalize back onto csep grid
1104-
area = np.loadtxt(area_filename, skiprows=1, delimiter=',')
1106+
area = numpy.loadtxt(area_filename, skiprows=1, delimiter=',')
11051107

11061108
# Allows to use broadcasting
1107-
m2_per_cell = np.reshape(area[:,-1], (len(area[:,1]), 1))
1109+
m2_per_cell = numpy.reshape(area[:,-1], (len(area[:,1]), 1))
11081110
incremental_yrly_rates = incremental_yrly_density * m2_per_cell
11091111

11101112
return incremental_yrly_rates, r, magnitudes

0 commit comments

Comments
 (0)