Ejemplo n.º 1
0
def test_FUV():
    x = loadbunch(fname, masked=False)
    # Switch epoch from Matlab to Python
    x.t -= 366
    x.t0 -= 366

    for i, flag in enumerate(x.flags):
        F, U, V = FUV(x.t, x.t0, x.lind - 1, x.lat, flag)
        print(f"i: {i} ngflags: {flag}")

        # We use broadcasting instead of replication, so
        # we need to test only against the first row of
        # the octave output in such cases.
        if F.shape[0] == 1:
            sub = (i, slice(0, 1))
        else:
            sub = (i, )
        assert_array_almost_equal(F, x.Fo[sub])
        assert_array_almost_equal(U, x.Uo[sub])

        if V.shape[0] == 1:
            sub = (i, slice(0, 1))
        else:
            sub = (i, )
        assert_array_almost_equal(V, x.Vo[sub])
Ejemplo n.º 2
0
def test_FUV():
    x = loadbunch(fname, masked=False)
    for i, flag in enumerate(x.flags):
        F, U, V = FUV(x.t, x.t0, x.lind - 1, x.lat, flag)
        print('i:', i, "ngflgs:", flag)

        # We use broadcasting instead of replication, so
        # we need to test only against the first row of
        # the octave output in such cases.
        if F.shape[0] == 1:
            sub = (i, slice(0, 1))
        else:
            sub = (i, )
        assert_array_almost_equal(F, x.Fo[sub])
        assert_array_almost_equal(U, x.Uo[sub])

        if V.shape[0] == 1:
            sub = (i, slice(0, 1))
        else:
            sub = (i, )
        assert_array_almost_equal(V, x.Vo[sub])
Ejemplo n.º 3
0
# this takes 12 minutes instead of Iris 14 seconds
#uamp = adcirc.Dataset.variables['u_amp'][0,adcirc.nodes_in_ss,:][:,ind_nc]
#vamp = adcirc.Dataset.variables['v_amp'][0,adcirc.nodes_in_ss,:][:,ind_nc]
#upha = adcirc.Dataset.variables['u_phase'][0,adcirc.nodes_in_ss,:][:,ind_nc]
#vpha = adcirc.Dataset.variables['v_phase'][0,adcirc.nodes_in_ss,:][:,ind_nc]

freq_nc = adcirc.Dataset.variables['tidefreqs'][:][ind_nc]
freq_ttide = con_info['freq'][ind_ttide]
t_tide_names = np.array(const_name)[ind_ttide]
omega_ttide = 2 * np.pi * freq_ttide  # Convert from radians/s to radians/hour.

omega = freq_nc * 3600

v, u, f = FUV(t=np.array([date2num(start)]),
              tref=np.array([0]),
              lind=np.array([ind_ttide]),
              lat=55,
              ngflgs=[0, 0, 0, 0])

# Convert phase in radians.
v, u, f = (np.squeeze(i) for i in (v, u, f))
v = v * 2 * np.pi
u = u * 2 * np.pi

thours = np.array([d.total_seconds()
                   for d in (glocals - glocals[0])]) / 60 / 60.

adcirc.data['u'] = np.ones((len(thours), len(adcirc.nodes_in_ss)), )
adcirc.data['v'] = np.ones((len(thours), len(adcirc.nodes_in_ss)), )

k = 0
Ejemplo n.º 4
0
    def get_tidal_vectors(self,
                          layer,
                          time,
                          bbox,
                          vector_scale=None,
                          vector_step=None):

        vector_scale = vector_scale or 1
        vector_step = vector_step or 1

        with netCDF4.Dataset(self.topology_file) as nc:
            data_obj = nc.variables[layer.access_name]
            data_location = getattr(data_obj, 'location', 'node')
            mesh_name = data_obj.mesh

            ug = UGrid.from_nc_dataset(nc, mesh_name=mesh_name)
            coords = np.empty(0)
            if data_location == 'node':
                coords = ug.nodes
            elif data_location == 'face':
                coords = ug.face_coordinates
            elif data_location == 'edge':
                coords = ug.edge_coordinates

            lon = coords[:, 0]
            lat = coords[:, 1]

            padding_factor = calc_safety_factor(vector_scale)
            padding = calc_lon_lat_padding(lon, lat,
                                           padding_factor) * vector_step
            spatial_idx = data_handler.ugrid_lat_lon_subset_idx(
                lon, lat, bbox=bbox, padding=padding)

            tnames = nc.get_variables_by_attributes(
                standard_name='tide_constituent')[0]
            tfreqs = nc.get_variables_by_attributes(
                standard_name='tide_frequency')[0]

            from utide import _ut_constants_fname
            from utide.utilities import loadmatbunch
            con_info = loadmatbunch(_ut_constants_fname)['const']

            # Get names from the utide constant file
            utide_const_names = [e.strip() for e in con_info['name'].tolist()]

            # netCDF4-python is returning ugly arrays of bytes...
            names = []
            for n in tnames[:]:
                z = ''.join([x.decode('utf-8') for x in n.tolist()
                             if x]).strip()
                names.append(z)

            if 'STEADY' in names:
                names[names.index('STEADY')] = 'Z0'
            extract_names = list(
                set(utide_const_names).intersection(set(names)))

            ntides = data_obj.shape[data_obj.dimensions.index('ntides')]
            extract_mask = np.zeros(shape=(ntides, ), dtype=bool)
            for n in extract_names:
                extract_mask[names.index(n)] = True

            if not spatial_idx.any() or not extract_mask.any():
                e = np.ma.empty(0)
                return e, e, e, e

            ua = nc.variables['u'][extract_mask, spatial_idx]
            va = nc.variables['v'][extract_mask, spatial_idx]
            up = nc.variables['u_phase'][extract_mask, spatial_idx]
            vp = nc.variables['v_phase'][extract_mask, spatial_idx]
            freqs = tfreqs[extract_mask]

            omega = freqs * 3600  # Convert from radians/s to radians/hour.

            from utide.harmonics import FUV
            from matplotlib.dates import date2num
            v, u, f = FUV(
                t=np.array([date2num(time) + 366.1667]),
                tref=np.array([0]),
                lind=np.array(
                    [utide_const_names.index(x) for x in extract_names]),
                lat=
                55,  # Reference latitude for 3rd order satellites (degrees) (55 is fine always)
                ngflgs=[0, 0, 0,
                        0])  # [NodsatLint NodsatNone GwchLint GwchNone]

            s = calendar.timegm(time.timetuple()) / 60 / 60.
            v, u, f = map(np.squeeze, (v, u, f))
            v = v * 2 * np.pi  # Convert phase in radians.
            u = u * 2 * np.pi  # Convert phase in radians.

            U = (f * ua.T *
                 np.cos(v + s * omega + u - up.T * np.pi / 180)).sum(axis=1)
            V = (f * va.T *
                 np.cos(v + s * omega + u - vp.T * np.pi / 180)).sum(axis=1)

            return U, V, lon[spatial_idx], lat[spatial_idx]
Ejemplo n.º 5
0
from matplotlib.dates import date2num

# Convert to Matlab datenum.
# (Soon UTide will take python datetime objects.)
jd_start = date2num(start) + 366.1667

# In[ ]:

from utide.harmonics import FUV

# NB: I am not a 100% sure if this is identical to what we had with t_tide.
# ngflgs -> [NodsatLint NodsatNone GwchLint GwchNone]
v, u, f = FUV(t=np.array([jd_start]),
              tref=np.array([0]),
              lind=np.array([ind_ttide]),
              lat=rllat,
              ngflgs=[0, 0, 0, 0])

# In[ ]:

# Convert phase in radians.
v, u, f = map(np.squeeze, (v, u, f))
v = v * 2 * np.pi
u = u * 2 * np.pi

thours = np.array([d.total_seconds()
                   for d in (glocals - glocals[0])]) / 60 / 60.

# In[ ]: