This file is indexed.

/usr/lib/python3/dist-packages/ccdproc/ccddata.py is in python3-ccdproc 1.3.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""This module implements the base CCDData class."""

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import copy
import numbers
import weakref
from collections import OrderedDict

import numpy as np

from astropy.nddata import (NDDataArray, StdDevUncertainty, NDUncertainty,
                            MissingDataAssociationException)
from astropy.io import fits, registry
from astropy import units as u
from astropy import log
from astropy.utils import minversion
from astropy.wcs import WCS

_ASTROPY_LT_1_2 = not minversion("astropy", "1.2")
_ASTROPY_LT_1_3 = not minversion("astropy", "1.3")
_ASTROPY_GT_2_0 = minversion("astropy", "2.0")

# FIXME: Remove the content of the following "if" as soon as astropy 1.1 isn't
# supported anymore. This is just a temporary workaround to fix the memory leak
# described in https://github.com/astropy/astropy/issues/4825
if _ASTROPY_LT_1_2:

    class ParentNDDataDescriptor(object):
        def __get__(self, obj, objtype=None):
            message = "uncertainty is not associated with an NDData object."
            try:
                if obj._parent_nddata is None:
                    raise MissingDataAssociationException(message)
                else:
                    # The NDData is saved as weak reference so we must call it
                    # to get the object the reference points to.
                    if isinstance(obj._parent_nddata, weakref.ref):
                        return obj._parent_nddata()
                    log.info("parent_nddata should be a weakref to an "
                             "NDData object.")
                    return obj._parent_nddata
            except AttributeError:
                raise MissingDataAssociationException(message)

        def __set__(self, obj, value):
            if value is not None and not isinstance(value, weakref.ref):
                # Save a weak reference on the uncertainty that points to this
                # instance of NDData. Direct references should NOT be used:
                # https://github.com/astropy/astropy/pull/4799#discussion_r61236832
                value = weakref.ref(value)
            obj._parent_nddata = value

    # Use the descriptor as parent_nddata property. This only affects
    # instances created after importing this module.
    StdDevUncertainty.parent_nddata = ParentNDDataDescriptor()


__all__ = ['CCDData', 'fits_ccddata_reader', 'fits_ccddata_writer']


# Global value which can turn on/off the unit requirements when creating a
# CCDData. Should be used with care because several functions actually break
# if the unit is None!
_config_ccd_requires_unit = True

if not _ASTROPY_LT_1_2:
    from astropy.utils.decorators import sharedmethod

    def _arithmetic(op):
        """Decorator factory which temporarly disables the need for a unit when
        creating a new CCDData instance. The final result must have a unit.

        Parameters
        ----------
        op : function
            The function to apply. Supported are:

            - ``np.add``
            - ``np.subtract``
            - ``np.multiply``
            - ``np.true_divide``

        Notes
        -----
        Should only be used on CCDData ``add``, ``subtract``, ``divide`` or
        ``multiply`` because only these methods from NDArithmeticMixin are
        overwritten.
        """
        def decorator(func):
            def inner(self, operand, operand2=None, **kwargs):
                global _config_ccd_requires_unit
                _config_ccd_requires_unit = False
                result = self._prepare_then_do_arithmetic(op, operand,
                                                          operand2, **kwargs)
                # Wrap it again as CCDData so it checks the final unit.
                _config_ccd_requires_unit = True
                return result.__class__(result)
            inner.__doc__ = ("See `astropy.nddata.NDArithmeticMixin.{}`."
                             "".format(func.__name__))
            return sharedmethod(inner)
        return decorator


class CCDData(NDDataArray):
    """A class describing basic CCD data.

    The CCDData class is based on the NDData object and includes a data array,
    uncertainty frame, mask frame, meta data, units, and WCS information for a
    single CCD image.

    Parameters
    -----------
    data : `~ccdproc.CCDData`-like or `numpy.ndarray`-like
        The actual data contained in this `~ccdproc.CCDData` object.
        Note that the data will always be saved by *reference*, so you should
        make a copy of the ``data`` before passing it in if that's the desired
        behavior.

    uncertainty : `~astropy.nddata.StdDevUncertainty`, `numpy.ndarray` or \
            None, optional
        Uncertainties on the data.
        Default is ``None``.

    mask : `numpy.ndarray` or None, optional
        Mask for the data, given as a boolean Numpy array with a shape
        matching that of the data. The values must be `False` where
        the data is *valid* and `True` when it is not (like Numpy
        masked arrays). If ``data`` is a numpy masked array, providing
        ``mask`` here will causes the mask from the masked array to be
        ignored.
        Default is ``None``.

    flags : `numpy.ndarray` or `~astropy.nddata.FlagCollection` or None, \
            optional
        Flags giving information about each pixel. These can be specified
        either as a Numpy array of any type with a shape matching that of the
        data, or as a `~astropy.nddata.FlagCollection` instance which has a
        shape matching that of the data.
        Default is ``None``.

    wcs : `~astropy.wcs.WCS` or None, optional
        WCS-object containing the world coordinate system for the data.
        Default is ``None``.

    meta : dict-like object or None, optional
        Metadata for this object. "Metadata" here means all information that
        is included with this object but not part of any other attribute
        of this particular object, e.g. creation date, unique identifier,
        simulation parameters, exposure time, telescope name, etc.

    unit : `~astropy.units.Unit` or str, optional
        The units of the data.
        Default is ``None``.

        .. warning::

            If the unit is ``None`` or not otherwise specified it will raise a
            ``ValueError``

    Raises
    ------
    ValueError
        If the ``uncertainty`` or ``mask`` inputs cannot be broadcast (e.g.,
        match shape) onto ``data``.

    Methods
    -------
    read(\\*args, \\**kwargs)
        ``Classmethod`` to create an CCDData instance based on a ``FITS`` file.
        This method uses :func:`fits_ccddata_reader` with the provided
        parameters.
    write(\\*args, \\**kwargs)
        Writes the contents of the CCDData instance into a new ``FITS`` file.
        This method uses :func:`fits_ccddata_writer` with the provided
        parameters.

    Notes
    -----
    `~ccdproc.CCDData` objects can be easily converted to a regular
     Numpy array using `numpy.asarray`.

    For example::

        >>> from ccdproc import CCDData
        >>> import numpy as np
        >>> x = CCDData([1,2,3], unit='adu')
        >>> np.asarray(x)
        array([1, 2, 3])

    This is useful, for example, when plotting a 2D image using
    matplotlib.

        >>> from ccdproc import CCDData
        >>> from matplotlib import pyplot as plt   # doctest: +SKIP
        >>> x = CCDData([[1,2,3], [4,5,6]], unit='adu')
        >>> plt.imshow(x)   # doctest: +SKIP

    """
    def __init__(self, *args, **kwd):
        if 'meta' not in kwd:
            kwd['meta'] = kwd.pop('header', None)
        if 'header' in kwd:
            raise ValueError("can't have both header and meta.")

        super(CCDData, self).__init__(*args, **kwd)

        # Check if a unit is set. This can be temporarly disabled by the
        # _CCDDataUnit contextmanager.
        if _config_ccd_requires_unit and self.unit is None:
            raise ValueError("a unit for CCDData must be specified.")

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        self._data = value

    @property
    def wcs(self):
        return self._wcs

    @wcs.setter
    def wcs(self, value):
        self._wcs = value

    @property
    def unit(self):
        return self._unit

    @unit.setter
    def unit(self, value):
        self._unit = u.Unit(value)

    @property
    def header(self):
        return self._meta

    @header.setter
    def header(self, value):
        self.meta = value

    @property
    def meta(self):
        return self._meta

    @meta.setter
    def meta(self, value):
        if value is None:
            self._meta = OrderedDict()
        else:
            if hasattr(value, 'keys'):
                self._meta = value
            else:
                raise TypeError(
                    'the meta attribute of CCDData must be dict-like.')

    @property
    def uncertainty(self):
        return self._uncertainty

    @uncertainty.setter
    def uncertainty(self, value):
        if value is not None:
            if isinstance(value, NDUncertainty):
                if getattr(value, '_parent_nddata', None) is not None:
                    value = value.__class__(value, copy=False)
                self._uncertainty = value
            elif isinstance(value, np.ndarray):
                if value.shape != self.shape:
                    raise ValueError("uncertainty must have same shape as "
                                     "data.")
                self._uncertainty = StdDevUncertainty(value)
                log.info("array provided for uncertainty; assuming it is a "
                         "StdDevUncertainty.")
            else:
                raise TypeError("uncertainty must be an instance of a "
                                "NDUncertainty object or a numpy array.")
            self._uncertainty.parent_nddata = self
        else:
            self._uncertainty = value

    def to_hdu(self, hdu_mask='MASK', hdu_uncertainty='UNCERT',
               hdu_flags=None, wcs_relax=True):
        """Creates an HDUList object from a CCDData object.

        Parameters
        ----------
        hdu_mask, hdu_uncertainty, hdu_flags : str or None, optional
            If it is a string append this attribute to the HDUList as
            `~astropy.io.fits.ImageHDU` with the string as extension name.
            Flags are not supported at this time. If ``None`` this attribute
            is not appended.
            Default is ``'MASK'`` for mask, ``'UNCERT'`` for uncertainty and
            ``None`` for flags.

        wcs_relax : bool
            Value of the ``relax`` parameter to use in converting the WCS to a
            FITS header using `~astropy.wcs.WCS.to_header`. The common
            ``CTYPE`` ``RA---TAN-SIP`` and ``DEC--TAN-SIP`` requires
            ``relax=True`` for the ``-SIP`` part of the ``CTYPE`` to be
            preserved.

        Raises
        -------
        ValueError
            - If ``self.mask`` is set but not a `numpy.ndarray`.
            - If ``self.uncertainty`` is set but not a
              `~astropy.nddata.StdDevUncertainty`.
            - If ``self.uncertainty`` is set but has another unit then
              ``self.data``.

        NotImplementedError
            Saving flags is not supported.

        Returns
        -------
        hdulist : `~astropy.io.fits.HDUList`
        """
        if isinstance(self.header, fits.Header):
            # Copy here so that we can modify the HDU header by adding WCS
            # information without changing the header of the CCDData object.
            header = self.header.copy()
        else:
            # Because _insert_in_metadata_fits_safe is written as a method
            # we need to create a dummy CCDData instance to hold the FITS
            # header we are constructing. This probably indicates that
            # _insert_in_metadata_fits_safe should be rewritten in a more
            # sensible way...
            dummy_ccd = CCDData([1], meta=fits.Header(), unit="adu")
            for k, v in self.header.items():
                dummy_ccd._insert_in_metadata_fits_safe(k, v)
            header = dummy_ccd.header
        if self.unit is not u.dimensionless_unscaled:
            header['bunit'] = self.unit.to_string()
        if self.wcs:
            # Simply extending the FITS header with the WCS can lead to
            # duplicates of the WCS keywords; iterating over the WCS
            # header should be safer.
            #
            # Turns out if I had read the io.fits.Header.extend docs more
            # carefully, I would have realized that the keywords exist to
            # avoid duplicates and preserve, as much as possible, the
            # structure of the commentary cards.
            #
            # Note that until astropy/astropy#3967 is closed, the extend
            # will fail if there are comment cards in the WCS header but
            # not header.
            wcs_header = self.wcs.to_header(relax=wcs_relax)
            header.extend(wcs_header, useblanks=False, update=True)
        hdus = [fits.PrimaryHDU(self.data, header)]

        if hdu_mask and self.mask is not None:
            # Always assuming that the mask is a np.ndarray (check that it has
            # a 'shape').
            if not hasattr(self.mask, 'shape'):
                raise ValueError('only a numpy.ndarray mask can be saved.')

            # Convert boolean mask to uint since io.fits cannot handle bool.
            hduMask = fits.ImageHDU(self.mask.astype(np.uint8), name=hdu_mask)
            hdus.append(hduMask)

        if hdu_uncertainty and self.uncertainty is not None:
            # We need to save some kind of information which uncertainty was
            # used so that loading the HDUList can infer the uncertainty type.
            # No idea how this can be done so only allow StdDevUncertainty.
            if self.uncertainty.__class__.__name__ != 'StdDevUncertainty':
                raise ValueError('only StdDevUncertainty can be saved.')

            # Assuming uncertainty is an StdDevUncertainty save just the array
            # this might be problematic if the Uncertainty has a unit differing
            # from the data so abort for different units. This is important for
            # astropy > 1.2
            if (hasattr(self.uncertainty, 'unit') and
                    self.uncertainty.unit is not None and
                    self.uncertainty.unit != self.unit):
                raise ValueError('saving uncertainties with a unit differing'
                                 'from the data unit is not supported.')

            hduUncert = fits.ImageHDU(self.uncertainty.array,
                                      name=hdu_uncertainty)
            hdus.append(hduUncert)

        if hdu_flags and self.flags:
            raise NotImplementedError('adding the flags to a HDU is not '
                                      'supported at this time.')

        hdulist = fits.HDUList(hdus)

        return hdulist

    def copy(self):
        """
        Return a copy of the CCDData object.
        """
        try:
            return self.__class__(self, copy=True)
        except TypeError:
            new = self.__class__(copy.deepcopy(self))
        return new

    def _ccddata_arithmetic(self, other, operation, scale_uncertainty=False):
        """
        Perform the common parts of arithmetic operations on CCDData objects.

        This should only be called when ``other`` is a Quantity or a number.
        """
        # THE "1 *" IS NECESSARY to get the right result, at least in
        # astropy-0.4dev. Using the np.multiply, etc, methods with a Unit
        # and a Quantity is currently broken, but it works with two Quantity
        # arguments.
        if isinstance(other, u.Quantity):
            if (operation.__name__ in ['add', 'subtract'] and
                    self.unit != other.unit):
                # For addition and subtraction we need to convert the unit
                # to the same unit otherwise operating on the values alone will
                # give wrong results (#291)
                other_value = other.to(self.unit).value
            else:
                other_value = other.value
        elif isinstance(other, numbers.Number):
            other_value = other
        else:
            raise TypeError("cannot do arithmetic with type '{0}' "
                            "and 'CCDData'".format(type(other)))

        result_unit = operation(1 * self.unit, other).unit
        result_data = operation(self.data, other_value)

        if self.uncertainty:
            result_uncertainty = self.uncertainty.array
            if scale_uncertainty:
                result_uncertainty = operation(result_uncertainty, other_value)
            result_uncertainty = StdDevUncertainty(result_uncertainty)
        else:
            result_uncertainty = None

        new_mask = copy.deepcopy(self.mask)
        new_meta = copy.deepcopy(self.meta)
        new_wcs = copy.deepcopy(self.wcs)
        result = CCDData(result_data, unit=result_unit, mask=new_mask,
                         uncertainty=result_uncertainty,
                         meta=new_meta, wcs=new_wcs)
        return result

    def multiply(self, other, compare_wcs='first_found'):
        if isinstance(other, CCDData):
            if compare_wcs is None or compare_wcs == 'first_found':
                tmp_wcs_1, tmp_wcs_2 = self.wcs, other.wcs
                self.wcs, other.wcs = None, None

                # Determine the WCS of the result
                if compare_wcs is None:
                    result_wcs = None
                else:
                    result_wcs = tmp_wcs_1 if tmp_wcs_1 else tmp_wcs_2

                result = super(CCDData, self).multiply(other)
                result.wcs = result_wcs
                self.wcs, other.wcs = tmp_wcs_1, tmp_wcs_2
                return result
            else:
                if hasattr(self, '_arithmetics_wcs'):
                    return super(CCDData, self).multiply(
                        other, compare_wcs=compare_wcs)
                else:
                    raise ImportError("wcs_compare functionality requires "
                                      "astropy 1.2 or greater.")

        return self._ccddata_arithmetic(other, np.multiply,
                                        scale_uncertainty=True)

    def divide(self, other, compare_wcs='first_found'):
        if isinstance(other, CCDData):
            if compare_wcs is None or compare_wcs == 'first_found':
                tmp_wcs_1, tmp_wcs_2 = self.wcs, other.wcs
                self.wcs, other.wcs = None, None

                # Determine the WCS of the result
                if compare_wcs is None:
                    result_wcs = None
                else:
                    result_wcs = tmp_wcs_1 if tmp_wcs_1 else tmp_wcs_2

                result = super(CCDData, self).divide(other)
                result.wcs = result_wcs
                self.wcs, other.wcs = tmp_wcs_1, tmp_wcs_2
                return result
            else:
                if hasattr(self, '_arithmetics_wcs'):
                    return super(CCDData, self).divide(
                        other, compare_wcs=compare_wcs)
                else:
                    raise ImportError("wcs_compare functionality requires "
                                      "astropy 1.2 or greater.")

        return self._ccddata_arithmetic(other, np.divide,
                                        scale_uncertainty=True)

    def add(self, other, compare_wcs='first_found'):
        if isinstance(other, CCDData):
            if compare_wcs is None or compare_wcs == 'first_found':
                tmp_wcs_1, tmp_wcs_2 = self.wcs, other.wcs
                self.wcs, other.wcs = None, None

                # Determine the WCS of the result
                if compare_wcs is None:
                    result_wcs = None
                else:
                    result_wcs = tmp_wcs_1 if tmp_wcs_1 else tmp_wcs_2

                result = super(CCDData, self).add(other)
                result.wcs = result_wcs
                self.wcs, other.wcs = tmp_wcs_1, tmp_wcs_2
                return result
            else:
                if hasattr(self, '_arithmetics_wcs'):
                    return super(CCDData, self).add(
                        other, compare_wcs=compare_wcs)
                else:
                    raise ImportError("wcs_compare functionality requires "
                                      "astropy 1.2 or greater.")

        return self._ccddata_arithmetic(other, np.add,
                                        scale_uncertainty=False)

    def subtract(self, other, compare_wcs='first_found'):
        if isinstance(other, CCDData):
            if compare_wcs is None or compare_wcs == 'first_found':
                tmp_wcs_1, tmp_wcs_2 = self.wcs, other.wcs
                self.wcs, other.wcs = None, None

                # Determine the WCS of the result
                if compare_wcs is None:
                    result_wcs = None
                else:
                    result_wcs = tmp_wcs_1 if tmp_wcs_1 else tmp_wcs_2

                result = super(CCDData, self).subtract(other)
                result.wcs = result_wcs
                self.wcs, other.wcs = tmp_wcs_1, tmp_wcs_2
                return result

            else:
                if hasattr(self, '_arithmetics_wcs'):
                    return super(CCDData, self).subtract(
                        other, compare_wcs=compare_wcs)
                else:
                    raise ImportError("wcs_compare functionality requires "
                                      "astropy 1.2 or greater.")

        return self._ccddata_arithmetic(other, np.subtract,
                                        scale_uncertainty=False)

    # Use NDDataArithmetic methods if astropy version is 1.2 or greater
    if not _ASTROPY_LT_1_2:
        del add, subtract, divide, multiply, _ccddata_arithmetic

        add = _arithmetic(np.add)(NDDataArray.add)
        subtract = _arithmetic(np.subtract)(NDDataArray.subtract)
        multiply = _arithmetic(np.multiply)(NDDataArray.multiply)
        divide = _arithmetic(np.true_divide)(NDDataArray.divide)

    def _insert_in_metadata_fits_safe(self, key, value):
        """
        Insert key/value pair into metadata in a way that FITS can serialize.

        Parameters
        ----------
        key : str
            Key to be inserted in dictionary.

        value : str or None
            Value to be inserted.

        Notes
        -----
        This addresses a shortcoming of the FITS standard. There are length
        restrictions on both the ``key`` (8 characters) and ``value`` (72
        characters) in the FITS standard. There is a convention for handline
        long keywords and a convention for handling long values, but the
        two conventions cannot be used at the same time.

        Autologging in `ccdproc` frequently creates keywords/values with this
        combination. The workaround is to use a shortened name for the keyword.
        """
        from .core import _short_names

        if key in _short_names and isinstance(self.meta, fits.Header):
            # This keyword was (hopefully) added by autologging but the
            # combination of it and its value not FITS-compliant in two
            # ways: the keyword name may be more than 8 characters and
            # the value may be too long. FITS cannot handle both of
            # those problems at once, so this fixes one of those
            # problems...
            # Shorten, sort of...
            short_name = _short_names[key]
            self.meta['HIERARCH {0}'.format(key.upper())] = (
                short_name, "Shortened name for ccdproc command")
            self.meta[short_name] = value
        else:
            self.meta[key] = value


# This needs to be importable by the tests...
_KEEP_THESE_KEYWORDS_IN_HEADER = [
    'JD-OBS',
    'MJD-OBS',
    'DATE-OBS'
]


def _generate_wcs_and_update_header(hdr):
    """
    Generate a WCS object from a header and remove the WCS-specific
    keywords from the header.
    Parameters
    ----------
    hdr : astropy.io.fits.header or other dict-like
    Returns
    -------
    new_header, wcs
    """

    # Try constructing a WCS object.
    try:
        wcs = WCS(hdr)
    except Exception as exc:
        # Normally WCS only raises Warnings and doesn't fail but in rare
        # cases (malformed header) it could fail...
        log.info('An exception happened while extracting WCS informations from '
                 'the Header.\n{}: {}'.format(type(exc).__name__, str(exc)))
        return hdr, None
    # Test for success by checking to see if the wcs ctype has a non-empty
    # value, return None for wcs if ctype is empty.
    if not wcs.wcs.ctype[0]:
        return hdr, None

    new_hdr = hdr.copy()
    # If the keywords below are in the header they are also added to WCS.
    # It seems like they should *not* be removed from the header, though.

    wcs_header = wcs.to_header(relax=True)
    for k in wcs_header:
        if k not in _KEEP_THESE_KEYWORDS_IN_HEADER:
            try:
                new_hdr.remove(k)
            except KeyError:
                pass
    return new_hdr, wcs


def fits_ccddata_reader(filename, hdu=0, unit=None, hdu_uncertainty='UNCERT',
                        hdu_mask='MASK', hdu_flags=None, **kwd):
    """
    Generate a CCDData object from a FITS file.

    Parameters
    ----------
    filename : str
        Name of fits file.

    hdu : int, optional
        FITS extension from which CCDData should be initialized. If zero and
        and no data in the primary extension, it will search for the first
        extension with data. The header will be added to the primary header.
        Default is ``0``.

    unit : `~astropy.units.Unit`, optional
        Units of the image data. If this argument is provided and there is a
        unit for the image in the FITS header (the keyword ``BUNIT`` is used
        as the unit, if present), this argument is used for the unit.
        Default is ``None``.

    hdu_uncertainty : str or None, optional
        FITS extension from which the uncertainty should be initialized. If the
        extension does not exist the uncertainty of the CCDData is ``None``.
        Default is ``'UNCERT'``.

    hdu_mask : str or None, optional
        FITS extension from which the mask should be initialized. If the
        extension does not exist the mask of the CCDData is ``None``.
        Default is ``'MASK'``.

    hdu_flags : str or None, optional
        Currently not implemented.
        Default is ``None``.

    kwd :
        Any additional keyword parameters are passed through to the FITS reader
        in :mod:`astropy.io.fits`; see Notes for additional discussion.

    Notes
    -----
    FITS files that contained scaled data (e.g. unsigned integer images) will
    be scaled and the keywords used to manage scaled data in
    :mod:`astropy.io.fits` are disabled.
    """
    unsupport_open_keywords = {
        'do_not_scale_image_data': ('Image data must be scaled to perform '
                                    'ccdproc operations.'),
        'scale_back': 'Scale information is not preserved.'
    }
    for key, msg in unsupport_open_keywords.items():
        if key in kwd:
            prefix = 'unsupported keyword: {0}.'.format(key)
            raise TypeError(' '.join([prefix, msg]))
    with fits.open(filename, **kwd) as hdus:
        hdr = hdus[hdu].header

        if hdu_uncertainty is not None and hdu_uncertainty in hdus:
            uncertainty = StdDevUncertainty(hdus[hdu_uncertainty].data)
        else:
            uncertainty = None

        if hdu_mask is not None and hdu_mask in hdus:
            # Mask is saved as uint but we want it to be boolean.
            mask = hdus[hdu_mask].data.astype(np.bool_)
        else:
            mask = None

        if hdu_flags is not None and hdu_flags in hdus:
            raise NotImplementedError('loading flags is currently not '
                                      'supported.')

        # search for the first instance with data if
        # the primary header is empty.
        if hdu == 0 and hdus[hdu].data is None:
            for i in range(len(hdus)):
                if hdus.fileinfo(i)['datSpan'] > 0:
                    hdu = i
                    comb_hdr = hdus[hdu].header.copy()
                    comb_hdr.extend(hdr, unique=True)
                    hdr = comb_hdr
                    log.info("first HDU with data is extension "
                             "{0}.".format(hdu))
                    break

        if 'bunit' in hdr:
            fits_unit_string = hdr['bunit']
            # patch to handle FITS files using ADU for the unit instead of the
            # standard version of 'adu'
            if fits_unit_string.strip().lower() == 'adu':
                fits_unit_string = fits_unit_string.lower()
        else:
            fits_unit_string = None

        if unit is not None and fits_unit_string:
            log.info("using the unit {0} passed to the FITS reader instead of "
                     "the unit {1} in the FITS file.".format(unit,
                                                             fits_unit_string))

        use_unit = unit or fits_unit_string
        hdr, wcs = _generate_wcs_and_update_header(hdr)
        ccd_data = CCDData(hdus[hdu].data, meta=hdr, unit=use_unit,
                           mask=mask, uncertainty=uncertainty, wcs=wcs)

    return ccd_data


def fits_ccddata_writer(ccd_data, filename, hdu_mask='MASK',
                        hdu_uncertainty='UNCERT', hdu_flags=None, **kwd):
    """
    Write CCDData object to FITS file.

    Parameters
    ----------
    filename : str
        Name of file.

    hdu_mask, hdu_uncertainty, hdu_flags : str or None, optional
        If it is a string append this attribute to the HDUList as
        `~astropy.io.fits.ImageHDU` with the string as extension name.
        Flags are not supported at this time. If ``None`` this attribute
        is not appended.
        Default is ``'MASK'`` for mask, ``'UNCERT'`` for uncertainty and
        ``None`` for flags.

    kwd :
        All additional keywords are passed to :py:mod:`astropy.io.fits`

    Raises
    -------
    ValueError
        - If ``self.mask`` is set but not a `numpy.ndarray`.
        - If ``self.uncertainty`` is set but not a
          `~astropy.nddata.StdDevUncertainty`.
        - If ``self.uncertainty`` is set but has another unit then
          ``self.data``.

    NotImplementedError
        Saving flags is not supported.
    """
    hdu = ccd_data.to_hdu(hdu_mask=hdu_mask, hdu_uncertainty=hdu_uncertainty,
                          hdu_flags=hdu_flags)
    hdu.writeto(filename, **kwd)

# This should be be a tuple to ensure it isn't inadvertently changed
# elsewhere.
_recognized_fits_file_extensions = ('fit', 'fits', 'fts')

if _ASTROPY_LT_1_3:
    def is_fits(origin, filepath, fileobj, *args, **kwargs):
        """
        Wrapper around astropy.io.fits.connect.is_fits that handles the extra
        extension.

        Can be removed if fts is added to astropy.io as a recognized FITS
        extension.
        """
        if ((filepath is not None) and
                filepath.lower().endswith(('.fts', '.fts.gz'))):

            return True

        else:
            return fits.connect.is_fits(origin, filepath, fileobj,
                                        *args, **kwargs)
else:
    is_fits = fits.connect.is_fits

if _ASTROPY_LT_1_3:
    registry.register_reader('fits', CCDData, fits_ccddata_reader)
    registry.register_writer('fits', CCDData, fits_ccddata_writer)
    registry.register_identifier('fits', CCDData, is_fits)
else:
    with registry.delay_doc_updates(CCDData):
        registry.register_reader('fits', CCDData, fits_ccddata_reader)
        registry.register_writer('fits', CCDData, fits_ccddata_writer)
        registry.register_identifier('fits', CCDData, is_fits)

try:
    CCDData.read.__doc__ = fits_ccddata_reader.__doc__
except AttributeError:
    CCDData.read.__func__.__doc__ = fits_ccddata_reader.__doc__

try:
    CCDData.write.__doc__ = fits_ccddata_writer.__doc__
except AttributeError:
    CCDData.write.__func__.__doc__ = fits_ccddata_writer.__doc__

# CCDData moved to astropy core so we just import them from there (overwriting)
# the classes defined here.
if _ASTROPY_GT_2_0:
    from astropy.nddata import fits_ccddata_reader, fits_ccddata_writer, CCDData