def testGridDistances(self): for i in range(100): gsize = random.uniform(0., 1.) * 2. * 10.**random.uniform(4., 7.) north_grid, east_grid = num.meshgrid( num.linspace(-gsize / 2., gsize / 2., 11), num.linspace(-gsize / 2., gsize / 2., 11)) north_grid = north_grid.flatten() east_grid = east_grid.flatten() lon = random.uniform(-180., 180.) lat = random.uniform(-90., 90.) lat_grid, lon_grid = orthodrome.ne_to_latlon( lat, lon, north_grid, east_grid) lat_grid_alt, lon_grid_alt = \ orthodrome.ne_to_latlon_alternative_method( lat, lon, north_grid, east_grid) for la, lo, no, ea in zip(lat_grid, lon_grid, north_grid, east_grid): a = orthodrome.Loc(lat=la, lon=lo) b = orthodrome.Loc(lat=lat, lon=lon) cd = orthodrome.cosdelta(a, b) assert cd <= 1.0 d = num.arccos(cd) * earthradius d2 = math.sqrt(no**2 + ea**2) assert abs(d - d2) < 1.0e-3 or d2 < 10.
def testDistancePython(self): ntest = 1000 lats1, lons1, lats2, lons2 = self.get_critical_random_locations(ntest) loc1 = orthodrome.Loc(0., 0.) loc2 = orthodrome.Loc(0., 0.) for i in range(ntest): loc1.lat, loc1.lon = lats1[i], lons1[i] loc2.lat, loc2.lon = lats2[i], lons2[i] orthodrome.distance_accurate50m(loc1, loc2, implementation='python')
def to_cartesian(items, latref=None, lonref=None): res = [] latref = latref or 0. lonref = lonref or 0. latlon00 = ortho.Loc(latref, lonref) for i, item in enumerate(items): y, x = ortho.latlon_to_ne(latlon00, item) depth = item.depth * 1000 lat = item.lat / 180. * num.pi res.append((x, y, -depth)) # vielleicht doch als array?! #res = num.array(res) #res = res.T return res
def plot_polarizations(stations, trs, event=None, size_factor=0.05, fontsize=10., output_filename=None, output_format=None, output_dpi=None): if event is None: slats = num.array([s.lat for s in stations], dtype=num.float) slons = num.array([s.lon for s in stations], dtype=num.float) clat, clon = od.geographic_midpoint(slats, slons) event = od.Loc(clat, clon) nsl_c_to_trs = defaultdict(dict) for tr in trs: nsl_c_to_trs[tr.nslc_id[:3]][tr.nslc_id[3]] = tr nsl_to_station = dict((s.nsl(), s) for s in stations) plot.mpl_init(fontsize=fontsize) fig = plt.figure(figsize=plot.mpl_papersize('a4', 'landscape')) plot.mpl_margins(fig, w=7., h=6., units=fontsize) grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.5, add_all=True, label_mode='L', aspect=True) axes_en = grid[0] axes_en.set_ylabel('Northing [km]') axes_dn = grid[1] axes_dn.locator_params(axis='x', nbins=4) axes_dn.set_xlabel('Depth [km]') axes_ed = grid[2] axes_ed.locator_params(axis='y', nbins=4) axes_ed.set_ylabel('Depth [km]') axes_ed.set_xlabel('Easting [km]') if isinstance(event, model.Event): axes_en.plot(0., 0., '*') axes_dn.plot(event.depth / km, 0., '*') axes_ed.plot(0., event.depth / km, '*') grid[3].set_axis_off() locations = [] for nsl in sorted(nsl_c_to_trs.keys()): station = nsl_to_station[nsl] n, e = od.latlon_to_ne(event.lat, event.lon, station.lat, station.lon) locations.append((n, e)) ns, es = num.array(locations, dtype=num.float).T n_min = num.min(ns) n_max = num.max(ns) e_min = num.min(es) e_max = num.max(es) factor = max((n_max - n_min) * size_factor, (e_max - e_min) * size_factor) fontsize_annot = fontsize * 0.7 data = {} for insl, nsl in enumerate(sorted(nsl_c_to_trs.keys())): color = plot.mpl_graph_color(insl) try: tr_e = nsl_c_to_trs[nsl]['E'] tr_n = nsl_c_to_trs[nsl]['N'] tr_z = nsl_c_to_trs[nsl]['Z'] except KeyError: continue station = nsl_to_station[nsl] n, e = od.latlon_to_ne(event.lat, event.lon, station.lat, station.lon) d = station.depth axes_en.annotate('.'.join(x for x in nsl if x), xy=(e / km, n / km), xycoords='data', xytext=(fontsize_annot / 3., fontsize_annot / 3.), textcoords='offset points', verticalalignment='bottom', horizontalalignment='left', rotation=0., size=fontsize_annot) axes_en.plot(e / km, n / km, '^', mfc=color, mec=darken(color)) axes_dn.plot(d / km, n / km, '^', mfc=color, mec=darken(color)) axes_ed.plot(e / km, d / km, '^', mfc=color, mec=darken(color)) arr_e = tr_e.ydata arr_n = tr_n.ydata arr_z = tr_z.ydata arr_t = tr_z.get_xdata() data[nsl] = (arr_e, arr_n, arr_z, arr_t, n, e, d, color) amaxs = [] amax_hors = [] for nsl in sorted(data.keys()): arr_e, arr_n, arr_z, arr_t, n, e, d, color = data[nsl] amaxs.append(num.max(num.abs(num.sqrt(arr_e**2 + arr_n**2 + arr_z**2)))) amax_hors.append(num.max(num.abs(num.sqrt(arr_e**2 + arr_n**2)))) amax = num.median(amaxs) amax_hor = num.median(amax_hors) for nsl in sorted(data.keys()): arr_e, arr_n, arr_z, arr_t, n, e, d, color = data[nsl] tmin = arr_t.min() tmax = arr_t.max() plot_color_line(axes_en, (e + arr_e / amax_hor * factor) / km, (n + arr_n / amax_hor * factor) / km, arr_t, color, tmin, tmax) plot_color_line(axes_dn, (d - arr_z / amax * factor) / km, (n + arr_n / amax * factor) / km, arr_t, color, tmin, tmax) plot_color_line(axes_ed, (e + arr_e / amax * factor) / km, (d - arr_z / amax * factor) / km, arr_t, color, tmin, tmax) axes_ed.invert_yaxis() for axes in (axes_dn, axes_ed, axes_en): axes.autoscale_view(tight=True) if output_filename is None: plt.show() else: fig.savefig(output_filename, format=output_format, dpi=output_dpi)
def plot_spatial_with_dcs(events, eventsclusters, clusters, conf, plotdir): ''' Plot map of seismicity clusters, with focal mechanisms ''' lats = [ev.lat for ev in events] lons = [ev.lon for ev in events] # deps = [ev.depth for ev in events] # colors = [cluster_to_color(clid) for clid in eventsclusters] if conf.sw_filterevent: latmin, latmax = conf.latmin, conf.latmax lonmin, lonmax = conf.lonmin, conf.lonmax # depmin, depmax = conf.depthmin, conf.depthmax if latmin > latmax: print('cases over lon +-180 still to be implemented') sys.exit() center = model.event(0.5 * (latmin + latmax), 0.5 * (lonmin + lonmax)) else: latmin, latmax = min(lats) - 0.1, max(lats) + 0.1 lonmin, lonmax = min(lons) - 0.1, max(lons) + 0.1 # depmin = 0.*km # depmax = 1.05*max(deps) center = od.Loc(0.5 * (latmin + latmax), 0.5 * (lonmin + lonmax)) # Map size if conf.sw_manual_radius: safe_radius = conf.map_radius else: corners = [od.Loc(latmin, lonmin), od.Loc(latmin, lonmax)] dist1 = od.distance_accurate50m(center, corners[0]) dist2 = od.distance_accurate50m(center, corners[1]) safe_radius = max(dist1, dist2) # Generate the basic map m = Map(lat=center.lat, lon=center.lon, radius=safe_radius, width=30., height=30., show_grid=False, show_topo=False, color_dry=(238, 236, 230), topo_cpt_wet='light_sea_uniform', topo_cpt_dry='light_land_uniform', illuminate=True, illuminate_factor_ocean=0.15, show_rivers=False, show_plates=False) if conf.sw_filterevent: rectlons = [lonmin, lonmin, lonmax, lonmax, lonmin] rectlats = [latmin, latmax, latmax, latmin, latmin] m.gmt.psxy(in_columns=(rectlons, rectlats), W='thin,0/0/0,dashed', *m.jxyr) # Draw some larger cities covered by the map area m.draw_cities() # Events in clusters factor_symbl_size = 5. beachball_symbol = 'd' for id_cluster in clusters: col = cluster_to_color(id_cluster) g_col = color2rgb(col) for iev, evcl in enumerate(eventsclusters): if evcl == id_cluster: ev = events[iev] if ev.moment_tensor is not None: factor_symbl_size = ev.magnitude devi = ev.moment_tensor.deviatoric() beachball_size = 3. * factor_symbl_size mt = devi.m_up_south_east() mt = mt / ev.moment_tensor.scalar_moment() \ * pmt.magnitude_to_moment(5.0) m6 = pmt.to6(mt) if m.gmt.is_gmt5(): kwargs = dict(M=True, S='%s%g' % (beachball_symbol[0], (beachball_size) / gmtpy.cm)) else: kwargs = dict(S='%s%g' % (beachball_symbol[0], (beachball_size) * 2 / gmtpy.cm)) data = (ev.lon, ev.lat, 10) + tuple(m6) + (1, 0, 0) m.gmt.psmeca(in_rows=[data], G=g_col, E='white', W='1p,%s' % g_col, *m.jxyr, **kwargs) figname = os.path.join(plotdir, 'plot_map_with_dcs.' + conf.figure_format) m.save(figname)
def plot_spatial(events, eventsclusters, clusters, conf, plotdir): ''' Plot map of seismicity clusters ''' lats = [ev.lat for ev in events] lons = [ev.lon for ev in events] # deps = [ev.depth for ev in events] # colors = [cluster_to_color(clid) for clid in eventsclusters] if conf.sw_filterevent: latmin, latmax = conf.latmin, conf.latmax lonmin, lonmax = conf.lonmin, conf.lonmax # depmin, depmax = conf.depthmin, conf.depthmax if latmin > latmax: print('cases over lon +-180 still to be implemented') sys.exit() center = model.event(0.5 * (latmin + latmax), 0.5 * (lonmin + lonmax)) else: latmin, latmax = min(lats) - 0.1, max(lats) + 0.1 lonmin, lonmax = min(lons) - 0.1, max(lons) + 0.1 # depmin = 0.*km # depmax = 1.05*max(deps) center = od.Loc(0.5 * (latmin + latmax), 0.5 * (lonmin + lonmax)) # Map size if conf.map_radius is not None: safe_radius = conf.map_radius else: corners = [od.Loc(latmin, lonmin), od.Loc(latmin, lonmax)] dist1 = od.distance_accurate50m(center, corners[0]) dist2 = od.distance_accurate50m(center, corners[1]) safe_radius = max(dist1, dist2) # Generate the basic map m = Map(lat=center.lat, lon=center.lon, radius=safe_radius, width=30., height=30., show_grid=False, show_topo=False, color_dry=(238, 236, 230), topo_cpt_wet='light_sea_uniform', topo_cpt_dry='light_land_uniform', illuminate=True, illuminate_factor_ocean=0.15, show_rivers=False, show_plates=False) if conf.sw_filterevent: rectlons = [lonmin, lonmin, lonmax, lonmax, lonmin] rectlats = [latmin, latmax, latmax, latmin, latmin] m.gmt.psxy(in_columns=(rectlons, rectlats), W='thin,0/0/0,dashed', *m.jxyr) # Draw some larger cities covered by the map area m.draw_cities() # Events in clusters for id_cluster in clusters: col = cluster_to_color(id_cluster) mylats, mylons = [], [] for iev, evcl in enumerate(eventsclusters): if evcl == id_cluster: mylats.append(events[iev].lat) mylons.append(events[iev].lon) m.gmt.psxy(in_columns=(mylons, mylats), S='c7p', G=color2rgb(col), *m.jxyr) figname = os.path.join(plotdir, 'plot_map.' + conf.figure_format) m.save(figname)