def copy_ccube(ccube, outsrcmap, hpx_order): """Copy a counts cube into outsrcmap file reducing the HEALPix order to hpx_order if needed. """ sys.stdout.write(" Copying counts cube from %s to %s\n" % (ccube, outsrcmap)) try: hdulist_in = fits.open(ccube) except IOError: hdulist_in = fits.open("%s.gz" % ccube) hpx_order_in = hdulist_in[1].header['ORDER'] if hpx_order_in > hpx_order: hpxmap = HpxMap.create_from_hdulist(hdulist_in) hpxmap_out = hpxmap.ud_grade(hpx_order, preserve_counts=True) hpxlist_out = hdulist_in #hpxlist_out['SKYMAP'] = hpxmap_out.create_image_hdu() hpxlist_out[1] = hpxmap_out.create_image_hdu() hpxlist_out[1].name = 'SKYMAP' hpxlist_out.writeto(outsrcmap) return hpx_order else: os.system('cp %s.gz %s.gz' % (ccube, outsrcmap)) os.system('gunzip -f %s.gz' % (outsrcmap)) return None
def copy_ccube(ccube, outsrcmap, hpx_order): """Copy a counts cube into outsrcmap file reducing the HEALPix order to hpx_order if needed. """ sys.stdout.write(" Copying counts cube from %s to %s\n" % (ccube, outsrcmap)) try: hdulist_in = fits.open(ccube) except IOError: hdulist_in = fits.open("%s.gz" % ccube) hpx_order_in = hdulist_in[1].header['ORDER'] if hpx_order_in > hpx_order: hpxmap = HpxMap.create_from_hdulist(hdulist_in) hpxmap_out = hpxmap.ud_grade(hpx_order, preserve_counts=True) hpxlist_out = hdulist_in #hpxlist_out['SKYMAP'] = hpxmap_out.create_image_hdu() hpxlist_out[1] = hpxmap_out.create_image_hdu() hpxlist_out[1].name = 'SKYMAP' hpxlist_out.writeto(outsrcmap) return hpx_order else: os.system('cp %s %s' % (ccube, outsrcmap)) #os.system('cp %s.gz %s.gz' % (ccube, outsrcmap)) #os.system('gunzip -f %s.gz' % (outsrcmap)) return None
def append_hdus(hdulist, srcmap_file, source_names, hpx_order): """Append HEALPix maps to a list Parameters ---------- hdulist : list The list being appended to srcmap_file : str Path to the file containing the HDUs source_names : list of str Names of the sources to extract from srcmap_file hpx_order : int Maximum order for maps """ sys.stdout.write(" Extracting %i sources from %s" % (len(source_names), srcmap_file)) try: hdulist_in = fits.open(srcmap_file) except IOError: try: hdulist_in = fits.open('%s.gz' % srcmap_file) except IOError: sys.stdout.write(" Missing file %s\n" % srcmap_file) return for source_name in source_names: sys.stdout.write('.') sys.stdout.flush() if hpx_order is None: hdulist.append(hdulist_in[source_name]) else: try: hpxmap = HpxMap.create_from_hdulist(hdulist_in, hdu=source_name) except IndexError: print(" Index error on source %s in file %s" % (source_name, srcmap_file)) continue except KeyError: print(" Key error on source %s in file %s" % (source_name, srcmap_file)) continue hpxmap_out = hpxmap.ud_grade(hpx_order, preserve_counts=True) hdulist.append(hpxmap_out.create_image_hdu(name=source_name)) sys.stdout.write("\n") hdulist.flush() hdulist_in.close()
def append_hdus(hdulist, srcmap_file, source_names, hpx_order): """Append HEALPix maps to a list Parameters ---------- hdulist : list The list being appended to srcmap_file : str Path to the file containing the HDUs source_names : list of str Names of the sources to extract from srcmap_file hpx_order : int Maximum order for maps """ sys.stdout.write(" Extracting %i sources from %s" % (len(source_names), srcmap_file)) try: hdulist_in = fits.open(srcmap_file) except IOError: try: hdulist_in = fits.open('%s.gz' % srcmap_file) except IOError: sys.stdout.write(" Missing file %s\n" % srcmap_file) return for source_name in source_names: sys.stdout.write('.') sys.stdout.flush() if hpx_order is None: hdulist.append(hdulist_in[source_name]) else: try: hpxmap = HpxMap.create_from_hdulist(hdulist_in, hdu=source_name) except IndexError: print(" Index error on source %s in file %s" % (source_name, srcmap_file)) continue except KeyError: print(" Key error on source %s in file %s" % (source_name, srcmap_file)) continue hpxmap_out = hpxmap.ud_grade(hpx_order, preserve_counts=True) hdulist.append(hpxmap_out.create_image_hdu(name=source_name)) sys.stdout.write("\n") hdulist.flush() hdulist_in.close()
def main(): import sys import argparse # Argument defintion usage = "usage: %(prog)s [options]" description = "Collect all the new source" parser = argparse.ArgumentParser(usage, description=__abstract__) parser.add_argument("-i", "--input", type=argparse.FileType('r'), required=True, help="Input file") parser.add_argument("-e", "--extension", type=str, default="SKYMAP", help="FITS HDU with HEALPix map") parser.add_argument("--ebin", type=str, default=None, help="Energy bin, integer or 'ALL'") parser.add_argument("--zscale", type=str, default='log', help="Scaling for color scale") parser.add_argument("--zmin", type=float, default=None, help="Minimum z-axis value") parser.add_argument("--zmax", type=float, default=None, help="Maximum z-axis value") parser.add_argument("-o", "--output", type=argparse.FileType('w'), help="Output file. Leave blank for interactive.") # Parse the command line args = parser.parse_args(sys.argv[1:]) # Get the model f = pf.open(args.input.name) # We need a better check maptype = "None" model_hdu = f[args.extension] hpxmap = HpxMap.create_from_hdulist(f, hdu=args.extension) outdata = [] if args.ebin == "ALL": wcsproj = hpxmap.hpx.make_wcs(naxis=2, proj='MOL', energies=None, oversample=2) mapping = HpxToWcsMapping(hpxmap.hpx, wcsproj) for i, data in enumerate(hpxmap.counts): ip = ImagePlotter(data=data, proj=hpxmap.hpx, mapping=mapping) fig = plt.figure(i) im, ax = ip.plot(zscale=args.zscale, vmin=args.zmin, vmax=args.zmax) outdata.append(fig) elif args.ebin is None: ip = ImagePlotter(data=hpxmap.counts, proj=hpxmap.hpx) im, ax = ip.plot(zscale=args.zscale, vmin=args.zmin, vmax=args.zmax) outdata.append((im, ax)) else: try: ibin = int(args.ebin) ip = ImagePlotter(data=hpxmap.counts[ibin], proj=hpxmap.hpx) im, ax = ip.plot(zscale=args.zscale, vmin=args.zmin, vmax=args.zmax) outdata.append((im, ax)) except: raise ValueError("--ebin argument must be an integer or 'ALL'") if args.output is None: plt.show() else: if len(outdata) == 1: plt.savefig(args.output.name) else: base, ext = os.path.splitext(args.output.name) for i, fig in enumerate(outdata): fig.savefig("%s_%02i%s" % (base, i, ext))
def main(): import sys import argparse # Argument defintion usage = "usage: %(prog)s [options]" description = "Collect all the new source" parser = argparse.ArgumentParser(usage,description=__abstract__) parser.add_argument("-i", "--input",type=argparse.FileType('r'),required=True, help="Input file") parser.add_argument("-e", "--extension",type=str,default="SKYMAP", help="FITS HDU with HEALPix map") parser.add_argument("--ebin",type=str,default=None, help="Energy bin, integer or 'ALL'") parser.add_argument("-o", "--output",type=argparse.FileType('w'), help="Output file. Leave blank for interactive.") # Parse the command line args = parser.parse_args(sys.argv[1:]) # Get the model f = pf.open(args.input.name) # We need a better check maptype = "None" model_hdu = f[args.extension] hpxmap = HpxMap.create_from_hdulist(f,extname=args.extension,ebounds="EBOUNDS") outdata = [] if args.ebin == "ALL": wcsproj = hpxmap.hpx.make_wcs(naxis=2,proj='AIT',energies=None,oversample=2) mapping = HpxToWcsMapping(hpxmap.hpx,wcsproj) for i,data in enumerate(hpxmap.counts): ip = ImagePlotter(data=data,proj=hpxmap.hpx,mapping=mapping) fig = plt.figure(i) im,ax = ip.plot(zscale='log') outdata.append(fig) elif args.ebin is None: ip = ImagePlotter(data=hpxmap.counts,proj=hpxmap.hpx) im,ax = ip.plot(zscale='log') outdata.append((im,ax)) else: try: ibin = int(args.ebin) ip = ImagePlotter(data=hpxmap.counts[ibin],proj=hpxmap.hpx) im,ax = ip.plot(zscale='log') outdata.append((im,ax)) except: print("--ebin argument must be an integer or 'ALL'") if args.output is None: plt.show() else: plt.savefig(args.output.name)
def merge_hpx_counts_cubes(filelist): """ Merge all the files in filelist, assuming that they HEALPix counts cubes """ out_prim = None out_skymap = None out_ebounds = None datalist_gti = [] exposure_sum = 0. nfiles = len(filelist) ngti = np.zeros(nfiles, int) out_name = None for i, filename in enumerate(filelist): fin = fits.open(filename) sys.stdout.write('.') sys.stdout.flush() if i == 0: out_prim = update_null_primary(fin[0], out_prim) out_name = fin[1].name map_in = HpxMap.create_from_hdulist(fin) out_skymap = update_hpx_skymap_allsky(map_in, out_skymap) if i == 0: try: out_ebounds = update_ebounds(fin["EBOUNDS"], out_ebounds) except KeyError: out_ebounds = update_energies(fin["ENERGIES"], out_ebounds) try: (gti_data, exposure, tstop) = extract_gti_data(fin["GTI"]) datalist_gti.append(gti_data) exposure_sum += exposure ngti[i] = len(gti_data) except KeyError: pass if i == 0: first = fin elif i == nfiles - 1: try: date_end = fin[0].header['DATE-END'] except KeyError: date_end = None else: fin.close() out_skymap_hdu = out_skymap.create_image_hdu("SKYMAP") hdulist = [out_prim, out_skymap_hdu, out_ebounds] if len(datalist_gti) > 0: out_gti = merge_all_gti_data(datalist_gti, ngti, first['GTI']) out_gti.header['EXPOSURE'] = exposure_sum out_gti.header['TSTOP'] = tstop hdulist.append(out_gti) for hdu in hdulist: if date_end: hdu.header['DATE-END'] = date_end out_prim.update_header() sys.stdout.write("!\n") return fits.HDUList(hdulist)
def merge_hpx_counts_cubes(filelist): """ Merge all the files in filelist, assuming that they HEALPix counts cubes """ out_prim = None out_skymap = None out_ebounds = None datalist_gti = [] exposure_sum = 0. nfiles = len(filelist) ngti = np.zeros(nfiles, int) out_name = None for i, filename in enumerate(filelist): fin = fits.open(filename) sys.stdout.write('.') sys.stdout.flush() if i == 0: out_prim = update_null_primary(fin[0], out_prim) out_name = fin[1].name map_in = HpxMap.create_from_hdulist(fin) out_skymap = update_hpx_skymap_allsky(map_in, out_skymap) if i == 0: try: out_ebounds = update_ebounds(fin["EBOUNDS"], out_ebounds) except KeyError: out_ebounds = update_energies(fin["ENERGIES"], out_ebounds) try: (gti_data, exposure, tstop) = extract_gti_data(fin["GTI"]) datalist_gti.append(gti_data) exposure_sum += exposure ngti[i] = len(gti_data) except KeyError: pass if i == 0: first = fin elif i == nfiles - 1: try: date_end = fin[0].header['DATE-END'] except KeyError: date_end = None else: fin.close() out_skymap_hdu = out_skymap.create_image_hdu("SKYMAP") hdulist = [out_prim, out_skymap_hdu, out_ebounds] if len(datalist_gti) > 0: out_gti = merge_all_gti_data(datalist_gti, ngti, first['GTI']) out_gti.header['EXPOSURE'] = exposure_sum out_gti.header['TSTOP'] = tstop hdulist.append(out_gti) for hdu in hdulist: if date_end: hdu.header['DATE-END'] = date_end out_prim.update_header() sys.stdout.write("!\n") return fits.HDUList(hdulist)