Example #1
0
    def plot(self):

        self.installdir = None
        if 'installdir' in self.meta:
            self.installdir = self.meta['installdir']

        self.configfile = None
        if 'configfile' in self.meta:
            self.configfile = self.meta['configfile']

        if 'color' in self.meta:
            color_scale = self.meta['color']

        if 'gate' in self.meta:
            scale_gate = int(self.meta['gate'])
        else:
            scale_gate = None

        if color_scale == "b" and scale_gate is None:
            scale_gate = 2.5

        if self.startingpoint.description == None:
            location_text = ""
        else:
            location_text = self.startingpoint.description + " "

        if 'data_type' in self.meta:
            mproperty = self.meta['data_type']
        else:
            mproperty = "vs"

        # Gets the better CVM description if it exists.
        try:
            cvmdesc = UCVM_CVMS[self.cvm]
        except:
            cvmdesc = self.cvm

        if 'title' in self.meta:
            title = self.meta['title']
        else:
            title = "%s%s Cross Section from (%.2f, %.2f) to (%.2f, %.2f)" % (location_text, cvmdesc, self.startingpoint.longitude, \
                        self.startingpoint.latitude, self.endingpoint.longitude, self.endingpoint.latitude)
            self.meta['title'] = title

        self.getplotvals(mproperty)

        # Call the plot object.
        p = Plot(None, None, None, None, 10, 10)

        plt.axes([0.1, 0.7, 0.8, 0.25])

        # Figure out which is upper-right and bottom-left.
        ur_lat = self.startingpoint.latitude if self.startingpoint.latitude > self.endingpoint.latitude else self.endingpoint.latitude
        ur_lon = self.startingpoint.longitude if self.startingpoint.longitude > self.endingpoint.longitude else self.endingpoint.longitude
        ll_lat = self.startingpoint.latitude if self.startingpoint.latitude < self.endingpoint.latitude else self.endingpoint.latitude
        ll_lon = self.startingpoint.longitude if self.startingpoint.longitude < self.endingpoint.longitude else self.endingpoint.longitude

        # Add 1% to each for good measure.
        ur_lat = ur_lat + 0.03 * ur_lat
        ur_lon = ur_lon - 0.015 * ur_lon
        ll_lat = ll_lat - 0.03 * ll_lat
        ll_lon = ll_lon + 0.015 * ll_lon

        # Plot map up top.
        m = basemap.Basemap(projection='cyl', llcrnrlat=ll_lat, urcrnrlat=ur_lat, \
                            llcrnrlon=ll_lon, urcrnrlon=ur_lon, \
                            resolution='f', anchor='C')

        lat_ticks = np.arange(ll_lat, ur_lat + 0.1, (ur_lat - ll_lat))
        lon_ticks = np.arange(ll_lon, ur_lon + 0.1, (ur_lon - ll_lon))

        m.drawparallels(lat_ticks, linewidth=1.0, labels=[1, 0, 0, 0])
        m.drawmeridians(lon_ticks, linewidth=1.0, labels=[0, 0, 0, 1])
        m.drawstates()
        m.drawcountries()

        m.drawcoastlines()
        m.drawmapboundary(fill_color='aqua')
        m.fillcontinents(color='brown', lake_color='aqua')

        m.plot([self.startingpoint.longitude, self.endingpoint.longitude],
               [self.startingpoint.latitude, self.endingpoint.latitude])

        valign1 = "top"
        valign2 = "bottom"
        if self.endingpoint.latitude < self.startingpoint.latitude:
            valign1 = "bottom"
            valign2 = "top"

        plt.text(self.startingpoint.longitude, self.startingpoint.latitude, \
                 '[S] %.1f, %.1f' % (self.startingpoint.longitude, self.startingpoint.latitude), \
                 color='k', horizontalalignment="center", verticalalignment=valign1)

        plt.text(self.endingpoint.longitude, self.endingpoint.latitude, \
                 '[E] %.1f, %.1f' % (self.endingpoint.longitude, self.endingpoint.latitude), \
                 color='k', horizontalalignment="center", verticalalignment=valign2)

        plt.axes([0.05, 0.18, 0.9, 0.54])

        datapoints = np.arange(self.num_x * self.num_y,
                               dtype=np.float32).reshape(
                                   self.num_y, self.num_x)

        for y in xrange(0, self.num_y):
            for x in xrange(0, self.num_x):
                if self.datafile != None:
                    datapoints[y][x] = self.materialproperties[y][
                        x].getProperty(mproperty)
                elif mproperty != "poisson":
                    datapoints[y][x] = self.materialproperties[y][
                        x].getProperty(mproperty)
                else:
                    if self.materialproperties[y][
                            x].vp == 0 or self.materialproperties[y][
                                x].vs == 0.0:
                        datapoints[y][x] = 0.0
                    else:
                        datapoints[y][x] = self.materialproperties[y][
                            x].getProperty("vp") / self.materialproperties[y][
                                x].getProperty("vs")

        u = UCVM(install_dir=self.installdir, config_file=self.configfile)

        myInt = 1000
        newdatapoints = datapoints / myInt

        self.max_val = np.nanmax(newdatapoints)
        self.min_val = np.nanmin(newdatapoints)
        self.mean_val = np.mean(newdatapoints)

        BOUNDS = u.makebounds()
        TICKS = u.maketicks()

        if mproperty == "vp":
            BOUNDS = [bound * 1.7 for bound in BOUNDS]
            TICKS = [tick * 1.7 for tick in TICKS]

        # Set default colormap and range
        colormap = basemap.cm.GMT_seis
        norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)

        umax = round(self.max_val)
        if (umax < 5):
            umax = 5
        umin = round(self.min_val)

        if color_scale == "s":
            colormap = basemap.cm.GMT_seis
            norm = mcolors.Normalize(vmin=0, vmax=umax)
        elif color_scale == "s_r":
            colormap = basemap.cm.GMT_seis_r
            norm = mcolors.Normalize(vmin=0, vmax=umax)
        elif color_scale == "sd":
            BOUNDS = u.makebounds(self.min_val,
                                  self.max_val,
                                  5,
                                  self.mean_val,
                                  substep=5)
            colormap = basemap.cm.GMT_seis
            TICKS = u.maketicks(self.min_val, self.max_val, 5)
            norm = mcolors.Normalize(vmin=self.min_val, vmax=self.max_val)
        elif color_scale == "b":
            C = []
            for bound in BOUNDS:
                if bound < scale_gate:
                    C.append("grey")
                else:
                    C.append("red")
            colormap = mcolors.ListedColormap(C)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        elif color_scale == 'd':
            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_seis,
                                            len(BOUNDS) - 1)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        elif color_scale == 'd_r':
            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_seis_r,
                                            len(BOUNDS) - 1)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        elif color_scale == 'dd':
            BOUNDS = u.makebounds(self.min_val,
                                  self.max_val,
                                  5,
                                  self.mean_val,
                                  substep=5)
            TICKS = u.maketicks(self.min_val, self.max_val, 5)
            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_seis,
                                            len(BOUNDS) - 1)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        else:
            print "ERROR: unknown option for colorscale."

## MEI, TODO this is a temporary way to generate an output of a cross_section input file
        if (self.datafile == None):
            self.meta['num_x'] = self.num_x
            self.meta['num_y'] = self.num_y
            self.meta['datapoints'] = datapoints.size
            self.meta['max'] = np.asscalar(self.max_val)
            self.meta['min'] = np.asscalar(self.min_val)
            self.meta['mean'] = np.asscalar(self.mean_val)
            self.meta['lon_list'] = self.lon_list
            self.meta['lat_list'] = self.lat_list
            self.meta['depth_list'] = self.depth_list
            if self.filename:
                u.export_metadata(self.meta, self.filename)
                u.export_binary(datapoints, self.filename)
            else:
                #https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python
                rnd = ''.join(
                    random.SystemRandom().choice(string.ascii_uppercase +
                                                 string.digits)
                    for _ in range(6))
                f = "cross_section" + rnd
                u.export_metadata(self.meta, f)
                u.export_binary(datapoints, f)

        img = plt.imshow(newdatapoints, cmap=colormap, norm=norm)
        plt.xticks([0,self.num_x/2,self.num_x], ["[S] %.3f" % self.startingpoint.longitude, \
                                                 "%.3f" % ((float(self.endingpoint.longitude) + float(self.startingpoint.longitude)) / 2), \
                                                 "[E] %.3f" % self.endingpoint.longitude])
        plt.yticks([0,self.num_y/2,self.num_y], ["%.2f" % (self.startingdepth/1000), \
                                                 "%.2f" % (self.startingdepth+ ((self.todepth-self.startingdepth)/2)/1000), \
                                                 "%.2f" % (self.todepth / 1000)])

        plt.title(title)

        cax = plt.axes([0.1, 0.1, 0.8, 0.02])
        cbar = plt.colorbar(img,
                            cax=cax,
                            orientation='horizontal',
                            ticks=TICKS,
                            spacing='regular')
        if mproperty != "poisson":
            cbar.set_label(mproperty.title() + " (km/s)")
        else:
            cbar.set_label("Vp/Vs")

        if self.filename:
            plt.savefig(self.filename)
        else:
            plt.show()
Example #2
0
    def plot(self, horizontal_label=None):

        if self.upperleftpoint.description == None:
            location_text = ""
        else:
            location_text = self.upperleftpoint.description + " "

        if 'data_type' in self.meta:
            mproperty = self.meta['data_type']
        else:
            mproperty = "vs"

        scale_gate = None
        if 'color' in self.meta:
            color_scale = self.meta['color']

        if 'gate' in self.meta:
            scale_gate = float(self.meta['gate'])

        if color_scale == "b" and scale_gate is None:
            scale_gate = 2.5

        # Gets the better CVM description if it exists.
        try:
            cvmdesc = UCVM_CVMS[self.cvm]
        except:
            cvmdesc = self.cvm

        if 'title' in self.meta:
            title = self.meta['title']
        else:
            title = "%s%s Horizontal Slice at %.0fm" % (
                location_text, cvmdesc, self.upperleftpoint.depth)
            self.meta['title'] = title

        self.getplotvals(mproperty)

        # Call the plot object.
        p = Plot(title, "", "", None, 10, 10)

        u = UCVM(install_dir=self.installdir, config_file=self.configfile)

        BOUNDS = u.makebounds()
        TICKS = u.maketicks()

        m = basemap.Basemap(projection='cyl', llcrnrlat=self.bottomrightpoint.latitude, \
                            urcrnrlat=self.upperleftpoint.latitude, \
                            llcrnrlon=self.upperleftpoint.longitude, \
                            urcrnrlon=self.bottomrightpoint.longitude, \
                            resolution='f', anchor='C')

        lat_ticks = np.arange(self.bottomrightpoint.latitude,
                              self.upperleftpoint.latitude + 0.1,
                              self.plot_height / 2)
        lon_ticks = np.arange(self.upperleftpoint.longitude,
                              self.bottomrightpoint.longitude + 0.1,
                              self.plot_width / 2)

        m.drawparallels(lat_ticks, linewidth=1.0, labels=[1, 0, 0, 0])
        m.drawmeridians(lon_ticks, linewidth=1.0, labels=[0, 0, 0, 1])
        m.drawstates()
        m.drawcountries()

        alons = np.arange(self.upperleftpoint.longitude,
                          self.bottomrightpoint.longitude, self.spacing)
        alats = np.arange(self.bottomrightpoint.latitude,
                          self.upperleftpoint.latitude, self.spacing)
        lons = np.linspace(self.upperleftpoint.longitude,
                           self.bottomrightpoint.longitude - self.spacing,
                           self.num_x - 1)
        lats = np.linspace(self.bottomrightpoint.latitude,
                           self.upperleftpoint.latitude - self.spacing,
                           self.num_y - 1)

        # Get the properties.
        datapoints = np.arange(self.num_x * self.num_y,
                               dtype=np.float32).reshape(
                                   self.num_y, self.num_x)

        nancnt = 0
        zerocnt = 0
        negcnt = 0
        print("total cnt is ", self.num_x * self.num_y)
        for i in xrange(0, self.num_y):
            for j in xrange(0, self.num_x):
                if (self.datafile != None):
                    datapoints[i][j] = self.materialproperties[i][
                        j].getProperty(mproperty)
                elif mproperty != "poisson":
                    if color_scale == "sd" or color_scale == "sd_r":
                        datapoints[i][j] = self.materialproperties[i][
                            j].getProperty(mproperty)
                        if (datapoints[i][j] == -1):
                            datapoints[i][j] = np.nan
                            nancnt = nancnt + 1
##to have blank background
##                        if (datapoints[i][j] == 0) :
##                            datapoints[i][j]=np.nan
##                            zerocnt=zerocnt+1
##
                    else:
                        datapoints[i][j] = self.materialproperties[i][
                            j].getProperty(mproperty)
                        if (datapoints[i][j] == 0):
                            # KEEP 0 as 0                           datapoints[i][j]=np.nan
                            zerocnt = zerocnt + 1
                        if (datapoints[i][j] < 0):
                            negcnt = negcnt + 1
                        if (datapoints[i][j] == -1):
                            datapoints[i][j] = np.nan
                            nancnt = nancnt + 1
                else:
                    datapoints[i][j] = u.poisson(
                        self.materialproperties[i][j].vs,
                        self.materialproperties[i][j].vp)

#        print (" total number of nancnt is ", nancnt)
#        print (" total number of zerocnt is ", zerocnt)
#        print (" total number of negcnt is ", negcnt)

        myInt = 1000
        if mproperty == "poisson":  ## no need to reduce.. should also be using sd or dd
            myInt = 1
            if color_scale == "s":
                color_scale = "sd"
            elif color_scale == "d":
                color_scale = "dd"

        newdatapoints = datapoints / myInt

        newmax_val = np.nanmax(newdatapoints)
        newmin_val = np.nanmin(newdatapoints)
        newmean_val = np.mean(newdatapoints)

        self.max_val = np.nanmax(datapoints)
        self.min_val = np.nanmin(datapoints)
        self.mean_val = np.mean(datapoints)

        if color_scale == "s":
            colormap = basemap.cm.GMT_seis
            norm = mcolors.Normalize(vmin=BOUNDS[0],
                                     vmax=BOUNDS[len(BOUNDS) - 1])
        elif color_scale == "s_r":
            colormap = basemap.cm.GMT_seis_r
            norm = mcolors.Normalize(vmin=BOUNDS[0],
                                     vmax=BOUNDS[len(BOUNDS) - 1])
        elif color_scale == "sd":
            BOUNDS = u.makebounds(newmin_val,
                                  newmax_val,
                                  5,
                                  newmean_val,
                                  substep=5)
            #            colormap = basemap.cm.GMT_globe
            colormap = basemap.cm.GMT_seis
            TICKS = u.maketicks(newmin_val, newmax_val, 5)
            norm = mcolors.Normalize(vmin=BOUNDS[0],
                                     vmax=BOUNDS[len(BOUNDS) - 1])
        elif color_scale == "b":
            C = []
            for bound in BOUNDS:
                if bound < scale_gate:
                    C.append("grey")
                else:
                    C.append("red")
            colormap = mcolors.ListedColormap(C)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        elif color_scale == "d":
            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_seis,
                                            len(BOUNDS) - 1)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        elif color_scale == "d_r":
            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_seis_r,
                                            len(BOUNDS) - 1)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        elif color_scale == 'dd':
            BOUNDS = u.makebounds(newmin_val,
                                  newmax_val,
                                  5,
                                  newmean_val,
                                  substep=5,
                                  all=True)
            TICKS = u.maketicks(newmin_val, newmax_val, 5)
            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_seis,
                                            len(BOUNDS) - 1)
            #            colormap = pycvm_cmapDiscretize(basemap.cm.GMT_globe, len(BOUNDS) - 1)
            norm = mcolors.BoundaryNorm(BOUNDS, colormap.N)
        else:
            print "ERROR: unknown option for colorscale."

        if (self.datafile == None):
            self.meta['num_x'] = self.num_x
            self.meta['num_y'] = self.num_y
            self.meta['datapoints'] = datapoints.size
            self.meta['max'] = np.asscalar(self.max_val)
            self.meta['min'] = np.asscalar(self.min_val)
            self.meta['mean'] = np.asscalar(self.mean_val)
            self.meta['lon_list'] = lons.tolist()
            self.meta['lat_list'] = lats.tolist()
            if self.filename:
                u.export_metadata(self.meta, self.filename)
                u.export_binary(datapoints, self.filename)

        ## reduce the datapoints before passing in..

        t = m.transform_scalar(newdatapoints, lons, lats, len(lons), len(lats))
        img = m.imshow(t, cmap=colormap, norm=norm)

        #        print "MIN is ", np.nanmin(datapoints)
        #        print "MAX is ", np.nanmax(datapoints)
        #        img=m.scatter(xlist, ylist, c=dlist, cmap=colormap, norm=norm, s=1, edgecolor='',marker='o')
        #        img=m.scatter(xcoords, ycoords, c=datapoints, cmap=colormap, norm=norm, s=1, edgecolor='',marker='o')

        m.drawcoastlines()

        cax = plt.axes([0.125, 0.05, 0.775, 0.02])
        cbar = plt.colorbar(img,
                            cax=cax,
                            orientation='horizontal',
                            spacing='proportional',
                            ticks=TICKS)
        if mproperty != "poisson":
            if horizontal_label == None:
                cbar.set_label(mproperty.title() + " (km/s)")
            else:
                cbar.set_label(horizontal_label)
        else:
            cbar.set_label("Poisson(Vs,Vp)")

        if self.filename:
            plt.savefig(self.filename)
## MEI, TODO p.savehtml("show.html")
        else:
            plt.show()