def test_with_scalar_field(self):
        # Transform and regrid vector (with no projection transform) with an
        # additional scalar field.
        expected_x_grid = np.array([[-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.]])
        expected_y_grid = np.array([[5., 5., 5., 5., 5.],
                                    [7.5, 7.5, 7.5, 7.5, 7.5],
                                    [10., 10., 10., 10., 10]])
        expected_u_grid = np.array([[np.nan, 2., 3., 2., np.nan],
                                    [np.nan, 2.5, 3.5, 2.5, np.nan],
                                    [2., 3., 4., 3., 2.]])
        expected_v_grid = np.array([[np.nan, .8, .3, .8, np.nan],
                                    [np.nan, 2.675, 2.15, 2.675, np.nan],
                                    [5.5, 4.75, 4., 4.75, 5.5]])
        expected_s_grid = np.array([[np.nan, 2., 3., 2., np.nan],
                                    [np.nan, 2.5, 3.5, 2.5, np.nan],
                                    [2., 3., 4., 3., 2.]])

        src_crs = target_crs = ccrs.PlateCarree()
        x_grid, y_grid, u_grid, v_grid, s_grid = \
            vec_trans.vector_scalar_to_grid(src_crs, target_crs, (5, 3),
                                            self.x, self.y,
                                            self.u, self.v, self.s)

        assert_array_equal(x_grid, expected_x_grid)
        assert_array_equal(y_grid, expected_y_grid)
        assert_array_almost_equal(u_grid, expected_u_grid)
        assert_array_almost_equal(v_grid, expected_v_grid)
        assert_array_almost_equal(s_grid, expected_s_grid)
Ejemplo n.º 2
0
    def test_with_scalar_field(self):
        # Transform and regrid vector (with no projection transform) with an
        # additional scalar field.
        expected_x_grid = np.array([[-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.]])
        expected_y_grid = np.array([[5., 5., 5., 5., 5.],
                                    [7.5, 7.5, 7.5, 7.5, 7.5],
                                    [10., 10., 10., 10., 10]])
        expected_u_grid = np.array([[np.nan, 2., 3., 2., np.nan],
                                    [np.nan, 2.5, 3.5, 2.5, np.nan],
                                    [2., 3., 4., 3., 2.]])
        expected_v_grid = np.array([[np.nan, .8, .3, .8, np.nan],
                                    [np.nan, 2.675, 2.15, 2.675, np.nan],
                                    [5.5, 4.75, 4., 4.75, 5.5]])
        expected_s_grid = np.array([[np.nan, 2., 3., 2., np.nan],
                                    [np.nan, 2.5, 3.5, 2.5, np.nan],
                                    [2., 3., 4., 3., 2.]])

        src_crs = target_crs = ccrs.PlateCarree()
        x_grid, y_grid, u_grid, v_grid, s_grid = \
            vec_trans.vector_scalar_to_grid(src_crs, target_crs, (5, 3),
                                            self.x, self.y,
                                            self.u, self.v, self.s)

        assert_array_equal(x_grid, expected_x_grid)
        assert_array_equal(y_grid, expected_y_grid)
        assert_array_almost_equal(u_grid, expected_u_grid)
        assert_array_almost_equal(v_grid, expected_v_grid)
        assert_array_almost_equal(s_grid, expected_s_grid)
Ejemplo n.º 3
0
    def test_with_transform(self):
        # Transform and regrid vector.
        target_crs = ccrs.PlateCarree()
        src_crs = ccrs.NorthPolarStereo()

        input_coords = [src_crs.transform_point(xp, yp, target_crs)
                        for xp, yp in zip(self.x, self.y)]
        x_nps = np.array([ic[0] for ic in input_coords])
        y_nps = np.array([ic[1] for ic in input_coords])
        u_nps, v_nps = src_crs.transform_vectors(target_crs, self.x, self.y,
                                                 self.u, self.v)

        expected_x_grid = np.array([[-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.]])
        expected_y_grid = np.array([[5., 5., 5., 5., 5.],
                                    [7.5, 7.5, 7.5, 7.5, 7.5],
                                    [10., 10., 10., 10., 10]])
        expected_u_grid = np.array([[np.nan, 2., 3., 2., np.nan],
                                    [np.nan, 2.5, 3.5, 2.5, np.nan],
                                    [2., 3., 4., 3., 2.]])
        expected_v_grid = np.array([[np.nan, .8, .3, .8, np.nan],
                                    [np.nan, 2.675, 2.15, 2.675, np.nan],
                                    [5.5, 4.75, 4., 4.75, 5.5]])

        x_grid, y_grid, u_grid, v_grid = vec_trans.vector_scalar_to_grid(
            src_crs, target_crs, (5, 3), x_nps, y_nps, u_nps, v_nps)

        assert_array_almost_equal(x_grid, expected_x_grid)
        assert_array_almost_equal(y_grid, expected_y_grid)
        # Vector transforms are somewhat approximate, so we are more lenient
        # with the returned values since we have transformed twice.
        assert_array_almost_equal(u_grid, expected_u_grid, decimal=4)
        assert_array_almost_equal(v_grid, expected_v_grid, decimal=4)
Ejemplo n.º 4
0
    def test_with_transform(self):
        # Transform and regrid vector.
        target_crs = ccrs.PlateCarree()
        src_crs = ccrs.NorthPolarStereo()

        input_coords = [src_crs.transform_point(xp, yp, target_crs)
                        for xp, yp in zip(self.x, self.y)]
        x_nps = np.array([ic[0] for ic in input_coords])
        y_nps = np.array([ic[1] for ic in input_coords])
        u_nps, v_nps = src_crs.transform_vectors(target_crs, self.x, self.y,
                                                 self.u, self.v)

        expected_x_grid = np.array([[-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.],
                                    [-10., -5., 0., 5., 10.]])
        expected_y_grid = np.array([[5., 5., 5., 5., 5.],
                                    [7.5, 7.5, 7.5, 7.5, 7.5],
                                    [10., 10., 10., 10., 10]])
        expected_u_grid = np.array([[np.nan, 2., 3., 2., np.nan],
                                    [np.nan, 2.5, 3.5, 2.5, np.nan],
                                    [2., 3., 4., 3., 2.]])
        expected_v_grid = np.array([[np.nan, .8, .3, .8, np.nan],
                                    [np.nan, 2.675, 2.15, 2.675, np.nan],
                                    [5.5, 4.75, 4., 4.75, 5.5]])

        x_grid, y_grid, u_grid, v_grid = vec_trans.vector_scalar_to_grid(
            src_crs, target_crs, (5, 3), x_nps, y_nps, u_nps, v_nps)

        assert_array_almost_equal(x_grid, expected_x_grid)
        assert_array_almost_equal(y_grid, expected_y_grid)
        # Vector transforms are somewhat approximate, so we are more lenient
        # with the returned values since we have transformed twice.
        assert_array_almost_equal(u_grid, expected_u_grid, decimal=4)
        assert_array_almost_equal(v_grid, expected_v_grid, decimal=4)
Ejemplo n.º 5
0
def plot_base(data, itime, var_levels, oname):
    #land,coastline,states,reader=features
    #Size
    #WXGA size
    #plt.figure(figsize=(12.80,7.20),dpi=100)
    t1 = dt.datetime.now()
    #FHD size
    fig = plt.figure(figsize=(19.20, 10.80), dpi=100)

    #Adjusting margins figure
    fig.subplots_adjust(left=0.05, right=0.95, top=0.97, bottom=0.03)
    ax = plt.subplot(
        1,
        1,
        1,
        #ax=fig.add_subplot(1,1,1,
        projection=proj,
    )
    ax.set_extent(ax_lim, proj)
    #load features
    #ax.add_feature(ocean,
    #        facecolor=feature.COLORS['water'],
    #        )
    ax.add_feature(
        land,
        zorder=10,
    )
    ax.add_feature(
        feature.BORDERS,
        zorder=11,
    )
    ax.add_feature(
        coastline,
        edgecolor='black',
        zorder=12,
    )
    ax.add_feature(
        states,
        edgecolor='gray',
        zorder=12,
    )
    #print('features:',dt.datetime.now()-t1, end=' ')
    #url = 'https://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi'
    #layer = 'VIIRS_CityLights_2012'
    #ax.add_wmts(url, layer)

    #print('names:',dt.datetime.now()-t1, end=' ')

    #logo plot
    im = image.imread('logo_atmosfera.png')
    ax.imshow(
        im,
        aspect='equal',
        extent=(lon_min + 0.1, lon_min + 1.1, lat_max, lat_max - 0.27),
        zorder=15,
    )
    #print('logo:',dt.datetime.now()-t1, end=' ')
    #info text
    txt_box = '\n'.join((
        '>Distribución del sargazo + elevación +',
        ' Corriente en superficie',
        '>Modelo: FVCOM',
        '>Grupo Interacción Océano-Atmósfera',
    ))
    #
    #bbox_props = dict(boxstyle="pad=0.8", fc="white", ec="b", lw=2)
    #info box plot
    draw_info(ax, title, txt_box)
    #print('box::',dt.datetime.now()-t1, end=' ')
    #wind plot
    #u=np.expand_dims(data['u'][itime],axis=0)
    #v=np.expand_dims(data['v'][itime],axis=0)
    #magn=np.expand_dims(data['magnitude'][itime],axis=0)
    x, y, u, v = cvt.vector_scalar_to_grid(
        proj,
        proj,
        25,
        data['lonc'],
        data['latc'],
        data['u'][itime],
        data['v'][itime],
    )

    #ind=np.arange(0,len(u),20)
    ax.quiver(
        x,
        y,
        u,
        v,
        scale=2,
        scale_units='inches',
        pivot='middle',
        color='blue',
        zorder=25,
    )
    #ax.barbs(
    #        x,y,u,v,
    #        #sizes=dict(emptybarb=0.1, spacing=2.2, height=0.5),
    #        #linewidth=0.95,
    #        #length=10,
    #        #pivot='middle',
    #        #transform=ccrs.PlateCarree(),
    #        zorder=25,
    #        )
    #wplot=ax.streamplot(
    #    data['lonc'],
    #    data['latc'],
    #    u,v,
    #    #linewidth=1,
    #    #density=2,
    #    color='red',
    #    #cmap=plt.cm.tab10,
    #    #levels=np.arange(0,2,0.2),
    #    zorder=5,
    #    )
    #city names
    for name in city:
        ax.text(
            name[0],
            name[1],
            name[2],
            #fontsize=2,
            zorder=17,
            #clip_on=True,
            ha='center',
            va='top',
            transform=ccrs.PlateCarree(),
            color='black',
        )
    #city points plot
    ax.scatter(
        xp,
        yp,
        100,
        marker='*',
        color='white',
        transform=ccrs.PlateCarree(),
        zorder=15,
    )
    #print('points:',dt.datetime.now()-t1, end=' ')
    #apply limits
    #plt.axis(ax_lim)
    ax_leg = ax.gridlines(
        draw_labels=True,
        linestyle='--',
    )
    ax_leg.xlabels_top = False
    ax_leg.ylabels_right = False
    ax_leg.xlocator = mticker.FixedLocator(
        range(int(lon_min),
              int(lon_max) + 1, 1))
    ax_leg.xformatter = LONGITUDE_FORMATTER
    ax_leg.ylocator = mticker.FixedLocator(
        range(int(lat_min),
              int(lat_max) + 2, 1))
    ax_leg.yformatter = LATITUDE_FORMATTER

    #plot
    l_map = ax.tricontour(
        data['lon'],
        data['lat'],
        data['nv'],
        data['var'][itime],
        colors='gray',
        levels=var_levels,
    )
    #ax.clabel(z_map,
    #        )
    #print('lines:',dt.datetime.now()-t1, end=' ')
    var_map = plt.tricontourf(
        data['lon'],
        data['lat'],
        data['nv'],
        data['var'][itime],
        levels=var_levels,
        #transform=ccrs.Mercator(),
        #cmap=plt.cm.coolwarm,
        cmap=my_cbar,
    )
    #print('fill:',dt.datetime.now()-t1, end=' ')
    #print(data['time'][itime])
    plt.title(data['time'][itime])

    #color bar
    cbar = plt.colorbar(
        var_map,
        #ax=ax,
        #ticks=t_ticks,
        ticks=var_levels,
        #shrink=0.75,
        #orientation='horizontal',
        aspect=50,
        pad=0.02,
        fraction=0.03,
    )
    cbar.ax.set_ylabel('Elevación de la superficie [m]')
    ##mat plot
    ax.plot(
        data['par_lon'],
        data['par_lat'],
        'r,',
        #color='red',
        #marker='*',
        #markersize=12,
        transform=ccrs.PlateCarree(),
        zorder=1,
    )
    #print('mat:',dt.datetime.now()-t1, )
    plt.savefig('{}_{:03}.png'.format(oname, itime))
    return 0
Ejemplo n.º 6
0
                               labelpad=4,
                               rotation=0.)
                cbar.ax.tick_params(labelsize=12)

            else:
                # change the gridded vector data, don't update other plot elements;
                # this speeds things up immensely, especially with high-res maps

                if regrid:
                    # manually perform calculations normally done by ax.quiver()
                    target_extent = ax.get_extent(ax.projection)
                    _, _, Ys_trans, Xs_trans, Ms_trans = cvt.vector_scalar_to_grid(
                        proj_data,
                        projection,
                        regrid,
                        Lons,
                        Lats,
                        Ys[i] * Ms[i],
                        Xs[i] * Ms[i], (10**Ms[i]),
                        target_extent=target_extent)
                else:
                    Ys_trans, Xs_trans = projection.transform_vectors(
                        proj_data, Lons, Lats, Ys[i] * Ms[i], Xs[i] * Ms[i])
                    Ms_trans = 10**Ms[i]
                Q.set_UVC(Ys_trans, Xs_trans, Ms_trans)

                # change scatter plot "offsets"
                avail = ObsFits[i].astype(bool)
                S_avail.set_offsets(
                    np.array(list(zip(ObsLons, ObsLats)))[avail])
                S_miss.set_offsets(