Beispiel #1
0
def centroid_loop(channel, cw=5.0, cs=1.0, write_log=True,
                  filter_turnarounds=3, filter_dipole=True, add_dipole = True,
                  az_filter=True, scan_cut_adu=5., scan_cut_net=1.5,
                  mce_filter=True, group=1,
                  sim=False, add_noise=True, net=170., pol=False):

    """
    Creates a specified number of spider maps for a specified channel.  
    Each map has a specified centroid offset. Can also produce 
    simulated equaivalent.

    Arguments
    ---------

    channel : string
        Channel name in the mux representation.
    cw : float, optional
        Range of azimuth and elevation centroid offsets in arcminutes. 
        Default: 5.0
    cs : float, optional
        Size of steps dividing up the range specified by cw. Default: 1.0
    write_log : bool, optional
        Provide debugging info in log file. Default: True
    filter_turnarounds:  bool or int, optional
         If int, apply a polynomial filter of this order between turnarounds.
         If True, apply the default filter of order 1.  Default: False
    filter_dipole : bool, optional
        If True, include a dipole template in the fit. Default: True
    add_dipole : bool
        Set to True if simulated data should include dipole
        Can also modify the add_dipole attribute after construction. 
        Default: True
    az_filter : bool, optional
        If True, filter in the az spatial domain. If 'False', the filtering
        will be done in the time domain. Default: True.
    scan_cut_adu : float, optional
        Noise level (ADU) above which the entire region between turnarounds
        is flagged.  The noise is estimated as the stdev of the
        unflagged samples within the scan, after detrending.
        The more stringent of scan_cut_adu and scan_cut_net is applied.
        Non-positive values (or None) disables the cut. Default: 5.
    scan_cut_net : float, optional
        Noise level relative to channel NET above which the entire region
        between turnarounds is flagged.  The noise is estimated as the
        stdev of the unflagged samples within the scan, after detrending.
        The more stringent of scan_cut_adu and scan_cut_net is applied.
        Non-positive values (or None) disables the cut. Default: 1.5.
    mce_filter : bool, optional
        Applies the MCE filter to the simulated data. This uses the default
        MCE filter params. Default: False.
    group : int, optional
        Number of channels to process at once.  If MPI is enabled,
        channels are grouped by the number of available OMP threads
        per MPI process.  Otherewise, if not supplied, then all channels are
        loaded into memory serially. Default: 1.
    sim : bool, optional
        if True: only generate rescanned Planck maps (sims) instead of 
        SPIDER maps.
        Default: False.
    add_noise : bool, optional
        Set to True if simulated data should include white noise. 
        Default: True.
    net : float or string, optional
        Noise equivalent temperature in uK*rt(s).  Used for simulating
        white noise. Either the value to use or a bolotable name. 
        Default: 170.
    pol : bool, optional
        If True, output maps are polarized. Will break current code. 
        Will perhaps be implemented later on. Default: False.
    """

    t0 = time.time()

    # Optional string for saving results.
    if sim:
        estr = '_sim'
    else:
        estr = ''

    # Initalising logging.
    bdir = base_dir+ana_dir
    logging.basicConfig(filename=bdir+'logs/'+channel+'.log',level=logging.DEBUG)

    # Initialising directory.
    dir_to_use = bdir+'maps/'+channel+'/'
    if not os.path.exists(dir_to_use):
        os.makedirs(dir_to_use)

    # Initiate map object and calibration.
    M = _sa.UnifileMap(default_latest=True, pol=pol)
    M.bolotable
    M.update_cal(table='cal_hfi_04')
    M.init_point(reset=True,ctime=True,event=event)

    # Initiate Simulation object.
    S = _sa.UnifileSim(
        default_latest=True, verbose=False, add_dipole=add_dipole,
        add_white_noise=add_noise, net=net, interp_pix=False, mce_filter=mce_filter)

    # Reading in the source map for S.
    if sim:
        f = int(channel[1])
        if _np.mod(f,2) == 1:
            pname = 'hfi_143_2048_equatorial_28arcmin.fits'
        else:
            pname = 'hfi_100_2048_equatorial_41arcmin.fits'

        source_coord = 'C'
        source_map = _sam.read_map(pmap_dir+pname)
        S.init_source(source_map,pol=pol,coord=source_coord)

    # Copying M's pointing pointers into S's pointing pointers.
    M.copy_point(S)

    # Check if there is good calibration for this channel.
    C = _sa.base.BoloTable('cal_hfi_04')
    c = C[channel]['cal']
    print 'cal is %5.2f' %c
    if _np.isnan(c):
        print 'Calibration is nan, trying to see if arbitrary value works'
        logging.info('calibration is nan for channel'+str(channel))
        M.update_cal(cal=-1000.0,channels=channel)
        scan_cut_adu = None
        scan_cut_net = None
        filter_dipole = False

    # Map making parameters.
    init_dict = {'nside':256,'coord':'C','pol':False,'reset':True}

    # Define centroid offsets.
    centroid_range = _np.arange(-cw, cw + cs, cs)

    # Creating maps.
    t1 = time.time()
    logging.info('Initiating map, {:5.3f} '.format(t1-t0))
    try:
        az_off, el_off = M.get_offsets(channels=channel,pol=False)
        for eli, el in enumerate(centroid_range):
            for azi, az in enumerate(centroid_range):

                n = _ot.azel2n(eli, azi, len(centroid_range))
                print channel, n
                logging.info('Az ({:d}) = {:5.3f} arcmin'.format(n,az))
                logging.info('El ({:d}) = {:5.3f} arcmin'.format(n,el))

                t2 = time.time()
                if sim:
                    S.update_offsets(channels=channel,
                                     az=az_off+az*1./60, el=el_off+el*1./60)
                    S.reset_dest()
                    vec, proj = S.init_dest(**init_dict)
                    sopts = S.hwp_partition(event=event)

                else:
                    M.update_offsets(channels=channel,
                                     az=az_off+az*1./60, el=el_off+el*1./60)
                    M.reset_dest()
                    vec, proj = M.init_dest(**init_dict)
                    sopts = M.hwp_partition(event=event)

                t3 = time.time()
                logging.info('Updating pointing ({:d}):'.format(n) + \
                                 '{:5.3f}'.format(t3-t2))
                logging.info('Before:' + channel +
                             ':n = {:d}, '.format(n)+
                             'ctime = {:d}'.format(int(time.time())))
                # Populating map.
                if sim:
                    for sopt in sopts:
                        vec, proj = S.from_tod(
                            channel, filter_turnarounds = filter_turnarounds,
                            filter_dipole = filter_dipole,
                            scan_cut_adu = scan_cut_adu,
                            scan_cut_net = scan_cut_net,
                            group=group,
                            az_filter=az_filter,
                            **sopt)
                else:
                    for sopt in sopts:
                        vec, proj = M.from_tod(
                            channel, filter_turnarounds = filter_turnarounds,
                            filter_dipole = filter_dipole,
                            scan_cut_adu = scan_cut_adu,
                            scan_cut_net = scan_cut_net,
                            group=group,
                            az_filter=az_filter,
                            **sopt)

                t4 = time.time()
                logging.info('After:' + channel +
                             ':n = {:d}, delta_t = {:5.2f} sec'.format(
                        n,t4-t3)+', ctime = {:d}'.format(int(time.time())))

                # Create the map and writing it to disk.
                map_real = _ot.maps2map(vec, proj)
                hits = _np.copy(proj)
                hits[hits == 0] = _hp.UNSEEN

                logging.info('Writing to disk')
                logging.info(dir_to_use+'map'+str(n)+'.fits')
                _sam.write_map(dir_to_use+'map'+str(n)+estr+'.fits',
                               map_real, partial = True)
                _sam.write_map(dir_to_use+'hits'+str(n)+estr+'.fits',
                               hits, partial = True)
                t5 = time.time()
                logging.info('Parsing and writing map ({:d}):'.format(n) + \
                                 '{:5.3f}'.format(t5-t4))

    except Exception,e:
        print str(e)
        print e.message
        print 'unable to make map for '+channel
        return -1
Beispiel #2
0
def fit2planck(channel, cw=5.0, cs=1.0, sim=False):
    """
    Compare the spider maps with centroid offsets to a planck map with
    nominal centroids

    Arguments
    ---------

    channel : string
        Channel name in the mux representation.
    cw : float, optional
        Range of azimuth and elevation centroid offsets in arcminutes
    cs : float, optional
        Size of steps dividing up the range specified by cw
    sim: bool, optimal
        Enable use on maps ending with _sim
    """

    # Extra string for sim option.
    if sim:
        estr = '_sim'
    else:
        estr = ''

    # Check if all maps are created for this channel.
    map_dir = base_dir+ana_dir+'maps/'
    maps_exist = _ot.check_exist(map_dir, channel, cw, cs, sim=sim)
    if not maps_exist:
        return -1
    print 'found maps for '+channel

    # Figure out if the channel is 95 or 150 GHz.
    f = int(channel[1])

    # Load Planck map and apodization mask.
    pmap_name = map_dir+channel+'/map_nominal.fits'
    amap_name = map_dir+channel+'/apo_nominal.fits'

    planck_map = _sam.read_map(pmap_name)
    apo_map = _sam.read_map(amap_name)

    bpidx = (planck_map == _hp.UNSEEN)

    # Specify centroid range and create list for results.
    centroid_range = _np.arange(-cw, cw + cs, cs)
    diff = _np.zeros((len(centroid_range), len(centroid_range)))

    # Loop over map realizations.
    for eli, el in enumerate(centroid_range):
        for azi, az in enumerate(centroid_range):
            n = _ot.azel2n(eli, azi, len(centroid_range))
            spider_map = _sam.read_map(map_dir+channel+'/map'+str(n)+estr+'.fits')

            # Create a mask.
            hits = _sam.read_map(map_dir+channel+'/hits'+str(n)+'.fits')
            bsidx = (hits == 0) | (spider_map == _hp.UNSEEN)
            bidx = bpidx | bsidx
            gidx = ~bidx

            # Calculate difference between Planck and spider.
            diff[eli, azi] = \
                _np.sum(hits[gidx] * apo_map[gidx] * \
                            (spider_map[gidx] - planck_map[gidx])**2) / \
                            _np.sum(hits[gidx] * apo_map[gidx])

    # Write results to disk.
    fid = open(map_dir+channel+'/diff'+estr+'.pkl','wb')
    data = {'diff': diff}
    pickle.dump(data,fid)
    fid.close()

    return 0
Beispiel #3
0
def nominal_loop(channel, filter_turnarounds=3, filter_dipole=True, add_dipole=True,
                 az_filter=True, scan_cut_adu=5., scan_cut_net=1.5,
                 mce_filter=True, group=1):

    """
    Creates a rescanned planck map with nominal centroids

    Arguments
    ---------

    channel : string
        Channel name in the mux representation.
    filter_turnarounds:  bool or int, optional
         If int, apply a polynomial filter of this order between turnarounds.
         If True, apply the default filter of order 1.  Default: False
    filter_dipole : bool, optional
        If True, include a dipole template in the fit. Default: True
    add_dipole : bool
        Set to True if simulated data should include dipole
        Can also modify the add_dipole attribute after construction. 
        Default: True
    az_filter : bool, optional
        If True, filter in the az spatial domain. If 'False', the filtering
        will be done in the time domain. Default: True.
    scan_cut_adu : float, optional
        Noise level (ADU) above which the entire region between turnarounds
        is flagged.  The noise is estimated as the stdev of the
        unflagged samples within the scan, after detrending.
        The more stringent of scan_cut_adu and scan_cut_net is applied.
        Non-positive values (or None) disables the cut. Default: 5.
    scan_cut_net : float, optional
        Noise level relative to channel NET above which the entire region
        between turnarounds is flagged.  The noise is estimated as the
        stdev of the unflagged samples within the scan, after detrending.
        The more stringent of scan_cut_adu and scan_cut_net is applied.
        Non-positive values (or None) disables the cut. Default: 1.5.
    mce_filter : bool, optional
        Applies the MCE filter to the simulated data. This uses the default
        MCE filter params. Default: False.
    group : int, optional
        Number of channels to process at once.  If MPI is enabled,
        channels are grouped by the number of available OMP threads
        per MPI process.  Otherewise, if not supplied, then all channels are
        loaded into memory serially. Default: 1.

    """

    # Path to directory.
    bdir = base_dir+ana_dir
    mdir = bdir+'maps/'

    f = int(channel[1])

    logging.basicConfig(filename=bdir+'logs/'+channel+'.log',level=logging.DEBUG)

    logging.info('Initiating UnifileSim object')
    if _np.mod(f,2) == 1:
        pname = 'hfi_143_2048_equatorial_28arcmin.fits'
    else:
        pname = 'hfi_100_2048_equatorial_41arcmin.fits'

    source_coord = 'C'
    source_map = _sam.read_map(pmap_dir+pname)

    # Initialising sim object.
    S = _sa.UnifileSim(default_latest=True, verbose=False,
                       add_dipole=add_dipole, add_from_map=True,
                       add_white_noise=False, source_map=source_map, pol=False,
                       source_coord=source_coord, interp_pix=True,
                       mce_filter=mce_filter)
    S.bolotable
    S.update_cal(table='cal_hfi_04')

    # Check if there is good calibration for this channel.
    C = _sa.base.BoloTable('cal_hfi_04')
    c = C[channel]['cal']
    print 'cal is %5.2f' %c
    if _np.isnan(c):
        print 'Calibration is nan, trying to see if arbitrary value works'
        logging.info('calibration is nan for channel'+str(channel))
        S.update_cal(cal=-1000.0,channels=channel)
        scan_cut_adu = None
        scan_cut_net = None
        filter_dipole = False

    logging.info('Initialized UnifileSim object')

   # Region dependent plotting cuts.
    init_dict = {'nside':256,'coord':'C','pol':False,'reset':True}
    if 'rcw38' in event:
        region = 'rcw38'; minm = -1000; maxm = 5000
    else:
        region = 'cmb'; minm = -400; maxm = 400
    S.init_point(region=region, reset=True, ctime=True)
    simvec, simproj = S.init_dest(**init_dict)
    sopts = S.hwp_partition(event=event)

    # Populating map.
    try:
        for sopt in sopts:
            simvec, simproj = S.from_tod(
                channel, filter_turnarounds=filter_turnarounds,
                filter_dipole=filter_dipole,
                scan_cut_adu = scan_cut_adu,
                scan_cut_net = scan_cut_net,
                group=group,
                az_filter=az_filter,
                **sopt)
        # Creating map.
        planck_map = _ot.maps2map(simvec, simproj)
        hits_map = simproj.copy()
        hits_map[hits_map == 0] = _hp.UNSEEN
        apo_map = _at.gen_apomask(hits_map)

        # Writing to disk.
        if not os.path.exists(mdir+channel+'/'):
            os.makedirs(mdir+channel+'/')
        _sam.write_map(mdir+channel+'/'+'map_nominal.fits',
                       planck_map, partial = True)
        _sam.write_map(mdir+channel+'/'+'hits_nominal.fits',
                       hits_map, partial = True)
        _sam.write_map(mdir+channel+'/'+'apo_nominal.fits',
                       apo_map, partial = True)

    except Exception,e:
        print str(e)
        print 'unable to make map for '+channel
        return -1