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())
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())
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())
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
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, )
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())
def plot_map(self, backend): get_wrapper(self.to_pandas()).plot_map(backend)
def plot_graph(self, backend): get_wrapper(self.to_pandas()).plot_graph(backend)
def plot_map(self, driver): get_wrapper(self.to_pandas()).plot_map(driver)