Skip to content

Commit d13f74f

Browse files
authored
quadtree csv reader (#186)
* quadtree csv reader * included depth columns in reader * reader name change * cartesian_grid.bounds added * check for zero area
1 parent ad217ed commit d13f74f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

csep/core/regions.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,9 @@ def __init__(self, polygons, dh, name='cartesian2d', mask=None):
517517
# index values of polygons array into the 2d cartesian grid, based on the midpoint.
518518
self.xs = xs
519519
self.ys = ys
520+
# Bounds [origin, top_right]
521+
orgs = self.origins()
522+
self.bounds = numpy.column_stack((orgs, orgs + dh))
520523

521524
def __eq__(self, other):
522525
return self.to_dict() == other.to_dict()
@@ -772,13 +775,16 @@ def geographical_area_from_bounds(lon1, lat1, lon2, lat2):
772775
Returns:
773776
Area of cell in Km2
774777
"""
775-
earth_radius_km = 6371.
776-
R2 = earth_radius_km ** 2
777-
rad_per_deg = numpy.pi / 180.0e0
778+
if lon1 == lon2 or lat1 == lat2:
779+
return 0
780+
else:
781+
earth_radius_km = 6371.
782+
R2 = earth_radius_km ** 2
783+
rad_per_deg = numpy.pi / 180.0e0
778784

779-
strip_area_steradian = 2 * numpy.pi * (1.0e0 - numpy.cos((90.0e0 - lat1) * rad_per_deg)) \
785+
strip_area_steradian = 2 * numpy.pi * (1.0e0 - numpy.cos((90.0e0 - lat1) * rad_per_deg)) \
780786
- 2 * numpy.pi * (1.0e0 - numpy.cos((90.0e0 - lat2) * rad_per_deg))
781-
area_km2 = strip_area_steradian * R2 / (360.0 / (lon2 - lon1))
787+
area_km2 = strip_area_steradian * R2 / (360.0 / (lon2 - lon1))
782788
return area_km2
783789

784790
def quadtree_grid_bounds(quadk):

csep/utils/readers.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def _parse_datetime_to_zmap(date, time):
719719
out['second'] = dt.second
720720
return out
721721

722-
def load_quadtree_forecast(ascii_fname):
722+
def quadtree_ascii_loader(ascii_fname):
723723
""" Load quadtree forecasted stored as ascii text file
724724
725725
Note: This function is adapted form csep.forecasts.load_ascii
@@ -756,4 +756,33 @@ def load_quadtree_forecast(ascii_fname):
756756
# reshape rates into correct 2d format
757757
rates = data[:, -1].reshape(n_poly, n_mag_bins)
758758

759+
return rates, region, mws
760+
761+
762+
def quadtree_csv_loader(csv_fname):
763+
""" Load quadtree forecasted stored as csv file
764+
765+
The format expects forecast as a comma separated file, in which first column corresponds to quadtree grid cell (quadkey).
766+
The second and thrid columns indicate depth range.
767+
The corresponding enteries in the respective row are forecast rates corresponding to the magnitude bins.
768+
The first line of forecast is a header, and its format is listed here:
769+
'Quadkey', depth_min, depth_max, Mag_0, Mag_1, Mag_2, Mag_3 , ....
770+
Quadkey is a string. Rest of the values are floats.
771+
For the purposes of defining region objects quadkey is used.
772+
773+
We assume that the starting value of magnitude bins are provided in the header.
774+
Args:
775+
csv_fname: file name of csep forecast in csv format
776+
Returns:
777+
rates, region, mws (numpy.ndarray, QuadtreeRegion2D, numpy.ndarray): rates, region, and magnitude bins needed
778+
to define QuadTree forecasts
779+
"""
780+
781+
data = numpy.genfromtxt(csv_fname, dtype='str', delimiter=',')
782+
quadkeys = data[1:, 0]
783+
mws = data[0, 3:]
784+
rates = data[1:, 3:]
785+
rates = rates.astype(float)
786+
region = QuadtreeGrid2D.from_quadkeys(quadkeys, magnitudes=mws)
787+
759788
return rates, region, mws

0 commit comments

Comments
 (0)