hydro_time¶
The hydro_time module provides utilities for handling time-related operations in hydrological data processing.
Core Functions¶
t2str¶
1 | |
Converts between datetime string and datetime object.
Example:
1 2 3 4 5 6 7 8 | |
date_to_julian¶
1 | |
Converts a date to its Julian day (day of year).
t_range_days¶
1 | |
Creates a uniformly-spaced array of dates from a date range.
Example:
1 2 3 4 5 | |
t_range_days_timedelta¶
1 | |
Adds a time delta to each date in an array.
generate_start0101_time_range¶
1 2 3 4 5 | |
Generates a time range that resets to January 1st each year.
t_days_lst2range¶
1 | |
Converts a list of dates to a date range [start, end].
assign_time_start_end¶
1 | |
Determines overall start and end times from multiple time ranges.
calculate_utc_offset¶
1 | |
Calculates UTC offset for a location using tzfpy.
get_year¶
1 | |
Extracts the year from various date formats.
intersect¶
1 | |
Finds indices where two time arrays intersect.
API Reference¶
Author: Wenyu Ouyang Date: 2022-12-02 11:03:04 LastEditTime: 2024-09-14 13:57:36 LastEditors: Wenyu Ouyang Description: some functions to deal with time FilePath: \hydroutils\hydroutils\hydro_time.py Copyright (c) 2023-2024 Wenyu Ouyang. All rights reserved.
assign_time_start_end(time_ranges, assign_way='intersection')
¶
Determine start and end times from multiple time ranges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_ranges
|
list
|
List of time range pairs [[start1, end1], [start2, end2], ...]. Each start/end can be any comparable type (datetime, string, etc.). |
required |
assign_way
|
str
|
Method to determine the final range. Defaults to "intersection". - "intersection": Use latest start time and earliest end time. - "union": Use earliest start time and latest end time. |
'intersection'
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(time_start, time_end) The determined start and end times. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If assign_way is not "intersection" or "union". |
Example
ranges = [["2020-01-01", "2020-12-31"], ["2020-03-01", "2021-02-28"]] assign_time_start_end(ranges, "intersection") ("2020-03-01", "2020-12-31") assign_time_start_end(ranges, "union") ("2020-01-01", "2021-02-28")
Source code in hydroutils/hydro_time.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
calculate_utc_offset(lat, lng, date=None)
¶
Calculate the UTC offset for a geographic location.
This function determines the timezone and UTC offset for a given latitude and longitude coordinate pair using the tzfpy library, which provides accurate timezone data based on geographic location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in decimal degrees (-90 to 90). |
required |
lng
|
float
|
Longitude in decimal degrees (-180 to 180). |
required |
date
|
datetime
|
The date to calculate the offset for. Defaults to current UTC time. Important for handling daylight saving time. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
UTC offset in hours, or None if timezone cannot be determined. |
Example
calculate_utc_offset(35.6762, 139.6503) # Tokyo, Japan 9 calculate_utc_offset(51.5074, -0.1278) # London, UK 0 # or 1 during DST
Source code in hydroutils/hydro_time.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
date_to_julian(a_time)
¶
Convert a date to Julian day of the year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_time
|
Union[str, datetime]
|
Date to convert. If string, must be in format 'YYYY-MM-DD'. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
Day of the year (1-366). |
Source code in hydroutils/hydro_time.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
generate_start0101_time_range(start_time, end_time, freq='8D')
¶
Generate a time range with annual reset to January 1st.
This function creates a time range with a specified frequency, but with the special behavior that each year starts from January 1st regardless of the frequency interval. This is particularly useful for creating time series that need to align with calendar years while maintaining a regular interval pattern within each year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_time
|
Union[str, Timestamp]
|
Start date of the range. Can be string ('YYYY-MM-DD') or pandas Timestamp. |
required |
end_time
|
Union[str, Timestamp]
|
End date of the range. Can be string ('YYYY-MM-DD') or pandas Timestamp. |
required |
freq
|
str
|
Time frequency for intervals. Defaults to '8D'. Common values: '7D' (weekly), '10D' (dekadal), etc. |
'8D'
|
Returns:
| Type | Description |
|---|---|
|
pd.DatetimeIndex: Time range with specified frequency and annual reset. |
Example
generate_start0101_time_range('2020-03-15', '2021-02-15', freq='10D') DatetimeIndex(['2020-03-15', '2020-03-25', '2020-04-04', ..., '2021-01-01', '2021-01-11', '2021-02-11'], dtype='datetime64[ns]', freq=None)
Note
- If an interval would cross into a new year, it's truncated and the next interval starts from January 1st of the new year.
- The frequency must be a valid pandas frequency string that represents a fixed duration.
Source code in hydroutils/hydro_time.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
get_year(a_time)
¶
Extract year from various time formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_time
|
Union[date, datetime64, str]
|
Time in various formats. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
Year value. |
Note
Supports datetime.date, numpy.datetime64, and string formats. For strings, assumes YYYY is at the start.
Source code in hydroutils/hydro_time.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
intersect(t_lst1, t_lst2)
¶
Find indices of common elements between two time lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_lst1
|
array - like
|
First time array. |
required |
t_lst2
|
array - like
|
Second time array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(ind1, ind2) where ind1 and ind2 are indices of common elements |
|
|
in t_lst1 and t_lst2 respectively. |
Source code in hydroutils/hydro_time.py
153 154 155 156 157 158 159 160 161 162 163 164 165 | |
t2str(t_)
¶
Convert between datetime string and datetime object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_
|
Union[str, datetime]
|
Input time, either as string or datetime object. |
required |
Returns:
| Type | Description |
|---|---|
|
Union[str, datetime.datetime]: If input is string, returns datetime object. If input is datetime, returns string. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If input type is not supported. |
Note
String format is always "%Y-%m-%d".
Source code in hydroutils/hydro_time.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
t_days_lst2range(t_array)
¶
Transform a list of dates into a start-end interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_array
|
list[Union[datetime64, str]]
|
List of dates in chronological order. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Two-element list containing first and last dates as strings. |
Example
t_days_lst2range(["2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04"]) ["2000-01-01", "2000-01-04"]
Source code in hydroutils/hydro_time.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
t_range_days(t_range, *, step=np.timedelta64(1, 'D'))
¶
Transform a date range into a uniformly-spaced array of dates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_range
|
list
|
Two-element list containing start and end dates as strings. |
required |
step
|
timedelta64
|
Time interval between dates. Defaults to 1 day. |
timedelta64(1, 'D')
|
Returns:
| Type | Description |
|---|---|
array
|
np.array: Array of datetime64 objects with uniform spacing. |
Example
t_range_days(["2000-01-01", "2000-01-05"]) array(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04'], dtype='datetime64[D]')
Source code in hydroutils/hydro_time.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
t_range_days_timedelta(t_array, td=12, td_type='h')
¶
Add a time delta to each date in an array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_array
|
array
|
Array of datetime64 objects (output of t_range_days). |
required |
td
|
int
|
Time period value. Defaults to 12. |
12
|
td_type
|
str
|
Time period unit ('Y','M','D','h','m','s'). Defaults to "h". |
'h'
|
Returns:
| Type | Description |
|---|---|
|
np.array: New array with time delta added to each element. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If td_type is not one of 'Y','M','D','h','m','s'. |
Source code in hydroutils/hydro_time.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
t_range_to_julian(t_range)
¶
Convert a date range to a list of Julian days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_range
|
list
|
Two-element list of dates as strings ["YYYY-MM-DD", "YYYY-MM-DD"]. |
required |
Returns:
| Type | Description |
|---|---|
|
list[int]: List of Julian days for each date in the range. |
Source code in hydroutils/hydro_time.py
187 188 189 190 191 192 193 194 195 196 197 198 | |
t_range_years(t_range)
¶
Get array of years covered by a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_range
|
list
|
Two-element list of dates as strings ["YYYY-MM-DD", "YYYY-MM-DD"]. |
required |
Returns:
| Type | Description |
|---|---|
|
np.array: Array of years covered by the date range. |
Note
- Range is left-closed and right-open interval.
- If end date is not January 1st, end year is included.
- Example: ["2000-01-01", "2002-01-01"] -> [2000, 2001]
- Example: ["2000-01-01", "2002-06-01"] -> [2000, 2001, 2002]
Source code in hydroutils/hydro_time.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |