Skip to content

Commit

Permalink
fix script import protection (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson authored Nov 7, 2023
1 parent 3fbf571 commit a3d160f
Showing 1 changed file with 99 additions and 89 deletions.
188 changes: 99 additions & 89 deletions romancal/scripts/static_preview.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,109 @@
from pathlib import Path
from typing import Optional
from typing import TYPE_CHECKING, Optional

import asdf
import numpy
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')
wcs = file["roman"]["meta"]["wcs"]

data = downsample_asdf_to(input=input, shape=shape, func=numpy.nanmean)

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, func=numpy.nanmean)

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

if TYPE_CHECKING:
from typing_extensions import Annotated


def command():
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')
wcs = file["roman"]["meta"]["wcs"]

data = downsample_asdf_to(input=input, shape=shape, func=numpy.nanmean)

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, func=numpy.nanmean)

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

app()


Expand Down

0 comments on commit a3d160f

Please sign in to comment.