Skip to content

Commit

Permalink
Updated to add utility functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
petebunting committed Dec 4, 2024
1 parent 5c81cfe commit 9a059c2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pb_gee_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

PB_GEE_TOOLS_VERSION_MAJOR = 0
PB_GEE_TOOLS_VERSION_MINOR = 0
PB_GEE_TOOLS_VERSION_PATCH = 5
PB_GEE_TOOLS_VERSION_PATCH = 6

PB_GEE_TOOLS_VERSION = "{}.{}.{}".format(
PB_GEE_TOOLS_VERSION_MAJOR,
Expand Down
16 changes: 8 additions & 8 deletions pb_gee_tools/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_sr_landsat_collection(
ls_start = datetime.datetime(year=1982, month=7, day=16)
ls_end = datetime.datetime.now()

if not pb_gee_tools.utils._do_dates_overlap(
if not pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=ls_start, e2_date=ls_end
):
raise Exception(
Expand Down Expand Up @@ -143,23 +143,23 @@ def apply_scale_factors(image):
use_ls7 = False
use_ls5 = False
use_ls4 = False
if pb_gee_tools.utils._do_dates_overlap(
if pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=ls9_start, e2_date=ls9_end
):
use_ls9 = True
if pb_gee_tools.utils._do_dates_overlap(
if pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=ls8_start, e2_date=ls8_end
):
use_ls8 = True
if pb_gee_tools.utils._do_dates_overlap(
if pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=ls7_start, e2_date=ls7_end
):
use_ls7 = True
if pb_gee_tools.utils._do_dates_overlap(
if pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=ls5_start, e2_date=ls5_end
):
use_ls5 = True
if pb_gee_tools.utils._do_dates_overlap(
if pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=ls4_start, e2_date=ls4_end
):
use_ls4 = True
Expand Down Expand Up @@ -241,7 +241,7 @@ def get_sen2_sr_harm_collection(
sen2_start = datetime.datetime(year=2015, month=7, day=1)
sen2_end = datetime.datetime.now()

if not pb_gee_tools.utils._do_dates_overlap(
if not pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=sen2_start, e2_date=sen2_end
):
raise Exception(
Expand Down Expand Up @@ -390,7 +390,7 @@ def _add_ndpi(s1_img):
sen1_start = datetime.datetime(year=2014, month=4, day=1)
sen1_end = datetime.datetime.now()

if not pb_gee_tools.utils._do_dates_overlap(
if not pb_gee_tools.utils.do_dates_overlap(
s1_date=start_date, e1_date=end_date, s2_date=sen1_start, e2_date=sen1_end
):
raise Exception(
Expand Down
92 changes: 91 additions & 1 deletion pb_gee_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
# History:
# Version 1.0 - Created.

from typing import List, Tuple
import datetime


def _do_dates_overlap(
def do_dates_overlap(
s1_date: datetime.datetime,
e1_date: datetime.datetime,
s2_date: datetime.datetime,
Expand All @@ -51,3 +52,92 @@ def _do_dates_overlap(
delta = (earliest_end - latest_start).days + 1
overlap = max(0, delta)
return overlap > 0


def create_year_month_start_end_lst(
start_year: int, start_month: int, end_year: int, end_month: int
) -> List[Tuple[int, int]]:
"""
A function which creates a list of year and month tuples from a start
and end month and year.
:param start_year: int with the start year
:param start_month: int with the start month
:param end_year: int with the end year
:param end_month: int with the end month
:return: List of tuples (year, month)
"""
import numpy

out_year_mnt_lst = list()
years = numpy.arange(start_year, end_year + 1, 1)
for year in years:
if (year == start_year) and (year == end_year):
months = numpy.arange(start_month, end_month + 1, 1)
elif year == start_year:
months = numpy.arange(start_month, 13, 1)
elif year == end_year:
months = numpy.arange(1, end_month + 1, 1)
else:
months = numpy.arange(1, 13, 1)
for month in months:
out_year_mnt_lst.append((year, month))

return out_year_mnt_lst

def create_year_month_n_months_lst(
start_year: int, start_month: int, n_months: int
) -> List[Tuple[int, int]]:
"""
A function which creates a list of year and month tuples from a start
and end month and year.
:param start_year: int with the start year
:param start_month: int with the start month
:param n_months: int with the number of months ahead
:return: List of tuples (year, month)
"""
import numpy

if start_year < 0:
raise Exception("Year must be positive")
if (start_month < 1) or (start_month > 12):
raise Exception("Month must be between 1-12")

out_year_mnt_lst = list()
out_year_mnt_lst.append((start_year, start_month))
months = numpy.arange(0, n_months, 1)
months = months + start_month

month_vals = months % 12
year = start_year
first = True
for month in month_vals:
if first:
out_year_mnt_lst.append((year, month + 1))
first = False
else:
if month == 0:
year += 1
out_year_mnt_lst.append((year, month + 1))

return out_year_mnt_lst

def find_month_end_date(year: int, month: int) -> int:
"""
A function which returns the date of the last day of the month.
:param year: int for the year (e.g., 2019)
:param month: month (e.g., 9)
:return: last day of the month date
"""
import calendar
import numpy

cal = calendar.Calendar()
month_days = cal.monthdayscalendar(year, month)
max_day_month = numpy.array(month_days).flatten().max()
return max_day_month

0 comments on commit 9a059c2

Please sign in to comment.