def load_skymap_fits(filename, hdu=None, keys=None, memmap=False, apply_units=False): """ Load a fits file containing a sky map. Arguments --------- filename : str Path to fits file hdu : int, optional If supplied, the data are extract from the given HDU index. keys : list of strings, optional If supplied, return only these keys in the output dictionary. Options are: T, Q, U, W. memmap : bool, optional Argument passed to astropy.io.fits.open. If True, the map is not read into memory, but only the required pixels are read when needed. Default: False. apply_units : bool, optional If True, and input maps have known units, multiply by the appropriate conversion factor to return maps in G3Units. Returns ------- a dictionary of maps keyed with e.g. 'T', 'Q', 'U' and 'W'. """ import astropy.io.fits assert (os.path.exists(filename)) map_type = None pol = None map_opts = {} output = {} # defaults for missing header entries pol_conv = None units = 'Tcmb' coord_ref = 'Equatorial' proj = 'Proj5' weighted = False alpha_center = None delta_center = None res = None xres = None unit_dict = { 'k_cmb': ('Tcmb', core.G3Units.K), 'kcmb': ('Tcmb', core.G3Units.K), 'kcmb^2': ('Tcmb', core.G3Units.K**2), } if keys is None: keys = ['T', 'Q', 'U', 'W'] with astropy.io.fits.open(filename, memmap=memmap) as hdulist: for hidx, H in enumerate(hdulist): hdr = H.header mtype = hdr.get('MAPTYPE', None) if mtype == 'FLAT' or 'PROJ' in hdr or 'WCSAXES' in hdr or 'CTYPE1' in hdr: # flat map if not map_type: map_type = 'flat' elif map_type != 'flat': raise ValueError( "Expected a {} sky map in HDU {}, found a flat map". format(map_type, hidx)) # expect that flat sky maps on disk are probably in IAU if not pol_conv: pol_conv = 'IAU' elif mtype == 'HEALPIX' or hdr.get( 'PIXTYPE', None) == 'HEALPIX' or 'NSIDE' in hdr: # healpix map if not map_type: map_type = 'healpix' elif map_type != 'healpix': raise ValueError( "Expected a {} sky map in HDU {}, found a healpix map". format(map_type, hidx)) # expect that healpix maps on disk are probably in COSMO if not pol_conv: pol_conv = 'COSMO' if 'POLAR' in hdr: pdict = {'T': True, 'F': False} pol = pdict.get(hdr['POLAR'], hdr['POLAR']) if pol and map_type is not None: if hdr.get('POLCCONV', '').upper() not in ['IAU', 'COSMO']: core.log_warn( 'Polarization convention not set, assuming %s' % pol_conv, unit='load_skymap_fits') else: pol_conv = hdr['POLCCONV'].upper() uconv = None if 'TUNIT' in hdr: u = hdr['TUNIT'].lower() if u in unit_dict: units, uconv = unit_dict[u] else: units = hdr.get('UNITS', units) if 'COORDSYS' in hdr: cdict = {'C': 'Equatorial', 'G': 'Galactic', 'L': 'Local'} coord_ref = cdict.get(hdr['COORDSYS'], coord_ref) else: coord_ref = hdr.get('COORDREF', coord_ref) overflow = hdr.get('OVERFLOW', 0) map_opts.update( units=getattr(core.G3TimestreamUnits, units), coord_ref=getattr(MapCoordReference, coord_ref), pol_conv=getattr(MapPolConv, pol_conv) if pol_conv else None, ) if map_type == 'flat': map_opts.update(flat_pol=hdr.get('FLATPOL', False)) map_opts.update(parse_wcs_header(hdr)) elif map_type == 'healpix': nside = hdr['NSIDE'] nested = hdr['ORDERING'].strip().lower() in ['nest', 'nested'] map_opts.update(nested=nested) # primary HDU if H.data is None: continue # extracting a particular HDU if map_type is not None and hdu is not None and hidx != hdu: continue if map_type == 'flat': if hdr.get('ISWEIGHT', None): if 'W' not in keys: continue else: if hdr.get('POLTYPE', 'T') not in keys: continue data = np.array(H.data, dtype=float) if map_opts.pop('transpose', False): data = np.array(data.T) if uconv is not None: data *= uconv # map type must be known if the HDU contains map data if map_type not in ['flat', 'healpix']: raise ValueError("Unknown map type in HDU {}".format(hidx)) if map_type == 'flat' and hdr.get('ISWEIGHT', None): # flat map weights assert ('T' in output) if pol is None: pol = True if ('Q' in output and 'U' in output) else False weight_map = output.setdefault( 'W', G3SkyMapWeights(output['T'], polarized=pol)) fm = FlatSkyMap(data, **map_opts) fm.overflow = overflow setattr(weight_map, hdr['WTYPE'], fm) del data elif map_type == 'flat' and not hdr.get('ISWEIGHT', None): # flat map data ptype = hdr.get('POLTYPE', 'T') pol_type = getattr(MapPolType, ptype, None) fm = FlatSkyMap(data, pol_type=pol_type, **map_opts) fm.overflow = overflow output[ptype] = fm del data elif map_type == 'healpix': # healpix map data col_dict = { 'TEMPERATURE': 'T', 'Q_POLARISATION': 'Q', 'U_POLARISATION': 'U', 'Q-POLARISATION': 'Q', 'U-POLARISATION': 'U', 'I_STOKES': 'T', 'Q_STOKES': 'Q', 'U_STOKES': 'U', 'I': 'T', 'II': 'TT', 'IQ': 'TQ', 'IU': 'TU', 'II_COV': 'TT', 'IQ_COV': 'TQ', 'IU_COV': 'TU', 'QQ_COV': 'QQ', 'QU_COV': 'QU', 'UU_COV': 'UU', } pix = None partial = hdr.get('INDXSCHM') == 'EXPLICIT' or hdr.get( 'OBJECT') == 'PARTIAL' for cidx, hcol in enumerate(H.data.names): col = col_dict.get(hcol, hcol) if col in 'TQU' and col not in keys: continue elif col in ['TT', 'TQ', 'TU', 'QQ', 'QU', 'UU' ] and 'W' not in keys: continue data = np.array(H.data[hcol], dtype=float).ravel() if col == 'PIXEL' or (partial and cidx == 0): pix = np.array(data, dtype=int).ravel() del data continue uconv = None u = 'TUNIT{:d}'.format(cidx + 1) if u in hdr: u = hdr[u].lower() if u in unit_dict: units, uconv = unit_dict[u.lower()] if uconv is not None: data *= uconv overflow = hdr.get('TOFLW{:d}'.format(cidx + 1), overflow) weighted = hdr.get( 'TISWGT{:d}'.format(cidx + 1), col in ['TT', 'TQ', 'TU', 'QQ', 'QU', 'UU'], ) if weighted: assert ('T' in output) if pol is None: pol = True if ('Q' in output and 'U' in output) else False weight_map = output.setdefault( 'W', G3SkyMapWeights(output['T'], polarized=pol)) mdata = (pix, data, nside) if pix is not None else data hm = HealpixSkyMap(mdata, **map_opts) hm.overflow = overflow setattr(weight_map, col, hm) else: pol_type = getattr(MapPolType, col, None) mdata = (pix, data, nside) if pix is not None else data hm = HealpixSkyMap(mdata, pol_type=pol_type, **map_opts) output[col] = hm del mdata, data del pix break del H.data for k, m in output.items(): m.pol_conv = map_opts['pol_conv'] if k == 'W': continue m.weighted = 'W' in output return output
#!/usr/bin/env python import numpy as np from spt3g import core from spt3g.maps import G3SkyMapWeights, FlatSkyMap, MapPolType, MapPolConv from spt3g.maps import get_mask_map, remove_weights, apply_weights for pol in [True, False]: # allocation m = FlatSkyMap(500, 20, core.G3Units.arcmin, pol_conv=MapPolConv.IAU) mt = m.Clone(False) mt.pol_type = MapPolType.T if pol: mq = m.Clone(False) mq.pol_type = MapPolType.Q mu = m.Clone(False) mu.pol_type = MapPolType.U mw = G3SkyMapWeights(m, polarized=pol) assert (mw.size == m.size) assert (mw.shape == m.shape) assert (mw.npix_allocated == 0) assert (mw.polarized == pol) assert (mw.congruent) assert (mw.IsCompatible(m)) assert (mw['TT'].IsCompatible(mw.TT)) if not pol: assert (mw.QQ is None) assert (mw.UU is None)
#!/usr/bin/env python import numpy as np from spt3g import core from spt3g.maps import FlatSkyMap, MapProjection, get_ra_dec_map, get_map_stats, get_map_median from scipy.stats import skew, kurtosis # Sparse extension operators m = FlatSkyMap(500, 20, core.G3Units.arcmin) m[7345] = 4 m[7345 - 500] = 4 # Backward m[7345 + 500] = 4 # Forward assert (m.sparse) assert (m.npix_allocated == 3) assert (m.npix_nonzero == 3) m[7345 - 4 * 500] = 4 # Several steps back assert (m.npix_allocated == 6) assert (m.npix_nonzero == 4) m[7345 + 3 * 500] = 4 # Several steps forward assert (m.npix_allocated == 8) assert (m.npix_nonzero == 5) # Simple in-place operators m = FlatSkyMap(500, 20, core.G3Units.arcmin) m[15] = 10 assert (m.sparse) m *= 5 m /= 25 assert (m[15] == 2) assert (m.sparse)
#!/usr/bin/env python import numpy from spt3g import core from spt3g.maps import FlatSkyMap, MapCoordReference, MapProjection # 1D arrays m = FlatSkyMap(1, 500, core.G3Units.arcmin) assert (m.shape[0] == 500) m[15] = 65.4 assert (numpy.asarray(m)[15] == 65.4) assert (numpy.asarray(m).shape == m.shape) # 2D arrays m = FlatSkyMap(500, 20, core.G3Units.arcmin) assert (len(m.shape) == 2) assert (m.shape[0] == 20) assert (m.shape[1] == 500) assert (m.shape[0] == numpy.asarray(m).shape[0]) assert (m.shape[1] == numpy.asarray(m).shape[1]) m[15] = 65.4 assert (numpy.asarray(m)[0, 15] == 65.4) m[3, 15] = 65.4 assert (numpy.asarray(m)[3, 15] == 65.4) assert (numpy.asarray(m).shape == m.shape) # Reverse direction 2D arrays w = numpy.random.uniform(size=(300, 50)) m = FlatSkyMap(w, core.G3Units.arcmin, coord_ref=MapCoordReference.Equatorial) assert (len(m.shape) == 2) assert (m.shape[0] == w.shape[0])