Skip to content

Commit

Permalink
updated function descriptions and created an endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal committed Jul 12, 2024
1 parent 6d665db commit 9941521
Showing 1 changed file with 92 additions and 8 deletions.
100 changes: 92 additions & 8 deletions hls_vi/generate_stac_items.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import datetime
import json
import os

import click
import argparse
import pystac
import rasterio
import untangle
Expand Down Expand Up @@ -82,6 +80,14 @@


def get_geometry(granule):
"""Function returns the geometry of the HLS_VI granule
Args:
granule (untangle.Element): HLS_VI Granule information from the xml file
Returns:
MultiPolygon: Coordinates information about the granule
"""
multipolygon = []
for poly in granule.Spatial.HorizontalSpatialDomain.Geometry.GPolygon:
ring = []
Expand All @@ -104,6 +110,13 @@ def get_geometry(granule):


def process_common_metadata(item, granule):
"""Function fetches and processes the general information from the granule metadata.
and updates the information in the STAC item.
Args:
item (PyStac item): STAC item created from the HLS_VI granule
granule (untangle.Element): HLS_VI Granule information from the xml file
"""
start_datetime_str = granule.Temporal.RangeDateTime.BeginningDateTime.cdata
item_start_datetime = datetime.datetime.strptime(
start_datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ"
Expand All @@ -126,13 +139,27 @@ def process_common_metadata(item, granule):


def process_eo(item, granule):
"""Function processes the Earth observation information from the STAC item.
Args:
item (PyStac item): STAC item created from the HLS_VI granule
granule (untangle.Element): HLS_VI Granule information from the xml file
"""
eo_extension = EOExtension.ext(item, add_if_missing=True)
for attribute in granule.AdditionalAttributes.AdditionalAttribute:
if attribute.Name == "CLOUD_COVERAGE":
eo_extension.cloud_cover = float(attribute.Values.Value.cdata)


def add_assets(item, granule, endpoint, version):
"""Function adds all the assets to the STAC item
Args:
item (PyStac item): STAC item created from the HLS_VI granule
granule (untangle.Element): HLS_VI Granule information from the xml file
endpoint (_type_): _description_
version (_type_): _description_
"""
item_id = granule.GranuleUR.cdata
product = item_id.split(".")[1]
if product == "S30":
Expand Down Expand Up @@ -165,6 +192,13 @@ def add_assets(item, granule, endpoint, version):


def process_projection(item, granule, band1_file):
"""Function fetches the projection information from the HLS_VI band file and compares if the projection is same for the granule as well as the HLS_VI band image.
Args:
item (PyStac item): STAC item created from the HLS_VI granule
granule (untangle.Element): HLS_VI Granule information from the xml file
band1_file (_type_): _description_
"""
proj_ext = ProjectionExtension.ext(item, add_if_missing=True)
with rasterio.open(band1_file) as band1_dataset:
proj_ext.transform = band1_dataset.transform
Expand All @@ -179,6 +213,12 @@ def process_projection(item, granule, band1_file):


def process_view_geometry(item, granule):
"""Function checks the geometry within the attributes of the STAC item and the HLS_VI granule
Args:
item (PyStac item): STAC item created from the HLS_VI granule
granule (untangle.Element): HLS_VI Granule information from the xml file
"""
view_extension = ViewExtension.ext(item, add_if_missing=True)
for attribute in granule.AdditionalAttributes.AdditionalAttribute:
if attribute.Name == "MEAN_SUN_AZIMUTH_ANGLE":
Expand All @@ -188,15 +228,31 @@ def process_view_geometry(item, granule):


def process_scientific(item, granule):
"""Function checks the attribute value in STAC item and the granule.
Args:
item (PyStac item): STAC item created from the HLS_VI granule
granule (untangle.Element): HLS_VI Granule information from the xml file
"""
scientific_extension = ScientificExtension.ext(item, add_if_missing=True)
for attribute in granule.AdditionalAttributes.AdditionalAttribute:
if attribute.Name == "IDENTIFIER_PRODUCT_DOI":
scientific_extension.doi = attribute.Values.Value.cdata


def cmr_to_item(hls_vi_metadata, endpoint, version):
# band1_file = f"{os.path.splitext(os.path.splitext(cmrxml)[0])[0]}.NDVI.tif"
band1_file = "code/new_test/HLS_VI.L30.T06WVS.2024120T211159.v2.0.NDVI.tif"
"""Function creates a pystac item from the CMR XML file provided as an input
Args:
hls_vi_metadata (str): CMR xml file for the hls_vi granule
out_json (str): name of the JSON file where we want to store the STAC item
endpoint (str): DAAC endpoint to fetch the hls_vi granule from
Returns:
dict: PyStac item in a dictionary form
"""
# provide one of the HLS_VI granules to fetch the projection and other information to generate the STAC item
band1_file = "code/new_test/HLS_VI.L30.T06WVS.2024120T211159.v2.0.NDVI.tif" # Since this was run in a local machine a local file was provided.
cmr = untangle.parse(hls_vi_metadata)
granule = cmr.Granule
item_id = granule.GranuleUR.cdata
Expand Down Expand Up @@ -226,6 +282,14 @@ def cmr_to_item(hls_vi_metadata, endpoint, version):


def create_item(hls_vi_metadata, out_json, endpoint, version):
"""Function acts as an endpoint to create a STAC item for the HLS_VI granule.
Args:
hls_vi_metadata (str): CMR xml file for the hls_vi granule
out_json (str): name of the JSON file where we want to store the STAC item
endpoint (str): DAAC endpoint to fetch the hls_vi granule from
version (str):
"""

hls_vi_metadata = hls_vi_metadata
out_json = out_json
Expand All @@ -236,7 +300,27 @@ def create_item(hls_vi_metadata, out_json, endpoint, version):
json.dump(item, outfile)


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--cmr_xml",
type=str,
default="code/data/HLS-VI.L30.T06WVS.2024120T211159.v2.0.cmr.xml",
)
parser.add_argument("--out_json", type=str, default="test_output.json")
parser.add_argument(
"--endpoint", type=str, default="data.lpdaac.earthdatacloud.nasa.gov"
)
parser.add_argument("--version", type=str, default="020")
args = parser.parse_args()

create_item(
hls_vi_metadata=args.cmr_xml,
out_json=args.out_json,
endpoint=args.endpoint,
version=args.version,
)


if __name__ == "__main__":
cmr_xml = "code/data/HLS-VI.L30.T06WVS.2024120T211159.v2.0.cmr.xml"
output_json = "test_hls_base_updated.json"
create_item(cmr_xml, output_json, "data.lpdaac.earthdatacloud.nasa.gov", "020")
main()

0 comments on commit 9941521

Please sign in to comment.