Example #1
0
def plot_header(fname, ff, iod_line):
    # ppgplot arrays
    heat_l = np.array([0.0, 0.2, 0.4, 0.6, 1.0])
    heat_r = np.array([0.0, 0.5, 1.0, 1.0, 1.0])
    heat_g = np.array([0.0, 0.0, 0.5, 1.0, 1.0])
    heat_b = np.array([0.0, 0.0, 0.0, 0.3, 1.0])

    # Plot
    ppg.pgopen(fname)
    ppg.pgpap(0.0, 1.0)
    ppg.pgsvp(0.1, 0.95, 0.1, 0.8)

    ppg.pgsch(0.8)
    ppg.pgmtxt("T", 6.0, 0.0, 0.0,
               "UT Date: %.23s  COSPAR ID: %04d" % (ff.nfd, ff.site_id))
    if is_calibrated(ff):
        ppg.pgsci(1)
    else:
        ppg.pgsci(2)
    ppg.pgmtxt(
        "T", 4.8, 0.0, 0.0, "R.A.: %10.5f (%4.1f'') Decl.: %10.5f (%4.1f'')" %
        (ff.crval[0], 3600.0 * ff.crres[0], ff.crval[1], 3600.0 * ff.crres[1]))
    ppg.pgsci(1)
    ppg.pgmtxt("T", 3.6, 0.0, 0.0, ("FoV: %.2f\\(2218)x%.2f\\(2218) "
                                    "Scale: %.2f''x%.2f'' pix\\u-1\\d") %
               (ff.wx, ff.wy, 3600.0 * ff.sx, 3600.0 * ff.sy))
    ppg.pgmtxt(
        "T", 2.4, 0.0, 0.0, "Stat: %5.1f+-%.1f (%.1f-%.1f)" %
        (np.mean(ff.zmax), np.std(ff.zmax), ff.zmaxmin, ff.zmaxmax))
    ppg.pgmtxt("T", 0.3, 0.0, 0.0, iod_line)

    ppg.pgsch(1.0)
    ppg.pgwnad(0.0, ff.nx, 0.0, ff.ny)
    ppg.pglab("x (pix)", "y (pix)", " ")
    ppg.pgctab(heat_l, heat_r, heat_g, heat_b, 5, 1.0, 0.5)
Example #2
0
    def startPlotter(self):
        if self.plotDeviceIsOpened:
            raise ValueError("You already started a plot!")

        devId = pgplot.pgopen(self.deviceName)
        self.plotDeviceIsOpened = True

        if not self.widthInches is None:
            pgplot.pgpap(self.widthInches, self.yOnXRatio)

        # For devices /xs, /xw, /png etc, should make the paper white and the ink black. Only for /ps does pgplot default to that.
        #
        deviceWithoutFile = self.deviceName.split('/')[-1]
        if deviceWithoutFile == 'xs' or deviceWithoutFile == 'xw' or deviceWithoutFile == 'png':
            pgplot.pgscr(0, 1.0, 1.0, 1.0)
            pgplot.pgscr(1, 0.0, 0.0, 0.0)

        pgplot.pgsvp(self._vXLo, self._vXHi, self._vYLo, self._vYHi)

        if self.fixAspect:
            pgplot.pgwnad(self.worldXLo, self.worldXHi, self.worldYLo,
                          self.worldYHi)
        else:
            pgplot.pgswin(self.worldXLo, self.worldXHi, self.worldYLo,
                          self.worldYHi)
        pgplot.pgsfs(2)

        pgplot.pgslw(1)
        pgplot.pgsch(self._charHeight)

        self._setColourRepresentations()

        # Set up things so calling pgplot.pggray() won't overwrite the CR of any of the colours in self.colours.
        #
        (minCI, maxCI) = pgplot.pgqcir()
        if minCI <= self.maxCI:
            pgplot.pgscir(self.maxCI + 1, maxCI)

        (xLoPixels, xHiPixels, yLoPixels, yHiPixels) = pgplot.pgqvsz(3)
        (xLoInches, xHiInches, yLoInches, yHiInches) = pgplot.pgqvsz(1)
        self.xPixelWorld = (xHiInches - xLoInches) / (xHiPixels - xLoPixels)
        self.yPixelWorld = (yHiInches - yLoInches) / (yHiPixels - yLoPixels)
Example #3
0
    ppgplot.pgslw(2)
    ppgplot.pgpap(0.0, 0.75)

    # For ever loop
    redraw = True
    while True:
        # Redraw
        if redraw == True:
            # Update
            m.update()

            # Initialize window
            ppgplot.pgscr(0, 0., 0., 0.)
            ppgplot.pgeras()
            ppgplot.pgsvp(0.01, 0.99, 0.01, 0.99)
            ppgplot.pgwnad(-1.5 * m.w, 1.5 * m.w, -m.w, m.w)

            # Set background depending on solar altitude
            if m.sunalt > 0.0:
                ppgplot.pgscr(0, 0.0, 0.0, 0.4)
            elif m.sunalt > -6.0:
                ppgplot.pgscr(0, 0.0, 0.0, 0.3)
            elif m.sunalt > -12.0:
                ppgplot.pgscr(0, 0.0, 0.0, 0.2)
            elif m.sunalt > -18.0:
                ppgplot.pgscr(0, 0.0, 0.0, 0.1)
            else:
                ppgplot.pgscr(0, 0.0, 0.0, 0.0)
            ppgplot.pgsci(0)

            # Plot box
Example #4
0
    def plot(self, vlo=2., vhi=98., nc=-1, method='p', mpl=False, cmap=CMDEF, \
                 close=True, x1=None, x2=None, y1=None, y2=None, sepmin=1.):
        """
        Plots an MCCD using pgplot or matplotlib if preferred.

        :Parameters:
         vlo : float
            number specifying the lowest level to plot (default as a percentile)
         vhi : float
            number specifying the lowest level to plot (default as a percentile)
         nc : int
            CCD number (starting from 0, -1 for all)
         method : string
            how vlo and vhi are to be interpreted. 'p' = percentile, 'a' = automatic (min to max,
            vlo and vhi are irrelevant), 'd' = direct, i.e. just take the values given.
         mpl : bool
            True to prefer matplotlib over pgplot (which may not even be an option)
         cmap : matplotlib.cm.binary
            colour map if using matplotlib
         close : bool
            close (pgplot) or 'show' (matplotlib) the plot at the end (or not, to allow 
            you to plot something else, use a cursor etc). In the case of pgplot, this also
            implies opening the plot at the start, i.e. a self-contained quick plot.
         x1 : float
            left-hand plot limit. Defaults to 0.5
         x2 : float
             right-hand plot limit. Defaults to nxmax+0.5
         y1 : float
            lower plot limit. Defaults to 0.5
         y2 : float
             upper plot limit. Defaults to nymax+0.5
         sepmin : float
             minimum separation between intensity limits (> 0 to stop PGPLOT complaining)

        :Returns:
         range(s) : tuple or list
            the plot range(s) used either as a single 2-element tuple, or
            a list of them, one per CCD plotted.
        """

        if nc == -1:
            nc1 = 0
            nc2 = len(self)
        else:
            nc1 = nc
            nc2 = nc+1
        
        if not mpl:
            if close: pg.pgopen('/xs')
            if nc2-nc1 > 1: pg.pgsubp(nc2-nc1,1)

        prange = []
        for nc, ccd in enumerate(self._data[nc1:nc2]):

            # Determine intensity range to display
            if method == 'p':
                vmin, vmax = ccd.centile((vlo,vhi))
            elif method == 'a':
                vmin, vmax = ccd.min(), ccd.max()
            elif method == 'd':
                vmin, vmax = vlo, vhi
            else:
                raise UltracamError('MCCD.plot: method must be one of p, a or d.')

            if vmin == vmax:
                vmin -= sepmin/2.
                vmax += sepmin/2.
            prange.append((vmin, vmax))

            # start
            nxmax, nymax = ccd.nxmax, ccd.nymax
            x1    = 0.5 if x1 is None else x1
            x2    = nxmax+0.5 if x2 is None else x2
            y1    = 0.5 if y1 is None else y1
            y2    = nymax+0.5 if y2 is None else y2

            if mpl:
                if nc2-nc1 > 1:
                    plt.subplot(1,nc2-nc1,nc+1)
                plt.axis('equal')
            else:
                if nc2-nc1 > 1: pg.pgpanl(nc-nc1+1,1)
                pg.pgwnad(x1,x2,y1,y2)

            # plot CCD
            ccd.plot(vmin,vmax,mpl,cmap)

            # per-frame finishing-off
            if mpl:
                plt.xlim(x1,x2)
                plt.ylim(y1,y2)
            else:
                pg.pgbox('bcnst',0,0,'bcnst',0,0)
                pg.pglab('X','Y','')

        if close:
            if mpl:
                plt.show()
            else:
                pg.pgclos()

        # return intensity range(s) used
        if len(prange) == 1:
            return prange[0]
        else:
            return tuple(prange)
Example #5
0
    def plot(self, vlo=2., vhi=98., nc=-1, method='p', mpl=False, cmap=CMDEF, \
                 close=True, x1=None, x2=None, y1=None, y2=None, sepmin=1.):
        """
        Plots an MCCD using pgplot or matplotlib if preferred.

        :Parameters:
         vlo : float
            number specifying the lowest level to plot (default as a percentile)
         vhi : float
            number specifying the lowest level to plot (default as a percentile)
         nc : int
            CCD number (starting from 0, -1 for all)
         method : string
            how vlo and vhi are to be interpreted. 'p' = percentile, 'a' = automatic (min to max,
            vlo and vhi are irrelevant), 'd' = direct, i.e. just take the values given.
         mpl : bool
            True to prefer matplotlib over pgplot (which may not even be an option)
         cmap : matplotlib.cm.binary
            colour map if using matplotlib
         close : bool
            close (pgplot) or 'show' (matplotlib) the plot at the end (or not, to allow 
            you to plot something else, use a cursor etc). In the case of pgplot, this also
            implies opening the plot at the start, i.e. a self-contained quick plot.
         x1 : float
            left-hand plot limit. Defaults to 0.5
         x2 : float
             right-hand plot limit. Defaults to nxmax+0.5
         y1 : float
            lower plot limit. Defaults to 0.5
         y2 : float
             upper plot limit. Defaults to nymax+0.5
         sepmin : float
             minimum separation between intensity limits (> 0 to stop PGPLOT complaining)

        :Returns:
         range(s) : tuple or list
            the plot range(s) used either as a single 2-element tuple, or
            a list of them, one per CCD plotted.
        """

        if nc == -1:
            nc1 = 0
            nc2 = len(self)
        else:
            nc1 = nc
            nc2 = nc + 1

        if not mpl:
            if close: pg.pgopen('/xs')
            if nc2 - nc1 > 1: pg.pgsubp(nc2 - nc1, 1)

        prange = []
        for nc, ccd in enumerate(self._data[nc1:nc2]):

            # Determine intensity range to display
            if method == 'p':
                vmin, vmax = ccd.centile((vlo, vhi))
            elif method == 'a':
                vmin, vmax = ccd.min(), ccd.max()
            elif method == 'd':
                vmin, vmax = vlo, vhi
            else:
                raise UltracamError(
                    'MCCD.plot: method must be one of p, a or d.')

            if vmin == vmax:
                vmin -= sepmin / 2.
                vmax += sepmin / 2.
            prange.append((vmin, vmax))

            # start
            nxmax, nymax = ccd.nxmax, ccd.nymax
            x1 = 0.5 if x1 is None else x1
            x2 = nxmax + 0.5 if x2 is None else x2
            y1 = 0.5 if y1 is None else y1
            y2 = nymax + 0.5 if y2 is None else y2

            if mpl:
                if nc2 - nc1 > 1:
                    plt.subplot(1, nc2 - nc1, nc + 1)
                plt.axis('equal')
            else:
                if nc2 - nc1 > 1: pg.pgpanl(nc - nc1 + 1, 1)
                pg.pgwnad(x1, x2, y1, y2)

            # plot CCD
            ccd.plot(vmin, vmax, mpl, cmap)

            # per-frame finishing-off
            if mpl:
                plt.xlim(x1, x2)
                plt.ylim(y1, y2)
            else:
                pg.pgbox('bcnst', 0, 0, 'bcnst', 0, 0)
                pg.pglab('X', 'Y', '')

        if close:
            if mpl:
                plt.show()
            else:
                pg.pgclos()

        # return intensity range(s) used
        if len(prange) == 1:
            return prange[0]
        else:
            return tuple(prange)
Example #6
0
def extract_tracks(fname, trkrmin, drdtmin, trksig, ntrkmin):
    # Read four frame
    ff = fourframe(fname)

    # Skip saturated frames
    if np.sum(ff.zavg > 240.0) / float(ff.nx * ff.ny) > 0.95:
        return

    # Read satelite IDs
    try:
        f = open(fname + ".id")
    except OSError:
        print("Cannot open", fname + ".id")
    else:
        lines = f.readlines()
        f.close()

    # ppgplot arrays
    tr = np.array([-0.5, 1.0, 0.0, -0.5, 0.0, 1.0])
    heat_l = np.array([0.0, 0.2, 0.4, 0.6, 1.0])
    heat_r = np.array([0.0, 0.5, 1.0, 1.0, 1.0])
    heat_g = np.array([0.0, 0.0, 0.5, 1.0, 1.0])
    heat_b = np.array([0.0, 0.0, 0.0, 0.3, 1.0])

    # Loop over identifications
    for line in lines:
        # Decode
        id = satid(line)

        # Skip slow moving objects
        drdt = np.sqrt(id.dxdt**2 + id.dydt**2)
        if drdt < drdtmin:
            continue

        # Extract significant pixels
        x, y, t, sig = ff.significant(trksig, id.x0, id.y0, id.dxdt, id.dydt,
                                      trkrmin)

        # Fit tracks
        if len(t) > ntrkmin:
            # Get times
            tmin = np.min(t)
            tmax = np.max(t)
            tmid = 0.5 * (tmax + tmin)
            mjd = ff.mjd + tmid / 86400.0

            # Skip if no variance in time
            if np.std(t - tmid) == 0.0:
                continue

            # Very simple polynomial fit; no weighting, no cleaning
            px = np.polyfit(t - tmid, x, 1)
            py = np.polyfit(t - tmid, y, 1)

            # Extract results
            x0, y0 = px[1], py[1]
            dxdt, dydt = px[0], py[0]
            xmin = x0 + dxdt * (tmin - tmid)
            ymin = y0 + dydt * (tmin - tmid)
            xmax = x0 + dxdt * (tmax - tmid)
            ymax = y0 + dydt * (tmax - tmid)

            cospar = get_cospar(id.norad)
            obs = observation(ff, mjd, x0, y0)
            iod_line = "%s" % format_iod_line(id.norad, cospar, ff.site_id,
                                              obs.nfd, obs.ra, obs.de)

            print(iod_line)

            if id.catalog.find("classfd.tle") > 0:
                outfname = "classfd.dat"
            elif id.catalog.find("inttles.tle") > 0:
                outfname = "inttles.dat"
            else:
                outfname = "catalog.dat"

            f = open(outfname, "a")
            f.write("%s\n" % iod_line)
            f.close()

            # Plot
            ppgplot.pgopen(
                fname.replace(".fits", "") + "_%05d.png/png" % id.norad)
            #ppgplot.pgopen("/xs")
            ppgplot.pgpap(0.0, 1.0)
            ppgplot.pgsvp(0.1, 0.95, 0.1, 0.8)

            ppgplot.pgsch(0.8)
            ppgplot.pgmtxt(
                "T", 6.0, 0.0, 0.0,
                "UT Date: %.23s  COSPAR ID: %04d" % (ff.nfd, ff.site_id))
            if (3600.0 * ff.crres[0] < 1e-3
                ) | (3600.0 * ff.crres[1] < 1e-3) | (
                    ff.crres[0] / ff.sx > 2.0) | (ff.crres[1] / ff.sy > 2.0):
                ppgplot.pgsci(2)
            else:
                ppgplot.pgsci(1)
            ppgplot.pgmtxt(
                "T", 4.8, 0.0, 0.0,
                "R.A.: %10.5f (%4.1f'') Decl.: %10.5f (%4.1f'')" %
                (ff.crval[0], 3600.0 * ff.crres[0], ff.crval[1],
                 3600.0 * ff.crres[1]))
            ppgplot.pgsci(1)
            ppgplot.pgmtxt(
                "T", 3.6, 0.0, 0.0,
                "FoV: %.2f\\(2218)x%.2f\\(2218) Scale: %.2f''x%.2f'' pix\\u-1\\d"
                % (ff.wx, ff.wy, 3600.0 * ff.sx, 3600.0 * ff.sy))
            ppgplot.pgmtxt(
                "T", 2.4, 0.0, 0.0, "Stat: %5.1f+-%.1f (%.1f-%.1f)" %
                (np.mean(ff.zmax), np.std(ff.zmax), ff.vmin, ff.vmax))
            ppgplot.pgmtxt("T", 0.3, 0.0, 0.0, iod_line)

            ppgplot.pgsch(1.0)
            ppgplot.pgwnad(0.0, ff.nx, 0.0, ff.ny)
            ppgplot.pglab("x (pix)", "y (pix)", " ")
            ppgplot.pgctab(heat_l, heat_r, heat_g, heat_b, 5, 1.0, 0.5)

            ppgplot.pgimag(ff.zmax, ff.nx, ff.ny, 0, ff.nx - 1, 0, ff.ny - 1,
                           ff.vmax, ff.vmin, tr)
            ppgplot.pgbox("BCTSNI", 0., 0, "BCTSNI", 0., 0)
            ppgplot.pgstbg(1)

            ppgplot.pgsci(0)
            if id.catalog.find("classfd.tle") > 0:
                ppgplot.pgsci(4)
            elif id.catalog.find("inttles.tle") > 0:
                ppgplot.pgsci(3)
            ppgplot.pgpt(np.array([x0]), np.array([y0]), 4)
            ppgplot.pgmove(xmin, ymin)
            ppgplot.pgdraw(xmax, ymax)
            ppgplot.pgsch(0.65)
            ppgplot.pgtext(np.array([x0]), np.array([y0]), " %05d" % id.norad)
            ppgplot.pgsch(1.0)
            ppgplot.pgsci(1)

            ppgplot.pgend()

        elif id.catalog.find("classfd.tle") > 0:
            # Track and stack
            t = np.linspace(0.0, ff.texp)
            x, y = id.x0 + id.dxdt * t, id.y0 + id.dydt * t
            c = (x > 0) & (x < ff.nx) & (y > 0) & (y < ff.ny)

            # Skip if no points selected
            if np.sum(c) == 0:
                continue

            # Compute track
            tmid = np.mean(t[c])
            mjd = ff.mjd + tmid / 86400.0
            xmid = id.x0 + id.dxdt * tmid
            ymid = id.y0 + id.dydt * tmid
            ztrk = ndimage.gaussian_filter(ff.track(id.dxdt, id.dydt, tmid),
                                           1.0)
            vmin = np.mean(ztrk) - 2.0 * np.std(ztrk)
            vmax = np.mean(ztrk) + 6.0 * np.std(ztrk)

            # Select region
            xmin = int(xmid - 100)
            xmax = int(xmid + 100)
            ymin = int(ymid - 100)
            ymax = int(ymid + 100)
            if xmin < 0: xmin = 0
            if ymin < 0: ymin = 0
            if xmax > ff.nx: xmax = ff.nx - 1
            if ymax > ff.ny: ymax = ff.ny - 1

            # Find peak
            x0, y0, w, sigma = peakfind(ztrk[ymin:ymax, xmin:xmax])
            x0 += xmin
            y0 += ymin

            # Skip if peak is not significant
            if sigma < trksig:
                continue

            # Skip if point is outside selection area
            if inside_selection(id, xmid, ymid, x0, y0) == False:
                continue

            # Format IOD line
            cospar = get_cospar(id.norad)
            obs = observation(ff, mjd, x0, y0)
            iod_line = "%s" % format_iod_line(id.norad, cospar, ff.site_id,
                                              obs.nfd, obs.ra, obs.de)

            print(iod_line)

            if id.catalog.find("classfd.tle") > 0:
                outfname = "classfd.dat"
            elif id.catalog.find("inttles.tle") > 0:
                outfname = "inttles.dat"
            else:
                outfname = "catalog.dat"

            f = open(outfname, "a")
            f.write("%s\n" % iod_line)
            f.close()

            # Plot
            ppgplot.pgopen(
                fname.replace(".fits", "") + "_%05d.png/png" % id.norad)
            ppgplot.pgpap(0.0, 1.0)
            ppgplot.pgsvp(0.1, 0.95, 0.1, 0.8)

            ppgplot.pgsch(0.8)
            ppgplot.pgmtxt(
                "T", 6.0, 0.0, 0.0,
                "UT Date: %.23s  COSPAR ID: %04d" % (ff.nfd, ff.site_id))
            ppgplot.pgmtxt(
                "T", 4.8, 0.0, 0.0,
                "R.A.: %10.5f (%4.1f'') Decl.: %10.5f (%4.1f'')" %
                (ff.crval[0], 3600.0 * ff.crres[0], ff.crval[1],
                 3600.0 * ff.crres[1]))
            ppgplot.pgmtxt(
                "T", 3.6, 0.0, 0.0,
                "FoV: %.2f\\(2218)x%.2f\\(2218) Scale: %.2f''x%.2f'' pix\\u-1\\d"
                % (ff.wx, ff.wy, 3600.0 * ff.sx, 3600.0 * ff.sy))
            ppgplot.pgmtxt(
                "T", 2.4, 0.0, 0.0, "Stat: %5.1f+-%.1f (%.1f-%.1f)" %
                (np.mean(ff.zmax), np.std(ff.zmax), ff.vmin, ff.vmax))
            ppgplot.pgmtxt("T", 0.3, 0.0, 0.0, iod_line)

            ppgplot.pgsch(1.0)
            ppgplot.pgwnad(0.0, ff.nx, 0.0, ff.ny)
            ppgplot.pglab("x (pix)", "y (pix)", " ")
            ppgplot.pgctab(heat_l, heat_r, heat_g, heat_b, 5, 1.0, 0.5)

            ppgplot.pgimag(ztrk, ff.nx, ff.ny, 0, ff.nx - 1, 0, ff.ny - 1,
                           vmax, vmin, tr)
            ppgplot.pgbox("BCTSNI", 0., 0, "BCTSNI", 0., 0)
            ppgplot.pgstbg(1)

            plot_selection(id, xmid, ymid)

            ppgplot.pgsci(0)
            if id.catalog.find("classfd.tle") > 0:
                ppgplot.pgsci(4)
            elif id.catalog.find("inttles.tle") > 0:
                ppgplot.pgsci(3)
            ppgplot.pgpt(np.array([id.x0]), np.array([id.y0]), 17)
            ppgplot.pgmove(id.x0, id.y0)
            ppgplot.pgdraw(id.x1, id.y1)
            ppgplot.pgpt(np.array([x0]), np.array([y0]), 4)
            ppgplot.pgsch(0.65)
            ppgplot.pgtext(np.array([id.x0]), np.array([id.y0]),
                           " %05d" % id.norad)
            ppgplot.pgsch(1.0)
            ppgplot.pgsci(1)

            ppgplot.pgend()