def read_data(path, dtype=np.float64, bin_factor=None, **kargs): """ Read SOHO / STEREO data files and output a Data instance Input : path : path of the data set dtype : cast of the data array kargs : arguments of the data filtering """ if not os.path.isdir(path): raise ValueError('Directory does not exist') # read files fnames = os.listdir(path) files = [pyfits.fitsopen(os.path.join(path, fname))[0] for fname in fnames] files = filter_files(files, **kargs) fits_arrays = list() for f in files: fits_array = fa.hdu2fitsarray(f) if bin_factor is not None: fits_array = fits_array.bin(bin_factor) fits_array.header['RSUN'] /= bin_factor update_header(fits_array) fits_arrays.append(fits_array) data = fa.infoarrays2infoarray(fits_arrays) data = data.astype(dtype) return data
def __new__(subtype, shape=None, data=None, file=None, ext=0, dtype=float, buffer=None, offset=0, strides=None, order=None, header=None): # various inputs if shape is not None: obj = np.ndarray.__new__(subtype, shape, dtype, buffer, offset, strides, order) elif data is not None: obj = np.array(data).view(subtype) elif file is not None: fits = pyfits.fitsopen(file) obj = fits[ext].data.view(subtype) header = fits[ext].header dtype = get_dtype(header) # ensure minimal header is setup if header is None: header = pyfits.PrimaryHDU(obj).header if isinstance(header, dict): header = dict2header(header) obj.header = header enforce_minimal_header(obj) return obj
def processImage(fn,d,obs_id=None,threshold=2.7,pr=False,ds9cat=None,bysegments=False,stars=[]): """Process image, print its FWHM. Works with multi extension images. """ ff = pyfits.fitsopen(fn) rts2 = rts2comm.Rts2Comm() if d: d.set('file mosaicimage iraf ' + fn) sexcols = ['X_IMAGE','Y_IMAGE','MAG_BEST','FLAGS','CLASS_STAR','FWHM_IMAGE','A_IMAGE','B_IMAGE','EXT_NUMBER','FLUX_BEST','BACKGROUND'] c = sextractor.Sextractor(sexcols,threshold=threshold,sexconfig='/home/observer/findfwhm/Sextractor/focus.sex',starnnw='/home/observer/findfwhm/Sextractor/default.nnw') c.runSExtractor(fn) c.sortObjects(2) for st in stars: # append distance - none and star number - to star list st.append(None) st.append(None) # dump Sextractor to DS9 catalogue if ds9cat: cat = open(ds9cat,'w') cat.write('\t'.join(sexcols) + '\n') for x in c.objects: cat.write('\t'.join(map(lambda y:str(y),x)) + '\n') cat.close() seg_fwhms = map(lambda x:FWHM(),range(0,len(ff)+1)) for x in c.objects: if pr: print '\t'.join(map(lambda y:str(y),x)) segnum = int(x[8]) for st in stars: if st[0] == segnum: dist = math.sqrt((x[0]-st[1])**2+(x[1]-st[2])**2) if st[4] is None or st[4] > dist: st[4] = dist st[5] = x if x[3] == 0 and x[4] != 0: if d: d.set('regions','tile {0}\nimage; circle {1} {2} 10 # color=green'.format(segnum,x[0],x[1])) seg_fwhms[0].addFWHM(x[5],x[6],x[7]) seg_fwhms[segnum].addFWHM(x[5],x[6],x[7]) elif d: d.set('regions','# tile {0}\nphysical; point {1} {2} # point = x 5 color=red'.format(segnum,x[0],x[1])) # average results map(FWHM.average,seg_fwhms) # default suffix defsuffix = '_KCAM' try: defsuffix = '_' + ff[0].header['CCD_NAME'] except KeyError,er: pass
def read_fits_array(filename, ext=0): """Reads a fits file and output a FitsArray """ fits = pyfits.fitsopen(filename) header = fits[ext].header dtype = get_dtype(header) data = fits[ext].data.astype(dtype) fits_array = FitsArray(data=data, header=header, dtype=dtype) return fits_array
def update_header(filename, ext, rec, val): """ Update value in an fits header """ fits = pyfits.fitsopen(filename, "update") for e in ext: for r, v in zip(rec, val): fits[e].header.update(r, v) fits.flush()
def median(of,files): """Computes normalized median of files.""" f = pyfits.fitsopen(files[0]) d = numpy.empty([len(files),len(f[0].data),len(f[0].data[0])]) avrg = numpy.mean(f[0].data) d[0] = f[0].data / avrg for x in range(1,len(files)): f = pyfits.fitsopen(files[x]) avrg = numpy.mean(f[0].data) d[x] = f[0].data / avrg if (os.path.exists(of)): print "removing " + of os.unlink(of) f = pyfits.open(of,mode='append') m = numpy.median(d,axis=0) i = pyfits.PrimaryHDU(data=m) f.append(i) print 'writing %s of mean: %f std: %f median: %f' % (of,numpy.mean(m), numpy.std(m), numpy.median(numpy.median(m))) f.close()
def mrdfits(filename, extension=1, transpose=False, verbose=False): """ Read a an extended fits file. Try to emulate mrdfits.pro Arguments --------- filename: string extension: int transpose: bool verbose: bool Returns ------- A dictionary of arrays """ if verbose: print 'extension = ', extension # load data fits = pyfits.fitsopen(filename) hdu = fits[extension] data = hdu.data if not (isinstance(hdu, pyfits.BinTableHDU) or isinstance(hdu, pyfits.TableHDU)): # this is an "image" return data # else this is a table (ASCII or Binary) header = hdu.header # create dictionnary mydict = dict.fromkeys(data._names) # beware order is not kept # fill the dictionnary for i, name in enumerate(data._names): mydim = "tdim{0}".format(i + 1) if header.has_key(mydim): myshape = header[mydim] # n = [int(i) for i in header.strip('()').split(',')] exec('myshape = ' + header[mydim]) # need to reverse shape due to FITS convention # (quick axis first in FITS and last in Python) myshape = myshape[::-1] mydict[name] = data.field(name).reshape(myshape) if transpose: mydict[name] = mydict[name].transpose() else: mydict[name] = data.field(name).ravel() # # check if verbose: for k, v in mydict.iteritems(): print k, v[0:1] # return mydict
def mrdfits(filename, extension=1, transpose=False, verbose=False): """ Read a an extended fits file. Try to emulate mrdfits.pro Arguments --------- filename: string extension: int transpose: bool verbose: bool Returns ------- A dictionary of arrays """ if verbose: print 'extension = ', extension # load data fits = pyfits.fitsopen(filename) hdu = fits[extension] data = hdu.data if not (isinstance(hdu, pyfits.BinTableHDU) or isinstance(hdu, pyfits.TableHDU)): # this is an "image" return data # else this is a table (ASCII or Binary) header = hdu.header # create dictionnary mydict = dict.fromkeys(data._names) # beware order is not kept # fill the dictionnary for i, name in enumerate(data._names): mydim = "tdim{0}".format(i+1) if header.has_key(mydim): myshape = header[mydim] # n = [int(i) for i in header.strip('()').split(',')] exec('myshape = ' + header[mydim]) # need to reverse shape due to FITS convention # (quick axis first in FITS and last in Python) myshape = myshape[::-1] mydict[name] = data.field(name).reshape(myshape) if transpose: mydict[name] = mydict[name].transpose() else: mydict[name] = data.field(name).ravel() # # check if verbose: for k, v in mydict.iteritems(): print k, v[0:1] # return mydict
def fitsfilter(filenames, test_str, ext=0): """ Filter a list of fits filenames checking a test string. Arguments: ---------- filenames: (list of strings) list of string corresponding to fits filenames. test_str: (string) a test string of the form : (a[NAXIS] == 3 and a[NAXIS1] == 1024) or a[NAXIS2] == 2 Returns: -------- out_files: (list of strings) list of string corresponding to fits filenames verifying the test. Notes: ------ The use of """ out_files = list() # parse filenames for f in filenames: test = True try: a = pyfits.fitsopen(f)[ext].header # file does not exist except IOError: test = False # extension does not exist except IndexError: test = False if "a" in locals(): try: test_str = add_quotes_to_str(test_str) test_str = add_quotes_to_keys(test_str) exec_str = " ".join(("if", "not", "(" + test_str + "):", "test = False")) exec (exec_str) # one of the key does not exist except KeyError: test = False if test: out_files.append(f) return out_files
def file_to_config(filename, config_filename=None): """ Convert info from header into configuration for srt inversion. Arguments --------- filename (str): The filename of the fits file (output of srt). config_filename (str, optional): If provided, the config is saved into this file. Returns ------- config (ConfigParser.RawConfigParser instance): A configuration instance, only if config_filename is not provided. """ import pyfits import ConfigParser # read data h = dict(pyfits.fitsopen(filename)[0].header) # convert to dictionaries obj_params, data_params, opt_params, mask_params = params_from_header(h) # generate config config = ConfigParser.RawConfigParser() # define sections config.add_section("object") config.add_section("data") config.add_section("masking") config.add_section("optimization") # fill in sections for k in obj_params: config.set("object", k, obj_params[k]) for k in data_params: config.set("data", k, data_params[k]) for k in opt_params: config.set("optimization", k, opt_params[k]) for k in mask_params: config.set("masking", k, mask_params[k]) if config_filename is not None: fp = file(config_filename, "w") config.write(fp) fp.close() return config
def read_data(path, dtype=np.float64, bin_factor=None, **kargs): """ Read SOHO / STEREO data files and output a Data instance Input : path : path of the data set dtype : cast of the data array kargs : arguments of the data filtering """ if not os.path.isdir(path): raise ValueError('Directory does not exist') # read files fnames = os.listdir(path) files = list() for fname in fnames: try: files.append(pyfits.fitsopen(os.path.join(path, fname))[0]) except (IOError): pass files = filter_files(files, **kargs) if len(files) == 0: print("No file matching.") return None for i, f in enumerate(files): fits_array = fa.hdu2fitsarray(f) if bin_factor is not None: fits_array = fits_array.bin(bin_factor) update_header(fits_array) fits_array = fits_array.T if i == 0: data = fa.InfoArray(fits_array.shape + (len(files), ), header=[ dict(fits_array.header), ]) data[..., i] = fits_array if i != 0: data.header.append(dict(fits_array.header)) # ensure coherent data type data = data.astype(dtype) for i in xrange(data.shape[-1]): data.header[i]['BITPIX'] = fa.bitpix_inv[dtype.__name__] return data
def filtersort(of,files): # sort by filters.. flats = {} for x in files: f = pyfits.fitsopen(x) filt = f[0].header['FILTER'] try: flats[filt].append(x) except KeyError: flats[filt] = [x] print '\rflats ', for y in flats.keys(): print '%s : %03d' % (y,len(flats[y])), for x in flats.keys(): print '' print 'processing filter %s with %d images' % (x,len(flats[x])) median(of + x + '.fits', flats[x])
def show_fits(filename, ext=None, outfile=None): # imports import matplotlib.pylab as mp import pyfits # open file fits = pyfits.fitsopen(filename) if ext is None: # display the first image-like extension for e in xrange(len(fits)): a = fits[e].data if a.ndim == 2: break else: a = fits[ext].data mp.imshow(a) if outfile is None: mp.show() else: mp.savefig(outfile)
def read_data(path, dtype=np.float64, bin_factor=None, **kargs): """ Read SOHO / STEREO data files and output a Data instance Input : path : path of the data set dtype : cast of the data array kargs : arguments of the data filtering """ if not os.path.isdir(path): raise ValueError('Directory does not exist') # read files fnames = os.listdir(path) files = list() for fname in fnames: try: files.append(pyfits.fitsopen(os.path.join(path, fname))[0]) except(IOError): pass files = filter_files(files, **kargs) if len(files) == 0: print("No file matching.") return None for i, f in enumerate(files): fits_array = fa.hdu2fitsarray(f) if bin_factor is not None: fits_array = fits_array.bin(bin_factor) update_header(fits_array) fits_array = fits_array.T if i == 0: data = fa.InfoArray(fits_array.shape + (len(files),), header=[dict(fits_array.header),]) data[..., i] = fits_array if i != 0: data.header.append(dict(fits_array.header)) # ensure coherent data type data = data.astype(dtype) for i in xrange(data.shape[-1]): data.header[i]['BITPIX'] = fa.bitpix_inv[dtype.__name__] return data
def print_header(filename, ext=None, rec=None): """ Print header to standard output. Arguments --------- fits : string The filename of a fits file. ext : optional int (default: None) The extension number. If not given, print all extensions. rec: optional string (default: None) Record names to look up. If not given, print all records. Returns ------- Nothing. Prints header to standard output. """ # load file try: fits = pyfits.fitsopen(filename) except (IOError): print("File " + filename + " not a fits file or does not exist.") return # check extension n_ext = len(fits) if len(ext) == 0: ext = xrange(n_ext) # print headers for e in ext: if isinstance(ext, xrange) and not n_ext == 1: print("-" * LINE_WIDTH) print("Extension number " + str(e)) print("-" * LINE_WIDTH) # check records replace_rec = False if len(rec) == 0: crec = fits[e].header.keys() else: crec = rec for r in crec: print(r + " " * (KEY_WIDTH - len(r)) + '\t' + str(fits[e].header[r]))
def print_header(filename, ext=None, rec=None): """ Print header to standard output. Arguments --------- fits : string The filename of a fits file. ext : optional int (default: None) The extension number. If not given, print all extensions. rec: optional string (default: None) Record names to look up. If not given, print all records. Returns ------- Nothing. Prints header to standard output. """ # load file try: fits = pyfits.fitsopen(filename) except(IOError): print("File " + filename + " not a fits file or does not exist.") return # check extension n_ext = len(fits) if len(ext) == 0: ext = xrange(n_ext) # print headers for e in ext: if isinstance(ext, xrange) and not n_ext==1: print("-" * LINE_WIDTH) print("Extension number " + str(e)) print("-" * LINE_WIDTH) # check records replace_rec = False if len(rec) == 0: crec = fits[e].header.keys() else: crec = rec for r in crec: print(r + " " * (KEY_WIDTH - len(r)) +'\t' + str(fits[e].header[r]))
#!/usr/bin/python # Test focusing infrastrucure import focusing import sys import pyfits from scipy import * from pylab import * tries = {} for fn in sys.argv[1:]: ff = pyfits.fitsopen(fn) tries[float(ff[0].header['FOC_POS'])]=fn f = focusing.Focusing() b,ftype = f.findBestFWHM(tries) # for way off-focus, low S/N images.. #b,ftype = f.findBestFWHM(tries,min_stars=10,filterGalaxies=False,threshold=5) print b f.plotFit(b,ftype)
def __init__(self, todfile, invnttfile, mapmaskfile, convert, ndetectors, missing_value=None, comm_tod=None): # Get information from files nslices, status = tmf.madmap1_nslices(invnttfile, ndetectors) if status != 0: raise RuntimeError() npixels_per_sample, nsamples, ncorrelations, status = tmf.madmap1_info(\ todfile, invnttfile, convert, ndetectors, nslices) if status != 0: raise RuntimeError() m=re.search(r'(?P<filename>.*)\[(?P<extname>\w+)\]$', mapmaskfile) if m is None: mask = pyfits.fitsopen(mapmaskfile)[0].data else: filename = m.group('filename') extname = m.group('extname') mask = pyfits.fitsopen(filename)[extname].data if mask is None: raise IOError('HDU '+mapmaskfile+' has no data.') mapmask = np.zeros(mask.shape, dtype='int8') if missing_value is None: mapmask[mask != 0] = 1 elif np.isnan(missing_value): mapmask[np.isnan(mask)] = 1 elif np.isinf(missing_value): mapmask[np.isinf(mask)] = 1 else: mapmask[mask == missing_value] = 1 # Store instrument information self.instrument = Instrument('Unknown', (ndetectors,)) # Store observation information class MadMap1ObservationInfo(object): pass self.info = MadMap1ObservationInfo() self.info.todfile = todfile self.info.invnttfile = invnttfile self.info.ncorrelations = ncorrelations self.info.npixels_per_sample = npixels_per_sample self.info.mapmaskfile = mapmaskfile self.info.convert = convert self.info.missing_value = missing_value self.info.mapmask = mapmask # Store slice information self.slice = np.recarray(nslices, dtype=[('nsamples_all', int), ('invnttfile', 'S256')]) self.slice.nsamples_all = nsamples self.slice.nfinesamples = nsamples self.slice.invnttfile = [invnttfile+'.'+str(i) for i in range(nslices)] # Store pointing information self.pointing = np.recarray(np.sum(nsamples), [('removed', np.bool_)]) self.pointing.removed = False # Store communicator information self.comm_tod = comm_tod or var.comm_tod if self.comm_tod.Get_size() > 1: raise NotImplementedError('The parallelisation of the TOD is not ' \ 'implemented')
#labels, nlabels = ndi.measurements.label(roi) # select biggest region #region_size = [np.sum(labels == i) for i in xrange(1, nlabels)] #biggest_index = np.where(region_size == np.max(region_size))[0][0] + 1 #roi = labels == biggest_index roi = np.ones((192, 192)) # intiat list of radial profiles phot_tables = [] pix_tables = [] p = [] datas = [] headers = [] for i, filename in enumerate(filenames): # read data p.append(pyfits.fitsopen(filename)[0]) datas.append(np.ma.MaskedArray(p[-1].data.T, mask=(1 - roi))) if i==0: # backprojection normalize datas[-1] /= 8 headers.append(p[-1].header) # run radial_profile radii, phot_table, pix_table = radial_profile(datas[-1], headers[-1], center, incl, pa) phot_tables.append(phot_table) pix_tables.append(pix_table) # compute background bkg_roi = np.zeros(datas[0].shape, dtype=np.bool8) bkg_roi[0:40, 0:50] = 1. bkg_roi[0:50, 150:-1] = 1.
#!/usr/bin/env python import getopt, sys, os import numpy as np import pyfits from pylab import matplotlib import matplotlib.pyplot as plt from mpl_toolkits.axes_grid.inset_locator import zoomed_inset_axes from mpl_toolkits.axes_grid.inset_locator import mark_inset #fname_ext = '/home/nbarbey/data/csh/output/ngc6946_cross_robust.fits' fname_ext = sys.argv[1] fname = fname_ext.split('.')[0] out_fname = fname + '.png' print('displaying ' + fname) title_str = fname.split(os.sep)[-1] t = np.flipud(pyfits.fitsopen(fname_ext)[0].data.T) fig = plt.figure(1, [5,4]) ax = fig.add_subplot(111) #imshow(t , interpolation="nearest") #imshow((t - t.min())) ** .25, interpolation="nearest") tt = t ** .25 tt[np.isnan(tt)] = 0 extent = [0., 192., 0., 192.] ax.imshow(tt, extent=extent, interpolation="nearest") tzoom = tt[135:155, 80:100,] axins = zoomed_inset_axes(ax, 2, loc=3) # zoom = 6 extent = [80., 100., 192. - 155., 192. - 135, ] im = axins.imshow(tzoom, extent=extent, interpolation="nearest") im.set_clim([tt.min(), tt.max()])
def __init__(self, todfile, invnttfile, mapmaskfile, convert, ndetectors, missing_value=None, comm_tod=None): # Get information from files nslices, status = tmf.madmap1_nslices(invnttfile, ndetectors) if status != 0: raise RuntimeError() npixels_per_sample, nsamples, ncorrelations, status = tmf.madmap1_info(\ todfile, invnttfile, convert, ndetectors, nslices) if status != 0: raise RuntimeError() m = re.search(r'(?P<filename>.*)\[(?P<extname>\w+)\]$', mapmaskfile) if m is None: mask = pyfits.fitsopen(mapmaskfile)[0].data else: filename = m.group('filename') extname = m.group('extname') mask = pyfits.fitsopen(filename)[extname].data if mask is None: raise IOError('HDU ' + mapmaskfile + ' has no data.') mapmask = np.zeros(mask.shape, dtype='int8') if missing_value is None: mapmask[mask != 0] = 1 elif np.isnan(missing_value): mapmask[np.isnan(mask)] = 1 elif np.isinf(missing_value): mapmask[np.isinf(mask)] = 1 else: mapmask[mask == missing_value] = 1 # Store instrument information self.instrument = Instrument('Unknown', (ndetectors, )) # Store observation information class MadMap1ObservationInfo(object): pass self.info = MadMap1ObservationInfo() self.info.todfile = todfile self.info.invnttfile = invnttfile self.info.ncorrelations = ncorrelations self.info.npixels_per_sample = npixels_per_sample self.info.mapmaskfile = mapmaskfile self.info.convert = convert self.info.missing_value = missing_value self.info.mapmask = mapmask # Store slice information self.slice = np.recarray(nslices, dtype=[('nsamples_all', int), ('invnttfile', 'S256')]) self.slice.nsamples_all = nsamples self.slice.nfinesamples = nsamples self.slice.invnttfile = [ invnttfile + '.' + str(i) for i in range(nslices) ] # Store pointing information self.pointing = np.recarray(np.sum(nsamples), [('removed', np.bool_)]) self.pointing.removed = False # Store communicator information self.comm_tod = comm_tod or var.comm_tod if self.comm_tod.Get_size() > 1: raise NotImplementedError('The parallelisation of the TOD is not ' \ 'implemented')
#!/usr/bin/env python import pyfits from pylab import matplotlib from matplotlib.pyplot import * from numpy import * import getopt, sys, os fname_ext = sys.argv[1] fname = fname_ext.split(".")[0] out_fname = fname + ".png" print("displaying " + fname) title_str = fname.split(os.sep)[-1] t = pyfits.fitsopen(fname_ext)[0].data h = figure() # imshow(flipud(t.T) , interpolation="nearest") # imshow(flipud(t.T) ** .25, interpolation="nearest") imshow(flipud((t - t.min()).T) ** 0.25, interpolation="nearest") colorbar() title(title_str) xlabel("Right Ascension") ylabel("Declination") show() h.savefig(out_fname)
#!/usr/bin/python # test xy2wcs - compares what we get using ds9 and xy2wcs algorithm from rts2.astrometry import xy2wcs import sys import ds9 import pyfits import re d=ds9.ds9('XY2WCS') for x in sys.argv[1:]: ff = pyfits.fitsopen(x,'readonly') fh=ff[0].header ff.close() d.set('file {0}'.format(x)) d.set('regions system wcs') xmax = fh['NAXIS1'] ymax = fh['NAXIS2'] # pixels to check xy = [[0,0],[0,ymax],[xmax,0],[xmax,ymax],[xmax/2.0,ymax/2.0]] pmatch = re.compile('point\(([^,]*),([^)]*)\)') for p in xy: d.set('regions delete all') d.set('regions','image; point {0} {1} # point=cross'.format(p[0],p[1])) r=d.get('regions') radec = xy2wcs(p[0],p[1],fh) for l in r.split('\n'): match = pmatch.match(l) if match:
#!/usr/bin/python # test xy2wcs - compares what we get using ds9 and xy2wcs algorithm from rts2.astrometry import xy2wcs import sys import ds9 import pyfits import re d = ds9.ds9('XY2WCS') for x in sys.argv[1:]: ff = pyfits.fitsopen(x, 'readonly') fh = ff[0].header ff.close() d.set('file {0}'.format(x)) d.set('regions system wcs') xmax = fh['NAXIS1'] ymax = fh['NAXIS2'] # pixels to check xy = [[0, 0], [0, ymax], [xmax, 0], [xmax, ymax], [xmax / 2.0, ymax / 2.0]] pmatch = re.compile('point\(([^,]*),([^)]*)\)') for p in xy: d.set('regions delete all') d.set('regions', 'image; point {0} {1} # point=cross'.format(p[0], p[1])) r = d.get('regions') radec = xy2wcs(p[0], p[1], fh) for l in r.split('\n'): match = pmatch.match(l)
#!/usr/bin/python # Test focusing infrastrucure import focusing import sys import pyfits from scipy import * from pylab import * tries = {} for fn in sys.argv[1:]: ff = pyfits.fitsopen(fn) tries[float(ff[0].header['FOC_POS'])] = fn f = focusing.Focusing() b, ftype = f.findBestFWHM(tries) # for way off-focus, low S/N images.. #b,ftype = f.findBestFWHM(tries,min_stars=10,filterGalaxies=False,threshold=5) print b f.plotFit(b, ftype)
#!/usr/bin/env python import numpy as np import pyfits import os import fht import pylab as pl from mpl_toolkits.axes_grid import AxesGrid # data datadir = os.getenv('CSH_DATA') filename = datadir + '/../lowfreq/1342182424_blue_level0Frames.fits' data = pyfits.fitsopen(filename)[1].data[..., 50000:60000] idx = 2000 im0 = data[..., idx].copy() m = np.mean(data, axis=-1) for i in xrange(data.shape[-1]): data[..., i] -= m im = data[..., idx] im_fht = fht.fht(im0) im_fht[0, 0] = 0. # figure fig = pl.figure() pl.gray() grid = AxesGrid(fig, 111, nrows_ncols=(3, 1), axes_pad=0.0, share_all=True, # cbar_mode="each",
class TestFailure(Exception): pass tamasis.var.verbose = True path = os.path.abspath(os.path.dirname(__file__)) + '/data/madmap1/' obs = MadMap1Observation(path+'todSpirePsw_be', path+'invnttSpirePsw_be', path+'madmapSpirePsw.fits[coverage]', 'big_endian', 135, missing_value=np.nan) obs.instrument.name = 'SPIRE/PSW' tod = obs.get_tod(unit='Jy/beam') projection = Projection(obs) packing = Unpacking(obs.info.mapmask, field=np.nan).T map_naive = mapper_naive(tod, projection) map_naive = mapper_naive(tod, projection*packing) map_ref = pyfits.fitsopen(path+'naivemapSpirePsw.fits')['image'].data if any_neq(map_naive,map_ref): raise TestFailure('mapper_naive madcap 1') map_naive_1d = mapper_naive(tod, projection) map_naive_2d = packing.T(map_naive_1d) if any_neq(map_naive_2d, map_ref): raise TestFailure('mapper_naive madcap 2') packing = Unpacking(obs.info.mapmask).T #M = 1 / map_naive.coverage #M[~np.isfinite(M)] = np.max(M[np.isfinite(M)]) #map_rlsw1 = mapper_rls(tod, projection*packing, padding.T * fft.T * invNtt * fft * padding, hyper=0, tol=1.e-5, M=M, solver=cg) #print('Elapsed time: ' + str(map_rlsw1.header['time'])) M = Diagonal(packing(1/map_naive.coverage)) if np.any(~np.isfinite(M.data)):
def processImage(fn, d, obs_id=None, threshold=2.7, pr=False, ds9cat=None, bysegments=False, stars=[]): """Process image, print its FWHM. Works with multi extension images. """ ff = pyfits.fitsopen(fn) rts2 = rts2comm.Rts2Comm() if d: d.set('file mosaicimage iraf ' + fn) sexcols = [ 'X_IMAGE', 'Y_IMAGE', 'MAG_BEST', 'FLAGS', 'CLASS_STAR', 'FWHM_IMAGE', 'A_IMAGE', 'B_IMAGE', 'EXT_NUMBER', 'FLUX_BEST', 'BACKGROUND' ] c = sextractor.Sextractor( sexcols, threshold=threshold, sexconfig='/home/observer/findfwhm/Sextractor/focus.sex', starnnw='/home/observer/findfwhm/Sextractor/default.nnw') c.runSExtractor(fn) c.sortObjects(2) for st in stars: # append distance - none and star number - to star list st.append(None) st.append(None) # dump Sextractor to DS9 catalogue if ds9cat: cat = open(ds9cat, 'w') cat.write('\t'.join(sexcols) + '\n') for x in c.objects: cat.write('\t'.join(map(lambda y: str(y), x)) + '\n') cat.close() seg_fwhms = map(lambda x: FWHM(), range(0, len(ff) + 1)) for x in c.objects: if pr: print '\t'.join(map(lambda y: str(y), x)) segnum = int(x[8]) for st in stars: if st[0] == segnum: dist = math.sqrt((x[0] - st[1])**2 + (x[1] - st[2])**2) if st[4] is None or st[4] > dist: st[4] = dist st[5] = x if x[3] == 0 and x[4] != 0: if d: d.set( 'regions', 'tile {0}\nimage; circle {1} {2} 10 # color=green'.format( segnum, x[0], x[1])) seg_fwhms[0].addFWHM(x[5], x[6], x[7]) seg_fwhms[segnum].addFWHM(x[5], x[6], x[7]) elif d: d.set( 'regions', '# tile {0}\nphysical; point {1} {2} # point = x 5 color=red'. format(segnum, x[0], x[1])) # average results map(FWHM.average, seg_fwhms) # default suffix defsuffix = '_KCAM' try: defsuffix = '_' + ff[0].header['CCD_NAME'] except KeyError, er: pass
#!/usr/bin/env python import os import numpy as np import pyfits version = '1.0.3' filename = os.path.join(os.getenv('HOME'), 'projets', 'tamasis-' + version, 'pacs', 'data', 'PCalPhotometer_BadPixelMask_FM_v5.fits') # save a backup os.system('cp ' + filename + ' ' + filename + '.bak') # load file fits = pyfits.fitsopen(filename) # put zeros into data for f in fits[1:]: f.data = np.zeros(f.data.shape) # remove old file os.system('rm -f ' + filename) # save file fits.writeto(filename)
data_path + 'ngc6946_cross_robust_noc.fits', data_path + 'ngc6946_cross_robust_ca.fits', data_path + 'ngc6946_cross_robust_cs.fits', data_path + 'ngc6946_cross_robust.fits', ] letters = ['a', 'b', 'c', 'd', 'e'] letters = ['(' + l + ')' for l in letters] markers = ['-o', '-v', '-s', '-p', '-*'] out_fname = data_path + 'ngc6946_cross_robust_line_cut_grid' + '.png' # create grid # figure fig = plt.figure() labels = ['PL', 'PLI', 'ACI', 'HCI', 'NOC'] labels = ['(' + l + ')' for l in labels] # loop on each map for i, fname in enumerate(fname_ext): data = np.flipud(pyfits.fitsopen(fname)[0].data.T) if i == 0: data /= 8. data[np.isnan(data)] = 0 plt.plot(linear_cut(data, xmin=153, xmax=136, ymin=85, ymax=95, N=20), markers[i], label=labels[i]) plt.legend() plt.show() fig.savefig(out_fname)
# cleanup shutil.rmtree(self.odir) return ret if __name__ == '__main__': if len(sys.argv) <= 2: print 'Usage: %s <odir> <fits filename> <xoffs> <yoffs> <origname> <obs_id> <imgid>' % (sys.argv[0]) sys.exit(1) a = imgAstrometryScript(sys.argv[2],sys.argv[1]) ra = dec = None ff=pyfits.fitsopen(sys.argv[2],'readonly') ra=ff[0].header['RA'] dec=ff[0].header['DEC'] object=ff[0].header['OBJECT'] ff.close() xoffs = 0 yoffs = 0 if len(sys.argv) >= 5: xoffs = float(sys.argv[3]) yoffs = float(sys.argv[4]) num = -1 try: fn = os.path.basename(sys.argv[5])
] letters = ['a', 'b', 'c', 'd', 'e'] letters = ['(' + l + ')' for l in letters] out_fname = data_path + 'ngc6946_cross_robust_maps_grid' + '.png' # create grid # figure #plt.gray() fig = plt.figure() grid = AxesGrid(fig, 111, nrows_ncols=(3, 2), axes_pad=0.0, share_all=True, ) # loop on each map for i, fname in enumerate(fname_ext): data = np.flipud(pyfits.fitsopen(fname)[0].data.T) if i == 0: data /= 8. data = data ** .25 data[np.isnan(data)] = 0 extent = [0., 192., 0., 192.] im = grid[i].imshow(data, extent=extent, interpolation="nearest") grid[i].text(10, 170, letters[i], fontsize=20, color="white") data_zoom = data[135:155, 80:100,] axins = zoomed_inset_axes(grid[i], 2, loc=3) # zoom = 6 extent = [80., 100., 192. - 155., 192. - 135, ] im2 = axins.imshow(data_zoom, extent=extent, interpolation="nearest") im2.set_clim([data.min(), data.max()]) plt.xticks(visible=False) plt.yticks(visible=False) im.set_clim([0., .3])
#!/usr/bin/env python import os import pyfits from tamasis import * import lo from csh import * # define data set datadir = os.getenv('CSH_DATA') ids = ['1342184518', '1342184519', '1342184596', '1342184597', '1342184598', '1342184599'] filenames = [os.path.join(datadir, id_str + '_blue_PreparedFrames.fits') for id_str in ids] pacs = PacsObservation(filename=filenames, fine_sampling_factor=1, keep_bad_detectors=False) # get header from altieri maps header = pyfits.fitsopen('/mnt/herschel1/mapmaking/nbarbey/Abell2218_altieri/' + 'a2218_red_Map.v2.2.sci.fits')[0].header # data tod = pacs.get_tod() # remove bad pixels (by updating mask !) #tod = remove_bad_pixels(tod) # deglitching projection = Projection(pacs, header=header, resolution=3., npixels_per_sample=6) tod.mask = deglitch_l2mad(tod, projection) # model masking = Masking(tod.mask) model = masking * projection # remove drift tod = filter_median(tod, length=49) # first map backmap = model.transpose(tod) P = lo.aslinearoperator(model.aslinearoperator())
#!/usr/bin/env python import numpy as np import pyfits import os import fht import pylab as pl from mpl_toolkits.axes_grid import AxesGrid # data datadir = os.getenv('CSH_DATA') filename = datadir + '/../lowfreq/1342182424_blue_level0Frames.fits' data = pyfits.fitsopen(filename)[1].data[..., 50000:60000] idx = 2000 im0 = data[..., idx].copy() m = np.mean(data, axis=-1) for i in xrange(data.shape[-1]): data[..., i] -= m im = data[..., idx] im_fht = fht.fht(im0) im_fht[0, 0] = 0. # figure fig = pl.figure() pl.gray() grid = AxesGrid( fig, 111, nrows_ncols=(3, 1),
#!/usr/bin/env python import pyfits from pylab import matplotlib from matplotlib.pyplot import * from numpy import * import getopt, sys, os fname_ext = sys.argv[1] fname = fname_ext.split('.')[0] out_fname = fname + '.png' print('displaying ' + fname) title_str = fname.split(os.sep)[-1] t = pyfits.fitsopen(fname_ext)[0].data h = figure() #imshow(flipud(t.T) , interpolation="nearest") #imshow(flipud(t.T) ** .25, interpolation="nearest") imshow(flipud((t - t.min()).T) ** .25, interpolation="nearest") colorbar() title(title_str) xlabel('Right Ascension') ylabel('Declination') show() h.savefig(out_fname)
#labels, nlabels = ndi.measurements.label(roi) # select biggest region #region_size = [np.sum(labels == i) for i in xrange(1, nlabels)] #biggest_index = np.where(region_size == np.max(region_size))[0][0] + 1 #roi = labels == biggest_index roi = np.ones((192, 192)) # intiat list of radial profiles phot_tables = [] pix_tables = [] p = [] datas = [] headers = [] for i, filename in enumerate(filenames): # read data p.append(pyfits.fitsopen(filename)[0]) datas.append(np.ma.MaskedArray(p[-1].data.T, mask=(1 - roi))) if i == 0: # backprojection normalize datas[-1] /= 8 headers.append(p[-1].header) # run radial_profile radii, phot_table, pix_table = radial_profile(datas[-1], headers[-1], center, incl, pa) phot_tables.append(phot_table) pix_tables.append(pix_table) # compute background bkg_roi = np.zeros(datas[0].shape, dtype=np.bool8) bkg_roi[0:40, 0:50] = 1. bkg_roi[0:50, 150:-1] = 1.
path = os.path.abspath(os.path.dirname(__file__)) + '/data/madmap1/' obs = MadMap1Observation(path + 'todSpirePsw_be', path + 'invnttSpirePsw_be', path + 'madmapSpirePsw.fits[coverage]', 'big_endian', 135, missing_value=np.nan) obs.instrument.name = 'SPIRE/PSW' tod = obs.get_tod(unit='Jy/beam') projection = Projection(obs) packing = Unpacking(obs.info.mapmask, field=np.nan).T map_naive = mapper_naive(tod, projection) map_naive = mapper_naive(tod, projection * packing) map_ref = pyfits.fitsopen(path + 'naivemapSpirePsw.fits')['image'].data if any_neq(map_naive, map_ref): raise TestFailure('mapper_naive madcap 1') map_naive_1d = mapper_naive(tod, projection) map_naive_2d = packing.T(map_naive_1d) if any_neq(map_naive_2d, map_ref): raise TestFailure('mapper_naive madcap 2') packing = Unpacking(obs.info.mapmask).T #M = 1 / map_naive.coverage #M[~np.isfinite(M)] = np.max(M[np.isfinite(M)]) #map_rlsw1 = mapper_rls(tod, projection*packing, padding.T * fft.T * invNtt * fft * padding, hyper=0, tol=1.e-5, M=M, solver=cg) #print('Elapsed time: ' + str(map_rlsw1.header['time'])) M = Diagonal(packing(1 / map_naive.coverage)) if np.any(~np.isfinite(M.data)):
# define data set datadir = os.getenv('CSH_DATA') ids = [ '1342184518', '1342184519', '1342184596', '1342184597', '1342184598', '1342184599' ] filenames = [ os.path.join(datadir, id_str + '_blue_PreparedFrames.fits') for id_str in ids ] pacs = PacsObservation(filename=filenames, fine_sampling_factor=1, keep_bad_detectors=False) # get header from altieri maps header = pyfits.fitsopen( '/mnt/herschel1/mapmaking/nbarbey/Abell2218_altieri/' + 'a2218_red_Map.v2.2.sci.fits')[0].header # data tod = pacs.get_tod() # remove bad pixels (by updating mask !) #tod = remove_bad_pixels(tod) # deglitching projection = Projection(pacs, header=header, resolution=3., npixels_per_sample=6) tod.mask = deglitch_l2mad(tod, projection) # model masking = Masking(tod.mask) model = masking * projection # remove drift
shutil.rmtree(self.odir) return ret if __name__ == '__main__': if len(sys.argv) <= 2: print 'Usage: %s <odir> <fits filename> <xoffs> <yoffs> <origname> <obs_id> <imgid>' % ( sys.argv[0]) sys.exit(1) a = imgAstrometryScript(sys.argv[2], sys.argv[1]) ra = dec = None ff = pyfits.fitsopen(sys.argv[2], 'readonly') ra = ff[0].header['RA'] dec = ff[0].header['DEC'] object = ff[0].header['OBJECT'] ff.close() xoffs = 0 yoffs = 0 if len(sys.argv) >= 5: xoffs = float(sys.argv[3]) yoffs = float(sys.argv[4]) num = -1 try: fn = os.path.basename(sys.argv[5])