def geometry_plot(self,Npoints = 256):
        x = np.linspace(-self.a, self.a)
        y = np.linspace(-self.b, self.b)
        X,Y = np.meshgrid(x,y)
        n_plot = np.zeros(np.shape(X))
        k_plot = np.zeros(np.shape(X))
        for i,xx in enumerate(x):
            for j,yy in enumerate(y):
                n_plot[i,j] = ref([xx,yy])*10000000
                k_plot[i,j] = extinction([xx,yy])*10000000

                cmap1, norm1 = from_levels_and_colors([1,ref([r_core,0])*10000000,ref([r_clad,0])*10000000], ['blue', 'green','red'],extend='max')
                cmap2, norm2 = from_levels_and_colors([0,extinction([r_core,0])*10000000,extinction([r_clad,0])*10000000],  ['blue', 'green','red'],extend='max')

                fig = plt.figure(figsize=(20.0, 20.0))
                ax1 = fig.add_subplot(221)
                ax1.pcolormesh(X,Y,n_plot, cmap=cmap1, norm=norm1)
                ax1.set_title('real part profile')
                ax1.axis('equal')
                
                ax2 = fig.add_subplot(222)
                ax2.pcolormesh(X,Y,k_plot, cmap=cmap2, norm=norm2)
                ax2.set_title('Imaginary part  profile')
                ax2.axis('equal')
                return n_plot,k_plot
def testplot(cats, catsavg, xy, data, levels=[0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 100], title=""):
    """Quick test plot layout for this example file
    """
    colors = plt.cm.spectral(np.linspace(0, 1, len(levels)))
    mycmap, mynorm = from_levels_and_colors(levels, colors, extend="max")

    radolevels = [0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 100]
    radocolors = plt.cm.spectral(np.linspace(0, 1, len(radolevels)))
    radocmap, radonorm = from_levels_and_colors(radolevels, radocolors, extend="max")

    fig = plt.figure(figsize=(14, 8))

    # Average rainfall sum
    ax = fig.add_subplot(121, aspect="equal")
    coll = PatchCollection(cats, array=catsavg, cmap=mycmap, norm=mynorm, edgecolors='white', lw=0.5)
    ax.add_collection(coll)
    ax.autoscale()
    plt.colorbar(coll, ax=ax, shrink=0.5)
    plt.xlabel("GK2 Easting")
    plt.ylabel("GK2 Northing")
    plt.title(title)
    plt.draw()

    # Original radar data
    ax1 = fig.add_subplot(122, aspect="equal")
    pm = plt.pcolormesh(xy[:, :, 0], xy[:, :, 1], np.ma.masked_invalid(data), cmap=radocmap, norm=radonorm)
    coll = PatchCollection(cats, facecolor='None', edgecolor='white', lw=0.5)
    ax1.add_collection(coll)
    cb = plt.colorbar(pm, ax=ax1, shrink=0.5)
    cb.set_label("(mm/h)")
    plt.xlabel("GK2 Easting")
    plt.ylabel("GK2 Northing")
    plt.title("Original radar rain sums")
    plt.draw()
    plt.tight_layout()
Ejemplo n.º 3
0
def test_cmap_and_norm_from_levels_and_colors2():
    levels = [-1, 2, 2.5, 3]
    colors = ['red', (0, 1, 0), 'blue', (0.5, 0.5, 0.5), (0.0, 0.0, 0.0, 1.0)]
    clr = mcolors.to_rgba_array(colors)
    bad = (0.1, 0.1, 0.1, 0.1)
    no_color = (0.0, 0.0, 0.0, 0.0)
    masked_value = 'masked_value'

    # Define the test values which are of interest.
    # Note: levels are lev[i] <= v < lev[i+1]
    tests = [('both', None, {-2: clr[0],
                             -1: clr[1],
                             2: clr[2],
                             2.25: clr[2],
                             3: clr[4],
                             3.5: clr[4],
                             masked_value: bad}),

             ('min', -1, {-2: clr[0],
                          -1: clr[1],
                          2: clr[2],
                          2.25: clr[2],
                          3: no_color,
                          3.5: no_color,
                          masked_value: bad}),

             ('max', -1, {-2: no_color,
                          -1: clr[0],
                          2: clr[1],
                          2.25: clr[1],
                          3: clr[3],
                          3.5: clr[3],
                          masked_value: bad}),

             ('neither', -2, {-2: no_color,
                              -1: clr[0],
                              2: clr[1],
                              2.25: clr[1],
                              3: no_color,
                              3.5: no_color,
                              masked_value: bad}),
             ]

    for extend, i1, cases in tests:
        cmap, norm = mcolors.from_levels_and_colors(levels, colors[0:i1],
                                                    extend=extend)
        cmap.set_bad(bad)
        for d_val, expected_color in cases.items():
            if d_val == masked_value:
                d_val = np.ma.array([1], mask=True)
            else:
                d_val = [d_val]
            assert_array_equal(expected_color, cmap(norm(d_val))[0],
                               'Wih extend={0!r} and data '
                               'value={1!r}'.format(extend, d_val))

    with pytest.raises(ValueError):
        mcolors.from_levels_and_colors(levels, colors)
def smart_colormap(clevs, name='jet', extend='both', minval=0.0, maxval=1.0):
    '''
    Automatically grabs the colors to extend the colorbar from the colormap.
    '''
    
    # Define number of colors
    if extend == 'both':
        nrColors = len(clevs)+1
    elif (extend == 'min') | (extend == 'max'):
        nrColors = len(clevs)
    elif (extend == 'neither'):
        nrColors = len(clevs)-1
    else:
        nrColors = len(clevs)-1
        extend = 'neither'
    
    # Get colormap
    cmap = get_cmap(name, nrColors)
    
    # Truncate colormap if asked
    if (minval != 0.0) or (maxval != 1.0):
        cmap = truncate_colormap(cmap, minval=minval, maxval=maxval, n=nrColors/2)
    
    # Get the list of colors
    colors = []
    for i in range(0, nrColors):
        colors.append(cmap(i/(nrColors-1)))
    
    # Use utility function to get cmap and norm at the same time
    cmap, norm = from_levels_and_colors(clevs, colors, extend=extend)

    return(cmap, norm)
Ejemplo n.º 5
0
def make_plot(a, b, z, title, maxterms):
    approx, terms = optimal_terms.asymptotic_series(a, b, z, maxterms)
    ref = np.float64(mpmath.hyp1f1(a, b, z))

    cd = correct_digits(approx, ref)
    termsize = np.abs(terms/terms[0])

    fig, ax1 = plt.subplots()
    ax1.plot(cd, '-', linewidth=2, color=GREEN)
    ax1.set_ylim(0, 17)
    ax1.set_xlabel('term number')
    # Make the y-axis label and tick labels match the line color.
    ax1.set_ylabel('correct digits', color=GREEN)
    for tl in ax1.get_yticklabels():
        tl.set_color(GREEN)

    ax2 = ax1.twinx()
    cmap, norm = colors.from_levels_and_colors([-np.inf, 0, np.inf],
                                               [BLUE, RED])
    ax2.scatter(np.arange(termsize.shape[0]), termsize,
                c=terms, cmap=cmap, norm=norm, edgecolors='')
    # ax2.semilogy(termsize, 'r--', linewidth=2)
    ax2.set_yscale('log')
    ax2.set_ylabel('relative term size', color=RED)
    # Set the limits, with a little margin.
    ax2.set_xlim(0, termsize.shape[0])
    ax2.set_ylim(np.min(termsize), np.max(termsize))
    for tl in ax2.get_yticklabels():
        tl.set_color(RED)

    ax1.set_title("a = {:.2e}, b = {:.2e}, z = {:.2e}".format(a, b, z))

    plt.savefig("{}.png".format(title))
Ejemplo n.º 6
0
    def animate(self, steps=10, start_fire_power=0):
        fig = plt.figure()

        colors = ['#990000', '#CC0000', '#FF0000', '#FF8000', '#FFFF00',
                  '#AFCB96',
                  '#66FF66', '#33FF33', '#00FF00', '#00CC00', '#009900']
        scale = [-1000, -80, -60, -40, -20, -0.1, 0.1, 50, 100, 200, 400, 1000]
        cmap, norm = mcolors.from_levels_and_colors(scale, colors)

        df = self.prepare_data_to_draw()
        images = [[plt.pcolor(df, cmap=cmap, norm=norm)]]

        self.start_fire(start_fire_power)
        df = self.prepare_data_to_draw()
        images.append([plt.pcolor(df, cmap=cmap, norm=norm)])
        drzewa = [sum([x.wood for x in sum(self.forest.forest, [])])]
        ogien = [sum([x.fire for x in sum(self.forest.forest, [])])]
        for i in range(steps):
            print i
            self.next_step()
            self.burn()
            drzewa.append(sum([x.wood for x in sum(self.forest.forest, [])]))
            ogien.append(sum([x.fire for x in sum(self.forest.forest, [])]))
            df = self.prepare_data_to_draw()
            images.append([plt.pcolor(df, cmap=cmap, norm=norm,)])

        ani = animation.ArtistAnimation(fig, images, interval=500, repeat_delay=0, repeat=True)
        plt.show()

        plt.plot(range(0, steps + 1), drzewa)
        plt.show()

        plt.plot(range(0, steps + 1), ogien)
        plt.show()
    def view(self, original, segmented, reduced):
        r"""
        This method is implemented for test purposes, it takes as arguments
        an untreated slice, a segmented slice and a reduced and segmented
        slice showing its differences on screen using a matplotlib figure

            view(original, segmented, reduced)
        """

        f = pyplot.figure()
        levels = [0, 1, 2]
        colores = ['red', 'white', 'blue', 'red']
        cmap, norm = colors.from_levels_and_colors(levels, colores, extend='both')

       # org = self.sample_mask(original) #Mask irelevant data

        f.add_subplot(1, 3, 1)  # Original image
        pyplot.imshow(original, cmap='binary', interpolation='nearest')
        pyplot.colorbar()

        f.add_subplot(1, 3, 2)  # Segmented image  by K-means
        pyplot.imshow(segmented, interpolation='nearest', cmap=cmap, norm=norm)
        pyplot.colorbar()

        f.add_subplot(1, 3, 3)  # Reduced image
        pyplot.imshow(reduced, interpolation='nearest',
                      origin='lower', cmap = cmap, norm = norm)
        pyplot.colorbar()
        pyplot.show()
def heatmap(data_, num_levels_=20, mode=None, xlabel='', ylabel='', xlabels=None, ylabels=None, rotation=90, changeTicks=True):
    '''generates a nice heatmap'''
    if mode == 'special':
        vmin, vmax = data_.min(), data_.max()
        midpoint = data_.mean()
    else:
        vmin, vmax = -1.0001, 1.0001
        midpoint = 0.0
    levels = np.linspace(vmin, vmax, num_levels_)
    midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
    vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
    colors = plt.cm.seismic(vals)
    cmap, norm = from_levels_and_colors(levels, colors)
    fig, ax = plt.subplots()
    im = ax.imshow(data_, cmap=cmap, norm=norm, interpolation='none')
    fig.colorbar(im)

    ax.xaxis.tick_top()
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.xaxis.set_label_position('top')
    if xlabels is not None:
        ax.set_xticklabels(xlabels, minor=False)
    if ylabels is not None:
        ax.set_yticklabels(ylabels, minor=False)
    if changeTicks:
        plt.xticks(np.arange(len(data_)), rotation=rotation, fontsize=10)
        plt.yticks(np.arange(len(data_)), fontsize=10)

    plt.show()
Ejemplo n.º 9
0
def create_wind_color_map(levels):
    # replicate XWS colours for wind speed
    #
    cmap = ["#ffffff", "#fff4e8", "#ffe1c1", "#ffaa4e", "#ff6d00",
    		"#d33100", "#890000"] #, "#650000", "#390000"]
    ccmap, norm = col.from_levels_and_colors(levels, cmap, 'neither')
    return ccmap, norm
Ejemplo n.º 10
0
    def generer_image_transp(self):
        """Fonction graphique.

        Elle est identique à la fonction précédente mais permet de générer
        une image transparente sauf pour les individus qui possèdent une
        couleur par tribu. Les deux images sont ensuite superposée pour
        donner l'illusion que la simulation se déroule sur la carte du
        dessous.

        Arguments:
            verbose (bool):
                Active ou désactive la sortie console.

        Retour:
            Modifie img_transp par référence interne.

        """
        nombre_tribus = len(self.liste_tribus)

        levels = list(range(0, 6)) + self.liste_tribus_int
        colors_void = [(0., 0., 0., 0.) for i in range(0, 6)]
        jet = plt.get_cmap('gist_rainbow')
        colors_tribu = [jet(x/(max(self.liste_tribus_int) - 10)) for x in range(0, nombre_tribus)]
        colors = colors_void + colors_tribu

        self.colormap_indiv, self.norm = from_levels_and_colors(levels, colors, extend='max')

        self.img_transp = self.matrice_tribus.astype(str).astype(int)
Ejemplo n.º 11
0
def test_cmap_and_norm_from_levels_and_colors2():
    levels = [-1, 2, 2.5, 3]
    colors = ["red", (0, 1, 0), "blue", (0.5, 0.5, 0.5), (0.0, 0.0, 0.0, 1.0)]
    clr = mcolors.colorConverter.to_rgba_array(colors)
    bad = (0.1, 0.1, 0.1, 0.1)
    no_color = (0.0, 0.0, 0.0, 0.0)
    masked_value = "masked_value"

    # Define the test values which are of interest.
    # Note: levels are lev[i] <= v < lev[i+1]
    tests = [
        ("both", None, {-2: clr[0], -1: clr[1], 2: clr[2], 2.25: clr[2], 3: clr[4], 3.5: clr[4], masked_value: bad}),
        ("min", -1, {-2: clr[0], -1: clr[1], 2: clr[2], 2.25: clr[2], 3: no_color, 3.5: no_color, masked_value: bad}),
        ("max", -1, {-2: no_color, -1: clr[0], 2: clr[1], 2.25: clr[1], 3: clr[3], 3.5: clr[3], masked_value: bad}),
        (
            "neither",
            -2,
            {-2: no_color, -1: clr[0], 2: clr[1], 2.25: clr[1], 3: no_color, 3.5: no_color, masked_value: bad},
        ),
    ]

    for extend, i1, cases in tests:
        cmap, norm = mcolors.from_levels_and_colors(levels, colors[0:i1], extend=extend)
        cmap.set_bad(bad)
        for d_val, expected_color in cases.items():
            if d_val == masked_value:
                d_val = np.ma.array([1], mask=True)
            else:
                d_val = [d_val]
            assert_array_equal(
                expected_color, cmap(norm(d_val))[0], "Wih extend={0!r} and data " "value={1!r}".format(extend, d_val)
            )

    assert_raises(ValueError, mcolors.from_levels_and_colors, levels, colors)
def plotheat(data_, num_levels_=20, mode=None, xlabel='', ylabel='',
             xlabels=None, ylabels=None, rotation=90,
             changeTicks=True, annotation=None, vmin=None, vmax=None,
             showplot=True, removebar=False):
    '''generates a nice heatmap'''
    if mode == 'special':
        if vmin is None or vmax is None:
            vmin, vmax = data_.min() - 0.0001, data_.max() + 0.0001
            midpoint = data_.mean()
        else:
            midpoint = data_.mean()
    else:
        vmin, vmax = -1.0001, 1.0001
        midpoint = 0.0
    levels = np.linspace(vmin, vmax, num_levels_)
    midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
    vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
    colors = plt.cm.seismic(vals)
    # == 6 option is photocopy friendly, == 7 is almost photocopy friendly
    if num_levels_ == 6:
        colors = [[215, 25, 28, 255], [253, 174, 97, 255], [255, 255, 191, 255],
                  [171, 221, 164, 255], [43, 131, 186, 255]]
        colors = [[x / 255.0 for x in c] for c in colors]
    if num_levels_ == 7:
        colors = [[213, 62, 79, 255], [252, 141, 89, 255], [254, 224, 139, 255],
                  [230, 245, 152, 255], [153, 213, 148, 255], [50, 136, 189, 255]]
        colors = [[x / 255.0 for x in c] for c in colors]
    # print(colors)
    cmap, norm = from_levels_and_colors(levels, colors)
    fig, ax = plt.subplots()
    im = ax.imshow(data_, cmap=cmap, norm=norm, interpolation='none')
    if not removebar:
        fig.colorbar(im)

    ax.xaxis.tick_top()
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.xaxis.set_label_position('top')
    if xlabels is not None:
        ax.set_xticklabels(xlabels, minor=False)
    if ylabels is not None:
        ax.set_yticklabels(ylabels, minor=False)
    if changeTicks:
        plt.xticks(np.arange(len(data_)), rotation=rotation, fontsize=14)
        plt.yticks(np.arange(len(data_)), fontsize=14)

    if annotation is not None:
        for t in annotation:
            if len(t[1]) == 1:
                x = -50
            else:
                x = -35
            ax.annotate(t[1], xy=(0, t[0]), xytext=(x, t[0]),
                        arrowprops=dict(facecolor='black', shrink=0.05),
                        annotation_clip=False
                        )

    if showplot:
        plt.show()
Ejemplo n.º 13
0
def create_color_map():
    # this is the color scale from Neu et al 2013 (BAMS)
    levels = numpy.array([0,0.0001,0.001,0.01,0.1,1.0,2.0,3.0,4.0,5.0,6.0], 'f')
    cmap = ["#ffffff", "#f8e73d", "#e9a833", "#009a4a",
            "#00aeca", "#0088b7", "#295393", "#2e3065", 
            "#973683", "#ff0000", "#999999"]
    ccmap, norm = col.from_levels_and_colors(levels, cmap, 'max')
    return ccmap, norm, levels
Ejemplo n.º 14
0
def testplot(cats, catsavg, xy, data, levels = np.arange(0,320,20), title=""):
    """Quick test plot layout for this example file
    """
    colors = plt.cm.spectral(np.linspace(0,1,len(levels)) )    
    mycmap, mynorm = from_levels_and_colors(levels, colors, extend="max")

    radolevels = levels.copy()#[0,1,2,3,4,5,10,15,20,25,30,40,50,100]
    radocolors = plt.cm.spectral(np.linspace(0,1,len(radolevels)) )    
    radocmap, radonorm = from_levels_and_colors(radolevels, radocolors, extend="max")

    fig = plt.figure(figsize=(12,12))
    # Average rainfall sum
    ax = fig.add_subplot(111, aspect="equal")
    wradlib.vis.add_lines(ax, cats, color='black', lw=0.5)
    catsavg[np.isnan(catsavg)]=-9999
    coll = PolyCollection(cats, array=catsavg, cmap=mycmap, norm=mynorm, edgecolors='none')
    ax.add_collection(coll)
    ax.autoscale()
    cb = plt.colorbar(coll, ax=ax, shrink=0.75)
    plt.xlabel("UTM 51N Easting")
    plt.ylabel("UTM 51N Northing")
#    plt.title("Subcatchment rainfall depth")
    plt.grid()
    plt.draw()
    plt.savefig(r"E:\docs\_projektantraege\SUSTAIN_EU_ASIA\workshop\maik\pampanga_cat.png")
#    plt.close()
    # Original RADOLAN data
    fig = plt.figure(figsize=(12,12))
    # Average rainfall sum
    ax1 = fig.add_subplot(111, aspect="equal")
    pm = plt.pcolormesh(xy[:, :, 0], xy[:, :, 1], np.ma.masked_invalid(data), cmap=radocmap, norm=radonorm)
    wradlib.vis.add_lines(ax1, cats, color='black', lw=0.5)
    plt.xlim(ax.get_xlim())
    plt.ylim(ax.get_ylim())
    cb = plt.colorbar(pm, ax=ax1, shrink=0.75)
    cb.set_label("(mm/h)")
    plt.xlabel("UTM 51N Easting")
    plt.ylabel("UTM 51N Northing")
#    plt.title("Composite rainfall depths")
    plt.grid()
    plt.draw()
    plt.savefig(r"E:\docs\_projektantraege\SUSTAIN_EU_ASIA\workshop\maik\pampanga_comp.png")
    plt.close()
Ejemplo n.º 15
0
def setup_colormap_with_zeroval(vmin, vmax, nlevs=5,
                                cmap=plt.get_cmap('Greens'),
                                extend='both'):
    """create a discrete colormap with reserved level for small values

    setup a colormap based on a existing colormap with a specified
    number N of levels reserving the lowest colormap level for
    extremely small values.

    Extremely small are currently defined as [0.0, 1e-8].

    Returns the N-level colormap and a normalizer to plot arbitrary
    data using the colormap.  The normalizing places the N levels at
    constant intervals between the vmin and vmax.

    ARGS:
        vmin (float): value to map to the lowest color level
        vmax (float): value to map to the highest color level
        nlevs (integer): number of levels for the colormap (default is 5)
        cmap (:class:`matplotlib.colors.Colormap` instance): colormap to use for the N-level colormap
        extend (string): ({'both'} | 'min' | 'max' | 'neither') should the top or bottom of the colormap use an arrow to indicate "and larger" or "and smaller"

    RETURNS:
        tuple (cmap, norm) containing a
        :class:`matplotlib.colors.Colormap` object and a
        :class:`matplotlib.colors.Normalize object`.

    EXAMPLE:
        >>> import matplotlib.pyplot as plt
        >>> import numpy as np
        >>> from timutils.colormap_nlevs import setup_colormap_with_zeroval
        >>> data = np.random.rand(100, 100)
        >>> mycmap, mynorm = setup_colormap_with_zeroval(vmin=data.min(), vmax=data.max(), nlevs=7, extend='neither')
        >>> fig, ax = plt.subplots()
        >>> cm = ax.pcolormesh(data, cmap=mycmap, norm=mynorm)
        >>> plt.colorbar(cm)
        >>> plt.title(('setup_colormap_with_zeroval example\\n data values 0 to 1; nlevs=5'))
        >>> plt.show()

    """

    # Pick some of the nicer colors from the palette...
    if extend is "neither":
        ncolors = nlevs
    elif (extend is "min") or (extend is "max"):
        ncolors = nlevs + 1
    elif extend is "both":
        ncolors = nlevs + 2
    levels = np.concatenate((np.array([0.0, 1e-8]),
                             np.linspace(start=vmin,
                                         stop=vmax,
                                         num=nlevs)[1:]))
    colors = cmap(np.linspace(start=0.0, stop=1.0, num=ncolors))
    cmap, norm = from_levels_and_colors(levels, colors, extend=extend)
    return((cmap, norm))
Ejemplo n.º 16
0
def main():
    bathymetry_path = ""
    topo_path = "/RECH2/huziy/coupling/coupled-GL-NEMO1h_30min/geophys_452x260_directions_new_452x260_GL+NENA_0.1deg_SAND_CLAY_LDPT_DPTH.fst"




    plot_utils.apply_plot_params()

    with RPN(topo_path) as r:
        assert isinstance(r, RPN)
        topo = r.get_first_record_for_name("ME")
        lons, lats = r.get_longitudes_and_latitudes_for_the_last_read_rec()

        print(lons.shape)

        prj_params = r.get_proj_parameters_for_the_last_read_rec()
        rll = RotatedLatLon(**prj_params)
        bmap = rll.get_basemap_object_for_lons_lats(lons2d=lons, lats2d=lats, resolution="i")


    xx, yy = bmap(lons, lats)

    plt.figure()
    ax = plt.gca()

    lons1 = np.where(lons <= 180, lons, lons - 360)
    topo = maskoceans(lons1, lats, topo)


    topo_clevs = [0, 100, 200, 300, 400, 500, 600, 800, 1000, 1200]
    # bn = BoundaryNorm(topo_clevs, len(topo_clevs) - 1)
    cmap = cm.get_cmap("terrain")

    ocean_color = cmap(0.18)




    cmap, norm = colors.from_levels_and_colors(topo_clevs, cmap(np.linspace(0.3, 1, len(topo_clevs) - 1)))


    add_rectangle(ax, xx, yy, margin=20, edge_style="solid")
    add_rectangle(ax, xx, yy, margin=10, edge_style="dashed")



    im = bmap.pcolormesh(xx, yy, topo, cmap=cmap, norm=norm)
    bmap.colorbar(im, ticks=topo_clevs)
    bmap.drawcoastlines(linewidth=0.3)
    bmap.drawmapboundary(fill_color=ocean_color)
    bmap.drawparallels(np.arange(-90, 90, 10), labels=[1, 0, 0, 1], color="0.3")
    bmap.drawmeridians(np.arange(-180, 190, 10), labels=[1, 0, 0, 1], color="0.3")
    plt.savefig("GL_452x260_0.1deg_domain.png", dpi=300, bbox_inches="tight")
Ejemplo n.º 17
0
def get_colormap_norm(cmap_type, levels):
    """Create a custom colormap."""
    if cmap_type == "rain":
        cmap, norm = from_levels_and_colors(levels, sns.color_palette("Blues", n_colors=len(levels)),
                                                    extend='max')
    elif cmap_type == "snow":
        cmap, norm = from_levels_and_colors(levels, sns.color_palette("PuRd", n_colors=len(levels)),
                                                    extend='max')
    elif cmap_type == "snow_discrete":    
        colors = ["#DBF069","#5AE463","#E3BE45","#65F8CA","#32B8EB",
                    "#1D64DE","#E97BE4","#F4F476","#E78340","#D73782","#702072"]
        cmap, norm = from_levels_and_colors(levels, colors, extend='max')
    elif cmap_type == "rain_acc":    
        cmap, norm = from_levels_and_colors(levels, sns.color_palette('gist_stern_r', n_colors=len(levels)),
                         extend='max')
    elif cmap_type == "rain_new":
        colors_tuple = pd.read_csv('/home/mpim/m300382/icon_forecasts/cmap_prec.rgba').values    
        cmap, norm = from_levels_and_colors(levels, sns.color_palette(colors_tuple, n_colors=len(levels)),
                         extend='max')

    return(cmap, norm)
Ejemplo n.º 18
0
def test_cmap_and_norm_from_levels_and_colors():
    data = np.linspace(-2, 4, 49).reshape(7, 7)
    levels = [-1, 2, 2.5, 3]
    colors = ['red', 'green', 'blue', 'yellow', 'black']
    extend = 'both'
    cmap, norm = mcolors.from_levels_and_colors(levels, colors, extend=extend)

    ax = plt.axes()
    m = plt.pcolormesh(data, cmap=cmap, norm=norm)
    plt.colorbar(m)

    # Hide the axes labels (but not the colorbar ones, as they are useful)
    ax.tick_params(labelleft=False, labelbottom=False)
Ejemplo n.º 19
0
def test_cmap_and_norm_from_levels_and_colors():
    data = np.linspace(-2, 4, 49).reshape(7, 7)
    levels = [-1, 2, 2.5, 3]
    colors = ['red', 'green', 'blue', 'yellow', 'black']
    extend = 'both'
    cmap, norm = mcolors.from_levels_and_colors(levels, colors, extend=extend)

    ax = plt.axes()
    m = plt.pcolormesh(data, cmap=cmap, norm=norm)
    plt.colorbar(m)

    # Hide the axes labels (but not the colorbar ones, as they are useful)
    for lab in ax.get_xticklabels() + ax.get_yticklabels():
        lab.set_visible(False)
Ejemplo n.º 20
0
def colorbar(nvalues,nticks=None,cmap='gnuplot', start=0.1, stop=0.8,strmap = '%.3f', shrink=1, aspect=20):
    ncolors = len(nvalues)
    if nticks==None:
        nticks = ncolors
    colors = mpltools.color.colors_from_cmap(ncolors, cmap=cmap, start=start, stop=stop)
    cmap, norm = mcolors.from_levels_and_colors(range(ncolors + 1), colors)
    mappable = matplotlib.cm.ScalarMappable(cmap=cmap)
    mappable.set_array([])
    mappable.set_clim(-0.5, ncolors+0.5)
    colorbar = plt.colorbar(mappable,shrink=shrink,aspect=aspect)
    tickpos = np.linspace(0, ncolors,nticks)
    tickvalues = np.linspace(min(nvalues), max(nvalues), nticks)
    tickvalues = [strmap%f for f in tickvalues]
    colorbar.set_ticks(tickpos)
    colorbar.set_ticklabels(tickvalues)
    return colorbar
Ejemplo n.º 21
0
def setup_colormap(vmin, vmax, nlevs=5,
                   cmap=plt.get_cmap('Greens'),
                   extend='both'):
    """
    create a discrete colormap from a continuous colormap

    Returns the N-level colormap and a normalizer to plot arbitrary
    data using the colormap.  The normalizing places the N levels at
    constant intervals between the vmin and vmax.

    ARGS:
        vmin (float): value to map to the lowest color level
        vmax (float): value to map to the highest color level
        nlevs (integer): number of levels for the colormap (default is 5)
        cmap (:class:`matplotlib.colors.Colormap` instance): colormap to use for the N-level colormap
        extend (string): ({'both'} | 'min' | 'max' | 'neither'}) should the top or bottom of the colormap use an arrow to indicate "and larger" or "and smaller"

    RETURNS:
        tuple (cmap, norm) containing a
        :class:`matplotlib.colors.Colormap` object and a
        :class:`matplotlib.colors.Normalize` object.

    EXAMPLES:
    >>> import matplotlib.pyplot as plt
    >>> import numpy as np
    >>> from timutils.colormap_nlevs import setup_colormap
    >>> data = np.random.rand(100, 100)  * 1000
    >>> mycmap, mynorm = setup_colormap(vmin=200.0, vmax=800.0, extend='max')
    >>> fig, ax = plt.subplots()
    >>> cm = ax.pcolormesh(data, cmap=mycmap, norm=mynorm)
    >>> plt.colorbar(cm)
    >>> plt.title(('setup_colormap example\\n data values 0 to 1000; nlevs=5, vmin=200, vmax=800'))
    >>> plt.show()

    """
    print('setting up colormaps')
    # Pick some of the nicer colors from the palette...
    if extend is "neither":
        ncolors = nlevs - 1
    elif (extend is "min") or (extend is "max"):
        ncolors = nlevs
    elif extend is "both":
        ncolors = nlevs + 1
    levels = np.linspace(start=vmin, stop=vmax, num=nlevs)
    colors = cmap(np.linspace(start=0.0, stop=1.0, num=ncolors))
    cmap, norm = from_levels_and_colors(levels, colors, extend=extend)
    return((cmap, norm))
Ejemplo n.º 22
0
def make_colors(levels, coloring, cmap):
    """ Generate colormaps and norms based on user-defined
    level sets.

    Parameters:
    -----------
    levels : iterable of floats
        List of level demarcations.
    coloring : str
        Either 'continuous' or 'discrete'; 'discrete' colorings
        have a set number of color elements, whereas 'continuous'
        ones span value in between the elements of `levels`.
    cmap : str
        The color palette / map name to use

    Returns:
    --------
    cmap
        A colormap that matplotlib can use to set plot colors
    norm
        The normalization controlling how the data is colored
        based on the user-defined levels.

    """

    ncolors = len(levels) + 1
    assert coloring in ['continuous', 'discrete']

    ## reverse the colorbar if its cubehelix and a negative scale
    if ("cubehelix" in cmap) and (levels[0] < 0):
        if "_r" in cmap: cmap = cmap.replace("_r", "")
        else: cmap = cmap+"_r"

    if coloring == 'continuous':
        norm = MidpointNorm(vmin=levels[0], vmax=levels[-1],
                            midpoint=levels[len(levels)/2])

    elif coloring == 'discrete':
        cmap = get_cmap(cmap)
        colors = [cmap(1.*i/ncolors) for i in range(ncolors)]

        cmap, norm = from_levels_and_colors(levels, colors, extend='both')
    else:
        raise ValueError("Received unknown coloring option (%r)" % coloring)

    return cmap, norm
def animate(grid, arm, route):
    fig, axs = plt.subplots(1, 2)
    fig.canvas.mpl_connect('key_press_event', press)
    colors = ['white', 'black', 'red', 'pink', 'yellow', 'green', 'orange']
    levels = [0, 1, 2, 3, 4, 5, 6, 7]
    cmap, norm = from_levels_and_colors(levels, colors)
    for i, node in enumerate(route):
        plt.subplot(1, 2, 1)
        grid[node] = 6
        plt.cla()
        plt.imshow(grid, cmap=cmap, norm=norm, interpolation=None)
        theta1 = 2 * pi * node[0] / M - pi
        theta2 = 2 * pi * node[1] / M - pi
        arm.update_joints([theta1, theta2])
        plt.subplot(1, 2, 2)
        arm.plot_arm(plt, obstacles=obstacles)
        plt.xlim(-2.0, 2.0)
        plt.ylim(-3.0, 3.0)
        plt.show()
        # Uncomment here to save the sequence of frames
        # plt.savefig('frame{:04d}.png'.format(i))
        plt.pause(0.1)
Ejemplo n.º 24
0
def astar_torus(grid, start_node, goal_node):
    """
    Finds a path between an initial and goal joint configuration using
    the A* Algorithm on a tororiadal grid.

    Args:
        grid: An occupancy grid (ndarray)
        start_node: Initial joint configuation (tuple)
        goal_node: Goal joint configuration (tuple)

    Returns:
        Obstacle-free route in joint space from start_node to goal_node
    """
    colors = ['white', 'black', 'red', 'pink', 'yellow', 'green', 'orange']
    levels = [0, 1, 2, 3, 4, 5, 6, 7]
    cmap, norm = from_levels_and_colors(levels, colors)

    grid[start_node] = 4
    grid[goal_node] = 5

    parent_map = [[() for _ in range(M)] for _ in range(M)]

    heuristic_map = calc_heuristic_map(M, goal_node)

    explored_heuristic_map = np.full((M, M), np.inf)
    distance_map = np.full((M, M), np.inf)
    explored_heuristic_map[start_node] = heuristic_map[start_node]
    distance_map[start_node] = 0
    while True:
        grid[start_node] = 4
        grid[goal_node] = 5

        current_node = np.unravel_index(
            np.argmin(explored_heuristic_map, axis=None), explored_heuristic_map.shape)
        min_distance = np.min(explored_heuristic_map)
        if (current_node == goal_node) or np.isinf(min_distance):
            break

        grid[current_node] = 2
        explored_heuristic_map[current_node] = np.inf

        i, j = current_node[0], current_node[1]

        neighbors = find_neighbors(i, j)

        for neighbor in neighbors:
            if grid[neighbor] == 0 or grid[neighbor] == 5:
                distance_map[neighbor] = distance_map[current_node] + 1
                explored_heuristic_map[neighbor] = heuristic_map[neighbor]
                parent_map[neighbor[0]][neighbor[1]] = current_node
                grid[neighbor] = 3

    if np.isinf(explored_heuristic_map[goal_node]):
        route = []
        print("No route found.")
    else:
        route = [goal_node]
        while parent_map[route[0][0]][route[0][1]] != ():
            route.insert(0, parent_map[route[0][0]][route[0][1]])

        print("The route found covers %d grid cells." % len(route))
        for i in range(1, len(route)):
            grid[route[i]] = 6
            plt.cla()
            plt.imshow(grid, cmap=cmap, norm=norm, interpolation=None)
            plt.show()
            plt.pause(1e-2)

    return route
m =\
Basemap(llcrnrlon=lon_low_plot,llcrnrlat=lat_low_plot,urcrnrlon=lon_high_plot,urcrnrlat=lat_high_plot,projection='mill', rsphere=6371229)

x, y = m(lons, lats)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([0.05, 0.05, 0.9, 0.85])

clevs = np.linspace(min_contour, max_contour, 256)
midpoint = 0
midp = np.mean(np.c_[clevs[:-1], clevs[1:]], axis=1)

vals = np.interp(midp, [min_contour, midpoint, max_contour], [0, 0.5, 1])
cols = plt.cm.RdBu_r(vals)

clevs_extend = np.linspace(min_contour, max_contour, 254)
cmap, norm = from_levels_and_colors(clevs_extend, cols, extend='both')

# draw coastlines, state and country boundaries, edge of map.
m.drawcoastlines(linewidth=0.5, color='#262626')
#m.drawstates()
m.drawcountries(linewidth=0.5, color='#262626')
# draw parallels.
parallels = np.arange(0., 90, divisor)
m.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=10, color='#262626')
# draw meridians
meridians = np.arange(0., 360., divisor)
m.drawmeridians(meridians, labels=[0, 0, 0, 1], fontsize=10, color='#262626')

cs_col = m.contourf(x, y, total_mean, clevs, cmap=cmap, extend='both')
cbar = m.colorbar(cs_col, location='bottom', pad="5%")
#cbar.ax.tick_params(labelsize=12,  colors='#262626')
Ejemplo n.º 26
0
yi = np.linspace(data_y.min(), data_y.max(), numrows)
xi, yi = np.meshgrid(xi, yi)

#-- Interpolating at the points in xi, yi
x, y, z = data_x, data_y, data_z
zi = griddata(x, y, z, xi, yi)
fig = plt.figure()
bands_time = [0, 5, 10, 15, 20, 25, 30, 40, 50, 60, 75 , 90, 105, 120 , 150, 1000] 
bands_cost = [0, 0.5, 1, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.5, 9.0, 10.5, 12.0, 15.0, 100.0] 
bands_gen_cost = [0, 1.5, 3, 4.5, 6.0, 7.5, 9.0, 12.0, 15.0, 18.0, 22.5, 27.0, 31.5, 36.0, 45.0, 100.0] # 
my_rgbs =['#800026', '#800026', '#bd0026', '#e31a1c', '#fc4e2a', '#fd8d3c', '#feb24c', '#fed976',    
          '#ffeda0', '#ffffcc', '#d0d1e6', '#a6bddb', '#74a9cf', '#3690c0', '#0570b0', '#034e7b'] 
##################################
# generate PT time contour map ###
##################################
cmap_time, norm_time = from_levels_and_colors(bands_time, my_rgbs, extend='min')
m = plt.contourf(xi, yi, zi, latlon=True,  levels= bands_time, cmap = cmap_time, norm=norm_time)
# set the path to generate PT time map
mapfile = file_path + '/map/PT_time.html'
# convert to leaflet map
save(fig = fig, path=mapfile, template = 'base_time.html', tiles='mapbox bright')
# fig.colorbar(m)
# plt.show()
##################################
# generate PT cost contour map ###
##################################
data_z = np.loadtxt(file_path+'/data/PT cost.txt')
x, y, z = data_x, data_y, data_z
zi = griddata(x, y, z, xi, yi)
fig = plt.figure()
cmap_cost, norm_cost = from_levels_and_colors(bands_cost, my_rgbs, extend='min')
Ejemplo n.º 27
0
    def track_height_image(
        self,
        field,
        track_key=None,
        plot_height_km=False,
        plot_track_km=False,
        mask_procedure=None,
        mask_tuple=None,
        plot_log10_var=False,
        cminmax=(0.0, 60.0),
        clevs=25,
        vmin=None,
        vmax=None,
        cmap="gist_ncar",
        discrete_cmap_levels=None,
        track_min=None,
        track_max=None,
        height_min=None,
        height_max=None,
        start_time=None,
        end_time=None,
        zero_track_start=False,
        track_MajTicks=None,
        track_MinTicks=None,
        height_MajTicks=None,
        height_MinTicks=None,
        fill_surface=False,
        fill_min=None,
        fill_color=None,
        color_bar=True,
        cb_orient="vertical",
        cb_pad=0.05,
        cb_tick_int=2,
        cb_label=None,
        cb_fontsize=None,
        cb_ticklabel_size=None,
        title=None,
        xlab=" ",
        xlabFontSize=16,
        xpad=7,
        ylab=" ",
        ylabFontSize=16,
        ypad=7,
        ax=None,
        fig=None,
    ):
        """
        Wrapper function to produce a contoured plot along a track
        of variable indicated.

        Parameters
        ----------
        field : array
            Variable to plot as time series.
        track_key : str
            Key name of track distance variable.
        mask_procedure : str
            String indicating how to apply mask via numpy, possibilities are:
            'less', 'less_equal', 'greater', 'greater_equal',
            'equal', 'inside', 'outside'.
        mask_tuple : (str, float[, float])
            Tuple containing the field name and value(s) below which to mask
            field prior to plotting.
        plot_log10_var : boolean
                True plots the log base 10 of Data field.
        cminmax : tuple
            (min, max) values for controur levels.
        clevs : int
            Number of contour levels.
        vmin : float
            Minimum contour value to display.
        vmax : float
            Maximum contour value to display.
        cmap : str
            Matplotlib color map to use.
        discrete_cmap_levels : array
            A list of levels to be used for display. If chosen discrete
            color will be used in the colorbar instead of a linear luminance
            mapping.
        height_min : float
            Minimum value for height axis.
        height_max : float
            Maximum value for height axis.
        start_time : str
            UTC time to use as start time for subsetting in datetime format.
            (e.g. 2014-08-20 12:30:00)
        end_time : str
            UTC time to use as an end time for subsetting in datetime format.
            (e.g. 2014-08-20 16:30:00)
        zero_track_start : bool
            If True the first element in the track will be forced to zero.
            Of interest if using start_time keyword.
            Defaults to False.
        track_MajTicks : float
            Values for major tickmark spacing on track axis.
        track_MinTicks : float
            Values for minor tickmark spacing on track axis.
        height_MajTicks : float
            Values for major tickmark spacing on height axis.
        height_MinTicks : float
            Values for minor tickmark spacing on height axis.
        track_min : float
            Minimum value for track distance axis.
        track_max : float
            Maximum value for track distance axis.
        title : str
            Plot title.
        xlab : str
            X-axis label.
        ylab : str
            Y-axis label.
        xpad : int
            Padding for X-axis label.
        ypad : int
            Padding for Y-axis label.
        color_bar : boolean
            True adds colorbar, False does not.
        cb_pad : str
            Pad to move colorbar, in the form "5%",
            pos is to right for righthand location.
        cb_orient : str
            Colorbar orientation, either 'vertical' or 'horizontal'.
        cb_tick_int : int
            Interval to use for colorbar tick labels,
            higher number "thins" labels.
        cb_label : str
            Label for colorbar (e.g. units 'dBZ').
        cb_fontsize : int
            Font size of the colorbar label.
        cb_ticklabel_size : int
            Font size of colorbar tick labels.
        ax : Matplotlib axes instance
            Optional axes instance to plot the graph.
        fig : Matplotlib figure instance
            Figure which to add the plot.
            None will use the current figure.
        """
        # parse parameters
        ax, fig = common._parse_ax_fig(ax, fig)

        # Return masked or unmasked variable
        # Subsetted if desired
        Var, tsub, Data = self._get_variable_dict_data_time_subset(field, start_time, end_time)
        if mask_procedure is not None:
            Data = common.get_masked_data(Data, mask_procedure, mask_tuple)

        if track_key is None:
            try:
                track = self.radar["track_distance_air"]
            except:
                ValueError("Did not find suitable track distance variable")
        else:
            track = self.radar[track_key]
        trackd = self._get_variable_subset(track["data"][:], start_time, end_time)

        if plot_track_km:
            if track["units"] != "km":
                trackd = trackd / 1000.0

        if zero_track_start:
            trackd = trackd - trackd[0]

        if plot_log10_var:
            Data = np.log10(Data)
            if cb_label is not None:
                cb_label = r"log$_{10}$[" + cb_label + "]"

        # Print out min and max values to screen
        print("Minimum value of %s = %g" % (field, np.ma.min(Data)))
        print("Maximum value of %s = %g" % (field, np.ma.max(Data)))

        # Get vmin/vmax if not given
        if vmin is None:
            vmin = np.ma.min(Data)
        if vmax is None:
            vmax = np.ma.max(Data)

        # Create contour level array
        clevels = np.linspace(cminmax[0], cminmax[1], clevs)

        if len(self.height["data"].shape) == 2:
            track2D, junk = np.meshgrid(trackd, self.height["data"][0, :])
            Ht2D = Height.T
            del junk
        else:
            track2D, Ht2D = np.meshgrid(trackd, self.height["data"][:])

        if plot_height_km:
            Ht2D = Ht2D / 1000.0

        # Get the colormap and calculate data spaced by number of levels
        norm = None
        if discrete_cmap_levels is not None:
            cm = plt.get_cmap(cmap)
            try:
                levpos = np.rint(np.squeeze([np.linspace(0, 255, len(discrete_cmap_levels))])).astype(int)
                # Convert levels to colormap values
                cmap, norm = from_levels_and_colors(discrete_cmap_levels, cm(levpos), extend="max")
            except:
                print("Keyword error: 'discrete_cmap_levels' must " "be a list of float or integer")

        # Plot the time series
        ts = common.image_2d(
            track2D,
            Ht2D,
            Data.T,
            vmin=vmin,
            vmax=vmax,
            clevs=clevs,
            cmap=cmap,
            norm=norm,
            x_min=track_min,
            x_max=track_max,
            y_min=height_min,
            y_max=height_max,
            x_minor_ticks=track_MinTicks,
            x_major_ticks=track_MajTicks,
            y_minor_ticks=height_MinTicks,
            y_major_ticks=height_MajTicks,
            title=title,
            xlab=xlab,
            xlabFontSize=xlabFontSize,
            xpad=xpad,
            ylab=ylab,
            ylabFontSize=ylabFontSize,
            ypad=ypad,
            color_bar=color_bar,
            cb_orient=cb_orient,
            cb_pad=cb_pad,
            cb_tick_int=cb_tick_int,
            cb_label=cb_label,
            cb_fontsize=cb_fontsize,
            cb_ticklabel_size=cb_ticklabel_size,
            ax=ax,
            fig=fig,
        )
        if fill_surface:
            if self.surface is not None:
                sfc = self._get_variable_subset(self.surface["data"][:], start_time, end_time)
                ft = common.plot_fill_surface(trackd, sfc, ymin=fill_min, color=fill_color, ax=ax)
            else:
                print("No surface height information, cannot fill...")
        return
Ejemplo n.º 28
0
    def plot_cross_section(
        self,
        field,
        start_pt,
        end_pt,
        xs_length=500,
        mask_procedure=None,
        mask_tuple=None,
        plot_km=False,
        title=" ",
        title_size=20,
        cminmax=(0.0, 60.0),
        clevs=25,
        vmin=15.0,
        vmax=60.0,
        cmap="gist_ncar",
        discrete_cmap_levels=None,
        color_bar=True,
        clabel="dBZ",
        cb_pad=None,
        cb_orient=None,
        cb_fontsize=None,
        cb_ticklabel_size=None,
        cb_tick_int=None,
        ax=None,
        fig=None,
    ):
        """
        Plot a cross-section between two points.

        Parameters
        ----------
        field : str
            3-D variable (e.g. Reflectivity [dBZ]) to use in plot.
        start_pt, end_pt : tuple
            (lat, lon) Tuple of start, end points for cross-section.
        xs_length : int
            Number of to use for the cross section.
        mask_procedure : str
            String indicating how to apply mask via numpy, possibilities are:
            'less', 'less_equal', 'greater', 'greater_equal',
            'equal', 'inside', 'outside'.
        mask_tuple : (str, float[, float])
            Tuple containing the field name and value(s) below which to mask
            field prior to plotting, for example to mask all data where.
        plot_km : boolean
            True to convert meters to kilometers for cappi_height. False
            retains meters information.
        title : str
            Plot title.
        title_size : int
            Font size of title to display.
        cminmax : tuple
            (min,max) values for controur levels.
        clevs : int
            Number of contour levels.
        vmin : float
            Minimum contour value to display.
        vmax : float
            Maximum contour value to display.
        cmap : str
            Matplotlib color map to use.
        discrete_cmap_levels : array
            A list of levels to be used for display. If chosen discrete
            color will be used in the colorbar instead of a linear luminance
            mapping.
        color_bar : bool
            True to add colorbar, False does not.
        clabel : str
            Label for colorbar (e.g. units 'dBZ').
        cb_fontsize : int
            Font size of the colorbar label.
        cb_ticklabel_size : int
            Font size of colorbar tick labels.
        cb_pad : str
            Pad to move colorbar, in the form "5%", pos is to right for
            righthand location.
        cb_orient : str
            Colorbar orientation, either 'vertical' or 'horizontal'.
        cb_tick_int : int
            Interval to use for colorbar tick labels,
            higher number "thins" labels.
        ax : Matplotlib axis instance
            Axis to plot. None will use the current axis.
        fig : Matplotlib figure instance
            Figure on which to add the plot. None will use the current figure.
        """
        # parse parameters
        ax, fig = common._parse_ax_fig(ax, fig)

        # Return masked or unmasked variable
        Var, Data = common._get_variable_dict_data(self.fields, field)
        if mask_procedure is not None:
            Data = common.get_masked_data(Data, mask_procedure, mask_tuple)

        # Create contour level array
        clevels = np.linspace(cminmax[0], cminmax[1], clevs)

        # Create lon and lat arrays for display
        xslon = np.linspace(start_pt[0], end_pt[0], xs_length)
        xslat = np.linspace(start_pt[1], end_pt[1], xs_length)

        # Create an array to hold the interpolated cross-section
        xs_data = np.empty([xs_length, len(self.height["data"][:])])

        # Create arrays for cross-section lon-lat points
        startloclon = self._get_lon_index(start_pt[0])
        startloclat = self._get_lat_index(start_pt[1])
        endloclon = self._get_lon_index(end_pt[0])
        endloclat = self._get_lat_index(end_pt[1])

        xsY = np.linspace(startloclat, endloclat, xs_length)
        xsX = np.linspace(startloclon, endloclon, xs_length)

        # Loop through each level to create cross-section and stack them
        for nlev in range(len(self.height["data"][:])):
            # Extract the values along the line, using cubic interpolation
            xs_data[:, nlev] = scim.map_coordinates(Data[nlev, :, :], np.vstack((xsY, xsX)), prefilter=False)
            # , mode='nearest')

        # Calculate the distance array along the cross-section
        Xdist = np.absolute((np.pi * common.EARTH_RADIUS / 180.0) * (xslon - xslon[0]))
        Ydist = np.absolute((np.pi * common.EARTH_RADIUS / 180.0) * (xslat - xslat[0]))
        xsDist = np.sqrt(Xdist ** 2 + Ydist ** 2)

        # Define the angle of the cross-secton
        Dlon = start_pt[1] - end_pt[1]
        Dlat = start_pt[0] - end_pt[0]
        Ang = np.arctan2(Dlat, Dlon)
        if Ang < 0:
            AngNref = 2 * np.pi + Ang
        else:
            AngNref = Ang

        # Convert Height, distance arrays to 2D
        if plot_km:
            Ht2D, Dist2D = np.meshgrid(self.height["data"][:] / 1000.0, xsDist)
            ylabel = "Altitude (km)"
        else:
            Ht2D, Dist2D = np.meshgrid(self.height["data"][:], xsDist)
            ylabel = "Altitude (m)"

        # Get the colormap and calculate data spaced by number of levels
        norm = None
        if discrete_cmap_levels is not None:
            cm = plt.get_cmap(cmap)
            try:
                levpos = np.rint(np.squeeze([np.linspace(0, 255, len(discrete_cmap_levels))])).astype(int)
                # Convert levels to colormap values
                cmap, norm = from_levels_and_colors(discrete_cmap_levels, cm(levpos), extend="max")
            except:
                print("Keyword error: 'discrete_cmap_levels' must " "be a list of float or integer")

        p = ax.pcolormesh(
            Dist2D, Ht2D, np.ma.masked_less_equal(xs_data, -800.0), vmin=vmin, vmax=vmax, norm=norm, cmap=cmap
        )

        ax.set_xlabel("Distance along track (km)")
        ax.set_ylabel(ylabel)

        # Add title
        ax.set_title(title, fontsize=title_size)

        # Add Colorbar
        if color_bar:
            cbStr = Var["long_name"] + " " + self.height["units"]
            cb = common.add_colorbar(
                ax,
                p,
                orientation=cb_orient,
                pad=cb_pad,
                label=cbStr,
                fontsize=cb_fontsize,
                ticklabel_size=cb_ticklabel_size,
                clevs=clevs,
                tick_interval=cb_tick_int,
            )

        # Add title
        ax.set_title(title, fontsize=title_size)
Ejemplo n.º 29
0
def plot_unitigs(genome, reads, read_len=30, filename="unitigs.txt"):
    mmers = {}
    mmer = ""
    line_no = 0
    char_no = 0
    unitigs = []

    with open(filename, "r") as fin:
        lines = fin.read().splitlines()

        while (line_no < len(lines)):
            # if line is clear mmer and skip line
            if not lines[line_no]:
                mmer = ""
                line_no += 1
                continue
            # if mmer is blank read new mmer
            if not mmer:
                mmer = lines[line_no]
                if mmer not in mmers:
                    mmers[mmer] = 0

                line_no += 1

            # if read mmer read kmer and read ids
            else:
                kmer = lines[line_no]
                mmers[mmer] += 1
                line_no += 1
                char_no = 0

                unitig = {}
                while (char_no < len(kmer)):
                    read_ids = list(
                        map(int, lines[line_no].rstrip().split(" ")))
                    for read_id in read_ids:
                        if read_id not in unitig:
                            unitig[read_id] = ""

                    for key in unitig.keys():
                        if key in read_ids:
                            unitig[key] = unitig[key] + kmer[char_no]
                        else:
                            unitig[key] = unitig[key] + " "

                    line_no += 1
                    char_no += 1

                # split across reads ids to find portions of genome that contribute to
                unitigs.append(unitig)

    # generate graph for mmers
    mpl.rcParams['xtick.labelsize'] = 8
    plt.bar(range(len(mmers)), list(mmers.values()), align='center')
    plt.xticks(
        range(len(mmers)),
        list(mmers.keys()),
        rotation='vertical',
    )  # reduce font size
    plt.savefig("mmers.png")

    # generate graphs for unitigs
    matrix = np.zeros((len(unitigs), len(genome)), dtype=int)
    for i, unitig in enumerate(unitigs):
        for key, val in unitig.items():
            for part in val.split(" "):
                start = reads[key]
                index = start + genome[start:start + read_len].find(part)
                if not index:
                    index = start + rev_comp(
                        genome[start:start + read_len]).find(part)
                for j in range(index, index + len(part)):
                    matrix[i][j] = 1
    # define the colors
    cmap, norm = from_levels_and_colors([0, 0.5, 1], ['r', 'k'])

    # create a normalize object the describes the limits of
    # each color
    bounds = [0., 0.5, 1.]
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    plt.figure(figsize=(20, 10))
    plt.imshow(matrix, interpolation='nearest', cmap=cmap, norm=norm)
    plt.savefig("kmers.png")
    plt.close()
def astar_torus(grid, start_node, goal_node):
    """
    Finds a path between an initial and goal joint configuration using
    the A* Algorithm on a tororiadal grid.

    Args:
        grid: An occupancy grid (ndarray)
        start_node: Initial joint configuation (tuple)
        goal_node: Goal joint configuration (tuple)

    Returns:
        Obstacle-free route in joint space from start_node to goal_node
    """
    colors = ['white', 'black', 'red', 'pink', 'yellow', 'green', 'orange']
    levels = [0, 1, 2, 3, 4, 5, 6, 7]
    cmap, norm = from_levels_and_colors(levels, colors)

    grid[start_node] = 4
    grid[goal_node] = 5

    parent_map = [[() for _ in range(M)] for _ in range(M)]

    X, Y = np.meshgrid([i for i in range(M)], [i for i in range(M)])
    heuristic_map = np.abs(X - goal_node[1]) + np.abs(Y - goal_node[0])
    for i in range(heuristic_map.shape[0]):
        for j in range(heuristic_map.shape[1]):
            heuristic_map[i, j] = min(heuristic_map[i, j],
                                      i + 1 + heuristic_map[M - 1, j],
                                      M - i + heuristic_map[0, j],
                                      j + 1 + heuristic_map[i, M - 1],
                                      M - j + heuristic_map[i, 0]
                                      )

    explored_heuristic_map = np.full((M, M), np.inf)
    distance_map = np.full((M, M), np.inf)
    explored_heuristic_map[start_node] = heuristic_map[start_node]
    distance_map[start_node] = 0
    while True:
        grid[start_node] = 4
        grid[goal_node] = 5

        current_node = np.unravel_index(
            np.argmin(explored_heuristic_map, axis=None), explored_heuristic_map.shape)
        min_distance = np.min(explored_heuristic_map)
        if (current_node == goal_node) or np.isinf(min_distance):
            break

        grid[current_node] = 2
        explored_heuristic_map[current_node] = np.inf

        i, j = current_node[0], current_node[1]

        neighbors = []
        if i - 1 >= 0:
            neighbors.append((i - 1, j))
        else:
            neighbors.append((M - 1, j))

        if i + 1 < M:
            neighbors.append((i + 1, j))
        else:
            neighbors.append((0, j))

        if j - 1 >= 0:
            neighbors.append((i, j - 1))
        else:
            neighbors.append((i, M - 1))

        if j + 1 < M:
            neighbors.append((i, j + 1))
        else:
            neighbors.append((i, 0))

        for neighbor in neighbors:
            if grid[neighbor] == 0 or grid[neighbor] == 5:
                distance_map[neighbor] = distance_map[current_node] + 1
                explored_heuristic_map[neighbor] = heuristic_map[neighbor]
                parent_map[neighbor[0]][neighbor[1]] = current_node
                grid[neighbor] = 3
        '''
        plt.cla()
        plt.imshow(grid, cmap=cmap, norm=norm, interpolation=None)
        plt.show()
        plt.pause(1e-5)
        '''

    if np.isinf(explored_heuristic_map[goal_node]):
        route = []
        print("No route found.")
    else:
        route = [goal_node]
        while parent_map[route[0][0]][route[0][1]] is not ():
            route.insert(0, parent_map[route[0][0]][route[0][1]])

        print("The route found covers %d grid cells." % len(route))
        for i in range(1, len(route)):
            grid[route[i]] = 6
            plt.cla()
            plt.imshow(grid, cmap=cmap, norm=norm, interpolation=None)
            plt.show()
            plt.pause(1e-2)

    return route
Ejemplo n.º 31
0
fourth, = plt.plot(xs_2, ys_2)  # plot all points
plt.setp(fourth, linestyle='', marker='.', color=color4)

fifth, = plt.plot(xs_2, ys_2)  # plot all points
plt.setp(fifth, linestyle='', marker='.', color=color5)

sixth, = plt.plot(xs_2, ys_2)  # plot all points
plt.setp(sixth, linestyle='', marker='.', color=color6)

# VERSION 2

fig, ax = plt.subplots(figsize=(5, 5))

nearness1 = np.where(np.array(zs_1) < 26, 0, 1)
cmap, norm = clr.from_levels_and_colors(levels=[0, 1],
                                        colors=['#ccccff', '#e5e5ff'],
                                        extend='max')
ax.scatter(xs_1,
           ys_1,
           c=nearness1,
           s=150,
           marker='.',
           edgecolor='none',
           cmap=cmap,
           norm=norm)

nearness2 = np.where(np.array(zs_2) < 29, 0, 1)
cmap, norm = clr.from_levels_and_colors(levels=[0, 1],
                                        colors=['#9999ff', '#b2b2ff'],
                                        extend='max')
ax.scatter(xs_2,
Ejemplo n.º 32
0
x[z[:, 1]] = xs[1][z[:, 1]]
x[z[:, 2]] = xs[2][z[:, 2]]

z_ind = np.zeros(n, dtype=int)
z_ind[z[:, 1]] = 1
z_ind[z[:, 2]] = 2

np.savetxt('./intermediate_data/sim_data.csv',
           np.column_stack((x, z_ind)),
           header='x1, x2, z',
           delimiter=',',
           comments='')

# Plot data
cmap, norm = colors.from_levels_and_colors(levels=[0, 1, 2],
                                           colors=['magenta', 'cyan', 'green'],
                                           extend='max')

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x[:, 0], x[:, 1], c=z_ind, cmap=cmap, norm=norm)
fig.savefig('./sim_data.pdf')

# Estimate
init_mu = np.array([[0., 0.], [1., 1.], [2., 2.]])
init_sigma = [np.identity(2) for i in range(3)]
init_mix = np.array([1., 1., 1.]) / 3

res_em, (logliks_em, times_em) = em_alg(x,
                                        init_mu,
                                        init_sigma,
def disc_cube_plot(
    cube,
    bin_edges,
    fig=None,
    ax=None,
    cax=None,
    cmap="inferno_r",
    aux0=None,
    aux1=None,
    extend="neither",
    aux0_label="",
    aux1_label="",
    aux0_c=plotting_configuration.aux0_c,
    aux1_c=plotting_configuration.aux1_c,
    cbar=True,
    cbar_label="",
    cbar_orientation="vertical",
    cbar_fraction=0.02,
    cbar_pad=0.07,
    cbar_aspect=24,
    cbar_shrink=0.6,
    cbar_extendfrac=0.07,
    cbar_anchor=(0.5, 1.0),
    cbar_panchor=(0.5, 0.0),
    cbar_format=get_sci_format(ndigits=1),
    loc=(0.7, 0.2),
    height=0.04,
    aspect=2,
    spacing=0.04 * 0.2,
    cmap_midpoint=None,
    cmap_symmetric=False,
):
    """Plotting of cube using given bin edges."""
    if not isinstance(cube, iris.cube.Cube):
        raise ValueError("cube is not an iris Cube.")
    if len(cube.shape) != 2:
        raise ValueError("cube is not 2-dimensional.")

    if cmap_symmetric and cmap_midpoint is None:
        raise ValueError("If cmap_symmetric is True, cmap_midpoint has to be given.")
    if cmap_midpoint is not None and np.sum(np.isclose(bin_edges, cmap_midpoint)) != 1:
        raise ValueError("cmap_midpoint has to match one of the given bin edges.")

    gridlons = cube.coord("longitude").contiguous_bounds()
    gridlats = cube.coord("latitude").contiguous_bounds()

    data = cube.data

    if not isinstance(data, np.ma.MaskedArray):
        raise ValueError("Data should be a MaskedArray.")

    projection = ccrs.Robinson()

    if fig is None and ax is None:
        fig = plt.figure()
    elif fig is None:
        fig = ax.get_figure()
    if ax is None:
        ax = plt.axes(projection=projection)

    cmap_slice = slice(None)
    try:
        orig_cmap = plt.get_cmap(cmap)
    except ValueError:
        if isinstance(cmap, str) and "_r" in cmap:
            # Try to reverse the colormap manually, in case a reversed colormap was
            # requested using the '_r' suffix, but this is not available.
            cmap = cmap.rstrip("_r")
            orig_cmap = plt.get_cmap(cmap)

            # Flip limits to achieve reversal effect.
            cmap_slice = slice(None, None, -1)

    cmap_sample_lims = [0, 1]

    if extend == "neither":
        add_colors = -1
        assert np.min(data) >= bin_edges[0]
        assert np.max(data) <= bin_edges[-1]
    elif extend in ("min", "max"):
        add_colors = 0
        if extend == "min":
            assert np.max(data) <= bin_edges[-1]
        else:
            assert np.min(data) >= bin_edges[0]
    elif extend == "both":
        add_colors = 1

    n_colors = len(bin_edges) + add_colors

    if cmap_midpoint is None:
        colors = orig_cmap(np.linspace(*cmap_sample_lims[cmap_slice], n_colors))
    else:
        if cmap_symmetric:
            # Adjust the colormap sample limits such that the deviation from
            # 0.5 is proportional to the magnitude of the maximum deviation
            # from the midpoint.
            diffs = np.array(
                (bin_edges[0] - cmap_midpoint, bin_edges[-1] - cmap_midpoint)
            )
            max_diff = max(np.abs(diffs))
            scaled = diffs / max_diff
            cmap_sample_lims = 0.5 + scaled * 0.5
        # Find closest bin edge.
        closest_bound_index = np.argmin(
            np.abs(np.asarray(bin_edges[cmap_slice]) - cmap_midpoint)
        )

        lower_range = 0.5 - cmap_sample_lims[0]
        n_lower = closest_bound_index + (1 if extend in ("min", "both") else 0)

        upper_range = cmap_sample_lims[1] - 0.5
        n_upper = (
            len(bin_edges)
            - 1
            - closest_bound_index
            + (1 if extend in ("max", "both") else 0)
        )

        colors = np.vstack(
            (
                orig_cmap(
                    cmap_sample_lims[0]
                    + np.arange(n_lower) * (2 * lower_range / (1 + 2 * n_lower))
                ),
                orig_cmap(
                    cmap_sample_lims[1]
                    - np.arange(n_upper - 1, -1, -1)
                    * (2 * upper_range / (1 + 2 * n_upper))
                ),
            )
        )[cmap_slice]

    cmap, norm = from_levels_and_colors(
        levels=list(bin_edges),
        colors=colors,
        # 'neither', 'min', 'max', 'both'
        extend=extend,
    )

    mesh = ax.pcolormesh(
        gridlons,
        gridlats,
        data,
        cmap=cmap,
        norm=norm,
        rasterized=True,
        transform=ccrs.PlateCarree(),
    )

    if cbar:
        colorbar = fig.colorbar(
            mesh,
            label=cbar_label,
            orientation=cbar_orientation,
            fraction=cbar_fraction,
            pad=cbar_pad,
            aspect=cbar_aspect,
            shrink=cbar_shrink,
            extendfrac=cbar_extendfrac,
            anchor=cbar_anchor,
            panchor=cbar_panchor,
            format=cbar_format,
            cax=cax,
            ax=ax,
        )
    else:
        colorbar = None

    if aux0 is not None or aux1 is not None:
        if aux0 is not None and aux1 is not None:
            # Ensure they do not overlap.
            assert not np.any(aux0 & aux1)

        # Draw data including these auxiliary points.
        data = data.copy()

        min_data = np.min(data)

        if aux0 is not None:
            data[aux0 & data.mask] = min_data - 2
        if aux1 is not None:
            data[aux1 & data.mask] = min_data - 1

        colors = np.vstack((np.array([aux0_c, aux1_c]), colors))

        cmap, norm = from_levels_and_colors(
            levels=[
                min_data - 2.5,
                min_data - 1.5,
                *((min_data - 0.5,) if extend in ("min", "both") else ()),
            ]
            + list(bin_edges),
            colors=colors,
            extend="max" if extend in ("max", "both") else "neither",
        )

        mesh = ax.pcolormesh(
            gridlons,
            gridlats,
            data,
            cmap=cmap,
            norm=norm,
            rasterized=True,
            transform=ccrs.PlateCarree(),
        )

        # Do not re-draw colorbar including the aux0 and aux1 colours.
        # Instead, add rectangles to indicate the meaning of the auxiliary levels.

        width = height * aspect

        label_kwargs = dict(
            x=loc[0] + width + spacing,
            transform=fig.transFigure,
            verticalalignment="center",
        )

        rect_kwargs = dict(
            width=width,
            height=height,
            fill=True,
            alpha=1,
            zorder=1000,
            transform=fig.transFigure,
            figure=fig,
        )

        if aux0 is not None and aux0_label:
            fig.patches.extend(
                [
                    plt.Rectangle(
                        loc,
                        color=aux0_c,
                        **rect_kwargs,
                    ),
                ]
            )
            ax.text(y=loc[1] + height / 2, s=aux0_label, **label_kwargs)
        if aux1 is not None and aux1_label:
            fig.patches.extend(
                [
                    plt.Rectangle(
                        (loc[0], loc[1] - height - spacing), color=aux1_c, **rect_kwargs
                    ),
                ]
            )
            ax.text(y=loc[1] - height / 2 - spacing, s=aux1_label, **label_kwargs)

    ax.gridlines(zorder=0, alpha=0.4, linestyle="--", linewidth=0.3)
    ax.coastlines(resolution="110m", linewidth=0.5)

    return fig, ax, colorbar
Ejemplo n.º 34
0
plt.grid()
plt.ylim([-2.5, 2.5])
plt.title('Aire de la CIF - Sections SI, BB & FC', weight='bold', fontsize=14)
ax2.tick_params(axis='x', rotation=0)

fig_name = 'section_CIL_anomaly_FR.png'
fig.set_size_inches(w=15, h=7)
fig.savefig(fig_name, dpi=200)
os.system('convert -trim ' + fig_name + ' ' + fig_name)

## ---- CIL area for ms_climate index ---- ##
# Build the colormap
from matplotlib.colors import from_levels_and_colors
YlGn = plt.cm.YlGn(np.linspace(0, 1, num=12))
YlGn = YlGn[1:, ]
cmap, norm = from_levels_and_colors(np.arange(0, 10), YlGn, extend='both')

std_vol_std = pd.concat(
    [std_anom_SI.vol_itp, std_anom_BB.vol_itp, std_anom_FC.vol_itp],
    axis=1) / 3
std_vol_std.columns = ['SI', 'BB', 'FC']
n = 5  # xtick every n years
#ax = std_vol_std.plot(kind='bar', stacked=True, cmap='tab10')
ax = std_vol_std.plot(kind='bar', stacked=True, cmap=cmap)
ticks = ax.xaxis.get_ticklocs()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
ax.xaxis.set_ticks(ticks[::n])
ax.xaxis.set_ticklabels(ticklabels[::n])
plt.fill_between([ticks[0], ticks[-1]], [-.5, -.5], [.5, .5],
                 facecolor='gray',
                 alpha=.2)
Ejemplo n.º 35
0
    m = wrf.get_basemap(tmp) ;     x, y = m(wrf.to_np(lons), wrf.to_np(lats))
    Z=maskoceans(lons,lats,wrf.to_np(vis)[ii,:,:],inlands=False)#, resolution='c', grid=2.5)
    #Z=wrf.to_np(vis)[ii,:,:]
    #nice_cmap=plt.get_cmap('RdYlGn_r') ;     
    clevs=[0,1,2,3,4,5,6,7,8,9,10]    
    
    #['white 0 ','lime 1','limegreen 2','greenyellow 3','yellow 4','gold 5','orange 6','indianred 7',
    #'firebrick 8', 'darkred 9','lightskyblue 10','deepskyblue 11','royalblue 12 ','blue 13']
    
    mymap = mcolors.ListedColormap(['white','ghostwhite','floralwhite','greenyellow','yellow','gold','orange','indianred','firebrick', \
                                'darkred','lightskyblue','deepskyblue','royalblue','blue'])    
    nice_cmap= plt.get_cmap(mymap)
    #colors = nice_cmap([0,1, 2, 3, 4, 5,6,7,8,9,10,11,12,13])
    colors = nice_cmap([13,11,12,9,8,7,6,4,3,2,0,1])

    cmap, norm = mcolors.from_levels_and_colors(clevs, colors, extend='both')
    norml = mcolors.BoundaryNorm(clevs, ncolors=cmap.N, clip=True)

    
    #m.contour(x, y, wrf.to_np(vis)[ii,:,:], 10, colors="black")
    cs=m.contourf(x, y,Z , levels=clevs,cmap=cmap,norm=norml,extended='both')

    m.readshapefile('/home/vkvalappil/Data/shapeFiles/uae/ARE_adm1','uae',drawbounds=True, zorder=None, linewidth=1.0, color='k', antialiased=1, ax=None, default_encoding='utf-8')
    #m.drawlsmask(land_color='0.8',ocean_color='w',lsmask=True)
    #m.drawlsmask(land_color=(0, 0, 0, 0), ocean_color='deeppink', lakes=True)
    m.drawcoastlines(linewidth=0.25,ax=ax) ; m.drawcountries(linewidth=0.25,ax=ax) ; m.drawstates(linewidth=0.25,ax=ax)     

    parallels = np.arange(np.amin(lats), np.amax(lats), 2.5) ; m.drawparallels(parallels, ax=ax, color="k", labels=[1,0,0,0]) 
    merids = np.arange(np.amin(lons), np.amax(lons), 2.5)    ; m.drawmeridians(merids, ax=ax, color="k", labels=[0,0,0,1])
    #m.drawmapboundary(fill_color='white') ; 
    #plt.colorbar(shrink=.62) location='right', pad='5%')
    taxon_gene_dict[taxon] = [gene_names,gene_values]



# 0 = no test
# 1 = tested, P > P*
# 2 = tested, P < P*

cols = {0:'lightgrey',1:'orangered',2:'deepskyblue'}
# ,-1:'black'
cvr = colors.ColorConverter()
tmp = sorted(cols.keys())
cols_rgb = [cvr.to_rgb(cols[k]) for k in tmp]
intervals = np.asarray(tmp + [tmp[-1]+1]) - 0.5
cmap, norm = colors.from_levels_and_colors(intervals,cols_rgb)

legend_elements = [Patch(color='lightgrey', label=r'$n_{mut} < 3$'),
                    Patch(color='orangered', label=r'$P\nless P^{*}$'),
                    Patch(color='deepskyblue', label=r'$P < P^{*}$')]
# Patch(color='black', label="No data")

for taxon in taxa:

    if taxon in pt.treatment_taxa_to_ignore:
        continue

    if taxon == 'J':
        continue

Ejemplo n.º 37
0
def choropleth(geodf,
               figsize=12,
               column=None,
               scheme='fisher_jenks',
               n_colors=5,
               palette='viridis',
               alpha=0.75,
               cbar_orientation='vertical'):

    bounds = geodf.total_bounds
    bbox = (bounds[1], bounds[0], bounds[3], bounds[2])
    smopy_map = SmopyMap(bbox, z=12, margin=0)

    fig_shape = smopy_map.to_numpy().shape
    aspect = fig_shape[0] / fig_shape[1]

    fig = plt.figure(figsize=(figsize, figsize / aspect))
    plt.imshow(smopy_map.img)
    ax = plt.gca()
    plt.axis('off')

    choro = []
    patch_values = []

    if scheme != 'categorical':

        for idx, row in geodf.iterrows():
            feature = row.geometry
            value = row[column]

            if feature.geom_type == 'Polygon':
                choro.append(feature_to_patch(feature, smopy_map))
                patch_values.append(value)
            elif feature.geom_type == 'MultiPolygon':
                for subfeature in feature:
                    choro.append(feature_to_patch(subfeature, smopy_map))
                    patch_values.append(value)
            else:
                continue

        binning = gpd.plotting.__pysal_choro(geodf[column],
                                             scheme='fisher_jenks',
                                             k=n_colors)
        bins = np.insert(binning.bins, 0, geodf[column].min())
        palette_values = sns.color_palette(palette, n_colors=n_colors)
        cmap, norm = from_levels_and_colors(bins,
                                            palette_values,
                                            extend='neither')
        cmap.set_over(palette_values[-1], alpha=alpha)

        collection = PatchCollection(choro, alpha=alpha, cmap=cmap, norm=norm)
        collection.set_array(np.array(patch_values))

        if cbar_orientation is not None:
            plt.colorbar(collection,
                         shrink=0.5,
                         orientation='vertical',
                         label=column,
                         fraction=0.05,
                         pad=0.01)
    else:
        category_values = sorted(geodf[column].unique())
        n_colors = len(category_values)
        palette = sns.color_palette(palette, n_colors=n_colors)
        color_dict = dict(zip(category_values, palette))

        for idx, row in geodf.iterrows():
            feature = row.geometry
            value = row[column]

            if feature.geom_type == 'Polygon':
                choro.append(feature_to_patch(feature, smopy_map))
                patch_values.append(color_dict[value])
            elif feature.geom_type == 'MultiPolygon':
                for subfeature in feature:
                    choro.append(feature_to_patch(subfeature, smopy_map))
                    patch_values.append(color_dict[value])
            else:
                continue

        collection = PatchCollection(choro, alpha=alpha, color=patch_values)

        bbox_to_anchor = None  #(0.99, 0.75)
        legend_parts = [
            Patch(color=color, label=label)
            for label, color in zip(category_values, palette)
        ]
        plt.legend(legend_parts, [p.get_label() for p in legend_parts],
                   bbox_to_anchor=bbox_to_anchor)

    ax.add_collection(collection)
    plt.tight_layout()

    return ax
Ejemplo n.º 38
0
def main():

    experiment_ids = [
        'djzny', 'djznq', 'djzns', 'djznw', 'dkjxq', 'dklyu', 'dkmbq', 'dklwu',
        'dklzq'
    ]

    #experiment_ids = ['djzny' ]
    for experiment_id in experiment_ids:

        expmin1 = experiment_id[:-1]
        pfile1201 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (
            expmin1, experiment_id, pp_file1201)
        pfile2201 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (
            expmin1, experiment_id, pp_file2201)
        pfile3217 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (
            expmin1, experiment_id, pp_file3217)
        pfile3234 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (
            expmin1, experiment_id, pp_file3234)
        #pc =  iris(pfile)
        pcube1201 = iris.load_cube(pfile1201)
        pcube2201 = iris.load_cube(pfile2201)
        pcube3217 = iris.load_cube(pfile3217)
        pcube3234 = iris.load_cube(pfile3234)

        pcubetotal = pcube1201 + pcube2201 - pcube3217 - pcube3234

        print pcubetotal
        #print pc

        # Get min and max latitude/longitude and unrotate  to get min/max corners to crop plot automatically - otherwise end with blank bits on the edges
        lats = pcubetotal.coord('grid_latitude').points
        lons = pcubetotal.coord('grid_longitude').points

        cs = pcubetotal.coord_system('CoordSystem')
        if isinstance(cs, iris.coord_systems.RotatedGeogCS):

            print 'Rotated CS %s' % cs

            lon_low = np.min(lons)
            lon_high = np.max(lons)
            lat_low = np.min(lats)
            lat_high = np.max(lats)

            lon_corners, lat_corners = np.meshgrid((lon_low, lon_high),
                                                   (lat_low, lat_high))

            lon_corner_u, lat_corner_u = unrotate.unrotate_pole(
                lon_corners, lat_corners, cs.grid_north_pole_longitude,
                cs.grid_north_pole_latitude)
            lon_low = lon_corner_u[0, 0]
            lon_high = lon_corner_u[0, 1]
            lat_low = lat_corner_u[0, 0]
            lat_high = lat_corner_u[1, 0]

        else:
            lon_low = np.min(lons)
            lon_high = np.max(lons)
            lat_low = np.min(lats)
            lat_high = np.max(lats)

        lon_low_tick = lon_low - (lon_low % divisor)
        lon_high_tick = math.ceil(lon_high / divisor) * divisor

        lat_low_tick = lat_low - (lat_low % divisor)
        lat_high_tick = math.ceil(lat_high / divisor) * divisor

        print lat_high_tick
        print lat_low_tick
        plt.figure(figsize=(8, 8))

        cmap = plt.cm.RdBu_r

        ax = plt.axes(projection=ccrs.PlateCarree(),
                      extent=(lon_low, lon_high, lat_low + degs_crop_bottom,
                              lat_high - degs_crop_top))

        clevs = np.linspace(min_contour, max_contour, 256)

        midpoint = 0
        midp = np.mean(np.c_[clevs[:-1], clevs[1:]], axis=1)

        vals = np.interp(midp, [min_contour, midpoint, max_contour],
                         [0, 0.5, 1])
        cols = plt.cm.RdBu_r(vals)

        clevs_extend = np.linspace(min_contour, max_contour, 254)
        cmap, norm = from_levels_and_colors(clevs_extend, cols, extend='both')

        cont = iplt.contourf(pcubetotal,
                             clevs,
                             cmap=cmap,
                             extend='both',
                             norm=norm)
        #cont = iplt.contourf(pcubetotal, cmap=cmap, extend='both')
        #plt.clabel(cont, fmt='%d')
        #ax.stock_img()
        ax.coastlines(resolution='110m', color='#262626')

        gl = ax.gridlines(draw_labels=True,
                          linewidth=0.5,
                          color='#262626',
                          alpha=0.5,
                          linestyle='--')
        gl.xlabels_top = False
        gl.ylabels_right = False
        #gl.xlines = False

        gl.xlocator = mticker.FixedLocator(
            range(int(lon_low_tick),
                  int(lon_high_tick) + divisor, divisor))
        gl.ylocator = mticker.FixedLocator(
            range(int(lat_low_tick),
                  int(lat_high_tick) + divisor, divisor))
        gl.xformatter = LONGITUDE_FORMATTER
        gl.yformatter = LATITUDE_FORMATTER

        gl.xlabel_style = {'size': 12, 'color': '#262626'}
        #gl.xlabel_style = {'color': '#262626', 'weight': 'bold'}
        gl.ylabel_style = {'size': 12, 'color': '#262626'}

        cbar = plt.colorbar(cont,
                            orientation='horizontal',
                            pad=0.05,
                            extend='both',
                            format='$%d$')
        cbar.set_label('$W m^{-2}$')
        #cbar.set_label(pcubetotal.units, fontsize=10)
        cbar.set_ticks(
            np.arange(min_contour, max_contour + tick_interval, tick_interval))
        ticks = (np.arange(min_contour, max_contour + tick_interval,
                           tick_interval))
        cbar.set_ticklabels(['${%d}$' % i for i in ticks])
        #main_title=pcubetotal.standard_name.title().replace('_',' ')
        #main_title=('Total

        #model_info=re.sub('(.{68} )', '\\1\n', str(model_name_convert_title.main(experiment_id)), 0, re.DOTALL)
        #model_info = re.sub(r'[(\']', ' ', model_info)
        #model_info = re.sub(r'[\',)]', ' ', model_info)
        #print model_info

        if not os.path.exists('%s%s/%s' % (save_path, experiment_id, pp_file)):
            os.makedirs('%s%s/%s' % (save_path, experiment_id, pp_file))

        plt.savefig(
            '%s%s/%s/%s_%s_notitle.png' %
            (save_path, experiment_id, pp_file, experiment_id, pp_file),
            format='png',
            bbox_inches='tight')

        #plt.title('\n'.join(wrap('%s\n%s' % (main_title, model_info), 1000,replace_whitespace=False)), fontsize=16)

        #plt.show()

        #plt.savefig('%s%s/%s/%s_%s.png' % (save_path, experiment_id, pp_file, experiment_id, pp_file), format='png', bbox_inches='tight')

        plt.close()
Ejemplo n.º 39
0
def azmp_map(my_file, my_year, my_season, my_depth, my_variable):

    pd.set_option('display.max_rows', 500)
    df = my_file
    df = df.set_index('timestamp', drop=False)

    #########remove unwanted sections##################
    #SSdrop = ('L3', 'YL', 'PS', 'BANQ', 'PL', 'SG', 'SPB', 'BP', 'LCC', 'LCM', 'VB')
    #for i in SSdrop:
    #    df.drop((df.loc[df['StationID'].str.contains(i, na=False)].index), inplace=True)

    ############################set data parameters here#######################
    #    my_year='2014'
    #    my_season='spring'
    #    my_depth='bottom'
    #    my_variable='pH'
    #
    df = df.loc[my_year]
    if my_season == 'spring':
        df = df[(df.index.month >= 3) & (df.index.month <= 6)]
    elif my_season == 'summer':
        df = df[~df.Region.str.contains('SS')]
        df = df.assign(x=df.index.strftime('%m-%d')).query(
            "'07-01' <= x <= '10-14'").drop('x', 1)
    elif my_season == 'fall':
        dfng = df[~df.Region.str.contains('SS')]
        dfng = dfng.assign(x=dfng.index.strftime('%m-%d')).query(
            "'10-15' <= x <= '12-31'").drop('x', 1)
        dfss = df[df.Region.str.contains('SS')]
        dfss = dfss[(dfss.index.month >= 9) & (dfss.index.month <= 12)]
        df = pd.concat([dfng, dfss], axis=0)
    else:
        print 'All seasons selected'

    if df[my_variable].isna().values.all():  #if df.size == 0  or
        print('!!! no data for this season !!!')
        return

    df.dropna(subset=[my_variable], axis=0, inplace=True)
    df = df.reset_index(drop=True)

    if my_depth == 'surface':
        df = df.loc[df.groupby('StationID')['depth'].idxmin(
        )]  #group by station then pull "min or max depth"
        df = df.loc[
            df.depth <
            20]  #take all depths >10m (for bottom) to eliminate lone surface samples, all depths <20m (for surface) to eliminate lone deep sample
    if my_depth == 'bottom':
        df = df.loc[df.groupby('StationID')['depth'].idxmax(
        )]  #group by station then pull "min or max depth"
        df = df.loc[
            df.depth >
            10]  #take all depths >10m (for bottom) to eliminate lone surface samples, all depths <20m (for surface) to eliminate lone deep sample

    v = vl.variable_parameters(my_variable)
    num_levels = v[0]
    vmin = v[1]
    vmax = v[2]
    midpoint = v[3]
    colors = v[4]
    ticks = v[5]
    axis_label = v[6]
    extent = v[7]

    os.environ[
        "CARTOPY_USER_BACKGROUNDS"] = "C:\ProgramData\Anaconda2\Lib\site-packages\cartopy\BG"
    dataFile = 'C:\Users\gibbo\Documents\data\GRIDONE_1D.nc'
    lonLims = [-72, -41.5]  #
    latLims = [40.5, 58.4]

    lat_data = np.array(df.latitude)
    lon_data = np.array(df.longitude)
    data = np.array(df[my_variable])
    lon_data = lon_data[~np.isnan(data)]
    lat_data = lat_data[~np.isnan(data)]
    data = data[~np.isnan(data)]

    ### Bathymetry
    print('Load and grid bathymetry')
    h5_outputfile = 'cc_bathymetry.h5'

    if os.path.isfile(h5_outputfile):
        print[h5_outputfile + ' exists! Reading directly']
        h5f = h5py.File(h5_outputfile, 'r')
        lon = h5f['lon'][:]
        lat = h5f['lat'][:]
        Z = h5f['Z'][:]
        h5f.close()
        print(' -> Done!')

    else:
        dataset = netCDF4.Dataset(dataFile)
        # Extract variables
        x = dataset.variables['x_range']
        y = dataset.variables['y_range']
        spacing = dataset.variables['spacing']
        # Compute Lat/Lon
        nx = int((x[-1] - x[0]) / spacing[0]) + 1  # num pts in x-dir
        ny = int((y[-1] - y[0]) / spacing[1]) + 1  # num pts in y-dir
        lon = np.linspace(x[0], x[-1], nx)
        lat = np.linspace(y[0], y[-1], ny)
        ## interpolate data on regular grid (temperature grid)
        ## Reshape data
        zz = dataset.variables['z']
        Z = zz[:].reshape(ny, nx)
        Z = np.flipud(Z)  # <------------ important!!!
        # Reduce data according to Region params
        idx_lon = np.where((lon >= lonLims[0]) & (lon <= lonLims[1]))
        idx_lat = np.where((lat >= latLims[0]) & (lat <= latLims[1]))
        Z = Z[idx_lat[0][0]:idx_lat[0][-1] + 1,
              idx_lon[0][0]:idx_lon[0][-1] + 1]
        lon = lon[idx_lon[0]]
        lat = lat[idx_lat[0]]
        print(' -> Done!')

    # Save data for later use
    if np.size(h5_outputfile):
        h5f = h5py.File(h5_outputfile, 'w')
        h5f.create_dataset('lon', data=lon)
        h5f.create_dataset('lat', data=lat)
        h5f.create_dataset('Z', data=Z)
        h5f.close()

    #############now make map########################################################
    fig = plt.figure(figsize=(7, 5))
    ax = fig.add_subplot(111, projection=ccrs.Mercator())
    ax.set_extent([-72, -41.5, 40.5, 58.4], crs=ccrs.PlateCarree())
    ax.add_feature(
        cpf.NaturalEarthFeature('physical',
                                'coastline',
                                '10m',
                                edgecolor='k',
                                alpha=0.7,
                                linewidth=0.6,
                                facecolor='black'))  #cpf.COLORS['land']))
    m = ax.gridlines(linewidth=0.5, color='black', draw_labels=True, alpha=0.5)
    m.xlabels_top = False
    m.ylabels_right = False
    m.xlocator = mticker.FixedLocator([-75, -70, -60, -50, -40])
    m.ylocator = mticker.FixedLocator([40, 45, 50, 55, 60, 65])
    m.xformatter = LONGITUDE_FORMATTER
    m.yformatter = LATITUDE_FORMATTER
    m.ylabel_style = {'size': 7, 'color': 'black', 'weight': 'bold'}
    m.xlabel_style = {'size': 7, 'color': 'black', 'weight': 'bold'}
    lightdeep = cmocean.tools.lighten(cmo.deep, 0.5)
    v = np.linspace(0, 5500, 20)
    c = plt.contourf(lon,
                     lat,
                     -Z,
                     v,
                     transform=ccrs.PlateCarree(),
                     cmap=lightdeep,
                     extend='max',
                     zorder=1)
    cc = plt.contour(lon,
                     lat,
                     -Z, [100, 500, 1000, 2000, 3000, 4000, 5000],
                     colors='lightgrey',
                     linewidths=.5,
                     transform=ccrs.PlateCarree(),
                     zorder=10)
    plt.clabel(cc, inline=True, fontsize=8, fmt='%i')

    #################to adjust the colorbar###################################
    levels = np.linspace(vmin, vmax, num_levels)
    midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
    vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
    colors = colors(vals)
    colors = np.concatenate([[colors[0, :]], colors, [colors[-1, :]]], 0)
    cmap, norm = from_levels_and_colors(levels, colors, extend=extent)

    #######################plot data onto map######################################
    s = ax.scatter(lon_data,
                   lat_data,
                   c=data,
                   s=40,
                   lw=0.3,
                   edgecolor='black',
                   vmin=vmin,
                   vmax=vmax,
                   cmap=cmap,
                   transform=ccrs.Geodetic(),
                   zorder=10)
    cax = plt.axes([0.83, 0.125, 0.03, 0.756])  #([0.863,0.11,0.03,0.771])
    cb = plt.colorbar(s, cax=cax, extend=extent, ticks=ticks)
    cb.ax.tick_params(labelsize=8)
    cb.set_label(axis_label, fontsize=12, fontweight='normal')

    fig.savefig(
        'C:\Users\gibbo\Documents\carbonates\AZMP_OA\AZMP_OA data\AZMP_OA plots\AZMP_OA maps\AZMP_OA maps\AZMP_OA_'
        + my_season + '_' + my_variable + '_' + my_depth + '.png',
        format='png',
        dpi=500,
        bbox_inches='tight')
    plt.close()
    print(' -> plot saved!')
def main():

 experiment_ids = ['djzny','djznq', 'djzns', 'djznw', 'dkjxq', 'dklyu', 'dkmbq', 'dklwu', 'dklzq' ] 
 
 #experiment_ids = ['djzny' ] 
 for experiment_id in experiment_ids:

  expmin1 = experiment_id[:-1]
  pfile1201 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (expmin1, experiment_id, pp_file1201)
  pfile2201 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (expmin1, experiment_id, pp_file2201)
  pfile3217 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (expmin1, experiment_id, pp_file3217)
  pfile3234 = '/nfs/a90/eepdw/Data/EMBRACE/Mean_State/pp_files/%s/%s/%s.pp' % (expmin1, experiment_id, pp_file3234)
     #pc =  iris(pfile)
  pcube1201 = iris.load_cube(pfile1201)
  pcube2201 = iris.load_cube(pfile2201)
  pcube3217 = iris.load_cube(pfile3217)
  pcube3234 = iris.load_cube(pfile3234)

  pcubetotal = pcube1201 + pcube2201 - pcube3217 - pcube3234
 
  print pcubetotal
     #print pc
 
 # Get min and max latitude/longitude and unrotate  to get min/max corners to crop plot automatically - otherwise end with blank bits on the edges 
  lats = pcubetotal.coord('grid_latitude').points
  lons = pcubetotal.coord('grid_longitude').points
  
  cs = pcubetotal.coord_system('CoordSystem')
  if isinstance(cs, iris.coord_systems.RotatedGeogCS):

      print 'Rotated CS %s' % cs
     
      lon_low= np.min(lons)
      lon_high = np.max(lons)
      lat_low = np.min(lats)
      lat_high = np.max(lats)

      lon_corners, lat_corners = np.meshgrid((lon_low, lon_high), (lat_low, lat_high))
      
      lon_corner_u,lat_corner_u = unrotate.unrotate_pole(lon_corners, lat_corners, cs.grid_north_pole_longitude, cs.grid_north_pole_latitude)
      lon_low = lon_corner_u[0,0]
      lon_high = lon_corner_u[0,1]
      lat_low = lat_corner_u[0,0]
      lat_high = lat_corner_u[1,0]

  else: 
      lon_low= np.min(lons)
      lon_high = np.max(lons)
      lat_low = np.min(lats)
      lat_high = np.max(lats)

  lon_low_tick=lon_low -(lon_low%divisor)
  lon_high_tick=math.ceil(lon_high/divisor)*divisor

  lat_low_tick=lat_low - (lat_low%divisor)
  lat_high_tick=math.ceil(lat_high/divisor)*divisor
 
  print lat_high_tick
  print lat_low_tick
  plt.figure(figsize=(8,8))
         
  cmap=plt.cm.RdBu_r

  ax = plt.axes(projection=ccrs.PlateCarree(), extent=(lon_low,lon_high,lat_low+degs_crop_bottom,lat_high-degs_crop_top))

  clevs = np.linspace(min_contour, max_contour,256)

  midpoint=0
  midp = np.mean(np.c_[clevs[:-1], clevs[1:]], axis=1)

  vals = np.interp(midp, [min_contour, midpoint, max_contour], [0, 0.5, 1])
  cols = plt.cm.RdBu_r(vals)

  clevs_extend = np.linspace(min_contour, max_contour,254)
  cmap, norm = from_levels_and_colors(clevs_extend, cols, extend='both')  
 
  cont = iplt.contourf(pcubetotal, clevs, cmap=cmap, extend='both', norm=norm)
  #cont = iplt.contourf(pcubetotal, cmap=cmap, extend='both')                  
  #plt.clabel(cont, fmt='%d')
  #ax.stock_img()
  ax.coastlines(resolution='110m', color='#262626') 
                     
  gl = ax.gridlines(draw_labels=True,linewidth=0.5, color='#262626', alpha=0.5, linestyle='--')
  gl.xlabels_top = False
  gl.ylabels_right = False
            #gl.xlines = False

  gl.xlocator = mticker.FixedLocator(range(int(lon_low_tick),int(lon_high_tick)+divisor,divisor))
  gl.ylocator = mticker.FixedLocator(range(int(lat_low_tick),int(lat_high_tick)+divisor,divisor))
  gl.xformatter = LONGITUDE_FORMATTER
  gl.yformatter = LATITUDE_FORMATTER
  
  gl.xlabel_style = {'size': 12, 'color':'#262626'}
  #gl.xlabel_style = {'color': '#262626', 'weight': 'bold'}
  gl.ylabel_style = {'size': 12, 'color':'#262626'}         

  cbar = plt.colorbar(cont, orientation='horizontal', pad=0.05, extend='both', format = '$%d$')
  cbar.set_label('$W m^{-2}$') 
  #cbar.set_label(pcubetotal.units, fontsize=10)
  cbar.set_ticks(np.arange(min_contour, max_contour+tick_interval,tick_interval))
  ticks = (np.arange(min_contour, max_contour+tick_interval,tick_interval))
  cbar.set_ticklabels(['${%d}$' % i for i in ticks])
  #main_title=pcubetotal.standard_name.title().replace('_',' ')
  #main_title=('Total 

  #model_info=re.sub('(.{68} )', '\\1\n', str(model_name_convert_title.main(experiment_id)), 0, re.DOTALL)
  #model_info = re.sub(r'[(\']', ' ', model_info)
  #model_info = re.sub(r'[\',)]', ' ', model_info)
  #print model_info
  
  if not os.path.exists('%s%s/%s' % (save_path, experiment_id, pp_file)): os.makedirs('%s%s/%s' % (save_path, experiment_id, pp_file))

  plt.savefig('%s%s/%s/%s_%s_notitle.png' % (save_path, experiment_id, pp_file, experiment_id, pp_file), format='png', bbox_inches='tight')

  #plt.title('\n'.join(wrap('%s\n%s' % (main_title, model_info), 1000,replace_whitespace=False)), fontsize=16)
 
  #plt.show()
 
  #plt.savefig('%s%s/%s/%s_%s.png' % (save_path, experiment_id, pp_file, experiment_id, pp_file), format='png', bbox_inches='tight')
  
  plt.close()
Ejemplo n.º 41
0
datafile = os.path.join(filepath, filename)
font_size = 20
font_weight = 'normal'
ifile = pd.read_csv(datafile)

x = ifile['Days']
y = ifile.columns[2:]
zmatrix = np.zeros((len(y), len(x)))

for i in range(len(x)):
    for j in range(len(y)):
        zmatrix[j][i] = ifile.at[i, y[j]]

X, Y = np.meshgrid(y, x)
bounds = [0, 35, 75, 115, 150, 250, 600]
cmap, norm = from_levels_and_colors(
    bounds, ['limegreen', 'yellow', 'orange', 'r', 'purple', 'maroon'])
fig, ax = plt.subplots(figsize=[50, 18], ncols=1, nrows=1)
# cax = ax.pcolormesh(zmatrix, shading = 'gouraud', snap = 'True',cmap = cmap, norm=norm) #BTHS_matri2.pdf

cax = ax.pcolor(zmatrix, cmap=cmap, norm=norm)  #不能修改shading方式
# cax = ax.imshow(zmatrix, interpolation="nearest",cmap = cmap, norm=norm)  #只适合len(x),len(y)相差不大的情况

cbar = fig.colorbar(cax, ax=ax, pad=0.01, ticks=bounds)
# cbar.make_axes(fraction= 0.5,aspect = 20)

font1 = {
    'family': 'Times New Roman',
    'weight': 'normal',
    'size': 25,
}  #图例的字体设置,prop参数适用于plt.legend
def plot_domain_and_interest_region(ax: Axes,
                                    topo_nc_file_path: Path,
                                    focus_region_lonlat_nc_file: Path = None):
    """
    :param focus_region_lonlat_nc_file: Path to the file containing focus region lons and lats
    :param region_mask_lats: latitudes corresponding to the region mask
    :param region_mask_lons:
    :param ax:
    :param topo_nc_file_path:
    :param region_mask:

    Note: below is the expected structure of the input netcdf file

    $ ncdump -h geophys_452x260_me.nc
    netcdf geophys_452x260_me {
    dimensions:
        x = 452 ;
        y = 260 ;
    variables:
        float ME(x, y) ;
        float lon(x, y) ;
        float lat(x, y) ;
        int proj_params ;
            proj_params:grid_type = "E" ;
            proj_params:lat1 = 0. ;
            proj_params:lon1 = 180. ;
            proj_params:lat2 = 1. ;
            proj_params:lon2 = 276. ;
    }
    """

    # read the model topography from the file
    with xarray.open_dataset(topo_nc_file_path) as topo_ds:
        topo_lons, topo_lats, topo = [
            topo_ds[k].values for k in ["lon", "lat", "ME"]
        ]

        prj_params = topo_ds["proj_params"]

        rll = RotatedLatLon(lon1=prj_params.lon1,
                            lat1=prj_params.lat1,
                            lon2=prj_params.lon2,
                            lat2=prj_params.lat2)

        rot_pole_cpy = rll.get_cartopy_projection_obj()

    ax.set_visible(False)
    ax = ax.figure.add_axes(ax.get_position().bounds, projection=rot_pole_cpy)
    # ax.coastlines()

    gl_mask = get_gl_mask(topo_nc_file_path)
    region_mask = get_mask_of_points_near_lakes(gl_mask, npoints_radius=20)
    topo_lons[topo_lons > 180] -= 360

    xll, yll = rot_pole_cpy.transform_point(topo_lons[0, 0], topo_lats[0, 0],
                                            cartopy.crs.PlateCarree())
    xur, yur = rot_pole_cpy.transform_point(topo_lons[-1, -1], topo_lats[-1,
                                                                         -1],
                                            cartopy.crs.PlateCarree())
    map_extent = [xll, xur, yll, yur]
    print("Map extent: ", map_extent)

    topo_clevs = [0, 100, 200, 300, 400, 500, 600, 800, 1000, 1200]
    # bn = BoundaryNorm(topo_clevs, len(topo_clevs) - 1)
    cmap = cm.get_cmap("terrain")
    ocean_color = cmap(0.18)
    cmap, norm = colors.from_levels_and_colors(
        topo_clevs, cmap(np.linspace(0.3, 1,
                                     len(topo_clevs) - 1)))

    xyz_coords = rot_pole_cpy.transform_points(cartopy.crs.PlateCarree(),
                                               topo_lons, topo_lats)
    xx = xyz_coords[:, :, 0]
    yy = xyz_coords[:, :, 1]

    add_rectangle(ax,
                  xx,
                  yy,
                  margin=20,
                  edge_style="solid",
                  zorder=10,
                  linewidth=0.5)
    add_rectangle(ax,
                  xx,
                  yy,
                  margin=10,
                  edge_style="dashed",
                  zorder=10,
                  linewidth=0.5)

    # plot a rectangle for the focus region
    if focus_region_lonlat_nc_file is not None:
        with xarray.open_dataset(focus_region_lonlat_nc_file) as fr:
            focus_lons, focus_lats = fr["lon"].data, fr["lat"].data

            xyz_coords = rot_pole_cpy.transform_points(
                cartopy.crs.PlateCarree(), focus_lons, focus_lats)
            xxf, yyf = xyz_coords[..., 0], xyz_coords[..., 1]

            add_rectangle(ax,
                          xxf,
                          yyf,
                          edge_style="solid",
                          margin=0,
                          edgecolor="magenta",
                          zorder=10,
                          linewidth=1)

    cs = ax.pcolormesh(topo_lons[:, :],
                       topo_lats[:, :],
                       topo[:, :],
                       transform=cartopy.crs.PlateCarree(),
                       cmap=cmap,
                       norm=norm)

    to_plot = np.ma.masked_where(region_mask < 0.5, region_mask)
    ax.scatter(topo_lons,
               topo_lats,
               to_plot * 0.01,
               c="cyan",
               transform=cartopy.crs.PlateCarree(),
               alpha=0.5)

    # Add geographic features
    line_color = "k"
    ax.add_feature(common_params.LAKES_50m,
                   facecolor=cartopy.feature.COLORS["water"],
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.add_feature(common_params.OCEAN_50m,
                   facecolor=cartopy.feature.COLORS["water"],
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.add_feature(common_params.COASTLINE_50m,
                   facecolor="none",
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.add_feature(common_params.RIVERS_50m,
                   facecolor="none",
                   edgecolor=line_color,
                   linewidth=0.5)
    ax.set_extent(map_extent, crs=rot_pole_cpy)

    # improve colorbar
    divider = make_axes_locatable(ax)
    ax_cb = divider.new_vertical(size="5%",
                                 pad=0.1,
                                 axes_class=plt.Axes,
                                 pack_start=True)
    ax.figure.add_axes(ax_cb)
    cb = plt.colorbar(cs, cax=ax_cb, orientation="horizontal")
    cb.ax.set_xticklabels(topo_clevs, rotation=45)
    return ax
Ejemplo n.º 43
0
def plot_rain_contr(cube_a,
                    cube_b,
                    plotname,
                    runtitle,
                    timescale_a,
                    timescale_b,
                    all_ppn_bounds=None,
                    region=None,
                    frac=0):
    """
    Function to plot set of histogram maps as rainfall contributions
    on two different timescales side by side.

    Inputs can be ACTUAL (assumed) or FRACTIONAL (if frac=1) contributions

    Args:
    * cube_a, cube_b:
       histogram cubes of rainfall contributions from each bin on timescale_a and timescale_b
    * plotname:
       filename for plot
    * runtitle:
       Name for dataset being plotted
    * timescale_a, timescale_b:
       Strings detailing time frequency of data from which histogram cubes were calculated
       e.g. 'Daily'
    * all_ppn_bounds:
       (optional) defines range of bin limits to lump together for plotting, otherwise is set
       to 4 default groups: [(0.005, 10.), (10., 50.), (50., 100.), (100., 3000.)] kg/m2/day
       Note that plots will work best for no more than 5 or 6 groups.
    * region:
       (optional) defines region to plot, otherwise just plots everywhere there are data.
    * frac:
       (optional) If frac=1, expects FRACTIONAL contributions have been input
                  otherwise assumes ACTUAL contributions have been input
    """

    # Check that the correct fields have been input

    test_a = cube_a.collapsed('precipitation_flux', iris.analysis.SUM)
    test_b = cube_b.collapsed('precipitation_flux', iris.analysis.SUM)

    if frac != 0:
        if round(np.max(test_a.data), 12) != 1.0 or round(
                np.max(test_b.data), 12) != 1.0:
            raise Exception(
                'One or more input cube(s) is not a fractional histogram')
    else:
        if round(np.max(test_a.data), 12) == 1.0 or round(
                np.max(test_b.data), 12) == 1.0:
            raise Exception(
                'One or more input cube(s) is a fractional histogram')

# Extract region if required

    if region:
        avg_rain_bins_a = extract_region(cube_a, region)
        if avg_rain_bins_a is None:
            raise Exception(
                'No data points in first dataset exist within region!')

        avg_rain_bins_b = extract_region(cube_b, region)
        if avg_rain_bins_b is None:
            raise Exception(
                'No data points in second dataset exist within region!')
    else:
        avg_rain_bins_a = cube_a
        avg_rain_bins_b = cube_b

# Put timescale and runtitle data into cube attributes so that they can be extracted later.

    avg_rain_bins_a.attributes['timescale'] = timescale_a
    avg_rain_bins_a.attributes['runtitle'] = runtitle
    avg_rain_bins_b.attributes['timescale'] = timescale_b
    avg_rain_bins_b.attributes['runtitle'] = runtitle

    # Set up contour levels and colours

    if frac != 0:
        contour_levels = [
            0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
        ]
    else:
        contour_levels = [0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 4, 6, 8, 10, 12]

# Make a range of colours from dark blue through near-white to red by concatenating
# part of two Cynthia Brewer color maps.

    colors = np.concatenate([
        plt.get_cmap('brewer_RdBu_11')(range(1, 10)),
        plt.get_cmap('brewer_YlOrBr_09')(range(5, 0, -1))
    ])

    cmap, norm = from_levels_and_colors(contour_levels, colors, extend='both')

    # Plot contribution amounts, lumped into bins as defined in all_ppn_bounds

    if not all_ppn_bounds:
        all_ppn_bounds = [(0.005, 10.), (10., 50.), (50., 100.), (100., 3000.)]

    cubes = [avg_rain_bins_a, avg_rain_bins_b]

    plot_shape = [len(all_ppn_bounds), len(cubes)]
    fig = plt.figure(figsize=(10, 6), dpi=300)

    plot_number = 0
    for ppn_bounds in all_ppn_bounds:
        for cube in cubes:
            plot_number += 1
            cf = _make_panel(tuple(plot_shape + [plot_number]), cube,
                             ppn_bounds, cmap, norm)

# Make an axes to put the shared colorbar in

    colorbar_axes = plt.gcf().add_axes([0.25, 0.05, 0.5, 0.02])
    colorbar = plt.colorbar(cf, colorbar_axes, orientation='horizontal')
    colorbar.ax.tick_params(labelsize=8)
    colorbar.ax.xaxis.set_label_position('top')

    if frac != 0:
        colorbar.set_label('Fractional contribution from events', fontsize=10)
    else:
        colorbar.set_label('Actual contribution from events (mm/day)',
                           fontsize=10)

    plt.suptitle('Histogram of total rainfall events (mm/day)' + '\n' +
                 runtitle,
                 fontsize=10)

    plt.savefig(plotname)

    plt.clf()
Ejemplo n.º 44
0
    def time_height_image(
        self,
        field,
        plot_km=False,
        mask_procedure=None,
        mask_tuple=None,
        plot_log10_var=False,
        cminmax=(0.0, 60.0),
        clevs=25,
        vmin=15.0,
        vmax=60.0,
        cmap="gist_ncar",
        discrete_cmap_levels=None,
        date_format="%H:%M",
        tz=None,
        xdate=True,
        date_minor_string="minute",
        height_MajTicks=None,
        height_MinTicks=None,
        height_min=None,
        height_max=None,
        fill_surface=False,
        fill_min=None,
        fill_color=None,
        start_time=None,
        end_time=None,
        color_bar=True,
        cb_orient="vertical",
        cb_pad=0.05,
        cb_tick_int=2,
        cb_label=None,
        cb_fontsize=None,
        cb_ticklabel_size=None,
        title=None,
        xlab=" ",
        xlabFontSize=16,
        xpad=7,
        ylab=" ",
        ylabFontSize=16,
        ypad=7,
        ax=None,
        fig=None,
    ):
        """
        Wrapper function to produce a time series vs. height plot
        of variable indicated.

        Parameters
        ----------
        field : float
            Variable to plot as time series.
        plot_km : boolean
            True to convert meters to kilometers for cappi_height. False
            retains meters information.
        mask_procedure : str
            String indicating how to apply mask via numpy, possibilities are:
            'less', 'less_equal', 'greater', 'greater_equal',
            'equal', 'inside', 'outside'.
        mask_tuple : (str, float[, float])
            Tuple containing the field name and value(s) below which to mask
            field prior to plotting, for example to mask all data where.
        plot_log10_var : bool
                True plots the log base 10 of Data field.
        cminmax : tuple
            (min,max) values for controur levels.
        clevs : int
            Number of contour levels.
        vmin : float
            Minimum value to display.
        vmax : float
            Maximum value to display.
        cmap : str
            Matplotlib color map to use.
        discrete_cmap_levels : array
            A list of levels to be used for display. If chosen discrete
            color will be used in the colorbar instead of a linear luminance
            mapping.
        date_format : str
            Format of the time string for x-axis labels.
        tz : str
            Time zone info to use when creating axis labels (see datetime).
        xdate : bool
            True to use X-axis as date axis, false implies Y-axis is date axis.
        date_minor_string : str
            Sting to set minor ticks of date axis,
            'second','minute','hour','day' supported.
        height_MajTicks : float
            Values for major tickmark spacing on height axis.
        height_MinTicks : float
            Values for minor tickmark spacing on height axis.
        height_min : float
            Minimum value for height axis.
        height_max : float
            Maximum value for height axis.
        fill_surface : boolean
            True to fill in surface, False to leave alone.
        fill_min : float
            Minimum surface elvation to shade. Only applied
            if fill_surface is True.
        fill_color : float
            Color to use if fill_surface is True.
        start_time : str
            UTC time to use as start time for subsetting in datetime format.
            (e.g. 2014-08-20 12:30:00)
        end_time : str
            UTC time to use as an end time for subsetting in datetime format.
            (e.g. 2014-08-20 16:30:00)
        title : str
            Plot title.
        xlab : str
            X-axis label.
        ylab : str
            Y-axis label.
        xpad : int
            Padding for X-axis label.
        ypad : int
            Padding for Y-axis label.
        color_bar : bool
            True to add colorbar, False does not.
        cb_pad : str
            Pad to move colorbar, in the form "5%",
            pos is to right for righthand location.
        cb_orient : str
            Colorbar orientation, either 'vertical' or 'horizontal'.
        cb_tick_int : int
            Interval to use for colorbar tick labels,
            higher number "thins" labels.
        cb_label : str
            Label for colorbar (e.g. units 'dBZ').
        cb_fontsize : int
            Font size of the colorbar label.
        cb_ticklabel_size : int
            Font size of colorbar tick labels.
        ax : Matplotlib axis instance
            Axis to plot. None will use the current axis.
        fig : Matplotlib figure instance
            Figure on which to add the plot. None will use the current figure.
        """
        # parse parameters
        ax, fig = common._parse_ax_fig(ax, fig)

        # Return masked or unmasked variable
        # Subsetted if desired
        Var, tsub, Data = self._get_variable_dict_data_time_subset(field, start_time, end_time)
        if mask_procedure is not None:
            Data = common.get_masked_data(Data, mask_procedure, mask_tuple)
        if len(self.height["data"].shape) == 2:
            Height = self._get_2d_height_time_subset(start_time, end_time)

        if plot_log10_var:
            Data = np.log10(Data)
            if cb_label is not None:
                cb_label = r"log$_{10}$[" + cb_label + "]"

        # Create contour level array
        clevels = np.linspace(cminmax[0], cminmax[1], clevs)

        if len(self.height["data"].shape) == 2:
            tSub2D, junk = np.meshgrid(date2num(tsub), self.height["data"][0, :])
            Ht2D = Height.T
            del junk
        else:
            tSub2D, Ht2D = np.meshgrid(date2num(tsub), self.height["data"][:])

        if plot_km:
            Ht2D = Ht2D / 1000.0

        # Get the colormap and calculate data spaced by number of levels
        norm = None
        if discrete_cmap_levels is not None:
            cm = plt.get_cmap(cmap)
            try:
                levpos = np.rint(np.squeeze([np.linspace(0, 255, len(discrete_cmap_levels))])).astype(int)
                # Convert levels to colormap values
                cmap, norm = from_levels_and_colors(discrete_cmap_levels, cm(levpos), extend="max")
            except:
                print("Keyword error: 'discrete_cmap_levels' must " "be a list of float or integer")

        # Plot the time series
        ts = common.image_2d_date(
            tSub2D,
            Ht2D,
            Data.T,
            vmin=vmin,
            vmax=vmax,
            clevs=clevs,
            cmap=cmap,
            norm=norm,
            date_format=date_format,
            tz=tz,
            xdate=xdate,
            date_minor_string=date_minor_string,
            other_major_ticks=height_MajTicks,
            other_minor_ticks=height_MinTicks,
            other_min=height_min,
            other_max=height_max,
            title=title,
            xlab=xlab,
            xlabFontSize=xlabFontSize,
            xpad=xpad,
            ylab=ylab,
            ylabFontSize=ylabFontSize,
            ypad=ypad,
            color_bar=color_bar,
            cb_orient=cb_orient,
            cb_pad=cb_pad,
            cb_tick_int=cb_tick_int,
            cb_label=cb_label,
            cb_fontsize=cb_fontsize,
            cb_ticklabel_size=cb_ticklabel_size,
            ax=ax,
            fig=fig,
        )
        if fill_surface:
            if self.surface is not None:
                sfc = self._get_variable_subset(self.surface["data"][:], start_time, end_time)
                ft = common.plot_fill_surface(tsub, sfc, ymin=fill_min, color=fill_color, ax=ax)
            else:
                print("No surface height information, cannot fill...")
        return
Ejemplo n.º 45
0
    def draw(self, read_log, write_log, step, visual_mode):
        assert visual_mode in [0, 1, 2]
        # read_log: (1, 1, mem_slots) * L_gen
        # write_log: (B, L_gen, mem_slots)
        current_gen_chars = [c for c in self._gen_lines[-1]]
        gen_len = len(current_gen_chars)

        if len(self._gen_lines) >= 2:
            last_gen_chars = [c for c in self._gen_lines[-2]]
            last_gen_len = len(last_gen_chars)
        else:
            last_gen_chars = [''] * gen_len
            last_gen_len = gen_len

        # (L_gen, mem_slots)
        mem_slots = self._topic_slots + self._history_slots + last_gen_len
        read_matrix = torch.cat(read_log,
                                dim=1)[0, 0:gen_len,
                                       0:mem_slots].detach().cpu().numpy()
        read_matrix = self.normalization(read_matrix)

        plt.figure(figsize=(11, 5))

        # visualization of reading attention weights
        num_levels = 100
        vmin, vmax = read_matrix.min(), read_matrix.max()
        midpoint = 0
        levels = np.linspace(vmin, vmax, num_levels)
        midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
        vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
        colors = plt.cm.seismic(vals)
        cmap, norm = from_levels_and_colors(levels, colors)

        plt.imshow(read_matrix, cmap=cmap, interpolation='none')

        # print generated chars and chars in the memory
        fontsize = 14

        plt.text(0.2, gen_len + 0.5, "Topic Memory", fontsize=fontsize)
        plt.text(self._topic_slots,
                 gen_len + 0.5,
                 "History Memory",
                 fontsize=fontsize)
        if last_gen_len == 5:
            shift = 5
        else:
            shift = 6
        plt.text(self._topic_slots + shift,
                 gen_len + 0.5,
                 "Local Memory",
                 fontsize=fontsize)

        # topic memory
        for i in range(0, len(self._keywords)):
            key = self._keywords[i]
            if len(key) == 1:
                key = " " + key + " "
            key = key + "|"
            plt.text(i - 0.4, -0.7, key, fontsize=fontsize)

        start_pos = self._topic_slots
        end_pos = self._topic_slots + self._history_slots

        # history memory
        for i in range(start_pos, end_pos):
            c = self._history_mem[i - start_pos]
            if i == end_pos - 1:
                c = c + " |"

            plt.text(i - 0.2, -0.7, c, fontsize=fontsize)

        start_pos = end_pos
        end_pos = start_pos + last_gen_len

        # local memory
        for i in range(start_pos, end_pos):
            idx = i - start_pos
            plt.text(i - 0.2, -0.7, last_gen_chars[idx], fontsize=fontsize)

        # generated line
        for i in range(0, len(current_gen_chars)):
            plt.text(-1.2, i + 0.15, current_gen_chars[i], fontsize=fontsize)

        plt.colorbar()
        plt.tick_params(labelbottom=False, labelleft=False)

        x_major_locator = plt.MultipleLocator(1)
        y_major_locator = plt.MultipleLocator(1)
        ax = plt.gca()
        ax.xaxis.set_major_locator(x_major_locator)
        ax.yaxis.set_major_locator(y_major_locator)
        #plt.tight_layout()

        if visual_mode == 1:
            fig = plt.gcf()
            fig.savefig(self._log_path + 'visual_step_{}.png'.format(step),
                        dpi=300,
                        quality=100,
                        bbox_inches="tight")
        elif visual_mode == 2:
            plt.show()

        # update history memory
        if write_log is not None:
            if len(last_gen_chars) == 0:
                print("last generated line is empty!")

            write_log = write_log[0, :, :].detach().cpu().numpy()
            history_mem = copy.deepcopy(self._history_mem)
            for i, c in enumerate(last_gen_chars):
                selected_slot = np.argmax(write_log[i, :])
                if selected_slot >= self._history_slots:
                    continue
                history_mem[selected_slot] = c

            self._history_mem = history_mem
Ejemplo n.º 46
0
    def plot_cfad(self,
                  field,
                  height_axis=1,
                  xbinsminmax=None,
                  nbinsx=50,
                  points_thresh_fraction=None,
                  start_time=None,
                  end_time=None,
                  vmin=None,
                  vmax=None,
                  cmap=None,
                  discrete_cmap_levels=None,
                  mask_below=None,
                  plot_percent=False,
                  plot_colorbar=True,
                  x_min=None,
                  x_max=None,
                  y_min=None,
                  y_max=None,
                  xlab=None,
                  xlabFontSize=None,
                  xpad=None,
                  ylab=None,
                  ylabFontSize=None,
                  ypad=None,
                  title=None,
                  titleFontSize=None,
                  cb_fontsize=None,
                  cb_ticklabel_size=None,
                  cb_orient=None,
                  cb_pad=None,
                  cb_levs=None,
                  cb_tick_int=None,
                  quantiles=None,
                  qcolor='k',
                  qlabels_on=False,
                  qlabel_color=None,
                  qlabel_size=None,
                  qmask_above_height=None,
                  qmask_below_height=None,
                  qmask_between_height=None,
                  ax=None,
                  fig=None):
        """
        Create a frequency by altitude distribution plot of two variables.
        This is the traditional method of calculating a frequency distribution
        at each height of input array by iterating through the height array
        and input data array.

        NOTE: This routine only works with Cartesian data.

        Parameters
        ----------
        field : str
            Name of the field to use in CFAD calculation.
        height_axis : int
            The dimension to use as the height axis.
        xbinsminmax : 2-tuple
            A tuple with the minimum and maximax values to
            use with xarr. None will use min/max of xarr.
        nbinsx : int
            The number of bins to use with xarr, default is 50.
        points_thresh_fraction : float
            The fraction of points that must be present for the
            CFAD to be calculated. Following Yuter and Houzed 1995,
            the default values is 0.1 (10%) of potential data coverage
            is required. This threshold removes anomolous results when
            a small number of points is present.
        start_time : str
            UTC time to use as start time for subsetting in datetime format.
            (e.g. 2014-08-20 12:30:00)
        end_time : str
            UTC time to use as an end time for subsetting in datetime format.
            (e.g. 2014-08-20 16:30:00)
        vmin : float
            Minimum value to display.
        vmax : float
            Maximum value to display.
        cmap : str
            Matplotlib colormap string.
        discrete_cmap_levels : array
            A list of levels to be used for display. If chosen discrete
            color will be used in the colorbar instead of a linear luminance
            mapping.
        mask_below : float
            If provided, values less than mask_below will be masked.
        plot_percent : boolean
            True to display percentage. Default is to display fraction.
        plot_colorbar : boolean
            True to diaplay colorbar. False does not display colorbar.
        x_min : float
            Minimum value for X-axis.
        x_max : float
            Maximum value for X-axis.
        y_min : float
            Minimum value for Y-axis.
        y_max : float
            Maximum value for Y-axis.
        xlab : str
            X-axis label.
        ylab : str
            Y-axis label.
        xpad : int
            Padding for X-axis label.
        ypad : int
            Padding for Y-axis label.
        xlabFontSize : int
            Font size to use for X-axis label.
        ylabFontSize : int
            Font size to use for Y-axis label.
        title : str
            Plot title.
        titleFontSize : int
            Font size to use for Title label.
        cb_fontsize : int
            Font size of the colorbar label.
        cb_ticklabel_size : int
            Font size of colorbar tick labels.
        cb_pad : str
            Pad to move colorbar, in the form "5%",
            pos is to right for righthand location.
        cb_orient : str
            Colorbar orientation, either 'vertical' or 'horizontal'.
        cb_levs : int
            Number of colorbar levels to use in tick calculation.
        cb_tick_int : int
            Interval to use for colorbar tick labels,
            higher number "thins" labels.
        quantiles : list
            A list of percentage values for quantile calculations.
        qcolor : str
            Color to use for quantile plot lines.
        qlabels_on : boolean
            True to print labels of quantiles on plot. False for no labels.
        qlabel_color : str
            Color of quantile labels if activated. Default black.
        qlabel_size : int
            Size of the quantile labels.
        qmask_above_height : float
            Mask quantile data above this height.
        qmask_below_height : float
            Mask quantile data below this height.
        qmask_between_height : tuple, float
            Mask quantile data between this height.
        ax : Matplotlib axis instance
            Axis to plot. None will use the current axis.
        fig : Matplotlib figure instance
            Figure on which to add the plot. None will use the current figure.
        """
        # parse parameters
        ax = common._parse_ax(ax)
        # Snag the data from requested field
        xarr = self._get_fields_variable_dict_data_time_subset(
            field, start_time, end_time)

        if xbinsminmax is None:
            xbinsminmax = (np.ma.min(xarr), np.ma.max(xarr))
        binsx = np.linspace(xbinsminmax[0],
                            xbinsminmax[1],
                            nbinsx,
                            endpoint=True)

        cb_label = "Frequency"

        if plot_percent:
            cb_label = cb_label + " (%)"

        # Reshape the array so that height axis is first dimension
        if height_axis != 0:
            ht = np.rollaxis(self.heightfield['data'], height_axis)
            xarr = np.rollaxis(xarr, height_axis)
        else:
            ht = self.heightfield['data'].copy()

        cfad_dict = self.calc_cfad(xarr, binsx, self.height['data'][:],
                                   points_thresh_fraction)
        if plot_percent:
            CFAD = cfad_dict['frequency_percent']
        else:
            CFAD = cfad_dict['frequency_points']

        if mask_below is not None:
            CFAD = np.ma.masked_where(CFAD < mask_below, CFAD)

        # Set the axes
        common._set_axes(ax,
                         x_min=x_min,
                         x_max=x_max,
                         y_min=y_min,
                         y_max=y_max,
                         title=title,
                         titleFontSize=titleFontSize,
                         xlab=xlab,
                         ylab=ylab,
                         xpad=xpad,
                         ypad=ypad,
                         xlabFontSize=xlabFontSize,
                         ylabFontSize=ylabFontSize)

        # Plot the data
        # Get the colormap and calculate data spaced by number of levels
        norm = None
        if discrete_cmap_levels is not None:
            cm = plt.get_cmap(cmap)
            try:
                levpos = np.rint(
                    np.squeeze(
                        [np.linspace(0, 255,
                                     len(discrete_cmap_levels))])).astype(int)
                # Convert levels to colormap values
                cmap, norm = from_levels_and_colors(discrete_cmap_levels,
                                                    cm(levpos),
                                                    extend='max')
            except:
                print("Keyword error: 'discrete_cmap_levels' must "
                      "be a list of float or integer")

        p = ax.pcolormesh(cfad_dict['xaxis'],
                          cfad_dict['yaxis'],
                          CFAD,
                          vmin=vmin,
                          vmax=vmax,
                          norm=norm,
                          cmap=cmap)

        if plot_colorbar:
            cb = common.add_colorbar(ax,
                                     p,
                                     orientation=cb_orient,
                                     pad=cb_pad,
                                     label=cb_label,
                                     fontsize=cb_fontsize,
                                     ticklabel_size=cb_ticklabel_size,
                                     clevs=cb_levs,
                                     tick_interval=cb_tick_int)

        if quantiles is not None:
            qArr = self.plot_quantiles(
                field,
                quantiles=quantiles,
                height_axis=height_axis,
                start_time=start_time,
                end_time=end_time,
                qcolor=qcolor,
                qlabels_on=qlabels_on,
                qlabel_color=qlabel_color,
                qlabel_size=qlabel_size,
                qmask_above_height=qmask_above_height,
                qmask_below_height=qmask_below_height,
                qmask_between_height=qmask_between_height,
                setup_axes=False,
                ax=ax)

        del (CFAD, norm)
        return cfad_dict
Ejemplo n.º 47
0
def main():
    clevs_lkeff_snowfalldays = [0, 0.1, 0.8, 1.6, 2.4, 3.2, 4.0, 5]
    clevs_lkeff_snowfall = [0, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 2, 5, 10]
    mycolors = ["white", "indigo", "blue", "dodgerblue", "aqua", "lime", "yellow", "gold",
                                                     "orange", "orangered", "red", "firebrick", ]



    start_year = 1980
    end_year = 2009


    label_to_hles_dir = OrderedDict(
        [
         ("Obs", Path("/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_Obs_monthly_icefix_1980-2009")),
         ("CRCM5_NEMO", Path("/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_CRCM5_NEMO_1980-2009")),
         ("CRCM5_HL", Path("/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_CRCM5_Hostetler_1980-2009")),
         # ("CRCM5_NEMO_TT_PR", Path("/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_CRCM5_NEMO_based_on_TT_PR_1980-2009"))
        ]
    )


    label_to_line_style = {
        "Obs": "k.-",
        "CRCM5_NEMO": "r",
        "CRCM5_HL": "b",
        "CRCM5_NEMO_TT_PR": "g"
    }

    # clevs = clevs_lkeff_snowfall
    # vname = "snow_fall"
    # units = "cm"

    clevs = clevs_lkeff_snowfalldays
    vname = "lkeff_snowfall_days"
    units = "days"
    npc = 1


    cmap, bn = colors.from_levels_and_colors(clevs, mycolors[:len(clevs) - 1])
    cmap.set_over(mycolors[len(clevs) - 2])





    label_to_y_to_snfl = {}
    label_to_pc = {}

    label_to_eof = OrderedDict()
    label_to_varfraction = OrderedDict()

    mask = None

    plot_utils.apply_plot_params(font_size=12)


    years = None
    lats = None
    lons = None
    the_mask = None
    for label, folder in label_to_hles_dir.items():

        y_to_snfl = {}


        for the_file in folder.iterdir():
            if not the_file.name.endswith(".nc"):
                continue

            with Dataset(str(the_file)) as ds:
                print(ds)
                snfl = ds.variables[vname][:]
                year_current = ds.variables["year"][:]

                if mask is None:
                    lons, lats = [ds.variables[k][:] for k in ["lon", "lat"]]
                    lons[lons > 180] -= 360
                    mask = maskoceans(lons, lats, lons, inlands=True, resolution="i")


                if start_year <= year_current[0] <= end_year:
                    y_to_snfl[year_current[0]] = snfl[0]


        label_to_y_to_snfl[label] = y_to_snfl




    lons[lons < 0] += 360
    b = Basemap(lon_0=180,
                llcrnrlon=lons[0, 0],
                llcrnrlat=lats[0, 0],
                urcrnrlon=lons[-1, -1],
                urcrnrlat=lats[-1, -1],
                resolution="i", area_thresh=2000)



    # plot the eofs


    plot_utils.apply_plot_params(font_size=10, width_cm=30, height_cm=8)


    xx, yy = b(lons, lats)

    fig = plt.figure()
    gs = GridSpec(1, len(label_to_hles_dir), wspace=0)


    for col, label in enumerate(label_to_hles_dir):

            y_to_snfl = label_to_y_to_snfl[label]

            snfl_clim = np.array([field for field in y_to_snfl.values()]).mean(axis=0)
            # snfl_clim = np.ma.masked_where(mask.mask, snfl_clim)

            ax = fig.add_subplot(gs[0, col])
            im = b.pcolormesh(xx, yy, snfl_clim, cmap=cmap, norm=bn, ax=ax)
            cb = b.colorbar(im, extend="max")
            cb.ax.set_visible(col == len(label_to_hles_dir) - 1)
            ax.set_title("{}".format(label))

            b.drawcoastlines(ax=ax)



    fig.savefig(str(label_to_hles_dir["Obs"].joinpath("hles_clim_{}_{}-{}.png".format(vname, start_year, end_year))), bbox_inches="tight", dpi=300)
def cross_section(variable_name,
                  date,
                  time,
                  start_lat,
                  start_lon,
                  end_lat,
                  end_lon,
                  save=False):
    '''This function plots a vertical cross section of the chosen 
    variable. Supported variables for plotting procedure are 
    vertical_velocity, rh, omega, absolute_vorticity, theta_e and
    reflectivity.'''

    ### Predefine some variables ###
    # Define data filename
    data_dir = '/scratch3/thomasl/work/data/casestudy_baden/'
    filename = '{}wrfout_d02_{}_{}:00'.format(data_dir, date, time)

    # Define save directory
    save_dir = '/scratch3/thomasl/work/retrospective_part/' 'casestudy_baden/cross_section/'

    # Create the start point and end point for the cross section
    start_point = CoordPair(lat=start_lat, lon=start_lon)
    end_point = CoordPair(lat=end_lat, lon=end_lon)

    ### Start plotting procedure ###
    # Open NetCDF file
    ncfile = Dataset(filename)

    # Extract the model height, terrain height and variables
    ht = getvar(ncfile, 'z') / 1000  # change to km
    ter = getvar(ncfile, 'ter') / 1000

    if variable_name == 'vertical_velocity':
        variable = getvar(ncfile, 'wa', units='kt')
        title_name = 'Vertical Velocity'
        colorbar_label = 'Vertical Velocity [$kn$]'
        variable_min = -2
        variable_max = 2

    elif variable_name == 'rh':
        variable = getvar(ncfile, 'rh')
        title_name = 'Relative Humidity'
        colorbar_label = 'Relative Humidity [$pct$]'
        variable_min = 0
        variable_max = 100

    elif variable_name == 'omega':
        variable = getvar(ncfile, 'omega')
        title_name = 'Vertical Motion (Omega)'
        colorbar_label = 'Omega [$Pa$ $s^-$$^1$]'
        variable_min = -5
        variable_max = 5

    elif variable_name == 'absolute_vorticity':
        variable = getvar(ncfile, 'avo')
        title_name = 'Absolute Vorticity'
        colorbar_label = 'Absolute Vorticity [$10^{-5}$' '$s^{-1}$]'
        variable_min = -50
        variable_max = 100

    elif variable_name == 'theta_e':
        variable = getvar(ncfile, 'theta_e')
        title_name = 'Theta-E'
        colorbar_label = 'Theta-E [$K$]'
        variable_min = 315
        variable_max = 335

    elif variable_name == 'reflectivity':
        variable = getvar(ncfile, 'dbz')  #, timeidx=-1
        title_name = 'Reflectivity'
        colorbar_label = 'Reflectivity [$dBZ$]'
        variable_min = 5
        variable_max = 75

    # Linear Z for interpolation
    Z = 10**(variable / 10)

    # Compute the vertical cross-section interpolation
    z_cross = vertcross(Z,
                        ht,
                        wrfin=ncfile,
                        start_point=start_point,
                        end_point=end_point,
                        latlon=True,
                        meta=True)

    # Convert back after interpolation
    variable_cross = 10.0 * np.log10(z_cross)

    # Make a copy of the z cross data
    variable_cross_filled = np.ma.copy(to_np(variable_cross))

    # For each cross section column, find the first index with
    # non-missing values and copy these to the missing elements below
    for i in range(variable_cross_filled.shape[-1]):
        column_vals = variable_cross_filled[:, i]
        first_idx = int(np.transpose((column_vals > -200).nonzero())[0])
        variable_cross_filled[0:first_idx,
                              i] = variable_cross_filled[first_idx, i]

    ter_line = interpline(ter,
                          wrfin=ncfile,
                          start_point=start_point,
                          end_point=end_point)

    # Get latitude and longitude points
    lats, lons = latlon_coords(variable)

    # Create the figure
    fig = plt.figure(figsize=(15, 10))
    ax = plt.axes()

    ys = to_np(variable_cross.coords['vertical'])
    xs = np.arange(0, variable_cross.shape[-1], 1)

    # Make contour plot
    if variable_name == 'reflectivity':
        levels = np.arange(variable_min, variable_max, 5)

        # Create the dbz color table found on NWS pages.
        dbz_rgb = np.array(
            [[4, 233, 231], [1, 159, 244], [3, 0, 244], [2, 253, 2],
             [1, 197, 1], [0, 142, 0], [253, 248, 2], [229, 188, 0],
             [253, 149, 0], [253, 0, 0], [212, 0, 0], [188, 0, 0],
             [248, 0, 253], [152, 84, 198]], np.float32) / 255.0

        dbz_cmap, dbz_norm = from_levels_and_colors(levels,
                                                    dbz_rgb,
                                                    extend='max')

    else:
        levels = np.linspace(variable_min, variable_max, 11)

    if variable_name == 'omega' or variable_name == 'vertical_velocity':

        def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
            new_cmap = colors.LinearSegmentedColormap.from_list(
                'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name,
                                                    a=minval,
                                                    b=maxval),
                cmap(np.linspace(minval, maxval, n)))
            return new_cmap

        old_cmap = plt.get_cmap('RdYlBu')
        cmap = truncate_colormap(old_cmap, 0.05, 0.9)

        norm = colors.DivergingNorm(vmin=variable_min,
                                    vcenter=0,
                                    vmax=variable_max)

    else:
        cmap = ListedColormap(sns.cubehelix_palette(20, start=.5, rot=-.75))

    if variable_name == 'omega' or variable_name == 'vertical_velocity':
        variable_contours = ax.contourf(xs,
                                        ys,
                                        to_np(variable_cross_filled),
                                        levels=levels,
                                        cmap=cmap,
                                        extend='both',
                                        norm=norm)

    elif variable_name == 'rh':
        variable_contours = ax.contourf(xs,
                                        ys,
                                        to_np(variable_cross_filled),
                                        levels=levels,
                                        cmap=cmap,
                                        extend='max')
    elif variable_name == 'reflectivity':
        variable_contours = ax.contourf(xs,
                                        ys,
                                        to_np(variable_cross_filled),
                                        levels=levels,
                                        cmap=dbz_cmap,
                                        norm=dbz_norm,
                                        extend='both')

    else:
        variable_contours = ax.contourf(xs,
                                        ys,
                                        to_np(variable_cross_filled),
                                        levels=levels,
                                        cmap=cmap,
                                        extend='both')
    # Plot wind barbs
    if variable_name == 'vertical_velocity':
        u = getvar(ncfile, 'ua', units='kt')

        U = 10**(u / 10)

        u_cross = vertcross(U,
                            ht,
                            wrfin=ncfile,
                            start_point=start_point,
                            end_point=end_point,
                            latlon=True,
                            meta=True)

        u_cross = 10.0 * np.log10(u_cross)

        u_cross_filled = np.ma.copy(to_np(u_cross))

        for i in range(u_cross_filled.shape[-1]):
            column_vals = u_cross_filled[:, i]
            first_idx = int(np.transpose((column_vals > -200).nonzero())[0])
            u_cross_filled[0:first_idx, i] = u_cross_filled[first_idx, i]

        ax.barbs(xs[::3],
                 ys[::3],
                 to_np(u_cross_filled[::3, ::3]),
                 to_np(variable_cross_filled[::3, ::3]),
                 length=7,
                 zorder=1)

    if variable_name == 'omega':
        u = getvar(ncfile, 'ua', units='kt')

        U = 10**(u / 10)

        u_cross = vertcross(U,
                            ht,
                            wrfin=ncfile,
                            start_point=start_point,
                            end_point=end_point,
                            latlon=True,
                            meta=True)

        u_cross = 10.0 * np.log10(u_cross)

        u_cross_filled = np.ma.copy(to_np(u_cross))

        for i in range(u_cross_filled.shape[-1]):
            column_vals = u_cross_filled[:, i]
            first_idx = int(np.transpose((column_vals > -200).nonzero())[0])
            u_cross_filled[0:first_idx, i] = u_cross_filled[first_idx, i]

        w = getvar(ncfile, 'wa', units='kt')

        W = 10**(w / 10)

        w_cross = vertcross(W,
                            ht,
                            wrfin=ncfile,
                            start_point=start_point,
                            end_point=end_point,
                            latlon=True,
                            meta=True)

        w_cross = 10.0 * np.log10(w_cross)

        w_cross_filled = np.ma.copy(to_np(w_cross))

        for i in range(w_cross_filled.shape[-1]):
            column_vals = w_cross_filled[:, i]
            first_idx = int(np.transpose((column_vals > -200).nonzero())[0])
            w_cross_filled[0:first_idx, i] = w_cross_filled[first_idx, i]

        ax.barbs(xs[::3],
                 ys[::3],
                 to_np(u_cross_filled[::3, ::3]),
                 to_np(w_cross_filled[::3, ::3]),
                 length=7,
                 zorder=1,
                 color='grey')

    # Add color bar
    cbar = mpu.colorbar(variable_contours,
                        ax,
                        orientation='vertical',
                        aspect=40,
                        shrink=.05,
                        pad=0.05)
    cbar.set_label(colorbar_label, fontsize=15)
    cbar.set_ticks(levels)

    # Set x-ticks to use latitude and longitude labels
    coord_pairs = to_np(variable_cross.coords['xy_loc'])
    x_ticks = np.arange(coord_pairs.shape[0])
    x_labels = [
        pair.latlon_str(fmt='{:.2f}, {:.2f}') for pair in to_np(coord_pairs)
    ]

    # Set desired number of x ticks below
    num_ticks = 5
    thin = int((len(x_ticks) / num_ticks) + .5)
    ax.set_xticks(x_ticks[::thin])
    ax.set_xticklabels(x_labels[::thin], rotation=45, fontsize=10)
    ax.set_xlim(x_ticks[0], x_ticks[-1])

    # Set y-ticks and limit the height
    ax.set_yticks(np.linspace(0, 12, 13))
    ax.set_ylim(0, 12)

    # Set x-axis and y-axis labels
    ax.set_xlabel('Latitude, Longitude', fontsize=12.5)
    ax.set_ylabel('Height [$km$]', fontsize=12.5)

    # Fill in mountian area
    ht_fill = ax.fill_between(xs,
                              0,
                              to_np(ter_line),
                              facecolor='saddlebrown',
                              zorder=2)

    # Make nicetime
    xr_file = xr.open_dataset(filename)
    nicetime = pd.to_datetime(xr_file.QVAPOR.isel(Time=0).XTIME.values)

    # Add title
    ax.set_title('Vertical Cross-Section of {}'.format(title_name),
                 fontsize=20,
                 loc='left')
    ax.set_title('Valid time: {} {}'.format(
        nicetime.strftime('%Y-%m-%d %H:%M'), 'UTC'),
                 fontsize=15,
                 loc='right')

    # Add grid for y axis
    ax.grid(axis='y', linestyle='--', color='grey')

    plt.show()

    ### Save figure ###
    if save == True:
        fig.savefig('{}cross_section_{}.png'.format(save_dir, variable_name),
                    bbox_inches='tight',
                    dpi=300)
Ejemplo n.º 49
0
def cfad_plot(var,
              data=None,
              cfad=None,
              hts=None,
              nbins=20,
              ax=None,
              maxval=10.0,
              above=2.0,
              below=15.0,
              bins=None,
              log=False,
              pick=None,
              z_resolution=1.0,
              levels=None,
              tspan=None,
              cont=False,
              rconf=None,
              mask=None,
              **kwargs):

    if hts is None:
        print('please provide nominal heights to cfad_plot')
        return
    if data is not None:
        #         hold = deepcopy(data)
        #         if mask is not None:
        #             data[mask] = -1
        cfad, reshts, vbins = GF.cfad(data,
                                      hts,
                                      value_bins=bins,
                                      above=above,
                                      below=below,
                                      pick=pick,
                                      z_resolution=z_resolution,
                                      tspan=tspan,
                                      z_ind=0,
                                      ret_z=1,
                                      ret_bin=1,
                                      mask=mask)
    elif cfad is not None:
        if bins is not None:
            vbins = bins[:-1]
        else:
            try:
                vbins = np.arange(np.nanmin(data), np.nanmax(data), nbins)
            except:
                vbins = np.arange(0, 10, nbins)
                bins = np.arange(0, 10, nbins)
        reshts = hts
    else:
        print('please specify data or cfad')
        return

    if ax is None:
        fig, ax = plt.subplots()
    else:
        # ax has been passed in, do nothing to ax, but need to get the parent fig
        fig = ax.get_figure()

    if log:
        norm = colors.LogNorm(vmin=1e-5, vmax=1e2)
    else:
        norm = None

    # plot the CFAD
    cfad_ma = np.ma.masked_where(cfad == 0, cfad)
    print(np.shape(cfad_ma), 'cfad shape')
    if cont is True:
        levs = [0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 15.0, 20., 25.]
        cols = [
            'silver', 'darkgray', 'slategrey', 'dimgray', 'blue',
            'mediumaquamarine', 'yellow', 'orange', 'red', 'fuchsia', 'violet'
        ]
        try:
            pc = ax.contourf(bins[:-1],
                             reshts,
                             cfad_ma,
                             levs,
                             colors=cols,
                             extend='both')
        except TypeError as e:
            print('Can not plot {v} with exception {e}'.format(v=var, e=e))
            return fig, ax
    else:

        if levels is not None:
            cmap, norm = from_levels_and_colors([
                0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0, 15.0, 20., 25.
            ], [
                'silver', 'darkgray', 'slategrey', 'dimgray', 'blue',
                'mediumaquamarine', 'yellow', 'orange', 'red', 'fuchsia',
                'violet'
            ])  # mention levels and colors here
            #print cmap
            pc = ax.pcolormesh(bins, reshts, cfad_ma, norm=norm, cmap=cmap)
        else:

            pc = ax.pcolormesh(bins,
                               reshts,
                               cfad_ma,
                               vmin=0,
                               vmax=maxval,
                               norm=norm,
                               **kwargs)

    cb = fig.colorbar(pc, ax=ax)
    cb.set_label('Frequency (%)')
    ax.set_ylabel('Height (km MSL)')
    #        try:
    if rconf is not None:
        if var == 'DRC' or var == 'DRS':
            varn = rconf.zdr_name
        elif var == 'DZC' or var == 'DZS':
            varn = rconf.dz_name
        elif var == 'KDC' or var == 'KDS':
            varn = rconf.kdp_name
        elif var == 'WSvar' or var == 'WCvar':
            varn = rconf.w_name
        else:
            varn = var
#        print 'ln192',varn
#         if varn in rconf.names.keys():
#
#             ax.set_xlabel('{n} {u}'.format(n=rconf.names[varn], u=rconf.units[varn]))
#             #print rconf.print_title(tm=tspan)
# #            ax.set_title("{d}".format(d=rconf.print_title(tm=tspan)))
#     #            ax.set_title('%s %s %s CFAD' % (self.print_date(), self.radar_name, self.longnames[var]))
#         else:
#             ax.set_xlabel('{n}'.format(n=var))
#             #print rconf.print_title(tm=tspan)
# #            ax.set_title("{d}".format(d=rconf.print_title(tm=tspan)))
# #        except:
# #            pass

    return fig, ax
Ejemplo n.º 50
0
def plot_number_of_bins():

    multiplier = 1

    selected_vars = {
        "NBIN": [
            8,
        ]
    }
    nbins_max = 8
    data_source = {
        # "(CRCM5-default, from GenPhysX)": "/RESCUE/skynet3_rech1/huziy/NEI_geophysics/WC_0.11_deg/geophys_CORDEX_NA_0.11deg_695x680_filled_grDes_barBor_Crop2Gras_peat_with_directions",
        # "(MODIS)": "/RESCUE/skynet3_rech1/huziy/NEI_geophysics/WC_0.11_deg/fields_from_Bernardo/VF_WCAN.rpn",
        "":
        "/RESCUE/skynet3_rech1/huziy/NEI_geophysics/WC_0.11_deg/fields_from_Caio/bins_WC011"
    }

    file_with_target_coords = "/RESCUE/skynet3_rech1/huziy/NEI_geophysics/WC_0.11_deg/fields_from_Caio/WC011_VF2.rpn"

    shape_files = {
        # "k": "/BIG1/aganji/skynet3_rech3/CLASS_snow/output_31year_HiRes_ck_paper_aug26/CORDEX_NA_0.44/PROVINCE.shp",
        "m":
        "/RESCUE/skynet3_rech1/huziy/CNRCWP/C3/GRDC_basins/GRDC_405_basins_from_mouth.shp"
    }

    with RPN(file_with_target_coords) as r:
        assert isinstance(r, RPN)
        glacier_fraction = r.get_first_record_for_name_and_level(varname="VF",
                                                                 level=2)

    lons_t, lats_t, bmap = get_lons_lats_basemap(file_with_target_coords)

    nx, ny = lons_t.shape

    indx_subspace = IndexSubspace(i_start=0,
                                  i_end=nx // 1.5,
                                  j_start=80,
                                  j_end=ny - 60)
    lons_t, lats_t, bmap = get_lons_lats_basemap(file_with_target_coords,
                                                 index_subset=indx_subspace)

    xx, yy = bmap(lons_t, lats_t)

    clevels = np.arange(0.5, 9, 1)

    color_list = [
        "blue", "royalblue", "dodgerblue", "cyan", "lime", "yellow", "orange",
        "red", "darkred", "brown"
    ][:len(clevels) - 1]

    print(len(clevels), len(color_list))

    assert len(clevels) == len(color_list) + 1

    cmap, norm = colors.from_levels_and_colors(clevels, color_list)

    # create the folder for images if it does not exist yet
    if not img_folder.is_dir():
        img_folder.mkdir()

    plot_utils.apply_plot_params()

    for ds_label, ds_path in data_source.items():

        with RPN(ds_path) as r:
            assert isinstance(r, RPN)
            print(r.get_list_of_varnames())

            for vname, levels in selected_vars.items():
                lev_to_field = r.get_2D_field_on_all_levels(name=vname)

                lons_s, lats_s = r.get_longitudes_and_latitudes_for_the_last_read_rec(
                )

                xs, ys, zs = lat_lon.lon_lat_to_cartesian(
                    lons_s.flatten(), lats_s.flatten())
                xt, yt, zt = lat_lon.lon_lat_to_cartesian(
                    lons_t.flatten(), lats_t.flatten())

                ktree = KDTree(list(zip(xs, ys, zs)))

                dists, inds = ktree.query(list(zip(xt, yt, zt)))

                for the_level in levels:
                    the_field = lev_to_field[the_level]

                    # Do the plotting
                    fig = plt.figure()

                    ax = plt.gca()
                    ax.set_title(vname_to_level_to_title[vname][the_level] +
                                 " {}".format(ds_label))

                    to_plot = the_field.flatten()[inds].reshape(xx.shape)

                    to_plot *= multiplier

                    # mask points without glacier fractions
                    to_plot = np.ma.masked_where(glacier_fraction < 0.01,
                                                 to_plot)

                    to_plot = maskoceans(
                        np.where(lons_t < 180, lons_t, lons_t - 360), lats_t,
                        to_plot)

                    if vname not in ["FACC"]:
                        # cs = bmap.contourf(xx, yy, to_plot, 20, ax=ax)
                        cs = bmap.pcolormesh(xx,
                                             yy,
                                             to_plot,
                                             cmap=cmap,
                                             norm=norm,
                                             ax=ax)
                    else:
                        cs = bmap.contourf(xx,
                                           yy,
                                           to_plot,
                                           20,
                                           ax=ax,
                                           norm=LogNorm())

                    bmap.colorbar(cs, ticks=clevels)
                    bmap.drawcoastlines(ax=ax)
                    bmap.drawcountries()
                    bmap.drawstates()

                    # read the relevant shape files
                    for i, (clr, shp) in enumerate(shape_files.items()):
                        bmap.readshapefile(shp[:-4],
                                           "field_{}".format(i),
                                           color=clr)

                    img_path = img_folder.joinpath("{}_{}_{}_{}.png".format(
                        vname, the_level, ds_label,
                        vname_to_level_to_title[vname][the_level].replace(
                            " ", "_")))
                    fig.tight_layout()
                    fig.savefig(str(img_path), bbox_inches="tight")
                    plt.close(fig)
Ejemplo n.º 51
0
def plotFig(heatMapDF, droppedPercentDF, droppedNumDF, numOrPercent,
            generation):
    global filePath
    global frequencies
    global threshNum
    global totalFreq
    global numGroup
    global meanPath

    meanDF = getMeanDF(heatMapDF)
    print(meanDF)

    ax = []
    with sns.axes_style('white'):
        #Plus 2 for dropped row and average row
        gs = gridspec.GridSpec(int(numGroup) + 2, 10)
        ax.append(plt.subplot(gs[:-3, 2:-1]))
        if (numOrPercent == 'num'):
            #Dropped
            ax.append(plt.subplot(gs[-2, 2:-1]))

            #Mean
            ax.append(plt.subplot(gs[-1, 2:-1]))
        else:
            #Dropped
            ax.append(plt.subplot(gs[-1, 2:-1]))

    #Show absolute number
        if numOrPercent == 'num':

            for i in range(len(frequencies)):
                trick = threshNum[i] + 10
                cmap, norm = mcolors.from_levels_and_colors(
                    [0, threshNum[i], trick], ['white', 'grey', 'grey'],
                    extend='max')
                sns.heatmap(
                    heatMapDF.mask((
                        (heatMapDF == heatMapDF) | heatMapDF.isnull())
                                   & (heatMapDF.columns != frequencies[i])),
                    ax=ax[0],
                    cbar=False,
                    annot=True,
                    fmt='g',
                    cmap=cmap,
                    norm=norm)

            #Dropped row
            for i in range(len(frequencies)):
                cmap, norm = mcolors.from_levels_and_colors(
                    [0, threshNum[i], trick], ['white', 'white', 'white'],
                    extend='max')
                sns.heatmap(droppedNumDF.mask(
                    ((droppedNumDF == droppedNumDF) | droppedNumDF.isnull())
                    & (droppedNumDF.columns != frequencies[i])),
                            ax=ax[1],
                            cbar=False,
                            annot=True,
                            fmt='g',
                            cmap=cmap,
                            norm=norm)
            #Mean row
            for i in range(len(frequencies)):
                cmap, norm = mcolors.from_levels_and_colors(
                    [0, threshNum[i], trick], ['white', 'white', 'white'],
                    extend='max')
                sns.heatmap(meanDF.mask(((meanDF == meanDF) | meanDF.isnull())
                                        & (meanDF.columns != frequencies[i])),
                            ax=ax[2],
                            cbar=False,
                            annot=True,
                            fmt='g',
                            cmap=cmap,
                            norm=norm)

        #Show percentage
        elif numOrPercent == 'percent':
            percentHeatMap = pd.DataFrame()
            for i in range(heatMapDF.shape[1]):
                percentHeatMap[frequencies[i]] = heatMapDF[
                    frequencies[i]].values / float(totalFreq[i])

            for i in range(len(frequencies)):
                trick = threshNum[i] + 10
                cmap, norm = mcolors.from_levels_and_colors(
                    [0, threshNum[i] / float(totalFreq[i]), trick],
                    ['white', 'grey', 'grey'],
                    extend='max')
                sns.heatmap(percentHeatMap.mask(
                    ((percentHeatMap == percentHeatMap)
                     | percentHeatMap.isnull())
                    & (percentHeatMap.columns != frequencies[i])),
                            ax=ax[0],
                            cbar=False,
                            annot=True,
                            fmt="2.1%",
                            cmap=cmap,
                            norm=norm)

            for i in range(len(frequencies)):
                cmap, norm = mcolors.from_levels_and_colors(
                    [0, threshNum[i], trick], ['white', 'white', 'white'],
                    extend='max')
                sns.heatmap(droppedPercentDF.mask(
                    ((droppedPercentDF == droppedPercentDF)
                     | droppedPercentDF.isnull())
                    & (droppedPercentDF.columns != frequencies[i])),
                            ax=ax[1],
                            cbar=False,
                            annot=True,
                            fmt="1.1%",
                            cmap=cmap,
                            norm=norm)

    # want a more natural, table-like display
    ax[0].xaxis.tick_top()

    ax[0].set_xticklabels(frequencies, minor=False)
    global yRange

    ax[0].set_yticklabels(yRange, minor=False, rotation=0)
    ax[1].set_yticklabels(['Dropped'], minor=False, rotation=0)
    ax[1].set_xticklabels([], minor=False)
    if (numOrPercent == 'num'):
        ax[2].set_yticklabels(['Mean'], minor=False, rotation=0)
        ax[2].set_xticklabels([], minor=False)

    fileName = filePath.split('.')[0]
    name = fileName.split('/')[-1]

    if generation == 0:
        ax[0].set_title('Prior Shrinking Range: Distribution of ' + col +
                        ' while measuring ' + name,
                        y=1.1)
    else:
        ax[0].set_title('After Shrinking Range: Distribution of ' + col +
                        ' while measuring ' + name,
                        y=1.1)

    cur_pos = []
    #	for i in range(2):
    #		cur_pos.append(ax[i].get_position())
    #		ax[i].set_position([cur_pos[i].x0 + cur_pos[i].width * 0.15, cur_pos[i].y0 + cur_pos[i].height * 0.1, cur_pos[i].width * 0.75, cur_pos[i].height * 0.8])

    #plt.colorbar(heatmap)
    figName = path(
    ) + "data/sunflow/fig/" + col + '_' + name + '_HeatMap_DVFS_' + numOrPercent + '_' + str(
        generation)
    plt.savefig(figName + ".eps", format='eps')
Ejemplo n.º 52
0
def main():

    begin_time = time()

    # =============================
    # Loading the verification data
    # =============================

    vtype = {'assim': 'Assimilated proxies', 'verif':'Non-assimilated proxies'}
    
    nbperiods = len(verif_period)
    assim_dict = [dict() for x in range(nbperiods)]
    verif_dict = [dict() for x in range(nbperiods)]

    # loop over verification periods & load data in dictionaries
    for p in range(nbperiods):
        # Read the pickle files containing summary stats
        fname_assim = datadir_input+'/'+nexp+'/'+'verifProxy_'+str(verif_period[p][0])+'to'+str(verif_period[p][1])+\
            '/reconstruction_eval_assimilated_proxy_summary.pckl'
        fname_verif = datadir_input+'/'+nexp+'/'+'verifProxy_'+str(verif_period[p][0])+'to'+str(verif_period[p][1])+\
            '/reconstruction_eval_withheld_proxy_summary.pckl'

        infile_assim   = open(fname_assim,'rb')
        assim_dict[p] = pickle.load(infile_assim)
        infile_assim.close()

        if os.path.isfile(fname_verif):
            infile_verif   = open(fname_verif,'rb')
            verif_dict[p] = pickle.load(infile_verif)
            infile_verif.close()
            verif_data = True
        else:
            verif_data = False

    # get list of all proxy types in the assimilated/withheld data
    lst = []
    for p in range(nbperiods):
        a_sites = list(assim_dict[p].keys())
        lst = lst + list(set([item[0] for item in a_sites]))
        if verif_data:
            v_sites = list(verif_dict[p].keys())
            lst = lst + list(set([item[0] for item in v_sites]))
    master_proxy_types = list(set([item for item in lst]))
    master_proxy_types.insert(0,'All')
    
    # ==================
    # Now creating plots
    # ==================

    if datadir_output != '.':
        figdir = datadir_output+'/VerifFigs'
        if not os.path.isdir(figdir):
            os.system('mkdir %s' % figdir)
    else:
        figdir = '.'

    # ============================================================================================================
    # 1) Histograms of (recon, proxy) CORRELATION, CE across grand ensemble for all proxy types and per proxy type
    # ============================================================================================================

    if make_plots_hist:

        # loop over proxy types
        for proxy in master_proxy_types:

            print('Proxies: %s' %proxy)

            fig = plt.figure(figsize=(12,8))

            irow = 1
            for v in list(vtype.keys()): # "assim" & "verif" proxies

                if v == 'verif' and not verif_data:
                    break

                ax_master = fig.add_subplot(2,1,irow)
                # Turn off axis lines and ticks of the big subplot
                ax_master.tick_params(labelcolor=(1.,1.,1., 0.0), top='off', bottom='off', left='off', right='off')
                # Removes the white frame
                ax_master._frameon = False
                ax_master.set_title("%s\n" % vtype[v], fontsize=16, fontweight='bold')

                facecolor = fcolor[0]
                if v == 'assim':
                    pos = [1,2,3]
                else:
                    pos = [4,5,6]

                bins_corr = np.arange(-1.-binwidth/2, 1.+binwidth/2, binwidth)
                bins_ce   = np.arange(-2.-binwidth/2, 1.+binwidth/2, binwidth)

                # 1) --- Correlation ---
                ax = fig.add_subplot(2,3,pos[0])

                mean_stat = np.zeros([nbperiods])
                std_stat = np.zeros([nbperiods])
                prior_tmp = []
                stat_comp = []
                for p in range(nbperiods):
                    # pick right dict and associate to "workdict"
                    dname = v+'_dict'
                    workdict = eval(dname)
                    sitetag = list(workdict[p].keys())
                    if proxy == 'All':
                        proxy_types = list(set([item[0] for item in sitetag]))
                    else:
                        proxy_types = proxy

                    tmp = [workdict[p][k]['MCensCorr'] for k in sitetag if k[0] in proxy_types and np.abs(workdict[p][k]['PSMinfo']['corr'])>=r_crit]
                    stat = [item for sublist in tmp for item in sublist] # flatten list of lists
                    nbdata = len(stat)
                    mean_stat[p] = np.mean(stat)
                    std_stat[p] = np.std(stat)
                    results, edges = np.histogram(stat, bins=bins_corr, normed=True)
                    plt.bar(edges[:-1]+binwidth/2,results,binwidth,color=fcolor[p],alpha=alpha,linewidth=0,align="center")

                    #  kernel density estimation
                    statv = np.asarray(stat)
                    kde = sm.nonparametric.KDEUnivariate(statv)
                    nbpts, = statv.shape
                    if nbpts > 0:
                        kde.fit(kernel='gau')
                        plt.plot(kde.support,kde.density,color=fcolor[p],lw=2,label=str(verif_period[p][0])+' to '+str(verif_period[p][1]))
                        stat_comp.append(stat)

                    # Accumulate prior stat
                    tmp = [workdict[p][k]['PriorMCensCorr'] for k in sitetag if k[0] in proxy_types and np.abs(workdict[p][k]['PSMinfo']['corr'])>=r_crit]
                    prior_tmp.append([item for sublist in tmp for item in sublist]) # flatten list of lists


                # Kolmogorov-Smirnov significance testing of difference between distributions from both tested periods
                nbdist = len(stat_comp)
                if nbdist > 1:
                    dist_test = stats.ks_2samp(stat_comp[0],stat_comp[1])
                    #print('Corr: %f %f' %(dist_test.statistic, dist_test.pvalue))

                xmind,xmaxd,ymind,ymaxd = plt.axis()

                prior_corr = [item for sublist in prior_tmp for item in sublist]
                results, edges = np.histogram(prior_corr, bins=bins_corr, normed=True)
                plt.plot(edges[:-1]+binwidth,results,linewidth=1,ls='steps',color='black',label='Prior')

                plt.xlabel("Correlation",fontweight='bold')
                plt.ylabel("Probability density",fontweight='bold')

                ymin = 0.0
                #ymax = 0.04; nbins = 4
                #ymax = 0.05; nbins = 5 # for r_crit = 0.2
                #ymax = 0.1; nbins = 5
                #ymax = 2.0; nbins = 5
                if proxy == 'All':
                    ymax = 2.0; nbins = 5
                else:
                    ymax = ymaxd

                plt.axis((CORRrange[0],CORRrange[1],ymin,ymax))
                plt.locator_params(axis = 'y', nbins = nbins)
                plt.legend(loc=2,fontsize=9,frameon=False,handlelength=1.2)

                xmin,xmax,ymin,ymax = plt.axis()
                xpos = xmin+0.025*(xmax-xmin)
                ypos = ymin+0.5*(ymax-ymin)
                for p in range(nbperiods):
                    plt.text(xpos,ypos,'Mean = %s' %"{:.2f}".format(mean_stat[p]),fontsize=10,fontweight='bold',color=fcolor[p])
                    ypos = ypos-0.075*(ymax-ymin)
                if nbdist > 1:
                    plt.text(xpos,ypos,'  p-value = %s' %"{:.3f}".format(dist_test.pvalue),fontsize=9,fontweight='bold')
                    ypos = ypos-0.075*(ymax-ymin)
                plt.text(xpos,ypos,'Mean = %s' %"{:.2f}".format(np.mean(prior_corr)),fontsize=10,fontweight='bold')


                # 2) --- CE ---
                ax = fig.add_subplot(2,3,pos[1])

                mean_stat = np.zeros([nbperiods])
                std_stat = np.zeros([nbperiods])
                prior_tmp = []
                stat_comp = []
                for p in range(nbperiods):
                    # pick right dict and associate to "workdict"
                    dname = v+'_dict'
                    workdict = eval(dname)
                    sitetag = list(workdict[p].keys())
                    if proxy == 'All':
                        proxy_types = list(set([item[0] for item in sitetag]))
                    else:
                        proxy_types = proxy

                    tmp = [workdict[p][k]['MCensCE'] for k in sitetag if k[0] in proxy_types and np.abs(workdict[p][k]['PSMinfo']['corr'])>=r_crit]
                    stat = [item for sublist in tmp for item in sublist] # flatten list of lists
                    nbdata = len(stat)
                    mean_stat[p] = np.mean(stat)
                    std_stat[p] = np.std(stat)
                    # Since CE is not bounded at the lower end, assign values smaller than 1st bin to value of 1st bin
                    #stat = [bins[0] if x<bins[0] else x for x in stat]
                    results, edges = np.histogram(stat, bins=bins_ce, normed=True)
                    plt.bar(edges[:-1],results,binwidth,color=fcolor[p],alpha=alpha,linewidth=0)

                    #  kernel density estimation
                    statv = np.asarray(stat)
                    kde = sm.nonparametric.KDEUnivariate(statv)
                    nbpts, = statv.shape
                    if nbpts > 0:
                        kde.fit(kernel='gau')
                        plt.plot(kde.support,kde.density,color=fcolor[p],lw=2,label=str(verif_period[p][0])+' to '+str(verif_period[p][1]))
                        stat_comp.append(stat)

                    # Accumulate prior stat
                    tmp = [workdict[p][k]['PriorMCensCE'] for k in sitetag if k[0] in proxy_types and np.abs(workdict[p][k]['PSMinfo']['corr'])>=r_crit]
                    prior_tmp.append([item for sublist in tmp for item in sublist]) # flatten list of lists


                # Kolmogorov-Smirnov significance testing of difference between distributions from both tested periods
                nbdist = len(stat_comp)
                if nbdist > 1:
                    dist_test = stats.ks_2samp(stat_comp[0],stat_comp[1])
                    #print('CE: %f %f' %(dist_test.statistic, dist_test.pvalue))


                prior_ce = [item for sublist in prior_tmp for item in sublist]
                # Since CE is not bounded at the lower end, assign values smaller than 1st bin to value of 1st bin
                prior_ce = [bins_ce[0] if x<bins_ce[0] else x for x in prior_ce]
                results, edges = np.histogram(prior_ce, bins=bins_ce, normed=True)
                plt.plot(edges[:-1]+binwidth,results,linewidth=1,ls='steps',color='black',label='Prior')

                plt.xlabel("Coefficient of efficiency",fontweight='bold')
                plt.ylabel("Probability density",fontweight='bold')
                xmin,xmax,ymin,ymax = plt.axis()
                ymin = 0.0
                #ymax = 0.45
                #ymax = 0.1 # for r_crit = 0.2
                #ymax = 0.5; nbins = 5
                ymax = 12.0; nbins = 6

                plt.axis((CErange[0],CErange[1],ymin,ymax))
                plt.legend(loc=2,fontsize=9,frameon=False,handlelength=1.2)

                xmin,xmax,ymin,ymax = plt.axis()
                xpos = xmin+0.025*(xmax-xmin)
                ypos = ymin+0.5*(ymax-ymin)
                for p in range(nbperiods):
                    plt.text(xpos,ypos,'Mean = %s' %"{:.2f}".format(mean_stat[p]),fontsize=10,fontweight='bold',color=fcolor[p])
                    ypos = ypos-0.075*(ymax-ymin)
                if nbdist > 1:
                    plt.text(xpos,ypos,'  p-value = %s' %"{:.3f}".format(dist_test.pvalue),fontsize=9,fontweight='bold')
                    ypos = ypos-0.075*(ymax-ymin)
                plt.text(xpos,ypos,'Mean = %s' %"{:.2f}".format(np.mean(prior_ce)),fontsize=10,fontweight='bold')


                # 3) --- Change in CE from prior to posterior ---
                ax = fig.add_subplot(2,3,pos[2])

                prior_tmp = []
                stat_comp = []
                for p in range(nbperiods):
                    # pick right dict and associate to "workdict"
                    dname = v+'_dict'
                    workdict = eval(dname)
                    sitetag = list(workdict[p].keys())
                    if proxy == 'All':
                        proxy_types = list(set([item[0] for item in sitetag]))
                    else:
                        proxy_types = proxy

                    tmpPost  = [workdict[p][k]['MCensCE'] for k in sitetag if k[0] in proxy_types and np.abs(workdict[p][k]['PSMinfo']['corr'])>=r_crit]
                    tmpPrior = [workdict[p][k]['PriorMCensCE'] for k in sitetag if k[0] in proxy_types and np.abs(workdict[p][k]['PSMinfo']['corr'])>=r_crit]
                    statPost  = [item for sublist in tmpPost for item in sublist]  # flatten list of lists
                    statPrior = [item for sublist in tmpPrior for item in sublist] # flatten list of lists

                    # difference
                    stat = [statPost[i]-statPrior[i] for i in range(len(statPost))]
                    nbdata = len(stat)
                    mean_stat = np.mean(stat)
                    std_stat = np.std(stat)
                    # % of positive change
                    dCEplus = [stat[i] for i in range(len(stat)) if stat[i] > 0.0]
                    if nbdata > 0:
                        frac = int(float(len(dCEplus))/float(len(stat))*100.)
                        fractiondCEplus = str(int('%d' % frac ))
                    else:
                        fractiondCEplus = 'n/a'

                    print('CE_stats: period= ', str('%12s' %verif_period[p]), ' category= ', v, ':', str('%8s' %str(len(dCEplus))), str('%8s' %str(len(stat))), \
                        ' Fraction of +change:', fractiondCEplus, '%')

                    results, edges = np.histogram(stat, bins=bins_ce, normed=True)
                    leg = str(verif_period[p][0])+' to '+str(verif_period[p][1])+' : % +change='+str(fractiondCEplus)
                    plt.bar(edges[:-1],results,binwidth,color=fcolor[p],alpha=alpha,linewidth=0)

                    #  kernel density estimation
                    statv = np.asarray(stat)
                    kde = sm.nonparametric.KDEUnivariate(statv)
                    nbpts, = statv.shape
                    if nbpts > 0:
                        kde.fit(kernel='gau')
                        plt.plot(kde.support,kde.density,color=fcolor[p],lw=2,label=leg)
                        stat_comp.append(stat)

                # Kolmogorov-Smirnov significance testing of difference between distributions from both periods
                nbdist = len(stat_comp)
                if nbdist > 1:
                    dist_test = stats.ks_2samp(stat_comp[0],stat_comp[1])
                    #print('deltaCE: %f %f' %(dist_test.statistic, dist_test.pvalue))


                plt.xlabel("Change in coefficient of efficiency",fontweight='bold')
                plt.ylabel("Probability density",fontweight='bold')
                xmin,xmax,ymin,ymax = plt.axis()
                ymin = 0.0
                ymax = 8.0; nbins = 5

                plt.axis((CEchangerange[0],CEchangerange[1],ymin,ymax))
                plt.legend(loc=2,fontsize=9,frameon=False,handlelength=1.2)

                if nbdist > 1:
                    xmin,xmax,ymin,ymax = plt.axis()
                    xpos = xmin+0.025*(xmax-xmin)
                    ypos = ymin+0.75*(ymax-ymin)
                    plt.text(xpos,ypos,'  p-value = %s' %"{:.3f}".format(dist_test.pvalue),fontsize=9,fontweight='bold')

                irow = irow + 1

            fig.tight_layout()

            if proxy == 'All':
                proxy_tag = 'Allproxies'
            else:
                proxy_tag = proxy.replace(' ','_')
            plt.savefig('%s/%s_verify_proxy_hist_corr_ce_%s.png' % (figdir,nexp,proxy_tag),bbox_inches='tight')
            if make_pdfs:
                plt.savefig('%s/%s_verify_proxy_hist_corr_ce_%s.pdf' % (figdir,nexp,proxy_tag),bbox_inches='tight',dpi=300, format='pdf')
            plt.close()

    # ==========================================================================
    # PART 2: MAPS of site-based verification metrics --------------------------
    # ==========================================================================
    if make_plots_maps:


        #water = '#9DD4F0'
        #continents = '#888888'
        water = '#D3ECF8'
        continents = '#F2F2F2'

        # Loop over proxy sets (assim vs verif)
        for v in list(vtype.keys()):

            # Loop over verification periods
            for p in range(nbperiods):
                # pick right dict and associate to "workdict"
                dname = v+'_dict'
                workdict = eval(dname)
                sites = list(workdict[p].keys())
                proxy_types = list(set([item[0] for item in sitetag]))

                verif_period_label = str(verif_period[p][0])+'-'+str(verif_period[p][1])

                proxy_types = []
                for sitetag in sites:
                    sitetype = sitetag[0]
                    if sitetype not in proxy_types:
                        proxy_types.append(sitetype)
                proxy_types = sorted(proxy_types)
                m = Basemap(projection='robin', lat_0=0, lon_0=0,resolution='l', area_thresh=700.0); latres = 20.; lonres=40.   # GLOBAL
                x, y = m(0.,0.)
                l = []
                for sitetype in sorted(proxy_types):
                    l.append(m.scatter(x,y,35,c='white',marker=proxy_verif[sitetype],edgecolor='black',linewidth='1'))

                # ===========================================================================
                # 2) Maps with proxy sites plotted with dots colored according to correlation
                # ===========================================================================

                verif_metric = 'Correlation'

                mapcolor = plt.cm.seismic
                cbarfmt = '%4.1f'

                fmin = -1.0; fmax = 1.0
                fval = np.linspace(fmin, fmax, 100);  fvalc = np.linspace(0, fmax, 101);
                scaled_colors = mapcolor(fvalc)
                cmap, norm = from_levels_and_colors(levels=fval, colors=scaled_colors, extend='both')
                cbarticks=np.linspace(fmin,fmax,11)

                fig = plt.figure(figsize=[8,5])
                #ax  = fig.add_axes([0.1,0.1,0.8,0.8])

                m = Basemap(projection='robin', lat_0=0, lon_0=0,resolution='l', area_thresh=700.0); latres = 20.; lonres=40.   # GLOBAL

                m.drawmapboundary(fill_color=water)
                m.drawcoastlines(linewidth=0.5); m.drawcountries(linewidth=0.5)
                m.fillcontinents(color=continents,lake_color=water)
                m.drawparallels(np.arange(-80.,81.,latres),linewidth=0.5)
                m.drawmeridians(np.arange(-180.,181.,lonres),linewidth=0.5)

                # loop over proxy sites
                for sitetag in sites:
                    sitetype = sitetag[0]
                    sitename = sitetag[1]
                    sitemarker = proxy_verif[sitetype]

                    lat = workdict[p][sitetag]['lat']
                    lon = workdict[p][sitetag]['lon']
                    x, y = m(lon,lat)
                    Gplt = m.scatter(x,y,35,c=workdict[p][sitetag]['MeanCorr'],marker=sitemarker,edgecolor='black',linewidth='1',zorder=4,cmap=cmap,norm=norm)

                cbar = m.colorbar(Gplt,location='right',pad="2%",size="2%",ticks=cbarticks,format=cbarfmt,extend='both')
                cbar.outline.set_linewidth(1.0)
                cbar.set_label('%s' % verif_metric,size=11,weight='bold')
                cbar.ax.tick_params(labelsize=10)
                plt.title('Period: '+verif_period_label+' : '+vtype[v],fontweight='bold')
                plt.legend(l,proxy_types,
                           scatterpoints=1,
                           loc='lower center', bbox_to_anchor=(0.5, -0.30),
                           ncol=3,
                           fontsize=9)

                plt.savefig('%s/%s_verify_proxy_map_%s_corr_%s.png' % (figdir,nexp,v,verif_period_label),bbox_inches='tight')
                if make_pdfs:
                    plt.savefig('%s/%s_verify_proxy_map_%s_corr_%s.pdf' % (figdir,nexp,v,verif_period_label),bbox_inches='tight', dpi=300, format='pdf')
                plt.close()


                # ===========================================================================
                # 3) Maps with proxy sites plotted with dots colored according to CE
                # ===========================================================================

                verif_metric = 'Coefficient of efficiency'

                mapcolor = plt.cm.seismic
                cbarfmt = '%4.1f'

                fmin = -1.0; fmax = 1.0
                fval = np.linspace(fmin, fmax, 100);  fvalc = np.linspace(0, fmax, 101);
                scaled_colors = mapcolor(fvalc)
                cmap, norm = from_levels_and_colors(levels=fval, colors=scaled_colors, extend='both')
                cbarticks=np.linspace(fmin,fmax,11)

                # Prior & Posterior
                fig = plt.figure(figsize=[8,10])

                dplot = {'Prior':'PriorMeanCE', 'Posterior':'MeanCE'}
                irow = 1
                for dd in list(dplot.keys()):

                    ax = fig.add_subplot(2,1,irow)
                    m = Basemap(projection='robin', lat_0=0, lon_0=0,resolution='l', area_thresh=700.0); latres = 20.; lonres=40.  # GLOBAL
                    m.drawmapboundary(fill_color=water)
                    m.drawcoastlines(linewidth=0.5); m.drawcountries(linewidth=0.5)
                    m.fillcontinents(color=continents,lake_color=water)
                    m.drawparallels(np.arange(-80.,81.,latres),linewidth=0.5)
                    m.drawmeridians(np.arange(-180.,181.,lonres),linewidth=0.5)

                    # loop over proxy sites
                    for sitetag in sites:
                        sitetype = sitetag[0]
                        sitename = sitetag[1]
                        sitemarker = proxy_verif[sitetype]

                        lat = workdict[p][sitetag]['lat']
                        lon = workdict[p][sitetag]['lon']
                        x, y = m(lon,lat)
                        plot_var = dplot[dd]
                        Gplt = m.scatter(x,y,35,c=workdict[p][sitetag][plot_var],marker=sitemarker,edgecolor='black',linewidth='1',zorder=4,cmap=cmap,norm=norm)

                    cbar = m.colorbar(Gplt,location='right',pad="2%",size="2%",ticks=cbarticks,format=cbarfmt,extend='both')
                    cbar.outline.set_linewidth(1.0)
                    cbar.set_label('%s' % verif_metric,size=11,weight='bold')
                    cbar.ax.tick_params(labelsize=10)
                    if irow == 1:
                        plt.title('Period: '+verif_period_label+'\n\n'+vtype[v]+' : '+ dd,fontweight='bold')
                    else:
                        plt.title(vtype[v]+' : '+ dd,fontweight='bold')

                    irow = irow + 1

                plt.legend(l,proxy_types,
                           scatterpoints=1,
                           loc='lower center', bbox_to_anchor=(0.5, -0.30),
                           ncol=3,
                           fontsize=9)

                fig.tight_layout()
                plt.savefig('%s/%s_verify_proxy_map_%s_ce_%s.png' % (figdir,nexp,v,verif_period_label),bbox_inches='tight')
                if make_pdfs:
                    plt.savefig('%s/%s_verify_proxy_map_%s_ce_%s.pdf' % (figdir,nexp,v,verif_period_label),bbox_inches='tight', dpi=300, format='pdf')
                plt.close()

                # ============================================================================
                # 4) Maps with proxy sites plotted with dots colored according to change in CE
                # ============================================================================

                # Change in CE from Prior to Posterior
                fig = plt.figure(figsize=[8,5])

                m = Basemap(projection='robin', lat_0=0, lon_0=0,resolution='l', area_thresh=700.0); latres = 20.; lonres=40.   # GLOBAL
                m.drawmapboundary(fill_color=water)
                m.drawcoastlines(linewidth=0.5); m.drawcountries(linewidth=0.5)
                m.fillcontinents(color=continents,lake_color=water)
                m.drawparallels(np.arange(-80.,81.,latres),linewidth=0.5)
                m.drawmeridians(np.arange(-180.,181.,lonres),linewidth=0.5)

                # loop over proxy sites
                for sitetag in sites:
                    sitetype = sitetag[0]
                    sitename = sitetag[1]
                    sitemarker = proxy_verif[sitetype]

                    lat = workdict[p][sitetag]['lat']
                    lon = workdict[p][sitetag]['lon']
                    x, y = m(lon,lat)
                    plot_var = workdict[p][sitetag]['MeanCE'] - workdict[p][sitetag]['PriorMeanCE']
                    Gplt = m.scatter(x,y,35,c=plot_var,marker=sitemarker,edgecolor='black',linewidth='1',zorder=4,cmap=cmap,norm=norm)
                cbar = m.colorbar(Gplt,location='right',pad="2%",size="2%",ticks=cbarticks,format=cbarfmt,extend='both')
                cbar.outline.set_linewidth(1.0)
                cbar.set_label('Change in coefficient of efficiency',size=11,weight='bold')
                cbar.ax.tick_params(labelsize=10)
                plt.title('Period: '+verif_period_label+' : '+vtype[v],fontweight='bold')
                plt.legend(l,proxy_types,
                           scatterpoints=1,
                           loc='lower center', bbox_to_anchor=(0.5, -0.30),
                           ncol=3,
                           fontsize=9)

                fig.tight_layout()
                plt.savefig('%s/%s_verify_proxy_map_%s_delta_ce_%s.png' % (figdir,nexp,v,verif_period_label),bbox_inches='tight')
                if make_pdfs:
                    plt.savefig('%s/%s_verify_proxy_map_%s_delta_ce_%s.pdf' % (figdir,nexp,v,verif_period_label),bbox_inches='tight', dpi=300, format='pdf')
                plt.close()


                # ==========================================================================================
                # 5) Maps with proxy sites plotted with dots colored according to ensemble calibration ratio
                # ==========================================================================================

                verif_metric = 'Ensemble calibration'

                mapcolor = plt.cm.seismic
                cbarfmt = '%4.1f'

                fmin = 0.0; fmax = 2.0
                fval = np.linspace(fmin, fmax, 100);  fvalc = np.linspace(0, 1, 100);
                scaled_colors = mapcolor(fvalc)
                cmap, norm = from_levels_and_colors(levels=fval, colors=scaled_colors, extend='max')
                cbarticks=np.linspace(fmin,fmax,11)

                # Prior & Posterior
                fig = plt.figure(figsize=[8,10])

                dplot = {'Prior':'PriorMeanCalRatio', 'Posterior':'MeanCalRatio'}
                irow = 1
                for dd in list(dplot.keys()):

                    ax = fig.add_subplot(2,1,irow)
                    m = Basemap(projection='robin', lat_0=0, lon_0=0,resolution='l', area_thresh=700.0); latres = 20.; lonres=40.  # GLOBAL
                    m.drawmapboundary(fill_color=water)
                    m.drawcoastlines(linewidth=0.5); m.drawcountries(linewidth=0.5)
                    m.fillcontinents(color=continents,lake_color=water)
                    m.drawparallels(np.arange(-80.,81.,latres),linewidth=0.5)
                    m.drawmeridians(np.arange(-180.,181.,lonres),linewidth=0.5)

                    # loop over proxy sites
                    for sitetag in sites:
                        sitetype = sitetag[0]
                        sitename = sitetag[1]
                        sitemarker = proxy_verif[sitetype]

                        lat = workdict[p][sitetag]['lat']
                        lon = workdict[p][sitetag]['lon']
                        x, y = m(lon,lat)
                        plot_var = dplot[dd]
                        Gplt = m.scatter(x,y,35,c=workdict[p][sitetag][plot_var],marker=sitemarker,edgecolor='black',linewidth='1',zorder=4,cmap=cmap,norm=norm)

                    cbar = m.colorbar(Gplt,location='right',pad="2%",size="2%",ticks=cbarticks,format=cbarfmt,extend='max')
                    cbar.outline.set_linewidth(1.0)
                    cbar.set_label('%s' % verif_metric,size=11,weight='bold')
                    cbar.ax.tick_params(labelsize=10)
                    if irow == 1:
                        plt.title('Period: '+verif_period_label+'\n\n'+vtype[v]+' : '+ dd,fontweight='bold')
                    else:
                        plt.title(vtype[v]+' : '+ dd,fontweight='bold')

                    irow = irow + 1

                plt.legend(l,proxy_types,
                           scatterpoints=1,
                           loc='lower center', bbox_to_anchor=(0.5, -0.30),
                           ncol=3,
                           fontsize=9)

                fig.tight_layout()
                plt.savefig('%s/%s_verify_proxy_map_%s_EnsCal_%s.png' % (figdir,nexp,v,verif_period_label),bbox_inches='tight')
                if make_pdfs:
                    plt.savefig('%s/%s_verify_proxy_map_%s_EnsCal_%s.pdf' % (figdir,nexp,v,verif_period_label),bbox_inches='tight', dpi=300, format='pdf')
                plt.close()



    # ==========================================================================
    # PART 3: Plots of individual time series of proxy records  ----------------
    # ==========================================================================
    if make_plots_individual_sites:

        assimilated_sites = sorted(assim_dict[p].keys())
        withheld_sites = verif_dict[p].keys()

        if nbperiods > 1:
            period_bnds = [item[0] for item in verif_period]
        else:
            period_bnds = verif_period[0]

        # Loop over sites
        siteCount = 0
        for site in assimilated_sites:

            sitename = site[0].replace(' ','_')+'_'+site[1].replace(' ','_')

            AssimPriorR  = np.zeros([nbperiods],dtype=float)
            AssimPriorCE = np.zeros([nbperiods],dtype=float)
            AssimReconR  = np.zeros([nbperiods],dtype=float)
            AssimReconCE = np.zeros([nbperiods],dtype=float)

            VerifPriorR  = np.zeros([nbperiods],dtype=float)
            VerifPriorCE = np.zeros([nbperiods],dtype=float)
            VerifReconR  = np.zeros([nbperiods],dtype=float)
            VerifReconCE = np.zeros([nbperiods],dtype=float)

            # setup the figure
            fig, ax = plt.subplots(2,1, figsize=(10,6))
            plt.subplots_adjust(bottom=0.35,hspace=0.4)

            # loop over periods
            for p in range(nbperiods):

                # assimilated --
                try:
                    assimCE =  '{:7.2f}'.format(np.mean(assim_dict[p][site]['MCensCE']))
                except KeyError:
                    assimCE = 'n/a'

                # withheld --
                try:
                    verifCE = '{:7.2f}'.format(np.mean(verif_dict[p][site]['MCensCE']))
                except KeyError:
                    verifCE = 'n/a'


                print('{:120}'.format(sitename), '{:12}'.format(str(verif_period[p])), '{:7}'.format(str(assimCE)), '{:7}'.format(str(verifCE)))


                ts_years_assim = assim_dict[p][site]['ts_years']
                ts_obs_assim   = assim_dict[p][site]['ts_ProxyValues']

                ts_prior_m_assim = assim_dict[p][site]['ts_MeanPrior']
                ts_prior_v_assim = assim_dict[p][site]['ts_SpreadPrior']

                ts_recon_m_assim = assim_dict[p][site]['ts_MeanRecon']
                ts_recon_v_assim = assim_dict[p][site]['ts_SpreadRecon']

                ts_years_plot = np.linspace(np.min(ts_years_assim),np.max(ts_years_assim),(np.max(ts_years_assim)-np.min(ts_years_assim))+1)
                ts_prior_m_plot = np.zeros(ts_years_plot.shape); ts_prior_m_plot[:] = np.nan
                ts_recon_m_plot = np.zeros(ts_years_plot.shape); ts_recon_m_plot[:] = np.nan

                ts_prior_m_plot[np.in1d(ts_years_plot,ts_years_assim)] = ts_prior_m_assim
                ts_recon_m_plot[np.in1d(ts_years_plot,ts_years_assim)] = ts_recon_m_assim


                AssimPriorR[p]  = '{:.2f}'.format(np.mean(assim_dict[p][site]['PriorMCensCorr']))
                AssimReconR[p]  = '{:.2f}'.format(np.mean(assim_dict[p][site]['MCensCorr']))
                AssimPriorCE[p] = '{:.2f}'.format(np.mean(assim_dict[p][site]['PriorMCensCE']))
                AssimReconCE[p] = '{:.2f}'.format(np.mean(assim_dict[p][site]['MCensCE']))


                # make the upper subplot
                ax[0].plot(ts_years_assim,ts_obs_assim, '.', color='#5CB8E6',  label='Proxy values')
                ax[0].plot(ts_years_plot,ts_prior_m_plot,'-k',lw=3,alpha=0.5,  label='Prior')
                ax[0].plot(ts_years_plot,ts_recon_m_plot,'-r',lw=2,alpha=0.75, label='Analysis')

                stitle = str(site)+'\nAssimilated'
                ax[0].set_title(stitle,fontsize=10)
                ax[0].set_ylabel('Proxy value')

                xmin,xmax,ymin,ymax = ax[0].axis()
                if nbperiods > 1:
                    for bndy in period_bnds:
                        if bndy > xmin:
                            ax[0].plot([bndy,bndy],[ymin,ymax],':k',lw=2)

                # ---

                # records are not always in the "withheld" (verif) set
                try:
                    ts_years_verif = verif_dict[p][site]['ts_years']
                    ts_obs_verif   = verif_dict[p][site]['ts_ProxyValues']

                    ts_prior_m_verif = verif_dict[p][site]['ts_MeanPrior']
                    ts_prior_v_verif = verif_dict[p][site]['ts_SpreadPrior']

                    ts_recon_m_verif = verif_dict[p][site]['ts_MeanRecon']
                    ts_recon_v_verif = verif_dict[p][site]['ts_SpreadRecon']

                    VerifPriorR[p]  = '{:.2f}'.format(np.mean(verif_dict[p][site]['PriorMCensCorr']))
                    VerifReconR[p]  = '{:.2f}'.format(np.mean(verif_dict[p][site]['MCensCorr']))
                    VerifPriorCE[p] = '{:.2f}'.format(np.mean(verif_dict[p][site]['PriorMCensCE']))
                    VerifReconCE[p] = '{:.2f}'.format(np.mean(verif_dict[p][site]['MCensCE']))

                except KeyError:
                    ts_years_verif = assim_dict[p][site]['ts_years']
                    ts_obs_verif   = assim_dict[p][site]['ts_ProxyValues']

                    ts_prior_m_verif = np.zeros(ts_years_verif.shape); ts_prior_m_verif[:] = np.nan
                    ts_prior_v_verif = np.zeros(ts_years_verif.shape); ts_prior_v_verif[:] = np.nan

                    ts_recon_m_verif = np.zeros(ts_years_verif.shape); ts_recon_m_verif[:] = np.nan
                    ts_recon_v_verif = np.zeros(ts_years_verif.shape); ts_recon_m_verif[:] = np.nan

                    VerifPriorR[p]  = '{:.2f}'.format(np.nan)
                    VerifReconR[p]  = '{:.2f}'.format(np.nan)
                    VerifPriorCE[p] = '{:.2f}'.format(np.nan)
                    VerifReconCE[p] = '{:.2f}'.format(np.nan)


                ts_years_plot = np.linspace(np.min(ts_years_verif),np.max(ts_years_verif),(np.max(ts_years_verif)-np.min(ts_years_verif))+1)
                ts_prior_m_plot = np.zeros(ts_years_plot.shape); ts_prior_m_plot[:] = np.nan
                ts_recon_m_plot = np.zeros(ts_years_plot.shape); ts_recon_m_plot[:] = np.nan

                ts_prior_m_plot[np.in1d(ts_years_plot,ts_years_verif)] = ts_prior_m_verif
                ts_recon_m_plot[np.in1d(ts_years_plot,ts_years_verif)] = ts_recon_m_verif


                # make the lower subplot
                if p == 0:
                    ax[1].plot(ts_years_verif,ts_obs_verif, '.', color='#5CB8E6',label='Proxy data')
                    ax[1].plot(ts_years_plot,ts_prior_m_plot,'-k',lw=3,alpha=0.5,label='Prior')
                    ax[1].plot(ts_years_plot,ts_recon_m_plot,'-r',lw=2,alpha=0.75,label='Analysis')
                else:
                    ax[1].plot(ts_years_verif,ts_obs_verif, '.', color='#5CB8E6')
                    ax[1].plot(ts_years_plot,ts_prior_m_plot,'-k',lw=3,alpha=0.5)
                    ax[1].plot(ts_years_plot,ts_recon_m_plot,'-r',lw=2,alpha=0.75)

                ax[1].set_title('Withheld',fontsize=10)
                ax[1].set_ylabel('Proxy value')
                ax[1].set_xlabel('Years (CE)')

                ax[1].legend(loc='upper left',bbox_to_anchor=(.85,-.2),handlelength=.2,fontsize=10)

                xmin,xmax,ymin,ymax = ax[1].axis()
                if nbperiods > 1:
                    for bndy in period_bnds:
                        if bndy > xmin:
                            ax[1].plot([bndy,bndy],[ymin,ymax],':k',lw=2)


            # --- Annotate with summary stats in table format ---

            plt.text(.025,-.62, 'Summary\nstatistics',transform=ax[1].transAxes,fontsize=10)

            header_col_labels = verif_period

            header = plt.table(cellText=[[' ']*nbperiods],colWidths=[0.2]*nbperiods, colLabels=header_col_labels,
                               loc='bottom',bbox=[0.15,-0.65,0.2*nbperiods,0.25])
            cellDict = header.get_celld()
            cells = cellDict.keys()
            for c in cells: cellDict[c].set_width(0.2)

            row_labels = ['Prior R', 'Posterior R', 'Prior CE', 'Posterior CE']
            indPrior = [i+1 for i,s in enumerate(row_labels) if 'Prior' in s]
            indPost = [i+1 for i,s in enumerate(row_labels) if 'Posterior' in s]

            categories = ['Assimilated', 'Withheld']
            col_labels = []
            for i in range(nbperiods):
                col_labels.extend(categories)


            table_vals = np.zeros([4,nbperiods*2])
            for i in range(nbperiods):
                j = i*2

                table_vals[0,j]   = AssimPriorR[i]
                table_vals[0,j+1] = VerifPriorR[i]

                table_vals[1,j]   = AssimReconR[i]
                table_vals[1,j+1] = VerifReconR[i]


                table_vals[2,j]   = AssimPriorCE[i]
                table_vals[2,j+1] = VerifPriorCE[i]

                table_vals[3,j]   = AssimReconCE[i]
                table_vals[3,j+1] = VerifReconCE[i]

            the_table = plt.table(cellText=table_vals,colWidths=[0.1]*5,
                                  rowLabels=row_labels,
                                  colLabels=col_labels,
                                  loc='bottom',bbox=[0.15,-1.29,0.2*nbperiods,0.75])

            cellDict = the_table.get_celld()
            cells = cellDict.keys()
            for c in cells: cellDict[c].set_width(0.0999)

            cellsPrior = [item for item in cells if item[0] in indPrior]
            for c in cellsPrior: cellDict[c].set_facecolor('lightgray')
            cellsPost = [item for item in cells if item[0] in indPost]
            for c in cellsPost: cellDict[c].set_facecolor('darkgray')

            figname = figdir+'/'+nexp+'_verify_proxy_'+sitename
            plt.savefig(figname+'.png')
            plt.close()


    end_time = time() - begin_time
    print('=======================================================')
    print('All completed in %s mins' %str(end_time/60.0))
    print('=======================================================')
Ejemplo n.º 53
0
def nostradamus_rain(in_msg):
            
    if in_msg.datetime is None:
        in_msg.get_last_SEVIRI_date()

    if in_msg.end_date is None:
        in_msg.end_date = in_msg.datetime
        #in_msg.end_date = in_msg.datetime + timedelta(15)
      
    delta     = timedelta(minutes=15) 

    # automatic choise of the FULL DISK SERVICE Meteosat satellite
    if in_msg.datetime <  datetime(2008, 5, 13, 0, 0):   # before 13.05.2008 only nominal MSG1 (meteosat8), no Rapid Scan Service yet
        sat_nr = "08" 
    elif in_msg.datetime <  datetime(2013, 2, 27, 9, 0): # 13.05.2008 ...  27.02.2013 
        sat_nr = "09"                              # MSG-2  (meteosat9) became nominal satellite, MSG-1 (meteosat8) started RSS
    elif in_msg.datetime <  datetime(2018, 3, 9, 0, 0):  # 27.02.2013 9:00UTC ... 09.03.2013                                   
        sat_nr = "10"                              # MSG-3 (meteosat10) became nominal satellite, MSG-2 started RSS (MSG1 is backup for MSG2)
    else:
        sat_nr = "11"
    print ("... work with Meteosat"+str(sat_nr))
    
    print ("")
    if in_msg.verbose:
        print ('*** Create plots for ')
        print ('    Satellite/Sensor: ' + in_msg.sat_str()) 
        print ('    Satellite number: ' + in_msg.sat_nr_str() +' // ' +str(in_msg.sat_nr))
        print ('    Satellite instrument: ' + in_msg.instrument)
        print ('    Start Date/Time:      '+ str(in_msg.datetime))
        print ('    End Date/Time:        '+ str(in_msg.datetime))
        print ('    Areas:           ', in_msg.areas)
        for area in in_msg.plots.keys():
            print ('    plots['+area+']:            ', in_msg.plots[area])
        #print ('    parallax_correction: ', in_msg.parallax_correction)
        #print ('    reader level:    ', in_msg.reader_level)
    
    ## read in all the constants files
    print('=================================')
    print('*** load the constant fields (radar mask, viewing geometry, and land/sea mask plus surface elevation)')
    global_radar_mask, global_vg, global_ls_ele = load_constant_fields(sat_nr) 
    
    ###############################################
    ## load the mlp for the precip detection (pd) #
    ###############################################

    if in_msg.model == 'mlp':
        dir_start_pd= './models/precipitation_detection/mlp/2hl_100100hu_10-7alpha_log/'
        dir_start_rr= './models/precipitation_rate/mlp/2hl_5050hu_10-2alpha_log/'

    if not in_msg.read_from_netCDF:

        clf_pd = joblib.load(dir_start_pd+'clf.pkl') 
        scaler_pd = joblib.load(dir_start_pd+'scaler.pkl')
        feature_list_pd = joblib.load(dir_start_pd+'featurelist.pkl')
        thres_pd=np.load(dir_start_pd+'opt_orig_posteriorprobab_thres.npy')

        #########################################
        ## load the mlp for the rain rates (rr) #
        #########################################

        reg_rr = joblib.load(dir_start_rr+'reg.pkl') 
        scaler_rr = joblib.load(dir_start_rr+'scaler.pkl')
        feature_list_rr = joblib.load(dir_start_rr+'featurelist.pkl')

    ####################################
    ## load the reference sets for a climatological probab matching (pm) if requested
    ####################################

    if in_msg.probab_match:
        # load in the ref data sets created with the script: rr_probab_matching_create_refset.ipynb
        ody_rr_ref=np.load(dir_start_rr+'pm_valid_data_ody_rr_ref.npy')
        pred_rr_ref=np.load(dir_start_rr+'pm_valid_data_pred_rr_ref.npy')

    # initialize processed RGBs
    plots_done={}
    
    time_slot = copy.deepcopy(in_msg.datetime)
    while time_slot <= in_msg.end_date:
        print('... processing for time: ', time_slot)

        ################################################
        ## CHOOSE THE SETUP (time_slot, area, model)
        ################################################
 
        ##########################
        ## LOAD THE NEEDED INPUTS
        ##########################

        if not in_msg.read_from_netCDF:
            ## read observations at the specific time
            print('=================================')
            print('*** load the time slot specific fields with in_msg.parallax_gapfilling:', in_msg.parallax_gapfilling)
            global_radar, global_sat, global_nwc, global_cth, global_hsaf = load_input(sat_nr, time_slot, in_msg.parallax_gapfilling, read_HSAF=in_msg.read_HSAF)
            #                                                               def load_input(sat_nr, time_slot, par_fill, read_HSAF=True):
        else:
            print('read Odyssey radar composite')
            from mpop.satellites import GeostationaryFactory
            global_radar = GeostationaryFactory.create_scene("odyssey", "", "radar", time_slot)
            global_radar.load(['RATE'])
            print(global_radar)
            print('=========================')
    
        for area in in_msg.areas:

            print ("================================")
            print ("*** PROCESSING FOR AREA: "+area)

            # declare "precipitation detection" and "rainrate dictionary", the applied model (e.g. MLP) is used as key
            pd = {}
            rr = {}
            plots_done[area]=[]
            
            if in_msg.read_from_netCDF:

                # reproject Odyssey radar mask to area of interest 
                #radar_mask = global_radar_mask.project(area, precompute=True)
                data_radar = global_radar.project(area, precompute=True)
                # radar mask to see where odyssey ground truth exists
                mask_r = data_radar['RATE-MASK'].data.data==False
                rr['ody'] = copy.deepcopy(data_radar['RATE'].data.data)
                # do not trust values below 0.3 & above 130 -> do not consider it as rain and set all values to 0 
                rr['ody'][np.logical_or(rr['ody'] < 0.3,rr['ody'] >= 130.0)] = 0.0
                print (rr['ody'].min(), rr['ody'].max(), rr['ody'].shape, type(rr['ody']))
                
                from netCDF4 import Dataset
                # read from file
                outdir_netCDF = time_slot.strftime('/data/COALITION2/database/meteosat/nostradamus_RR/%Y/%m/%d/')
                file_netCDF = time_slot.strftime('MSG_rr-'+in_msg.model+'-'+area+'_%Y%m%d%H%M.nc')
                print ("*** read precip prediction from", outdir_netCDF+"/"+file_netCDF)

                ncfile = Dataset(outdir_netCDF+"/"+file_netCDF,'r')
                rr_tmp    = ncfile.variables['rainfall_rate'][:,:]

                ### now, we read radar data directly from odyssey file 
                #rr['ody'] = ncfile.variables['rainfall_rate (odyssey)'][:,:]
                #print (rr['ody'].min(), rr['ody'].max(), rr['ody'].shape, type(rr['ody']))

                ### now, we read radar mask directly from odyssey file 
                #mask_r    = ncfile.variables['odyssey_mask'][:,:]
                #print ("... convert mask_r (1, 0) from int to bolean (True, False)")
                #mask_r = (mask_r == 1)
                
                # create fake mask_h (where rainfall is larger than 0 mm/h)
                mask_h = rr_tmp>0
                pd[in_msg.model] = rr_tmp>0
                rr_tmp = rr_tmp.flatten()
                # remove 0 entries 
                rr_tmp = rr_tmp [ rr_tmp != 0 ]

                if False:
                    import matplotlib.pyplot as plt
                    #fig = plt.figure()
                    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6,6))
                    plt.subplot(2, 1, 1)
                    plt.imshow(mask_h)
                    #plt.colorbar()
                    plt.subplot(2, 1, 2)
                    plt.imshow(mask_r)
                    #plt.colorbar()
                    fig.savefig("mask_h_mask_r_netCDF.png")
                    print("... display mask_h_mask_r_netCDF.png &")
                    #plt.show()
                    #quit()

                
            else:
                
                ## project all data to the desired projection 
                radar_mask, vg, ls_ele, data_radar, data_sat, data_nwc, data_cth, data_hsaf = \
                    project_data(area, global_radar_mask, global_vg, global_ls_ele, global_radar, global_sat, global_nwc, global_cth, global_hsaf, read_HSAF=in_msg.read_HSAF)

                ###########################################################
                ## SINGLE TIME SLOT TO CARRY OUT A FULL RAIN RATE RETRIEVAL 
                ###########################################################

                # preprocess the data 
                # mask_h: field indicating where NWCSAF products are available & thus where predictions are carried out: True if NWCSAF products available
                # mask_r: field indicating where radar products are available: True if radar product is available
                # mask_rnt: field indicating where radar product available but not trustworthy: i.e. in threshold_mask, 0<rr<0.3, rr>130 overlaid: True if radar product is NOT trustworthy
                all_data, all_data_names, mask_h, mask_r, mask_rnt, rr['ody'], rr['hsaf'], lon, lat = \
                         pd_rr_preprocess_data_single_scene( area, time_slot,
                                                             radar_mask, vg, ls_ele,
                                                             data_radar, data_sat, data_nwc, data_cth, data_hsaf,
                                                             in_msg.parallax_gapfilling, 'rr', read_HSAF=in_msg.read_HSAF)
                         #pd_rr_preprocess_data_single_scene( sat_nr, area, time_slot, 'nearest', 'rr', read_HSAF=False)

                if False:
                    import matplotlib.pyplot as plt
                    #fig = plt.figure()
                    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(6,6))
                    plt.subplot(2, 1, 1)
                    plt.imshow(mask_h)
                    #plt.colorbar()
                    plt.subplot(2, 1, 2)
                    plt.imshow(mask_r)
                    #plt.colorbar()
                    fig.savefig("mask_h_mask_r.png")
                    print("... display mask_h_mask_r.png &")
                    #plt.show()
                    #quit()

                del rr['hsaf'] # since not actually needed in this script

                # project all data to desired projection 
                # ...

                print('... predictions at ' + str(mask_h.sum())+' out of ' +str(mask_h.flatten().shape[0])+ ' points')

                ####################################
                ## precip detection
                ####################################

                # create y_pd, y_hsaf_pd, X_raw_pd 
                y_pd_vec, y_hsaf_pd_vec, X_raw, feature_list = pd_rr_create_y_yhsaf_Xraw( all_data, all_data_names, 'pd', cut_precip=False )

                del y_pd_vec, y_hsaf_pd_vec # (since not actually ever needed in this script)

                if in_msg.remove_vg==True:
                    print('... remove viewing geometry from predictors')
                    feature_list = np.append(feature_list[:6],feature_list[8:])
                    X_raw = np.hstack([X_raw[:,:6],X_raw[:,8:]])
                    print('    new X_raw.shape:', X_raw.shape)
                    feature_list

                # check features
                if np.array_equal(feature_list, feature_list_pd):
                    print('OK, input features correspond to input features required by the loaded model')
                else:
                    print('ATTENTION, input features do not correspond to input features required by the loaded model')
                    quit()

                # create X_pd
                X_pd=scaler_pd.transform(X_raw) 

                # create final precip detection fields: opera + hsaf
                pd['ody']=rr['ody']>=0.3

                # make precip detection predictions
                print ("***  make precip detection predictions")
                pd_probab = clf_pd.predict_proba(X_pd)[:,1]   # probab precip balanced classes
                pd_vec_h = pd_probab>=thres_pd  
                pd[in_msg.model] = np.zeros(lon.shape,dtype=bool)
                pd[in_msg.model][mask_h] = pd_vec_h

                ####################################
                ## rain rate on above identified precipitating pixels
                ####################################

                # reduce X_raw to the points where rain was predicted by the mlp
                X_raw= X_raw[pd_vec_h,:] 

                # check, if read features correspond to the trained model
                if np.array_equal(feature_list, feature_list_rr):
                    print('OK, input features correspond to input features required by the loaded model')
                else:
                    print('ATTENTION, input features do not correspond to input features required by the loaded model')
                    quit()

                # create X_rr
                X_rr=scaler_rr.transform(X_raw)    

                # rain rate prediction at places where precip detected by mlp
                rr_tmp=reg_rr.predict(X_rr)  
                
            # carry out a probability machting if requested    
            if in_msg.probab_match:
                print("... do probability matching for:", in_msg.model)
                pm_str = str(in_msg.model)+'_pm'
                rr_tmp_pm = probab_match_rr_refprovide(ody_rr_ref,pred_rr_ref,rr_tmp)  
                #rr[pm_str] =  np.zeros_like(lon) 
                rr[pm_str] =  np.zeros_like(rr['ody'])   # also casts the type float
                rr[pm_str][pd[in_msg.model]]=rr_tmp_pm
                print("... probability matching done for:", in_msg.model)

            # copy rainrate data to the final place
            # replace all prediction lower than precipitation detection threshold with threhold rain rate        
            rr_tmp[rr_tmp<0.3]=0.3 # correct upward all too low predictions (i.e. the ones below the precip detection threshold)
            rr[in_msg.model] = np.zeros_like(rr['ody'])
            rr[in_msg.model][pd[in_msg.model]]=rr_tmp
                
            #####################################
            ## SAVE RESULT AS NETCDF 
            #####################################
            if area in in_msg.save_netCDF and (not in_msg.read_from_netCDF):
                outdir_netCDF = time_slot.strftime(in_msg.outdir_netCDF)
                file_netCDF   = time_slot.strftime(in_msg.file_netCDF)
                file_netCDF   = file_netCDF.replace("%(area)s", area)
                file_netCDF   = file_netCDF.replace("%(model)s", in_msg.model)
                #save_RR_as_netCDF(outdir_netCDF, file_netCDF, rr[in_msg.model], save_rr_ody=True, rr_ody=rr['ody'], save_ody_mask=True, ody_mask=mask_r, zlib=True)
                save_RR_as_netCDF(outdir_netCDF, file_netCDF, rr[in_msg.model])
                
                
            #####################################
            ## SINGLE TIME SLOT TO DRAW THE MAPS 
            #####################################

            print ("*** start to create plots")
            
            ####################################
            ## plot precip detection
            ####################################
            
            if 'pdMlp' in in_msg.plots[area]:
                
                mask_rt = np.logical_and(mask_r, mask_rnt==False) # trusted radar i.e. True where I have a trustworthy radar product available

                mod_ss = [in_msg.model] + ['ody']

                # ver for verification;
                ver={}    
                for x in mod_ss:
                    ver[x]=np.zeros_like(lon) #  sat: no 
                    ver[x][pd[x]>0] = 1 # sat: yes
                    ver[x][np.logical_and(ver[x]==0,mask_rnt)] = 2 # sat: no (rad clutter)
                    ver[x][np.logical_and(ver[x]==1,mask_rnt)] = 3 # sat: yes (rad clutter)
                    ver[x][np.logical_and(mask_rt,np.logical_and(pd[x]==1,pd['ody']==1))] = 4 # hit
                    ver[x][np.logical_and(mask_rt,np.logical_and(pd[x]==1,pd['ody']==0))] = 5 # false alarm
                    ver[x][np.logical_and(mask_rt,np.logical_and(pd[x]==0,pd['ody']==0))] = 6 # correct reject
                    ver[x][np.logical_and(mask_rt,np.logical_and(pd[x]==0,pd['ody']==1))] = 7 # miss

                # define colorkey 
                v_pd=np.array([-0.5,0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5])
                cmap_pd, norm_pd = from_levels_and_colors(v_pd,
                                    colors =['darkgrey', '#984ea3','lightgrey','plum', '#377eb8', '#e41a1c','ivory','#ff7f00'],
                                    extend='neither')

                plot_precipitation_detection=False
                if plot_precipitation_detection:    

                    # single prediction plot
                    #fig,ax= plt.subplots(figsize=(20, 10))
                    #plt.rcParams.update({'font.size': 16})
                    fig,ax= plt.subplots(figsize=(10, 5))
                    plt.rcParams.update({'font.size': 8})
                    plt.rcParams.update({'mathtext.default':'regular'}) 

                    m = map_plot(axis=ax,area=area)
                    m.ax.set_title('precip detection based on sat vs opera')

                    # plot sat precip detection product against opera product
                    tick_label_pd_nr=np.array([0,1,2,3,4,5,6,7])
                    tick_label_pd=['sat: no','sat: yes','sat: no (rad unr)','sat: yes (rad unr)','hit','false alarm','correct reject','miss']


                    im=m.pcolormesh( lon, lat, ver['mlp'], cmap=cmap_pd, norm=norm_pd, latlon=True )
                    divider = make_axes_locatable(ax)
                    cax = divider.append_axes("right", size="4%", pad=0.05)
                    cbar = fig.colorbar(im, cax=cax, ticks=tick_label_pd_nr, spacing='uniform')
                    a=cbar.ax.set_yticklabels(tick_label_pd)
                    outfile= 'precip_detection_sat'+in_msg.model+'_vs_opera_%s'
                    fig.savefig((in_msg.outputDir+outfile %time_slot.strftime('%Y%m%d%H%M')), dpi=300, bbox_inches='tight')
                    print('... create figure: display ' + in_msg.outputDir+outfile %time_slot.strftime('%Y%m%d%H%M') + '.png')

                plots_done[area].append('pdMlp')
                    
            ####################################
            ## plot rain rate with matplotlib
            ####################################

            if 'rrMatplotlib' in in_msg.plots[area]:  

                # create the combi rr field
                rr['combi']=copy.deepcopy(rr[in_msg.model+'_pm'])
                rr['combi'][mask_r]=rr['ody'][mask_r]

                # determine where I have >0.3 mm/h precip on the permanent mask -> overlay end picture with a pink(?) color there
                pd_nt=np.logical_and(mask_rnt,pd['ody']>=0.3) #precip detected but not trusted

                t = time.time()

                #fig, axes = plt.subplots(1, 2,figsize=(19, 6))
                #plt.rcParams.update({'font.size': 16})
                fig, axes = plt.subplots(1, 2,figsize=(9.5, 3))
                plt.rcParams.update({'font.size': 8})
                plt.rcParams.update({'mathtext.default':'regular'}) 

                ## 1st subplot
                m =  map_plot(axis=axes[0],area=area)
                m.ax.set_title('Rain Rate (opera + MSG ANN), '+str(time_slot))

                # plot a white colored background where I have data available
                v_pd_nt=np.array([0.5,1.5]) 
                cmap_pd_nt, norm_pd_nt = from_levels_and_colors(v_pd_nt, colors=['white'], extend='neither')
                im4=m.pcolormesh(lon,lat,np.ones(lon.shape),cmap=cmap_pd_nt,norm=norm_pd_nt,latlon=True)

                # plot mask which contains no rad & not trusted rad values
                nr_ntr = copy.deepcopy(ver['ody']) 
                nr_ntr=np.ma.masked_greater(nr_ntr,2)
                nr_ntr=np.ma.masked_equal(nr_ntr,1)
                im2=m.pcolormesh(lon,lat,nr_ntr,cmap=cmap_pd,norm=norm_pd,latlon=True)

                # plot combined precip opera + sat
                v_rr = [0.3,0.6,1.2,2.4,4.8,9.6]
                cmap_rr,norm_rr=smart_colormap(v_rr,name='coolwarm',extend='max')
                im=m.pcolormesh(lon,lat,rr['combi'],cmap=cmap_rr,norm=norm_rr,latlon=True)

                # plot pink pixels everywhere on permanently not trusted radar mask where we observe > 0.3 mm/h precip
                v_pd_nt=np.array([0.5,1.5])
                cmap_pd_nt, norm_pd_nt = from_levels_and_colors(v_pd_nt, colors=['plum'], extend='neither')
                im3=m.pcolormesh(lon,lat,pd_nt,cmap=cmap_pd_nt,norm=norm_pd_nt,latlon=True)

                ## 2nd subplot
                # plot purely satellite based precip product
                m =  map_plot(axis=axes[1],area=area)
                m.ax.set_title('Rain Rate (MSG ANN), '+str(time_slot))

                if in_msg.IR_108 and not in_msg.read_from_netCDF:
                    # plot the IR_108 channel
                    clevs = np.arange(225,316,10)
                    cmap_sat,norm_sat=smart_colormap(clevs,name='Greys',extend='both')
                    im4 = m.pcolormesh(lon,lat,data_sat['IR_108_PC'].data,cmap=cmap_sat,norm=norm_sat,latlon=True)
                else:
                    # plot a white surface to distinguish between the regions where the produ
                    v_pd_nt=np.array([0.5,1.5])
                    cmap_pd_nt, norm_pd_nt = from_levels_and_colors(v_pd_nt, colors=['white'], extend='neither')
                    im4=m.pcolormesh(lon,lat,np.ones(lon.shape),cmap=cmap_pd_nt,norm=norm_pd_nt,latlon=True)

                if in_msg.probab_match:
                    im=m.pcolormesh(lon, lat,rr[in_msg.model+'_pm'], cmap=cmap_rr, norm=norm_rr, latlon=True)
                else:
                    im=m.pcolormesh(lon, lat,rr[in_msg.model],       cmap=cmap_rr, norm=norm_rr, latlon=True)

                if in_msg.IR_108 and in_msg.probab_match:
                    outfile= 'rr_combioperasat'+in_msg.model+'pm_satIR108'+in_msg.model+'pm_%s'    
                elif in_msg.IR_108 and (in_msg.probab_match==False):
                    outfile= 'rr_combioperasat'+in_msg.model+'_satIR108'+in_msg.model+'_%s'
                elif (in_msg.IR_108==False) and in_msg.probab_match:
                    outfile= 'rr_combioperasat'+in_msg.model+'pm_sat'+in_msg.model+'pm_%s'
                elif (in_msg.IR_108==False) and (in_msg.probab_match==False):
                    outfile= 'rr_combioperasat'+in_msg.model+'_sat'+in_msg.model+'_%s'    

                fig.subplots_adjust(bottom=0.15)
                cbar_ax = fig.add_axes([0.25, 0.05, 0.5, 0.05])
                cbar=fig.colorbar(im, cax=cbar_ax, orientation='horizontal')
                cbar.set_label('$mm\,h^{-1}$')


                fig.savefig((in_msg.outputDir+outfile %time_slot.strftime('%Y%m%d%H%M')), dpi=300, bbox_inches='tight')
                print('... create figure: display ' + in_msg.outputDir+outfile %time_slot.strftime('%Y%m%d%H%M') + '.png')

                elapsed = time.time() - t
                print("... elapsed time for creating the rainrate image in seconds: "+str(elapsed))

                plots_done[area].append('rrMatplotlib')
                
            ####################################
            ## plot rain rate with trollimage
            ####################################
            plot_trollimage=True
            if plot_trollimage:

                from plotting_tools import create_trollimage
                from plot_msg import add_title
                
                print ("*** create plot with trollimage")
                from copy import deepcopy
                from trollimage.colormap import RainRate
                colormap = deepcopy(RainRate)

                # define contour write for coasts, borders, rivers
                from pycoast import ContourWriterAGG
                cw = ContourWriterAGG(in_msg.mapDir)

                from plot_msg import choose_map_resolution
                resolution = choose_map_resolution(area, None) 
                #resolution='l' # odyssey, europe
                #resolution='i' # ccs4
                print ("    resolution=", resolution)

                IR_file=time_slot.strftime(in_msg.outputDir+'MSG_IR-108-'+area+'_%Y%m%d%H%M.png')
                
                if 'IR_108' in in_msg.plots[area] and not in_msg.read_from_netCDF:
                    # create black white background
                    #img_IR_108 = data_sat.image.channel_image('IR_108_PC')
                    img_IR_108 = data_sat.image.ir108()
                    img_IR_108.save(IR_file)
                                    
                for rgb in in_msg.plots[area]:
                        if rgb == 'RATE':
                            prop = np.ma.masked_equal(rr['ody'], 0)
                            mask2plot=deepcopy(mask_r)
                        elif rgb =='rrMlp':
                            prop = np.ma.masked_equal(rr[in_msg.model], 0)
                            mask2plot=None
                        elif rgb == 'rrMlpPm':
                            prop = np.ma.masked_equal(rr[in_msg.model+'_pm'], 0)
                            mask2plot=None
                        elif rgb == 'rrOdyMlp':
                            rr['combi']=copy.deepcopy(rr[in_msg.model])
                            rr['combi'][mask_r]=rr['ody'][mask_r]
                            prop = np.ma.masked_equal(rr['combi'], 0)
                            mask2plot=deepcopy(mask_r)
                        elif rgb == 'rrOdyMlpPm':
                            rr['combi']=copy.deepcopy(rr[in_msg.model+'_pm'])
                            rr['combi'][mask_r] = rr['ody'][mask_r]
                            prop = np.ma.masked_equal(rr['combi'], 0)
                            mask2plot=deepcopy(mask_r)
                        elif rgb == 'IR_108':
                            continue
                        else:
                            "*** Error, unknown product requested"
                            quit()
                        filename = None
                        if area in in_msg.postprocessing_composite:
                            composite_file = in_msg.outputDir+"/"+'MSG_'+in_msg.postprocessing_composite[area][0]+"-"+area+'_%Y%m%d%H%M.png'
                            composite_file = composite_file.replace("%(rgb)s", rgb)
                        else:
                            composite_file = None
                            
                        PIL_image = create_trollimage(rgb, prop, colormap, cw, filename, time_slot, area, composite_file=composite_file,
                                          background=IR_file, mask=mask2plot, resolution=resolution, scpOutput=in_msg.scpOutput)

                        # add title to image
                        dc = DecoratorAGG(PIL_image)
                        if in_msg.add_title:
                            add_title(PIL_image, in_msg.title, rgb, 'MSG', sat_nr, in_msg.datetime, area, dc, in_msg.font_file, True,
                                      title_color=in_msg.title_color, title_y_line_nr=in_msg.title_y_line_nr ) # !!! needs change
                            
                        # save image as file
                        outfile = time_slot.strftime(in_msg.outputDir+"/"+in_msg.outputFile).replace("%(rgb)s", rgb).replace("%(area)s", area).replace("%(model)s", in_msg.model)

                        PIL_image.save(outfile, optimize=True)
                        if isfile(outfile):
                            print ("... create figure: display "+outfile+" &")
                            chmod(outfile, 0777)
                            plots_done[area].append(rgb)
                        else:
                            print ("*** Error: "+outfile+" could not be generated")
                            quit()
                            
                            

                print('=================================')

            ##############################################
            ## potential other map setups
            ##############################################
            ##############################################

            ##############################################
            ## opera composite vs the prediction... but I think it'd be less confusing to only show the prediction
            ##############################################

            if 'OdyVsRr' in in_msg.plots[area]:

                fig, axes = plt.subplots(1, 2,figsize=(23.5, 5))
                    # will be switched to basemap once have new training set together
                plt.rcParams.update({'font.size': 16})
                plt.rcParams.update({'mathtext.default':'regular'}) 

                # set up nn subplot       
                m =  map_plot(axis=axes[0],area=area)
                m.ax.set_title('precip detection based on mlp vs opera')
                v_pd=np.array([-0.5,0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5])
                cmap_pd, norm_pd = from_levels_and_colors(v_pd, colors=['darkgrey', '#984ea3','lightgrey','plum', '#377eb8', '#e41a1c','ivory','#ff7f00'], extend='neither')
                tick_label_pd_nr=np.array([0,1,2,3,4,5,6,7])
                tick_label_pd=['sat: no','sat: yes','sat: no (rad unr)','sat: yes (rad unr)','hit','false alarm','correct reject','miss']

                im=m.pcolormesh(lon,lat,ver['mlp'],cmap=cmap_pd, norm=norm_pd, latlon=True)
                divider = make_axes_locatable(m.ax)
                cax = divider.append_axes("right", size="4%", pad=0.05)
                cbar = fig.colorbar(im,cax=cax, ticks=tick_label_pd_nr, spacing='uniform')
                cbar.ax.set_yticklabels(tick_label_pd, fontsize=14)

                m =  map_plot(axis=axes[1],area=area)
                m.ax.set_title('precip detection based on opera vs opera')

                v_pd=np.array([-0.5,0.5,2.5,3.5,4.5,6.5])
                cmap_pd, norm_pd = from_levels_and_colors(v_pd, colors=['darkgrey','lightgrey','plum', '#377eb8','ivory'], extend='neither')
                tick_label_pd_nr=np.array([0,1.5,3,4,5.5])
                tick_label_pd=['no rad','rad clutter: no','rad clutter: yes','rad: yes','rad: no']

                im=m.pcolormesh(lon,lat,ver['ody'],cmap=cmap_pd,norm=norm_pd,latlon=True)
                divider = make_axes_locatable(m.ax)
                cax = divider.append_axes("right", size="4%", pad=0.05)
                cbar = fig.colorbar(im,cax=cax,ticks=tick_label_pd_nr,spacing='uniform')
                a=cbar.ax.set_yticklabels(tick_label_pd,fontsize=14)

                outfile= 'test_%s'
                fig.savefig((in_msg.outputDir+ outfile %time_slot.strftime('%Y%m%d%H%M')), dpi=300, bbox_inches='tight')
                print('... create figure: display ' + in_msg.outputDir+outfile %time_slot.strftime('%Y%m%d%H%M') + '.png')

                plots_done[area].append('OdyVsRr')
                
            ##############################################
            ## cth visualisation without parallax corr for a test
            ##############################################

            if 'CTH' in in_msg.plots[area]:
                fig, axes = plt.subplots(1, 1,figsize=(5, 3))
                plt.rcParams.update({'font.size': 16})
                plt.rcParams.update({'mathtext.default':'regular'}) 

                ## 1st subplot
                m =  map_plot(axis=axes,area=area)
                m.ax.set_title('CTH (without parallax corr)')

                v_rr = np.arange(6000,12001,1000)
                cmap_rr,norm_rr=smart_colormap(v_rr,name='coolwarm',extend='neither')

                im4 = m.pcolormesh(lon,lat,data_cth['CTTH'].height,cmap=cmap_rr,norm=norm_rr,latlon=True)
                fig.colorbar(im4)

                data_cth['CTTH'].height

                outfile= 'CTH_without_parallax_%s'
                fig.savefig((in_msg.outputDir+ outfile %time_slot.strftime('%Y%m%d%H%M')), dpi=300, bbox_inches='tight')
                print('... create figure: display ' + in_msg.outputDir+outfile %time_slot.strftime('%Y%m%d%H%M') + '.png')

                plots_done[area].append('CTH')
                
        # end of area loop
        ## start postprocessing
        for area in in_msg.postprocessing_areas:
            postprocessing(in_msg, time_slot, int(sat_nr), area)
            
        # increase the time by a time delta
        time_slot += delta
        # end of time loop

    return plots_done
Ejemplo n.º 54
0
def test_cmap_and_norm_from_levels_and_colors2():
    levels = [-1, 2, 2.5, 3]
    colors = ['red', (0, 1, 0), 'blue', (0.5, 0.5, 0.5), (0.0, 0.0, 0.0, 1.0)]
    clr = mcolors.to_rgba_array(colors)
    bad = (0.1, 0.1, 0.1, 0.1)
    no_color = (0.0, 0.0, 0.0, 0.0)
    masked_value = 'masked_value'

    # Define the test values which are of interest.
    # Note: levels are lev[i] <= v < lev[i+1]
    tests = [
        ('both', None, {
            -2: clr[0],
            -1: clr[1],
            2: clr[2],
            2.25: clr[2],
            3: clr[4],
            3.5: clr[4],
            masked_value: bad
        }),
        ('min', -1, {
            -2: clr[0],
            -1: clr[1],
            2: clr[2],
            2.25: clr[2],
            3: no_color,
            3.5: no_color,
            masked_value: bad
        }),
        ('max', -1, {
            -2: no_color,
            -1: clr[0],
            2: clr[1],
            2.25: clr[1],
            3: clr[3],
            3.5: clr[3],
            masked_value: bad
        }),
        ('neither', -2, {
            -2: no_color,
            -1: clr[0],
            2: clr[1],
            2.25: clr[1],
            3: no_color,
            3.5: no_color,
            masked_value: bad
        }),
    ]

    for extend, i1, cases in tests:
        cmap, norm = mcolors.from_levels_and_colors(levels,
                                                    colors[0:i1],
                                                    extend=extend)
        cmap.set_bad(bad)
        for d_val, expected_color in cases.items():
            if d_val == masked_value:
                d_val = np.ma.array([1], mask=True)
            else:
                d_val = [d_val]
            assert_array_equal(
                expected_color,
                cmap(norm(d_val))[0], 'Wih extend={0!r} and data '
                'value={1!r}'.format(extend, d_val))

    with pytest.raises(ValueError):
        mcolors.from_levels_and_colors(levels, colors)
Ejemplo n.º 55
0
            mlab.view(180, 0, 450, [0, 10, 0])
            mlab.savefig('{d}{cont}_connectivity_{f}_sum_top.png'.format(
                d=save_dir, cont=contrast, f=freq),
                         magnification=4)
            mlab.view(180, 180, 480, [0, 10, 0])
            mlab.savefig('{d}{cont}_connectivity_{f}_sum_bottom.png'.format(
                d=save_dir, cont=contrast, f=freq),
                         magnification=4)
            mlab.close(fig)

            # Plot the connectivity diagram with optimized diverging colorspace
            con_parc_sum.data = con_parc_sum.data * 1000
            vmax = con_parc_sum.data.max()
            vmin = con_parc_sum.data.min()
            num_levels = 40
            midpoint = 0
            levels = np.linspace(vmin, vmax, num_levels)
            midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
            vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
            colors = plt.cm.seismic(vals)
            cmap, norm = from_levels_and_colors(levels, colors)
            fig, _ = con_parc_sum.plot(title='Parcel-wise Connectivity by Sum',
                                       facecolor='white',
                                       textcolor='black',
                                       node_edgecolor='white',
                                       colormap=cmap,
                                       show=True)
            fig.savefig('{d}{cont}_connectivity_{f}_sum_squircle.pdf'.format(
                d=save_dir, cont=contrast, f=freq),
                        bbox_inches='tight')