Metadata-Version: 2.4
Name: snowblind
Version: 1.4.1
Summary: Algorithms for cleaning JWST data
Author-email: James Davies <jdavies@mpia.de>
License: BSD 3-Clause License
        
        Copyright (c) 2023-2026,  Max Planck Institute for Astronomy
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: Homepage, https://github.com/mpi-astronomy/snowblind
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: asdf
Requires-Dist: astropy
Requires-Dist: jwst>=2.0.0
Requires-Dist: numpy
Requires-Dist: scikit-image>=0.26.0
Requires-Dist: scipy
Requires-Dist: stcal
Requires-Dist: stdatamodels
Requires-Dist: stpipe
Provides-Extra: spizzle
Requires-Dist: spizzle>=0.2.1; extra == "spizzle"
Requires-Dist: numba>=0.58; extra == "spizzle"
Provides-Extra: docs
Requires-Dist: sphinx; extra == "docs"
Requires-Dist: numpydoc; extra == "docs"
Requires-Dist: pydata-sphinx-theme; extra == "docs"
Provides-Extra: test
Requires-Dist: snowblind[spizzle]; extra == "test"
Requires-Dist: pytest; extra == "test"
Requires-Dist: flake8; extra == "test"
Requires-Dist: flake8-pyproject; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Provides-Extra: cff
Requires-Dist: cff-from-621; extra == "cff"
Dynamic: license-file

# snowblind: algorithms for cleaning JWST data

snowblind provides steps for JWST near-infrared data that improve on, or add
to, steps of the official jwst pipeline. They run like the pipeline's own
steps, on their own or as hooks inside the jwst pipelines.

Read the [documentation](https://mpi-astronomy.github.io/snowblind/) for how
each step works and how to choose its settings.

## What it provides

### Steps
- **`SnowblindStep`** flags snowballs, the large cosmic-ray events that
  leave a saturated core inside a jump-flagged halo. It grows each halo to
  follow the shape of the event, snowballs cut by the edge of the detector
  included, flags the ring around the core to the end of the integration,
  and flags the core's persistence in the later integrations of the ramp,
  for times measured for each detector and packaged with snowblind. It
  replaces the jump step's `expand_large_events`, which draws circles or
  ellipses, and works on rate products as well as ramps, so it can be
  applied after the fact.
- **`PersistenceFlagStep`** flags the pixels that saturated in an earlier
  exposure of the same detector, and the pixels next to them, for a time
  measured for each detector and packaged with snowblind. The jwst `persistence` step (as of jwst 3.0)
  needs a time chosen by hand and the exposures run one after another; this
  step takes a whole set of finished `_rate` or `_cal` files at once.
- **`OpenPixelStep`** finds open and hot pixels that have appeared since the
  reference bad-pixel mask was made, by comparing the exposures of each
  detector. Experimental.
- **`StreamCleanStep`** removes 1/f noise (striping) from the ramp along
  each output channel's readout stream. Unlike the jwst
  `clean_flicker_noise` step, it applies the same linear filter to every
  group difference, so it adds no noise along the ramp and does better the
  more groups there are.
- **`ResetRampFitStep`** is the jwst likelihood ramp fit with the reset
  level as an extra point at the start, so pixels with only one unsaturated
  group, such as the cores of bright stars, still get a rate.
- **~`JumpPlusStep`~** <mark>is deprecated</mark> and will be removed. It flags, in
  readouts that average several frames per group, the group after each jump
  and the group before saturation; the jwst saturation step (since jwst
  1.16) and jump step flag the groups that need it and the ramp fits leave
  them out, so the step mostly removes good data.
- **`SparseResampleStep`** and **`SparseOutlierDetectionStep`** are the jwst
  resample and outlier detection steps for images with drizzle replaced by
  [spizzle](https://github.com/mpi-astronomy/spizzle). The mosaic's
  variance planes are the exact variance of each pixel, and a noise factor
  image gives what the correlation between neighboring pixels adds to the
  noise of a sum over them; both steps are faster than the jwst steps.
- **`TiledOutlierStep`** finds outliers against one reference built from
  the input pixels of all the exposures at once, on a grid finer than the
  pixels, so it resolves undersampled star cores: it finds more cosmic rays
  near stars and flags far fewer star cores than the jwst step, with the
  same settings for every filter, and never allocates a mosaic-sized array.

### Pipelines
- **`snowblind_detector1`** is `calwebb_detector1` with StreamCleanStep,
  SnowblindStep and ResetRampFitStep in place, the SIRS reference-pixel
  correction turned on, and the pixels next to saturated ones flagged
  CHARGELOSS rather than SATURATED, so that SATURATED marks only the
  pixels that saturated. It works as a drop-in replacement.
- **`snowblind_image3`** is `calwebb_image3` with `TiledOutlierStep` and
  `SparseResampleStep` in place (`--outlier_step=sparse_outlier` uses
  `SparseOutlierDetectionStep` for the outliers).  Also a drop-in
  replacement.

## Installation

    pip install snowblind

snowblind needs jwst 2.0.0 or later; earlier releases of snowblind work with
earlier jwst releases. The resampling and outlier detection steps need
spizzle as well:

    pip install 'snowblind[spizzle]'

## Getting started

Run Detector1 with the 1/f cleaning, the reset-anchored fit and the snowball
flagging (and the SIRS reference-pixel correction, which the 1/f cleaning is
matched to):

    strun snowblind_detector1 jw..._uncal.fits

Or add only the snowball flagging to the jwst pipeline, as a hook after its
jump step:

```python
from jwst.pipeline import Detector1Pipeline
from snowblind import SnowblindStep

steps = {"jump": {"expand_large_events": False, "post_hooks": [SnowblindStep]}}
Detector1Pipeline.call("jw..._uncal.fits", steps=steps, save_results=True)
```

Flag persistence in a set of `_cal` files, after `calwebb_image2`:

```python
from glob import glob
from snowblind import PersistenceFlagStep

PersistenceFlagStep.call(sorted(glob("jw*_cal.fits")), save_results=True, suffix="cal", output_dir="persist")
```

This writes each file again in `persist/`, with `DO_NOT_USE` and
`PERSISTENCE` set where an earlier exposure of the same detector saturated
recently.

Run Image3 with the snowblind outlier detection and resampling, which saves
the noise factor as `<product>_nfactor.fits` next to the `_i2d.fits` mosaic:

    strun snowblind_image3 jw..._image3_asn.json

## Results

`StreamCleanStep` on a dark exposure, against no cleaning and the jwst
`clean_flicker_noise` step:

![Rate images of an NRCA1 dark with four kinds of 1/f cleaning](https://raw.githubusercontent.com/mpi-astronomy/snowblind/main/docs/figures/dark_rate_images.png)

The persistence of saturated stars against the time since their exposure
ended, which sets the time `PersistenceFlagStep` flags them for:

<img src="https://raw.githubusercontent.com/mpi-astronomy/snowblind/main/docs/figures/persistence_decay.png" width="75%">

## Citing snowblind

Please cite `snowblind` through its Zenodo record DOI,
[10.5281/zenodo.15791614](https://doi.org/10.5281/zenodo.15791614), that
always points to the latest release.

    @software{snowblind,
      author    = {Davies, James E.},
      title     = {snowblind: algorithms for cleaning JWST data},
      publisher = {Zenodo},
      doi       = {10.5281/zenodo.15791614},
      url       = {https://github.com/mpi-astronomy/snowblind}
    }

The same information is in `CITATION.cff`.

Please open an issue if you have any problems!
