Skip to content

Commit

Permalink
Merge pull request #69 from lsst/tickets/DM-39731
Browse files Browse the repository at this point in the history
DM-39731: Use DATE-BEG
  • Loading branch information
timj committed Jun 21, 2023
2 parents d0d831f + 4c2d60a commit 71d10c1
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 256 deletions.
5 changes: 3 additions & 2 deletions python/astro_metadata_translator/bin/translateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import logging
import sys
import traceback
from typing import IO, List, Sequence, Tuple
from collections.abc import Sequence
from typing import IO

import yaml

Expand Down Expand Up @@ -275,7 +276,7 @@ def process_files(
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
output_mode: str = "auto",
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
"""Read and translate metadata from the specified files.
Parameters
Expand Down
9 changes: 5 additions & 4 deletions python/astro_metadata_translator/bin/writeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import logging
import os
import sys
from typing import IO, List, MutableMapping, Optional, Sequence, Tuple
from collections.abc import MutableMapping, Sequence
from typing import IO

from ..file_helpers import find_files
from ..indexing import index_files
Expand All @@ -31,10 +32,10 @@ def write_index_files(
hdrnum: int,
print_trace: bool,
content_mode: str = "translated",
outpath: Optional[str] = None,
outpath: str | None = None,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
"""Process each file and create JSON index file.
The index file will have common information in the toplevel.
Expand Down Expand Up @@ -90,7 +91,7 @@ def write_index_files(

failed = []
okay = []
files_per_directory: MutableMapping[str, List[str]] = {}
files_per_directory: MutableMapping[str, list[str]] = {}

# Group each file by directory if no explicit output path
if outpath is None:
Expand Down
7 changes: 4 additions & 3 deletions python/astro_metadata_translator/bin/writesidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import os
import sys
import traceback
from typing import IO, List, Sequence, Tuple
from collections.abc import Sequence
from typing import IO

from ..file_helpers import find_files, read_file_info


def _split_ext(file: str) -> Tuple[str, str]:
def _split_ext(file: str) -> tuple[str, str]:
"""Split the extension from the file name and return it and the root.
Special case handling of .gz and other compression extensions.
Expand Down Expand Up @@ -120,7 +121,7 @@ def write_sidecar_files(
print_trace: bool,
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
) -> Tuple[List[str], List[str]]:
) -> tuple[list[str], list[str]]:
"""Process each file and create sidecar file.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion python/astro_metadata_translator/cli/astrometadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import importlib
import logging
import os
from typing import Sequence
from collections.abc import Sequence

import click

Expand Down
19 changes: 8 additions & 11 deletions python/astro_metadata_translator/file_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import re
import sys
import traceback
from typing import IO, Any, Iterable, List, MutableMapping, Optional, Union
from collections.abc import Iterable, MutableMapping
from typing import IO, Any

from .headers import merge_headers
from .observationInfo import ObservationInfo
Expand All @@ -31,9 +32,7 @@
import lsst.daf.base # noqa: F401 need PropertyBase for readMetadata
from lsst.afw.fits import FitsError, readMetadata

def _read_fits_metadata(
file: str, hdu: int, can_raise: bool = False
) -> Optional[MutableMapping[str, Any]]:
def _read_fits_metadata(file: str, hdu: int, can_raise: bool = False) -> MutableMapping[str, Any] | None:
"""Read a FITS header using afw.
Parameters
Expand Down Expand Up @@ -70,9 +69,7 @@ def _read_fits_metadata(
except ImportError:
from astropy.io import fits

def _read_fits_metadata(
file: str, hdu: int, can_raise: bool = False
) -> Optional[MutableMapping[str, Any]]:
def _read_fits_metadata(file: str, hdu: int, can_raise: bool = False) -> MutableMapping[str, Any] | None:
"""Read a FITS header using astropy."""

# For detailed docstrings see the afw implementation above
Expand All @@ -90,7 +87,7 @@ def _read_fits_metadata(
return header


def find_files(files: Iterable[str], regex: str) -> List[str]:
def find_files(files: Iterable[str], regex: str) -> list[str]:
"""Find files for processing.
Parameters
Expand Down Expand Up @@ -125,7 +122,7 @@ def find_files(files: Iterable[str], regex: str) -> List[str]:

def read_basic_metadata_from_file(
file: str, hdrnum: int, errstream: IO = sys.stderr, can_raise: bool = True
) -> Optional[MutableMapping[str, Any]]:
) -> MutableMapping[str, Any] | None:
"""Read a raw header from a file, merging if necessary
Parameters
Expand Down Expand Up @@ -190,12 +187,12 @@ def read_basic_metadata_from_file(
def read_file_info(
file: str,
hdrnum: int,
print_trace: Optional[bool] = None,
print_trace: bool | None = None,
content_mode: str = "translated",
content_type: str = "simple",
outstream: IO = sys.stdout,
errstream: IO = sys.stderr,
) -> Optional[Union[str, MutableMapping[str, Any], ObservationInfo]]:
) -> str | MutableMapping[str, Any] | ObservationInfo | None:
"""Read information from file
Parameters
Expand Down
30 changes: 14 additions & 16 deletions python/astro_metadata_translator/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import os
import posixpath
from collections import Counter
from collections.abc import Mapping
from typing import IO, Any, List, MutableMapping, Optional, Sequence, Tuple, Type, Union
from collections.abc import Mapping, MutableMapping, Sequence
from typing import IO, Any

import pkg_resources
import yaml
Expand All @@ -47,8 +47,8 @@ def merge_headers(
headers: Sequence[MutableMapping[str, Any]],
mode: str = "overwrite",
sort: bool = False,
first: Optional[Sequence[str]] = None,
last: Optional[Sequence[str]] = None,
first: Sequence[str] | None = None,
last: Sequence[str] | None = None,
) -> MutableMapping[str, Any]:
"""Merge multiple headers into a single dict.
Expand Down Expand Up @@ -245,8 +245,8 @@ def key_func(hdr: Mapping[str, Any]) -> Any:

def retain_value(
to_receive: MutableMapping[str, Any],
to_retain: Optional[Sequence[str]],
sources: Tuple[Mapping[str, Any], ...],
to_retain: Sequence[str] | None,
sources: tuple[Mapping[str, Any], ...],
) -> None:
if to_retain:
for k in to_retain:
Expand All @@ -263,7 +263,7 @@ def retain_value(
return merged


def _read_yaml(fh: IO[bytes], msg: str) -> Optional[Mapping[str, Any]]:
def _read_yaml(fh: IO[bytes], msg: str) -> Mapping[str, Any] | None:
"""Read YAML from file descriptor.
Parameters
Expand Down Expand Up @@ -294,9 +294,7 @@ def _read_yaml(fh: IO[bytes], msg: str) -> Optional[Mapping[str, Any]]:
return content


def _find_from_file(
header: MutableMapping[str, Any], paths: Sequence[str], target_file: str
) -> Optional[str]:
def _find_from_file(header: MutableMapping[str, Any], paths: Sequence[str], target_file: str) -> str | None:
"""Search file system for matching correction files.
Parameters
Expand Down Expand Up @@ -332,8 +330,8 @@ def _find_from_file(


def _find_from_resource(
header: MutableMapping[str, Any], package: Optional[str], resource_root: Optional[str], target_file: str
) -> Optional[str]:
header: MutableMapping[str, Any], package: str | None, resource_root: str | None, target_file: str
) -> str | None:
"""Search package resource for correction information.
Parameters
Expand Down Expand Up @@ -370,9 +368,9 @@ def _find_from_resource(

def fix_header(
header: MutableMapping[str, Any],
search_path: Optional[Union[str, Sequence[str]]] = None,
translator_class: Optional[Type[MetadataTranslator]] = None,
filename: Optional[str] = None,
search_path: str | Sequence[str] | None = None,
translator_class: type[MetadataTranslator] | None = None,
filename: str | None = None,
) -> bool:
"""Update, in place, the supplied header with known corrections.
Expand Down Expand Up @@ -468,7 +466,7 @@ class or else support automatic translation class determination.
log.debug("Checking for header correction file named %s", target_file)

# Work out the search path
paths: List[str] = []
paths: list[str] = []
if search_path is not None:
if isinstance(search_path, str):
# Allow a single path to be given as a string
Expand Down

0 comments on commit 71d10c1

Please sign in to comment.