Beispiel #1
0
 def test_save_load(self):
     t1 = Time(TestTime.time_var)
     fn = 'time.txt'
     t1._write_time_to_file('time.txt')
     t2 = Time.from_file(fn)
     #         pytest.set_trace()
     assert all(t1.time == t2.time)
     os.remove(fn)
Beispiel #2
0
 def test_serialization(self, _json_):
     ts = Time(TestTime.time_var)
     ser = ts.serialize(_json_)
     if _json_ == 'webapi':
         deser = Time.deserialize(ser)
         t2 = Time.new_from_dict(deser)
         assert all(ts.data == t2.data)
         assert 'data' in ser
     else:
         assert 'data' in ser
Beispiel #3
0
    def test_set_time(self, u):
        with pytest.raises(ValueError):
            # mismatched data and time length
            u.time = dates

        u.time = dates2
        assert u.time == Time(dates2)
Beispiel #4
0
    def test_construction(self):

        t1 = Time(TestTime.time_var)
        assert all(TestTime.time_arr == t1.time)

        t2 = Time(TestTime.time_arr)
        assert all(TestTime.time_arr == t2.time)

        t = Time(TestTime.time_var, tz_offset=dt.timedelta(hours=1))
        print TestTime.time_arr
        print t.time
        print TestTime.time_arr[0] + dt.timedelta(hours=1)
        assert t.time[0] == (TestTime.time_arr[0] + dt.timedelta(hours=1))

        t = Time(TestTime.time_arr.copy(), tz_offset=dt.timedelta(hours=1))
        assert t.time[0] == TestTime.time_arr[0] + dt.timedelta(hours=1)
Beispiel #5
0
 def time(self, t):
     if self.data is not None and len(t) != len(self.data):
         raise ValueError("Data/time interval mismatch")
     if isinstance(t, Time):
         self._time = t
     elif isinstance(t, collections.Iterable):
         self._time = Time(t)
     else:
         raise ValueError(
             "Object being assigned must be an iterable or a Time object")
Beispiel #6
0
 def time(self, t):
     if self.variables is not None:
         for v in self.variables:
             v.time = t
     if isinstance(t, Time):
         self._time = t
     elif isinstance(t, collections.Iterable):
         self._time = Time(t)
     else:
         raise ValueError(
             "Object being assigned must be an iterable or a Time object")
Beispiel #7
0
    def constant(cls, name=None, units=None, variables=None):
        if any(var is None for var in (name, variables, units)):
            raise ValueError("name, variables, or units may not be None")

        if not isinstance(variables, collections.Iterable):
            raise TypeError('{0} variables must be an iterable'.format(name))
        t = Time.constant_time()
        return cls(name=name,
                   units=units,
                   time=t,
                   variables=[v for v in variables])
Beispiel #8
0
    def constant(
        cls,
        name=None,
        units=None,
        data=None,
    ):
        if any(var is None for var in (name, data, units)):
            raise ValueError("name, data, or units may not be None")

        if not isinstance(data, Number):
            raise TypeError('{0} data must be a number'.format(name))
        t = Time.constant_time()
        return cls(name=name, units=units, time=t, data=[data])
Beispiel #9
0
    def test_construction(self):

        u = None
        v = None
        with pytest.raises(ValueError):
            # mismatched data and dates length
            u = TimeSeriesProp('u', 'm/s', dates, u_data)

        assert u is None

        u = TimeSeriesProp('u', 'm/s', dates2, u_data)

        assert u is not None
        assert u.name == 'u'
        assert u.units == 'm/s'
        print u.time == Time(dates2)
        assert u.time == Time(dates2)
        assert (u.data == u_data).all()

        v = None
        with pytest.raises(ValueError):
            v = TimeSeriesProp('v', 'nm/hr', dates2, v_data)

        assert v is None
Beispiel #10
0
 def test_construction(self, gp, gp2):
     #Grid_file and data_file missing
     with pytest.raises(ValueError):
         gvp = GridVectorProp(name='velocity',
                              units='m/s',
                              time=grid_time,
                              variables=[grid_u, grid_v])
     #Units inconsistent with variables units
     with pytest.raises(ValueError):
         gvp = GridVectorProp(name='velocity',
                              units='km/hr',
                              time=grid_time,
                              variables=[gp, gp2])
     gvp = GridVectorProp(name='velocity',
                          units='m/s',
                          time=grid_time,
                          variables=[gp, gp2])
     assert gvp.name == 'velocity'
     assert gvp.units == 'm/s'
     assert gvp.time == Time(grid_time)
     assert gvp._variables[0].data == grid_u
     assert gvp._variables[0].name == 'u'
Beispiel #11
0
 def __init__(self, angle=None, **kwargs):
     """
     :param angle: scalar field of cell rotation angles (for rotated/distorted grids)
     """
     if 'variables' in kwargs:
         variables = kwargs['variables']
         if len(variables) == 2:
             variables.append(TimeSeriesProp(name='constant w', data=[0.0], time=Time.constant_time(), units='m/s'))
         kwargs['variables'] = variables
     if angle is None:
         df = None
         if kwargs.get('dataset', None) is not None:
             df = kwargs['dataset']
         elif kwargs.get('grid_file', None) is not None:
             df = _get_dataset(kwargs['grid_file'])
         if df is not None and 'angle' in df.variables.keys():
             # Unrotated ROMS Grid!
             self.angle = GriddedProp(name='angle', units='radians', time=None, grid=kwargs['grid'], data=df['angle'])
         else:
             self.angle = None
     else:
         self.angle = angle
     super(VelocityGrid, self).__init__(**kwargs)
Beispiel #12
0
 def test_offset(self):
     t = Time(dates2.copy(), tz_offset=dt.timedelta(hours=1))
     assert t.time[0] == dt.datetime(2000, 1, 1, 1)
Beispiel #13
0
    def test_construction(self):
        t1 = Time(dates2)
        assert all(dates2 == t1.time)

        t2 = Time(grid_time)
        assert len(t2.time) == 54
Beispiel #14
0
def ts():
    return Time(dates2)
Beispiel #15
0
 def test_extrapolation(self):
     ts = Time(TestTime.time_var)
     before = TestTime.time_arr[0] - dt.timedelta(hours=1)
     after = TestTime.time_arr[-1] + dt.timedelta(hours=1)
     assert ts.index_of(before, True) == 0
     assert ts.index_of(after, True) == 11
     assert ts.index_of(ts.time[-1], True) == 10
     assert ts.index_of(ts.time[0], True) == 0
     with pytest.raises(ValueError):
         ts.index_of(before, False)
     with pytest.raises(ValueError):
         ts.index_of(after, False)
     assert ts.index_of(ts.time[-1], True) == 10
     assert ts.index_of(ts.time[0], True) == 0