<commit_before><commit_msg>add master.py: add master frame processing utils<commit_after># -*- coding: utf-8 -*-
"""

Author
------
<NAME>

Email
-----
<EMAIL>

Created on
----------
- Fri Feb 24 21:00:00 2017

Modifications
-------------
-

Aims
----
- utils to process master images

"""

import numpy as np

from twodspec import ccdproc_mod as ccdproc


def combine(fps, s, gain_corr=True, method='average'):
    """ estimate bias & read noise """

    # read bias images
    bias_list = [read_image(fp, s.cfg.read, gain_corr,
                            s.cfg.kwds['kw_pregain'], s.cfg.gain, s.cfg.rot90)
                 for fp in fps]
    bias_array = np.array([im.data.astype(float) for im in bias_list])

    # estimate bias
    if method in {'average', 'mean'}:
        bias = np.mean(bias_array, axis=0)
    elif method is 'median':
        bias = np.median(bias_array, axis=0)
    else:
        raise(ValueError("@SONG: method is not valid!"))

    # estimate readout noise
    readout = np.std(bias_array, axis=0)

    return bias, readout


def read_image(fp, kwargs_read, gain_corr, kw_gain, kwargs_gain, rot90):
    im = ccdproc.CCDData.read(fp, **kwargs_read)
    if gain_corr:
        gain_value = im.header[kw_gain]
        im = ccdproc.gain_correct(im, gain=gain_value, **kwargs_gain)
    if rot90 is not None:
        im.rot90(rot90)
    return im
{'fraction_non_alphanumeric': 0.1433796634967081, 'fraction_numerical': 0.019019751280175568, 'mean_word_length': 2.553246753246753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '602161', 'n_tokens_mistral': 495, 'n_tokens_neox': 466, 'n_words': 124}