def set_naxis_cb(self, w, n): idx = int(w.get_value()) - 1 self.logger.debug("naxis %d index is %d" % (n + 1, idx + 1)) image = AstroImage() image.set(path=self.path) try: hdu = self.fits_f[self.curhdu] data = hdu.data self.logger.debug("HDU #%d has naxis=%s" % (self.curhdu + 1, str(data.shape))) # invert index m = len(data.shape) - (n + 1) self.naxispath[m] = idx self.logger.debug("m=%d naxispath=%s" % (m, str(self.naxispath))) image.load_hdu(hdu, naxispath=self.naxispath) self.fitsimage.set_image(image) self.logger.debug("NAXIS%d slice %d loaded." % (n + 1, idx + 1)) except Exception, e: errmsg = "Error loading NAXIS%d slice %d: %s" % (n + 1, idx + 1, str(e)) self.logger.error(errmsg) self.fv.error(errmsg)
def set_naxis_cb(self, idx, n): self.logger.debug("naxis %d index is %d" % (n + 1, idx + 1)) image = AstroImage() image.set(path=self.path) try: hdu = self.fits_f[self.curhdu] data = hdu.data self.logger.debug("HDU #%d has naxis=%s" % (self.curhdu, str(data.shape))) # invert index m = len(data.shape) - (n + 1) self.naxispath[m] = idx self.logger.debug("m=%d naxispath=%s" % (m, str(self.naxispath))) self.logger.info("loading image from pyfits") start_time = time.time() image.load_hdu(hdu, fobj=self.fits_f, naxispath=self.naxispath) end_time = time.time() self.logger.info("loading image time %.3f sec" % (end_time - start_time)) start_time = end_time self.fitsimage.set_image(image) end_time = time.time() self.logger.debug("NAXIS%d slice %d loaded (%.3f sec)." % (n + 1, idx + 1, end_time - start_time)) except Exception, e: errmsg = "Error loading NAXIS%d slice %d: %s" % (n + 1, idx + 1, str(e)) self.logger.error(errmsg) self.fv.error(errmsg)
def set_naxis_cb(self, idx, n): self.logger.debug("naxis %d index is %d" % (n+1, idx+1)) image = AstroImage() image.set(path=self.path) try: hdu = self.fits_f[self.curhdu] data = hdu.data self.logger.debug("HDU #%d has naxis=%s" % ( self.curhdu, str(data.shape))) # invert index m = len(data.shape) - (n+1) self.naxispath[m] = idx self.logger.debug("m=%d naxispath=%s" % (m, str(self.naxispath))) self.logger.info("loading image from pyfits") start_time = time.time() image.load_hdu(hdu, fobj=self.fits_f, naxispath=self.naxispath) end_time = time.time() self.logger.info("loading image time %.3f sec" % (end_time - start_time)) start_time = end_time self.fitsimage.set_image(image) end_time = time.time() self.logger.debug("NAXIS%d slice %d loaded (%.3f sec)." % ( n+1, idx+1, end_time-start_time)) except Exception, e: errmsg = "Error loading NAXIS%d slice %d: %s" % ( n+1, idx+1, str(e)) self.logger.error(errmsg) self.fv.error(errmsg)
def set_naxis_cb(self, w, n): idx = int(w.get_value()) - 1 self.logger.debug("naxis %d index is %d" % (n+1, idx+1)) image = AstroImage() image.set(path=self.path) try: hdu = self.fits_f[self.curhdu] data = hdu.data self.logger.debug("HDU #%d has naxis=%s" % ( self.curhdu+1, str(data.shape))) # invert index m = len(data.shape) - (n+1) self.naxispath[m] = idx self.logger.debug("m=%d naxispath=%s" % (m, str(self.naxispath))) image.load_hdu(hdu, naxispath=self.naxispath) self.fitsimage.set_image(image) self.logger.debug("NAXIS%d slice %d loaded." % (n+1, idx+1)) except Exception, e: errmsg = "Error loading NAXIS%d slice %d: %s" % ( n+1, idx+1, str(e)) self.logger.error(errmsg) self.fv.error(errmsg)
def load_fits(self, fname="", extver=None): """Load fits image to current frame. Parameters ---------- fname: string, optional The name of the file to be loaded. You can specify the full extension in the name, such as filename_flt.fits[sci,1] or filename_flt.fits[1] extver: int, optional The extension to load (EXTVER in the header) Notes ----- Extname isn't used here, ginga wants the absolute extension number, not the version number associated with a name """ if fname is None: raise ValueError("No filename provided") try: shortname, extn, extv = util.verify_filename(fname) image = AstroImage(logger=self.logger) if extn is not None: raise ValueError("Extension name given, must specify " "the absolute extension you want") # prefer specified over filename if extver is None: extver = extv if (extv is None and extver is None): mef_file, nextend, first_image = util.check_valid(shortname) # if mef_file: # extver = 1 # MEF fits # else: # extver = 0 # simple fits extver = first_image # really ext number for first IMAGE with fits.open(shortname) as filedata: hdu = filedata[extver] image.load_hdu(hdu) except Exception as e: self.logger.error("Exception opening file: {0}".format(repr(e))) raise Exception(repr(e)) self._set_frameinfo(fname=shortname, hdu=hdu, image=image) self.ginga_view.set_image(image)
def load_fits(self, fname="", extver=None): """Load fits image to current frame. Parameters ---------- fname: string, optional The name of the file to be loaded. You can specify the full extension in the name, such as filename_flt.fits[sci,1] or filename_flt.fits[1] extver: int, optional The extension to load (EXTVER in the header) Notes ----- Extname isn't used here, ginga wants the absolute extension number, not the version number associated with a name """ if fname is None: raise ValueError("No filename provided") try: shortname, extn, extv = util.verify_filename(fname) image = AstroImage(logger=self.logger) if extn is not None: raise ValueError("Extension name given, must specify " "the absolute extension you want") # prefer specified over filename if extver is None: extver = extv if (extv is None and extver is None): mef = util.check_filetype(shortname) if mef: extver = 1 # MEF fits else: extver = 0 # simple fits with fits.open(shortname) as filedata: hdu = filedata[extver] image.load_hdu(hdu) except Exception as e: self.logger.error("Exception opening file: {0}".format(repr(e))) raise Exception(repr(e)) self._set_frameinfo(fname=shortname, hdu=hdu, image=image) self.ginga_view.set_image(image)
def load_fits(self, fileorhdu, viewer_name='Main Viewer'): """Load FITS image into the desired Ginga image viewer. Parameters ---------- fileorhdu File or HDU list object. viewer_name : str Name of Ginga image viewer to display to. Raises ------ KeyError Viewer name does not exist. ValueError Invalid file or HDU list object, or HDU list does not contain any image. """ if isinstance(fileorhdu, file): fileorhdu = fits.HDUList.fromfile(fileorhdu) if isiterable(fileorhdu): for hdui in fileorhdu: if hasattr(hdui, 'is_image') and hdui.is_image: hdu = hdui break else: raise ValueError( 'fileorhdu was iterable but did not contain any ' 'image HDUs') elif hasattr(fileorhdu, 'data') and hasattr(fileorhdu, 'header'): # quacks like an HDU - give it a shot hdu = fileorhdu else: raise ValueError('fileorhdu was not a fits file or HDU-ish thing') viewer = self.viewers[viewer_name] if viewer.fitsimage.get_image() is None: aim = AstroImage(logger=self.logger) aim.load_hdu(hdu) viewer.fitsimage.set_image(aim) else: viewer.fitsimage.get_image().load_hdu(hdu)
def fetch_cache_hdu(self, idx): with self._hdu_cache_lock: if self.hdu_cache.has_key(idx): self.logger.debug("HDU cache hit for index %d" % (idx)) image, dims = self.hdu_cache[idx] return dims, image else: hdu = self.fits_f[idx] path = self.path image = AstroImage() image.set(path=path) dims = list(hdu.data.shape) dims.reverse() image.load_hdu(hdu, fobj=self.fits_f) # TODO: when we've added cache size limiting #self.add_cache_hdu(idx, (image, dims)) return dims, image
def set_hdu(self, idx): self.logger.debug("Loading fits hdu #%d" % (idx)) image = AstroImage() image.set(path=self.path) try: hdu = self.fits_f[idx - 1] dims = list(hdu.data.shape) dims.reverse() image.load_hdu(hdu) self.fitsimage.set_image(image) self.build_naxis(dims) self.curhdu = idx - 1 self.logger.debug("hdu #%d loaded." % (idx)) except Exception, e: errmsg = "Error loading fits hdu #%d: %s" % (idx, str(e)) self.logger.error(errmsg) self.fv.error(errmsg)
def load_fits(self, fname="", extver=1, extname=None): """Load fits image to current frame. Parameters ---------- fname: string, optional The name of the file to be loaded. You can specify the full extension in the name, such as filename_flt.fits[sci,1] or filename_flt.fits[1] extver: int, optional The extension to load (EXTVER in the header) extname: string, optional The name (EXTNAME in the header) of the image to load Notes ----- """ if fname: # see if the image is MEF or Simple fname = os.path.abspath(fname) short = True try: mef = util.check_filetype(fname) if not mef: extver = 0 cstring = util.verify_filename(fname, getshort=short) image = AstroImage(logger=self.logger) with fits.open(cstring) as filedata: hdu = filedata[extver] image.load_hdu(hdu) except Exception as e: self.logger.error("Exception opening file: {0}".format(e)) raise IOError(str(e)) self._set_frameinfo(fname=fname, hdu=hdu, image=image) self.ginga_view.set_image(image) else: print("No filename provided")
def set_hdu(self, idx): self.logger.debug("Loading fits hdu #%d" % (idx)) image = AstroImage() image.set(path=self.path) try: hdu = self.fits_f[idx-1] dims = list(hdu.data.shape) dims.reverse() image.load_hdu(hdu) self.fitsimage.set_image(image) self.build_naxis(dims) self.curhdu = idx-1 self.logger.debug("hdu #%d loaded." % (idx)) except Exception, e: errmsg = "Error loading fits hdu #%d: %s" % ( idx, str(e)) self.logger.error(errmsg) self.fv.error(errmsg)
def load_hdu(self, hdu, dstobj=None, **kwargs): typ = self.get_hdu_type(hdu) if typ == 'image': if dstobj is None: dstobj = AstroImage(logger=self.logger) dstobj.load_hdu(hdu, **kwargs) else: # TODO: migrate code from AstroImage to here dstobj.load_hdu(hdu, **kwargs) elif typ == 'table': # <-- data may be a table if hdu.name == 'ASDF': if dstobj is not None: raise ValueError( "It is not supported to load ASDF HDU with dstobj != None" ) self.logger.debug('Attempting to load {} extension from ' 'FITS'.format(hdu.name)) from ginga.util.io import io_asdf opener = io_asdf.ASDFFileHandler(self.logger) return opener.load_asdf_hdu_in_fits(self.fits_f, hdu, **kwargs) if dstobj is None: dstobj = AstroTable(logger=self.logger) self.logger.debug('Attempting to load table from FITS') # TODO: migrate code from AstroTable to here dstobj.load_hdu(hdu, **kwargs) else: raise FITSError("I don't know how to read this HDU") dstobj.io = self return dstobj
def load_fits(self, fname=None, extver=None): """Load fits image to current frame. Parameters ---------- fname: string, FITS HDU The name of the file to be loaded. You can specify the full extension in the name, such as filename_flt.fits[sci,1] or filename_flt.fits[1] extver: int, optional The extension to load (EXTVER in the header) Notes ----- Extname isn't used here, ginga wants the absolute extension number, not the version number associated with a name """ if fname is None: raise ValueError("No filename or HDU provided") image = AstroImage(logger=self.logger) if isinstance(fname, (fits.hdu.image.PrimaryHDU, fits.hdu.image.ImageHDU)): # Simple fits, data + header shortname = fname extn = None if extver is None: extv = None extver = 0 if extver: hdu = shortname[extver] else: hdu = shortname elif isinstance(fname, fits.hdu.hdulist.HDUList): shortname = fname extn = None extv = extver if extver: hdu = shortname[extver] else: hdu = shortname elif isinstance(fname, str): shortname, extn, extv = util.verify_filename(fname) if extn is not None: raise ValueError("Extension name given, must " "specify the absolute extension you want") # prefer the keyword value over the extension in the name if extver is None: extver = extv # safety for a valid imexam file if ((extv is None) and (extver is None)): mef_file, nextend, first_image = util.check_valid(shortname) extver = first_image # the extension of the first IMAGE else: raise TypeError("Expected FITS data as input") try: if isinstance(shortname, str): with fits.open(shortname) as filedata: hdu = filedata[extver] image.load_hdu(hdu) else: image.load_hdu(hdu) self._set_frameinfo(fname=shortname, hdu=hdu, image=image) self.ginga_view.set_image(image) except Exception as e: self.logger.error(f"Exception loading image: {repr(e)}") raise Exception(repr(e))
def load_fits(self, fname=None, extver=None): """Load fits image to current frame. Parameters ---------- fname: string, FITS HDU The name of the file to be loaded. You can specify the full extension in the name, such as filename_flt.fits[sci,1] or filename_flt.fits[1] extver: int, optional The extension to load (EXTVER in the header) Notes ----- Extname isn't used here, ginga wants the absolute extension number, not the version number associated with a name """ if fname is None: raise ValueError("No filename or HDU provided") if isinstance(fname, (fits.hdu.image.PrimaryHDU, fits.hdu.image.ImageHDU)): # Simple fits, data + header shortname = fname extn = None if extver is None: extv = None extver = 0 elif isinstance(fname, fits.hdu.hdulist.HDUList): shortname = fname extn = None extv = extver elif isinstance(fname, str): shortname, extn, extv = util.verify_filename(fname) if extn is not None: raise ValueError("Extension name given, must " "specify the absolute extension you want") # prefer the keyword value over the extension in the name if extver is None: extver = extv else: raise TypeError("Expected FITS data as input") # safety for a valid imexam file if ((extv is None) and (extver is None)): mef_file, nextend, first_image = util.check_valid(shortname) extver = first_image # the extension of the first IMAGE image = AstroImage(logger=self.logger) try: if isinstance(fname, str): with fits.open(shortname) as filedata: hdu = filedata[extver] image.load_hdu(hdu) else: if extver: hdu = shortname[extver] else: hdu = shortname image.load_hdu(hdu) self._set_frameinfo(fname=shortname, hdu=hdu, image=image) self.ginga_view.set_image(image) except Exception as e: self.logger.error("Exception loading image: {0}".format(repr(e))) raise Exception(repr(e))