Skip to content

Commit

Permalink
Merge pull request #40 from CH-Earth/fix_functions
Browse files Browse the repository at this point in the history
fixes and style change
  • Loading branch information
wknoben authored Mar 11, 2024
2 parents ad41747 + 9445e04 commit dd13e95
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
3 changes: 2 additions & 1 deletion python_cs_functions/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def calculate_signatures(hyd, pre, source, l_values, l_index):
slopes = []
for year,group in groups:
flows = group.values.copy()
if (flows == 0).all() or (np.isnan(flows).all()):
if np.logical_or(np.isnan(flows),flows == 0).all():
slopes.append(np.nan) # so we can ignore these years
else:
# Account for NaNs - we don't want these to influence the calculations
Expand Down Expand Up @@ -1312,6 +1312,7 @@ def get_river_attributes(riv_str, l_values, l_index, area):
# Load shapefiles
river = gpd.read_file(riv_str)
river = river.set_index('COMID')
river = river[~river.index.duplicated(keep='first')] # Removes any duplicate river segments

# Check if we actually have a river segment (empty shapefile is not the same as no shapefile)
if len(river) > 0:
Expand Down
2 changes: 1 addition & 1 deletion python_cs_functions/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import urllib.request
from pathlib import Path

def download_url_into_folder(url,folder, retries_max=10, requests_kwargs={}, overwrite=False):
def download_url_into_folder(url, folder, retries_max=10, requests_kwargs={}, overwrite=False):

# Extract the filename from the URL
file_name = url.split('/')[-1].strip() # Get the last part of the url, strip whitespace and characters
Expand Down
42 changes: 28 additions & 14 deletions python_cs_functions/geospatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,22 @@ def download_era5_time_invariant_data_to_netcdf(coordinates,file,retries_max=10)
break
return

def find_first_day_next_month(time):

'''Takes a datetime and find day 1 on the next month'''

# Initial settings: day 1 of a month in the same year
new_day = 1
new_month = time.month+1
new_year = time.year

# Check if we're at the end of the year
if new_month == 13:
new_month = 1
new_year = time.year+1

return time.replace(year=new_year, month=new_month, day=new_day)

def convert_start_and_end_dates_to_era5_download_lists(start,end):

'''Takes two datetime.datetime(y,m,d,h,min) objects and returns two lists with start and end dates for ERA5 downloads at monthly intervals'''
Expand All @@ -1028,25 +1044,23 @@ def convert_start_and_end_dates_to_era5_download_lists(start,end):

# Add to start list
start_l.append(cur)

# Figure out the index of the next month and if the year changes:
tmp = cur + timedelta(days=31) # Add 31 days to current date to ensure we're in the next month, might also switch the year
next_month = tmp.month # Extract 'month' from this object
next_year = tmp.year # Extract the year too. If we ticked over into a new year we need to track this, otherwise we never increment the year

# Create the end-of-month date
cur = cur.replace(year=next_year, month=next_month) - timedelta(days=1)

# Find what the 1st day of the next month is
new_start = find_first_day_next_month(cur)

# Find the end date of the current month
cur_end = new_start - timedelta(days=1)

# Ensure this does not step over our end date
if cur >= end:
cur = end
if cur_end >= end:
cur_end = end

# Add to end list
end_l.append(cur)
end_l.append(cur_end)

# Update variable for while loop
cur = new_start

# Add 1 day to create the new start-of-month date
cur = cur + timedelta(days=1)

return start_l,end_l

# --- ERA5 processing ---
Expand Down

0 comments on commit dd13e95

Please sign in to comment.