Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed Oct 2, 2024
1 parent 5da615d commit a6ffe26
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/stcal/resample/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .resample import *

__all__ = [

Check warning on line 3 in src/stcal/resample/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/__init__.py#L3

Added line #L3 was not covered by tests
"LibModelAccess",
"LibModelAccessBase",
"OutputTooLargeError",
"Resample",
"resampled_wcs_from_models",
"UnsupportedWCSError",
]
31 changes: 19 additions & 12 deletions src/stcal/resample/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
interpret_bit_flags,
)

from .utils import bytes2human, get_tmeasure
from ..alignment.util import (
from stcal.resample.utils import bytes2human, get_tmeasure
from stcal.alignment.util import (

Check warning on line 22 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L21-L22

Added lines #L21 - L22 were not covered by tests
compute_scale,
wcs_bbox_from_shape,
wcs_from_footprints,
Expand All @@ -30,10 +30,11 @@
log.setLevel(logging.DEBUG)

Check warning on line 30 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L29-L30

Added lines #L29 - L30 were not covered by tests

__all__ = [

Check warning on line 32 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L32

Added line #L32 was not covered by tests
"LibModelAccess",
"LibModelAccessBase",
"OutputTooLargeError",
"Resample",
"resampled_wcs_from_models",
"UnsupportedWCSError",
]

_SUPPORTED_CUSTOM_WCS_PARS = [

Check warning on line 40 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L40

Added line #L40 was not covered by tests
Expand Down Expand Up @@ -62,7 +63,7 @@ def _resample_range(data_shape, bbox=None):
return xmin, xmax, ymin, ymax

Check warning on line 63 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L63

Added line #L63 was not covered by tests


class LibModelAccess(abc.ABC):
class LibModelAccessBase(abc.ABC):

Check warning on line 66 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L66

Added line #L66 was not covered by tests
# list of model attributes needed by this module. While this is not
# required, it is helpful for subclasses to check they know how to
# access these attributes.
Expand Down Expand Up @@ -131,8 +132,8 @@ def resampled_wcs_from_models(
Parameters
----------
input_models : LibModelAccess
An object of `LibModelAccess`-derived type.
input_models : LibModelAccessBase
An object of `LibModelAccessBase`-derived type.
pixel_scale_ratio : float, optional
Desired pixel scale ratio defined as the ratio of the first model's
Expand Down Expand Up @@ -241,6 +242,12 @@ class OutputTooLargeError(RuntimeError):
"""Raised when the output is too large for in-memory instantiation"""


class UnsupportedWCSError(RuntimeError):

Check warning on line 245 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L245

Added line #L245 was not covered by tests
""" Raised when provided output WCS has an unexpected number of axes
or has an unsupported structure.
"""


class Resample:

Check warning on line 251 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L251

Added line #L251 was not covered by tests
"""
This is the controlling routine for the resampling process.
Expand Down Expand Up @@ -282,9 +289,9 @@ def __init__(self, input_models, pixfrac=1.0, kernel="square",
"""
Parameters
----------
input_models : LibModelAccess
A `LibModelAccess` object allowing iterating over all contained
models of interest.
input_models : LibModelAccessBase
A `LibModelAccessBase`-based object allowing iterating over
all contained models of interest.
kwargs : dict
Other parameters.
Expand Down Expand Up @@ -414,8 +421,8 @@ def check_output_wcs(self, output_wcs, wcs_pars,
"""
naxes = output_wcs.output_frame.naxes
if naxes != 2:
raise RuntimeError(
"Output WCS needs 2 spatial axes but the "
raise UnsupportedWCSError(

Check warning on line 424 in src/stcal/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/resample/resample.py#L422-L424

Added lines #L422 - L424 were not covered by tests
"Output WCS needs 2 coordinate axes but the "
f"supplied WCS has {naxes} axes."
)

Expand Down Expand Up @@ -841,7 +848,7 @@ def init_resample_variance(self):
'fillval': np.nan,
'out_shape': self._output_array_shape,
# 'exptime': 1.0,
'no_ctx': True,
'disable_ctx': True,
}
self.driz_rnoise = Drizzle(**driz_init_kwargs)
self.driz_poisson = Drizzle(**driz_init_kwargs)
Expand Down
60 changes: 48 additions & 12 deletions src/stcal/resample/utils.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
import os
from pathlib import Path, PurePath
from copy import deepcopy
import asdf

import numpy as np
from astropy.nddata.bitmask import interpret_bit_flags

__all__ = [
"build_mask", "build_output_model_name", "get_tmeasure", "bytes2human"
"build_mask", "get_tmeasure", "bytes2human", "load_custom_wcs"
]


def build_output_model_name(input_filename_list):
fnames = {f for f in input_filename_list if f is not None}
def load_custom_wcs(asdf_wcs_file, output_shape=None):
""" Load a custom output WCS from an ASDF file.
if not fnames:
return "resampled_data_{resample_suffix}{resample_file_ext}"
Parameters
----------
asdf_wcs_file : str
Path to an ASDF file containing a GWCS structure.
output_shape : tuple of int, optional
Array shape (in ``[x, y]`` order) for the output data. If not provided,
the custom WCS must specify one of: pixel_shape,
array_shape, or bounding_box.
Returns
-------
wcs : WCS
The output WCS to resample into.
# TODO: maybe remove ending suffix for single file names?
prefix = os.path.commonprefix(
[PurePath(f).stem.strip('_- ') for f in fnames]
)
"""
if not asdf_wcs_file:
return None

with asdf.open(asdf_wcs_file) as af:
wcs = deepcopy(af.tree["wcs"])
wcs.pixel_area = af.tree.get("pixel_area", None)
wcs.pixel_shape = af.tree.get("pixel_shape", None)
wcs.array_shape = af.tree.get("array_shape", None)

if output_shape is not None:
wcs.array_shape = output_shape[::-1]
wcs.pixel_shape = output_shape
elif wcs.pixel_shape is not None:
wcs.array_shape = wcs.pixel_shape[::-1]
elif wcs.array_shape is not None:
wcs.pixel_shape = wcs.array_shape[::-1]
elif wcs.bounding_box is not None:
wcs.array_shape = tuple(
int(axs[1] + 0.5)
for axs in wcs.bounding_box.bounding_box(order="C")
)
else:
raise ValueError(
"Step argument 'output_shape' is required when custom WCS "
"does not have neither of 'array_shape', 'pixel_shape', or "
"'bounding_box' attributes set."
)

return prefix + "{resample_suffix}{resample_file_ext}"
return wcs


def build_mask(dqarr, bitvalue, flag_name_map=None):
Expand Down

0 comments on commit a6ffe26

Please sign in to comment.