Source code for openquake.hazardlib.geo.surface.planar
# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2012-2023 GEM Foundation## OpenQuake is free software: you can redistribute it and/or modify it# under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## OpenQuake is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>."""Module :mod:`openquake.hazardlib.geo.surface.planar` contains:class:`PlanarSurface`."""importmathimportloggingimportnumpyfromopenquake.baselib.nodeimportNodefromopenquake.baselib.performanceimportnumba,compilefromopenquake.hazardlib.geo.geodeticimport(point_at,spherical_to_cartesian,fast_spherical_to_cartesian)fromopenquake.hazardlib.geoimportPoint,Linefromopenquake.hazardlib.geo.surface.baseimportBaseSurfacefromopenquake.hazardlib.geo.meshimportMeshfromopenquake.hazardlib.geoimportgeodeticfromopenquake.hazardlib.geo.nodalplaneimportNodalPlanefromopenquake.hazardlib.geoimportutilsasgeo_utils# Maximum difference in surface's rectangle side lengths, maximum offset# of a bottom right corner from a plane that contains other corners,# as well as maximum offset of a bottom left corner from a line drawn# downdip perpendicular to top edge from top left corner, expressed# as a fraction of the surface's area.IMPERFECT_RECTANGLE_TOLERANCE=0.002planar_array_dt=numpy.dtype([('corners',(float,4)),('xyz',(float,4)),('normal',float),('uv1',float),('uv2',float),('wlr',float),('sdr',float),('hypo',float)])planin_dt=numpy.dtype([('mag',float),('strike',float),('dip',float),('rake',float),('rate',float),('lon',float),('lat',float),('dims',(float,3)),])@compile("(f8[:, :], f8, f8, f8, f8[:], f8, f8, f8, f8, f8, f8)")def_update(corners,usd,lsd,mag,dims,strike,dip,rake,clon,clat,cdep):# from the rupture center we can now compute the coordinates of the# four coorners by moving along the diagonals of the plane. This seems# to be better then moving along the perimeter, because in this case# errors are accumulated that induce distorsions in the shape with# consequent raise of exceptions when creating PlanarSurface objects# theta is the angle between the diagonal of the surface projection# and the line passing through the rupture center and parallel to the# top and bottom edges. Theta is zero for vertical ruptures (because# rup_proj_width is zero)half_length,half_width,half_height=dims/2.rdip=math.radians(dip)# precalculated azimuth values for horizontal-only and vertical-only# moves from one point to another on the plane defined by strike# and dip:azimuth_right=strikeazimuth_down=azimuth_right+90azimuth_left=azimuth_down+90azimuth_up=azimuth_left+90# half height of the vertical component of rupture width# is the vertical distance between the rupture geometrical# center and it's upper and lower borders:# calculate how much shallower the upper border of the rupture# is than the upper seismogenic depth:vshift=usd-cdep+half_height# if it is shallower (vshift > 0) than we need to move the rupture# by that value vertically.ifvshift<0:# the top edge is below upper seismogenic depth. now we need# to check that we do not cross the lower border.vshift=lsd-cdep-half_heightifvshift>0:# the bottom edge of the rupture is above the lower seismo# depth; that means that we don't need to move the rupture# as it fits inside seismogenic layer.vshift=0# if vshift < 0 than we need to move the rupture up.# now we need to find the position of rupture's geometrical center.# in any case the hypocenter point must lie on the surface, however# the rupture center might be off (below or above) along the dip.ifvshift!=0:# we need to move the rupture center to make the rupture fit# inside the seismogenic layer.hshift=abs(vshift/math.tan(rdip))clon,clat=geodetic.fast_point_at(clon,clat,azimuth_upifvshift<0elseazimuth_down,hshift)cdep+=vshifttheta=math.degrees(math.atan(half_width/half_length))hor_dist=math.sqrt(half_length**2+half_width**2)corners[0,:2]=geodetic.fast_point_at(clon,clat,strike+180+theta,hor_dist)corners[1,:2]=geodetic.fast_point_at(clon,clat,strike-theta,hor_dist)corners[2,:2]=geodetic.fast_point_at(clon,clat,strike+180-theta,hor_dist)corners[3,:2]=geodetic.fast_point_at(clon,clat,strike+theta,hor_dist)corners[0:2,2]=cdep-half_heightcorners[2:4,2]=cdep+half_heightcorners[4,0]=strikecorners[4,1]=dipcorners[4,2]=rakecorners[5,0]=cloncorners[5,1]=clatcorners[5,2]=cdep# numbified below, ultrafast
ifnumba:F8=numba.float64build_corners=compile(F8[:,:,:,:,:](F8,# usdF8,# lsdF8[:,:],# magF8[:,:,:],# dimsF8[:,:],# strikeF8[:,:],# dipF8[:,:],# rakeF8[:,:],# hddF8,# lonF8,# lat))(build_corners)# not numbified but fast anyway
[docs]defbuild_planar(planin,hdd,lon,lat,usd,lsd):""" :param planin: Surface input parameters as an array of shape (M, N) :param lon, lat: Longitude and latitude of the hypocenters (scalars) :parameter deps: Depths of the hypocenters (vector) :return: an array of shape (M, N, D, 3) """corners=build_corners(usd,lsd,planin.mag,planin.dims,planin.strike,planin.dip,planin.rake,hdd,lon,lat)planar_array=build_planar_array(corners[:4],corners[4],corners[5])ford,(drate,dep)inenumerate(hdd):planar_array.wlr[:,:,d,2]=planin.rate*dratereturnplanar_array
[docs]defbuild_planar_array(corners,sdr=None,hypo=None,check=False):""" :param corners: array of shape (4, M, N, D, 3) :param hypo: None or array of shape (M, N, D, 3) :returns: a planar_array array of length (M, N, D, 3) """shape=corners.shape[:-1]# (4, M, N, D)planar_array=numpy.zeros(corners.shape[1:],planar_array_dt).view(numpy.recarray)ifsdrisnotNone:planar_array['sdr']=sdr# strike, dip, rakeifhypoisnotNone:planar_array['hypo']=hypotl,tr,bl,br=xyz=spherical_to_cartesian(corners[...,0],corners[...,1],corners[...,2])fori,cornerinenumerate(corners):planar_array['corners'][...,i]=cornerplanar_array['xyz'][...,i]=xyz[i]# these two parameters define the plane that contains the surface# (in 3d Cartesian space): a normal unit vector,planar_array['normal']=n=geo_utils.normalized(numpy.cross(tl-tr,tl-bl))# these two 3d vectors together with a zero point represent surface's# coordinate space (the way to translate 3d Cartesian space with# a center in earth's center to 2d space centered in surface's top# left corner with basis vectors directed to top right and bottom left# corners. see :meth:`_project`.planar_array['uv1']=uv1=geo_utils.normalized(tr-tl)planar_array['uv2']=uv2=numpy.cross(n,uv1)# translate projected points to surface coordinate space, shape (N, 3)delta=xyz-xyz[0]dists,xx,yy=numpy.zeros(shape),numpy.zeros(shape),numpy.zeros(shape)foriinrange(1,4):mat=delta[i]dists[i],xx[i],yy[i]=dot(mat,n),dot(mat,uv1),dot(mat,uv2)# "length" of the rupture is measured along the top edgelength1,length2=xx[1]-xx[0],xx[3]-xx[2]# "width" of the rupture is measured along downdip directionwidth1,width2=yy[2]-yy[0],yy[3]-yy[1]width=(width1+width2)/2.0length=(length1+length2)/2.0wlr=planar_array['wlr']wlr[...,0]=widthwlr[...,1]=lengthifcheck:# calculate the imperfect rectangle tolerance# relative to surface's areadists=(xyz-tl)@ntolerance=width*length*IMPERFECT_RECTANGLE_TOLERANCEifnumpy.abs(dists).max()>tolerance:logging.warning("corner points do not lie on the same plane")iflength2<0:raiseValueError("corners are in the wrong order")ifnumpy.abs(length1-length2).max()>tolerance:raiseValueError("top and bottom edges have different lengths")returnplanar_array
# numbified below
[docs]defproject(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of euclidean coordinates of shape (N, 3) :returns: (3, U, N) values """out=numpy.zeros((3,len(planar),len(points)))defdot(a,v):# array @ vectorreturna[:,0]*v[0]+a[:,1]*v[1]+a[:,2]*v[2]foru,plainenumerate(planar):width,length,_=pla.wlr# we project all the points of the mesh on a plane that contains# the surface (translating coordinates of the projections to a local# 2d space) and at the same time calculate the distance to that# plane.mat=points-pla.xyz[:,0]dists=dot(mat,pla.normal)xx=dot(mat,pla.uv1)yy=dot(mat,pla.uv2)# the actual resulting distance is a square root of squares# of a distance from a point to a plane that contains the surface# and a distance from a projection of that point on that plane# and a surface rectangle. we have former (``dists``), now we need# to find latter.## we process separately two coordinate components of the point# projection. for abscissa we consider three possible cases:## I . III . II# . .# 0-----+ → x axis direction# | |# +-----+# . .# . .#mxx=numpy.select(condlist=[# case "I": point on the left hand side from the rectanglexx<0,# case "II": point is on the right hand sidexx>length# default -- case "III": point is in between vertical sides],choicelist=[# case "I": we need to consider distance between a point# and a line containing left side of the rectanglexx,# case "II": considering a distance between a point and# a line containing the right sidexx-length],# case "III": abscissa doesn't have an effect on a distance# to the rectangledefault=0.)# for ordinate we do the same operation (again three cases):## I# - - - 0---+ - - - ↓ y axis direction# III | |# - - - +---+ - - -# II#myy=numpy.select(condlist=[# case "I": point is above the rectangle top edgeyy<0,# case "II": point is below the rectangle bottom edgeyy>width# default -- case "III": point is in between lines containing# top and bottom edges],choicelist=[# case "I": considering a distance to a line containing# a top edgeyy,# case "II": considering a distance to a line containing# a bottom edgeyy-width],# case "III": ordinate doesn't affect the distancedefault=0)# combining distance on a plane with distance to a planeout[0,u]=numpy.sqrt(dists**2+mxx**2+myy**2)out[1,u]=xxout[2,u]=yyreturnout
# numbified below
[docs]defproject_back(planar,xx,yy):""" :param planar: a planar recarray of shape (U, 3) :param xx: an array of of shape (U, N) :param yy: an array of of shape (U, N) :returns: (3, U, N) values """U,N=xx.shapearr=numpy.zeros((3,U,N))foruinrange(U):arr3N=numpy.zeros((3,N))mxx=numpy.clip(xx[u],0.,planar.wlr[u,1])myy=numpy.clip(yy[u],0.,planar.wlr[u,0])foriinrange(3):arr3N[i]=(planar.xyz[u,i,0]+planar.uv1[u,i]*mxx+planar.uv2[u,i]*myy)arr[:,u]=geo_utils.cartesian_to_spherical(arr3N.T)returnarr
# NB: we define four great circle arcs that contain four sides# of projected planar surface:## ↓ II ↓# I ↓ ↓ I# ↓ + ↓# →→→→→TL→→→→1→→→→TR→→→→→ → azimuth direction →# ↓ - ↓# ↓ ↓# III -3+ IV -4+ III ↓# ↓ ↓ downdip direction# ↓ + ↓ ↓# →→→→→BL→→→→2→→→→BR→→→→→# ↓ - ↓# I ↓ ↓ I# ↓ II ↓## arcs 1 and 2 are directed from left corners to right ones (the# direction has an effect on the sign of the distance to an arc,# as it shown on the figure), arcs 3 and 4 are directed from top# corners to bottom ones.## then we measure distance from each of the points in a mesh# to each of those arcs and compare signs of distances in order# to find a relative positions of projections of points and# projection of a surface.## then we consider four special cases (labeled with Roman numerals)# and either pick one of distances to arcs or a closest distance# to corner.## indices 0, 2 and 1 represent corners TL, BL and TR respectively.
[docs]defget_rjb(planar,points):# numbified below""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) values """lons,lats,deps=geo_utils.cartesian_to_spherical(points)out=numpy.zeros((len(planar),len(points)))foru,plainenumerate(planar):strike,dip,rake=pla['sdr']downdip=(strike+90)%360corners=pla.cornersclons,clats=numpy.zeros(4),numpy.zeros(4)clons[:],clats[:]=corners[0],corners[1]dists_to_arcs=numpy.zeros((len(lons),4))# shape (N, 4)# calculate distances from all the target points to all four arcsdists_to_arcs[:,0]=geodetic.distances_to_arc(clons[2],clats[2],strike,lons,lats)dists_to_arcs[:,1]=geodetic.distances_to_arc(clons[0],clats[0],strike,lons,lats)dists_to_arcs[:,2]=geodetic.distances_to_arc(clons[0],clats[0],downdip,lons,lats)dists_to_arcs[:,3]=geodetic.distances_to_arc(clons[1],clats[1],downdip,lons,lats)# distances from all the target points to each of surface's# corners' projections (we might not need all of those but it's# better to do that calculation once for all).corners=fast_spherical_to_cartesian(clons,clats,numpy.zeros(4))# shape (4, 3) and (N, 3) -> (4, N) -> Ndists_to_corners=numpy.array([geo_utils.min_distance(point,corners)forpointinpoints])# extract from ``dists_to_arcs`` signs (represent relative positions# of an arc and a point: +1 means on the left hand side, 0 means# on arc and -1 means on the right hand side) and minimum absolute# values of distances to each pair of parallel arcs.ds1,ds2,ds3,ds4=numpy.sign(dists_to_arcs).transpose()dta=numpy.abs(dists_to_arcs).reshape(-1,2,2)dists_to_arcs0=numpy.array([d2[0].min()ford2indta])dists_to_arcs1=numpy.array([d2[1].min()ford2indta])out[u]=numpy.select(# consider four possible relative positions of point and arcs:condlist=[# signs of distances to both parallel arcs are the same# in both pairs, case "I" on a figure above(ds1==ds2)&(ds3==ds4),# sign of distances to two parallels is the same only# in one pair, case "II"ds1==ds2,# ... or another (case "III")ds3==ds4# signs are different in both pairs (this is a "default"),# case "IV"],choicelist=[# case "I": closest distance is the closest distance to cornersdists_to_corners,# case "II": closest distance is distance to arc "1" or "2",# whichever is closerdists_to_arcs0,# case "III": closest distance is distance to either# arc "3" or "4"dists_to_arcs1],# default -- case "IV"default=0.)returnout
# numbified below
[docs]defget_rx(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) distances """lons,lats,deps=geo_utils.cartesian_to_spherical(points)out=numpy.zeros((len(planar),len(points)))foru,plainenumerate(planar):clon,clat,_=pla.corners[:,0]strike=pla.sdr[0]out[u]=geodetic.distances_to_arc(clon,clat,strike,lons,lats)returnout
# numbified below
[docs]defget_ry0(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) distances """lons,lats,deps=geo_utils.cartesian_to_spherical(points)out=numpy.zeros((len(planar),len(points)))foru,plainenumerate(planar):llon,llat,_=pla.corners[:,0]# top leftrlon,rlat,_=pla.corners[:,1]# top rightstrike=(pla.sdr[0]+90.)%360.dst1=geodetic.distances_to_arc(llon,llat,strike,lons,lats)dst2=geodetic.distances_to_arc(rlon,rlat,strike,lons,lats)# Get the shortest distance from the two linesidx=numpy.sign(dst1)==numpy.sign(dst2)out[u][idx]=numpy.fmin(numpy.abs(dst1[idx]),numpy.abs(dst2[idx]))returnout
# numbified below
[docs]defget_rhypo(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) distances """out=numpy.zeros((len(planar),len(points)))lons,lats,deps=geo_utils.cartesian_to_spherical(points)hypo=planar.hypoforu,plainenumerate(planar):hdist=geodetic.distances(math.radians(hypo[u,0]),math.radians(hypo[u,1]),numpy.radians(lons),numpy.radians(lats))vdist=hypo[u,2]-depsout[u]=numpy.sqrt(hdist**2+vdist**2)returnout
# numbified below
[docs]defget_repi(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) distances """out=numpy.zeros((len(planar),len(points)))lons,lats,deps=geo_utils.cartesian_to_spherical(points)hypo=planar.hypoforu,plainenumerate(planar):out[u]=geodetic.distances(math.radians(hypo[u,0]),math.radians(hypo[u,1]),numpy.radians(lons),numpy.radians(lats))returnout
# numbified below
[docs]defget_azimuth(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) distances """out=numpy.zeros((len(planar),len(points)))lons,lats,deps=geo_utils.cartesian_to_spherical(points)hypo=planar.hypoforu,plainenumerate(planar):azim=geodetic.fast_azimuth(hypo[u,0],hypo[u,1],lons,lats)strike=planar.sdr[u,0]out[u]=(azim-strike)%360returnout
# TODO: fix this
[docs]defget_rvolc(planar,points):""" :param planar: a planar recarray of shape (U, 3) :param points: an array of of shape (N, 3) :returns: (U, N) distances """returnnumpy.zeros((len(planar),len(points)))
[docs]defget_distances_planar(planar,sites,dist_type):""" :param planar: a planar array of shape (U, 3) :param sites: a filtered site collection with N sites :param dist_type: kind of distance to compute :returns: an array of distances of shape (U, N) """getdist=globals()['get_'+dist_type]returngetdist(planar,sites.xyz)
[docs]classPlanarSurface(BaseSurface):""" Planar rectangular surface with two sides parallel to the Earth surface. :param strike: Strike of the surface is the azimuth from ``top_left`` to ``top_right`` points. :param dip: Dip is the angle between the surface itself and the earth surface. Other parameters are points (instances of :class:`~openquake.hazardlib.geo.point.Point`) defining the surface corners in clockwise direction starting from top left corner. Top and bottom edges of the polygon must be parallel to earth surface and to each other. See :class:`~openquake.hazardlib.geo.nodalplane.NodalPlane` for more detailed definition of ``strike`` and ``dip``. Note that these parameters are supposed to match the factual surface geometry (defined by corner points), but this is not enforced or even checked. :raises ValueError: If either top or bottom points differ in depth or if top edge is not parallel to the bottom edge, if top edge differs in length from the bottom one, or if mesh spacing is not positive. """@propertydefsurface_nodes(self):""" A single element list containing a planarSurface node """node=Node('planarSurface')forname,lon,lat,depthinzip('topLeft topRight bottomLeft bottomRight'.split(),self.corner_lons,self.corner_lats,self.corner_depths):node.append(Node(name,dict(lon=lon,lat=lat,depth=depth)))return[node]@propertydefmesh(self):# used in event based""" :returns: a mesh with the 4 corner points tl, tr, bl, br """returnMesh(*self.array.corners)@propertydefcorner_lons(self):returnself.array.corners[0]@propertydefcorner_lats(self):returnself.array.corners[1]@propertydefcorner_depths(self):returnself.array.corners[2]@propertydeftor(self):""" :returns: top of rupture line """lo=[]la=[]forpntin[self.top_left,self.top_right]:lo.append(pnt.longitude)la.append(pnt.latitude)returnLine.from_vectors(lo,la)def__init__(self,strike,dip,top_left,top_right,bottom_right,bottom_left,check=True):ifcheck:ifnot(top_left.depth==top_right.depthandbottom_left.depth==bottom_right.depth):raiseValueError("top and bottom edges must be parallel ""to the earth surface")NodalPlane.check_dip(dip)NodalPlane.check_strike(strike)self.dip=dipself.strike=strikeself.corners=numpy.array([[top_left.longitude,top_right.longitude,bottom_left.longitude,bottom_right.longitude],[top_left.latitude,top_right.latitude,bottom_left.latitude,bottom_right.latitude],[top_left.depth,top_right.depth,bottom_left.depth,bottom_right.depth]]).T# shape (4, 3)# now set the attributes normal, d, uv1, uv2, tlself._init_plane(check,[float(strike),float(dip),0.])
[docs]@classmethoddeffrom_corner_points(cls,top_left,top_right,bottom_right,bottom_left):""" Create and return a planar surface from four corner points. The azimuth of the line connecting the top left and the top right corners define the surface strike, while the angle between the line connecting the top left and bottom left corners and a line parallel to the earth surface defines the surface dip. :param openquake.hazardlib.geo.point.Point top_left: Upper left corner :param openquake.hazardlib.geo.point.Point top_right: Upper right corner :param openquake.hazardlib.geo.point.Point bottom_right: Lower right corner :param openquake.hazardlib.geo.point.Point bottom_left: Lower left corner :returns: An instance of :class:`PlanarSurface`. """strike=top_left.azimuth(top_right)dist=top_left.distance(bottom_left)vert_dist=bottom_left.depth-top_left.depthdip=numpy.degrees(numpy.arcsin(vert_dist/dist))self=cls(strike,dip,top_left,top_right,bottom_right,bottom_left)returnself
[docs]@classmethoddeffrom_hypocenter(cls,hypoc,msr,mag,aratio,strike,dip,rake,ztor=None):""" Create and return a planar surface given the hypocenter location and other rupture properties. :param hypoc: An instance of :class: `openquake.hazardlib.geo.point.Point` :param msr: The magnitude scaling relationship e.g. an instance of :class: `openquake.hazardlib.scalerel.WC1994` :param mag: The magnitude :param aratio: The rupture aspect ratio :param strike: The rupture strike :param dip: The rupture dip :param rake: The rupture rake :param ztor: If not None it doesn't consider the hypocentral depth constraint """lon=hypoc.longitudelat=hypoc.latitudedepth=hypoc.deptharea=msr.get_median_area(mag,rake)width=(area/aratio)**0.5length=width*aratio## ..... the dotted line is the width# \ |# \ | this dashed vertical line is the height# \ |# \ |# rupture \ |#height=width*numpy.sin(numpy.radians(dip))hdist=width*numpy.cos(numpy.radians(dip))ifztorisnotNone:depth=ztor+height/2# Move hor. 1/2 hdist in direction -90mid_top=point_at(lon,lat,strike-90,hdist/2)# Move hor. 1/2 hdist in direction +90mid_bot=point_at(lon,lat,strike+90,hdist/2)# compute corner points at the surfacetop_right=point_at(mid_top[0],mid_top[1],strike,length/2)top_left=point_at(mid_top[0],mid_top[1],strike+180,length/2)bot_right=point_at(mid_bot[0],mid_bot[1],strike,length/2)bot_left=point_at(mid_bot[0],mid_bot[1],strike+180,length/2)# compute corner points in 3D; rounded to 5 digits to avoid having# slightly different surfaces between macos and linuxpbl=Point(bot_left[0],bot_left[1],depth+height/2).round()pbr=Point(bot_right[0],bot_right[1],depth+height/2).round()ptl=Point(top_left[0],top_left[1],depth-height/2).round()ptr=Point(top_right[0],top_right[1],depth-height/2).round()returncls(strike,dip,ptl,ptr,pbr,pbl)
[docs]@classmethoddeffrom_array(cls,array34):""" :param array34: an array of shape (3, 4) in order tl, tr, bl, br :returns: a :class:`PlanarSurface` instance """# this is used in event based calculations# when the planar surface geometry comes from an array# in the datastore, which means it is correct and there is no need# to check it again; also the check would fail because of a bug,# https://github.com/gem/oq-engine/issues/3392# NB: this different from the ucerf order below, bl<->br!tl,tr,bl,br=[Point(*p)forpinarray34.T]strike=tl.azimuth(tr)dip=numpy.degrees(numpy.arcsin((bl.depth-tl.depth)/tl.distance(bl)))returncls(strike,dip,tl,tr,br,bl,check=False)
[docs]@classmethoddeffrom_ucerf(cls,array43):""" :param array43: an array of shape (4, 3) in order tl, tr, br, bl :returns: a :class:`PlanarSurface` instance """tl,tr,br,bl=[Point(*p)forpinarray43]strike=tl.azimuth(tr)dip=numpy.degrees(numpy.arcsin((bl.depth-tl.depth)/tl.distance(bl)))self=cls(strike,dip,tl,tr,br,bl,check=False)returnself
def_init_plane(self,check=False,sdr=None):""" Prepare everything needed for projecting arbitrary points on a plane containing the surface. """self.array=build_planar_array(self.corners,sdr,check=check)# this is not used anymore by the engine
[docs]deftranslate(self,p1,p2):""" Translate the surface for a specific distance along a specific azimuth direction. Parameters are two points (instances of :class:`openquake.hazardlib.geo.point.Point`) representing the direction and an azimuth for translation. The resulting surface corner points will be that far along that azimuth from respective corner points of this surface as ``p2`` is located with respect to ``p1``. :returns: A new :class:`PlanarSurface` object with the same mesh spacing, dip, strike, width, length and depth but with corners longitudes and latitudes translated. """azimuth=geodetic.azimuth(p1.longitude,p1.latitude,p2.longitude,p2.latitude)distance=geodetic.geodetic_distance(p1.longitude,p1.latitude,p2.longitude,p2.latitude)# avoid calling PlanarSurface's constructornsurf=object.__new__(PlanarSurface)nsurf.corners=numpy.zeros((4,3))fori,(lon,lat)inenumerate(zip(self.corner_lons,self.corner_lats)):lo,la=geodetic.point_at(lon,lat,azimuth,distance)nsurf.corners[i,0]=lonsurf.corners[i,1]=lansurf.corners[i,2]=self.corner_depths[i]nsurf.dip=self.dipnsurf.strike=self.strikensurf._init_plane()returnnsurf
@propertydeftop_left(self):returnPoint(self.corner_lons[0],self.corner_lats[0],self.corner_depths[0])@propertydeftop_right(self):returnPoint(self.corner_lons[1],self.corner_lats[1],self.corner_depths[1])@propertydefbottom_left(self):returnPoint(self.corner_lons[2],self.corner_lats[2],self.corner_depths[2])@propertydefbottom_right(self):returnPoint(self.corner_lons[3],self.corner_lats[3],self.corner_depths[3])@property# used in the SMTKdeflength(self):""" Return length of the rupture """returnself.array.wlr[1]@property# used in the SMTKdefwidth(self):""" Return length of the rupture """returnself.array.wlr[0]
[docs]defget_strike(self):""" Return strike value that was provided to the constructor. """returnself.strike
[docs]defget_dip(self):""" Return dip value that was provided to the constructor. """returnself.dip
[docs]defget_min_distance(self,mesh):""" See :meth:`superclass' method <openquake.hazardlib.geo.surface.base.BaseSurface.get_min_distance>`. """returnproject(self.array.reshape(1,3),mesh.xyz)[0,0]
[docs]defget_closest_points(self,mesh):""" See :meth:`superclass' method <openquake.hazardlib.geo.surface.base.BaseSurface.get_closest_points>`. """array=self.arraymat=mesh.xyz-array.xyz[:,0]xx=numpy.clip(mat@array.uv1,0,array.wlr[1])yy=numpy.clip(mat@array.uv2,0,array.wlr[0])vectors=(array.xyz[:,0]+array.uv1*xx.reshape(xx.shape+(1,))+array.uv2*yy.reshape(yy.shape+(1,)))returnMesh(*geo_utils.cartesian_to_spherical(vectors))
[docs]defget_top_edge_centroid(self):""" Overrides :meth:`superclass' method <openquake.hazardlib.geo.surface.base.BaseSurface.get_top_edge_centroid>` in order to avoid creating a mesh. """lon,lat=geo_utils.get_middle_point(self.corner_lons[0],self.corner_lats[0],self.corner_lons[1],self.corner_lats[1])returnPoint(lon,lat,self.corner_depths[0])
[docs]defget_top_edge_depth(self):""" Overrides :meth:`superclass' method <openquake.hazardlib.geo.surface.base.BaseSurface.get_top_edge_depth>` in order to avoid creating a mesh. """returnself.corner_depths[0]
[docs]defget_joyner_boore_distance(self,mesh):""" See :meth:`superclass' method <openquake.hazardlib.geo.surface.base.BaseSurface.get_joyner_boore_distance>`. This is an optimized version specific to planar surface that doesn't make use of the mesh. """rjb=get_rjb(self.array.reshape(1,3),mesh.xyz)[0]returnrjb
[docs]defget_rx_distance(self,mesh):""" See :meth:`superclass method <.base.BaseSurface.get_rx_distance>` for spec of input and result values. This is an optimized version specific to planar surface that doesn't make use of the mesh. """returnget_rx(self.array.reshape(1,3),mesh.xyz)[0]
[docs]defget_ry0_distance(self,mesh):""" :param mesh: :class:`~openquake.hazardlib.geo.mesh.Mesh` of points to calculate Ry0-distance to. :returns: Numpy array of distances in km. See also :meth:`superclass method <.base.BaseSurface.get_ry0_distance>` for spec of input and result values. This is version specific to the planar surface doesn't make use of the mesh """returnget_ry0(self.array.reshape(1,3),mesh.xyz)[0]
[docs]defget_width(self):""" Return surface's width value (in km) as computed in the constructor (that is mean value of left and right surface sides). """returnself.array.wlr[0]
[docs]defget_area(self):""" Return surface's area value (in squared km) obtained as the product of surface length and width. """returnself.array.wlr[0]*self.array.wlr[1]
[docs]defget_bounding_box(self):""" Compute surface bounding box from plane's corners coordinates. Calls :meth:`openquake.hazardlib.geo.utils.get_spherical_bounding_box` :return: A tuple of four items. These items represent western, eastern, northern and southern borders of the bounding box respectively. Values are floats in decimal degrees. """returngeo_utils.get_spherical_bounding_box(self.corner_lons,self.corner_lats)
[docs]defget_middle_point(self):""" Compute middle point from surface's corners coordinates. Calls :meth:`openquake.hazardlib.geo.utils.get_middle_point` """# compute middle point between upper left and bottom right cornerslon,lat=geo_utils.get_middle_point(self.corner_lons[0],self.corner_lats[0],self.corner_lons[3],self.corner_lats[3])depth=(self.corner_depths[0]+self.corner_depths[3])/2.returnPoint(lon,lat,depth)
[docs]defget_surface_boundaries(self):""" The corners lons/lats in WKT-friendly order (clockwise) """return(self.corner_lons.take([0,1,3,2,0]),self.corner_lats.take([0,1,3,2,0]))
[docs]defget_surface_boundaries_3d(self):""" The corners lons/lats/depths in WKT-friendly order (clockwise) """return(self.corner_lons.take([0,1,3,2,0]),self.corner_lats.take([0,1,3,2,0]),self.corner_depths.take([0,1,3,2,0]))