def createFromCenter(cls, cx, cy, xspan, yspan, dx, dy, defaultVs30=686.0, vs30File=None, vs30measured_grid=None, backarc=False, padding=False, resample=False): """ Create a Sites object by defining a center point, resolution, extent, and Vs30 values. :param cx: X coordinate of desired center point. :param cy: Y coordinate of desired center point. :param xspan: Width of desired grid. :param yspan: Height of desired grid. :param dx: Resolution of desired grid in X direction. :param dy: Resolution of desired grid in Y direction. :param defaultVs30: Default Vs30 value to use if vs30File not specified. :param vs30File: Name of GMT or GDAL format grid file containing Vs30 values. :param vs30measured_grid: Boolean grid indicating whether Vs30 values were measured or derived (i.e., from slope) :param backarc: Boolean indicating whether event is on the backarc as defined `here <http://earthquake.usgs.gov/learn/glossary/?term=backarc>`__. :param padding: Boolean indicating whether or not to pad resulting Vs30 grid out to edges of input bounds. If False, grid will be clipped to the extent of the input file. :param resample: Boolean indicating whether or not the grid should be resampled. """ geodict = GeoDict.createDictFromCenter(cx, cy, dx, dy, xspan, yspan) if vs30File is not None: vs30grid = cls._create(geodict, defaultVs30, vs30File, padding, resample) else: griddata = np.ones( (geodict.ny, geodict.nx), dtype=np.float64) * defaultVs30 vs30grid = Grid2D(griddata, geodict) return cls(vs30grid, vs30measured_grid=vs30measured_grid, backarc=backarc, defaultVs30=defaultVs30)
def getRegions(self, lat, lon, depth): """Get information about the tectonic region of a given hypocenter. Args: lat (float): Earthquake hypocentral latitude. lon (float): Earthquake hypocentral longitude. depth (float): Earthquake hypocentral depth. Returns: Series: Pandas series object containing labels: - TectonicRegion: Subduction, Active, Stable, or Volcanic. - DistanceToStable: Distance in km to nearest stable region. - DistanceToActive: Distance in km to nearest active region. - DistanceToSubduction: Distance in km to nearest subduction region. - DistanceToVolcanic: Distance in km to nearest volcanic region. - Oceanic: Boolean indicating if epicenter is in the ocean. - DistanceToOceanic: Distance in km to nearest oceanic region. - DistanceToContinental: Distance in km to nearest continental region. """ regions = OrderedDict() gd = GeoDict.createDictFromCenter(lon, lat, DX, DY, XSPAN, YSPAN) tec_grid = read(self._tectonic_grid, samplegeodict=gd) region_dict = get_dist_to_type(lon, lat, tec_grid, TECTONIC_REGIONS) ocean_grid = read(self._oceanic_grid, samplegeodict=gd) ocean_dict = get_dist_to_type(lon, lat, ocean_grid, OCEANIC_REGIONS) if region_dict['DistanceToActive'] == 0: region_dict['TectonicRegion'] = 'Active' elif region_dict['DistanceToStable'] == 0: region_dict['TectonicRegion'] = 'Stable' elif region_dict['DistanceToSubduction'] == 0: region_dict['TectonicRegion'] = 'Subduction' else: region_dict['TectonicRegion'] = 'Volcanic' region_dict['DistanceToOceanic'] = ocean_dict['DistanceToOceanic'] region_dict['DistanceToContinental'] = ocean_dict[ 'DistanceToContinental'] region_dict['Oceanic'] = False if ocean_dict['DistanceToOceanic'] == 0: region_dict['Oceanic'] = True regions = pd.Series(region_dict, index=[ 'TectonicRegion', 'DistanceToStable', 'DistanceToActive', 'DistanceToSubduction', 'DistanceToVolcanic', 'Oceanic', 'DistanceToOceanic', 'DistanceToContinental' ]) return regions
def getRegions(self, lat, lon, depth): """Get information about the tectonic region of a given hypocenter. Args: lat (float): Earthquake hypocentral latitude. lon (float): Earthquake hypocentral longitude. depth (float): Earthquake hypocentral depth. Returns: Series: Pandas series object containing labels: - TectonicRegion: Subduction, Active, Stable, or Volcanic. - DistanceToStable: Distance in km to nearest stable region. - DistanceToActive: Distance in km to nearest active region. - DistanceToSubduction: Distance in km to nearest subduction region. - DistanceToVolcanic: Distance in km to nearest volcanic region. - Oceanic: Boolean indicating if epicenter is in the ocean. - DistanceToOceanic: Distance in km to nearest oceanic region. - DistanceToContinental: Distance in km to nearest continental region. """ regions = OrderedDict() gd = GeoDict.createDictFromCenter(lon, lat, DX, DY, XSPAN, YSPAN) tec_grid = read(self._tectonic_grid, samplegeodict=gd) region_dict = get_dist_to_type(lon, lat, tec_grid, TECTONIC_REGIONS) ocean_grid = read(self._oceanic_grid, samplegeodict=gd) ocean_dict = get_dist_to_type(lon, lat, ocean_grid, OCEANIC_REGIONS) if region_dict['DistanceToActive'] == 0: region_dict['TectonicRegion'] = 'Active' elif region_dict['DistanceToStable'] == 0: region_dict['TectonicRegion'] = 'Stable' elif region_dict['DistanceToSubduction'] == 0: region_dict['TectonicRegion'] = 'Subduction' else: region_dict['TectonicRegion'] = 'Volcanic' region_dict['DistanceToOceanic'] = ocean_dict['DistanceToOceanic'] region_dict['DistanceToContinental'] = ocean_dict['DistanceToContinental'] region_dict['Oceanic'] = False if ocean_dict['DistanceToOceanic'] == 0: region_dict['Oceanic'] = True regions = pd.Series(region_dict, index=['TectonicRegion', 'DistanceToStable', 'DistanceToActive', 'DistanceToSubduction', 'DistanceToVolcanic', 'Oceanic', 'DistanceToOceanic', 'DistanceToContinental']) return regions
def fromCenter(cls, cx, cy, xspan, yspan, dx, dy, defaultVs30=686.0, vs30File=None, vs30measured_grid=None, backarc=None, padding=False, resample=False): """ Create a Sites object by defining a center point, resolution, extent, and Vs30 values. :param cx: X coordinate of desired center point. :param cy: Y coordinate of desired center point. :param xspan: Width of desired grid. :param yspan: Height of desired grid. :param dx: Resolution of desired grid in X direction. :param dy: Resolution of desired grid in Y direction. :param defaultVs30: Default Vs30 value to use if vs30File not specified. :param vs30File: Name of GMT or GDAL format grid file containing Vs30 values. :param vs30measured_grid: Boolean grid indicating whether Vs30 values were measured or derived (i.e., from slope) :param backarc: Boolean array indicating whether site is in the subduction `backarc <http://earthquake.usgs.gov/learn/glossary/?term=backarc>`__. :param padding: Boolean indicating whether or not to pad resulting Vs30 grid out to edges of input bounds. If False, grid will be clipped to the extent of the input file. :param resample: Boolean indicating whether or not the grid should be resampled. """ geodict = GeoDict.createDictFromCenter(cx, cy, dx, dy, xspan, yspan) if vs30File is not None: vs30grid = cls._create(geodict, defaultVs30, vs30File, padding, resample) else: griddata = np.ones((geodict.ny, geodict.nx), dtype=np.float64) * defaultVs30 vs30grid = Grid2D(griddata, geodict) return cls(vs30grid, vs30measured_grid=vs30measured_grid, backarc=backarc, defaultVs30=defaultVs30)
def test(): #these values taken from the shakemap header of: #http://earthquake.usgs.gov/realtime/product/shakemap/ak12496371/ak/1453829475592/download/grid.xml print('Testing various dictionaries for consistency...') print('Testing consistent dictionary...') #this should pass, and will serve as the comparison from now on gdict = { 'xmin': -160.340600, 'xmax': -146.340600, 'ymin': 54.104700, 'ymax': 65.104700, 'dx': 0.025000, 'dy': 0.025000, 'ny': 441, 'nx': 561 } gd = GeoDict(gdict) print('Consistent dictionary passed.') print('Testing dictionary with inconsistent resolution...') #this should pass gdict = { 'xmin': -160.340600, 'xmax': -146.340600, 'ymin': 54.104700, 'ymax': 65.104700, 'dx': 0.026000, 'dy': 0.026000, 'ny': 441, 'nx': 561 } gd3 = GeoDict(gdict, adjust='res') assert gd3 == gd print('Dimensions modification passed.') print('Testing dictionary with inconsistent lower right corner...') #this should pass gdict = { 'xmin': -160.340600, 'xmax': -146.350600, 'ymin': 54.103700, 'ymax': 65.104700, 'dx': 0.025000, 'dy': 0.025000, 'ny': 441, 'nx': 561 } gd4 = GeoDict(gdict, adjust='bounds') assert gd4 == gd print('Corner modification passed.') print( 'Testing to make sure lat/lon and row/col calculations are correct...') #make sure the lat/lon row/col calculations are correct ndec = int(np.abs(np.log10(GeoDict.EPS))) lat, lon = gd.getLatLon(0, 0) dlat = np.abs(lat - gd.ymax) dlon = np.abs(lon - gd.xmin) assert dlat < GeoDict.EPS and dlon < GeoDict.EPS row, col = gd.getRowCol(lat, lon) assert row == 0 and col == 0 lat, lon = gd.getLatLon(gd.ny - 1, gd.nx - 1) dlat = np.abs(lat - gd.ymin) dlon = np.abs(lon - gd.xmax) assert dlat < GeoDict.EPS and dlon < GeoDict.EPS row, col = gd.getRowCol(lat, lon) assert row == (gd.ny - 1) and col == (gd.nx - 1) print('lat/lon and row/col calculations are correct.') print('Testing a dictionary for a global grid...') #this is the file geodict for Landscan - should pass muster globaldict = { 'nx': 43200, 'ny': 20880, 'dx': 0.00833333333333, 'xmax': 179.99583333318935, 'xmin': -179.99583333333334, 'dy': 0.00833333333333, 'ymax': 83.99583333326376, 'ymin': -89.99583333333334 } gd5 = GeoDict(globaldict) lat, lon = gd5.getLatLon(gd5.ny - 1, gd5.nx - 1) dlat = np.abs(lat - gd5.ymin) dlon = np.abs(lon - gd5.xmax) assert dlat < GeoDict.EPS and dlon < GeoDict.EPS print('Global grid is internally consistent.') #Test class methods for creating a GeoDict print('Testing whether GeoDict creator class methods work...') xmin = -121.05333277776235 xmax = -116.03833388890432 ymin = 32.138334444506171 ymax = 36.286665555493826 dx = 0.0083333333333333332 dy = 0.0083333333333333332 gd6 = GeoDict.createDictFromBox(xmin, xmax, ymin, ymax, dx, dy, inside=False) assert gd6.xmax > xmax assert gd6.ymin < ymin print('Created dictionary (outside) is correct.') gd7 = GeoDict.createDictFromBox(xmin, xmax, ymin, ymax, dx, dy, inside=True) assert gd7.xmax < xmax assert gd7.ymin > ymin print('Created dictionary (inside) is correct.') xspan = 2.5 yspan = 2.5 gd8 = GeoDict.createDictFromCenter(xmin, ymin, dx, dy, xspan, yspan) print('Created dictionary (from center point) is valid.') print('Testing a geodict with dx/dy values that are NOT the same...') xmin, xmax, ymin, ymax = (-121.06166611109568, -116.03000055557099, 32.130001111172838, 36.294998888827159) dx, dy = (0.009999722214505959, 0.009999444413578534) td = GeoDict.createDictFromBox(xmin, xmax, ymin, ymax, dx, dy) print( 'Passed testing a geodict with dx/dy values that are NOT the same...') #test getBoundsWithin #use global grid, and then a shakemap grid that we can get print('Testing getBoundsWithin...') grussia = { 'xmin': 155.506400, 'xmax': 161.506400, 'ymin': 52.243000, 'ymax': 55.771000, 'dx': 0.016667, 'dy': 0.016642, 'nx': 361, 'ny': 213 } gdrussia = GeoDict(grussia, adjust='res') sampledict = gd5.getBoundsWithin(gdrussia) xSmaller = sampledict.xmin > grussia['xmin'] and sampledict.xmax < grussia[ 'xmax'] ySmaller = sampledict.ymin > grussia['ymin'] and sampledict.ymax < grussia[ 'ymax'] assert xSmaller and ySmaller assert gd5.isAligned(sampledict) print('getBoundsWithin returned correct result.') print('Testing isAligned() method...') gd = GeoDict({ 'xmin': 0.5, 'xmax': 3.5, 'ymin': 0.5, 'ymax': 3.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4 }) inside_aligned = GeoDict({ 'xmin': 1.5, 'xmax': 2.5, 'ymin': 1.5, 'ymax': 2.5, 'dx': 1.0, 'dy': 1.0, 'nx': 2, 'ny': 2 }) inside_not_aligned = GeoDict({ 'xmin': 2.0, 'xmax': 3.0, 'ymin': 2.0, 'ymax': 3.0, 'dx': 1.0, 'dy': 1.0, 'nx': 2, 'ny': 2 }) assert gd.isAligned(inside_aligned) assert not gd.isAligned(inside_not_aligned) print('Passed isAligned() method...') print('Testing getAligned method...') popdict = GeoDict({ 'dx': 0.00833333333333, 'dy': 0.00833333333333, 'nx': 43200, 'ny': 20880, 'xmax': 179.99583333318935, 'xmin': -179.99583333333334, 'ymax': 83.99583333326376, 'ymin': -89.99583333333334 }) sampledict = GeoDict({ 'dx': 0.008333333333333333, 'dy': 0.008336693548387094, 'nx': 601, 'ny': 497, 'xmax': -116.046, 'xmin': -121.046, 'ymax': 36.2785, 'ymin': 32.1435 }) aligndict = popdict.getAligned(sampledict) assert popdict.isAligned(aligndict) print('Testing geodict intersects method...') gd1 = GeoDict({ 'xmin': 0.5, 'xmax': 3.5, 'ymin': 0.5, 'ymax': 3.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4 }) print('Testing geodict intersects method...') gd2 = GeoDict({ 'xmin': 2.5, 'xmax': 5.5, 'ymin': 2.5, 'ymax': 5.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4 }) gd3 = GeoDict({ 'xmin': 4.5, 'xmax': 7.5, 'ymin': 4.5, 'ymax': 7.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4 }) gd4 = GeoDict({ 'xmin': 1.5, 'xmax': 2.5, 'ymin': 1.5, 'ymax': 2.5, 'dx': 1.0, 'dy': 1.0, 'nx': 2, 'ny': 2 }) assert gd1.intersects(gd2) assert not gd1.intersects(gd3) print('Passed intersects method...') print('Testing geodict intersects method with real geographic data...') gda = GeoDict({ 'ymax': 83.62083333333263, 'nx': 43201, 'ny': 20835, 'dx': 0.00833333333333, 'dy': 0.00833333333333, 'xmin': -179.99583333333334, 'ymin': -89.99583333326461, 'xmax': -179.99583333347732 }) gdb = GeoDict({ 'ymax': 28.729166666619193, 'nx': 300, 'ny': 264, 'dx': 0.00833333333333, 'dy': 0.00833333333333, 'xmin': 84.08749999989436, 'ymin': 26.537499999953404, 'xmax': 86.57916666656007 }) assert gda.intersects(gdb) print('Passed geodict intersects method with real geographic data.') print('Testing geodict doesNotContain method...') assert gd1.doesNotContain(gd3) assert not gd1.doesNotContain(gd4) print('Passed doesNotContain method...') print('Testing geodict contains method...') assert gd1.contains(gd4) assert not gd1.contains(gd3) print('Passed contains method...')
def test(): # these values taken from the shakemap header of: # http://earthquake.usgs.gov/realtime/product/shakemap/ak12496371/ak/1453829475592/download/grid.xml print('Testing various dictionaries for consistency...') print('Testing consistent dictionary...') # this should pass, and will serve as the comparison from now on gdict = {'xmin': -160.340600, 'xmax': -146.340600, 'ymin': 54.104700, 'ymax': 65.104700, 'dx': 0.025000, 'dy': 0.025000, 'ny': 441, 'nx': 561} gd = GeoDict(gdict) print('Consistent dictionary passed.') print('Testing dictionary with inconsistent resolution...') # this should pass gdict = {'xmin': -160.340600, 'xmax': -146.340600, 'ymin': 54.104700, 'ymax': 65.104700, 'dx': 0.026000, 'dy': 0.026000, 'ny': 441, 'nx': 561} gd3 = GeoDict(gdict, adjust='res') assert gd3 == gd print('Dimensions modification passed.') print('Testing dictionary with inconsistent lower right corner...') # this should pass gdict = {'xmin': -160.340600, 'xmax': -146.350600, 'ymin': 54.103700, 'ymax': 65.104700, 'dx': 0.025000, 'dy': 0.025000, 'ny': 441, 'nx': 561} gd4 = GeoDict(gdict, adjust='bounds') assert gd4 == gd print('Corner modification passed.') print('Testing to make sure lat/lon and row/col calculations are correct...') # make sure the lat/lon row/col calculations are correct ndec = int(np.abs(np.log10(GeoDict.EPS))) lat, lon = gd.getLatLon(0, 0) dlat = np.abs(lat-gd.ymax) dlon = np.abs(lon-gd.xmin) assert dlat < GeoDict.EPS and dlon < GeoDict.EPS row, col = gd.getRowCol(lat, lon) assert row == 0 and col == 0 lat, lon = gd.getLatLon(gd.ny-1, gd.nx-1) dlat = np.abs(lat-gd.ymin) dlon = np.abs(lon-gd.xmax) assert dlat < GeoDict.EPS and dlon < GeoDict.EPS row, col = gd.getRowCol(lat, lon) assert row == (gd.ny-1) and col == (gd.nx-1) print('lat/lon and row/col calculations are correct.') print('Testing a dictionary for a global grid...') # this is the file geodict for Landscan - should pass muster globaldict = {'nx': 43200, 'ny': 20880, 'dx': 0.00833333333333, 'xmax': 179.99583333318935, 'xmin': -179.99583333333334, 'dy': 0.00833333333333, 'ymax': 83.99583333326376, 'ymin': -89.99583333333334} gd5 = GeoDict(globaldict) lat, lon = gd5.getLatLon(gd5.ny-1, gd5.nx-1) dlat = np.abs(lat-gd5.ymin) dlon = np.abs(lon-gd5.xmax) assert dlat < GeoDict.EPS and dlon < GeoDict.EPS print('Global grid is internally consistent.') # Test class methods for creating a GeoDict print('Testing whether GeoDict creator class methods work...') xmin = -121.05333277776235 xmax = -116.03833388890432 ymin = 32.138334444506171 ymax = 36.286665555493826 dx = 0.0083333333333333332 dy = 0.0083333333333333332 gd6 = GeoDict.createDictFromBox( xmin, xmax, ymin, ymax, dx, dy, inside=False) assert gd6.xmax > xmax assert gd6.ymin < ymin print('Created dictionary (outside) is correct.') gd7 = GeoDict.createDictFromBox( xmin, xmax, ymin, ymax, dx, dy, inside=True) assert gd7.xmax < xmax assert gd7.ymin > ymin print('Created dictionary (inside) is correct.') xspan = 2.5 yspan = 2.5 gd8 = GeoDict.createDictFromCenter(xmin, ymin, dx, dy, xspan, yspan) print('Created dictionary (from center point) is valid.') print('Testing a geodict with dx/dy values that are NOT the same...') xmin, xmax, ymin, ymax = (-121.06166611109568, -116.03000055557099, 32.130001111172838, 36.294998888827159) dx, dy = (0.009999722214505959, 0.009999444413578534) td = GeoDict.createDictFromBox(xmin, xmax, ymin, ymax, dx, dy) print('Passed testing a geodict with dx/dy values that are NOT the same...') # test getBoundsWithin # use global grid, and then a shakemap grid that we can get print('Testing getBoundsWithin...') grussia = {'xmin': 155.506400, 'xmax': 161.506400, 'ymin': 52.243000, 'ymax': 55.771000, 'dx': 0.016667, 'dy': 0.016642, 'nx': 361, 'ny': 213} gdrussia = GeoDict(grussia, adjust='res') sampledict = gd5.getBoundsWithin(gdrussia) xSmaller = sampledict.xmin > grussia['xmin'] and sampledict.xmax < grussia['xmax'] ySmaller = sampledict.ymin > grussia['ymin'] and sampledict.ymax < grussia['ymax'] assert xSmaller and ySmaller assert gd5.isAligned(sampledict) print('getBoundsWithin returned correct result.') print('Testing isAligned() method...') gd = GeoDict({'xmin': 0.5, 'xmax': 3.5, 'ymin': 0.5, 'ymax': 3.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4}) inside_aligned = GeoDict({'xmin': 1.5, 'xmax': 2.5, 'ymin': 1.5, 'ymax': 2.5, 'dx': 1.0, 'dy': 1.0, 'nx': 2, 'ny': 2}) inside_not_aligned = GeoDict({'xmin': 2.0, 'xmax': 3.0, 'ymin': 2.0, 'ymax': 3.0, 'dx': 1.0, 'dy': 1.0, 'nx': 2, 'ny': 2}) assert gd.isAligned(inside_aligned) assert not gd.isAligned(inside_not_aligned) print('Passed isAligned() method...') print('Testing getAligned method...') popdict = GeoDict({'dx': 0.00833333333333, 'dy': 0.00833333333333, 'nx': 43200, 'ny': 20880, 'xmax': 179.99583333318935, 'xmin': -179.99583333333334, 'ymax': 83.99583333326376, 'ymin': -89.99583333333334}) sampledict = GeoDict({'dx': 0.008333333333333333, 'dy': 0.008336693548387094, 'nx': 601, 'ny': 497, 'xmax': -116.046, 'xmin': -121.046, 'ymax': 36.2785, 'ymin': 32.1435}) aligndict = popdict.getAligned(sampledict) assert popdict.isAligned(aligndict) print('Testing geodict intersects method...') gd1 = GeoDict({'xmin': 0.5, 'xmax': 3.5, 'ymin': 0.5, 'ymax': 3.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4}) print('Testing geodict intersects method...') gd2 = GeoDict({'xmin': 2.5, 'xmax': 5.5, 'ymin': 2.5, 'ymax': 5.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4}) gd3 = GeoDict({'xmin': 4.5, 'xmax': 7.5, 'ymin': 4.5, 'ymax': 7.5, 'dx': 1.0, 'dy': 1.0, 'nx': 4, 'ny': 4}) gd4 = GeoDict({'xmin': 1.5, 'xmax': 2.5, 'ymin': 1.5, 'ymax': 2.5, 'dx': 1.0, 'dy': 1.0, 'nx': 2, 'ny': 2}) assert gd1.intersects(gd2) assert not gd1.intersects(gd3) print('Passed intersects method...') print('Testing geodict intersects method with real geographic data...') gda = GeoDict({'ymax': 83.62083333333263, 'nx': 43201, 'ny': 20835, 'dx': 0.00833333333333, 'dy': 0.00833333333333, 'xmin': -179.99583333333334, 'ymin': -89.99583333326461, 'xmax': -179.99583333347732}) gdb = GeoDict({'ymax': 28.729166666619193, 'nx': 300, 'ny': 264, 'dx': 0.00833333333333, 'dy': 0.00833333333333, 'xmin': 84.08749999989436, 'ymin': 26.537499999953404, 'xmax': 86.57916666656007}) assert gda.intersects(gdb) print('Passed geodict intersects method with real geographic data.') print('Testing geodict doesNotContain method...') assert gd1.doesNotContain(gd3) assert not gd1.doesNotContain(gd4) print('Passed doesNotContain method...') print('Testing geodict contains method...') assert gd1.contains(gd4) assert not gd1.contains(gd3) print('Passed contains method...')