Skip to content

Commit

Permalink
Uniformize field names
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuarte47 committed Nov 25, 2022
1 parent 9d03d4a commit 67ca91f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
48 changes: 48 additions & 0 deletions geodataflow/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import json
import datetime
from typing import Tuple


class CaseInsensitiveDict(dict):
Expand Down Expand Up @@ -83,3 +84,50 @@ def default(self, obj):
return obj.strftime('%Y-%m-%d')

return json.JSONEncoder.default(self, obj)


class DateUtils:
"""
Provides useful methods to manage Datetimes.
"""
@staticmethod
def parse_date_range(startDate: str = None,
endDate: str = None,
closestToDate: str = None,
windowDate: int = 10) -> Tuple[datetime.datetime, datetime.datetime]:
"""
Returns a Date range according to the specified criteria.
"""
today_date = datetime.date.today()

if isinstance(startDate, str):
start_date = today_date \
if startDate.upper() == '$TODAY()' \
else datetime.datetime.strptime(startDate, '%Y-%m-%d')

elif startDate is None and closestToDate:
temps_time = datetime.datetime.strptime(closestToDate, '%Y-%m-%d')
start_date = closestToDate \
if not windowDate \
else (temps_time - datetime.timedelta(days=windowDate)).strftime('%Y-%m-%d')

start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
else:
start_date = startDate if startDate else today_date

if isinstance(endDate, str):
final_date = today_date \
if endDate.upper() == '$TODAY()' \
else datetime.datetime.strptime(endDate, '%Y-%m-%d')

elif startDate is None and closestToDate:
temps_time = datetime.datetime.strptime(closestToDate, '%Y-%m-%d')
final_date = closestToDate \
if not windowDate \
else (temps_time + datetime.timedelta(days=windowDate)).strftime('%Y-%m-%d')

final_date = datetime.datetime.strptime(final_date, '%Y-%m-%d')
else:
final_date = endDate if endDate else today_date

return start_date, final_date
55 changes: 14 additions & 41 deletions geodataflow/pipeline/filters/EOProductCatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import datetime
from typing import Dict, Iterable

from geodataflow.core.common import CaseInsensitiveDict
from geodataflow.core.common import CaseInsensitiveDict, DateUtils
from geodataflow.pipeline.basictypes import AbstractFilter
from geodataflow.eogeo.productcatalog import ProductCatalog

Expand Down Expand Up @@ -131,39 +131,10 @@ def params(self) -> Dict:

def fetch_products(self, geometry, input_crs, app_config: Dict, limit: int = 1000) -> Iterable:
"""
Ferch EO Products using current settings.
Fetch EO Products using current settings.
"""
today_date = datetime.date.today()

if isinstance(self.startDate, str):
start_date = today_date \
if self.startDate.upper() == '$TODAY()' \
else datetime.datetime.strptime(self.startDate, '%Y-%m-%d')

elif self.startDate is None and self.closestToDate:
temps_time = datetime.datetime.strptime(self.closestToDate, '%Y-%m-%d')
start_date = self.closestToDate \
if not self.windowDate \
else (temps_time - datetime.timedelta(days=self.windowDate)).strftime('%Y-%m-%d')

start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
else:
start_date = self.startDate if self.startDate else today_date

if isinstance(self.endDate, str):
final_date = today_date \
if self.endDate.upper() == '$TODAY()' \
else datetime.datetime.strptime(self.endDate, '%Y-%m-%d')

elif self.startDate is None and self.closestToDate:
temps_time = datetime.datetime.strptime(self.closestToDate, '%Y-%m-%d')
final_date = self.closestToDate \
if not self.windowDate \
else (temps_time + datetime.timedelta(days=self.windowDate)).strftime('%Y-%m-%d')

final_date = datetime.datetime.strptime(final_date, '%Y-%m-%d')
else:
final_date = self.endDate if self.endDate else today_date
start_date, final_date = \
DateUtils.parse_date_range(self.startDate, self.endDate, self.closestToDate, self.windowDate)

product_filter = dict()
if self.filter:
Expand Down Expand Up @@ -211,7 +182,8 @@ def starting_run(self, schema_def, pipeline, processing_args):
app_config = pipeline.config
field_dict = {f.name: f for f in schema_def.fields}
new_fields = [
FieldDef(name='gdf:product_date', data_type=DataType.String)
FieldDef(name='productType', data_type=DataType.String),
FieldDef(name='productDate', data_type=DataType.String)
]
for product in self.fetch_products(geometry, schema_def.crs, app_config, limit=1):
properties = product.properties
Expand Down Expand Up @@ -278,12 +250,12 @@ def run(self, feature_store, processing_args):
]
logging.info("Available {} EO Products for type '{}'.".format(len(eo_products), self.product))

available_dates = set([product.properties.get('gdf:product_date') for product in eo_products])
available_dates = set([product.properties.get('productDate') for product in eo_products])
report_info['availableDates'] = list(available_dates)

# Return results.
for product in self.pass_products(eo_products):
report_info['selectedDate'] = product.properties.get('gdf:product_date')
report_info['selectedDate'] = product.properties.get('productDate')
if inv_transform_fn:
product.geometry = inv_transform_fn(product.geometry)

Expand All @@ -307,7 +279,7 @@ def pass_products(self, products: Iterable[object]) -> Iterable[object]:
temp_list = list()

for product in products:
product_date = product.properties.get('gdf:product_date')
product_date = product.properties.get('productDate')
product_time = datetime.datetime.strptime(product_date, '%Y-%m-%d')
product_diff = abs(product_time - closest_time).days
temp_list.append((product_date, product_time, product_diff))
Expand All @@ -329,17 +301,18 @@ def normalize_product(self, product: object, feature: object) -> object:
case_props = CaseInsensitiveDict(product.properties)
attributes = feature.properties.copy()

for name, value in product.properties.items():
attributes[name] = value if not isinstance(value, list) else ','.join([str(v) for v in value])

product_date = \
case_props.get('startTimeFromAscendingNode') or \
case_props.get('beginPosition') or \
case_props.get('ingestionDate') or \
case_props.get('publicationDate') or \
case_props.get('datetime')

attributes['gdf:product_date'] = product_date[0:10]

for name, value in product.properties.items():
attributes[name] = value if not isinstance(value, list) else ','.join([str(v) for v in value])
attributes['productType'] = self.product
attributes['productDate'] = product_date[0:10]

product.areaOfInterest = feature
product.properties = attributes
Expand Down
2 changes: 1 addition & 1 deletion geodataflow/pipeline/filters/EOProductDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def run(self, feature_store, processing_args):

# Group EO Products by Date.
for product in EOProductCatalog.run(self, feature_store, processing_args):
product_date = product.properties.get('gdf:product_date')
product_date = product.properties.get('productDate')

curr_list = product_groups.get(product_date, [])
curr_list.append(product)
Expand Down

0 comments on commit 67ca91f

Please sign in to comment.