Пример #1
0
def datetime(
        year, month, day, hour=0, minute=0, second=0, microsecond=0,
        tzinfo=None, calendar='proleptic_gregorian'):
    """
    Retrieves a datetime-like object with the requested calendar. Calendar types
    other than proleptic_gregorian require the netcdftime module to be
    installed.

    Parameters
    ----------
    year : int,
    month  : int,
    day  : int,
    hour  : int, optional
    minute  : int, optional
    second : int, optional
    microsecond : int, optional
    tzinfo  : datetime.tzinfo, optional
        A timezone informaton class, such as from pytz. Can only be used with
        'proleptic_gregorian' calendar, as netcdftime does not support
        timezones.
    calendar : string, optional
        Should be one of 'proleptic_gregorian', 'no_leap', '365_day',
        'all_leap', '366_day', '360_day', 'julian', or 'gregorian'. Default
        is 'proleptic_gregorian', which returns a normal Python datetime.
        Other options require the netcdftime module to be installed.

    Returns
    -------
    datetime : datetime-like
        The requested datetime. May be a Python datetime, or one of the
        datetime-like types in netcdftime.
    """
    kwargs = {
        'year': year, 'month': month, 'day': day, 'hour': hour,
        'minute': minute, 'second': second, 'microsecond': microsecond
    }
    if calendar.lower() == 'proleptic_gregorian':
        return real_datetime(tzinfo=tzinfo, **kwargs)
    elif tzinfo is not None:
        raise ValueError('netcdftime does not support timezone-aware datetimes')
    elif ct is None:
        raise DependencyError(
            "Calendars other than 'proleptic_gregorian' require the netcdftime "
            "package, which is not installed.")
    elif calendar.lower() in ('all_leap', '366_day'):
        return ct.DatetimeAllLeap(**kwargs)
    elif calendar.lower() in ('no_leap', 'noleap', '365_day'):
        return ct.DatetimeNoLeap(**kwargs)
    elif calendar.lower() == '360_day':
        return ct.Datetime360Day(**kwargs)
    elif calendar.lower() == 'julian':
        return ct.DatetimeJulian(**kwargs)
    elif calendar.lower() == 'gregorian':
        return ct.DatetimeGregorian(**kwargs)
Пример #2
0
def test_assert_all_valid_date_type(date_type, index):
    import cftime
    if date_type is cftime.DatetimeNoLeap:
        mixed_date_types = [
            date_type(1, 1, 1),
            cftime.DatetimeAllLeap(1, 2, 1)
        ]
    else:
        mixed_date_types = [date_type(1, 1, 1), cftime.DatetimeNoLeap(1, 2, 1)]
    with pytest.raises(TypeError):
        assert_all_valid_date_type(mixed_date_types)

    with pytest.raises(TypeError):
        assert_all_valid_date_type([1, date_type(1, 1, 1)])

    assert_all_valid_date_type([date_type(1, 1, 1), date_type(1, 2, 1)])
Пример #3
0
        (("NRCANdaily", "nrcan_canada_daily_pr_1990.nc"), "default", 366),
    ],
)
def test_get_calendar(file, cal, maxdoy):
    with open_dataset(os.path.join(*file)) as ds:
        out_cal = get_calendar(ds)
        assert cal == out_cal
        assert max_doy[cal] == maxdoy


@pytest.mark.parametrize(
    "obj,cal",
    [
        ([pd.Timestamp.now()], "default"),
        (pd.Timestamp.now(), "default"),
        (cftime.DatetimeAllLeap(2000, 1, 1), "all_leap"),
        (np.array([cftime.DatetimeNoLeap(2000, 1, 1)]), "noleap"),
        (xr.cftime_range("2000-01-01", periods=4, freq="D"), "standard"),
    ],
)
def test_get_calendar_nonxr(obj, cal):
    assert get_calendar(obj) == cal


@pytest.mark.parametrize("obj", ["astring", {"a": "dict"}, lambda x: x])
def test_get_calendar_errors(obj):
    with pytest.raises(ValueError, match="Calendar could not be inferred from object"):
        get_calendar(obj)


@pytest.mark.parametrize(