def setup_vtk_map_actor(lat, lon, radius, super_elevation=1, decimation=1,
                        smoothing=False):
    m = Map(lat=lat, lon=lon, radius=radius)
    m._setup()
    fn, ilum = m._prep_topo('land')
    topodata = gmtpy.loadgrd(fn)
    return get_topography_actor(*topodata, super_elevation=super_elevation,
                                decimation=decimation, smoothing=smoothing)
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
    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