Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…dels into RCAL-769_MosaicName
  • Loading branch information
PaulHuwe committed Oct 30, 2024
2 parents 5c9085d + 99446a3 commit aad56bf
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 215 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
args: ["--py38-plus"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.6.9'
rev: 'v0.7.0'
hooks:
- id: ruff
args: ["--fix"]
Expand Down
1 change: 1 addition & 0 deletions changes/404.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update datamodels and tests for L1/L2 Roman Doc
3 changes: 3 additions & 0 deletions changes/409.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Have datamodels update their ``meta.filename`` attribute match the filename of the
file they were loaded from. This means users can rename files on disk, but when they
open them in datamodels, the filename will be updated to match the new filename.
1 change: 1 addition & 0 deletions src/roman_datamodels/datamodels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from ._datamodels import * # noqa: F403

# rename rdm_open to open to match the current roman_datamodels API
from ._utils import FilenameMismatchWarning # noqa: F403, F401
from ._utils import rdm_open as open # noqa: F403, F401
7 changes: 6 additions & 1 deletion src/roman_datamodels/datamodels/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,13 @@ def save(self, path, dir_path=None, *args, **kwargs):
return output_path

def open_asdf(self, init=None, **kwargs):
from ._utils import _open_path_like

with validate.nuke_validation():
return asdf.open(init, **kwargs) if isinstance(init, str) else asdf.AsdfFile(init, **kwargs)
if isinstance(init, str):
return _open_path_like(init, **kwargs)

return asdf.AsdfFile(init, **kwargs)

def to_asdf(self, init, *args, **kwargs):
with validate.nuke_validation(), _temporary_update_filename(self, Path(init).name):
Expand Down
25 changes: 24 additions & 1 deletion src/roman_datamodels/datamodels/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
the open/factory function for creating datamodels
"""

import warnings
from collections.abc import Mapping
from pathlib import Path

import asdf
Expand All @@ -11,7 +13,14 @@

from ._core import MODEL_REGISTRY, DataModel

__all__ = ["rdm_open"]
__all__ = ["rdm_open", "FilenameMismatchWarning"]


class FilenameMismatchWarning(UserWarning):
"""
Warning when the filename in the meta attribute does not match the filename
of the file being opened.
"""


def _open_path_like(init, lazy_tree=True, **kwargs):
Expand All @@ -34,12 +43,26 @@ def _open_path_like(init, lazy_tree=True, **kwargs):
# asdf defaults to lazy_tree=False, this overwrites it to
# lazy_tree=True for roman_datamodels
kwargs["lazy_tree"] = lazy_tree
init = Path(init)

try:
asdf_file = asdf.open(init, **kwargs)
except ValueError as err:
raise TypeError("Open requires a filepath, file-like object, or Roman datamodel") from err

if (
"roman" in asdf_file
and isinstance(asdf_file["roman"], Mapping) # Fix issue for Python 3.10
and "meta" in asdf_file["roman"]
and "filename" in asdf_file["roman"]["meta"]
and asdf_file["roman"]["meta"]["filename"] != init.name
):
warnings.warn(
f"meta.filename: {asdf_file['roman']['meta']['filename']} does not match filename: {init.name}, updating the filename in memory!",
FilenameMismatchWarning,
)
asdf_file["roman"]["meta"]["filename"] = init.name

return asdf_file


Expand Down
7 changes: 7 additions & 0 deletions src/roman_datamodels/maker_utils/_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import asdf

NONUM = -999999
Expand All @@ -20,6 +22,11 @@ def save_node(node, filepath=None):
"""

if filepath:
# Force sync filename with file path if we are saving to a file
if "meta" in node and "filename" in node["meta"]:
# Need the __class__ to avoid issues for tvac and fps
node["meta"]["filename"] = node["meta"]["filename"].__class__(Path(filepath).name)

af = asdf.AsdfFile()
af.tree = {"roman": node}
af.write_to(filepath)
Expand Down
38 changes: 31 additions & 7 deletions src/roman_datamodels/maker_utils/_basic_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
from ._base import NOFN, NOSTR


def mk_calibration_software_name(**kwargs):
"""
Create a dummy CalibrationSoftwareName object with valid values
Returns
-------
roman_datamodels.stnode.CalibrationSoftwareName
"""
return stnode.CalibrationSoftwareName(kwargs.get("calibration_software_name", "RomanCAL"))


def mk_calibration_software_version(**kwargs):
"""
Create a dummy CalibrationSoftwareVersion object with valid values
Expand Down Expand Up @@ -71,18 +82,29 @@ def mk_origin(**kwargs):
roman_datamodels.stnode.Origin
"""

return stnode.Origin(kwargs.get("origin", "STSCI"))
return stnode.Origin(kwargs.get("origin", "STSCI/SOC"))


def mk_prd_version(**kwargs):
"""
Create a dummy PrdVersion object with valid values
Returns
-------
roman_datamodels.stnode.PrdVersion
"""
return stnode.PrdVersion(kwargs.get("prd_version", "8.8.8"))


def mk_prd_software_version(**kwargs):
def mk_product_type(**kwargs):
"""
Create a dummy PrdSoftwareVersion object with valid values
Create a dummy ProductType object with valid values
Returns
-------
roman_datamodels.stnode.PrdSoftwareVersion
roman_datamodels.stnode.ProductType
"""
return stnode.PrdSoftwareVersion(kwargs.get("prd_software_version", "8.8.8"))
return stnode.ProductType(kwargs.get("product_type", "l2"))


def mk_telescope(**kwargs):
Expand All @@ -105,13 +127,15 @@ def mk_basic_meta(**kwargs):
dict (defined by the basic-1.0.0 schema)
"""
meta = {}
meta["calibration_software_name"] = mk_calibration_software_name(**kwargs)
meta["calibration_software_version"] = mk_calibration_software_version(**kwargs)
meta["sdf_software_version"] = mk_sdf_software_version(**kwargs)
meta["product_type"] = mk_product_type(**kwargs)
meta["filename"] = mk_filename(**kwargs)
meta["file_date"] = mk_file_date(**kwargs)
meta["model_type"] = mk_model_type(**kwargs)
meta["origin"] = mk_origin(**kwargs)
meta["prd_software_version"] = mk_prd_software_version(**kwargs)
meta["prd_version"] = mk_prd_version(**kwargs)
meta["sdf_software_version"] = mk_sdf_software_version(**kwargs)
meta["telescope"] = mk_telescope(**kwargs)

return meta
Loading

0 comments on commit aad56bf

Please sign in to comment.