Skip to content

Commit

Permalink
Fix activity:end in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
GernotMaier committed Dec 19, 2024
1 parent 0e6c23b commit c73e42b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,12 @@ def main():
logger = logging.getLogger()
logger.setLevel(gen.get_log_level_from_user(args_dict["log_level"]))

json_type = args_dict.get("input", "").endswith(".json")
# simplified metadata treatment for model parameter json files
if json_type:
if args_dict.get("input", "").endswith(".json"):
site = args_dict.get("site", None)
top_level_meta = None
validate_schema_file = None
metadata, validate_schema_file = None, None
else:
metadata = MetadataCollector(args_dict=args_dict, data_model_name=data_model_name)
site = metadata.get_site(from_input_meta=True)
top_level_meta = metadata.top_level_meta
validate_schema_file = metadata.get_data_model_schema_file_name()

layout = array_layout.ArrayLayout(
Expand All @@ -172,12 +168,12 @@ def main():
if args_dict["export"] is not None:
product_data = (
layout.export_one_telescope_as_json(crs_name=args_dict["export"])
if json_type
if args_dict.get("input", "").endswith(".json")
else layout.export_telescope_list_table(crs_name=args_dict["export"])
)
writer.ModelDataWriter.dump(
args_dict=args_dict,
metadata=top_level_meta,
metadata=metadata.get_top_level_metadata() if metadata else None,
product_data=product_data,
validate_schema_file=validate_schema_file,
)
Expand Down
2 changes: 1 addition & 1 deletion src/simtools/applications/submit_data_from_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def main(): # noqa: D103

writer.ModelDataWriter.dump(
args_dict=args_dict,
metadata=_metadata.top_level_meta,
metadata=_metadata.get_top_level_metadata(),
product_data=data_validator.validate_and_transform(),
)

Expand Down
18 changes: 18 additions & 0 deletions src/simtools/data_model/metadata_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ def collect_meta_data(self):
except AttributeError:
self._logger.debug(f"Method _fill_{meta_type}_meta not implemented")

def get_top_level_metadata(self):
"""
Return top level metadata dictionary (with updated activity end time).
Returns
-------
dict
Top level metadata dictionary.
"""
try:
self.top_level_meta[self.observatory]["activity"][
"end"
] = datetime.datetime.now().isoformat(timespec="seconds")
except KeyError:
pass
return self.top_level_meta

def get_data_model_schema_file_name(self):
"""
Return data model schema file name.
Expand Down
2 changes: 1 addition & 1 deletion src/simtools/data_model/model_data_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def dump_model_parameter(
metadata_input_dict["output_file"] = output_file
metadata_input_dict["output_file_format"] = Path(output_file).suffix.lstrip(".")
writer.write_metadata_to_yml(
metadata=MetadataCollector(args_dict=metadata_input_dict).top_level_meta,
metadata=MetadataCollector(args_dict=metadata_input_dict).get_top_level_metadata(),
yml_file=output_path / f"{Path(output_file).stem}",
)
return _json_dict
Expand Down
2 changes: 1 addition & 1 deletion src/simtools/ray_tracing/mirror_panel_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,6 @@ def write_optimization_data(self):
)
writer.ModelDataWriter.dump(
args_dict=self.args_dict,
metadata=MetadataCollector(args_dict=self.args_dict).top_level_meta,
metadata=MetadataCollector(args_dict=self.args_dict).get_top_level_metadata(),
product_data=result_table,
)
21 changes: 21 additions & 0 deletions tests/unit_tests/data_model/test_metadata_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import getpass
import json
import logging
import time
import uuid
from importlib.resources import files
from pathlib import Path
Expand Down Expand Up @@ -71,6 +72,26 @@ def test_get_data_model_schema_dict(args_dict_site):
assert metadata.get_data_model_schema_dict() == {}


def test_get_top_level_metadata(args_dict_site):

collector = metadata_collector.MetadataCollector(args_dict=args_dict_site)
assert (
collector.top_level_meta["cta"]["activity"]["end"]
== collector.top_level_meta["cta"]["activity"]["start"]
)

# no update when activity cannot be found in the metadata
time.sleep(1)
collector.observatory = "not_cta"
top_level_meta = collector.get_top_level_metadata()
assert top_level_meta["cta"]["activity"]["end"] == top_level_meta["cta"]["activity"]["start"]

time.sleep(1)
collector.observatory = "cta" # back to default
top_level_meta = collector.get_top_level_metadata()
assert top_level_meta["cta"]["activity"]["end"] > top_level_meta["cta"]["activity"]["start"]


def test_fill_contact_meta(args_dict_site):
contact_dict = {}
collector = metadata_collector.MetadataCollector(args_dict=args_dict_site)
Expand Down

0 comments on commit c73e42b

Please sign in to comment.