コード例 #1
0
 def test_vector_t1_and_t2(self):
     field = self._field()
     field.lbtim = 11
     t1 = ([netcdftime.datetime(1970, 1, 2, 6),
            netcdftime.datetime(1970, 1, 2, 9),
            netcdftime.datetime(1970, 1, 2, 12)], [1])
     t2 = ([netcdftime.datetime(1970, 1, 1, 12),
            netcdftime.datetime(1970, 1, 2, 0)], [0])
     collation = mock.Mock(fields=[field], vector_dims_shape=(2, 3),
                           element_arrays_and_dims={'t1': t1, 't2': t2})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 3),
                        (LATITUDE, 2),
                        (iris.coords.DimCoord([30, 33, 36], 'time',
                                              units='hours since epoch'),
                         (1,)),
                        (iris.coords.DimCoord([12, 24],
                                              'forecast_reference_time',
                                              units='hours since epoch'),
                         (0,))]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.AuxCoord([[18, 21, 24], [6, 9, 12]],
                               'forecast_period', units='hours'), (0, 1))
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #2
0
ファイル: test__convert_collation.py プロジェクト: jcmt/iris
 def test_vector_lbft(self):
     field = self._field()
     field.lbtim = 21
     field.t1 = netcdftime.datetime(1970, 1, 1, 12)
     field.t2 = netcdftime.datetime(1970, 1, 1, 18)
     lbft = ([18, 15, 12], [0])
     collation = mock.Mock(fields=[field], vector_dims_shape=(3,),
                           element_arrays_and_dims={'lbft': lbft})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 2),
                        (LATITUDE, 1),
                        (iris.coords.DimCoord([0, 3, 6],
                                              'forecast_reference_time',
                                              units='hours since epoch'),
                         (0,))]
     coords_and_dims = [
         (iris.coords.DimCoord(15, 'time', units='hours since epoch',
                               bounds=[[12, 18]]), None),
         (iris.coords.DimCoord([15, 12, 9], 'forecast_period',
                               units='hours',
                               bounds=[[12, 18], [9, 15], [6, 12]]),
          (0,))
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #3
0
 def test_vector_t1(self):
     field = self._field()
     field.lbtim = 11
     field.t2 = netcdftime.datetime(1970, 1, 1, 12)
     t1 = ([netcdftime.datetime(1970, 1, 1, 18),
            netcdftime.datetime(1970, 1, 2, 0),
            netcdftime.datetime(1970, 1, 2, 6)], [0])
     collation = mock.Mock(fields=[field], vector_dims_shape=(3,),
                           element_arrays_and_dims={'t1': t1})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 2),
                        (LATITUDE, 1),
                        (iris.coords.DimCoord([18, 24, 30], 'time',
                                              units='hours since epoch'),
                         (0,))
                        ]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.DimCoord(12, 'forecast_reference_time',
                               units='hours since epoch'), None),
         (iris.coords.DimCoord([6, 12, 18], 'forecast_period',
                               units='hours'), (0,))
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #4
0
ファイル: test__convert_collation.py プロジェクト: jcmt/iris
 def test_vector_t1_and_t2(self):
     field = self._field()
     field.lbtim = 11
     t1 = ([netcdftime.datetime(1970, 1, 2, 6),
            netcdftime.datetime(1970, 1, 2, 9),
            netcdftime.datetime(1970, 1, 2, 12)], [1])
     t2 = ([netcdftime.datetime(1970, 1, 1, 12),
            netcdftime.datetime(1970, 1, 2, 0)], [0])
     collation = mock.Mock(fields=[field], vector_dims_shape=(2, 3),
                           element_arrays_and_dims={'t1': t1, 't2': t2})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 3),
                        (LATITUDE, 2),
                        (iris.coords.DimCoord([30, 33, 36], 'time',
                                              units='hours since epoch'),
                         (1,)),
                        (iris.coords.DimCoord([12, 24],
                                              'forecast_reference_time',
                                              units='hours since epoch'),
                         (0,))]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.AuxCoord([[18, 21, 24], [6, 9, 12]],
                               'forecast_period', units='hours'), (0, 1))
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #5
0
ファイル: common.py プロジェクト: jonseddon/primavera-dmt
def calc_last_day_in_month(year, month, calendar):
    """
    Calculate the last day of the specified month using the calendar given.

    :param int year: The year
    :param int month: The month
    :param str calendar: The calendar to use, which must be supported by
        cf_units
    :returns: The last day of the specified month
    :rtype: int
    """
    ref_units = 'days since 1969-07-21'

    if month == 12:
        start_next_month_obj = netcdftime.datetime(year + 1, 1, 1)
    else:
        start_next_month_obj = netcdftime.datetime(year, month + 1, 1)

    start_next_month = cf_units.date2num(start_next_month_obj, ref_units,
                                         calendar)

    end_this_month = cf_units.num2date(start_next_month - 1, ref_units,
                                       calendar)

    return end_this_month.day
コード例 #6
0
def harmonize_hourly_timestamp(time_range, dt):
    '''
    Adjust the ``time_range`` by setting hour value to datetime.datetime objects.
    
    :param time_range: time range selected by user   
    :type time_range: list of two datetime.datetime objects
    
    :param dt: any datetime step of input datetime vector
    :type dt: datetime.datetime object
    
    :rtype: list of two datetime.datetime objects
    
    WHY:
    if input time steps vector is from 1990-01-01 12:00 to 2000-12-31 12:00,
    and user's time_range is [datetime.datetime(1900, 1, 1), datetime.datetime(1905, 12, 31)],
    i.e. [datetime.datetime(1900, 1, 1, 0, 0), datetime.datetime(1905, 12, 31, 0, 0)],
    it will be not included in the input time steps (there will be the error message "The time range is not included in the input time steps array.").
    Thus, this function will adjust the hour of the user's time_range: [datetime.datetime(1900, 1, 1, 12, 0), datetime.datetime(1905, 12, 31, 12, 0)]

    '''

    #     time_range_begin = datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
    #     time_range_end = datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)

    time_range_begin = netcdftime.datetime(time_range[0].year,
                                           time_range[0].month,
                                           time_range[0].day, dt.hour)
    time_range_end = netcdftime.datetime(time_range[1].year,
                                         time_range[1].month,
                                         time_range[1].day, dt.hour)

    return [time_range_begin, time_range_end]
コード例 #7
0
 def test_vector_lbft(self):
     field = self._field()
     field.lbtim = 21
     field.t1 = netcdftime.datetime(1970, 1, 1, 12)
     field.t2 = netcdftime.datetime(1970, 1, 1, 18)
     lbft = ([18, 15, 12], [0])
     collation = mock.Mock(fields=[field], vector_dims_shape=(3,),
                           element_arrays_and_dims={'lbft': lbft})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 2),
                        (LATITUDE, 1),
                        (iris.coords.DimCoord([0, 3, 6],
                                              'forecast_reference_time',
                                              units='hours since epoch'),
                         (0,))]
     coords_and_dims = [
         (iris.coords.DimCoord(15, 'time', units='hours since epoch',
                               bounds=[[12, 18]]), None),
         (iris.coords.DimCoord([15, 12, 9], 'forecast_period',
                               units='hours',
                               bounds=[[12, 18], [9, 15], [6, 12]]),
          (0,))
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #8
0
ファイル: test__convert_collation.py プロジェクト: jcmt/iris
 def test_vector_t1(self):
     field = self._field()
     field.lbtim = 11
     field.t2 = netcdftime.datetime(1970, 1, 1, 12)
     t1 = ([netcdftime.datetime(1970, 1, 1, 18),
            netcdftime.datetime(1970, 1, 2, 0),
            netcdftime.datetime(1970, 1, 2, 6)], [0])
     collation = mock.Mock(fields=[field], vector_dims_shape=(3,),
                           element_arrays_and_dims={'t1': t1})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 2),
                        (LATITUDE, 1),
                        (iris.coords.DimCoord([18, 24, 30], 'time',
                                              units='hours since epoch'),
                         (0,))
                        ]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.DimCoord(12, 'forecast_reference_time',
                               units='hours since epoch'), None),
         (iris.coords.DimCoord([6, 12, 18], 'forecast_period',
                               units='hours'), (0,))
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #9
0
ファイル: test_pandas.py プロジェクト: vt100/iris
 def test_time_360(self):
     cube = Cube(np.array([0, 1, 2, 3, 4]), long_name="ts")
     time_unit = cf_units.Unit("days since 2000-01-01 00:00",
                               calendar=cf_units.CALENDAR_360_DAY)
     time_coord = DimCoord([0, 100.1, 200.2, 300.3, 400.4],
                           long_name="time",
                           units=time_unit)
     cube.add_dim_coord(time_coord, 0)
     if netCDF4.__version__ > '1.2.4':
         expected_index = [
             netcdftime.Datetime360Day(2000, 1, 1, 0, 0),
             netcdftime.Datetime360Day(2000, 4, 11, 2, 24),
             netcdftime.Datetime360Day(2000, 7, 21, 4, 48),
             netcdftime.Datetime360Day(2000, 11, 1, 7, 12),
             netcdftime.Datetime360Day(2001, 2, 11, 9, 36)
         ]
     else:
         expected_index = [
             netcdftime.datetime(2000, 1, 1, 0, 0),
             netcdftime.datetime(2000, 4, 11, 2, 24),
             netcdftime.datetime(2000, 7, 21, 4, 48),
             netcdftime.datetime(2000, 11, 1, 7, 12),
             netcdftime.datetime(2001, 2, 11, 9, 36)
         ]
     series = iris.pandas.as_series(cube)
     self.assertArrayEqual(series, cube.data)
     self.assertArrayEqual(series.index, expected_index)
コード例 #10
0
ファイル: test_convert.py プロジェクト: vt100/iris
    def test_365_calendar(self):
        f = mock.MagicMock(lbtim=SplittableInt(4, {
            'ia': 2,
            'ib': 1,
            'ic': 0
        }),
                           lbyr=2013,
                           lbmon=1,
                           lbdat=1,
                           lbhr=12,
                           lbmin=0,
                           lbsec=0,
                           t1=netcdftime.datetime(2013, 1, 1, 12, 0, 0),
                           t2=netcdftime.datetime(2013, 1, 2, 12, 0, 0),
                           spec=PPField3)
        f.time_unit = six.create_bound_method(PPField3.time_unit, f)
        f.calendar = cf_units.CALENDAR_365_DAY
        (factories, references, standard_name, long_name, units, attributes,
         cell_methods, dim_coords_and_dims, aux_coords_and_dims) = convert(f)

        def is_t_coord(coord_and_dims):
            coord, dims = coord_and_dims
            return coord.standard_name == 'time'

        coords_and_dims = list(filter(is_t_coord, aux_coords_and_dims))
        self.assertEqual(len(coords_and_dims), 1)
        coord, dims = coords_and_dims[0]
        self.assertEqual(guess_coord_axis(coord), 'T')
        self.assertEqual(coord.units.calendar, '365_day')
コード例 #11
0
 def test_vector_t2(self):
     field = self._field()
     field.lbtim = 11
     field.t1 = netcdftime.datetime(1970, 1, 1, 18)
     t2 = (
         [
             netcdftime.datetime(1970, 1, 1, 12),
             netcdftime.datetime(1970, 1, 1, 15),
             netcdftime.datetime(1970, 1, 1, 18),
         ],
         [0],
     )
     collation = mock.Mock(fields=[field], vector_dims_shape=(3,), element_arrays_and_dims={"t2": t2})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [
         (LONGITUDE, 2),
         (LATITUDE, 1),
         (iris.coords.DimCoord([12, 15, 18], "forecast_reference_time", units="hours since epoch"), (0,)),
     ]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.DimCoord(18, "time", units="hours since epoch"), None),
         (iris.coords.DimCoord([6, 3, 0.0], "forecast_period", units="hours"), (0,)),
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #12
0
ファイル: util_dt.py プロジェクト: andrejsim/icclim
def harmonize_hourly_timestamp(time_range, dt):
    '''
    Adjust the ``time_range`` by setting hour value to datetime.datetime objects.
    
    :param time_range: time range selected by user   
    :type time_range: list of two datetime.datetime objects
    
    :param dt: any datetime step of input datetime vector
    :type dt: datetime.datetime object
    
    :rtype: list of two datetime.datetime objects
    
    WHY:
    if input time steps vector is from 1990-01-01 12:00 to 2000-12-31 12:00,
    and user's time_range is [datetime.datetime(1900, 1, 1), datetime.datetime(1905, 12, 31)],
    i.e. [datetime.datetime(1900, 1, 1, 0, 0), datetime.datetime(1905, 12, 31, 0, 0)],
    it will be not included in the input time steps (there will be the error message "The time range is not included in the input time steps array.").
    Thus, this function will adjust the hour of the user's time_range: [datetime.datetime(1900, 1, 1, 12, 0), datetime.datetime(1905, 12, 31, 12, 0)]

    '''

#     time_range_begin = datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
#     time_range_end = datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)

    time_range_begin = netcdftime.datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
    time_range_end = netcdftime.datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    
    return [time_range_begin, time_range_end]
コード例 #13
0
ファイル: test_FieldCollation.py プロジェクト: Jozhogg/iris
 def test_t1(self):
     collation = FieldCollation([_make_field(lbyr=2013),
                                 _make_field(lbyr=2014)])
     result = collation.element_arrays_and_dims
     self.assertEqual(list(result.keys()), ['t1'])
     values, dims = result['t1']
     self.assertArrayEqual(values, [datetime(2013, 1, 1),
                                    datetime(2014, 1, 1)])
     self.assertEqual(dims, (0,))
コード例 #14
0
ファイル: test__fixup_dates.py プロジェクト: cpelley/iris
 def test_365_day_calendar(self):
     unit = Unit('minutes since 2000-02-25 00:00:00', calendar='365_day')
     coord = AuxCoord([30, 60, 150], 'time', units=unit)
     result = _fixup_dates(coord, coord.points)
     expected_datetimes = [netcdftime.datetime(2000, 2, 25, 0, 30),
                           netcdftime.datetime(2000, 2, 25, 1, 0),
                           netcdftime.datetime(2000, 2, 25, 2, 30)]
     self.assertArrayEqual([cdt.datetime for cdt in result],
                           expected_datetimes)
コード例 #15
0
ファイル: __init__.py プロジェクト: tv3141/nc-time-axis
    def tick_values(self, vmin, vmax):
        vmin, vmax = mtransforms.nonsingular(vmin,
                                             vmax,
                                             expander=1e-7,
                                             tiny=1e-13)

        self.ndays = float(abs(vmax - vmin))

        utime = netcdftime.utime(self.date_unit, self.calendar)
        lower = utime.num2date(vmin)
        upper = utime.num2date(vmax)

        resolution, n = self.compute_resolution(vmin, vmax, lower, upper)

        if resolution == 'YEARLY':
            # TODO START AT THE BEGINNING OF A DECADE/CENTURY/MILLENIUM as
            # appropriate.
            years = self._max_n_locator.tick_values(lower.year, upper.year)
            ticks = [netcdftime.datetime(int(year), 1, 1) for year in years]
        elif resolution == 'MONTHLY':
            # TODO START AT THE BEGINNING OF A DECADE/CENTURY/MILLENIUM as
            # appropriate.
            months_offset = self._max_n_locator.tick_values(0, n)
            ticks = []
            for offset in months_offset:
                year = lower.year + np.floor((lower.month + offset) / 12)
                month = ((lower.month + offset) % 12) + 1
                ticks.append(netcdftime.datetime(int(year), int(month), 1))
        elif resolution == 'DAILY':
            # TODO: It would be great if this favoured multiples of 7.
            days = self._max_n_locator_days.tick_values(vmin, vmax)
            ticks = [utime.num2date(dt) for dt in days]
        elif resolution == 'HOURLY':
            hour_unit = 'hours since 2000-01-01'
            hour_utime = netcdftime.utime(hour_unit, self.calendar)
            in_hours = hour_utime.date2num([lower, upper])
            hours = self._max_n_locator.tick_values(in_hours[0], in_hours[1])
            ticks = [hour_utime.num2date(dt) for dt in hours]
        elif resolution == 'MINUTELY':
            minute_unit = 'minutes since 2000-01-01'
            minute_utime = netcdftime.utime(minute_unit, self.calendar)
            in_minutes = minute_utime.date2num([lower, upper])
            minutes = self._max_n_locator.tick_values(in_minutes[0],
                                                      in_minutes[1])
            ticks = [minute_utime.num2date(dt) for dt in minutes]
        elif resolution == 'SECONDLY':
            second_unit = 'seconds since 2000-01-01'
            second_utime = netcdftime.utime(second_unit, self.calendar)
            in_seconds = second_utime.date2num([lower, upper])
            seconds = self._max_n_locator.tick_values(in_seconds[0],
                                                      in_seconds[1])
            ticks = [second_utime.num2date(dt) for dt in seconds]
        else:
            msg = 'Resolution {} not implemented yet.'.format(resolution)
            raise ValueError(msg)

        return utime.date2num(ticks)
コード例 #16
0
 def test_axis_default_limits(self):
     cal = '360_day'
     unit = (cal, 'days since 2000-02-25 00:00:00')
     result = NetCDFTimeConverter().axisinfo(unit, None)
     expected_dt = [netcdftime.datetime(2000, 1, 1),
                    netcdftime.datetime(2010, 1, 1)]
     np.testing.assert_array_equal(
         result.default_limits,
         [CalendarDateTime(edt, cal) for edt in expected_dt])
コード例 #17
0
 def test_axis_default_limits(self):
     cal = '360_day'
     unit = (cal, 'days since 2000-02-25 00:00:00')
     result = NetCDFTimeConverter().axisinfo(unit, None)
     expected_dt = [netcdftime.datetime(2000, 1, 1),
                    netcdftime.datetime(2010, 1, 1)]
     np.testing.assert_array_equal(
         result.default_limits,
         [CalendarDateTime(edt, cal) for edt in expected_dt])
コード例 #18
0
 def test_nonequal_calendars(self):
     # Test that different supplied calendars causes an error.
     calendar_1 = '360_day'
     calendar_2 = '365_day'
     unit = 'days since 2000-01-01'
     val = [CalendarDateTime(netcdftime.datetime(2014, 8, 12), calendar_1),
            CalendarDateTime(netcdftime.datetime(2014, 8, 13), calendar_2)]
     with self.assertRaisesRegexp(ValueError, 'not all equal'):
         NetCDFTimeConverter().default_units(val, None)
コード例 #19
0
 def test_360_day_calendar_nd(self):
     # Test the case where the input is an nd-array.
     calendar = '360_day'
     unit = 'days since 2000-01-01'
     val = np.array([[CalendarDateTime(netcdftime.datetime(2014, 8, 12),
                                       calendar)],
                    [CalendarDateTime(netcdftime.datetime(2014, 8, 13),
                                      calendar)]])
     result = NetCDFTimeConverter().default_units(val, None)
     self.assertEqual(result, (calendar, unit))
コード例 #20
0
ファイル: test_pandas.py プロジェクト: fionaRust/iris
 def test_data_frame_netcdftime_360(self):
     data_frame = pandas.DataFrame(
         [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
         index=[netcdftime.datetime(2001, 1, 1, 1, 1, 1), netcdftime.datetime(2002, 2, 2, 2, 2, 2)],
         columns=[10, 11, 12, 13, 14],
     )
     self.assertCML(
         iris.pandas.as_cube(data_frame, calendars={0: cf_units.CALENDAR_360_DAY}),
         tests.get_result_path(("pandas", "as_cube", "data_frame_netcdftime_360.cml")),
     )
コード例 #21
0
ファイル: test__fixup_dates.py プロジェクト: iluckyyang/iris
 def test_365_day_calendar(self):
     unit = Unit('minutes since 2000-02-25 00:00:00', calendar='365_day')
     coord = AuxCoord([30, 60, 150], 'time', units=unit)
     result = _fixup_dates(coord, coord.points)
     expected_datetimes = [
         netcdftime.datetime(2000, 2, 25, 0, 30),
         netcdftime.datetime(2000, 2, 25, 1, 0),
         netcdftime.datetime(2000, 2, 25, 2, 30)
     ]
     self.assertArrayEqual([cdt.datetime for cdt in result],
                           expected_datetimes)
コード例 #22
0
 def test_t1(self):
     collation = FieldCollation(
         [_make_field(lbyr=2013),
          _make_field(lbyr=2014)])
     result = collation.element_arrays_and_dims
     self.assertEqual(list(result.keys()), ['t1'])
     values, dims = result['t1']
     self.assertArrayEqual(
         values,
         [datetime(2013, 1, 1), datetime(2014, 1, 1)])
     self.assertEqual(dims, (0, ))
コード例 #23
0
 def test_data_frame_netcdftime_360(self):
     data_frame = pandas.DataFrame(
         [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
         index=[
             netcdftime.datetime(2001, 1, 1, 1, 1, 1),
             netcdftime.datetime(2002, 2, 2, 2, 2, 2)
         ],
         columns=[10, 11, 12, 13, 14])
     self.assertCML(
         iris.pandas.as_cube(data_frame,
                             calendars={0: cf_units.CALENDAR_360_DAY}),
         tests.get_result_path(
             ('pandas', 'as_cube', 'data_frame_netcdftime_360.cml')))
コード例 #24
0
 def test_series_netcdftime_360(self):
     series = pandas.Series(
         [0, 1, 2, 3, 4],
         index=[netcdftime.datetime(2001, 1, 1, 1, 1, 1),
                netcdftime.datetime(2002, 2, 2, 2, 2, 2),
                netcdftime.datetime(2003, 3, 3, 3, 3, 3),
                netcdftime.datetime(2004, 4, 4, 4, 4, 4),
                netcdftime.datetime(2005, 5, 5, 5, 5, 5)])
     self.assertCML(
         iris.pandas.as_cube(series,
                             calendars={0: cf_units.CALENDAR_360_DAY}),
         tests.get_result_path(('pandas', 'as_cube',
                                'series_netcdfimte_360.cml')))
コード例 #25
0
ファイル: temporal.py プロジェクト: Ouranosinc/ocgis
def get_datetime_or_netcdftime(*args, **kwargs):
    if env.PREFER_NETCDFTIME:
        try:
            ret = netcdftime.datetime(*args, **kwargs)
        except ValueError:
            # Assume the datetime object is not compatible with the arguments. Return a netcdftime object.
            ret = datetime.datetime(*args, **kwargs)
    else:
        try:
            ret = datetime.datetime(*args, **kwargs)
        except ValueError:
            ret = netcdftime.datetime(*args, **kwargs)
    return ret
コード例 #26
0
ファイル: test_pandas.py プロジェクト: RachelNorth/iris
 def test_data_frame_netcdftime_360(self):
     data_frame = pandas.DataFrame(
         [[0, 1, 2, 3, 4],
          [5, 6, 7, 8, 9]],
         index=[netcdftime.datetime(2001, 01, 01, 01, 01, 01),
                netcdftime.datetime(2002, 02, 02, 02, 02, 02)],
         columns=[10, 11, 12, 13, 14])
     self.assertCML(
         iris.pandas.as_cube(
             data_frame,
             calendars={0: iris.unit.CALENDAR_360_DAY}),
         tests.get_result_path(('pandas', 'as_cube',
                                'data_frame_netcdftime_360.cml')))
コード例 #27
0
ファイル: util_dt.py プロジェクト: cerfacs-globc/icclim
def harmonize_hourly_timestamp(time_range, calend, dt):
    '''
    Adjust the ``time_range`` by setting hour value to datetime.datetime objects.
    
    :param time_range: time range selected by user   
    :type time_range: list of two datetime.datetime objects
    
    :param dt: any datetime step of input datetime vector
    :type dt: datetime.datetime object
    
    :param calend: calendar attribute of variable "time" in netCDF file
    :type calend: str

    :rtype: list of two datetime.datetime objects
    
    WHY:
    if input time steps vector is from 1990-01-01 12:00 to 2000-12-31 12:00,
    and user's time_range is [datetime.datetime(1900, 1, 1), datetime.datetime(1905, 12, 31)],
    i.e. [datetime.datetime(1900, 1, 1, 0, 0), datetime.datetime(1905, 12, 31, 0, 0)],
    it will be not included in the input time steps (there will be the error message "The time range is not included in the input time steps array.").
    Thus, this function will adjust the hour of the user's time_range: [datetime.datetime(1900, 1, 1, 12, 0), datetime.datetime(1905, 12, 31, 12, 0)]

    '''

#     time_range_begin = datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
#     time_range_end = datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)

    if calend == 'noleap' or calend == '365_day':
        time_range_begin = netcdftime._netcdftime.DatetimeNoLeap(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeNoLeap(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == '360_day':
        time_range_begin = netcdftime._netcdftime.Datetime360Day(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.Datetime360Day(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'gregorian':
        time_range_begin = netcdftime._netcdftime.DatetimeGregorian(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeGregorian(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'proleptic_gregorian':
        time_range_begin = netcdftime._netcdftime.DatetimeProlepticGregorian(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeProlepticGregorian(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'julian':
        time_range_begin = netcdftime._netcdftime.DatetimeJulian(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeJulian(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'all_leap' or calend == '366_day':
        time_range_begin = netcdftime._netcdftime.DatetimeAllLeap(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeAllLeap(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    else:
        time_range_begin = netcdftime.datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime.datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)

    return [time_range_begin, time_range_end]
コード例 #28
0
def harmonize_hourly_timestamp(time_range, calend, dt):
    '''
    Adjust the ``time_range`` by setting hour value to datetime.datetime objects.
    
    :param time_range: time range selected by user   
    :type time_range: list of two datetime.datetime objects
    
    :param dt: any datetime step of input datetime vector
    :type dt: datetime.datetime object
    
    :param calend: calendar attribute of variable "time" in netCDF file
    :type calend: str

    :rtype: list of two datetime.datetime objects
    
    WHY:
    if input time steps vector is from 1990-01-01 12:00 to 2000-12-31 12:00,
    and user's time_range is [datetime.datetime(1900, 1, 1), datetime.datetime(1905, 12, 31)],
    i.e. [datetime.datetime(1900, 1, 1, 0, 0), datetime.datetime(1905, 12, 31, 0, 0)],
    it will be not included in the input time steps (there will be the error message "The time range is not included in the input time steps array.").
    Thus, this function will adjust the hour of the user's time_range: [datetime.datetime(1900, 1, 1, 12, 0), datetime.datetime(1905, 12, 31, 12, 0)]

    '''

#     time_range_begin = datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
#     time_range_end = datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)

    if calend == 'noleap' or calend == '365_day':
        time_range_begin = netcdftime._netcdftime.DatetimeNoLeap(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeNoLeap(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == '360_day':
        time_range_begin = netcdftime._netcdftime.Datetime360Day(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.Datetime360Day(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'gregorian':
        time_range_begin = netcdftime._netcdftime.DatetimeGregorian(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeGregorian(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'proleptic_gregorian':
        time_range_begin = netcdftime._netcdftime.DatetimeProlepticGregorian(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeProlepticGregorian(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'julian':
        time_range_begin = netcdftime._netcdftime.DatetimeJulian(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeJulian(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    elif calend == 'all_leap' or calend == '366_day':
        time_range_begin = netcdftime._netcdftime.DatetimeAllLeap(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime._netcdftime.DatetimeAllLeap(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    else:
        time_range_begin = netcdftime.datetime(time_range[0].year, time_range[0].month, time_range[0].day, dt.hour)
        time_range_end = netcdftime.datetime(time_range[1].year, time_range[1].month, time_range[1].day, dt.hour)
    
    return [time_range_begin, time_range_end]
コード例 #29
0
ファイル: test_pandas.py プロジェクト: djkirkham/iris
 def test_time_360(self):
     cube = Cube(np.array([0, 1, 2, 3, 4]), long_name="ts")
     time_unit = cf_units.Unit("days since 2000-01-01 00:00",
                               calendar=cf_units.CALENDAR_360_DAY)
     time_coord = DimCoord([0, 100.1, 200.2, 300.3, 400.4],
                           long_name="time", units=time_unit)
     cube.add_dim_coord(time_coord, 0)
     expected_index = [netcdftime.datetime(2000, 1, 1, 0, 0),
                       netcdftime.datetime(2000, 4, 11, 2, 24),
                       netcdftime.datetime(2000, 7, 21, 4, 48),
                       netcdftime.datetime(2000, 11, 1, 7, 12),
                       netcdftime.datetime(2001, 2, 11, 9, 36)]
     series = iris.pandas.as_series(cube)
     self.assertArrayEqual(series, cube.data)
     self.assertArrayEqual(series.index, expected_index)
コード例 #30
0
ファイル: test_pandas.py プロジェクト: djkirkham/iris
 def test_time_360(self):
     cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]),
                 long_name="ts")
     time_unit = cf_units.Unit("days since 2000-01-01 00:00",
                               calendar=cf_units.CALENDAR_360_DAY)
     time_coord = DimCoord([100.1, 200.2], long_name="time",
                           units=time_unit)
     cube.add_dim_coord(time_coord, 0)
     expected_index = [netcdftime.datetime(2000, 4, 11, 2, 24),
                       netcdftime.datetime(2000, 7, 21, 4, 48)]
     expected_columns = [0, 1, 2, 3, 4]
     data_frame = iris.pandas.as_data_frame(cube)
     self.assertArrayEqual(data_frame, cube.data)
     self.assertArrayEqual(data_frame.index, expected_index)
     self.assertArrayEqual(data_frame.columns, expected_columns)
コード例 #31
0
def _fixup_dates(coord, values):
    if coord.units.calendar is not None and values.ndim == 1:
        # Convert coordinate values into tuples of
        # (year, month, day, hour, min, sec)
        dates = [coord.units.num2date(val).timetuple()[0:6] for val in values]
        if coord.units.calendar == 'gregorian':
            r = [datetime.datetime(*date) for date in dates]
        else:
            try:
                import nc_time_axis
            except ImportError:
                msg = ('Cannot plot against time in a non-gregorian '
                       'calendar, because "nc_time_axis" is not available :  '
                       'Install the package from '
                       'https://github.com/SciTools/nc-time-axis to enable '
                       'this usage.')
                raise IrisError(msg)

            r = [
                nc_time_axis.CalendarDateTime(netcdftime.datetime(*date),
                                              coord.units.calendar)
                for date in dates
            ]
        values = np.empty(len(r), dtype=object)
        values[:] = r
    return values
コード例 #32
0
 def test_360_day_calendar(self):
     calendar = '360_day'
     unit = 'days since 2000-01-01'
     val = [netcdftime.datetime(2014, 8, 12)]
     val[0].calendar = calendar
     result = NetCDFTimeConverter().default_units(val, None)
     self.assertEqual(result, (calendar, unit))
コード例 #33
0
ファイル: plot.py プロジェクト: bjlittle/iris
def _fixup_dates(coord, values):
    if coord.units.calendar is not None and values.ndim == 1:
        # Convert coordinate values into tuples of
        # (year, month, day, hour, min, sec)
        dates = [coord.units.num2date(val).timetuple()[0:6]
                 for val in values]
        if coord.units.calendar == 'gregorian':
            r = [datetime.datetime(*date) for date in dates]
        else:
            try:
                import nc_time_axis
            except ImportError:
                msg = ('Cannot plot against time in a non-gregorian '
                       'calendar, because "nc_time_axis" is not available :  '
                       'Install the package from '
                       'https://github.com/SciTools/nc-time-axis to enable '
                       'this usage.')
                raise IrisError(msg)

            r = [nc_time_axis.CalendarDateTime(
                 netcdftime.datetime(*date), coord.units.calendar)
                 for date in dates]
        values = np.empty(len(r), dtype=object)
        values[:] = r
    return values
コード例 #34
0
def get_datetime_or_netcdftime(year, month, day, **kwargs):
    try:
        ret = datetime.datetime(year, month, day, **kwargs)
    except ValueError:
        # Assume the datetime object is not compatible with the arguments. Return a netcdftime object.
        ret = netcdftime.datetime(year, month, day, **kwargs)
    return ret
コード例 #35
0
ファイル: test_Cell.py プロジェクト: scollis/iris
 def test_netcdftime_other(self):
     # Check that cell comparison to a netcdftime.datetime object
     # raises an exception otherwise this will fall back to id comparison
     # producing unreliable results.
     dt = netcdftime.datetime(2010, 3, 21)
     cell = Cell(mock.Mock(timetuple=mock.Mock()))
     self.assert_raises_on_comparison(cell, dt, TypeError, "determine the order of netcdftime")
コード例 #36
0
 def test_all_scalar(self):
     field = self._field()
     field.lbtim = 11
     field.t1 = netcdftime.datetime(1970, 1, 1, 18)
     field.t2 = netcdftime.datetime(1970, 1, 1, 12)
     collation = mock.Mock(fields=[field], vector_dims_shape=(), element_arrays_and_dims={})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 1), (LATITUDE, 0)]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.DimCoord(18, "time", units="hours since epoch"), None),
         (iris.coords.DimCoord(12, "forecast_reference_time", units="hours since epoch"), None),
         (iris.coords.DimCoord(6, "forecast_period", units="hours"), None),
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #37
0
ファイル: __init__.py プロジェクト: bjlittle/nc-time-axis
    def axisinfo(unit, axis):
        """
        Return the :class:`~matplotlib.units.AxisInfo` for *unit*.

        *unit* is a tzinfo instance or None.
        The *axis* argument is required but not used.
        """
        calendar, date_unit = unit

        majloc = NetCDFTimeDateLocator(4, calendar=calendar, date_unit=date_unit)
        majfmt = NetCDFTimeDateFormatter(majloc, calendar=calendar, time_units=date_unit)
        datemin = netcdftime.datetime(2000, 1, 1)
        datemax = netcdftime.datetime(2010, 1, 1)
        datemin.calendar = datemax.calendar = calendar
        return munits.AxisInfo(majloc=majloc, majfmt=majfmt, label='Testing',
                               default_limits=(datemin, datemax))
コード例 #38
0
ファイル: test_plot.py プロジェクト: djkirkham/nc-time-axis
 def test_360_day_calendar(self):
     datetimes = [netcdftime.datetime(1986, month, 30)
                  for month in range(1, 6)]
     cal_datetimes = [nc_time_axis.CalendarDateTime(dt, '360_day')
                      for dt in datetimes]
     line1, = plt.plot(cal_datetimes)
     result_ydata = line1.get_ydata()
     np.testing.assert_array_equal(result_ydata, cal_datetimes)
コード例 #39
0
ファイル: test_Cell.py プロジェクト: MahatmaCane/iris
 def test_PartialDateTime_unbounded_cell(self):
     # Check that cell comparison works with PartialDateTimes.
     dt = PartialDateTime(month=6)
     cell = Cell(netcdftime.datetime(2010, 3, 1))
     self.assertLess(cell, dt)
     self.assertGreater(dt, cell)
     self.assertLessEqual(cell, dt)
     self.assertGreaterEqual(dt, cell)
コード例 #40
0
ファイル: test_Cell.py プロジェクト: andreas-h/iris
 def test_netcdftime_other(self):
     # Check that cell comparison to a netcdftime.datetime object
     # raises an exception otherwise this will fall back to id comparison
     # producing unreliable results.
     dt = netcdftime.datetime(2010, 3, 21)
     cell = Cell(mock.Mock(timetuple=mock.Mock()))
     self.assert_raises_on_comparison(cell, dt, TypeError,
                                      'determine the order of netcdftime')
コード例 #41
0
ファイル: test_Cell.py プロジェクト: andreas-h/iris
 def test_PartialDateTime_unbounded_cell(self):
     # Check that cell comparison works with PartialDateTimes.
     dt = PartialDateTime(month=6)
     cell = Cell(netcdftime.datetime(2010, 3, 1))
     self.assertLess(cell, dt)
     self.assertGreater(dt, cell)
     self.assertLessEqual(cell, dt)
     self.assertGreaterEqual(dt, cell)
コード例 #42
0
    def axisinfo(unit, axis):
        """
        Returns the :class:`~matplotlib.units.AxisInfo` for *unit*.

        *unit* is a tzinfo instance or None.
        The *axis* argument is required but not used.
        """
        calendar, date_unit = unit

        majloc = NetCDFTimeDateLocator(4, calendar=calendar,
                                       date_unit=date_unit)
        majfmt = NetCDFTimeDateFormatter(majloc, calendar=calendar,
                                         time_units=date_unit)
        datemin = CalendarDateTime(netcdftime.datetime(2000, 1, 1), calendar)
        datemax = CalendarDateTime(netcdftime.datetime(2010, 1, 1), calendar)
        return munits.AxisInfo(majloc=majloc, majfmt=majfmt, label='',
                               default_limits=(datemin, datemax))
コード例 #43
0
ファイル: __init__.py プロジェクト: bjlittle/nc-time-axis
    def tick_values(self, vmin, vmax):
        vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=1e-7, tiny=1e-13)

        lower = netcdftime.num2date(vmin, self.date_unit, self.calendar)
        upper = netcdftime.num2date(vmax, self.date_unit, self.calendar)

        self.ndays = abs(vmax - vmin)

        resolution, n = self.compute_resolution(vmin, vmax, lower, upper)

        if resolution == 'YEARLY':
            # TODO START AT THE BEGINNING OF A DECADE/CENTURY/MILLENIUM as appropriate.
            years = self._max_n_locator.tick_values(lower.year, upper.year)
            ticks = [netcdftime.datetime(int(year), 1, 1) for year in years]
        elif resolution == 'MONTHLY':
            # TODO START AT THE BEGINNING OF A DECADE/CENTURY/MILLENIUM as appropriate.
            months_offset = self._max_n_locator.tick_values(0, n)
            ticks = []
            for offset in months_offset:
                year = lower.year + np.floor((lower.month + offset) / 12)
                month = ((lower.month + offset) % 12) + 1
                ticks.append(netcdftime.datetime(int(year), int(month), 1))
        elif resolution == 'DAILY':
            # TODO: It would be great if this favoured multiples of 7.
            days = self._max_n_locator_days.tick_values(vmin, vmax)
            ticks = [netcdftime.num2date(dt, self.date_unit, self.calendar) for dt in days]
        elif resolution == 'HOURLY':
            hour_unit = 'hours since 2000-01-01'
            in_hours = netcdftime.date2num([lower, upper], hour_unit, self.calendar)
            hours = self._max_n_locator.tick_values(in_hours[0], in_hours[1])
            ticks = [netcdftime.num2date(dt, hour_unit, self.calendar) for dt in hours]
        elif resolution == 'MINUTELY':
            minute_unit = 'minutes since 2000-01-01'
            in_minutes = netcdftime.date2num([lower, upper], minute_unit, self.calendar)
            minutes = self._max_n_locator.tick_values(in_minutes[0], in_minutes[1])
            ticks = [netcdftime.num2date(dt, minute_unit, self.calendar) for dt in minutes]
        elif resolution == 'SECONDLY':
            second_unit = 'seconds since 2000-01-01'
            in_seconds = netcdftime.date2num([lower, upper], second_unit, self.calendar)
            seconds = self._max_n_locator.tick_values(in_seconds[0], in_seconds[1])
            ticks = [netcdftime.num2date(dt, second_unit, self.calendar) for dt in seconds]
        else:
            raise ValueError('Resolution {} not implemented yet.'.format(resolution))

        return netcdftime.date2num(ticks, self.date_unit, self.calendar)
コード例 #44
0
ファイル: climate.py プロジェクト: arulalant/aostools
def FindDayOfYear(dateStruc, dateUnits, calendar):
    import netcdftime as nct
    nDays = len(dateStruc)
    t = nct.utime(dateUnits, calendar=calendar)
    dateLoc = np.zeros_like(dateStruc)
    for d in range(nDays):
        dateLoc[d] = nct.datetime(1, dateStruc[d].month, dateStruc[d].day)
    dayOfYear = t.date2num(dateLoc)
    return dayOfYear
コード例 #45
0
 def test_time_360(self):
     cube = Cube(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]),
                 long_name="ts")
     time_unit = cf_units.Unit("days since 2000-01-01 00:00",
                               calendar=cf_units.CALENDAR_360_DAY)
     time_coord = DimCoord([100.1, 200.2],
                           long_name="time",
                           units=time_unit)
     cube.add_dim_coord(time_coord, 0)
     expected_index = [
         netcdftime.datetime(2000, 4, 11, 2, 24),
         netcdftime.datetime(2000, 7, 21, 4, 48)
     ]
     expected_columns = [0, 1, 2, 3, 4]
     data_frame = iris.pandas.as_data_frame(cube)
     self.assertArrayEqual(data_frame, cube.data)
     self.assertArrayEqual(data_frame.index, expected_index)
     self.assertArrayEqual(data_frame.columns, expected_columns)
コード例 #46
0
ファイル: test_pandas.py プロジェクト: scollis/iris
 def test_series_netcdftime_360(self):
     series = pandas.Series(
         [0, 1, 2, 3, 4],
         index=[
             netcdftime.datetime(2001, 01, 01, 01, 01, 01),
             netcdftime.datetime(2002, 02, 02, 02, 02, 02),
             netcdftime.datetime(2003, 03, 03, 03, 03, 03),
             netcdftime.datetime(2004, 04, 04, 04, 04, 04),
             netcdftime.datetime(2005, 05, 05, 05, 05, 05),
         ],
     )
     self.assertCML(
         iris.pandas.as_cube(series, calendars={0: iris.unit.CALENDAR_360_DAY}),
         tests.get_result_path(("pandas", "as_cube", "series_netcdfimte_360.cml")),
     )
コード例 #47
0
ファイル: constraints.py プロジェクト: mcmweb80/iris
    def _contains_point(self, point, unit):
        point_dt = unit.num2date(point)
        point_yday = self._day_of_year(point_dt)

        start_dt = netcdftime.datetime(point_dt.year, *self.day_of_year[0])
        start_dt = _fix_netcdftime_datetime(start_dt, unit)
        start_yday = self._day_of_year(start_dt)

        end_dt = netcdftime.datetime(point_dt.year, *self.day_of_year[1])
        end_dt = _fix_netcdftime_datetime(end_dt, unit)
        end_yday = self._day_of_year(end_dt)

        if start_yday < end_yday:
            result = start_yday <= point_yday <= end_yday
        elif start_yday > end_yday:
            result = point_yday >= start_yday or point_yday <= end_yday
        else:
            raise ValueError('start_yday == end_yday')
        return result
コード例 #48
0
ファイル: nimrod_load_rules.py プロジェクト: Jozhogg/iris
def reference_time(cube, field):
    """Add a 'reference time' to the cube, if present in the field."""
    if field.dt_year != field.int_mdi:
        data_date = netcdftime.datetime(field.dt_year, field.dt_month, field.dt_day, field.dt_hour, field.dt_minute)

        ref_time_coord = DimCoord(
            TIME_UNIT.date2num(data_date), standard_name="forecast_reference_time", units=TIME_UNIT
        )

        cube.add_aux_coord(ref_time_coord)
コード例 #49
0
def time(cube, field):
    """Add a time coord to the cube."""
    valid_date = netcdftime.datetime(field.vt_year, field.vt_month,
                                     field.vt_day, field.vt_hour,
                                     field.vt_minute, field.vt_second)
    time_coord = DimCoord(iris.unit.date2num(valid_date,
                                             'hours since 1970-01-01 00:00:00',
                                             iris.unit.CALENDAR_STANDARD),
                          standard_name='time', units='hours')
    cube.add_aux_coord(time_coord)
コード例 #50
0
ファイル: test_plot.py プロジェクト: tv3141/nc-time-axis
 def test_360_day_calendar(self):
     datetimes = [
         netcdftime.datetime(1986, month, 30) for month in range(1, 6)
     ]
     cal_datetimes = [
         nc_time_axis.CalendarDateTime(dt, '360_day') for dt in datetimes
     ]
     line1, = plt.plot(cal_datetimes)
     result_ydata = line1.get_ydata()
     np.testing.assert_array_equal(result_ydata, cal_datetimes)
コード例 #51
0
ファイル: test_Cell.py プロジェクト: scollis/iris
 def test_netcdftime_cell(self):
     # Check that cell comparison when the cell contains
     # netcdftime.datetime objects raises an exception otherwise
     # this will fall back to id comparison producing unreliable
     # results.
     cell = Cell(netcdftime.datetime(2010, 3, 21))
     dt = mock.Mock(timetuple=mock.Mock())
     self.assert_raises_on_comparison(cell, dt, TypeError, "determine the order of netcdftime")
     self.assert_raises_on_comparison(cell, 23, TypeError, "determine the order of netcdftime")
     self.assert_raises_on_comparison(cell, "hello", TypeError, "Unexpected type.*str")
コード例 #52
0
 def test_all_scalar(self):
     field = self._field()
     field.lbtim = 11
     field.t1 = netcdftime.datetime(1970, 1, 1, 18)
     field.t2 = netcdftime.datetime(1970, 1, 1, 12)
     collation = mock.Mock(fields=[field], vector_dims_shape=(),
                           element_arrays_and_dims={})
     metadata = convert_collation(collation)
     self._check_phenomenon(metadata)
     coords_and_dims = [(LONGITUDE, 1),
                        (LATITUDE, 0)]
     self.assertEqual(metadata.dim_coords_and_dims, coords_and_dims)
     coords_and_dims = [
         (iris.coords.DimCoord(18, 'time', units='hours since epoch'),
          None),
         (iris.coords.DimCoord(12, 'forecast_reference_time',
                               units='hours since epoch'), None),
         (iris.coords.DimCoord(6, 'forecast_period', units='hours'), None)
     ]
     self.assertEqual(metadata.aux_coords_and_dims, coords_and_dims)
コード例 #53
0
def reference_time(cube, field):
    """Add a 'reference time' to the cube, if present in the field."""
    if field.dt_year != field.int_mdi:
        data_date = netcdftime.datetime(field.dt_year, field.dt_month,
                                        field.dt_day, field.dt_hour,
                                        field.dt_minute)

        ref_time_coord = DimCoord(TIME_UNIT.date2num(data_date),
                                  standard_name='forecast_reference_time',
                                  units=TIME_UNIT)

        cube.add_aux_coord(ref_time_coord)
コード例 #54
0
ファイル: test_convert.py プロジェクト: dennissergeev/iris
    def test_365_calendar(self):
        f = mock.MagicMock(lbtim=SplittableInt(4, {'ia': 2, 'ib': 1, 'ic': 0}),
                           lbyr=2013, lbmon=1, lbdat=1, lbhr=12, lbmin=0,
                           lbsec=0,
                           t1=netcdftime.datetime(2013, 1, 1, 12, 0, 0),
                           t2=netcdftime.datetime(2013, 1, 2, 12, 0, 0),
                           spec=PPField3)
        f.time_unit = six.create_bound_method(PPField3.time_unit, f)
        f.calendar = cf_units.CALENDAR_365_DAY
        (factories, references, standard_name, long_name, units,
         attributes, cell_methods, dim_coords_and_dims,
         aux_coords_and_dims) = convert(f)

        def is_t_coord(coord_and_dims):
            coord, dims = coord_and_dims
            return coord.standard_name == 'time'

        coords_and_dims = list(filter(is_t_coord, aux_coords_and_dims))
        self.assertEqual(len(coords_and_dims), 1)
        coord, dims = coords_and_dims[0]
        self.assertEqual(guess_coord_axis(coord), 'T')
        self.assertEqual(coord.units.calendar, '365_day')
コード例 #55
0
ファイル: test_Cell.py プロジェクト: andreas-h/iris
 def test_netcdftime_cell(self):
     # Check that cell comparison when the cell contains
     # netcdftime.datetime objects raises an exception otherwise
     # this will fall back to id comparison producing unreliable
     # results.
     cell = Cell(netcdftime.datetime(2010, 3, 21))
     dt = mock.Mock(timetuple=mock.Mock())
     self.assert_raises_on_comparison(cell, dt, TypeError,
                                      'determine the order of netcdftime')
     self.assert_raises_on_comparison(cell, 23, TypeError,
                                      'determine the order of netcdftime')
     self.assert_raises_on_comparison(cell, 'hello', TypeError,
                                      'Unexpected type.*str')
コード例 #56
0
ファイル: test_netcdftime.py プロジェクト: xigrug/iris
 def test_360_day_calendar(self):
     n = 360
     calendar = '360_day'
     time_unit = Unit('days since 1970-01-01 00:00', calendar=calendar)
     time_coord = AuxCoord(np.arange(n), 'time', units=time_unit)
     times = [time_unit.num2date(point) for point in time_coord.points]
     times = [netcdftime.datetime(atime.year, atime.month, atime.day,
                                  atime.hour, atime.minute, atime.second)
              for atime in times]
     expected_ydata = np.array([CalendarDateTime(time, calendar)
                                for time in times])
     line1, = iplt.plot(time_coord)
     result_ydata = line1.get_ydata()
     self.assertArrayEqual(expected_ydata, result_ydata)
コード例 #57
0
def reference_time(cube, field):
    """Add a 'reference time' to the cube, if present in the field."""
    if field.dt_year != field.int_mdi:
        data_date = netcdftime.datetime(field.dt_year, field.dt_month,
                                        field.dt_day, field.dt_hour,
                                        field.dt_minute)
        ref_time_coord = DimCoord(
            iris.unit.date2num(
                data_date,
                'hours since 1970-01-01 00:00:00',
                iris.unit.CALENDAR_STANDARD),
            standard_name='forecast_reference_time',
            units='hours')
        cube.add_aux_coord(ref_time_coord)