예제 #1
0
파일: AstroTable.py 프로젝트: teuben/ginga
    def load_file(self, filepath, numhdu=None, **kwargs):
        self.logger.debug("Loading file '%s' ..." % (filepath))
        self.clear_metadata()

        # These keywords might be provided but not used.
        kwargs.pop('allow_numhdu_override')
        kwargs.pop('memmap')

        info = iohelper.get_fileinfo(filepath)
        if numhdu is None:
            numhdu = info.numhdu

        try:
            with fits.open(filepath, 'readonly') as in_f:
                self.load_hdu(in_f[numhdu], **kwargs)

        except Exception as e:
            self.logger.error("Error reading table from file: {0}".format(
                str(e)))

        # Set the table name if no name currently exists for this table
        # TODO: should this *change* the existing name, if any?
        if self.name is not None:
            self.set(name=self.name)
        else:
            name = self.get('name', None)
            if name is None:
                name = info.name
                if ('[' not in name):
                    name += iohelper.get_hdu_suffix(numhdu)
                self.set(name=name)

        self.set(path=filepath, idx=numhdu)
예제 #2
0
    def open_files(self, path):
        """Load file(s) -- image*.fits, image*.fits[ext].
        Returns success code (True or False).
        """
        paths = []
        input_list = _patt.findall(path)
        if not input_list:
            input_list = [path]

        for path in input_list:
            # Strips trailing wildcard
            if path.endswith('*'):
                path = path[:-1]

            if os.path.isdir(path):
                continue

            self.logger.debug('Opening files matched by {0}'.format(path))
            info = iohelper.get_fileinfo(path)
            ext = iohelper.get_hdu_suffix(info.numhdu)
            files = glob.glob(info.filepath)  # Expand wildcard
            paths.extend(['{0}{1}'.format(f, ext) for f in files])

        if len(paths) > 0:
            self.load_paths(paths)
            return True

        return False
예제 #3
0
    def get_hdu(self, numhdu, dstobj=None, **kwargs):

        if numhdu is None:
            numhdu, hdu = self.find_first_good_hdu()

        elif numhdu in self.hdu_db:
            d = self.hdu_db[numhdu]
            hdu = self.fits_f[d.index]
            # normalize the index
            numhdu = (d.name, d.extver)

        else:
            hdu = self.fits_f[numhdu]
            # normalize the hdu index, if possible
            name = hdu.name
            extver = hdu.ver
            _numhdu = (name, extver)
            if ((len(name) > 0) and (_numhdu in self.fits_f)
                    and hdu is self.fits_f[_numhdu]):
                numhdu = _numhdu

        dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=self.fits_f, **kwargs)

        # Set the name if no name currently exists for this object
        # TODO: should this *change* the existing name, if any?
        name = dstobj.get('name', None)
        if name is None:
            name = self.fileinfo.name
            if '[' not in name:
                name += iohelper.get_hdu_suffix(numhdu)
            dstobj.set(name=name)

        dstobj.set(path=self.fileinfo.filepath, idx=numhdu)

        return dstobj
예제 #4
0
    def open_files(self, path):
        """Load file(s) -- image*.fits, image*.fits[ext].
        Returns success code (True or False).
        """
        paths = []
        input_list = _patt.findall(path)
        if not input_list:
            input_list = [path]

        for path in input_list:
            # Strips trailing wildcard
            if path.endswith('*'):
                path = path[:-1]

            if os.path.isdir(path):
                continue

            self.logger.debug('Opening files matched by {0}'.format(path))
            info = iohelper.get_fileinfo(path)
            ext = iohelper.get_hdu_suffix(info.numhdu)
            files = glob.glob(info.filepath)  # Expand wildcard
            paths.extend(['{0}{1}'.format(f, ext) for f in files])

        if len(paths) > 0:
            self.load_paths(paths)
            return True

        return False
예제 #5
0
    def load_file(self,
                  filepath,
                  numhdu=None,
                  naxispath=None,
                  allow_numhdu_override=True,
                  memmap=None):
        self.logger.debug("Loading file '%s' ..." % (filepath))
        self.clear_metadata()

        ahdr = self.get_header()

        info = iohelper.get_fileinfo(filepath)
        if numhdu is None:
            numhdu = info.numhdu

        _data, numhdu_, naxispath = self.io.load_file(info.filepath,
                                                      ahdr,
                                                      numhdu=numhdu,
                                                      naxispath=naxispath,
                                                      phdr=self._primary_hdr,
                                                      memmap=memmap)
        # this is a handle to the full data array
        self._md_data = _data

        if naxispath is None:
            naxispath = []

        # Drill down to 2D data slice
        if len(naxispath) == 0:
            naxispath = ([0] * (len(_data.shape) - 2))

        # Set the image name if no name currently exists for this image
        # TODO: should this *change* the existing name, if any?
        if not (self.name is None):
            self.set(name=self.name)
        else:
            name = self.get('name', None)
            if name is None:
                name = info.name
                if ('[' not in name):
                    if (numhdu is None) or allow_numhdu_override:
                        numhdu = numhdu_
                    name += iohelper.get_hdu_suffix(numhdu)
                self.set(name=name)

        self.set(path=filepath, idx=numhdu)

        self.set_naxispath(naxispath)

        # Try to make a wcs object on the header
        # TODO: in order to do more sophisticated WCS (e.g. distortion
        #   correction) that requires info in additional headers we need
        #   to pass additional information to the wcs class
        #self.wcs.load_header(hdu.header, fobj=fobj)
        self.wcs.load_header(ahdr)
예제 #6
0
    def popup(self, title, callfn, initialdir=None, filename=None):
        """Let user select and load file(s). This allows wildcards and
        extensions, like in FBrowser.

        Parameters
        ----------
        title : str
            Title for the file dialog.

        callfn : func
            Function used to open the file(s).

        initialdir : str or `None`
            Directory for file dialog.

        filename : str
            Filter for file dialog.

        """
        self.cb = callfn
        filenames = QtGui.QFileDialog.getOpenFileNames(self.parent, title,
                                                       initialdir, filename)

        # Special handling for PyQt5, see
        # https://www.reddit.com/r/learnpython/comments/2xhagb/pyqt5_trouble_with_openinggetting_the_name_of_the/
        if ginga.toolkit.get_toolkit() == 'qt5':
            filenames = filenames[0]

        all_paths = []
        for filename in filenames:

            # Special handling for wildcard or extension.
            # This is similar to open_files() in FBrowser plugin.
            if '*' in filename or '[' in filename:
                info = iohelper.get_fileinfo(filename)
                ext = iohelper.get_hdu_suffix(info.numhdu)
                files = glob.glob(info.filepath)  # Expand wildcard
                paths = ['{0}{1}'.format(f, ext) for f in files]
                if self.all_at_once:
                    all_paths.extend(paths)
                else:
                    for path in paths:
                        self.cb(path)

            else:
                # Normal load
                if self.all_at_once:
                    all_paths.append(filename)
                else:
                    self.cb(filename)

        if self.all_at_once and len(all_paths) > 0:
            self.cb(all_paths)
예제 #7
0
    def popup(self, title, callfn, initialdir=None, filename=None):
        """Let user select and load file(s). This allows wildcards and
        extensions, like in FBrowser.

        Parameters
        ----------
        title : str
            Title for the file dialog.

        callfn : func
            Function used to open the file(s).

        initialdir : str or `None`
            Directory for file dialog.

        filename : str
            Filter for file dialog.

        """
        self.cb = callfn
        filenames = QtGui.QFileDialog.getOpenFileNames(
            self.parent, title, initialdir, filename)

        # Special handling for PyQt5, see
        # https://www.reddit.com/r/learnpython/comments/2xhagb/pyqt5_trouble_with_openinggetting_the_name_of_the/
        if ginga.toolkit.get_toolkit() == 'qt5':
            filenames = filenames[0]

        all_paths = []
        for filename in filenames:

            # Special handling for wildcard or extension.
            # This is similar to open_files() in FBrowser plugin.
            if '*' in filename or '[' in filename:
                info = iohelper.get_fileinfo(filename)
                ext = iohelper.get_hdu_suffix(info.numhdu)
                files = glob.glob(info.filepath)  # Expand wildcard
                paths = ['{0}{1}'.format(f, ext) for f in files]
                if self.all_at_once:
                    all_paths.extend(paths)
                else:
                    for path in paths:
                        self.cb(path)

            else:
                # Normal load
                if self.all_at_once:
                    all_paths.append(filename)
                else:
                    self.cb(filename)

        if self.all_at_once and len(all_paths) > 0:
            self.cb(all_paths)
예제 #8
0
파일: AstroImage.py 프로젝트: saimn/ginga
    def load_file(self, filepath, numhdu=None, naxispath=None,
                  allow_numhdu_override=True, memmap=None):
        self.logger.debug("Loading file '%s' ..." % (filepath))
        self.clear_metadata()

        ahdr = self.get_header()

        info = iohelper.get_fileinfo(filepath)
        if numhdu is None:
            numhdu = info.numhdu

        _data, numhdu_, naxispath = self.io.load_file(info.filepath, ahdr,
                                                      numhdu=numhdu,
                                                      naxispath=naxispath,
                                                      phdr=self._primary_hdr,
                                                      memmap=memmap)
        # this is a handle to the full data array
        self._md_data = _data

        if naxispath is None:
            naxispath = []

        # Drill down to 2D data slice
        if len(naxispath) == 0:
            naxispath = ([0] * (len(_data.shape)-2))

        # Set the image name if no name currently exists for this image
        # TODO: should this *change* the existing name, if any?
        if not (self.name is None):
            self.set(name=self.name)
        else:
            name = self.get('name', None)
            if name is None:
                name = info.name
                if ('[' not in name):
                    if (numhdu is None) or allow_numhdu_override:
                        numhdu = numhdu_
                    name += iohelper.get_hdu_suffix(numhdu)
                self.set(name=name)

        self.set(path=filepath, idx=numhdu)

        self.set_naxispath(naxispath)

        # Try to make a wcs object on the header
        # TODO: in order to do more sophisticated WCS (e.g. distortion
        #   correction) that requires info in additional headers we need
        #   to pass additional information to the wcs class
        #self.wcs.load_header(hdu.header, fobj=fobj)
        self.wcs.load_header(ahdr)
예제 #9
0
    def set_hdu(self, idx):
        self.logger.debug("Loading fits hdu #%d" % (idx))

        # determine canonical index of this HDU
        info = self.file_obj.hdu_info[idx]
        aidx = (info.name, info.extver)
        if aidx not in self.file_obj.hdu_db:
            aidx = idx
        sfx = get_hdu_suffix(aidx)

        # See if this HDU is still in the channel's datasrc
        imname = self.name_pfx + sfx
        chname = self.chname
        chinfo = self.channel
        if imname in chinfo.datasrc:
            self.curhdu = idx
            image = chinfo.datasrc[imname]
            self.fv.switch_name(chname, imname)

            return

        # Nope, we'll have to load it
        self.logger.debug("HDU %d not in memory; refreshing from file" % (idx))
        try:
            self.curhdu = idx
            dims = [0, 0]
            info = self.file_obj.hdu_info[idx]

            image = self.file_obj.get_hdu(idx)

            # create a future for reconstituting this HDU
            future = Future.Future()
            future.freeze(self.fv.load_image, self.img_path, idx=aidx)
            image.set(path=self.img_path,
                      idx=aidx,
                      name=imname,
                      image_future=future)

            self.fv.add_image(imname, image, chname=chname)

            self.logger.debug("HDU #%d loaded." % (idx))

        except Exception as e:
            errmsg = "Error loading FITS HDU #%d: %s" % (idx, str(e))
            self.logger.error(errmsg)
            self.fv.show_error(errmsg, raisetab=True)
예제 #10
0
파일: FBrowser.py 프로젝트: jochym/ginga
    def open_files(self, path):
        """Load file(s) -- image*.fits, image*.fits[ext].
        Returns success code (True or False).
        """
        # Strips trailing wildcard
        if path.endswith('*'):
            path = path[:-1]

        if os.path.isdir(path):
            return False

        self.logger.debug('Opening files matched by {0}'.format(path))
        info = iohelper.get_fileinfo(path)
        ext = iohelper.get_hdu_suffix(info.numhdu)
        files = glob.glob(info.filepath)  # Expand wildcard
        paths = ['{0}{1}'.format(f, ext) for f in files]
        self.fv.gui_do(self.fitsimage.make_callback, 'drag-drop', paths)

        return True
예제 #11
0
    def open_files(self, path):
        """Load file(s) -- image*.fits, image*.fits[ext].
        Returns success code (True or False).
        """
        # Strips trailing wildcard
        if path.endswith('*'):
            path = path[:-1]

        if os.path.isdir(path):
            return False

        self.logger.debug('Opening files matched by {0}'.format(path))
        info = iohelper.get_fileinfo(path)
        ext = iohelper.get_hdu_suffix(info.numhdu)
        files = glob.glob(info.filepath)  # Expand wildcard
        paths = ['{0}{1}'.format(f, ext) for f in files]

        self.load_paths(paths)
        return True
예제 #12
0
파일: GingaQt.py 프로젝트: jochym/ginga
    def gui_load_file(self, initialdir=None):
        if self.filesel.exec_():
            filename = list(map(str, list(self.filesel.selectedFiles())))[0]

            # Normal load
            if os.path.isfile(filename):
                self.logger.debug('Loading {0}'.format(filename))
                self.load_file(filename)

            # Fancy load (first file only)
            # TODO: If load all the matches, might get QPainter errors
            else:
                info = iohelper.get_fileinfo(filename)
                ext = iohelper.get_hdu_suffix(info.numhdu)
                paths = ['{0}{1}'.format(fname, ext)
                         for fname in glob.iglob(info.filepath)]
                self.logger.debug(
                    'Found {0} and only loading {1}'.format(paths, paths[0]))
                self.load_file(paths[0])
예제 #13
0
    def set_hdu(self, idx):
        self.logger.debug("Loading fits hdu #%d" % (idx))

        # determine canonical index of this HDU
        info = self.file_obj.hdu_info[idx]
        aidx = (info.name, info.extver)
        if aidx not in self.file_obj.hdu_db:
            aidx = idx
        sfx = iohelper.get_hdu_suffix(aidx)

        # See if this HDU is still in the channel's datasrc
        imname = self.name_pfx + sfx
        chname = self.chname
        chinfo = self.channel
        if imname in chinfo.datasrc:
            self.curhdu = idx
            image = chinfo.datasrc[imname]
            self.fv.switch_name(chname, imname)
            return

        # Nope, we'll have to load it
        self.logger.debug("HDU %d not in memory; refreshing from file" % (idx))
        try:
            self.curhdu = idx
            info = self.file_obj.hdu_info[idx]
            image = self.file_obj.get_hdu(idx)

            # create a future for reconstituting this HDU
            future = Future.Future()
            future.freeze(self.fv.load_image, self.img_path, idx=aidx)
            image.set(path=self.img_path, idx=aidx, name=imname,
                      image_future=future)

            self.fv.add_image(imname, image, chname=chname)

            self.logger.debug("HDU #%d loaded." % (idx))

        except Exception as e:
            errmsg = "Error loading FITS HDU #%d: %s" % (
                idx, str(e))
            self.logger.error(errmsg)
            self.fv.show_error(errmsg, raisetab=True)
예제 #14
0
파일: GingaGw.py 프로젝트: saimn/ginga
    def load_file_cb(self, w, rsp):
        w.hide()
        if rsp == 0:
            return

        filename = w.selected_files()[0]

        # Normal load
        if os.path.isfile(filename):
            self.logger.debug('Loading {0}'.format(filename))
            self.load_file(filename)

        # Fancy load (first file only)
        # TODO: If load all the matches, might get (Qt) QPainter errors
        else:
            info = iohelper.get_fileinfo(filename)
            ext = iohelper.get_hdu_suffix(info.numhdu)
            paths = ['{0}{1}'.format(fname, ext)
                     for fname in glob.iglob(info.filepath)]
            self.logger.debug(
                'Found {0} and only loading {1}'.format(paths, paths[0]))
            self.load_file(paths[0])
예제 #15
0
    def get_hdu(self, numhdu, dstobj=None, **kwargs):

        if numhdu is None:
            numhdu, hdu = self.find_first_good_hdu()

        elif numhdu in self.hdu_db:
            d = self.hdu_db[numhdu]
            hdu = self.fits_f[d.index]
            # normalize the index
            numhdu = (d.name, d.extver)

        else:
            hdu = self.fits_f[numhdu]
            # normalize the hdu index, if possible
            hduinfo = hdu.get_info()
            name = hduinfo['extname']
            extver = hduinfo['extver']
            _numhdu = (name, extver)
            if ((len(name) > 0) and (_numhdu in self.fits_f) and
                hdu is self.fits_f[_numhdu]):
                numhdu = _numhdu

        dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=self.fits_f,
                               **kwargs)

        # Set the name if no name currently exists for this object
        # TODO: should this *change* the existing name, if any?
        name = dstobj.get('name', None)
        if name is None:
            name = self.fileinfo.name
            if '[' not in name:
                name += iohelper.get_hdu_suffix(numhdu)
            dstobj.set(name=name)

        dstobj.set(path=self.fileinfo.filepath, idx=numhdu)

        return dstobj
예제 #16
0
파일: GingaGw.py 프로젝트: rupak0577/ginga
    def load_file_cb(self, w, rsp):
        w.hide()
        if rsp == 0:
            return

        filename = w.selected_files()[0]

        # Normal load
        if os.path.isfile(filename):
            self.logger.debug('Loading {0}'.format(filename))
            self.load_file(filename)

        # Fancy load (first file only)
        # TODO: If load all the matches, might get (Qt) QPainter errors
        else:
            info = iohelper.get_fileinfo(filename)
            ext = iohelper.get_hdu_suffix(info.numhdu)
            paths = [
                '{0}{1}'.format(fname, ext)
                for fname in glob.iglob(info.filepath)
            ]
            self.logger.debug('Found {0} and only loading {1}'.format(
                paths, paths[0]))
            self.load_file(paths[0])
예제 #17
0
    def load_file(self,
                  filespec,
                  numhdu=None,
                  dstobj=None,
                  memmap=None,
                  **kwargs):

        info = iohelper.get_fileinfo(filespec)
        if not info.ondisk:
            raise FITSError("File does not appear to be on disk: %s" %
                            (info.url))

        filepath = info.filepath
        if numhdu is None:
            numhdu = info.numhdu

        self.logger.debug("Loading file '%s' ..." % (filepath))
        fits_f = fitsio.FITS(filepath, memmap=memmap)
        try:

            if numhdu is None:

                found_valid_hdu = False
                for i in range(len(fits_f)):
                    hdu = fits_f[i]
                    hduinfo = hdu.get_info()
                    #print(hduinfo)
                    name = hduinfo['extname']
                    extver = hduinfo['extver']

                    if not ('ndims' in hduinfo) or (hduinfo['ndims'] == 0):
                        # compressed FITS file or non-pixel data hdu?
                        continue
                    #print(dir(hdu))
                    # Looks good, let's try it
                    found_valid_hdu = True
                    if len(name) == 0:
                        numhdu = i
                    else:
                        numhdu = (name, extver)
                    break

                if not found_valid_hdu:
                    # Just load the header
                    hdu = fits_f[0]
                    hduinfo = hdu.get_info()
                    name = hduinfo['extname']
                    extver = hduinfo['extver']
                    if len(name) == 0:
                        numhdu = 0
                    else:
                        numhdu = (name, extver)

            elif isinstance(numhdu, (int, str)):
                hdu = fits_f[numhdu]
                hduinfo = hdu.get_info()
                name = hduinfo['extname']
                extver = hduinfo['extver']
                if len(name) > 0:
                    numhdu = (name, extver)

            self.logger.debug("HDU index looks like: %s" % str(numhdu))
            hdu = fits_f[numhdu]

            dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=fits_f, **kwargs)

            # Read PRIMARY header
            #self.fromHDU(fits_f[0], phdr)

        finally:
            fits_f.close()

        # Set the name if no name currently exists for this object
        # TODO: should this *change* the existing name, if any?
        name = dstobj.get('name', None)
        if name is None:
            name = info.name
            if ('[' not in name):
                name += iohelper.get_hdu_suffix(numhdu)
            dstobj.set(name=name)

        dstobj.set(path=filepath, idx=numhdu)

        return dstobj
예제 #18
0
    def set_hdu(self, idx):
        self.logger.debug("Loading fits hdu #%d" % (idx))

        # determine canonical index of this HDU
        info = self.hdu_info[idx]
        aidx = (info.name, info.extver)
        if aidx not in self.fits_f:
            aidx = idx
        sfx = get_hdu_suffix(aidx)

        # See if this HDU is still in the channel's datasrc
        imname = self.name_pfx + sfx
        chname = self.chname
        chinfo = self.channel
        if imname in chinfo.datasrc:
            self.curhdu = idx
            self.image = chinfo.datasrc[imname]
            self.fv.switch_name(chname, imname)

            # Still need to build datacube profile
            hdu = self.fits_f[idx]
            if hdu.data is not None:
                dims = list(hdu.data.shape)
                dims.reverse()
                self.build_naxis(dims)
                return

        # Nope, we'll have to load it
        self.logger.debug("HDU %d not in memory; refreshing from file" % (idx))
        try:
            self.curhdu = idx
            dims = [0, 0]
            hdu = self.fits_f[idx]

            image = self.fv.fits_opener.load_hdu(hdu, fobj=self.fits_f)
            self.image = image

            if hdu.data is None:
                # <- empty data part to this HDU
                self.logger.warning("Empty data part in HDU #%d" % (idx))

            elif info['htype'].lower() in ('bintablehdu', 'tablehdu',):
                dims = [0, 0]

            elif info['htype'].lower() not in ('imagehdu', 'primaryhdu'):
                self.logger.warning("HDU #%d is not an image" % (idx))

            else:
                dims = list(hdu.data.shape)
                dims.reverse()

            image.load_hdu(hdu, fobj=self.fits_f)

            # create a future for reconstituting this HDU
            future = Future.Future()
            future.freeze(self.fv.load_image, self.path, idx=aidx)
            image.set(path=self.path, idx=aidx, name=imname,
                      image_future=future)

            self.fv.add_image(imname, image, chname=chname)

            self.build_naxis(dims)
            self.logger.debug("HDU #%d loaded." % (idx))

        except Exception as e:
            errmsg = "Error loading FITS HDU #%d: %s" % (
                idx, str(e))
            self.logger.error(errmsg)
            self.fv.show_error(errmsg, raisetab=False)
예제 #19
0
    def load_file(self,
                  filespec,
                  numhdu=None,
                  dstobj=None,
                  memmap=None,
                  **kwargs):

        info = iohelper.get_fileinfo(filespec)
        if not info.ondisk:
            raise FITSError("File does not appear to be on disk: %s" %
                            (info.url))

        filepath = info.filepath
        if numhdu is None:
            numhdu = info.numhdu

        self.logger.debug("Loading file '%s' ..." % (filepath))
        with pyfits.open(filepath, 'readonly', memmap=memmap) as fits_f:

            # this seems to be necessary now for some fits files...
            try:
                fits_f.verify('fix')

            except Exception as e:
                # Let's hope for the best!
                self.logger.warn("Problem verifying fits file '%s': %s" %
                                 (filepath, str(e)))

            if numhdu is None:

                hduinfo = fits_f.info(output=False)
                extver_db = {}

                found_valid_hdu = False
                for i in range(len(fits_f)):
                    hdu = fits_f[i]
                    tup = hduinfo[i]
                    name = tup[1]
                    # figure out the EXTVER for this HDU
                    extver = extver_db.setdefault(name, 0)
                    extver += 1
                    extver_db[name] = extver

                    # rule out HDUs we can't deal with
                    if not isinstance(hdu, (
                            pyfits.ImageHDU,
                            pyfits.PrimaryHDU,
                            pyfits.TableHDU,
                            pyfits.BinTableHDU,
                    )):
                        continue
                    if not isinstance(hdu.data, numpy.ndarray):
                        # We need to open a numpy array
                        continue
                    if 0 in hdu.data.shape:
                        # non-pixel or zero-length data hdu?
                        continue

                    # Looks good, let's try it
                    found_valid_hdu = True
                    _numhdu = (name, extver)
                    if (len(name) == 0) or (_numhdu not in fits_f):
                        numhdu = i
                    else:
                        numhdu = _numhdu
                    break

                if not found_valid_hdu:
                    # Load just the header
                    hdu = fits_f[0]
                    name = hdu.name
                    extver = hdu.ver
                    _numhdu = (name, extver)
                    if (len(name) == 0) or (_numhdu not in fits_f):
                        numhdu = 0
                    else:
                        numhdu = _numhdu

            elif isinstance(numhdu, (int, str)):
                hdu = fits_f[numhdu]
                name = hdu.name
                extver = hdu.ver
                _numhdu = (name, extver)
                if (len(name) > 0) and (_numhdu in fits_f):
                    numhdu = _numhdu

            self.logger.debug("HDU index looks like: %s" % str(numhdu))
            hdu = fits_f[numhdu]

            dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=fits_f, **kwargs)

            # Set the name if no name currently exists for this object
            # TODO: should this *change* the existing name, if any?
            name = dstobj.get('name', None)
            if name is None:
                name = info.name
                if ('[' not in name):
                    ## if (numhdu is None) or allow_numhdu_override:
                    ##     numhdu = numhdu_
                    name += iohelper.get_hdu_suffix(numhdu)
                dstobj.set(name=name)

            dstobj.set(path=filepath, idx=numhdu)

            return dstobj
예제 #20
0
    def load_file(self, filespec, numhdu=None, dstobj=None, memmap=None,
                  **kwargs):

        info = iohelper.get_fileinfo(filespec)
        if not info.ondisk:
            raise FITSError("File does not appear to be on disk: %s" % (
                info.url))

        filepath = info.filepath
        if numhdu is None:
            numhdu = info.numhdu

        self.logger.debug("Loading file '%s' ..." % (filepath))
        fits_f = fitsio.FITS(filepath, memmap=memmap)
        try:

            if numhdu is None:

                found_valid_hdu = False
                for i in range(len(fits_f)):
                    hdu = fits_f[i]
                    hduinfo = hdu.get_info()
                    #print(hduinfo)
                    name = hduinfo['extname']
                    extver = hduinfo['extver']

                    if not ('ndims' in hduinfo) or (hduinfo['ndims'] == 0):
                        # compressed FITS file or non-pixel data hdu?
                        continue
                    #print(dir(hdu))
                    # Looks good, let's try it
                    found_valid_hdu = True
                    if len(name) == 0:
                        numhdu = i
                    else:
                        numhdu = (name, extver)
                    break

                if not found_valid_hdu:
                    # Just load the header
                    hdu = fits_f[0]
                    hduinfo = hdu.get_info()
                    name = hduinfo['extname']
                    extver = hduinfo['extver']
                    if len(name) == 0:
                        numhdu = 0
                    else:
                        numhdu = (name, extver)

            elif isinstance(numhdu, (int, str)):
                hdu = fits_f[numhdu]
                hduinfo = hdu.get_info()
                name = hduinfo['extname']
                extver = hduinfo['extver']
                if len(name) > 0:
                    numhdu = (name, extver)

            self.logger.debug("HDU index looks like: %s" % str(numhdu))
            hdu = fits_f[numhdu]

            dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=fits_f,
                                   **kwargs)

            # Read PRIMARY header
            #self.fromHDU(fits_f[0], phdr)

        finally:
            fits_f.close()

        # Set the name if no name currently exists for this object
        # TODO: should this *change* the existing name, if any?
        name = dstobj.get('name', None)
        if name is None:
            name = info.name
            if ('[' not in name):
                name += iohelper.get_hdu_suffix(numhdu)
            dstobj.set(name=name)

        dstobj.set(path=filepath, idx=numhdu)

        return dstobj
예제 #21
0
    def load_file(self, filespec, numhdu=None, dstobj=None, memmap=None,
                  **kwargs):

        info = iohelper.get_fileinfo(filespec)
        if not info.ondisk:
            raise FITSError("File does not appear to be on disk: %s" % (
                info.url))

        filepath = info.filepath
        if numhdu is None:
            numhdu = info.numhdu

        self.logger.debug("Loading file '%s' ..." % (filepath))
        with pyfits.open(filepath, 'readonly', memmap=memmap) as fits_f:

            # this seems to be necessary now for some fits files...
            try:
                fits_f.verify('fix')

            except Exception as e:
                # Let's hope for the best!
                self.logger.warn("Problem verifying fits file '%s': %s" % (
                    filepath, str(e)))

            if numhdu is None:
                try:
                    # this can fail for certain FITS files, with a "name undefined"
                    # error bubbling up from astropy
                    hduinfo = fits_f.info(output=False)

                except Exception as e:
                    # if so, this insures that name will be translated into the
                    # HDU index below
                    hduinfo = tuple((None, '') for i in range(len(fits_f)))

                extver_db = {}

                found_valid_hdu = False
                for i in range(len(fits_f)):
                    hdu = fits_f[i]
                    tup = hduinfo[i]
                    name = tup[1]
                    # figure out the EXTVER for this HDU
                    extver = extver_db.setdefault(name, 0)
                    extver += 1
                    extver_db[name] = extver

                    # rule out HDUs we can't deal with
                    if not isinstance(hdu, (pyfits.ImageHDU,
                                            pyfits.PrimaryHDU,
                                            pyfits.TableHDU,
                                            pyfits.BinTableHDU,
                                            )):
                        continue
                    if not isinstance(hdu.data, numpy.ndarray):
                        # We need to open a numpy array
                        continue
                    if 0 in hdu.data.shape:
                        # non-pixel or zero-length data hdu?
                        continue

                    # Looks good, let's try it
                    found_valid_hdu = True
                    _numhdu = (name, extver)
                    if (len(name) == 0) or (_numhdu not in fits_f):
                        numhdu = i
                    else:
                        numhdu = _numhdu
                    break

                if not found_valid_hdu:
                    # Load just the header
                    hdu = fits_f[0]
                    name = hdu.name
                    extver = hdu.ver
                    _numhdu = (name, extver)
                    if (len(name) == 0) or (_numhdu not in fits_f):
                        numhdu = 0
                    else:
                        numhdu = _numhdu

            elif isinstance(numhdu, (int, str)):
                hdu = fits_f[numhdu]
                name = hdu.name
                extver = hdu.ver
                _numhdu = (name, extver)
                if (len(name) > 0) and (_numhdu in fits_f):
                    numhdu = _numhdu

            self.logger.debug("HDU index looks like: %s" % str(numhdu))
            hdu = fits_f[numhdu]

            dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=fits_f,
                                   **kwargs)

            # Set the name if no name currently exists for this object
            # TODO: should this *change* the existing name, if any?
            name = dstobj.get('name', None)
            if name is None:
                name = info.name
                if ('[' not in name):
                    ## if (numhdu is None) or allow_numhdu_override:
                    ##     numhdu = numhdu_
                    name += iohelper.get_hdu_suffix(numhdu)
                dstobj.set(name=name)

            dstobj.set(path=filepath, idx=numhdu)

            return dstobj
예제 #22
0
    def get_hdu(self, numhdu, dstobj=None, **kwargs):

        if numhdu is None:
            found_valid_hdu = False
            for i, d in enumerate(self.hdu_info):
                name = d.name
                extver = d.extver
                hdu = self.fits_f[i]
                hduinfo = hdu.get_info()

                if not ('ndims' in hduinfo) or (hduinfo['ndims'] == 0):
                    # compressed FITS file or non-pixel data hdu?
                    continue

                if not hasattr(hdu, 'read'):
                    continue
                data = hdu.read()

                if not isinstance(data, np.ndarray):
                    # We need to open a numpy array
                    continue

                if 0 in data.shape:
                    # non-pixel or zero-length data hdu?
                    continue

                # Looks good, let's try it
                found_valid_hdu = True
                if len(name) == 0:
                    numhdu = i
                else:
                    numhdu = (name, extver)
                break

            if not found_valid_hdu:
                # Just load the header
                hdu = self.fits_f[0]
                d = self.hdu_info[0]
                name = d.name
                extver = d.extver
                if len(name) == 0:
                    numhdu = 0
                else:
                    numhdu = (name, extver)

        elif isinstance(numhdu, (int, str)):
            hdu = self.fits_f[numhdu]
            hduinfo = hdu.get_info()
            name = hduinfo['extname']
            extver = hduinfo['extver']
            if len(name) > 0:
                numhdu = (name, extver)

        self.logger.debug("HDU index looks like: %s" % str(numhdu))
        hdu = self.fits_f[numhdu]

        dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=self.fits_f, **kwargs)

        # Set the name if no name currently exists for this object
        # TODO: should this *change* the existing name, if any?
        name = dstobj.get('name', None)
        if name is None:
            name = self.fileinfo.name
            if '[' not in name:
                name += iohelper.get_hdu_suffix(numhdu)
            dstobj.set(name=name)

        dstobj.set(path=self.fileinfo.filepath, idx=numhdu)

        return dstobj
예제 #23
0
    def get_hdu(self, numhdu, dstobj=None, **kwargs):

        if numhdu is None:
            found_valid_hdu = False
            for i, d in enumerate(self.hdu_info):
                name = d.name
                hdu = self.fits_f[i]

                # rule out HDUs we can't deal with
                if not isinstance(hdu, (
                        pyfits.ImageHDU,
                        pyfits.CompImageHDU,
                        pyfits.PrimaryHDU,
                        pyfits.TableHDU,
                        pyfits.BinTableHDU,
                )):
                    continue

                if not isinstance(hdu.data, np.ndarray):
                    # We need to open a numpy array
                    continue

                if 0 in hdu.data.shape:
                    # non-pixel or zero-length data hdu?
                    continue

                # Looks good, let's try it
                found_valid_hdu = True
                extver = d.extver
                _numhdu = (name, extver)
                if (len(name) == 0) or (_numhdu not in self.fits_f):
                    numhdu = i
                else:
                    numhdu = _numhdu
                break

            if not found_valid_hdu:
                # Load just the header
                hdu = self.fits_f[0]
                d = self.hdu_info[0]
                name = d.name
                extver = d.extver
                _numhdu = (name, extver)
                if (len(name) == 0) or (_numhdu not in self.fits_f):
                    numhdu = 0
                else:
                    numhdu = _numhdu

        elif isinstance(numhdu, (int, str)):
            hdu = self.fits_f[numhdu]
            name = hdu.name
            extver = hdu.ver
            _numhdu = (name, extver)
            if (len(name) > 0) and (_numhdu in self.fits_f):
                numhdu = _numhdu

        self.logger.debug("HDU index looks like: %s" % str(numhdu))
        if numhdu not in self.fits_f:
            info = self.hdu_db[numhdu]
            hdu = self.fits_f[info.index]
        else:
            hdu = self.fits_f[numhdu]

        dstobj = self.load_hdu(hdu, dstobj=dstobj, fobj=self.fits_f, **kwargs)

        # Set the name if no name currently exists for this object
        # TODO: should this *change* the existing name, if any?
        name = dstobj.get('name', None)
        if name is None:
            name = self.fileinfo.name
            if '[' not in name:
                name += iohelper.get_hdu_suffix(numhdu)
            dstobj.set(name=name)

        dstobj.set(path=self.fileinfo.filepath, idx=numhdu)

        return dstobj
예제 #24
0
파일: MultiDim.py 프로젝트: naojsoft/ginga
    def set_hdu(self, idx):
        self.logger.debug("Loading fits hdu #%d" % (idx))

        # determine canonical index of this HDU
        info = self.hdu_info[idx]
        aidx = (info.name, info.extver)
        if aidx not in self.fits_f:
            aidx = idx
        sfx = get_hdu_suffix(aidx)

        # See if this HDU is still in the channel's datasrc
        imname = self.name_pfx + sfx
        chname = self.chname
        chinfo = self.channel
        if imname in chinfo.datasrc:
            self.curhdu = idx
            self.image = chinfo.datasrc[imname]
            self.fv.switch_name(chname, imname)

            # Still need to build datacube profile
            hdu = self.fits_f[idx]
            if hdu.data is not None:
                dims = list(hdu.data.shape)
                dims.reverse()
                self.build_naxis(dims)
                return

        # Nope, we'll have to load it
        self.logger.debug("HDU %d not in memory; refreshing from file" % (idx))
        try:
            self.curhdu = idx
            dims = [0, 0]
            hdu = self.fits_f[idx]

            image = self.fv.fits_opener.load_hdu(hdu, fobj=self.fits_f)
            self.image = image

            if hdu.data is None:
                # <- empty data part to this HDU
                self.logger.warning("Empty data part in HDU #%d" % (idx))

            elif info['htype'].lower() in ('bintablehdu', 'tablehdu',):
                dims = [0, 0]

            elif info['htype'].lower() not in ('imagehdu', 'primaryhdu'):
                self.logger.warning("HDU #%d is not an image" % (idx))

            else:
                dims = list(hdu.data.shape)
                dims.reverse()

            image.load_hdu(hdu, fobj=self.fits_f)

            # create a future for reconstituting this HDU
            future = Future.Future()
            future.freeze(self.fv.load_image, self.path, idx=aidx)
            image.set(path=self.path, idx=aidx, name=imname,
                      image_future=future)

            self.fv.add_image(imname, image, chname=chname)

            self.build_naxis(dims)
            self.logger.debug("HDU #%d loaded." % (idx))

        except Exception as e:
            errmsg = "Error loading FITS HDU #%d: %s" % (
                idx, str(e))
            self.logger.error(errmsg)
            self.fv.show_error(errmsg, raisetab=False)