Skip to content

Commit

Permalink
Calculate true timeliness
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Oct 30, 2023
1 parent 1523802 commit 0d9a429
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/data_processing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def create_product_put_payload(
) -> dict:
"""Creates put payload for data portal."""
with netCDF4.Dataset(full_path, "r") as nc:
start_time, stop_time = get_data_timestamps(nc)
payload = {
"product": product or nc.cloudnet_file_type,
"site": site or nc.location.lower(),
Expand All @@ -49,6 +50,8 @@ def create_product_put_payload(
"uuid": getattr(nc, "file_uuid", ""),
"pid": getattr(nc, "pid", ""),
"software": {"cloudnet-processing": get_data_processing_version()},
"startTime": start_time,
"stopTime": stop_time,
**storage_service_response,
}
if instrument_pid := getattr(nc, "instrument_pid", None):
Expand All @@ -68,6 +71,24 @@ def create_product_put_payload(
return payload


def get_data_timestamps(nc: netCDF4) -> tuple[str, str]:
"""Returns first timestamp."""
t1 = _get_datetime(nc, ind=0)
t2 = _get_datetime(nc, ind=-1)
time_format = "%Y-%m-%dT%H:%M:%S%z"
return t1.strftime(time_format), t2.strftime(time_format)


def _get_datetime(nc: netCDF4, ind: int) -> datetime.datetime:
y, m, d = int(nc.year), int(nc.month), int(nc.day)
tz = datetime.timezone.utc
base = datetime.datetime(y, m, d, tzinfo=tz)
fraction_hour = float(nc.variables["time"][:][ind])
delta_seconds = 1 if fraction_hour == 24 else 0
delta = datetime.timedelta(seconds=delta_seconds)
return base + datetime.timedelta(hours=fraction_hour) - delta


def get_data_processing_version() -> str:
version_file = Path(os.path.abspath(data_processing.version.__file__))
version: dict = {}
Expand Down

0 comments on commit 0d9a429

Please sign in to comment.