예제 #1
0
파일: ellipse.py 프로젝트: mdunphy/tools
def prepare_vel(u, v, depav=False, depth='None'):
    """ Preparing the time series of the orthogonal pair of velocities to get tidal
    ellipse parameters. This function masks, rotates and unstaggers the time
    series. The depth averaging does not work over masked values.

    :arg u: One of the orthogonal components of the time series of tidal
        current velocities.
    :type u:  :py:class:'np.ndarray'

    :arg v: One of the orthogonal components of the time series of tidal
        current velocities.
    :type v:  :py:class:'np.ndarray'

    :arg depav: True will depth average over the whole depth profile given.
        Default is False.
    :type dep: boolean

    :arg depth: depth vector corresponding to the depth of the velocities, only
        requiered if depav=True.
    :type depth: :py:class:'np.ndarray' or string
    """
    # Masks land values
    u_0 = np.ma.masked_values(u, 0)
    v_0 = np.ma.masked_values(v, 0)

    # Unstaggers velocities. Will loose one x and one y dimension due to
    # unstaggering.
    u_u, v_v = research_VENUS.unstag_rot(u_0, v_0)

    # Depth averaging over all the depth values given if set to True.
    if depav:
        u_u = analyze.depth_average(u_u, depth, 1)
        v_v = analyze.depth_average(v_v, depth, 1)
    return u_u, v_v
예제 #2
0
def baroclinic_tide(u, time, depth, nconst):
    """Perform a harmonic analysis on the baroclinic tide

    u is the full depth profile of a current
    If u is from NEMO output, it should be unstaggered before this funcion
    is applied.
    u should have at least a time and depth dimension but could also have
    y, x shape
    the depth dimensions must be in axis 1.
    time is the times associated with the current.
    depth is an array of depths associated with u
    nconsts is the number of constituents to analyze
    returns tide_bc - a nested dictionary with amp and phase for each
    constituent eg. tide_bc['M2']['phase']
    also returns the baroclinic currnt, u_bc"""

    # Calculate depth-averaged current
    u_depav = analyze.depth_average(u, depth, depth_axis=1)
    u_depav = np.expand_dims(u_depav, axis=1)
    # Calculate baroclinic current by removing depth averaged bit
    u_bc = u - u_depav

    # tidal analysis of baroclinic current
    tide_bc = tidetools.fittit(u_bc, time, nconst)

    return tide_bc, u_bc
예제 #3
0
    def test_1d_zeros_array_n(self, linear_depths):
        """Cell 5 case

        Case with var shape (40,).
        Result is not an array so should not use array equailty check.
        """
        var = np.zeros((linear_depths.shape[0]))
        result = analyze.depth_average(var, linear_depths, depth_axis=0)
        assert result == 0
예제 #4
0
 def test_masking_fullmask(self, var, depth_axis, expected,
                           nonuniform_depths):
     """Test for simple masked arrays - entire input array is masked."""
     var = np.ma.masked_values(var, 0)
     result = analyze.depth_average(var,
                                    nonuniform_depths,
                                    depth_axis=depth_axis)
     expected = np.ma.masked_values(expected, 0)
     assert np.ma.allequal(result, expected)
예제 #5
0
 def test_nonuniform_grid_spacing(self, var, depth_axis, expected,
                                  nonuniform_depths):
     """Series of tests for a depth array with non-uniform grid spacing."""
     result = analyze.depth_average(var,
                                    nonuniform_depths,
                                    depth_axis=depth_axis)
     # using almost equal because of some floating point differences
     # Tolerance is 10^-6
     np.testing.assert_array_almost_equal(result, expected)
예제 #6
0
    def test_masking_partial_1d(self, nonuniform_depths):
        """Test for partially masked, 1d array.

        Cell 23 case
        """
        var = np.ones((nonuniform_depths.shape[0], 1))
        var[10:] = 0
        var = np.ma.masked_values(var, 0)
        expected = np.ones((1, ))
        result = analyze.depth_average(var, nonuniform_depths, depth_axis=0)
        np.testing.assert_array_almost_equal(result, expected)
예제 #7
0
    def test_masking_partial_multi(self, nonuniform_depths):
        """Test for partially masked, multidimensional array.

        Cell 24 case
        """
        ts, xs = 2, 3
        var = np.ones((ts, nonuniform_depths.shape[0], xs))
        var[0, 10:, 0] = 0
        var = np.ma.masked_values(var, 0)
        expected = np.ones((ts, xs))
        result = analyze.depth_average(var, nonuniform_depths, depth_axis=1)
        np.testing.assert_array_almost_equal(result, expected)
예제 #8
0
    def test_masking_column_full_mask(self, nonuniform_depths):
        """Test for one column of input fully masked

        Cell 21 case
        """
        ts = 4
        var = np.ones((ts, nonuniform_depths.shape[0]))
        var[0, :] = 0
        var = np.ma.masked_values(var, 0)
        expected = np.ones((ts, ))
        expected[0] = 0
        expected = np.ma.masked_values(expected, 0)
        result = analyze.depth_average(var, nonuniform_depths, depth_axis=1)
        assert np.ma.allclose(result, expected)
예제 #9
0
    def test_multi_dim_nonuniform_x(self, linear_depths):
        """Test for multidimensional array with non uniform values along the
        x axis

        Cell 12 case
        """
        ts = 3
        xs = 2
        var = np.ones((ts, linear_depths.shape[0], xs))
        var[..., 1] = 2 * var[..., 1]
        expected = np.ones((ts, xs))
        expected[..., 1] = 2 * expected[..., 1]
        result = analyze.depth_average(var, linear_depths, depth_axis=1)
        np.testing.assert_array_equal(result, expected)
예제 #10
0
def plotdepavADCP(grid_m, grid_o, day, station):
    """ This function plots a comparison of the depth averaged velocities of
    the model and the observations.

    :arg grid_m: The model grid
    :type grid_m: netCDF4 dataset.

    :arg grid_o: The observational grid
    :type grid_o: dictionary

    :arg day: day of interest
    :type day: datetime object

    :arg station: Station of interest. Either 'Central' or 'East' or 'ddl'
    :type station: string

    :return: fig
    """
    if station == 'Central':
        profile = [40, 270]
    elif station == 'East':
        profile = [30, 150]
    else:
        profile = [25, 130]

    # Get grids into unstaggered and masked velocities at the chose depths
    u_E, v_N, dep_t = load_vel(day, grid_m, 'model', station, profile)
    u, v, dep = load_vel(day, grid_o, 'observation', station, profile)

    # Depth averaging center of water column
    uE_av = analyze.depth_average(u_E, dep_t, 1)
    vN_av = analyze.depth_average(v_N, dep_t, 1)
    u_av = analyze.depth_average(u, dep[::-1], 0)
    v_av = analyze.depth_average(v, dep[::-1], 0)

    # Begin figure
    fig, ([ax1, ax2]) = plt.subplots(2, 1, figsize=(15, 10), sharex=True)
    fig.patch.set_facecolor('#2B3E50')

    # Setting the date for title
    date = day.strftime('%d%b%y')

    timestep = 0.5
    velocities = [uE_av, vN_av]
    veloobs = [u_av, v_av]
    axes = [ax1, ax2]
    direction = ['East/West', 'North/South']

    for ax, vel, velo, direc in zip(axes, velocities, veloobs, direction):
        ax.plot(np.arange(0, 24, timestep/2), vel, label='Model')
        ax.plot(np.arange(0.25, 24, timestep), velo, label='Observations')
        ax.set_xlim([0, 24])
        ax.set_ylabel('Velocity [m/s]', **axis_font)
        figures.axis_colors(ax, 'gray')
        ax.set_title(
            f'Depth Averaged ({profile[0]}-{profile[1]}m) {direc} velocities '
            f'at VENUS {station} -{date}',
            **title_font)
        ax.grid()
        ax.set_ylim([-0.6, 0.6])
    ax1.legend(loc=0)
    ax2.set_xlabel('Hour [UTC]')

    return fig
예제 #11
0
 def test_1d_zeros_array_n_1(self, linear_depths):
     """Cell 4 case"""
     var = np.zeros((linear_depths.shape[0], 1))
     result = analyze.depth_average(var, linear_depths, depth_axis=0)
     assert result == np.zeros((1, ))
     assert result.shape == (1, )
예제 #12
0
 def test_multi_dim_array(self, var, depth_axis, expected, linear_depths):
     """Series of tests for multi-dimensional arrays"""
     result = analyze.depth_average(var,
                                    linear_depths,
                                    depth_axis=depth_axis)
     np.testing.assert_array_equal(result, expected)
예제 #13
0
 def test_1d_array(self, var, depth_axis, expected, linear_depths):
     result = analyze.depth_average(var,
                                    linear_depths,
                                    depth_axis=depth_axis)
     np.testing.assert_array_equal(result, expected)