m.draw_cities() # Generate with latitute, longitude and labels of the stations stations = model.load_stations('stations_deadsea.pf') lats = [s.lat for s in stations] lons = [s.lon for s in stations] labels = ['.'.join(s.nsl()) for s in stations] # Stations as black triangles. Genuine GMT commands can be parsed by the maps' # gmt attribute. Last argument of the psxy function call pipes the maps' # pojection system. m.gmt.psxy(in_columns=(lons, lats), S='t20p', G='black', *m.jxyr) # Station labels for i in range(len(stations)): m.add_label(lats[i], lons[i], labels[i]) # Load events from catalog file (generated using catalog.GlobalCMT() # download from www.globalcmt.org) # If no moment tensor is provided in the catalogue, the event is plotted # as a red circle. Symbol size relative to magnitude. events = model.load_events('deadsea_events_1976-2017.txt') beachball_symbol = 'd' factor_symbl_size = 5.0 for ev in events: mag = ev.magnitude if ev.moment_tensor is None: ev_symb = 'c' + str(mag * factor_symbl_size) + 'p' m.gmt.psxy(in_rows=[[ev.lon, ev.lat]], S=ev_symb,
def call(self): if not self.viewer_connected: self.get_viewer().about_to_close.connect( self.file_serving_worker.stop) self.viewer_connected = True try: from OpenGL import GL # noqa except ImportError: logger.warn('Could not find package OpenGL, ' 'if the map does not work try installing OpenGL\n' 'e.g. sudo pip install PyOpenGL') self.cleanup() try: viewer = self.get_viewer() cli_mode = False except NoViewerSet: viewer = None cli_mode = True if not cli_mode: if self.only_active: _, active_stations = \ self.get_active_event_and_stations() else: active_stations = viewer.stations.values() elif cli_mode: active_stations = self.stations station_list = [] if active_stations: for stat in active_stations: is_blacklisted = util.match_nslc(viewer.blacklist, stat.nsl()) if (viewer and not is_blacklisted) or cli_mode: xml_station_marker = XMLStationMarker( nsl='.'.join(stat.nsl()), longitude=float(stat.lon), latitude=float(stat.lat), active='yes') station_list.append(xml_station_marker) active_station_list = StationMarkerList(stations=station_list) if self.only_active: markers = [viewer.get_active_event_marker()] else: if cli_mode: markers = self.markers else: markers = self.get_selected_markers() if len(markers) == 0: tmin, tmax = self.get_selected_time_range(fallback=True) markers = [ m for m in viewer.get_markers() if isinstance(m, gui_util.EventMarker) and m.tmin >= tmin and m.tmax <= tmax ] ev_marker_list = [] for m in markers: if not isinstance(m, gui_util.EventMarker): continue xmleventmarker = convert_event_marker(m) if xmleventmarker is None: continue ev_marker_list.append(xmleventmarker) event_list = EventMarkerList(events=ev_marker_list) event_station_list = MarkerLists( station_marker_list=active_station_list, event_marker_list=event_list) event_station_list.validate() if self.map_kind != 'GMT': tempdir = self.marker_tempdir if self.map_kind == 'Google Maps': map_fn = 'map_googlemaps.html' elif self.map_kind == 'OpenStreetMap': map_fn = 'map_osm.html' url = 'http://localhost:' + str(self.port) + '/%s' % map_fn files = ['loadxmldoc.js', 'map_util.js', 'plates.kml', map_fn] snuffling_dir = op.dirname(op.abspath(__file__)) for entry in files: shutil.copy(os.path.join(snuffling_dir, entry), os.path.join(tempdir, entry)) logger.debug('copied data to %s' % tempdir) markers_fn = os.path.join(self.marker_tempdir, 'markers.xml') self.data_proxy.content_to_serve.emit(self.port) dump_xml(event_station_list, filename=markers_fn) if self.open_external: qg.QDesktopServices.openUrl(qc.QUrl(url)) else: global g_counter g_counter += 1 self.web_frame(url, name='Map %i (%s)' % (g_counter, self.map_kind)) else: lats_all = [] lons_all = [] slats = [] slons = [] slabels = [] for s in active_stations: slats.append(s.lat) slons.append(s.lon) slabels.append('.'.join(s.nsl())) elats = [] elons = [] elats = [] elons = [] psmeca_input = [] markers = self.get_selected_markers() for m in markers: if isinstance(m, gui_util.EventMarker): e = m.get_event() elats.append(e.lat) elons.append(e.lon) if e.moment_tensor is not None: mt = e.moment_tensor.m6() psmeca_input.append((e.lon, e.lat, e.depth / 1000., mt[0], mt[1], mt[2], mt[3], mt[4], mt[5], 1., e.lon, e.lat, e.name)) else: if e.magnitude is None: moment = -1. else: moment = moment_tensor.magnitude_to_moment( e.magnitude) psmeca_input.append( (e.lon, e.lat, e.depth / 1000., moment / 3., moment / 3., moment / 3., 0., 0., 0., 1., e.lon, e.lat, e.name)) lats_all.extend(elats) lons_all.extend(elons) lats_all.extend(slats) lons_all.extend(slons) lats_all = num.array(lats_all) lons_all = num.array(lons_all) if len(lats_all) == 0: return center_lat, center_lon = ortho.geographic_midpoint( lats_all, lons_all) ntotal = len(lats_all) clats = num.ones(ntotal) * center_lat clons = num.ones(ntotal) * center_lon dists = ortho.distance_accurate50m_numpy(clats, clons, lats_all, lons_all) maxd = num.max(dists) or 0. m = Map(lat=center_lat, lon=center_lon, radius=max(10000., maxd) * 1.1, width=35, height=25, show_grid=True, show_topo=True, 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) m.gmt.psxy(in_columns=(slons, slats), S='t15p', G='black', *m.jxyr) for i in range(len(active_stations)): m.add_label(slats[i], slons[i], slabels[i]) m.gmt.psmeca(in_rows=psmeca_input, S='m1.0', G='red', C='5p,0/0/0', *m.jxyr) tmpdir = self.tempdir() self.outfn = os.path.join(tmpdir, '%i.png' % self.figcount) m.save(self.outfn) f = self.pixmap_frame(self.outfn) # noqa
m.draw_cities() # Generate with latitute, longitude and labels of the stations stations = model.load_stations('stations_deadsea.pf') lats = [s.lat for s in stations] lons = [s.lon for s in stations] labels = ['.'.join(s.nsl()) for s in stations] # Stations as black triangles. Genuine GMT commands can be parsed by the maps' # gmt attribute. Last argument of the psxy function call pipes the maps' # pojection system. m.gmt.psxy(in_columns=(lons, lats), S='t20p', G='black', *m.jxyr) # Station labels for i in range(len(stations)): m.add_label(lats[i], lons[i], labels[i]) # Load events from catalog file (generated using catalog.GlobalCMT() to download from www.globalcmt.org) # If no moment tensor is provided in the catalogue, the event is plotted as a red circle. # Symbol size relative to magnitude. events = model.load_events('deadsea_events_1976-2017.txt') beachball_symbol = 'd' factor_symbl_size = 5.0 for ev in events: mag = ev.magnitude if ev.moment_tensor is None: ev_symb = 'c'+str(mag*factor_symbl_size)+'p' m.gmt.psxy( in_rows=[[ev.lon, ev.lat]],
def plot_map(stations, center, events=None, savename=None): from pyrocko.plot.automap import Map from pyrocko.example import get_example_data from pyrocko import model, gmtpy from pyrocko import moment_tensor as pmt gmtpy.check_have_gmt() # Generate the basic map m = Map(lat=center[0], lon=center[1], radius=150000., width=30., height=30., show_grid=False, show_topo=True, 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) # Draw some larger cities covered by the map area m.draw_cities() # Generate with latitute, longitude and labels of the stations lats = [s.lat for s in stations] lons = [s.lon for s in stations] labels = ['.'.join(s.nsl()) for s in stations] # Stations as black triangles. m.gmt.psxy(in_columns=(lons, lats), S='t20p', G='black', *m.jxyr) # Station labels for i in range(len(stations)): m.add_label(lats[i], lons[i], labels[i]) beachball_symbol = 'd' factor_symbl_size = 5.0 if events is not None: for ev in events: mag = ev.magnitude if ev.moment_tensor is None: ev_symb = 'c' + str(mag * factor_symbl_size) + 'p' m.gmt.psxy(in_rows=[[ev.lon, ev.lat]], S=ev_symb, G=gmtpy.color('scarletred2'), W='1p,black', *m.jxyr) else: devi = ev.moment_tensor.deviatoric() beachball_size = mag * 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) data = (ev.lon, ev.lat, 10) + tuple(m6) + (1, 0, 0) 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)) m.gmt.psmeca(in_rows=[data], G=gmtpy.color('chocolate1'), E='white', W='1p,%s' % gmtpy.color('chocolate3'), *m.jxyr, **kwargs) if savename is None: if events is None: m.save('pics/stations_ridgecrest.png') else: m.save('pics/mechanisms_scedc_ridgecrest.png') else: m.save(savename)
def plot_corr_angles(ns, st_lats, st_lons, orientfile, dir_orient, pl_options, pl_topo, mapsize, ls=False): ''' Plot correction angles of all stations on a map. nans are igored. Values for plotting are read from file which was automatically prepared in the orient section. :param ls: label position [lon (symbol), lat (symbol + label), lon (label)] ''' logs = logging.getLogger('plot_corr_angles') gmtconf = dict(MAP_TICK_PEN_PRIMARY='1.25p', MAP_TICK_PEN_SECONDARY='1.25p', MAP_TICK_LENGTH_PRIMARY='0.2c', MAP_TICK_LENGTH_SECONDARY='0.6c', FONT_ANNOT_PRIMARY='20p,1,black', FONT_LABEL='20p,1,black', PS_CHAR_ENCODING='ISOLatin1+', MAP_FRAME_TYPE='fancy', FORMAT_GEO_MAP='D', PS_PAGE_ORIENTATION='portrait', MAP_GRID_PEN_PRIMARY='thinnest,0/50/0', MAP_ANNOT_OBLIQUE='6') angles_fromfile = load(filename=os.path.join(dir_orient, orientfile)) angle_no_nan = [] lat_no_nan = [] lon_no_nan = [] angle_no_nan_u = [] lat_no_nan_u = [] lon_no_nan_u = [] stats_no_nan = [] stats_no_nan_u = [] for i_ns, ns_now in enumerate(ns): for l in ['00', '', '01', '10', '60']: try: ns_now_ = '%s %s %s' % (ns_now[0], ns_now[1], l) a = angles_fromfile.CorrectAngl_perStat_median[ns_now_] nev = angles_fromfile.n_events[ns_now_] if a > -181.0 and a < 180.0: # not nan if nev >= 5: angle_no_nan.append(0.0 - a) stats_no_nan.append(ns_now[1]) lat_no_nan.append(st_lats[i_ns]) lon_no_nan.append(st_lons[i_ns]) else: angle_no_nan_u.append(0.0 - a) stats_no_nan_u.append(ns_now[1]) lat_no_nan_u.append(st_lats[i_ns]) lon_no_nan_u.append(st_lons[i_ns]) except KeyError: continue # Generate the basic map m = Map( lat=pl_options[0], lon=pl_options[1], radius=pl_options[2], width=mapsize[0], height=mapsize[1], show_grid=False, show_topo=pl_topo, #color_dry=(143, 188, 143), #(238, 236, 230), topo_cpt_wet='white_sea_land', #'light_sea_uniform', topo_cpt_dry='light_land_uniform', illuminate=True, illuminate_factor_ocean=0.15, show_rivers=True, show_plates=False, gmt_config=gmtconf) # Draw some larger cities covered by the map area # m.draw_cities() # Draw max. amplitudes at station locations as colored circles cptfile = 'tempfile2.cpt' abs_angs = list(num.abs(angle_no_nan)) m.gmt.makecpt(C='jet', T='%g/%g' % (0.1, 180.), out_filename=cptfile) #, suppress_defaults=True) # m.gmt.makecpt( # C='/home/gesap/Documents/CETperceptual_GMT/CET-D8.cpt', # T='%g/%g' % (0.1, 180.), # out_filename=cptfile)#, suppress_defaults=True) # same length for every vector: length = [1.5 for a in range(len(lat_no_nan))] length_u = [0.7 for a in range(len(lat_no_nan_u))] # angle_zero = [1.5 for a in range(len(lat_no_nan))] # angle_zero_u = [1.5 for a in range(len(lat_no_nan_u))] # plot obtained rotation vectors: #m.gmt.psxy(in_columns=(lon_no_nan, lat_no_nan, angle_zero, length), # S='V0.5c+jc', W='0.07c,black', # *m.jxyr) #m.gmt.psxy(in_columns=(lon_no_nan_u, lat_no_nan_u, angle_zero_u, length_u), # S='V0.5c+jc', W='0.07c,black', # *m.jxyr) m.gmt.psxy(in_columns=(lon_no_nan_u, lat_no_nan_u, angle_no_nan_u, length_u), S='V0.4c+jc+eA', W='0.05c,black', *m.jxyr) m.gmt.psxy(in_columns=(lon_no_nan, lat_no_nan, abs_angs, angle_no_nan, length), C=cptfile, S='V0.7c+jc+eA', W='0.1c+cl', *m.jxyr) # add handmade label if ls: m.gmt.psxy(in_columns=([ls[0]], [ls[1]], [ls[3]], [0.9]), S='V0.5c+eA', W='0.07c,red', *m.jxyr) labels = ['Sensor orientation'] lat_lab = [ls[1]] lon_lab = [ls[2]] for i in range(len(labels)): m.add_label(lat_lab[i], lon_lab[i], labels[i]) # add a colorbar B_opt_psscale = 'xaf' m.gmt.psscale( B=B_opt_psscale + '+l abs. misorientation [deg]', D='x9c/6c+w12c/0.5c+jTC+h', # 'x9c/17c+w12c/0.5c+jTC+h' C=cptfile) # add station labels has_label = [] for i in range(len(stats_no_nan)): if stats_no_nan[i] not in has_label: m.add_label(lat_no_nan[i], lon_no_nan[i], stats_no_nan[i]) has_label.append(stats_no_nan[i]) has_label = [] for i in range(len(stats_no_nan_u)): if stats_no_nan_u[i] not in has_label: m.add_label(lat_no_nan_u[i], lon_no_nan_u[i], stats_no_nan_u[i]) has_label.append(stats_no_nan_u[i]) m.save(os.path.join(dir_orient, 'map_orient.png')) logs.info('Saved map with corr. angles for sensor orientations')