Skip to content

Commit

Permalink
Fix bbox and geometry issues #9, #14
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jul 27, 2022
1 parent 212806c commit 4914207
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 51 deletions.
22 changes: 18 additions & 4 deletions examples/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
}
],
"stac_extensions": [
"https://stac-extensions.github.io/goes/v1.0.0/schema.json",
"https://stac-extensions.github.io/processing/v1.1.0/schema.json",
"https://stac-extensions.github.io/scientific/v1.0.0/schema.json",
"https://stac-extensions.github.io/table/v1.2.0/schema.json",
Expand Down Expand Up @@ -99,6 +98,24 @@
"extent": {
"spatial": {
"bbox": [
[
156.44,
-66.56,
-8.44,
66.56
],
[
156.44,
-66.56,
180.0,
66.56
],
[
-180.0,
-66.56,
-70.44,
66.56
],
[
-141.56,
-66.56,
Expand Down Expand Up @@ -162,9 +179,6 @@
"processing:level": [
"L2"
],
"goes:image-type": [
"FULL DISK"
],
"goes:orbital-slot": [
"West",
"East"
Expand Down
7 changes: 4 additions & 3 deletions examples/item.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
"processing:facility": "WCDAS",
"goes:orbital-slot": "East",
"goes:system-environment": "OR",
"goes:image-type": "FULL DISK",
"goes-glm:product_time": 662731180.0,
"goes-glm:lightning_wavelength": 777.3699951171875,
"goes-glm:group_time_threshold": 0.0,
"goes-glm:flash_time_threshold": 3.3299999237060547,
"goes-glm:lat_field_of_view": 0.0,
"goes-glm:event_count": 11236,
"goes-glm:group_count": 3706,
"goes-glm:flash_count": 179,
Expand All @@ -30,9 +28,12 @@
"goes-glm:nominal_satellite_subpoint_lat": 0.0,
"goes-glm:nominal_satellite_height": 35786.0234375,
"goes-glm:nominal_satellite_subpoint_lon": -75.19999694824219,
"goes-glm:lon_field_of_view": -75.0,
"goes-glm:percent_uncorrectable_L0_errors": 0.0,
"proj:epsg": 4326,
"proj:centroid": {
"lat": 0.0,
"lon": -75.0
},
"datetime": "2020-12-31T23:59:50.200000Z"
},
"geometry": {
Expand Down
49 changes: 46 additions & 3 deletions src/stactools/goes_glm/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
PLATFORM_T = "GOES-18"
PLATFORMS = [PLATFORM_R, PLATFORM_S] # We can add PLATFORM_T at some point
INSTRUMENTS = ["FM1", "FM2"] # We can add FM3 for GOES-18 at some point
GOES_IMAGE_TYPE = "FULL DISK"


class Platforms(str, enum.Enum):
Expand Down Expand Up @@ -111,5 +110,49 @@ class OrbitalSlot(str, enum.Enum):
SOURCE_CRS = "EPSG:4326"
TARGET_CRS = 4326

# Bounding box
BBOXES = [[-141.56, -66.56, -8.44, 66.56]]
# Bounding boxes and geometries - note: west crosses the antimeridian!
ITEM_BBOX_WEST = [156.44, -66.56, -70.44, 66.56]
ITEM_BBOX_EAST = [-141.56, -66.56, -8.44, 66.56]
COLLECTION_BBOXES = [
# Union
[156.44, -66.56, -8.44, 66.56],
# Split west into two parts as it crosses the antimeridian
[156.44, -66.56, 180.0, 66.56],
[-180.0, -66.56, -70.44, 66.56],
# East
ITEM_BBOX_EAST,
]

# Split west into two polygons as propsoed by the GeoJSON spec as it crosses the antimeridian
GEOMETRY_WEST = {
"type": "Polygon",
"coordinates": [
[
[156.44, 66.56],
[180, 66.56],
[180, -66.56],
[156.44, -66.56],
[156.44, 66.56],
],
[
[-180, 66.56],
[-70.44, 66.56],
[-70.44, -66.56],
[-180, -66.56],
[-180, 66.56],
],
],
}
# East
GEOMETRY_EAST = {
"type": "Polygon",
"coordinates": [
[
[-141.56, 66.56],
[-8.44, 66.56],
[-8.44, -66.56],
[-141.56, -66.56],
[-141.56, 66.56],
]
],
}
45 changes: 22 additions & 23 deletions src/stactools/goes_glm/stac.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
from typing import Optional

from dateutil.parser import isoparse
from netCDF4 import Dataset
Expand Down Expand Up @@ -58,7 +58,7 @@ def create_collection(
start_datetime = isoparse(start_time)

extent = Extent(
SpatialExtent(constants.BBOXES),
SpatialExtent(constants.COLLECTION_BBOXES),
TemporalExtent([[start_datetime, None]]),
)

Expand Down Expand Up @@ -86,7 +86,6 @@ def create_collection(
"instruments": constants.INSTRUMENTS,
"gsd": [constants.RESOLUTION],
"processing:level": [constants.PROCESSING_LEVEL],
"goes:image-type": [constants.GOES_IMAGE_TYPE],
"goes:orbital-slot": [e.value for e in constants.OrbitalSlot],
}
)
Expand Down Expand Up @@ -187,9 +186,7 @@ def create_item(
if sys_env != "OR":
logger.warning("You are ingesting test data.")

bbox = constants.BBOXES[0]
geometry = bbox_to_polygon(bbox) # todo: check whether this makes sense #9 #14

slot = constants.OrbitalSlot[dataset.orbital_slot.replace("-", "_")]
properties = {
"start_datetime": dataset.time_coverage_start,
"end_datetime": dataset.time_coverage_end,
Expand All @@ -200,19 +197,31 @@ def create_item(
"gsd": constants.RESOLUTION,
"processing:level": constants.PROCESSING_LEVEL,
"processing:facility": dataset.production_site,
"goes:orbital-slot": constants.OrbitalSlot[
dataset.orbital_slot.replace("-", "_")
],
"goes:orbital-slot": slot,
"goes:system-environment": sys_env,
"goes:image-type": constants.GOES_IMAGE_TYPE,
}

if slot == constants.OrbitalSlot.GOES_East:
bbox = constants.ITEM_BBOX_EAST
geometry = constants.GEOMETRY_EAST
elif slot == constants.OrbitalSlot.GOES_West:
bbox = constants.ITEM_BBOX_WEST
geometry = constants.GEOMETRY_WEST
else:
bbox = None
geometry = None

centroid = {}
for key, var in dataset.variables.items():
if len(var.dimensions) != 0:
continue

ma = var[...]
if ma.count() > 0:
if var.name == "lat_field_of_view":
centroid["lat"] = ma.tolist()
elif var.name == "lon_field_of_view":
centroid["lon"] = ma.tolist()
elif ma.count() > 0:
properties[f"goes-glm:{var.name}"] = ma.tolist()

item = Item(
Expand All @@ -233,6 +242,8 @@ def create_item(

proj = ProjectionExtension.ext(item, add_if_missing=True)
proj.epsg = constants.TARGET_CRS
if len(centroid) == 2:
proj.centroid = centroid

if not nogeoparquet:
target_folder = os.path.dirname(asset_href)
Expand All @@ -255,18 +266,6 @@ def create_item(
return item


def bbox_to_polygon(b: List[float]) -> Dict[str, Any]:
"""
Converts a STAC bounding box to a GeoJSON polygon.
"""
return {
"type": "Polygon",
"coordinates": [
[[b[0], b[3]], [b[2], b[3]], [b[2], b[1]], [b[0], b[1]], [b[0], b[3]]]
],
}


def center_datetime(start: str, end: str) -> datetime:
"""
Takes the start and end datetime and computes the central datetime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
"processing:facility": "WCDAS",
"goes:orbital-slot": "East",
"goes:system-environment": "OR",
"goes:image-type": "FULL DISK",
"goes-glm:product_time": 662731180.0,
"goes-glm:lightning_wavelength": 777.3699951171875,
"goes-glm:group_time_threshold": 0.0,
"goes-glm:flash_time_threshold": 3.3299999237060547,
"goes-glm:lat_field_of_view": 0.0,
"goes-glm:event_count": 11236,
"goes-glm:group_count": 3706,
"goes-glm:flash_count": 179,
Expand All @@ -30,9 +28,12 @@
"goes-glm:nominal_satellite_subpoint_lat": 0.0,
"goes-glm:nominal_satellite_height": 35786.0234375,
"goes-glm:nominal_satellite_subpoint_lon": -75.19999694824219,
"goes-glm:lon_field_of_view": -75.0,
"goes-glm:percent_uncorrectable_L0_errors": 0.0,
"proj:epsg": 4326,
"proj:centroid": {
"lat": 0.0,
"lon": -75.0
},
"datetime": "2020-12-31T23:59:50.200000Z"
},
"geometry": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
"processing:facility": "WCDAS",
"goes:orbital-slot": "West",
"goes:system-environment": "OR",
"goes:image-type": "FULL DISK",
"goes-glm:product_time": 707562000.0,
"goes-glm:lightning_wavelength": 777.3699951171875,
"goes-glm:group_time_threshold": 0.0,
"goes-glm:flash_time_threshold": 3.3299999237060547,
"goes-glm:lat_field_of_view": 0.0,
"goes-glm:event_count": 1229,
"goes-glm:group_count": 811,
"goes-glm:flash_count": 117,
Expand All @@ -30,33 +28,58 @@
"goes-glm:nominal_satellite_subpoint_lat": 0.0,
"goes-glm:nominal_satellite_height": 35786.0234375,
"goes-glm:nominal_satellite_subpoint_lon": -137.1999969482422,
"goes-glm:lon_field_of_view": -137.0,
"goes-glm:percent_uncorrectable_L0_errors": 0.0,
"proj:epsg": 4326,
"proj:centroid": {
"lat": 0.0,
"lon": -137.0
},
"datetime": "2022-06-03T21:00:10Z"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-141.56,
156.44,
66.56
],
[
180,
66.56
],
[
180,
-66.56
],
[
156.44,
-66.56
],
[
156.44,
66.56
]
],
[
[
-180,
66.56
],
[
-8.44,
-70.44,
66.56
],
[
-8.44,
-70.44,
-66.56
],
[
-141.56,
-180,
-66.56
],
[
-141.56,
-180,
66.56
]
]
Expand Down Expand Up @@ -656,9 +679,9 @@
}
},
"bbox": [
-141.56,
156.44,
-66.56,
-8.44,
-70.44,
66.56
],
"stac_extensions": [
Expand Down
21 changes: 18 additions & 3 deletions tests/data-files/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@
"extent": {
"spatial": {
"bbox": [
[
156.44,
-66.56,
-8.44,
66.56
],
[
156.44,
-66.56,
180.0,
66.56
],
[
-180.0,
-66.56,
-70.44,
66.56
],
[
-141.56,
-66.56,
Expand Down Expand Up @@ -155,9 +173,6 @@
"processing:level": [
"L2"
],
"goes:image-type": [
"FULL DISK"
],
"goes:orbital-slot": [
"West",
"East"
Expand Down
2 changes: 0 additions & 2 deletions tests/test_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def test_create_collection(self) -> None:
self.assertEqual(summaries["instruments"], ["FM1", "FM2"])
self.assertEqual(summaries["gsd"], [8000])
self.assertEqual(summaries["processing:level"], ["L2"])
self.assertEqual(summaries["goes:image-type"], ["FULL DISK"])
self.assertEqual(summaries["goes:orbital-slot"], ["West", "East"])

self.assertTrue("item_assets" in collection_dict)
Expand Down Expand Up @@ -162,7 +161,6 @@ def test_create_item(self) -> None:
self.assertEqual(item.properties["instruments"], [f"FM{instrument}"])
self.assertEqual(item.properties["gsd"], 8000)
self.assertEqual(item.properties["goes:system-environment"], "OR")
self.assertEqual(item.properties["goes:image-type"], "FULL DISK")
if (
id
== "OR_GLM-L2-LCFA_G16_s20203662359400_e20210010000004_c20210010000030"
Expand Down

0 comments on commit 4914207

Please sign in to comment.