Esempio n. 1
0
def _main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--show", "--plot", action="store_true")
    args = viscid.vutil.common_argparse(parser)
    # args.show = True

    t = viscid.linspace_datetime64('2006-06-10 12:30:00.0',
                                   '2006-06-10 12:33:00.0', 16)
    tL = viscid.as_datetime64('2006-06-10 12:31:00.0')
    tR = viscid.as_datetime64('2006-06-10 12:32:00.0')
    y = np.linspace(2 * np.pi, 4 * np.pi, 12)

    ### plots with a datetime64 axis
    f0 = viscid.ones([t, y], crd_names='ty', center='node')
    T, Y = f0.get_crds(shaped=True)
    f0.data += np.arange(T.size).reshape(T.shape)
    f0.data += np.cos(Y)

    fig = plt.figure(figsize=(10, 5))
    # 1D plot
    vlt.subplot(121)
    vlt.plot(f0[tL:tR]['y=0'], marker='^')
    plt.xlim(*viscid.as_datetime(t[[0, -1]]).tolist())
    # 2D plot
    vlt.subplot(122)
    vlt.plot(f0, x=(t[0], t[-1]))

    plt.suptitle("datetime64")
    vlt.auto_adjust_subplots(subplot_params=dict(top=0.9))
    plt.savefig(next_plot_fname(__file__))
    if args.show:
        vlt.show()
    plt.close(fig)

    ### plots with a timedelta64 axis
    tL = tL - t[0]
    tR = tR - t[0]
    t = t - t[0]
    f0 = viscid.ones([t, y], crd_names='ty', center='node')
    T, Y = f0.get_crds(shaped=True)
    f0.data += np.arange(T.size).reshape(T.shape)
    f0.data += np.cos(Y)

    fig = plt.figure(figsize=(10, 5))
    # 1D plot
    vlt.subplot(121)
    vlt.plot(f0[tL:tR]['y=0'], marker='^')
    plt.xlim(*viscid.as_datetime(t[[0, -1]]).tolist())
    # 2D plot
    vlt.subplot(122)
    vlt.plot(f0, x=(t[0], t[-1]))

    plt.suptitle("timedelta64")
    vlt.auto_adjust_subplots(subplot_params=dict(top=0.9))
    plt.savefig(next_plot_fname(__file__))
    if args.show:
        vlt.show()
    plt.close(fig)

    return 0
Esempio n. 2
0
def _main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--show", "--plot", action="store_true")
    args = viscid.vutil.common_argparse(parser)
    # args.show = True

    t = viscid.linspace_datetime64('2006-06-10 12:30:00.0',
                                   '2006-06-10 12:33:00.0', 16)
    tL = viscid.as_datetime64('2006-06-10 12:31:00.0')
    tR = viscid.as_datetime64('2006-06-10 12:32:00.0')
    y = np.linspace(2 * np.pi, 4 * np.pi, 12)

    ### plots with a datetime64 axis
    f0 = viscid.ones([t, y], crd_names='ty', center='node')
    T, Y = f0.get_crds(shaped=True)
    f0.data += np.arange(T.size).reshape(T.shape)
    f0.data += np.cos(Y)

    fig = plt.figure(figsize=(10, 5))
    # 1D plot
    vlt.subplot(121)
    vlt.plot(f0[tL:tR]['y=0'], marker='^')
    plt.xlim(*viscid.as_datetime(t[[0, -1]]).tolist())
    # 2D plot
    vlt.subplot(122)
    vlt.plot(f0, x=(t[0], t[-1]))

    plt.suptitle("datetime64")
    vlt.auto_adjust_subplots(subplot_params=dict(top=0.9))
    plt.savefig(next_plot_fname(__file__))
    if args.show:
        vlt.show()
    plt.close(fig)

    ### plots with a timedelta64 axis
    tL = tL - t[0]
    tR = tR - t[0]
    t = t - t[0]
    f0 = viscid.ones([t, y], crd_names='ty', center='node')
    T, Y = f0.get_crds(shaped=True)
    f0.data += np.arange(T.size).reshape(T.shape)
    f0.data += np.cos(Y)

    fig = plt.figure(figsize=(10, 5))
    # 1D plot
    vlt.subplot(121)
    vlt.plot(f0[tL:tR]['y=0'], marker='^')
    plt.xlim(*viscid.as_datetime(t[[0, -1]]).tolist())
    # 2D plot
    vlt.subplot(122)
    vlt.plot(f0, x=(t[0], t[-1]))

    plt.suptitle("timedelta64")
    vlt.auto_adjust_subplots(subplot_params=dict(top=0.9))
    plt.savefig(next_plot_fname(__file__))
    if args.show:
        vlt.show()
    plt.close(fig)

    return 0
Esempio n. 3
0
def main():
    parser = argparse.ArgumentParser(description="Test divergence")
    parser.add_argument("--show", "--plot", action="store_true")
    args = viscid.vutil.common_argparse(parser)
    # args.show = True

    t = viscid.linspace_datetime64("2006-06-10 12:30:00.0", "2006-06-10 12:33:00.0", 16)
    tL = viscid.as_datetime64("2006-06-10 12:31:00.0")
    tR = viscid.as_datetime64("2006-06-10 12:32:00.0")
    y = np.linspace(2 * np.pi, 4 * np.pi, 12)

    ### plots with a datetime64 axis
    f0 = viscid.ones([t, y], crd_names="ty", center="node")
    T, Y = f0.get_crds(shaped=True)
    f0.data += np.arange(T.size).reshape(T.shape)
    f0.data += np.cos(Y)

    fig = mpl.plt.figure(figsize=(10, 5))
    # 1D plot
    mpl.subplot(121)
    mpl.plot(f0[tL:tR]["y=0"], marker="^")
    mpl.plt.xlim(*viscid.as_datetime(t[[0, -1]]).tolist())
    # 2D plot
    mpl.subplot(122)
    mpl.plot(f0, x=(t[0], t[-1]))

    mpl.plt.suptitle("datetime64")
    mpl.auto_adjust_subplots(subplot_params=dict(top=0.9))
    mpl.plt.savefig(next_plot_fname(__file__))
    if args.show:
        mpl.show()
    mpl.plt.close(fig)

    ### plots with a timedelta64 axis
    tL = tL - t[0]
    tR = tR - t[0]
    t = t - t[0]
    f0 = viscid.ones([t, y], crd_names="ty", center="node")
    T, Y = f0.get_crds(shaped=True)
    f0.data += np.arange(T.size).reshape(T.shape)
    f0.data += np.cos(Y)

    fig = mpl.plt.figure(figsize=(10, 5))
    # 1D plot
    mpl.subplot(121)
    mpl.plot(f0[tL:tR]["y=0"], marker="^")
    mpl.plt.xlim(*viscid.as_datetime(t[[0, -1]]).tolist())
    # 2D plot
    mpl.subplot(122)
    mpl.plot(f0, x=(t[0], t[-1]), y=(y[0], y[-1]))

    mpl.plt.suptitle("timedelta64")
    mpl.auto_adjust_subplots(subplot_params=dict(top=0.9))
    mpl.plt.savefig(next_plot_fname(__file__))
    if args.show:
        mpl.show()
    mpl.plt.close(fig)

    return 0
Esempio n. 4
0
 def basetime(self):
     basetime = self.find_info('basetime', default=None)
     if basetime is None:
         s = ("Node {0} did not set basetime, so it can't deal with "
              "datetimes. Maybe you forgot to set basetime, or your "
              "logfile is missing or mangled.".format(repr(self)))
         raise viscid.NoBasetimeError(s)
     return viscid.as_datetime64(basetime)
Esempio n. 5
0
 def dipoletime(self):
     dipoletime = self.find_info('ggcm_dipole_dipoltime', default=None)
     if dipoletime is None:
         raise KeyError("Node {0} did not set ggcm_dipole_dipoltime. Maybe "
                        "your logfile is missing or mangled."
                        "".format(repr(self)))
     dipoletime = ":".join((dipoletime))
     return viscid.as_datetime64(dipoletime)
Esempio n. 6
0
 def time(self, val):
     if viscid.is_datetime_like(val):
         val = viscid.as_timedelta(self.basetime - viscid.as_datetime64(val))
         val = val.total_seconds()
     elif viscid.is_timedelta_like(val, conservative=True):
         val = viscid.as_timedelta(val).total_seconds()
     elif val is not None:
         self.set_info('time', float(val))
Esempio n. 7
0
 def basetime(self):
     basetime = self.find_info('basetime', default=None)
     if basetime is None:
         s = ("Node {0} did not set basetime, so it can't deal with "
              "datetimes. Maybe you forgot to set basetime, or your "
              "logfile is missing or mangled.".format(repr(self)))
         raise viscid.NoBasetimeError(s)
     return viscid.as_datetime64(basetime)
Esempio n. 8
0
 def time(self, val):
     if viscid.is_datetime_like(val):
         val = viscid.as_timedelta(self.basetime -
                                   viscid.as_datetime64(val))
         val = val.total_seconds()
     elif viscid.is_timedelta_like(val, conservative=True):
         val = viscid.as_timedelta(val).total_seconds()
     elif val is not None:
         self.set_info('time', float(val))
Esempio n. 9
0
def convert_timelike(value):
    if value in (None, ''):
        return None
    elif viscid.is_datetime_like(value, conservative=True):
        return viscid.as_datetime64(value)
    elif viscid.is_timedelta_like(value, conservative=True):
        return viscid.as_timedelta64(value)
    else:
        return value
Esempio n. 10
0
 def parse_timestring(timestr):
     prefix = 'time='
     timestr.strip()
     if not timestr.startswith(prefix):
         raise ValueError("Time string '{0}' is malformed".format(timestr))
     timestr = timestr[len(prefix):].split()
     t = float(timestr[0])
     uttime = viscid.as_datetime64(timestr[2])
     basetime = uttime - viscid.as_timedelta64(t, 's')
     basetime = viscid.time_as_seconds(basetime, 1)
     return basetime, uttime
Esempio n. 11
0
    def as_floating_t(self, t, none_passthrough=False):
        t_as_s = None
        try:
            t = vutil.str_to_value(t)

            if viscid.is_timedelta_like(t, conservative=True):
                t_as_s = viscid.as_timedelta(t).total_seconds()
            elif viscid.is_datetime_like(t, conservative=True):
                delta_t = viscid.as_datetime64(t) - self.basetime
                t_as_s = viscid.as_timedelta(delta_t).total_seconds()
            elif not isinstance(t, (int, np.integer, type(None))):
                t_as_s = float(t)
        except AttributeError:
            if t is None:
                if none_passthrough:
                    pass
                else:
                    t = 0.0
            else:
                t_as_s = float(t)

        return t_as_s
Esempio n. 12
0
    def as_floating_t(self, t, none_passthrough=False):
        t_as_s = None
        try:
            t = vutil.str_to_value(t)

            if viscid.is_timedelta_like(t, conservative=True):
                t_as_s = viscid.as_timedelta(t).total_seconds()
            elif viscid.is_datetime_like(t, conservative=True):
                delta_t = viscid.as_datetime64(t) - self.basetime
                t_as_s = viscid.as_timedelta(delta_t).total_seconds()
            elif not isinstance(t, (int, np.integer, type(None))):
                t_as_s = float(t)
        except AttributeError:
            if t is None:
                if none_passthrough:
                    pass
                else:
                    t = 0.0
            else:
                t_as_s = float(t)

        return t_as_s
Esempio n. 13
0
def _main():
    fld = viscid.mfield[-4:4:24j, -5:5:16j, -6:6:20j]
    fld_f = viscid.scalar_fields_to_vector([fld, fld, fld], layout='flat')
    fld_i = fld_f.as_layout('interlaced')

    check_nd_sel(fld_f,
                 np.s_[...],
                 good_sel_list=np.s_[:, :, :],
                 good_sel_names=['x', 'y', 'z'],
                 good_sel_newmask=[False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[0],
                 good_sel_list=np.s_[0, :, :],
                 good_sel_names=['x', 'y', 'z'],
                 good_sel_newmask=[False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[..., :3j],
                 good_sel_list=np.s_[:, :, :3j],
                 good_sel_names=['x', 'y', 'z'],
                 good_sel_newmask=[False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[..., None, :3j],
                 good_sel_list=np.s_[:, :, np.newaxis, :3j],
                 good_sel_names=['x', 'y', 'new-x0', 'z'],
                 good_sel_newmask=[False, False, True, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[..., :3j, None],
                 good_sel_list=np.s_[:, :, :3j, np.newaxis],
                 good_sel_names=['x', 'y', 'z', 'new-x0'],
                 good_sel_newmask=[False, False, False, True],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[..., None, :3j, None],
                 good_sel_list=np.s_[:, :, np.newaxis, :3j, np.newaxis],
                 good_sel_names=['x', 'y', 'new-x0', 'z', 'new-x1'],
                 good_sel_newmask=[False, False, True, False, True],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[None, 'u=None', ..., None, :3j, None],
                 good_sel_list=np.s_[np.newaxis, np.newaxis, :, :, np.newaxis,
                                     :3j, np.newaxis],
                 good_sel_names=['new-x0', 'u', 'x', 'y', 'new-x1', 'z', 'new-x2'],
                 good_sel_newmask=[True, True, False, False, True, False, True],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[:3j, ...],
                 good_sel_list=np.s_[:3j, :, :],
                 good_sel_names=['x', 'y', 'z'],
                 good_sel_newmask=[False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[0, ..., :3j],
                 good_sel_list=np.s_[0, :, :3j],
                 good_sel_names=['x', 'y', 'z'],
                 good_sel_newmask=[False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[0, 'u=newaxis', ..., :3j],
                 good_sel_list=np.s_[0, np.newaxis, :, :3j],
                 good_sel_names=['x', 'u', 'y', 'z'],
                 good_sel_newmask=[False, True, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[0, np.newaxis, ..., :3j],
                 good_sel_list=np.s_[0, np.newaxis, :, :3j],
                 good_sel_names=['x', 'new-x0', 'y', 'z'],
                 good_sel_newmask=[False, True, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[0, ..., 'u=newaxis', :3j],
                 good_sel_list=np.s_[0, :, np.newaxis, :3j],
                 good_sel_names=['x', 'y', 'u', 'z'],
                 good_sel_newmask=[False, False, True, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_[0, ..., np.newaxis, :3j],
                 good_sel_list=np.s_[0, :, np.newaxis, :3j],
                 good_sel_names=['x', 'y', 'new-x0', 'z'],
                 good_sel_newmask=[False, False, True, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_['z=4:10, x=:3'],
                 good_sel_list=np.s_[':3', :, '4:10'],
                 good_sel_names=['x', 'y', 'z'],
                 good_sel_newmask=[False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_['z', None, 'u=None', :, :, None, 'z=:3j', None],
                 good_sel_list=np.s_[np.newaxis, np.newaxis, :, :, np.newaxis,
                                     ':3j', np.newaxis],
                 good_sel_names=['new-x0', 'u', 'x', 'y', 'new-x1', 'z', 'new-x2'],
                 good_sel_newmask=[True, True, False, False, True, False, True],
                 good_comp_sel=2,
                 )

    check_nd_sel(fld_f,
                 np.s_['z=4:10, newaxis, x=:3'],
                 good_sel_list=np.s_[':3', :, '4:10', np.newaxis],
                 good_sel_names=['x', 'y', 'z', 'new-x0'],
                 good_sel_newmask=[False, False, False, True],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_['z=4:10, u=newaxis, x=:3'],
                 good_sel_list=np.s_[':3', :, '4:10', np.newaxis],
                 good_sel_names=['x', 'y', 'z', 'u'],
                 good_sel_newmask=[False, False, False, True],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_['u=newaxis, z=4:10, x=:3'],
                 good_sel_list=np.s_[np.newaxis, ':3', :, '4:10'],
                 good_sel_names=['u', 'x', 'y', 'z'],
                 good_sel_newmask=[True, False, False, False],
                 good_comp_sel=slice(None),
                 )

    check_nd_sel(fld_f,
                 np.s_['z=4:10, x=:3, u=newaxis'],
                 good_sel_list=np.s_[':3', :, '4:10', np.newaxis],
                 good_sel_names=['x', 'y', 'z', 'u'],
                 good_sel_newmask=[False, False, False, True],
                 good_comp_sel=slice(None),
                 )

    check_slc_val('UT2010-01-01', np.datetime64('2010-01-01'))
    check_slc_val('T2010-01-01:T60:12:01.0', slice(viscid.as_datetime64('2010-01-01'),
                                                   viscid.as_timedelta64('60:12:01.0')))
    check_slc_val('[2010-01-01, 2011-02-02]', viscid.as_datetime64(['2010-01-01',
                                                                    '2011-02-02']))
    check_slc_val('[T31, 12:20.1]', viscid.as_timedelta64(['31', '12:20.1']))
    check_slc_val('[T31, T12]', viscid.as_timedelta64(['31', '12']))
    check_slc_val('13j:12:3', np.s_[13j:12:3])
    check_slc_val(np.s_[13j:12:3], np.s_[13j:12:3])
    check_slc_val(np.s_[13j:'T12':3], np.s_[13j:viscid.as_timedelta64(12):3])
    check_slc_val(np.s_[13j:'2010-01-01':3],
                  np.s_[13j:viscid.as_datetime64('2010-01-01'):3])
    check_slc_val(np.s_[13j:'None':3], np.s_[13j:None:3])

    # ret = viscid.std_sel2index(np.s_[:3], x, np.s_[:3])
    x = np.array([-1, -0.5, 0, 0.5, 1])

    # start, step > 0
    check_sbv(np.s_[-1.0j:], x, np.s_[0:])
    check_sbv(np.s_[-0.9999999j:], x, np.s_[0:])
    check_sbv(np.s_[-0.9999j:], x, np.s_[1:])
    check_sbv(np.s_[-0.8j:], x, np.s_[1:])
    check_sbv(np.s_[-0.6j:], x, np.s_[1:])
    check_sbv(np.s_[0.5j:], x, np.s_[3:])
    # start, step < 0
    check_sbv(np.s_[-1.0j::-1], x, np.s_[0::-1])
    check_sbv(np.s_[-0.51j::-1], x, np.s_[0::-1])
    check_sbv(np.s_[-0.50000001j::-1], x, np.s_[1::-1])
    check_sbv(np.s_[-0.5j::-1], x, np.s_[1::-1])
    check_sbv(np.s_[-0.4j::-1], x, np.s_[1::-1])
    check_sbv(np.s_[-0.6j::-1], x, np.s_[0::-1])
    check_sbv(np.s_[0.5j::-1], x, np.s_[3::-1])
    # stop, step > 0
    check_sbv(np.s_[:-1.1j], x, np.s_[:0])
    check_sbv(np.s_[:-1j], x, np.s_[:1])
    check_sbv(np.s_[:-0.51j], x, np.s_[:1])
    check_sbv(np.s_[:-0.5000001j], x, np.s_[:2])
    check_sbv(np.s_[:-0.5j], x, np.s_[:2])
    check_sbv(np.s_[:-0.48j], x, np.s_[:2])
    # stop, step < 0
    check_sbv(np.s_[:-1.0j:-1], x, np.s_[::-1])
    check_sbv(np.s_[:-0.51j:-1], x, np.s_[:0:-1])
    check_sbv(np.s_[:-0.5j:-1], x, np.s_[:0:-1])
    check_sbv(np.s_[:-0.499999j:-1], x, np.s_[:0:-1])
    check_sbv(np.s_[:-0.49j:-1], x, np.s_[:1:-1])
    # length-zero slice
    check_sbv(np.s_[:-10j:1], x, np.s_[:0:1])
    check_sbv(np.s_[:10j:-1], x, np.s_[:5:-1])

    crds = fld.crds
    check_crd_slice(crds, np.s_[-2j:2j, 0j:, :0j],
                    (-1.91304348, 0.33333, -6.0),
                    (1.91304348, 5.0, -0.31578947),
                    (12, 8, 10),
                    ('x', 'y', 'z')
                    )

    check_crd_slice(crds, np.s_[-2j:2j, 0j, :0j],
                    (-1.91304348, -6.0),
                    (1.91304348, -0.31578947),
                    (12, 10),
                    ('x', 'z')
                    )

    check_crd_slice(crds, np.s_[-2j:2j, 0j, :0j],
                    (-1.91304348, 0.33333, -6.0),
                    (1.91304348, 0.33333, -0.31578947),
                    (12, 1, 10),
                    ('x', 'y', 'z'),
                    variant='keep'
                    )

    check_crd_slice(crds, np.s_[-2j:2j, 0j, 'w=newaxis', :0j],
                    (-1.91304348, 0.33333, 0.0, -6.0),
                    (1.91304348, 0.33333, 0.0, -0.31578947),
                    (12, 1, 1, 10),
                    ('x', 'y', 'w', 'z'),
                    variant='keep'
                    )

    check_crd_slice(crds.slice_and_keep(np.s_[0j]), np.s_[..., 0j],
                    (-5.0,), (5.0,), (16,), ('y'),
                    variant='reduce'
                    )

    check_fld_slice(fld, np.s_[-2j:2j, 0j:, :0j],
                    (-1.91304348, 0.33333, -6.0),
                    (1.91304348, 5.0, -0.31578947),
                    (12, 8, 10),
                    ('x', 'y', 'z')
                    )

    check_fld_slice(fld, np.s_[-2j:2j, 0j:, 0j],
                    (-1.91304348, 0.33333, -0.3157894),
                    (1.91304348, 5.0, -0.31578947),
                    (12, 8, 1),
                    ('x', 'y', 'z'),
                    variant='keep'
                    )

    check_fld_slice(fld.slice_and_keep(np.s_[:, 5j]),
                    np.s_[-2j:2j, :, 0j],
                    (-1.91304348,),
                    (1.91304348,),
                    (12,),
                    ('x',),
                    variant='reduce'
                    )

    # fld = viscid.mfield[-4:4:24j, -5:5:16j, -6:6:20j]
    x = fld.get_crd('x')

    # check slice-by-in-array
    xslc = [1, 3, 5, 7]

    assert np.allclose(fld[xslc].get_crd('x'), x[xslc])
    assert np.allclose(fld[np.array(xslc)].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(xslc)].get_crd('x'), x[xslc])

    bool_arr = np.zeros((fld.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld[list(bool_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[bool_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(bool_arr)].get_crd('x'), x[xslc])

    val_arr = 1j * x[xslc]
    assert np.allclose(fld[list(val_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[val_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(val_arr)].get_crd('x'), x[xslc])

    assert isinstance(fld[list(val_arr)].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld[val_arr].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld[str(val_arr)].crds, viscid.coordinate.UniformCrds)

    # will turn uniform into non-uniform
    xslc = [1, 3, 5, 8]

    assert np.allclose(fld[xslc].get_crd('x'), x[xslc])
    assert np.allclose(fld[np.array(xslc)].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(xslc)].get_crd('x'), x[xslc])

    bool_arr = np.zeros((fld.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld[list(bool_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[bool_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(bool_arr)].get_crd('x'), x[xslc])

    val_arr = 1j * x[xslc]
    assert np.allclose(fld[list(val_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[val_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(val_arr)].get_crd('x'), x[xslc])

    assert isinstance(fld[list(val_arr)].crds, viscid.coordinate.NonuniformCrds)
    assert isinstance(fld[val_arr].crds, viscid.coordinate.NonuniformCrds)
    assert isinstance(fld[str(val_arr)].crds, viscid.coordinate.NonuniformCrds)

    ########### cell centered....
    fld_cc = fld.as_centered('cell')

    x_cc = fld_cc.get_crd('x')

    # check slice-by-in-array
    xslc = [1, 3, 5, 7]

    assert np.allclose(fld_cc[xslc].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[np.array(xslc)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(xslc)].get_crd('x'), x_cc[xslc])

    bool_arr = np.zeros((fld_cc.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld_cc[list(bool_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[bool_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(bool_arr)].get_crd('x'), x_cc[xslc])

    val_arr = 1j * x_cc[xslc]
    assert np.allclose(fld_cc[list(val_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[val_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(val_arr)].get_crd('x'), x_cc[xslc])

    assert isinstance(fld_cc[list(val_arr)].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld_cc[val_arr].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld_cc[str(val_arr)].crds, viscid.coordinate.UniformCrds)

    # will turn uniform into non-uniform
    xslc = [1, 3, 5, 8]

    assert np.allclose(fld_cc[xslc].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[np.array(xslc)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(xslc)].get_crd('x'), x_cc[xslc])

    bool_arr = np.zeros((fld_cc.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld_cc[list(bool_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[bool_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(bool_arr)].get_crd('x'), x_cc[xslc])

    val_arr = 1j * x_cc[xslc]
    assert np.allclose(fld_cc[list(val_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[val_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(val_arr)].get_crd('x'), x_cc[xslc])

    assert isinstance(fld_cc[list(val_arr)].crds, viscid.coordinate.NonuniformCrds)
    assert isinstance(fld_cc[val_arr].crds, viscid.coordinate.NonuniformCrds)
    assert isinstance(fld_cc[str(val_arr)].crds, viscid.coordinate.NonuniformCrds)

    return 0
Esempio n. 14
0
 def basetime(self, val):
     self.set_info('basetime', viscid.as_datetime64(val))
Esempio n. 15
0
 def basetime(self, val):
     self.set_info('basetime', viscid.as_datetime64(val))
Esempio n. 16
0
def _main():
    fld = viscid.mfield[-4:4:24j, -5:5:16j, -6:6:20j]
    fld_f = viscid.scalar_fields_to_vector([fld, fld, fld], layout='flat')
    fld_i = fld_f.as_layout('interlaced')

    check_nd_sel(
        fld_f,
        np.s_[...],
        good_sel_list=np.s_[:, :, :],
        good_sel_names=['x', 'y', 'z'],
        good_sel_newmask=[False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[0],
        good_sel_list=np.s_[0, :, :],
        good_sel_names=['x', 'y', 'z'],
        good_sel_newmask=[False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[..., :3j],
        good_sel_list=np.s_[:, :, :3j],
        good_sel_names=['x', 'y', 'z'],
        good_sel_newmask=[False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[..., None, :3j],
        good_sel_list=np.s_[:, :, np.newaxis, :3j],
        good_sel_names=['x', 'y', 'new-x0', 'z'],
        good_sel_newmask=[False, False, True, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[..., :3j, None],
        good_sel_list=np.s_[:, :, :3j, np.newaxis],
        good_sel_names=['x', 'y', 'z', 'new-x0'],
        good_sel_newmask=[False, False, False, True],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[..., None, :3j, None],
        good_sel_list=np.s_[:, :, np.newaxis, :3j, np.newaxis],
        good_sel_names=['x', 'y', 'new-x0', 'z', 'new-x1'],
        good_sel_newmask=[False, False, True, False, True],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[None, 'u=None', ..., None, :3j, None],
        good_sel_list=np.s_[np.newaxis, np.newaxis, :, :, np.newaxis, :3j,
                            np.newaxis],
        good_sel_names=['new-x0', 'u', 'x', 'y', 'new-x1', 'z', 'new-x2'],
        good_sel_newmask=[True, True, False, False, True, False, True],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[:3j, ...],
        good_sel_list=np.s_[:3j, :, :],
        good_sel_names=['x', 'y', 'z'],
        good_sel_newmask=[False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[0, ..., :3j],
        good_sel_list=np.s_[0, :, :3j],
        good_sel_names=['x', 'y', 'z'],
        good_sel_newmask=[False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[0, 'u=newaxis', ..., :3j],
        good_sel_list=np.s_[0, np.newaxis, :, :3j],
        good_sel_names=['x', 'u', 'y', 'z'],
        good_sel_newmask=[False, True, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[0, np.newaxis, ..., :3j],
        good_sel_list=np.s_[0, np.newaxis, :, :3j],
        good_sel_names=['x', 'new-x0', 'y', 'z'],
        good_sel_newmask=[False, True, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[0, ..., 'u=newaxis', :3j],
        good_sel_list=np.s_[0, :, np.newaxis, :3j],
        good_sel_names=['x', 'y', 'u', 'z'],
        good_sel_newmask=[False, False, True, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_[0, ..., np.newaxis, :3j],
        good_sel_list=np.s_[0, :, np.newaxis, :3j],
        good_sel_names=['x', 'y', 'new-x0', 'z'],
        good_sel_newmask=[False, False, True, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_['z=4:10, x=:3'],
        good_sel_list=np.s_[':3', :, '4:10'],
        good_sel_names=['x', 'y', 'z'],
        good_sel_newmask=[False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_['z', None, 'u=None', :, :, None, 'z=:3j', None],
        good_sel_list=np.s_[np.newaxis, np.newaxis, :, :, np.newaxis, ':3j',
                            np.newaxis],
        good_sel_names=['new-x0', 'u', 'x', 'y', 'new-x1', 'z', 'new-x2'],
        good_sel_newmask=[True, True, False, False, True, False, True],
        good_comp_sel=2,
    )

    check_nd_sel(
        fld_f,
        np.s_['z=4:10, newaxis, x=:3'],
        good_sel_list=np.s_[':3', :, '4:10', np.newaxis],
        good_sel_names=['x', 'y', 'z', 'new-x0'],
        good_sel_newmask=[False, False, False, True],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_['z=4:10, u=newaxis, x=:3'],
        good_sel_list=np.s_[':3', :, '4:10', np.newaxis],
        good_sel_names=['x', 'y', 'z', 'u'],
        good_sel_newmask=[False, False, False, True],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_['u=newaxis, z=4:10, x=:3'],
        good_sel_list=np.s_[np.newaxis, ':3', :, '4:10'],
        good_sel_names=['u', 'x', 'y', 'z'],
        good_sel_newmask=[True, False, False, False],
        good_comp_sel=slice(None),
    )

    check_nd_sel(
        fld_f,
        np.s_['z=4:10, x=:3, u=newaxis'],
        good_sel_list=np.s_[':3', :, '4:10', np.newaxis],
        good_sel_names=['x', 'y', 'z', 'u'],
        good_sel_newmask=[False, False, False, True],
        good_comp_sel=slice(None),
    )

    check_slc_val('UT2010-01-01', np.datetime64('2010-01-01'))
    check_slc_val(
        'T2010-01-01:T60:12:01.0',
        slice(viscid.as_datetime64('2010-01-01'),
              viscid.as_timedelta64('60:12:01.0')))
    check_slc_val('[2010-01-01, 2011-02-02]',
                  viscid.as_datetime64(['2010-01-01', '2011-02-02']))
    check_slc_val('[T31, 12:20.1]', viscid.as_timedelta64(['31', '12:20.1']))
    check_slc_val('[T31, T12]', viscid.as_timedelta64(['31', '12']))
    check_slc_val('13j:12:3', np.s_[13j:12:3])
    check_slc_val(np.s_[13j:12:3], np.s_[13j:12:3])
    check_slc_val(np.s_[13j:'T12':3], np.s_[13j:viscid.as_timedelta64(12):3])
    check_slc_val(np.s_[13j:'2010-01-01':3],
                  np.s_[13j:viscid.as_datetime64('2010-01-01'):3])
    check_slc_val(np.s_[13j:'None':3], np.s_[13j:None:3])

    # ret = viscid.std_sel2index(np.s_[:3], x, np.s_[:3])
    x = np.array([-1, -0.5, 0, 0.5, 1])

    # start, step > 0
    check_sbv(np.s_[-1.0j:], x, np.s_[0:])
    check_sbv(np.s_[-0.9999999j:], x, np.s_[0:])
    check_sbv(np.s_[-0.9999j:], x, np.s_[1:])
    check_sbv(np.s_[-0.8j:], x, np.s_[1:])
    check_sbv(np.s_[-0.6j:], x, np.s_[1:])
    check_sbv(np.s_[0.5j:], x, np.s_[3:])
    # start, step < 0
    check_sbv(np.s_[-1.0j::-1], x, np.s_[0::-1])
    check_sbv(np.s_[-0.51j::-1], x, np.s_[0::-1])
    check_sbv(np.s_[-0.50000001j::-1], x, np.s_[1::-1])
    check_sbv(np.s_[-0.5j::-1], x, np.s_[1::-1])
    check_sbv(np.s_[-0.4j::-1], x, np.s_[1::-1])
    check_sbv(np.s_[-0.6j::-1], x, np.s_[0::-1])
    check_sbv(np.s_[0.5j::-1], x, np.s_[3::-1])
    # stop, step > 0
    check_sbv(np.s_[:-1.1j], x, np.s_[:0])
    check_sbv(np.s_[:-1j], x, np.s_[:1])
    check_sbv(np.s_[:-0.51j], x, np.s_[:1])
    check_sbv(np.s_[:-0.5000001j], x, np.s_[:2])
    check_sbv(np.s_[:-0.5j], x, np.s_[:2])
    check_sbv(np.s_[:-0.48j], x, np.s_[:2])
    # stop, step < 0
    check_sbv(np.s_[:-1.0j:-1], x, np.s_[::-1])
    check_sbv(np.s_[:-0.51j:-1], x, np.s_[:0:-1])
    check_sbv(np.s_[:-0.5j:-1], x, np.s_[:0:-1])
    check_sbv(np.s_[:-0.499999j:-1], x, np.s_[:0:-1])
    check_sbv(np.s_[:-0.49j:-1], x, np.s_[:1:-1])
    # length-zero slice
    check_sbv(np.s_[:-10j:1], x, np.s_[:0:1])
    check_sbv(np.s_[:10j:-1], x, np.s_[:5:-1])

    crds = fld.crds
    check_crd_slice(crds, np.s_[-2j:2j,
                                0j:, :0j], (-1.91304348, 0.33333, -6.0),
                    (1.91304348, 5.0, -0.31578947), (12, 8, 10),
                    ('x', 'y', 'z'))

    check_crd_slice(crds, np.s_[-2j:2j, 0j, :0j], (-1.91304348, -6.0),
                    (1.91304348, -0.31578947), (12, 10), ('x', 'z'))

    check_crd_slice(crds,
                    np.s_[-2j:2j, 0j, :0j], (-1.91304348, 0.33333, -6.0),
                    (1.91304348, 0.33333, -0.31578947), (12, 1, 10),
                    ('x', 'y', 'z'),
                    variant='keep')

    check_crd_slice(crds,
                    np.s_[-2j:2j, 0j,
                          'w=newaxis', :0j], (-1.91304348, 0.33333, 0.0, -6.0),
                    (1.91304348, 0.33333, 0.0, -0.31578947), (12, 1, 1, 10),
                    ('x', 'y', 'w', 'z'),
                    variant='keep')

    check_crd_slice(crds.slice_and_keep(np.s_[0j]),
                    np.s_[..., 0j], (-5.0, ), (5.0, ), (16, ), ('y'),
                    variant='reduce')

    check_fld_slice(fld, np.s_[-2j:2j, 0j:, :0j], (-1.91304348, 0.33333, -6.0),
                    (1.91304348, 5.0, -0.31578947), (12, 8, 10),
                    ('x', 'y', 'z'))

    check_fld_slice(fld,
                    np.s_[-2j:2j, 0j:, 0j], (-1.91304348, 0.33333, -0.3157894),
                    (1.91304348, 5.0, -0.31578947), (12, 8, 1),
                    ('x', 'y', 'z'),
                    variant='keep')

    check_fld_slice(fld.slice_and_keep(np.s_[:, 5j]),
                    np.s_[-2j:2j, :, 0j], (-1.91304348, ), (1.91304348, ),
                    (12, ), ('x', ),
                    variant='reduce')

    # fld = viscid.mfield[-4:4:24j, -5:5:16j, -6:6:20j]
    x = fld.get_crd('x')

    # check slice-by-in-array
    xslc = [1, 3, 5, 7]

    assert np.allclose(fld[xslc].get_crd('x'), x[xslc])
    assert np.allclose(fld[np.array(xslc)].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(xslc)].get_crd('x'), x[xslc])

    bool_arr = np.zeros((fld.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld[list(bool_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[bool_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(bool_arr)].get_crd('x'), x[xslc])

    val_arr = 1j * x[xslc]
    assert np.allclose(fld[list(val_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[val_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(val_arr)].get_crd('x'), x[xslc])

    assert isinstance(fld[list(val_arr)].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld[val_arr].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld[str(val_arr)].crds, viscid.coordinate.UniformCrds)

    # will turn uniform into non-uniform
    xslc = [1, 3, 5, 8]

    assert np.allclose(fld[xslc].get_crd('x'), x[xslc])
    assert np.allclose(fld[np.array(xslc)].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(xslc)].get_crd('x'), x[xslc])

    bool_arr = np.zeros((fld.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld[list(bool_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[bool_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(bool_arr)].get_crd('x'), x[xslc])

    val_arr = 1j * x[xslc]
    assert np.allclose(fld[list(val_arr)].get_crd('x'), x[xslc])
    assert np.allclose(fld[val_arr].get_crd('x'), x[xslc])
    assert np.allclose(fld[str(val_arr)].get_crd('x'), x[xslc])

    assert isinstance(fld[list(val_arr)].crds,
                      viscid.coordinate.NonuniformCrds)
    assert isinstance(fld[val_arr].crds, viscid.coordinate.NonuniformCrds)
    assert isinstance(fld[str(val_arr)].crds, viscid.coordinate.NonuniformCrds)

    ########### cell centered....
    fld_cc = fld.as_centered('cell')

    x_cc = fld_cc.get_crd('x')

    # check slice-by-in-array
    xslc = [1, 3, 5, 7]

    assert np.allclose(fld_cc[xslc].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[np.array(xslc)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(xslc)].get_crd('x'), x_cc[xslc])

    bool_arr = np.zeros((fld_cc.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld_cc[list(bool_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[bool_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(bool_arr)].get_crd('x'), x_cc[xslc])

    val_arr = 1j * x_cc[xslc]
    assert np.allclose(fld_cc[list(val_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[val_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(val_arr)].get_crd('x'), x_cc[xslc])

    assert isinstance(fld_cc[list(val_arr)].crds,
                      viscid.coordinate.UniformCrds)
    assert isinstance(fld_cc[val_arr].crds, viscid.coordinate.UniformCrds)
    assert isinstance(fld_cc[str(val_arr)].crds, viscid.coordinate.UniformCrds)

    # will turn uniform into non-uniform
    xslc = [1, 3, 5, 8]

    assert np.allclose(fld_cc[xslc].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[np.array(xslc)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(xslc)].get_crd('x'), x_cc[xslc])

    bool_arr = np.zeros((fld_cc.shape[0]), dtype=np.bool_)
    bool_arr[xslc] = True
    assert np.allclose(fld_cc[list(bool_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[bool_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(bool_arr)].get_crd('x'), x_cc[xslc])

    val_arr = 1j * x_cc[xslc]
    assert np.allclose(fld_cc[list(val_arr)].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[val_arr].get_crd('x'), x_cc[xslc])
    assert np.allclose(fld_cc[str(val_arr)].get_crd('x'), x_cc[xslc])

    assert isinstance(fld_cc[list(val_arr)].crds,
                      viscid.coordinate.NonuniformCrds)
    assert isinstance(fld_cc[val_arr].crds, viscid.coordinate.NonuniformCrds)
    assert isinstance(fld_cc[str(val_arr)].crds,
                      viscid.coordinate.NonuniformCrds)

    return 0