Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCAL-629: static preview generation script #953

Merged
merged 20 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ refpix

resample
--------

- Implement resampling step. [#787]

- Use resampled exposure time images to compute image exposure times. [#959]

scripts
-------

- added ``roman_static_preview`` script to generate static previews of ASDF images [#953]

stpipe
------

Expand All @@ -71,7 +77,6 @@ tweakreg
- Fix a bug due to which source catalog may contain sources
outside of the bounding box. [#947]


0.12.0 (2023-08-18)
===================

Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ dev = [
'tox > 4',
'pre-commit > 3'
]
sdp = [
'stpreview>=0.5.0',
zacharyburnett marked this conversation as resolved.
Show resolved Hide resolved
]

[project.urls]
'tracker' = 'https://github.com/spacetelescope/romancal/issues'
Expand All @@ -73,6 +76,7 @@ dev = [
pytest11 = {webbpsf = 'pytest_plugin.webbpsf_plugin'}

[project.scripts]
roman_static_preview = 'romancal.scripts.static_preview:command'
okify_regtests = 'romancal.scripts.okify_regtests:main'
schema_editor = 'romancal.scripts.schema_editor:main'
schemadoc = 'romancal.scripts.schemadoc:main'
Expand Down
100 changes: 100 additions & 0 deletions romancal/scripts/static_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from pathlib import Path
from typing import Optional

import asdf
from typing_extensions import Annotated

try:
import typer
from stpreview.downsample import downsample_asdf_to
from stpreview.image import north_pole_angle, percentile_normalization, write_image
except (ImportError, ModuleNotFoundError):
raise ImportError(
'SDP requirements not installed; do `pip install "romancal[sdp]"`'
)

app = typer.Typer()


@app.command()
def preview(
input: Annotated[Path, typer.Argument(help="path to ASDF file with 2D image data")],
output: Annotated[
Optional[Path], typer.Argument(help="path to output image file")
] = None,
shape: Annotated[
Optional[tuple[int, int]],
typer.Argument(help="desired pixel resolution of output image"),
] = (1080, 1080),
compass: Annotated[
Optional[bool], typer.Option(help="whether to draw a north arrow on the image")
] = True,
):
"""
create a preview image with a north arrow overlay indicating orientation
"""

if output is None:
output = Path.cwd()
if output.is_dir():
output = output / f"{input.stem}.png"

with asdf.open(input) as file:
model = file["roman"]["meta"]["model_type"]
if "image" not in model.lower():
raise NotImplementedError(f'"{model}" model not supported')
zacharyburnett marked this conversation as resolved.
Show resolved Hide resolved
wcs = file["roman"]["meta"]["wcs"]

data = downsample_asdf_to(input=input, shape=shape)

write_image(
data,
output,
shape=shape,
normalization=percentile_normalization(data, percentile=90),
colormap="afmhot",
north_arrow_angle=north_pole_angle(wcs).degree - 90,
)


@app.command()
def thumbnail(
input: Annotated[Path, typer.Argument(help="path to ASDF file with 2D image data")],
output: Annotated[
Optional[Path], typer.Argument(help="path to output image file")
] = None,
shape: Annotated[
Optional[tuple[int, int]],
typer.Argument(help="desired pixel resolution of output image"),
] = (300, 300),
compass: Annotated[
Optional[bool], typer.Option(help="whether to draw a north arrow on the image")
] = False,
):
if output is None:
output = Path.cwd()
if output.is_dir():
output = output / f"{input.stem}_thumb.png"

with asdf.open(input) as file:
model = file["roman"]["meta"]["model_type"]
if "image" not in model.lower():
raise NotImplementedError(f'"{model}" model not supported')

data = downsample_asdf_to(input=input, shape=shape)

write_image(
data,
output,
shape=shape,
normalization=percentile_normalization(data, percentile=90),
colormap="afmhot",
)


def command():
app()


if __name__ == "__main__":
command()
2 changes: 1 addition & 1 deletion romancal/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def dependencies(package, exclude: [str]):
]


MODULES = dependencies(romancal, exclude=["test", "time"])
MODULES = dependencies(romancal, exclude=["test", "time", "static_preview"])


@pytest.mark.parametrize(
Expand Down