Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
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
Ejemplo n.º 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]
Ejemplo n.º 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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
    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')
Ejemplo n.º 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)
Ejemplo n.º 12
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]
Ejemplo n.º 13
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,))
Ejemplo n.º 14
0
 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)
Ejemplo n.º 15
0
    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)
 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])
Ejemplo n.º 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])
Ejemplo n.º 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)
Ejemplo n.º 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))
Ejemplo n.º 20
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")),
     )
Ejemplo n.º 21
0
 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)
Ejemplo n.º 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, ))
Ejemplo n.º 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')))
Ejemplo n.º 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')))
Ejemplo n.º 25
0
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
Ejemplo n.º 26
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, 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')))
Ejemplo n.º 27
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]
Ejemplo n.º 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]
Ejemplo n.º 29
0
 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)
Ejemplo n.º 30
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)
Ejemplo n.º 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
Ejemplo n.º 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))
Ejemplo n.º 33
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
Ejemplo n.º 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
Ejemplo n.º 35
0
 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")
Ejemplo n.º 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)
Ejemplo n.º 37
0
    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))
Ejemplo n.º 38
0
 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)
Ejemplo n.º 39
0
 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)
Ejemplo n.º 40
0
 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')
Ejemplo n.º 41
0
 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)
Ejemplo n.º 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))
Ejemplo n.º 43
0
    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)
Ejemplo n.º 44
0
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
Ejemplo n.º 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)
Ejemplo n.º 46
0
 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")),
     )
Ejemplo n.º 47
0
    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
Ejemplo n.º 48
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)
Ejemplo n.º 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)
Ejemplo n.º 50
0
 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)
Ejemplo n.º 51
0
 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")
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 54
0
    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')
Ejemplo n.º 55
0
 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')
Ejemplo n.º 56
0
 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)
Ejemplo n.º 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)