2018-03-09 13:08:47 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
from astropy.io import fits
|
|
|
|
from astropy.time import Time
|
|
|
|
from astropy import wcs
|
2018-05-07 14:19:05 -06:00
|
|
|
from scipy import ndimage
|
2018-04-21 03:12:39 -06:00
|
|
|
|
2018-07-22 01:36:20 -06:00
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
class observation:
|
|
|
|
"""Satellite observation"""
|
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
def __init__(self, ff, mjd, x0, y0):
|
2018-03-09 13:08:47 -07:00
|
|
|
"""Define an observation"""
|
|
|
|
|
|
|
|
# Store
|
2018-04-21 03:12:39 -06:00
|
|
|
self.mjd = mjd
|
|
|
|
self.x0 = x0
|
|
|
|
self.y0 = y0
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Get times
|
2018-04-21 03:12:39 -06:00
|
|
|
self.nfd = Time(self.mjd, format='mjd', scale='utc').isot
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Correct for rotation
|
2019-05-12 08:11:15 -06:00
|
|
|
tobs = Time(ff.mjd + 0.5 * ff.texp / 86400.0,
|
|
|
|
format='mjd',
|
|
|
|
scale='utc')
|
2018-04-21 03:12:39 -06:00
|
|
|
tobs.delta_ut1_utc = 0
|
|
|
|
hobs = tobs.sidereal_time("mean", longitude=0.0).degree
|
|
|
|
tmid = Time(self.mjd, format='mjd', scale='utc')
|
|
|
|
tmid.delta_ut1_utc = 0
|
|
|
|
hmid = tmid.sidereal_time("mean", longitude=0.0).degree
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Compute ra/dec
|
2018-04-21 03:12:39 -06:00
|
|
|
world = ff.w.wcs_pix2world(np.array([[self.x0, self.y0]]), 1)
|
2019-05-12 08:11:15 -06:00
|
|
|
self.ra = world[0, 0] + hobs - hmid
|
2018-04-21 03:12:39 -06:00
|
|
|
self.de = world[0, 1]
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
class satid:
|
|
|
|
"""Satellite identifications"""
|
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
def __init__(self, line):
|
|
|
|
s = line.split()
|
|
|
|
self.nfd = s[0]
|
|
|
|
self.x0 = float(s[1])
|
|
|
|
self.y0 = float(s[2])
|
|
|
|
self.t0 = 0.0
|
|
|
|
self.x1 = float(s[3])
|
|
|
|
self.y1 = float(s[4])
|
|
|
|
self.t1 = float(s[5])
|
|
|
|
self.norad = int(s[6])
|
|
|
|
self.catalog = s[7]
|
|
|
|
self.state = s[8]
|
2019-05-12 08:11:15 -06:00
|
|
|
self.dxdt = (self.x1 - self.x0) / (self.t1 - self.t0)
|
|
|
|
self.dydt = (self.y1 - self.y0) / (self.t1 - self.t0)
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
def __repr__(self):
|
2019-05-12 08:11:15 -06:00
|
|
|
return "%s %f %f %f -> %f %f %f %d %s %s" % (
|
|
|
|
self.nfd, self.x0, self.y0, self.t0, self.x1, self.y1, self.t1,
|
|
|
|
self.norad, self.catalog, self.state)
|
2018-04-21 03:12:39 -06:00
|
|
|
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
class fourframe:
|
|
|
|
"""Four frame class"""
|
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
def __init__(self, fname=None):
|
|
|
|
if fname is None:
|
2018-03-09 13:08:47 -07:00
|
|
|
# Initialize empty fourframe
|
2018-04-21 03:12:39 -06:00
|
|
|
self.nx = 0
|
|
|
|
self.ny = 0
|
|
|
|
self.nz = 0
|
|
|
|
self.mjd = -1
|
|
|
|
self.nfd = None
|
|
|
|
self.zavg = None
|
|
|
|
self.zstd = None
|
|
|
|
self.zmax = None
|
|
|
|
self.znum = None
|
|
|
|
self.dt = None
|
|
|
|
self.site_id = 0
|
|
|
|
self.observer = None
|
|
|
|
self.texp = 0.0
|
|
|
|
self.fname = None
|
|
|
|
self.crpix = np.array([0.0, 0.0])
|
|
|
|
self.crval = np.array([0.0, 0.0])
|
|
|
|
self.cd = np.array([[1.0, 0.0], [0.0, 1.0]])
|
|
|
|
self.ctype = ["RA---TAN", "DEC--TAN"]
|
|
|
|
self.cunit = np.array(["deg", "deg"])
|
|
|
|
self.crres = np.array([0.0, 0.0])
|
2018-03-09 13:08:47 -07:00
|
|
|
else:
|
|
|
|
# Read FITS file
|
2018-04-21 03:12:39 -06:00
|
|
|
hdu = fits.open(fname)
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Read image planes
|
2018-04-21 03:12:39 -06:00
|
|
|
self.zavg, self.zstd, self.zmax, self.znum = hdu[0].data
|
2018-03-09 13:08:47 -07:00
|
|
|
|
2018-05-07 14:19:05 -06:00
|
|
|
# Generate sigma frame
|
2019-05-12 08:11:15 -06:00
|
|
|
self.zsig = (self.zmax - self.zavg) / (self.zstd + 1e-9)
|
2018-07-22 01:36:20 -06:00
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Frame properties
|
2018-04-21 03:12:39 -06:00
|
|
|
self.ny, self.nx = self.zavg.shape
|
|
|
|
self.nz = hdu[0].header['NFRAMES']
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Read frame time oselfsets
|
2019-05-12 08:11:15 -06:00
|
|
|
self.dt = np.array(
|
|
|
|
[hdu[0].header['DT%04d' % i] for i in range(self.nz)])
|
2018-04-21 03:12:39 -06:00
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Read header
|
2018-04-21 03:12:39 -06:00
|
|
|
self.mjd = hdu[0].header['MJD-OBS']
|
|
|
|
self.nfd = hdu[0].header['DATE-OBS']
|
|
|
|
self.site_id = hdu[0].header['COSPAR']
|
|
|
|
self.observer = hdu[0].header['OBSERVER']
|
|
|
|
self.texp = hdu[0].header['EXPTIME']
|
|
|
|
self.fname = fname
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Astrometry keywords
|
2019-05-12 08:11:15 -06:00
|
|
|
self.crpix = np.array(
|
|
|
|
[hdu[0].header['CRPIX1'], hdu[0].header['CRPIX2']])
|
|
|
|
self.crval = np.array(
|
|
|
|
[hdu[0].header['CRVAL1'], hdu[0].header['CRVAL2']])
|
|
|
|
self.cd = np.array(
|
|
|
|
[[hdu[0].header['CD1_1'], hdu[0].header['CD1_2']],
|
|
|
|
[hdu[0].header['CD2_1'], hdu[0].header['CD2_2']]])
|
2018-04-21 03:12:39 -06:00
|
|
|
self.ctype = [hdu[0].header['CTYPE1'], hdu[0].header['CTYPE2']]
|
|
|
|
self.cunit = [hdu[0].header['CUNIT1'], hdu[0].header['CUNIT2']]
|
2019-05-12 08:11:15 -06:00
|
|
|
self.crres = np.array(
|
|
|
|
[hdu[0].header['CRRES1'], hdu[0].header['CRRES2']])
|
2018-03-09 13:08:47 -07:00
|
|
|
|
2019-04-22 08:10:40 -06:00
|
|
|
hdu.close()
|
2019-05-12 08:11:15 -06:00
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Compute image properties
|
2019-05-12 08:11:15 -06:00
|
|
|
self.sx = np.sqrt(self.cd[0, 0]**2 + self.cd[1, 0]**2)
|
|
|
|
self.sy = np.sqrt(self.cd[0, 1]**2 + self.cd[1, 1]**2)
|
|
|
|
self.wx = self.nx * self.sx
|
|
|
|
self.wy = self.ny * self.sy
|
|
|
|
self.zmaxmin = np.mean(self.zmax) - 2.0 * np.std(self.zmax)
|
|
|
|
self.zmaxmax = np.mean(self.zmax) + 6.0 * np.std(self.zmax)
|
|
|
|
self.zavgmin = np.mean(self.zavg) - 2.0 * np.std(self.zavg)
|
|
|
|
self.zavgmax = np.mean(self.zavg) + 6.0 * np.std(self.zavg)
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Setup WCS
|
2018-04-21 03:12:39 -06:00
|
|
|
self.w = wcs.WCS(naxis=2)
|
|
|
|
self.w.wcs.crpix = self.crpix
|
|
|
|
self.w.wcs.crval = self.crval
|
|
|
|
self.w.wcs.cd = self.cd
|
|
|
|
self.w.wcs.ctype = self.ctype
|
|
|
|
self.w.wcs.set_pv([(2, 1, 45.0)])
|
|
|
|
|
2018-07-22 01:36:20 -06:00
|
|
|
def mask(self, xmin, xmax, ymin, ymax):
|
|
|
|
x, y = np.meshgrid(np.arange(self.nx), np.arange(self.ny))
|
|
|
|
c = (x >= xmin) & (x <= self.nx-xmax)\
|
|
|
|
& (y >= ymin)\
|
|
|
|
& (y <= self.ny-ymax)
|
|
|
|
self.mask = np.ones_like(self.zavg)
|
|
|
|
self.mask[~c] = 0.0
|
|
|
|
self.zavg *= self.mask
|
|
|
|
self.zstd *= self.mask
|
|
|
|
self.zmax *= self.mask
|
|
|
|
self.znum *= self.mask
|
|
|
|
self.zsig *= self.mask
|
|
|
|
|
|
|
|
def selection_mask(self, sigma, zstd):
|
2018-05-07 14:19:05 -06:00
|
|
|
"""Create a selection mask"""
|
2018-07-22 01:36:20 -06:00
|
|
|
c1 = ndimage.uniform_filter(self.znum, 3, mode='constant')
|
2019-05-12 08:11:15 -06:00
|
|
|
c2 = ndimage.uniform_filter(self.znum * self.znum, 3, mode='constant')
|
2019-04-22 09:16:30 -06:00
|
|
|
|
|
|
|
# Add epsilon to keep square root positive
|
2019-05-12 08:11:15 -06:00
|
|
|
z = np.sqrt(c2 - c1 * c1 + 1e-9)
|
2018-05-07 14:19:05 -06:00
|
|
|
|
|
|
|
# Standard deviation mask
|
2018-07-22 01:36:20 -06:00
|
|
|
c = z < zstd
|
|
|
|
m1 = np.zeros_like(self.zavg)
|
|
|
|
m1[c] = 1.0
|
2018-05-07 14:19:05 -06:00
|
|
|
|
|
|
|
# Sigma mask
|
2018-07-22 01:36:20 -06:00
|
|
|
c = self.zsig < sigma
|
|
|
|
m2 = np.zeros_like(self.zavg)
|
|
|
|
m2[~c] = 1.0
|
2019-05-12 08:11:15 -06:00
|
|
|
self.zsel = m1 * m2
|
2018-05-07 14:19:05 -06:00
|
|
|
|
|
|
|
# Generate points
|
2018-07-22 01:36:20 -06:00
|
|
|
c = self.zsel == 1.0
|
|
|
|
xm, ym = np.meshgrid(np.arange(self.nx), np.arange(self.ny))
|
|
|
|
x, y = np.ravel(xm[c]), np.ravel(ym[c])
|
|
|
|
inum = np.ravel(self.znum[c]).astype('int')
|
|
|
|
sig = np.ravel(self.zsig[c])
|
|
|
|
t = np.array([self.dt[i] for i in inum])
|
2018-05-07 14:19:05 -06:00
|
|
|
|
2018-07-22 01:36:20 -06:00
|
|
|
return x, y, inum, t, sig
|
2018-05-07 14:19:05 -06:00
|
|
|
|
2019-05-12 08:11:15 -06:00
|
|
|
def significant_pixels_along_track(self,
|
|
|
|
sigma,
|
|
|
|
x0,
|
|
|
|
y0,
|
|
|
|
dxdt,
|
|
|
|
dydt,
|
|
|
|
rmin=10.0):
|
2018-03-23 09:36:31 -06:00
|
|
|
"""Extract significant pixels along a track"""
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Generate sigma frame
|
2019-05-12 08:11:15 -06:00
|
|
|
zsig = (self.zmax - self.zavg) / (self.zstd + 1e-9)
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Select
|
2018-04-21 03:12:39 -06:00
|
|
|
c = (zsig > sigma)
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Positions
|
2018-04-21 03:12:39 -06:00
|
|
|
xm, ym = np.meshgrid(np.arange(self.nx), np.arange(self.ny))
|
|
|
|
x, y = np.ravel(xm[c]), np.ravel(ym[c])
|
|
|
|
inum = np.ravel(self.znum[c]).astype('int')
|
|
|
|
sig = np.ravel(zsig[c])
|
|
|
|
t = np.array([self.dt[i] for i in inum])
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Predicted positions
|
2019-05-12 08:11:15 -06:00
|
|
|
xr = x0 + dxdt * t
|
|
|
|
yr = y0 + dydt * t
|
|
|
|
r = np.sqrt((x - xr)**2 + (y - yr)**2)
|
2018-04-21 03:12:39 -06:00
|
|
|
c = r < rmin
|
2018-03-09 13:08:47 -07:00
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
return x[c], y[c], t[c], sig[c]
|
2018-03-09 13:08:47 -07:00
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
def significant_pixels(self, sigma):
|
2018-03-23 09:36:31 -06:00
|
|
|
"""Extract significant pixels"""
|
|
|
|
|
|
|
|
# Generate sigma frame
|
2019-05-12 08:11:15 -06:00
|
|
|
zsig = (self.zmax - self.zavg) / (self.zstd + 1e-9)
|
2018-03-23 09:36:31 -06:00
|
|
|
|
|
|
|
# Select
|
2018-04-21 03:12:39 -06:00
|
|
|
c = (zsig > sigma)
|
2018-03-23 09:36:31 -06:00
|
|
|
|
|
|
|
# Positions
|
2018-04-21 03:12:39 -06:00
|
|
|
xm, ym = np.meshgrid(np.arange(self.nx), np.arange(self.ny))
|
|
|
|
x, y = np.ravel(xm[c]), np.ravel(ym[c])
|
|
|
|
inum = np.ravel(self.znum[c]).astype('int')
|
|
|
|
sig = np.ravel(zsig[c])
|
|
|
|
t = np.array([self.dt[i] for i in inum])
|
2018-03-23 09:36:31 -06:00
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
return x, y, t, sig
|
2018-03-23 09:36:31 -06:00
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
def track(self, dxdt, dydt, tref):
|
2018-03-09 13:08:47 -07:00
|
|
|
"""Track and stack"""
|
|
|
|
# Empty frame
|
2018-04-21 03:12:39 -06:00
|
|
|
ztrk = np.zeros_like(self.zavg)
|
|
|
|
|
2018-03-09 13:08:47 -07:00
|
|
|
# Loop over frames
|
|
|
|
for i in range(self.nz):
|
2019-05-12 08:11:15 -06:00
|
|
|
dx = int(np.round(dxdt * (self.dt[i] - tref)))
|
|
|
|
dy = int(np.round(dydt * (self.dt[i] - tref)))
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
# Skip if shift larger than image
|
2018-04-21 03:12:39 -06:00
|
|
|
if np.abs(dx) >= self.nx:
|
2018-03-09 13:08:47 -07:00
|
|
|
continue
|
2018-04-21 03:12:39 -06:00
|
|
|
if np.abs(dy) >= self.ny:
|
2018-03-09 13:08:47 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Extract range
|
2018-04-21 03:12:39 -06:00
|
|
|
if dx >= 0:
|
2019-05-12 08:11:15 -06:00
|
|
|
i1min, i1max = dx, self.nx - 1
|
|
|
|
i2min, i2max = 0, self.nx - dx - 1
|
2018-03-09 13:08:47 -07:00
|
|
|
else:
|
2019-05-12 08:11:15 -06:00
|
|
|
i1min, i1max = 0, self.nx + dx - 1
|
|
|
|
i2min, i2max = -dx, self.nx - 1
|
2018-04-21 03:12:39 -06:00
|
|
|
if dy >= 0:
|
2019-05-12 08:11:15 -06:00
|
|
|
j1min, j1max = dy, self.ny - 1
|
|
|
|
j2min, j2max = 0, self.ny - dy - 1
|
2018-03-09 13:08:47 -07:00
|
|
|
else:
|
2019-05-12 08:11:15 -06:00
|
|
|
j1min, j1max = 0, self.ny + dy - 1
|
|
|
|
j2min, j2max = -dy, self.ny - 1
|
2018-04-21 03:12:39 -06:00
|
|
|
zsel = np.where(self.znum == i, self.zmax, 0.0)
|
|
|
|
ztrk[j2min:j2max, i2min:i2max] += zsel[j1min:j1max, i1min:i1max]
|
2018-03-09 13:08:47 -07:00
|
|
|
|
|
|
|
return ztrk
|
|
|
|
|
2018-04-21 03:12:39 -06:00
|
|
|
def __repr__(self):
|
2019-05-12 08:11:15 -06:00
|
|
|
return "%s %dx%dx%d %s %.3f %d %s" % (self.fname, self.nx, self.ny,
|
|
|
|
self.nz, self.nfd, self.texp,
|
|
|
|
self.site_id, self.observer)
|