|
1 | 1 | # Licensed under a 3-clause BSD style license - see LICENSE.rst |
2 | 2 | # This module implements the base CCDData class. |
3 | | - |
4 | | -import copy |
5 | | -import numbers |
6 | | - |
7 | | -import numpy as np |
8 | | - |
9 | 3 | from astropy.nddata import NDData |
10 | | -from astropy.nddata.nduncertainty import StdDevUncertainty, NDUncertainty |
11 | | -from astropy.io import fits, registry |
12 | | -from astropy.utils.compat.odict import OrderedDict |
13 | | -from astropy import units as u |
14 | | -import astropy |
15 | | - |
16 | | -adu = u.adu |
17 | | -electron = u.def_unit('electron') |
18 | | -photon = u.photon |
19 | | - |
20 | 4 |
|
21 | 5 | class CCDData(NDData): |
22 | 6 |
|
23 | 7 | """A class describing basic CCD data |
24 | 8 |
|
25 | | - The CCDData class is based on the NDData object and includes a data array, |
26 | | - variance frame, mask frame, meta data, units, and WCS information for a |
27 | | - single CCD image. |
28 | | -
|
29 | | - Parameters |
30 | | - ----------- |
31 | | - data : `~numpy.ndarray` or `~astropy.nddata.NDData` |
32 | | - The actual data contained in this `NDData` object. Not that this |
33 | | - will always be copies by *reference* , so you should make copy |
34 | | - the `data` before passing it in if that's the desired behavior. |
35 | | -
|
36 | | - uncertainty : `~astropy.nddata.NDUncertainty`, optional |
37 | | - Uncertainties on the data. |
38 | | -
|
39 | | - mask : `~numpy.ndarray`, optional |
40 | | - Mask for the data, given as a boolean Numpy array with a shape |
41 | | - matching that of the data. The values must be ``False`` where |
42 | | - the data is *valid* and ``True`` when it is not (like Numpy |
43 | | - masked arrays). If `data` is a numpy masked array, providing |
44 | | - `mask` here will causes the mask from the masked array to be |
45 | | - ignored. |
46 | | -
|
47 | | - flags : `~numpy.ndarray` or `~astropy.nddata.FlagCollection`, optional |
48 | | - Flags giving information about each pixel. These can be specified |
49 | | - either as a Numpy array of any type with a shape matching that of the |
50 | | - data, or as a `~astropy.nddata.FlagCollection` instance which has a |
51 | | - shape matching that of the data. |
52 | | -
|
53 | | - wcs : undefined, optional |
54 | | - WCS-object containing the world coordinate system for the data. |
55 | | -
|
56 | | - .. warning:: |
57 | | - This is not yet defind because the discussion of how best to |
58 | | - represent this class's WCS system generically is still under |
59 | | - consideration. For now just leave it as None |
60 | | -
|
61 | | - meta : `dict`-like object, optional |
62 | | - Metadata for this object. "Metadata" here means all information that |
63 | | - is included with this object but not part of any other attribute |
64 | | - of this particular object. e.g., creation date, unique identifier, |
65 | | - simulation parameters, exposure time, telescope name, etc. |
66 | | -
|
67 | | - unit : `astropy.units.UnitBase` instance or str, optional |
68 | | - The units of the data. |
69 | | -
|
70 | | - Raises |
71 | | - ------ |
72 | | - ValueError |
73 | | - If the `uncertainty` or `mask` inputs cannot be broadcast (e.g., match |
74 | | - shape) onto `data`. |
75 | | -
|
76 | | - Notes |
77 | | - ----- |
78 | | - `NDData` objects can be easily converted to a regular Numpy array |
79 | | - using `numpy.asarray` |
80 | | -
|
81 | | - For example:: |
82 | | -
|
83 | | - >>> from astropy.nddata import NDData |
84 | | - >>> import numpy as np |
85 | | - >>> x = NDData([1,2,3]) |
86 | | - >>> np.asarray(x) |
87 | | - array([1, 2, 3]) |
88 | | -
|
89 | | - If the `NDData` object has a `mask`, `numpy.asarray` will return a |
90 | | - Numpy masked array. |
91 | | -
|
92 | | - This is useful, for example, when plotting a 2D image using |
93 | | - matplotlib:: |
94 | | -
|
95 | | - >>> from astropy.nddata import NDData |
96 | | - >>> from matplotlib import pyplot as plt |
97 | | - >>> x = NDData([[1,2,3], [4,5,6]]) |
98 | | - >>> plt.imshow(x) |
99 | | -
|
100 | 9 | """ |
101 | 10 | def __init__(self, *args, **kwd): |
102 | 11 | super(CCDData, self).__init__(*args, **kwd) |
103 | 12 | if self.unit is None: |
104 | 13 | raise ValueError("Unit for CCDData must be specified") |
105 | | - |
106 | | - @property |
107 | | - def header(self): |
108 | | - return self._meta |
109 | | - |
110 | | - @header.setter |
111 | | - def header(self, value): |
112 | | - self.meta = value |
113 | | - |
114 | | - @property |
115 | | - def meta(self): |
116 | | - return self._meta |
117 | | - |
118 | | - @meta.setter |
119 | | - def meta(self, value): |
120 | | - if value is None: |
121 | | - self._meta = fits.Header() |
122 | | - elif isinstance(value, fits.Header): |
123 | | - self._meta = value |
124 | | - else: |
125 | | - h = fits.Header() |
126 | | - try: |
127 | | - for k, v in value.items(): |
128 | | - h[k] = v |
129 | | - except (ValueError, AttributeError): |
130 | | - raise TypeError('NDData meta attribute must be dict-like') |
131 | | - self._meta = h |
132 | | - |
133 | | - |
134 | | - @property |
135 | | - def uncertainty(self): |
136 | | - return self._uncertainty |
137 | | - |
138 | | - @uncertainty.setter |
139 | | - def uncertainty(self, value): |
140 | | - if value is not None: |
141 | | - if isinstance(value, NDUncertainty): |
142 | | - self._uncertainty = value |
143 | | - self._uncertainty._parent_nddata = self |
144 | | - else: |
145 | | - raise TypeError("Uncertainty must be an instance of a " |
146 | | - "NDUncertainty object") |
147 | | - else: |
148 | | - self._uncertainty = value |
149 | | - |
150 | | - def to_hdu(self): |
151 | | - """Creates an HDUList object from a CCDData object |
152 | | -
|
153 | | - Raises |
154 | | - ------- |
155 | | - ValueError |
156 | | - Multi-Exenstion FITS files are not supported |
157 | | -
|
158 | | - Returns |
159 | | - ------- |
160 | | - hdulist : astropy.io.fits.HDUList object |
161 | | -
|
162 | | - """ |
163 | | - hdu = fits.PrimaryHDU(self.data, self.header) |
164 | | - hdulist = fits.HDUList([hdu]) |
165 | | - return hdulist |
166 | | - |
167 | | - def copy(self): |
168 | | - """ |
169 | | - Return a copy of the CCDData object |
170 | | - """ |
171 | | - return copy.deepcopy(self) |
172 | | - |
173 | | - def _ccddata_arithmetic(self, other, operation, scale_uncertainty=False): |
174 | | - """ |
175 | | - Perform the common parts of arithmetic operations on CCDData objects |
176 | | -
|
177 | | - This should only be called when `other` is a Quantity or a number |
178 | | - """ |
179 | | - # THE "1 *" IS NECESSARY to get the right result, at least in |
180 | | - # astropy-0.4dev. Using the np.multiply, etc, methods with a Unit |
181 | | - # and a Quantity is currently broken, but it works with two Quantity |
182 | | - # arguments. |
183 | | - if isinstance(other, u.Quantity): |
184 | | - other_value = other.value |
185 | | - elif isinstance(other, numbers.Number): |
186 | | - other_value = other |
187 | | - else: |
188 | | - raise TypeError("Cannot do arithmetic with type '{0}' " |
189 | | - "and 'CCDData'".format(type(other))) |
190 | | - |
191 | | - result_unit = operation(1 * self.unit, other).unit |
192 | | - result_data = operation(self.data, other_value) |
193 | | - |
194 | | - if self.uncertainty: |
195 | | - result_uncertainty = self.uncertainty.array |
196 | | - if scale_uncertainty: |
197 | | - result_uncertainty = operation(result_uncertainty, other_value) |
198 | | - result_uncertainty = StdDevUncertainty(result_uncertainty) |
199 | | - else: |
200 | | - result_uncertainty = None |
201 | | - |
202 | | - result = CCDData(data=result_data, unit=result_unit, |
203 | | - uncertainty=result_uncertainty, |
204 | | - meta=self.meta) |
205 | | - return result |
206 | | - |
207 | | - def multiply(self, other): |
208 | | - if isinstance(other, CCDData): |
209 | | - return super(CCDData, self).multiply(other) |
210 | | - |
211 | | - return self._ccddata_arithmetic(other, np.multiply, |
212 | | - scale_uncertainty=True) |
213 | | - |
214 | | - def divide(self, other): |
215 | | - if isinstance(other, CCDData): |
216 | | - return super(CCDData, self).divide(other) |
217 | | - |
218 | | - return self._ccddata_arithmetic(other, np.divide, |
219 | | - scale_uncertainty=True) |
220 | | - |
221 | | - def add(self, other): |
222 | | - if isinstance(other, CCDData): |
223 | | - return super(CCDData, self).add(other) |
224 | | - |
225 | | - return self._ccddata_arithmetic(other, np.add, |
226 | | - scale_uncertainty=False) |
227 | | - |
228 | | - def subtract(self, other): |
229 | | - if isinstance(other, CCDData): |
230 | | - return super(CCDData, self).subtract(other) |
231 | | - |
232 | | - return self._ccddata_arithmetic(other, np.subtract, |
233 | | - scale_uncertainty=False) |
234 | | - |
235 | | - |
236 | | -def fits_ccddata_reader(filename, hdu=0, unit=None, **kwd): |
237 | | - """ |
238 | | - Generate a CCDData object from a FITS file |
239 | | -
|
240 | | - Parameters |
241 | | - ---------- |
242 | | -
|
243 | | - filename : str |
244 | | - Name of fits file. |
245 | | -
|
246 | | - hdu : int, optional |
247 | | - FITS extension from which CCDData should be initialized. |
248 | | -
|
249 | | - unit : astropy.units.Unit |
250 | | - Units of the image data |
251 | | -
|
252 | | - kwd : |
253 | | - Any additional keyword parameters are passed through to the FITS reader |
254 | | - in :mod:`astropy.io.fits`; see Notes for additional discussion. |
255 | | -
|
256 | | - Notes |
257 | | - ----- |
258 | | -
|
259 | | - FITS files that contained scaled data (e.g. unsigned integer images) will |
260 | | - be scaled and the keywords used to manage scaled data in |
261 | | - :mod:`astropy.io.fits` are disabled. |
262 | | - """ |
263 | | - unsupport_open_keywords = { |
264 | | - 'do_not_scale_image_data': ('Image data must be scaled to perform ' |
265 | | - 'ccdproc operations.'), |
266 | | - 'scale_back': 'Scale information is not preserved.' |
267 | | - } |
268 | | - for key, msg in unsupport_open_keywords.items(): |
269 | | - if key in kwd: |
270 | | - prefix = 'Unsupported keyword: {0}.'.format(key) |
271 | | - raise TypeError(' '.join([prefix, msg])) |
272 | | - hdus = fits.open(filename, **kwd) |
273 | | - ccd_data = CCDData(hdus[hdu].data, meta=hdus[hdu].header, unit=unit) |
274 | | - hdus.close() |
275 | | - return ccd_data |
276 | | - |
277 | | - |
278 | | -def fits_ccddata_writer(ccd_data, filename, **kwd): |
279 | | - """ |
280 | | - Write CCDData object to FITS file |
281 | | -
|
282 | | - Parameters |
283 | | - ---------- |
284 | | -
|
285 | | - filename : str |
286 | | - Name of file |
287 | | -
|
288 | | - kwd : |
289 | | - All additional keywords are passed to :py:mod:`astropy.io.fits` |
290 | | - """ |
291 | | - hdu = fits.PrimaryHDU(data=ccd_data.data, header=ccd_data.header) |
292 | | - hdu.writeto(filename, **kwd) |
293 | | - |
294 | | - |
295 | | -registry.register_reader('fits', CCDData, fits_ccddata_reader) |
296 | | -registry.register_writer('fits', CCDData, fits_ccddata_writer) |
297 | | -registry.register_identifier('fits', CCDData, fits.connect.is_fits) |
0 commit comments