|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import inspect |
4 | | -from datetime import timedelta |
5 | 4 | from math import cos, pi |
6 | 5 |
|
7 | 6 | import cftime |
|
13 | 12 | "GeographicPolar", |
14 | 13 | "GeographicPolarSquare", |
15 | 14 | "GeographicSquare", |
16 | | - "TimeConverter", |
17 | 15 | "UnitConverter", |
18 | 16 | "convert_to_flat_array", |
19 | 17 | "unitconverters_map", |
@@ -42,122 +40,6 @@ def _get_cftime_calendars() -> list[str]: |
42 | 40 | return [getattr(cftime, cf_datetime)(1990, 1, 1).calendar for cf_datetime in _get_cftime_datetimes()] |
43 | 41 |
|
44 | 42 |
|
45 | | -class TimeConverter: |
46 | | - """Converter class for dates with different calendars in FieldSets |
47 | | -
|
48 | | - Parameters |
49 | | - ---------- |
50 | | - time_origin : float, integer, numpy.datetime64 or cftime.DatetimeNoLeap |
51 | | - time origin of the class. |
52 | | - """ |
53 | | - |
54 | | - def __init__(self, time_origin: float | np.datetime64 | np.timedelta64 | cftime.datetime = 0): |
55 | | - self.time_origin = time_origin |
56 | | - self.calendar: str | None = None |
57 | | - if isinstance(time_origin, np.datetime64): |
58 | | - self.calendar = "np_datetime64" |
59 | | - elif isinstance(time_origin, np.timedelta64): |
60 | | - self.calendar = "np_timedelta64" |
61 | | - elif isinstance(time_origin, cftime.datetime): |
62 | | - self.calendar = time_origin.calendar |
63 | | - |
64 | | - def reltime(self, time: TimeConverter | np.datetime64 | np.timedelta64 | cftime.datetime) -> float | npt.NDArray: |
65 | | - """Method to compute the difference, in seconds, between a time and the time_origin |
66 | | - of the TimeConverter |
67 | | -
|
68 | | - Parameters |
69 | | - ---------- |
70 | | - time : |
71 | | -
|
72 | | -
|
73 | | - Returns |
74 | | - ------- |
75 | | - type |
76 | | - time - self.time_origin |
77 | | -
|
78 | | - """ |
79 | | - time = time.time_origin if isinstance(time, TimeConverter) else time |
80 | | - if self.calendar in ["np_datetime64", "np_timedelta64"]: |
81 | | - return (time - self.time_origin) / np.timedelta64(1, "s") # type: ignore |
82 | | - elif self.calendar in _get_cftime_calendars(): |
83 | | - if isinstance(time, (list, np.ndarray)): |
84 | | - try: |
85 | | - return np.array([(t - self.time_origin).total_seconds() for t in time]) # type: ignore |
86 | | - except ValueError: |
87 | | - raise ValueError( |
88 | | - f"Cannot subtract 'time' (a {type(time)} object) from a {self.calendar} calendar.\n" |
89 | | - f"Provide 'time' as a {type(self.time_origin)} object?" |
90 | | - ) |
91 | | - else: |
92 | | - try: |
93 | | - return (time - self.time_origin).total_seconds() # type: ignore |
94 | | - except ValueError: |
95 | | - raise ValueError( |
96 | | - f"Cannot subtract 'time' (a {type(time)} object) from a {self.calendar} calendar.\n" |
97 | | - f"Provide 'time' as a {type(self.time_origin)} object?" |
98 | | - ) |
99 | | - elif self.calendar is None: |
100 | | - return time - self.time_origin # type: ignore |
101 | | - else: |
102 | | - raise RuntimeError(f"Calendar {self.calendar} not implemented in TimeConverter") |
103 | | - |
104 | | - def fulltime(self, time): |
105 | | - """Method to convert a time difference in seconds to a date, based on the time_origin |
106 | | -
|
107 | | - Parameters |
108 | | - ---------- |
109 | | - time : |
110 | | -
|
111 | | -
|
112 | | - Returns |
113 | | - ------- |
114 | | - type |
115 | | - self.time_origin + time |
116 | | -
|
117 | | - """ |
118 | | - time = time.time_origin if isinstance(time, TimeConverter) else time |
119 | | - if self.calendar in ["np_datetime64", "np_timedelta64"]: |
120 | | - if isinstance(time, (list, np.ndarray)): |
121 | | - return [self.time_origin + np.timedelta64(int(t), "s") for t in time] |
122 | | - else: |
123 | | - return self.time_origin + np.timedelta64(int(time), "s") |
124 | | - elif self.calendar in _get_cftime_calendars(): |
125 | | - return self.time_origin + timedelta(seconds=time) |
126 | | - elif self.calendar is None: |
127 | | - return self.time_origin + time |
128 | | - else: |
129 | | - raise RuntimeError(f"Calendar {self.calendar} not implemented in TimeConverter") |
130 | | - |
131 | | - def __repr__(self): |
132 | | - return f"{self.time_origin}" |
133 | | - |
134 | | - def __eq__(self, other): |
135 | | - other = other.time_origin if isinstance(other, TimeConverter) else other |
136 | | - return self.time_origin == other |
137 | | - |
138 | | - def __ne__(self, other): |
139 | | - other = other.time_origin if isinstance(other, TimeConverter) else other |
140 | | - if not isinstance(other, type(self.time_origin)): |
141 | | - return True |
142 | | - return self.time_origin != other |
143 | | - |
144 | | - def __gt__(self, other): |
145 | | - other = other.time_origin if isinstance(other, TimeConverter) else other |
146 | | - return self.time_origin > other |
147 | | - |
148 | | - def __lt__(self, other): |
149 | | - other = other.time_origin if isinstance(other, TimeConverter) else other |
150 | | - return self.time_origin < other |
151 | | - |
152 | | - def __ge__(self, other): |
153 | | - other = other.time_origin if isinstance(other, TimeConverter) else other |
154 | | - return self.time_origin >= other |
155 | | - |
156 | | - def __le__(self, other): |
157 | | - other = other.time_origin if isinstance(other, TimeConverter) else other |
158 | | - return self.time_origin <= other |
159 | | - |
160 | | - |
161 | 43 | class UnitConverter: |
162 | 44 | """Interface class for spatial unit conversion during field sampling that performs no conversion.""" |
163 | 45 |
|
|
0 commit comments