示例#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)

    dt = get_wrapper(dt)

    return to_datetime(dt.to_datetime())
示例#2
0
文件: dates.py 项目: ecmwf/climetlab
def to_datetime_list(datetimes):  # noqa C901

    if isinstance(datetimes, (datetime.datetime, np.datetime64)):
        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_like_date_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_like_date_list(
                to_datetime(datetimes[0]), to_datetime(datetimes[2]), int(datetimes[4])
            )

        return [to_datetime(x) for x in datetimes]

    datetimes = get_wrapper(datetimes)

    return to_datetime_list(datetimes.to_datetime_list())
示例#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])

    obj = get_wrapper(obj)

    return to_bounding_box(obj.to_bounding_box())
示例#4
0
    def plot_graph(self, data=None, **kwargs):

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

        for d in data:
            d = get_wrapper(d)
            d.plot_graph(self.backend)

        options = Options(kwargs)
        self.backend.apply_options(options)
        options.check_unused()

        return self
示例#5
0
    def plot_map(self, driver):

        metadata = get_wrapper(driver.option("metadata"))
        metadata = metadata.field_metadata()

        driver.bounding_box(
            north=metadata["north"],
            south=metadata["south"],
            west=metadata["west"],
            east=metadata["east"],
        )

        driver.plot_numpy(
            self.data.reshape(metadata.get("shape", self.data.shape)),
            metadata=metadata,
        )
示例#6
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]

    datetimes = get_wrapper(datetimes)

    return to_datetime_list(datetimes.to_datetime_list())
示例#7
0
文件: fwf.py 项目: ecmwf/climetlab
 def plot_map(self, backend):
     get_wrapper(self.to_pandas()).plot_map(backend)
示例#8
0
文件: fwf.py 项目: ecmwf/climetlab
 def plot_graph(self, backend):
     get_wrapper(self.to_pandas()).plot_graph(backend)
示例#9
0
 def plot_map(self, driver):
     get_wrapper(self.to_pandas()).plot_map(driver)