/usr/lib/python2.7/dist-packages/ginga/util/dp.py is in python-ginga 2.6.1-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 | #
# dp.py -- Data pipeline and reduction routines
#
# Eric Jeschke (eric@naoj.org)
#
# Copyright (c) Eric R. Jeschke. All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import numpy
from collections import OrderedDict
from ginga import AstroImage, colors
from ginga.RGBImage import RGBImage
from ginga.util import wcs
# counter used to name anonymous images
prefixes = dict(dp=0)
def get_image_name(image, pfx='dp'):
global prefixes
name = image.get('name', None)
if name is None:
if not pfx in prefixes:
prefixes[pfx] = 0
name = '{0}{1:d}'.format(pfx, prefixes[pfx])
prefixes[pfx] += 1
image.set(name=name)
return name
def make_image(data_np, oldimage, header, pfx='dp'):
# Prepare a new image with the numpy array as data
image = AstroImage.AstroImage()
image.set_data(data_np)
# Set the header to be the old image header updated
# with items from the new header
oldhdr = oldimage.get_header()
oldhdr.update(header)
image.update_keywords(oldhdr)
# give the image a name
get_image_name(image, pfx=pfx)
return image
def create_blank_image(ra_deg, dec_deg, fov_deg, px_scale, rot_deg,
cdbase=[1, 1], dtype=None, logger=None, pfx='dp'):
# ra and dec in traditional format
ra_txt = wcs.raDegToString(ra_deg, format='%02d:%02d:%06.3f')
dec_txt = wcs.decDegToString(dec_deg, format='%s%02d:%02d:%05.2f')
# Create an empty image
imagesize = int(round(fov_deg / px_scale))
# round to an even size
if imagesize % 2 != 0:
imagesize += 1
## # round to an odd size
## if imagesize % 2 == 0:
## imagesize += 1
width = height = imagesize
if dtype is None:
dtype = numpy.float32
data = numpy.zeros((height, width), dtype=dtype)
crpix = float(imagesize // 2)
header = OrderedDict((('SIMPLE', True),
('BITPIX', -32),
('EXTEND', True),
('NAXIS', 2),
('NAXIS1', imagesize),
('NAXIS2', imagesize),
('RA', ra_txt),
('DEC', dec_txt),
('EQUINOX', 2000.0),
('OBJECT', 'MOSAIC'),
('LONPOLE', 180.0),
))
# Add basic WCS keywords
wcshdr = wcs.simple_wcs(crpix, crpix, ra_deg, dec_deg, px_scale,
rot_deg, cdbase=cdbase)
header.update(wcshdr)
# Create image container
image = AstroImage.AstroImage(data, logger=logger)
image.update_keywords(header)
# give the image a name
get_image_name(image, pfx=pfx)
return image
def recycle_image(image, ra_deg, dec_deg, fov_deg, px_scale, rot_deg,
cdbase=[1, 1], logger=None, pfx='dp'):
# ra and dec in traditional format
ra_txt = wcs.raDegToString(ra_deg, format='%02d:%02d:%06.3f')
dec_txt = wcs.decDegToString(dec_deg, format='%s%02d:%02d:%05.2f')
header = image.get_header()
pointing = OrderedDict((('RA', ra_txt),
('DEC', dec_txt),
))
header.update(pointing)
# Update WCS keywords and internal wcs objects
wd, ht = image.get_size()
crpix1 = wd // 2
crpix2 = ht // 2
wcshdr = wcs.simple_wcs(crpix1, crpix2, ra_deg, dec_deg, px_scale,
rot_deg, cdbase=cdbase)
header.update(wcshdr)
# this should update the wcs
image.update_keywords(header)
# zero out data array
data = image.get_data()
data.fill(0)
## # Create new image container sharing same data
## new_image = AstroImage.AstroImage(data, logger=logger)
## new_image.update_keywords(header)
## # give the image a name
## get_image_name(new_image, pfx=pfx)
new_image = image
return new_image
def make_flat(imglist, bias=None):
flats = [ image.get_data() for image in imglist ]
flatarr = numpy.array(flats)
# Take the median of the individual frames
flat = numpy.median(flatarr, axis=0)
# Normalize flat
# mean or median?
#norm = numpy.mean(flat.flat)
norm = numpy.median(flat.flat)
flat = flat / norm
# no zero divisors
flat[flat == 0.0] = 1.0
img_flat = make_image(flat, imglist[0], {}, pfx='flat')
return img_flat
def make_bias(imglist):
biases = [ image.get_data() for image in imglist ]
biasarr = numpy.array(biases)
# Take the median of the individual frames
bias = numpy.median(biasarr, axis=0)
img_bias = make_image(bias, imglist[0], {}, pfx='bias')
return img_bias
def add(image1, image2):
data1_np = image1.get_data()
data2_np = image2.get_data()
result = data1_np + data2_np
image = make_image(result, image1, {}, pfx='add')
return image
def subtract(image1, image2):
data1_np = image1.get_data()
data2_np = image2.get_data()
result = data1_np - data2_np
image = make_image(result, image1, {}, pfx='sub')
return image
def divide(image1, image2):
data1_np = image1.get_data()
data2_np = image2.get_data()
result = data1_np / data2_np
image = make_image(result, image1, {}, pfx='div')
return image
# https://gist.github.com/stscieisenhamer/25bf6287c2c724cb9cc7
def masktorgb(mask, color='lightgreen', alpha=1.0):
"""Convert boolean mask to RGB image object for canvas overlay.
Parameters
----------
mask : ndarray
Boolean mask to overlay. 2D image only.
color : str
Color name accepted by Ginga.
alpha : float
Opacity. Unmasked data are always transparent.
Returns
-------
rgbobj : RGBImage
RGB image for canvas Image object.
Raises
------
ValueError
Invalid mask dimension.
"""
mask = numpy.asarray(mask)
if mask.ndim != 2:
raise ValueError('ndim={0} is not supported'.format(mask.ndim))
ht, wd = mask.shape
r, g, b = colors.lookup_color(color)
rgbobj = RGBImage(data_np = numpy.zeros((ht, wd, 4), dtype=numpy.uint8))
rc = rgbobj.get_slice('R')
gc = rgbobj.get_slice('G')
bc = rgbobj.get_slice('B')
ac = rgbobj.get_slice('A')
ac[:] = 0 # Transparent background
rc[mask] = int(r * 255)
gc[mask] = int(g * 255)
bc[mask] = int(b * 255)
ac[mask] = int(alpha * 255)
# For debugging
#rgbobj.save_as_file('ztmp_rgbobj.png')
return rgbobj
def split_n(lst, sz):
n = len(lst)
k, m = n // sz, n % sz
return [ lst[i * k + min(i, m):(i + 1) * k + min(i + 1, m)]
for i in range(sz) ]
# END
|