def main() : parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('file', type = str, default = None, help = 'FITS file') parser.add_argument("--list", default=False, help="List the extensions only?", action="store_true") parser.add_argument('--raw_lris', action="store_true") parser.add_argument('--exten', type=int, help="FITS extension") pargs = parser.parse_args() # List? if pargs.list: from astropy.io import fits hdu = fits.open(pargs.file) print(hdu.info()) return kludge_fil = 'tmp_ginga.fits' # Setup for PYPIT imports import subprocess from astropy.io import fits from pypit import pyputils msgs = pyputils.get_dummy_logger() from pypit import arlris # Extension if pargs.exten is not None: hdu = fits.open(pargs.file) img = hdu[pargs.exten].data # Write msgs.warn('Writing kludge file to {:s}'.format(kludge_fil)) hdunew = fits.PrimaryHDU(img) hdulist = fits.HDUList([hdunew]) hdulist.writeto(kludge_fil,clobber=True) # pargs.file = kludge_fil # RAW_LRIS?? if pargs.raw_lris: # img, head, _ = arlris.read_lris(pargs.file) # Generate hdu hdu = fits.PrimaryHDU(img) hdulist = fits.HDUList([hdu]) # Write msgs.warn('Writing kludge file to {:s}'.format(kludge_fil)) hdulist.writeto(kludge_fil,clobber=True) pargs.file = kludge_fil # Spawn ginga subprocess.call(["ginga", pargs.file]) if pargs.raw_lris: msgs.warn('Removing kludge file {:s}'.format(kludge_fil)) subprocess.call(["rm", pargs.file])
def main(args): # List only? from astropy.io import fits if args.list: hdu = fits.open(args.file) print(hdu.info()) return kludge_fil = 'tmp_ginga.fits' # Setup for PYPIT imports import subprocess from pypit import pyputils msgs = pyputils.get_dummy_logger() from pypit import arlris # Extension if args.exten is not None: hdu = fits.open(args.file) img = hdu[args.exten].data # Write msgs.warn('Writing kludge file to {:s}'.format(kludge_fil)) hdunew = fits.PrimaryHDU(img) hdulist = fits.HDUList([hdunew]) hdulist.writeto(kludge_fil,clobber=True) # args.file = kludge_fil # RAW_LRIS?? if args.raw_lris: # img, head, _ = arlris.read_lris(args.file) # Generate hdu hdu = fits.PrimaryHDU(img) hdulist = fits.HDUList([hdu]) # Write msgs.warn('Writing kludge file to {:s}'.format(kludge_fil)) hdulist.writeto(kludge_fil,clobber=True) args.file = kludge_fil # Spawn ginga subprocess.call(["ginga", args.file]) if args.raw_lris: msgs.warn('Removing kludge file {:s}'.format(kludge_fil)) subprocess.call(["rm", args.file])
def show_image(inp, chname='Image', wcs_img=None, **kwargs): """ Displays input image in Ginga viewer Supersedes method in xastropy Parameters ---------- inp : str or ndarray (2D) If str, assumes the image is written to disk wcs_img : str, optional If included, use this in WCS. Mainly to show wavelength array Returns ------- """ from astropy.io import fits from pypit import arlris if isinstance(inp, basestring): if '.fits' in inp: if 'raw_lris' in kwargs.keys(): img, head, _ = arlris.read_lris(inp) else: hdu = fits.open(inp) try: exten = kwargs['exten'] except KeyError: exten = 0 img = hdu[exten].data else: img = inp viewer = connect_to_ginga() ch = viewer.channel(chname) name = 'image' # Header header = {} header['NAXIS1'] = img.shape[1] header['NAXIS2'] = img.shape[0] if wcs_img is not None: header['WCS-XIMG'] = wcs_img #header['WCS-XIMG'] = '/home/xavier/REDUX/Keck/LRIS/2017mar20/lris_red_setup_C/MF_lris_red/MasterWave_C_02_aa.fits' # Giddy up ch.load_np(name, img, 'fits', header) return viewer, ch
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('file', type=str, default=None, help='FITS file') parser.add_argument("--list", default=False, help="List the extensions only?", action="store_true") parser.add_argument('--raw_lris', action="store_true") parser.add_argument('--exten', type=int, help="FITS extension") pargs = parser.parse_args() # List? if pargs.list: from astropy.io import fits hdu = fits.open(pargs.file) print(hdu.info()) return kludge_fil = 'tmp_ginga.fits' # Setup for PYPIT imports import subprocess from astropy.io import fits from pypit import pyputils msgs = pyputils.get_dummy_logger() from pypit import arlris # Extension if pargs.exten is not None: hdu = fits.open(pargs.file) img = hdu[pargs.exten].data # Write msgs.warn('Writing kludge file to {:s}'.format(kludge_fil)) hdunew = fits.PrimaryHDU(img) hdulist = fits.HDUList([hdunew]) hdulist.writeto(kludge_fil, clobber=True) # pargs.file = kludge_fil # RAW_LRIS?? if pargs.raw_lris: # img, head, _ = arlris.read_lris(pargs.file) # Generate hdu hdu = fits.PrimaryHDU(img) hdulist = fits.HDUList([hdu]) # Write msgs.warn('Writing kludge file to {:s}'.format(kludge_fil)) hdulist.writeto(kludge_fil, clobber=True) pargs.file = kludge_fil # Spawn ginga subprocess.call(["ginga", pargs.file]) if pargs.raw_lris: msgs.warn('Removing kludge file {:s}'.format(kludge_fil)) subprocess.call(["rm", pargs.file])
def load_frames(slf, fitsdict, ind, det, frametype='<None>', msbias=None, trim=True, transpose=False): """ Load data frames, usually raw. Bias subtract (if not msbias!=None) and trim (if True) Parameters ---------- fitsdict : dict Contains relevant information from fits header files ind : list or array integers of indices det : int Detector number, starts at 1 Returns ------- frames : ndarray (3 dimensional) One image per ind """ def load_indfr(name,ext): msgs.work("Trim and overscan has not been applied") temp = pyfits.getdata(name, ext) return temp msgs.info("Loading individual {0:s} frames".format(frametype)) if np.size(ind) == 0: msgs.warn("No {0:s} frames to load".format(frametype)) return None msgs.work("Implement multiprocessing here (better -- at the moment it's slower than not) to speed up data reading") for i in range(np.size(ind)): # Instrument specific read if slf._argflag['run']['spectrograph'] in ['lris_blue', 'lris_red']: temp, head0, _ = arlris.read_lris(fitsdict['directory'][ind[i]]+fitsdict['filename'][ind[i]], det=det) else: temp = pyfits.getdata(fitsdict['directory'][ind[i]]+fitsdict['filename'][ind[i]], slf._spect['fits']['dataext']) temp = temp.astype(float) # Let us avoid uint16 if transpose: temp = temp.T if msbias is not None: if type(msbias) is np.ndarray: temp -= msbias # Subtract the master bias frame elif type(msbias) is str: if msbias == "overscan": arproc.sub_overscan(slf, det, temp) else: msgs.error("Could not subtract bias level when loading {0:s} frames".format(frametype)) if trim: temp = arproc.trim(slf, temp, det) if i == 0: frames = np.zeros((temp.shape[0], temp.shape[1], np.size(ind))) frames[:,:,i] = temp.copy() else: frames[:,:,i] = temp.copy() del temp # pool = mpPool(processes=np.min([slf._argflag['run']['ncpus'],np.size(ind)])) # async_results = [] # for i in range(np.size(ind)): # async_results.append(pool.apply_async(pyfits.getdata, (fitsdict['directory'][ind[i]]+fitsdict['filename'][ind[i]], slf._spect['fits']['dataext']))) # pool.close() # pool.join() # map(ApplyResult.wait, async_results) # for j in range(np.size(ind)): # if j == 0: # temp = async_results[j].get() # frames = np.zeros((temp.shape[0], temp.shape[1], np.size(ind))) # if msbias is None: # frames[:,:,i] = temp # else: # frames[:,:,i] = temp - msbias # del temp # else: # if msbias is None: # frames[:,:,i] = async_results[j].get() # else: # frames[:,:,i] = async_results[j].get() - msbias if np.size(ind) == 1: msgs.info("Loaded {0:d} {1:s} frame successfully".format(np.size(ind), frametype)) else: msgs.info("Loaded {0:d} {1:s} frames successfully".format(np.size(ind), frametype)) return frames
def load_frames(slf, fitsdict, ind, det, frametype='<None>', msbias=None, trim=True, transpose=False): """ Load data frames, usually raw. Bias subtract (if not msbias!=None) and trim (if True) Parameters ---------- fitsdict : dict Contains relevant information from fits header files ind : list or array integers of indices det : int Detector number, starts at 1 Returns ------- frames : ndarray (3 dimensional) One image per ind """ def load_indfr(name, ext): msgs.work("Trim and overscan has not been applied") temp = pyfits.getdata(name, ext) return temp msgs.info("Loading individual {0:s} frames".format(frametype)) if np.size(ind) == 0: msgs.warn("No {0:s} frames to load".format(frametype)) return None msgs.work( "Implement multiprocessing here (better -- at the moment it's slower than not) to speed up data reading" ) for i in range(np.size(ind)): # Instrument specific read if slf._argflag['run']['spectrograph'] in ['lris_blue', 'lris_red']: temp, head0, _ = arlris.read_lris(fitsdict['directory'][ind[i]] + fitsdict['filename'][ind[i]], det=det) else: temp = pyfits.getdata( fitsdict['directory'][ind[i]] + fitsdict['filename'][ind[i]], slf._spect['fits']['dataext']) temp = temp.astype(float) # Let us avoid uint16 if transpose: temp = temp.T if msbias is not None: if type(msbias) is np.ndarray: temp -= msbias # Subtract the master bias frame elif type(msbias) is str: if msbias == "overscan": arproc.sub_overscan(slf, det, temp) else: msgs.error( "Could not subtract bias level when loading {0:s} frames" .format(frametype)) if trim: temp = arproc.trim(slf, temp, det) if i == 0: frames = np.zeros((temp.shape[0], temp.shape[1], np.size(ind))) frames[:, :, i] = temp.copy() else: frames[:, :, i] = temp.copy() del temp # pool = mpPool(processes=np.min([slf._argflag['run']['ncpus'],np.size(ind)])) # async_results = [] # for i in range(np.size(ind)): # async_results.append(pool.apply_async(pyfits.getdata, (fitsdict['directory'][ind[i]]+fitsdict['filename'][ind[i]], slf._spect['fits']['dataext']))) # pool.close() # pool.join() # map(ApplyResult.wait, async_results) # for j in range(np.size(ind)): # if j == 0: # temp = async_results[j].get() # frames = np.zeros((temp.shape[0], temp.shape[1], np.size(ind))) # if msbias is None: # frames[:,:,i] = temp # else: # frames[:,:,i] = temp - msbias # del temp # else: # if msbias is None: # frames[:,:,i] = async_results[j].get() # else: # frames[:,:,i] = async_results[j].get() - msbias if np.size(ind) == 1: msgs.info("Loaded {0:d} {1:s} frame successfully".format( np.size(ind), frametype)) else: msgs.info("Loaded {0:d} {1:s} frames successfully".format( np.size(ind), frametype)) return frames