Пример #1
0
def to_datetime(dt):
    if isinstance(dt, datetime.datetime):
        return dt

    if isinstance(dt, datetime.date):
        return datetime.datetime(dt.year, dt.month, dt.day)

    if isinstance(dt, np.datetime64):
        # Looks like numpy dates conversion vary
        dt = dt.astype(datetime.datetime)

        if isinstance(dt, datetime.datetime):
            return dt

        if isinstance(dt, datetime.date):
            return to_datetime(dt)

        if isinstance(dt, int):
            return datetime.datetime.utcfromtimestamp(dt * 1e-9)

        raise ValueError("Failed to convert numpy datetime {}".format(
            (dt, type(dt))))

    if isinstance(dt, str):
        return parse_date(dt)

    if getattr(dt, "to_datetime", None) is None:
        dt = helper(dt)

    return to_datetime(dt.to_datetime())
Пример #2
0
    def request(self, **kwargs):
        based_on = kwargs.pop("based_on", None)
        if based_on is not None:
            data = based_on.pop("data")
            data = helper(data, **based_on)
            kwargs["area"] = data.bounding_box()
            kwargs["date"] = data.dates()

        return kwargs
Пример #3
0
def to_bounding_box(obj):

    if isinstance(obj, BoundingBox):
        return obj

    if isinstance(obj, (list, tuple)):
        return BoundingBox(north=obj[0],
                           west=obj[1],
                           south=obj[2],
                           east=obj[3])

    if getattr(obj, "to_bounding_box", None) is None:
        obj = helper(obj)

    return to_bounding_box(obj.to_bounding_box())
Пример #4
0
    def plot_map(self, data=None, **kwargs):

        # try:
        #     iter(data)
        #     data = list(data)
        # except Exception:
        #     pass

        if not isinstance(data, (list, tuple)):
            data = [data]

        for d in data:
            if getattr(d, "plot_map", None) is None:
                d = helper(d)

            d.plot_map(self.driver)

        options = Options(kwargs)
        self.driver.apply_options(options)
        options.check_unused()
Пример #5
0
def to_datetime_list(datetimes):  # noqa C901

    if isinstance(datetimes, str):
        # MARS style lists
        bits = datetimes.split("/")
        if len(bits) == 3 and bits[1].lower() == "to":
            return _mars_list(to_datetime(bits[0]), to_datetime(bits[2]), 1)

        if len(bits) == 5 and bits[1].lower() == "to" and bits[3].lower(
        ) == "by":
            return _mars_list(to_datetime(bits[0]), to_datetime(bits[2]),
                              int(bits[4]))

        try:
            return to_datetime_list(bits)
        except Exception:
            pass

    if isinstance(datetimes, (datetime.datetime, np.datetime64, str)):
        return to_datetime_list([datetimes])

    if isinstance(datetimes, (list, tuple)):
        if (len(datetimes) == 3 and isinstance(datetimes[1], str)
                and datetimes[1].lower() == "to"):
            return _mars_list(to_datetime(datetimes[0]),
                              to_datetime(datetimes[2]), 1)

        if (len(datetimes) == 5 and datetimes[1].lower() == "to"
                and datetimes[3].lower() == "by"):
            return _mars_list(to_datetime(datetimes[0]),
                              to_datetime(datetimes[2]), int(datetimes[4]))

        return [to_datetime(x) for x in datetimes]

    if getattr(datetimes, "to_datetime_list", None) is None:
        datetimes = helper(datetimes)

    return to_datetime_list(datetimes.to_datetime_list())