Exemplo n.º 1
0
def interp_band_to_band(img, ref_map, map):

    map_size = map['signal'].shape
    ref_size = ref_map['signal'].shape

    #create a grid of RA/DEC coordinates for image we want to interpolate
    w = world(map['shead'])

    #holder for the x, y pixel coordinates that we want.
    x = np.arange(0, map_size[0])
    y = np.arange(0, map_size[1])
    xvec = np.repeat(x[:, np.newaxis], map_size[1], axis=1)
    yvec = np.repeat(y[np.newaxis, :], map_size[0], axis=0)

    c = pixel_to_skycoord(xvec, yvec, w, origin=0)
    #this is converting the pixel coords to right ascension and declination in fk4
    ra = np.asarray(c.ra.to_string(decimal=True), dtype=float)
    dec = np.asarray(c.dec.to_string(decimal=True), dtype=float)
    # reformat map data and coordinates
    data = np.ravel(img)
    points = np.column_stack((np.ravel(ra), np.ravel(dec)))

    #create a agrid of RA/DEC coordinates that we want to interpolate over
    ref_w = world(ref_map['shead'])
    ref_grid_x, ref_grid_y = np.mgrid[0:ref_size[0], 0:ref_size[1]]
    ref_grid_ra, ref_grid_dec = ref_w.wcs_pix2world(ref_grid_x, ref_grid_y, 0)

    #do the interpolation
    interp_map = griddata(points,
                          data, (ref_grid_ra, ref_grid_dec),
                          method='linear')
    return interp_map
Exemplo n.º 2
0
def interp_back_to_ref(img, ra, dec, ref_head, ref_shape):
    '''
    Purpose: Perform the inperolation of the completed Planck Map to a reference header
    Inputs: img - the Planck flux map
            ra  - grid of the Right Ascension values used to create the Planck Map
            dec - grid of the Decliation values used to create the Planck Map
            ref_head - the reference header for the interpolatino grid
            ref_shape - the shape of the interpolation grid
    '''

    map_size = img.shape

    # reformat map data and coordinates
    data = np.ravel(img)
    points = np.column_stack((np.ravel(ra), np.ravel(dec)))

    #create a agrid of RA/DEC coordinates that we want to interpolate over
    ref_w = world(ref_head)
    ref_grid_x, ref_grid_y = np.mgrid[0:ref_shape[0], 0:ref_shape[1]]
    ref_grid_ra, ref_grid_dec = ref_w.wcs_pix2world(ref_grid_x, ref_grid_y, 0)

    #do the interpolation
    interp_map = griddata(points, data, (ref_grid_ra, ref_grid_dec))
    final_map = np.swapaxes(interp_map, 0, 1)
    return final_map, ref_grid_ra, ref_grid_dec
Exemplo n.º 3
0
def one_map(name, header, band):

    if 'CDELT1' in header.keys():
        #calculating the pixsize based off of astrometry parameters.
        pixsize = 3600 * np.mean(
            [abs(header['CDELT1']),
             abs(header['CDELT2'])])
        #if the given astrometry has CDELT values and not a cd matrix create a cd matrix.
        header['cd_1'] = header['CDELT1']
        header['cd_2'] = 0
        header['cd_1'] = 0
        header['cd_2'] = header['CDELT2']

    else:
        #if the astrometry is given with a cd matrix calculate the pixsize this way.
        pixsize = 3600 * \
                  np.mean([abs(header['CD1_1']+  header['CD2_1']), \
                        abs(header['CD2_1'] + header['CD2_2'])])

    #this is to call IRIS code
    ra_c = header['crval1'] * u.deg
    dec_c = header['crval2'] * u.deg
    cent = [ra_c.value, dec_c.value]
    coord = SkyCoord(ra=ra_c, dec=dec_c)
    b1950_coord = coord.transform_to(FK4(equinox='B1950'))
    ra_c_b = float(b1950_coord.ra.to_string(decimal=True))
    dec_c_b = float(b1950_coord.dec.to_string(decimal=True))
    #square patch of sky, this can be changed if desired.
    naxis = [header['naxis1'], header['naxis2']]

    iris_head = make_header(pixsize, naxis, ra_c_b, dec_c_b)
    iris_map = mosaic(iris_head, band=4)

    x = np.arange(0, iris_head['NAXIS1'])
    y = np.arange(0, iris_head['NAXIS2'])
    yvec = np.repeat(x[:, np.newaxis], iris_head['NAXIS2'], axis=1)
    xvec = np.repeat(y[np.newaxis, :], iris_head['NAXIS1'], axis=0)
    w = world(iris_head)
    c = pixel_to_skycoord(xvec, yvec, w, origin=0)
    c = c.transform_to('icrs')

    #this is converting the pixel coords to right ascension and declination in fk4
    ra = np.asarray(c.ra.to_string(decimal=True), dtype=float)
    dec = np.asarray(c.dec.to_string(decimal=True), dtype=float)

    iris_interp, r, d = interp_back_to_ref(iris_map, ra.ravel(), dec.ravel(),
                                           header)

    c = 2.99792458e8  #m/s

    nu = c / clus_get_lambdas(band, center=False)

    planck, ra, dec, avg_T, avg_beta, avg_tau = create_map(header, nu=nu)
    planck_head = save_header(header, avg_beta, avg_T, avg_tau,
                              'Planck_' + band, 'Jy/Beam', cent)
    return planck, iris_interp, avg_T, avg_beta, avg_tau, planck_head
Exemplo n.º 4
0
def mosaic(header, band=4, catname=config.IrisLookupFile, dir=config.IrisDir):
    '''
    purpose: create mosaic of images
    Inputs : Header - header information -should contain some astrometry information
             band   - the band you wish to look at
                - 1 : 12 micron
                - 2 : 25 micron
                - 3 : 60 micron
                - 4 : 100 micron (default)
             catname- filename of the catalog
             dir    - directory where iris maps are
    Outputs:
    '''


    header.set('NAXIS', 2)
    w = world(header)
    try:
        equinox = header['EQUINOX']
    except KeyError:
        equinox = header['EPOCH']


    x_size = header['NAXIS1']
    y_size = header['NAXIS2']
    print('Handling %s elements' % (x_size * y_size))
    x = np.arange(0, x_size)
    y = np.arange(0, y_size)
    xlist = np.tile(1, x_size)
    ylist = np.tile(1, y_size)
    xmap = np.outer(x, ylist)
    ymap = np.outer(xlist, y)

    result = np.zeros((x_size, y_size))
    weight = np.zeros((x_size, y_size))
    new_c = pixel_to_skycoord(xmap, ymap, w, origin=0)
    #this is converting the pixel coords to right ascension and declination in fk4
    ra = np.asarray(new_c.ra.to_string(decimal=True), dtype=float)
    dec = np.asarray(new_c.dec.to_string(decimal=True), dtype=float)

    ctype = get_cord_type(header)

    if ctype == 2:
        fk4 = 1
        equinox = 1950 #force the equinox to be 1950 in the case of galactic coordinates
        print('converting from Galactic to Celestial')
        sk = SkyCoord(ra * u.deg, dec * u.deg, frame=Galactic)
        new_c = sk.transform_to(FK4)
        ra = np.asarray(new_c.ra.to_string(decimal=True), dtype=float)
        dec = np.asarray(new_c.dec.to_string(decimal=True), dtype=float)

    elif ctype == 3:
        print('converting from Ecliptic to Celestial')
        sk = SkyCoord(ra * u.deg, dec * u.deg, frame=BarycentricTrueEcliptic)
        new_c = sk.transform_to(FK4)

        ra = []
        dec = []
        coordinates = new_c.to_string('decimal')
        ra = np.asarray(new_c.ra.to_string(decimal=True), dtype=float)
        dec = np.asarray(new_c.dec.to_string(decimal=True), dtype=float)

    if equinox == 2000.0:
        print('precessing coordinates from J2000 to B1950')
        new_c.transform_to(FK4(equinox='B1950'))
        ra = np.asarray(new_c.ra.to_string(decimal=True), dtype=float)
        dec = np.asarray(new_c.dec.to_string(decimal=True), dtype=float)

    ra = nan2undef(ra)
    dec = nan2undef(dec)
    ra = np.asarray(ra)
    dec = np.asarray(dec)
    #converted these to arrays for easier data manipulation

    inum, ramin, ramax, raavg, decmin, decmax, decavg, medianval, noise_key = np.loadtxt(catname, unpack=True)
    numel = int(inum[-1]) #number of entries in the text file
    print('Checking for ISSA maps that intersect with the given header')

    ind1 = np.where(ra != -32768)[0]
    ind2 = np.where(dec != -32768)[0]

    ind = []
    for i in range(ra.shape[0]):
        for j in range(ra.shape[1]):
            if ra[i,j] != -32768 and dec[i,j] != -32768:
                ind.append([i,j])
    ind = np.asarray(ind)
    good_inds = np.zeros(numel)

    c1min = np.min(ra[ind])
    c1max = np.max(ra[ind])
    c2min = np.min(dec[ind])
    c2max = np.max(dec[ind])

    for i in range(numel):
        if c1min > ramin[i] and c1min < ramax[i] and c2min > decmin[i] and c2min < decmax[i]:
            good_inds[i] = 1
        elif c1min > ramin[i] and c1min < ramax[i] and c2max > decmin[i] and c2max < decmax[i]:
            good_inds[i] = 1
        elif c1max > ramin[i] and c1max < ramax[i] and c2max > decmin[i] and c2max < decmax[i]:
            good_inds[i] = 1
        elif c1max > ramin[i] and c1max < ramax[i] and c2min > decmin[i] and c2min < decmax[i]:
            good_inds[i] = 1
        elif ramin[i] > c1min and ramin[i] < c1max and decmin[i] > c2min and decmin[i] < c2min:
            good_inds[i] = 1
        elif ramax[i] > c1min and ramax[i] < c1max and decmin[i] > c2min and decmin[i] < c2min:
            good_inds[i] = 1
        elif ramin[i] > c1min and ramin[i] < c1max and decmax[i] > c2min and decmax[i] < c2min:
            good_inds[i] = 1
        elif ramax[i] > c1min and ramax[i] < c1max and decmax[i] > c2min and decmax[i] < c2min:
            good_inds[i] = 1

    good_inds = np.where(good_inds > 0)[0]
    if good_inds.shape[0] == 0:
        print('No ISSA map corresponds to the header given')
        exit()

    print('%s ISSA maps will be combined to produce the mosaic' %(good_inds.shape[0]))

    for i in range(good_inds.shape[0]):
        mapi = get_iris(inum[good_inds[i]], dir=dir, band=band)


        mapi[0].header['EQUINOX'] = 2000.0
        try:
            del(mapi[0].header['NAXIS3'])
        except KeyError:
            pass

        #do the transform back to pixel coords
        w = world(mapi[0].header)
        x, y = skycoord_to_pixel(new_c, w, origin=0)
        tempo = mbilinear(x, y, mapi[0].data)
        for j in range(tempo.shape[0]):
            for k in range(tempo.shape[1]):
                if tempo[j,k] != -32768:
                    weight[j,k] = weight[j,k] + 1
                    result[j,k] = result[j,k] + tempo[j,k]
    indw = []
    complement = []
    for i in range(weight.shape[0]):
        for j in range(weight.shape[1]):
            if weight[i,j] > 0:
                result[i,j] = result[i,j] / weight[i,j]
            else:
                result[i,j] = -32768
    #because of the way the image gets made it is rotated and flipped along its x-axis
    #this is a correction to get it to line up with the idl version and does not have any
    #real effect on its astrometry
    result = np.rot90(result, k=3)
    result = np.fliplr(result)

    #for testing
    # plt.imshow(result, origin='lower')
    # plt.show()
    return result
Exemplo n.º 5
0
def read_in_fits(filename, center, ref_head, ref_pixsize=8, ref_mapsize=260):
    '''
    Purpose : This function reads in the fits files for the components and parses them so that we are left with data only for our field.
    Inputs: filename (str) - the singular filename for a component used in the MBB fit
            center (float array) - center of the field of interest
            ref_head (Astropy.header) - header for the reference field
            ref_pixsize - pixel size of the reference map
            ref_mapsize - mapsize of the reference map
    Outputs: map (float array) - an array of flux values at the given field
             pixsize (float) - pixel size of the uninterpolated component maps
             x_side (int) - the length of the x-axis of the map
             y_side (int) - the length of the y-axis of the map
             RA_grid (float array) - grid of the Right Ascension values used to pull out components
             DEC_grid (float array) - grid of the Declination values used to pull out components
    '''

    hdul = fits.open(filename)
    head = hdul[1].header
    if 'Temperature' in filename:
        data = hdul[1].data.field('TEMP')
        error = hdul[1].data.field('ERR_TEMP')
    elif 'Spectral-Index' in filename:
        data = hdul[1].data.field('BETA')
        error = hdul[1].data.field('ERR_BETA')

    elif 'Opacity' in filename:
        data = hdul[1].data.field('TAU353')
        error = hdul[1].data.field('ERR_TAU')

    else:
        data = hdul[1].data.field(0)
    nside = head['NSIDE']
    order = head['ORDERING']
    hdul.close()



    #Galactic Coordinate System
    hp = HEALPix(nside=nside, order=order, frame='galactic')
    #create a pixel grid in terms of the nu=353 grid for GNILC to create our intensity maps
    pixsize = hp.pixel_resolution.to(u.arcsecond).value

    #* 2 is for boosting the size of the map so to fix edge effects from interpolation
    map_arc_x = ref_mapsize[0] * 2 * ref_pixsize #map size in arcseconds
    map_arc_y = ref_mapsize[1] * 2 * ref_pixsize

    npixxside = ceil(map_arc_x / pixsize) #convert to map size in pixels for nu = 353 map.
    npixyside = ceil(map_arc_y / pixsize)

    #* 2 is for boosting the size of the map so to fix edge effects from interpolation
    x  = np.linspace(0, ref_mapsize[0] * 2,   npixxside)
    y  = np.linspace(0, ref_mapsize[1] * 2,   npixyside)

    X, Y = np.meshgrid(x, y)
    w = world(ref_head)
    skycoords = pixel_to_skycoord(X.ravel(), Y.ravel(), wcs=w, origin=0)
    RA_grid = np.asarray(skycoords.ra.to_string(decimal=True), dtype='float') * u.deg
    DEC_grid = np.asarray(skycoords.dec.to_string(decimal=True), dtype='float') * u.deg




    # coords = SkyCoord(RA_grid.ravel(), DEC_grid.ravel(), frame='icrs')
    coords = SkyCoord(ra=RA_grid.ravel(), dec=DEC_grid.ravel(), frame='icrs')
    gal_coords = coords.galactic

    map = hp.interpolate_bilinear_skycoord(gal_coords, data)

    x_side = len(x)
    y_side = len(y)
    return map, pixsize, y_side, x_side, RA_grid, DEC_grid
Exemplo n.º 6
0
def mosaic(header,
           band=4,
           catname=iris_config.IrisLookupFile,
           dir=iris_config.IrisDir):
    '''
    purpose: create mosaic of images
    Inputs : Header - header information -should contain some astrometry information
             band   - the band you wish to look at
                - 1 : 12 micron
                - 2 : 25 micron
                - 3 : 60 micron
                - 4 : 100 micron (default)
             catname- filename of the catalog
             dir    - directory where iris maps are
    Outputs:
    '''

    header.set('NAXIS', 2)
    w = world(header)
    try:
        equinox = header['EQUINOX']
    except KeyError:
        equinox = header['EPOCH']

    x_size = header['NAXIS2']
    y_size = header['NAXIS1']
    print('Handling %s elements' % (x_size * y_size))
    x = np.arange(0, x_size)
    y = np.arange(0, y_size)
    xlist = np.tile(1, x_size)
    ylist = np.tile(1, y_size)
    xmap = np.outer(x, ylist)
    ymap = np.outer(xlist, y)

    result = np.zeros((x_size, y_size))
    weight = np.zeros((x_size, y_size))
    new_c = pixel_to_skycoord(xmap, ymap, w, origin=0)
    #this is converting the pixel coords to right ascension and declination in fk4
    ra = np.asarray(new_c.ra.to_string(decimal=True), dtype=float)
    dec = np.asarray(new_c.dec.to_string(decimal=True), dtype=float)

    ctype = get_cord_type(header)

    ra = nan2undef(ra)
    dec = nan2undef(dec)
    ra = np.asarray(ra)
    dec = np.asarray(dec)
    #converted these to arrays for easier data manipulation

    inum, ramin, ramax, raavg, decmin, decmax, decavg, medianval, noise_key = np.loadtxt(
        catname, unpack=True)
    numel = int(inum[-1])  #number of entries in the text file
    print('Checking for ISSA maps that intersect with the given header')

    ind1 = np.where(ra != -32768)[0]
    ind2 = np.where(dec != -32768)[0]

    ind = []
    for i in range(ra.shape[0]):
        for j in range(ra.shape[1]):
            if ra[i, j] != -32768 and dec[i, j] != -32768:
                ind.append([i, j])
    ind = np.asarray(ind)
    good_inds = np.zeros(numel)

    c1min = np.min(ra[ind])
    c1max = np.max(ra[ind])
    c2min = np.min(dec[ind])
    c2max = np.max(dec[ind])

    for i in range(numel):
        if c1min > ramin[i] and c1min < ramax[i] and c2min > decmin[
                i] and c2min < decmax[i]:
            good_inds[i] = 1
        elif c1min > ramin[i] and c1min < ramax[i] and c2max > decmin[
                i] and c2max < decmax[i]:
            good_inds[i] = 1
        elif c1max > ramin[i] and c1max < ramax[i] and c2max > decmin[
                i] and c2max < decmax[i]:
            good_inds[i] = 1
        elif c1max > ramin[i] and c1max < ramax[i] and c2min > decmin[
                i] and c2min < decmax[i]:
            good_inds[i] = 1
        elif ramin[i] > c1min and ramin[i] < c1max and decmin[
                i] > c2min and decmin[i] < c2min:
            good_inds[i] = 1
        elif ramax[i] > c1min and ramax[i] < c1max and decmin[
                i] > c2min and decmin[i] < c2min:
            good_inds[i] = 1
        elif ramin[i] > c1min and ramin[i] < c1max and decmax[
                i] > c2min and decmax[i] < c2min:
            good_inds[i] = 1
        elif ramax[i] > c1min and ramax[i] < c1max and decmax[
                i] > c2min and decmax[i] < c2min:
            good_inds[i] = 1

    good_inds = np.where(good_inds > 0)[0]
    if good_inds.shape[0] == 0:
        print('No ISSA map corresponds to the header given')
        exit()

    print('%s ISSA maps will be combined to produce the mosaic' %
          (good_inds.shape[0]))

    for i in range(good_inds.shape[0]):
        mapi = get_iris(inum[good_inds[i]], dir=dir, band=band)

        try:
            del (mapi[0].header['NAXIS3'])
        except KeyError:
            pass

        #do the transform back to pixel coords
        w = world(mapi[0].header)
        x, y = skycoord_to_pixel(new_c, w, origin=0)
        tempo = mbilinear(x, y, mapi[0].data)
        for j in range(tempo.shape[0]):
            for k in range(tempo.shape[1]):
                if tempo[j, k] != -32768:
                    weight[j, k] = weight[j, k] + 1
                    result[j, k] = result[j, k] + tempo[j, k]
    indw = []
    complement = []
    for i in range(weight.shape[0]):
        for j in range(weight.shape[1]):
            if weight[i, j] > 0:
                result[i, j] = result[i, j] / weight[i, j]
            else:
                result[i, j] = -32768
    #in interpolating the image the x and y axes get flipped.
    result = np.swapaxes(result, 0, 1)
    if band == 4:
        result -= 0.65  #subtract CIB to get just dgl / cirrus

    return result
Exemplo n.º 7
0
pixsize = 0.0250000000000  #pixels / arcsecond of observation to interpolate IRIS
# maps to.
naxis = 22
name = 'rxj1347'
#square patch of sky, this can be changed if desired.
naxis1 = naxis2 = naxis

y = np.arange(0, naxis1)
x = np.arange(0, naxis2)
yvec = np.repeat(x[:, np.newaxis], naxis2, axis=1)
xvec = np.repeat(y[np.newaxis, :], naxis1, axis=0)
head = make_header(pixsize, naxis, ra_c_b,
                   dec_c_b)  #this function assumes a square.

w = world(head)
c = pixel_to_skycoord(xvec, yvec, w, origin=0)
c = c.transform_to('icrs')

#this is converting the pixel coords to right ascension and declination in fk4
ra = np.asarray(c.ra.to_string(decimal=True), dtype=float)
dec = np.asarray(c.dec.to_string(decimal=True), dtype=float)

#get information from the IRAS astrometry positions
map = mosaic(head, band=4)

hdu = fits.PrimaryHDU(map, head)
hdul = fits.HDUList([hdu])
hdul.writeto(config.DataDir + 'iris_0' + name + '_fx.fits')
#ra
hdu = fits.PrimaryHDU(ra, head)