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

Black-formatted all files #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions sdi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .ref import ref
from .secid import secid
from .collate import collate
from .snr_function import snr #New
from .snr_function import snr # New
from . import test

# in order to test click commands
Expand All @@ -20,7 +20,7 @@
from .extract import extract_cmd as _extract_cmd
from .subtract import subtract_cmd as _subtract_cmd
from .collate import collate_cmd as _collate_cmd
from .snr_function import snr_cmd as _snr_cmd #New
from .snr_function import snr_cmd as _snr_cmd # New
from . import test_cmd

_scidir = os.path.join(os.path.dirname(__file__), "test/fixtures/science")
Expand Down
8 changes: 8 additions & 0 deletions sdi/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from functools import update_wrapper
import click


@click.group(chain=True)
def cli():
"""
Chains together pipeline commands.
"""


@cli.resultcallback()
def run_pipeline(operators):
"""
Expand All @@ -26,29 +28,35 @@ def run_pipeline(operators):
# do necessary things on overall outputs
pass


def operator(func):
"""
Decorator which wraps commands so that they return functions after being
run by click.
All of the returned functions are passed as an iterable
into run_pipeline.
"""

def new_func(*args, **kwargs):
def operator(hduls):
# args and kwargs are subcommand-specific
return func(hduls, *args, **kwargs)

return operator

# basically return new_func, but better
return update_wrapper(new_func, func)


def generator(func):
"""
Does what operator does, but for the first thing in the series, like
opening the hduls.
Works with sub-funcs that do not have 'hduls' as the first argument.
"""

def new_func(hduls, *args, **kwargs):
yield from hduls
yield from func(*args, **kwargs)

return operator(update_wrapper(new_func, func))
54 changes: 33 additions & 21 deletions sdi/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,84 @@
import click
import numpy as np
from astropy.io.fits import CompImageHDU
#from sources import Source

# from sources import Source
import astroalign
from .snr_function import snr
#from _scripts import snr
from .snr_function import snr

# from _scripts import snr
from . import _cli as cli


def align(hduls, name="SCI", ref=None):
"""
Aligns the source astronomical image(s) to the reference astronomical image
\b
:param hduls: list of fitsfiles
:param name: header containing image data.
:param reference: index of desired
:param reference: index of desired
:return: list of FITS files with <name> HDU aligned
"""

hduls_list = [hdul for hdul in hduls]
outputs = []

hduls_list = snr(hduls_list,name) #SNR function appends snr to each hdul's "CAT" table.
hduls_list = list(hduls_list) #convert generator to list.
hduls_list = snr(
hduls_list, name
) # SNR function appends snr to each hdul's "CAT" table.
hduls_list = list(hduls_list) # convert generator to list.

if ref is None: #No reference index given. we establish reference based on best signal to noise ratio
if (
ref is None
): # No reference index given. we establish reference based on best signal to noise ratio

reference = hduls_list[0][name] #0th index reference is used by default
reference = hduls_list[0][name] # 0th index reference is used by default
ref_SNR = hduls_list[0]["CAT"].header["SNR"]

for hdul in hduls_list: #loops though hdul lists finding the hdul with greatest snr
if hdul["CAT"].header["SNR"] > ref_SNR: #compares SNR value of current hdul to ref
for (
hdul
) in hduls_list: # loops though hdul lists finding the hdul with greatest snr
if (
hdul["CAT"].header["SNR"] > ref_SNR
): # compares SNR value of current hdul to ref
ref_SNR = hdul["CAT"].header["SNR"]
reference = hdul[name]

else: #ref index is provided
else: # ref index is provided
reference = hduls_list[ref][name]

try:
ref_data = reference.data
except AttributeError:
print("The reference file have doesn't have Attribute: Data")
print("The reference file have doesn't have Attribute: Data")
pass

for i,hdul in enumerate(hduls_list):
for i, hdul in enumerate(hduls_list):
np_src = hdul[name]

# possibly unneccessary but unsure about scoping
output = np.array([])

try:
output = astroalign.register(np_src,ref_data)[0]
output = astroalign.register(np_src, ref_data)[0]
except ValueError:
np_src = hdul[name].data.byteswap().newbyteorder()
output = astroalign.register(np_src, ref_data)[0]
pass

if hasattr(hdul[name], "data"):
if hasattr(hdul[name], "data"):
idx = hdul.index_of(name)
hdul[idx].data = output
hdul[idx].header['EXTNAME'] = ("ALGN ")
hdul[idx].header = reference.header #hdul['sci'].header = reference.header
hdul[idx].header["EXTNAME"] = "ALGN "
hdul[idx].header = reference.header # hdul['sci'].header = reference.header

return (hdul for hdul in hduls_list)


@cli.cli.command("align")
@click.option("-n", "--name", default="SCI", help="The HDU to be aligned.")
@cli.operator
#TODO use CAT sources if they exist
# TODO use CAT sources if they exist

## align function wrapper
def align_cmd(hduls, name="SCI", ref=None):
Expand Down
Loading