From 10c0262b039fbd51e1663f4f5df3026c0b6af339 Mon Sep 17 00:00:00 2001
From: Simon Grasse <s.grasse@fz-juelich.de>
Date: Tue, 19 Dec 2023 13:35:40 +0100
Subject: [PATCH] cleanup imports, refactor metadata functions

---
 toargridding/gridding.py |  4 ++--
 toargridding/metadata.py | 33 ++++++++++++++++-----------------
 2 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/toargridding/gridding.py b/toargridding/gridding.py
index 6bc4025..3268dc2 100644
--- a/toargridding/gridding.py
+++ b/toargridding/gridding.py
@@ -15,8 +15,8 @@ def get_gridded_toar_data(
     grid: GridDefinition,
     time: TimeSample,
     variables: list[str],
-    stats: tuple[list[str], list[Metadata]],
-) -> list[xr.Dataset]:
+    stats: list[str],
+) -> tuple[list[xr.Dataset], list[Metadata]]:
     metadatas = [
         Metadata.construct(var, stat, time) for var, stat in product(variables, stats)
     ]
diff --git a/toargridding/metadata.py b/toargridding/metadata.py
index bdb6d29..43ba5be 100644
--- a/toargridding/metadata.py
+++ b/toargridding/metadata.py
@@ -1,12 +1,9 @@
 from datetime import datetime
 from enum import Enum
 from dataclasses import dataclass
-from collections import namedtuple
-import json
 
 import numpy as np
 import pandas as pd
-import xarray as xr
 
 from toargridding.toarstats_constants import STATISTICS_LIST
 from toargridding.static_metadata import global_cf_attributes, TOARVariable
@@ -62,6 +59,15 @@ class Metadata:
 
         return Metadata(variable, stat, time)
 
+    def get_id(self):
+        return f"{self.variable.name}_{self.statistic}_{self.time.daterange_option}_at_{datetime.now().date().isoformat()}"
+
+    def get_title(self):
+        return f"{self.time.sampling} {self.statistic} statistic for {self.variable.standart_name} from {self.time.start} to {self.time.end} aggregated on global grid"
+
+    def get_summary(self):
+        return f"{self.time.sampling} {self.statistic} statistic for {self.variable.standart_name} from {self.time.start} to {self.time.end} aggregated on global grid"
+
 
 @dataclass
 class AnalysisRequestResult:
@@ -70,10 +76,11 @@ class AnalysisRequestResult:
     metadata: Metadata
 
 
-def get_global_attributes(metadata):
-    metadata = {
-        "title": get_title(metadata),
-        "summary": get_summary(metadata),
+def get_global_attributes(metadata: Metadata):
+    dynamic_cf_attributes = {
+        "id": metadata.get_id(),
+        "title": metadata.get_title(),
+        "summary": metadata.get_summary(),
         "date_created": date_created,
         "history": f"{date_created}: File created by toargridding package using data from toar-analysis service",
         # "geospatial_bounds": 0,  # for polygonal desrcription
@@ -86,16 +93,8 @@ def get_global_attributes(metadata):
         # "time_coverage_duration": 0, # TODO insert durations
         # "time_coverage_resolution": 0,
     }
-    metadata = metadata | global_cf_attributes
-    return metadata
-
-
-def get_title(metadata: Metadata):
-    return f"{metadata.time.sampling} {metadata.statistic} statistic for {metadata.variable.standart_name} from {metadata.time.start} to {metadata.time.end} aggregated on global grid"
-
-
-def get_summary(metadata: Metadata):  # TODO make summary different from title
-    return f"{metadata.time.sampling} {metadata.statistic} statistic for {metadata.variable.standart_name} from {metadata.time.start} to {metadata.time.end} aggregated on global grid"
+    cf_attributes = dynamic_cf_attributes | global_cf_attributes
+    return cf_attributes
 
 
 def get_cf_metadata(variable: Variables, metadata: Metadata | None):
-- 
GitLab