Skip to content

Commit

Permalink
add a func for calculate_utc_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
OuyangWenyu committed Aug 15, 2024
1 parent be48fd1 commit 58c7f41
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 178 deletions.
5 changes: 2 additions & 3 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dependencies:
- python=3.10
- numpy
- pandas
- tzfpy
# for plotting
- matplotlib
- seaborn
# for gis plotting
Expand All @@ -31,8 +33,5 @@ dependencies:
- grip
- pytest
- pytest-runner
- tzfpy
- geopandas

# for pypi package
- pip
121 changes: 0 additions & 121 deletions hydroutils/hydro_configs.py

This file was deleted.

54 changes: 2 additions & 52 deletions hydroutils/hydro_time.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
"""
Author: Wenyu Ouyang
Date: 2022-12-02 11:03:04
LastEditTime: 2023-07-27 10:01:29
LastEditTime: 2024-08-15 11:21:53
LastEditors: Wenyu Ouyang
Description:
Description: some functions to deal with time
FilePath: \hydroutils\hydroutils\hydro_time.py
Copyright (c) 2023-2024 Wenyu Ouyang. All rights reserved.
"""

import contextlib
import datetime
import tempfile
from typing import Union
import numpy as np
import pytz
import tzfpy
import geopandas as gpd

from hydroutils.hydro_configs import FS


def t2str(t_: Union[str, datetime.datetime]):
Expand Down Expand Up @@ -166,49 +162,3 @@ def calculate_utc_offset(lat, lng, date=None):
if offset is not None:
return int(offset.total_seconds() / 3600)
return None


def calculate_basin_offsets(shp_file_path):
"""
Calculate the UTC offset for each basin based on the outlet shapefile.
Parameters:
shp_file (str): The path to the basin outlet shapefile.
Returns:
dict: A dictionary where the keys are the BASIN_ID and the values are the corresponding UTC offsets.
"""
# read shapefile
if "s3://" in shp_file_path:
# related list
extensions = [".shp", ".shx", ".dbf", ".prj"]

# create a temporary directory
with tempfile.TemporaryDirectory() as tmpdir:
# download all related files to the temporary directory
base_name = shp_file_path.rsplit(".", 1)[0]
extensions = [".shp", ".shx", ".dbf", ".prj"]

for ext in extensions:
remote_file = f"{base_name}{ext}"
local_file = f"{tmpdir}/shp_file{ext}"
with contextlib.suppress(FileNotFoundError):
FS.get(remote_file, local_file)
gdf = gpd.read_file(f"{tmpdir}/shp_file.shp")

else:
# If the file is not on S3 (MinIO), read it directly
gdf = gpd.read_file(shp_file_path)

# create an empty dictionary
basin_offset_dict = {}

for index, row in gdf.iterrows():
outlet = row["geometry"]
offset = calculate_utc_offset(outlet.y, outlet.x)
basin_id = row.get(
"BASIN_ID", index
) # Use the index as the default value if "BASIN_ID" is not found
basin_offset_dict[basin_id] = offset

return basin_offset_dict
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rich
boto3~=1.34.34
minio
s3fs~=2024.2
tzfpy
3 changes: 1 addition & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ rich
boto3==1.34.34
minio
s3fs==2024.2
tzfpy
geopandas
tzfpy

0 comments on commit 58c7f41

Please sign in to comment.