Example #1
0
def isOpen():

    targets = ds9.ds9_targets()
    if targets is None:
        return False

    if type(targets) in (list, ):
        for target in targets:
            if _target in target:
                return True

    return False
Example #2
0
def isOpen():

    targets = ds9.ds9_targets()
    if targets is None:
        return False

    if type(targets) in (list,):
        for target in targets:
            if _target in target:
                return True

    return False
Example #3
0
def ds9display(filename):
    targs = ds9.ds9_targets()
    if targs is None:
        # Open a new ds9 window
        d = ds9.ds9(start=True)
    else:
        # Default grab the first ds9 instance
        d = ds9.ds9(targs[0])
    d.set('file ' + filename)
    d.set('zoom to fit')
    d.set('zscale')
    d.set("zscale contrast 0.1")
Example #4
0
def ds9display(filename):
    targs = ds9.ds9_targets()
    if targs is None:
        # Open a new ds9 window
        d = ds9.ds9(start=True)
    else:
        # Default grab the first ds9 instance
        d = ds9.ds9(targs[0])
    d.set('file ' + filename)
    d.set('zoom to fit')
    d.set('zscale')
    d.set("zscale contrast 0.1")
Example #5
0
def pydisplay(datalist,
              regions=None,
              loadregions=0,
              target=None,
              scale='zscale'):
    if target == None:
        if ds9.ds9_targets() != None:
            target = ds9.ds9_targets()[0].split()[1]
            d = ds9.ds9(target=target)
        else:
            d = ds9.ds9(target='DS9:pyds9')

    else:
        d = ds9.ds9(target=target, wait=15)

    #d = ds9.ds9()  #target='pyds9',start=True)
    #try:
    #except:
    #    d = ds9.ds9('pyds9',wait=20)

    # If filelist is just a single file string
    if isinstance(datalist, str):
        d.set("file %s" % datalist)
        d.set(scale)
        d.set("zoom to fit")
        if regions is not None:
            d.set("regions load %s" % regions)
        return target

    # If datalist is a single array
    if isinstance(datalist, np.ndarray):
        d.set("frame 1")
        d.set_np2arr(datalist, dtype=float)
        d.set(scale)
        d.set("zoom to fit")
        if regions is not None:
            d.set("regions load %s" % regions)
        return target

    # If datalist is a single HDU
    '''
    if isinstance(datalist,pyfits.HDUList):
        d.set("frame 1")
        d.set_pyfits(datalist)
        d.set("zscale")
        d.set("zoom to fit")
        if regions is not None:
            d.set("regions load %s" % regions)
        return target
    '''

    for idx, f in enumerate(datalist):
        d.set("frame %i" % (idx + 1))
        if isinstance(f, str):
            d.set("file %s" % f)
    # elif isinstance(f,pyfits.HDUList):
    #     d.set_pyfits(f)
        elif isinstance(f, np.ndarray):
            d.set_np2arr(f, dtype=float)
        else:
            try:
                d.set_np2arr(f.data, dtype=float)
            except AttributeError:
                d.set_np2arr(f[0].data, dtype=float)
        if regions is not None:
            if loadregions == 0 or loadregions - 1 == idx:
                d.set("regions load %s" % regions)
        d.set(scale)
        d.set("zoom to fit")

    d.set("match colorbar")
    d.set("match scale")
    d.set("frame 1")

    return d
Example #6
0
 def targets(self):
     """
     Returns a list of the ids of the running ds9 instances.
     """
     import ds9
     return ds9.ds9_targets()
Example #7
0
 def targets(self):
     """
     Returns a list of the ids of the running ds9 instances.
     """
     import ds9
     return ds9.ds9_targets()
Example #8
0
    def ds9(self, xpamsg=None, origin=None, new=True, **keywords):
        """
        Display the array using ds9.

        The ds9 process will be given an random id. By default, the
        following access point are set before the array is loaded:
            -cmap heat
            -scale scope local
            -scale mode 99.5
        Other access points can be set before the data is loaded though
        the keywords (see examples below).
        After the array is loaded, the map's header is set and the user
        may add other XPA messages through the xpamsg argument or by
        setting them through the returned ds9 instance.

        Parameters
        ----------
        xpamsg : string or tuple of string
            XPA access point message to be set after the array is loaded.
            (see http://hea-www.harvard.edu/RD/ds9/ref/xpa.html).
        origin: string
            Set origin to 'upper' for Y increasing downwards
        new: boolean
            If true, open the array in a new ds9 instance.
        **keywords : string or tuple of string
            Specify more access points to be set before array loading.
            a keyword such as 'height=400' will be appended to the command
            that launches ds9 in the form 'ds9 [...] -height 400'

        Returns
        -------
        The returned object is a ds9 instance. It can be manipulated using
        XPA access points.

        Examples
        --------
        >>> m = Map('myfits.fits')
        >>> d=m.ds9(('zoom to fit','saveimage png myfits.png'),scale='histequ', 
                    cmap='invert yes', height=400)
        >>> d.set('exit')
        """
        if not _imported_ds9:
            raise RuntimeError('The library pyds9 has not been installed.')
        import ds9, os, time, sys, uuid, xpa

        id = None
        if not new:
            list = ds9.ds9_targets()
            if list is not None:
                id = list[-1]

        if id is None:
            if 'cmap' not in keywords:
                keywords['cmap'] = 'heat'

            if 'scale' not in keywords:
                keywords['scale'] = ('scope local', 'mode 99.5')

            if origin == 'upper' or \
               'orient' not in keywords and self.origin == 'upper':
                keywords['orient'] = 'y'

            wait = 10

            id = 'ds9_' + str(uuid.uuid1())[4:8]

            command = 'ds9 -title ' + id

            for k,v in keywords.items():
                k = str(k)
                if type(v) is not tuple:
                    v = (v,)
                command += reduce(lambda x,y: \
                                  str(x) + ' -' + k + ' ' + str(y),v,'')

            os.system(command + ' &')

            # start the xpans name server
            if xpa.xpaaccess("xpans", None, 1) == None:
                _cmd = None
                # look in install directories
                for _dir in sys.path:
                    _fname = os.path.join(_dir, "xpans")
                    if os.path.exists(_fname):
                        _cmd = _fname + " -e &"
                if _cmd:
                    os.system(_cmd)

            for i in range(wait):
                list = xpa.xpaaccess(id, None, 1024)
                if list: break
                time.sleep(1)
            if not list:
                raise ValueError('No active ds9 running for target: %s' % list)

        # get ds9 instance with given id
        d = ds9.ds9(id)

        # load array
        input = self.view(np.ndarray)
        if input.dtype.kind in ('b', 'i'):
            input = np.array(input, np.int32, copy=False)
        d.set_np2arr(input.T)

        # load header
        if self.has_wcs():
            d.set('wcs append', str(self.header))

        if xpamsg is not None:
            if isinstance(xpamsg, str):
                xpamsg = (xpamsg,)
            for v in xpamsg:
                d.set(v)

        return d
Example #9
0
    def ds9(self, xpamsg=None, origin=None, new=True, **keywords):
        """
        Display the array using ds9.

        The ds9 process will be given an random id. By default, the
        following access point are set before the array is loaded:
            -cmap heat
            -scale scope local
            -scale mode 99.5
        Other access points can be set before the data is loaded though
        the keywords (see examples below).
        After the array is loaded, the map's header is set and the user
        may add other XPA messages through the xpamsg argument or by
        setting them through the returned ds9 instance.

        Parameters
        ----------
        xpamsg : string or tuple of string
            XPA access point message to be set after the array is loaded.
            (see http://hea-www.harvard.edu/RD/ds9/ref/xpa.html).
        origin: string
            Set origin to 'upper' for Y increasing downwards
        new: boolean
            If true, open the array in a new ds9 instance.
        **keywords : string or tuple of string
            Specify more access points to be set before array loading.
            a keyword such as 'height=400' will be appended to the command
            that launches ds9 in the form 'ds9 [...] -height 400'

        Returns
        -------
        The returned object is a ds9 instance. It can be manipulated using
        XPA access points.

        Examples
        --------
        >>> m = Map('myfits.fits')
        >>> d=m.ds9(('zoom to fit','saveimage png myfits.png'),scale='histequ', 
                    cmap='invert yes', height=400)
        >>> d.set('exit')
        """
        if not _imported_ds9:
            raise RuntimeError('The library pyds9 has not been installed.')
        import ds9, os, time, sys, uuid, xpa

        id = None
        if not new:
            list = ds9.ds9_targets()
            if list is not None:
                id = list[-1]

        if id is None:
            if 'cmap' not in keywords:
                keywords['cmap'] = 'heat'

            if 'scale' not in keywords:
                keywords['scale'] = ('scope local', 'mode 99.5')

            if origin == 'upper' or \
               'orient' not in keywords and self.origin == 'upper':
                keywords['orient'] = 'y'

            wait = 10

            id = 'ds9_' + str(uuid.uuid1())[4:8]

            command = 'ds9 -title ' + id

            for k, v in keywords.items():
                k = str(k)
                if type(v) is not tuple:
                    v = (v, )
                command += reduce(lambda x,y: \
                                  str(x) + ' -' + k + ' ' + str(y),v,'')

            os.system(command + ' &')

            # start the xpans name server
            if xpa.xpaaccess("xpans", None, 1) == None:
                _cmd = None
                # look in install directories
                for _dir in sys.path:
                    _fname = os.path.join(_dir, "xpans")
                    if os.path.exists(_fname):
                        _cmd = _fname + " -e &"
                if _cmd:
                    os.system(_cmd)

            for i in range(wait):
                list = xpa.xpaaccess(id, None, 1024)
                if list: break
                time.sleep(1)
            if not list:
                raise ValueError('No active ds9 running for target: %s' % list)

        # get ds9 instance with given id
        d = ds9.ds9(id)

        # load array
        input = self.view(np.ndarray)
        if input.dtype.kind in ('b', 'i'):
            input = np.array(input, np.int32, copy=False)
        d.set_np2arr(input.T)

        # load header
        if self.has_wcs():
            d.set('wcs append', str(self.header))

        if xpamsg is not None:
            if isinstance(xpamsg, str):
                xpamsg = (xpamsg, )
            for v in xpamsg:
                d.set(v)

        return d