def main():

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin
    from astropy.io import fits as pyfits
    import numpy as np

    os.chdir('/d/bip3/ezbc/perseus/data')

    # If true, deletes files to be written
    clobber = 1
    clobber_hi = 1

    # First, change zeros in lee image to nans
    data, header = pyfits.getdata('av/perseus_av_lee12_2mass.fits',
                                  header=True)
    data[data == 0] = np.nan
    pyfits.writeto('av/perseus_av_lee12_2mass_nan.fits',
                   data,
                   header,
                   clobber=True)

    in_images = (
        'hi/perseus_hi_galfa_cube',
        'hi/perseus_hi_galfa_dr1_cube',
        'av/perseus_av_planck_tau353',
        'av/perseus_av_error_planck_tau353',
        'dust_temp/perseus_dust_temp',
        'dust_temp/perseus_dust_temp_error',
        'av/perseus_av_planck_radiance',
        'av/perseus_av_error_planck_radiance',
        'co/perseus_co_cfa_cube',
        #'av/perseus_av_k09_nan',
        '/d/bip3/ezbc/perseus/data/av/perseus_av_k09_nan',
        'av/perseus_av_lee12_2mass_nan',
        'av/perseus_av_lee12_iris_masked')

    im_hi = 'hi/perseus_hi_galfa_cube'
    im_hi_dr1 = 'hi/perseus_hi_galfa_dr1_cube'
    im_pl = 'av/perseus_av_planck_tau353'
    im_pl_err = 'av/perseus_av_error_planck_tau353'
    im_Td = 'dust_temp/perseus_dust_temp'
    im_Td_err = 'dust_temp/perseus_dust_temp_error'
    im_pl2 = 'av/perseus_av_planck_radiance'
    im_pl2_err = 'av/perseus_av_error_planck_radiance'
    im_co = 'co/perseus_co_cfa_cube'
    im_k09 = 'av/perseus_av_k09'
    im_lee_2mass = 'av/perseus_av_lee12_2mass'
    im_lee_iris = 'av/perseus_av_lee12_iris'

    # Load the images into miriad
    print('\nLoading images into miriad...')
    out_images = (im_hi, im_hi_dr1, im_pl, im_pl_err, im_Td, im_Td_err, im_pl2,
                  im_pl2_err, im_co, im_k09, im_lee_2mass, im_lee_iris)

    for i in xrange(len(in_images)):
        if out_images[i] == im_hi:
            clobber_temp = clobber_hi
        else:
            clobber_temp = clobber
        exists = check_file(out_images[i] + '.mir', clobber=clobber_temp)
        if not exists:
            print('\tLoading {:s}.fits\n'.format(in_images[i]))
            fits(in_images[i] + '.fits', out=out_images[i] + '.mir', op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (im_pl, im_pl_err, im_Td, im_Td_err, im_pl2, im_pl2_err, im_hi,
              im_hi_dr1)

    desc = (59.75, 0, -0.08333, 180, 26.05, 0, 0.08333, 132)

    delta_ra = -0.083333333
    delta_dec = 0.083333333

    ref_pix, npix = calc_image_origin(x_limits=(70, 40),
                                      y_limits=(21, 38),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, im_hi_dr1):
            desc = desc_hi
        else:
            desc = desc_av

        exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_5arcmin.mir\n'.format(image))
            regrid(image + '.mir', out=image + '_5arcmin.mir', desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0  # arcmin
    im_beams = np.array([3.7, 3.7, 2.5, 2.5, 4.3])  # arcmin
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_hi, im_hi_dr1, im_k09, im_lee_2mass, im_lee_iris]

    for i in xrange(len(images)):
        exists = check_file(images[i] + '_smooth_planckres.mir',
                            clobber=clobber)

        if not exists:
            if images[i] in (im_hi, im_hi_dr1):
                image = images[i] + '_5arcmin'
            else:
                image = images[i]

            print('\tSmoothing {:s}.mir\n'.format(image))

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_smooth_planckres.mir\n'.format(image))
            if image == im_co or image == im_lee_2mass or image == im_lee_iris:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
        im_pl + '_5arcmin',
        im_pl_err + '_5arcmin',
        im_Td + '_5arcmin',
        im_Td_err + '_5arcmin',
        im_pl2 + '_5arcmin',
        im_pl2_err + '_5arcmin',
        im_k09 + '_regrid_planckres',
        im_lee_2mass + '_regrid_planckres',
        im_lee_iris + '_regrid_planckres',
        im_co + '_regrid_planckres',
        im_hi + '_regrid_planckres',
        im_hi_dr1 + '_regrid_planckres',
    ]

    for image in images:
        exists = check_file(image + '.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.mir\n'.format(image))

            fits(image + '.mir', out=image + '.fits', op='xyout')

    # write masked Planck image as same as Lee+12
    data_pl, header = pyfits.getdata(im_pl + '_5arcmin.fits', header=True)
    data_l12 = pyfits.getdata(im_lee_iris + '_regrid_planckres.fits')
    data_pl[np.isnan(data_l12)] = np.nan
    pyfits.writeto(im_pl + '_5arcmin_lee12mask.fits',
                   data_pl,
                   header,
                   clobber=True)
def main():

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin
    from astropy.io import fits as pyfits
    import numpy as np

    os.chdir('/d/bip3/ezbc/perseus/data')

    # If true, deletes files to be written
    clobber = 1
    clobber_hi = 1

    in_images = ('/d/bip3/ezbc/multicloud/data/hi/multicloud_hi_galfa_cube',
                 )

    im_hi = 'hi/perseus_hi_galfa_cube_roy'

    # Load the images into miriad
    print('\nLoading images into miriad...')
    out_images = (im_hi,
                  )

    for i in xrange(len(in_images)):
        if out_images[i] == im_hi:
            clobber_temp = clobber_hi
        else:
            clobber_temp = clobber
        exists = check_file(out_images[i] + '.mir', clobber=clobber_temp)
        if not exists:
            print('\tLoading {:s}.fits\n'.format(in_images[i]))
            fits(in_images[i] + '.fits',
                    out=out_images[i] + '.mir',
                    op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding images')

    images = (im_hi,)

    # The center coordinate of the region is RA: 4h13m26s, Dec =33d22.
    # Will it be too much of asking if we want to get a coverage of around 20deg
    # X20 deg?

    # In terms of rectangular region it will be

    # DEC ---> 20deg to 40 deg
    # RA-------> 4h40m to 3h20m

    #desc = (59.75,0,-0.08333,180,26.05,0,0.08333,132)

    delta_ra = -0.083333333 / 5.0
    delta_dec = 0.083333333 / 5.0

    ref_pix, npix = calc_image_origin(x_limits=(15 * (4 + 40./60.),
                                                15 * (3 + 20./60.)),
                                      y_limits=(20, 38),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi,):
            desc = desc_hi
        else:
            desc = desc_av

        exists = check_file(image + '_regrid.mir', clobber=clobber)


        if not exists:
            print('\tRegridding {:s}_regrid.mir\n'.format(image))
            regrid(image + '.mir',
                    out=image + '_regrid.mir',
                    desc=desc)

    images = [im_hi,]

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
              im_hi + '_regrid',
              ]

    for image in images:
        exists = check_file(image + '.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.mir\n'.format(image))

            fits(image + '.mir',
                 out=image + '.fits',
                 op='xyout')
示例#3
0
def main():

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin
    from astropy.io import fits as pyfits
    import numpy as np

    os.chdir('/d/bip3/ezbc/perseus/data')

    # If true, deletes files to be written
    clobber = 1
    clobber_hi = 1

    in_images = ('/d/bip3/ezbc/perseus/data/hi/perseus_hi_galfa_cube', )

    im_hi = 'hi/perseus_hi_galfa_cube_sub'

    # Load the images into miriad
    print('\nLoading images into miriad...')
    out_images = (im_hi, )

    for i in xrange(len(in_images)):
        if out_images[i] == im_hi:
            clobber_temp = clobber_hi
        else:
            clobber_temp = clobber
        exists = check_file(out_images[i] + '.mir', clobber=clobber_temp)
        if not exists:
            print('\tLoading {:s}.fits\n'.format(in_images[i]))
            fits(in_images[i] + '.fits', out=out_images[i] + '.mir', op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding images')

    images = (im_hi, )

    # The center coordinate of the region is RA: 4h13m26s, Dec =33d22.
    # Will it be too much of asking if we want to get a coverage of around 20deg
    # X20 deg?

    # In terms of rectangular region it will be

    # DEC ---> 20deg to 40 deg
    # RA-------> 4h40m to 3h20m

    #desc = (59.75,0,-0.08333,180,26.05,0,0.08333,132)

    delta_ra = -0.083333333
    delta_dec = 0.083333333

    ref_pix, npix = calc_image_origin(x_limits=(15 * (3 + 50. / 60.),
                                                15 * (3 + 10. / 60.)),
                                      y_limits=(26, 35),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -50.0
    high_vel = 30.0
    vel_res = 1
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, ):
            desc = desc_hi
        else:
            desc = desc_av

        exists = check_file(image + '_regrid.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_regrid.mir\n'.format(image))
            regrid(image + '.mir', out=image + '_regrid.mir', desc=desc)

    images = [
        im_hi,
    ]

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
        im_hi + '_regrid',
    ]

    for image in images:
        exists = check_file(image + '.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.mir\n'.format(image))

            fits(image + '.mir', out=image + '.fits', op='xyout')
def main():

    ''' Planck resolution is 5'. We will smooth and regrid all maps to the
    Planck map.

    Map                           Resolution      Convol size
    GALFA HI                      3.7'            3.363
    Kainulainen et al. (2009) Av  2.4'            4.386
    Pineda et al. (2010) Av       3.3'            3.75
    CfA CO Dame et al. (2001)     8'              No smoothing
    '''

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin

    os.chdir('/d/bip3/ezbc/taurus/data')

    # If true, deletes files to be written
    clobber = 1

    in_images = (
              #'av/taurus_av_kainulainen2009_nan',
              '/d/bip3/ezbc/multicloud/data/av/multicloud_av_k09_nan',
              'av/taurus_av_pineda2010',
              'hi/taurus_hi_galfa_cube',
              'hi/taurus_hi_galfa_dr1_cube',
              'av/taurus_av_planck_tau353',
              'av/taurus_av_error_planck_tau353',
              'av/taurus_av_planck_radiance',
              'av/taurus_av_error_planck_radiance',
              'co/taurus_co_cfa_cube',
              'co/taurus_co_planck',
              'co/taurus_co_error_planck',
              'co/taurus_co_1-0_planck',
              'co/taurus_co_1-0_error_planck',
              'co/taurus_co_2-1_planck',
              'co/taurus_co_2-1_error_planck',
              'co/taurus_co_3-2_planck',
              'co/taurus_co_3-2_error_planck',
              )

    im_pl = 'av/taurus_av_planck_tau353'
    im_pl_err = 'av/taurus_av_error_planck_tau353'
    im_pl2 = 'av/taurus_av_planck_radiance'
    im_pl2_err = 'av/taurus_av_error_planck_radiance'
    im_hi = 'hi/taurus_hi_galfa_cube'
    im_hi_dr1 = 'hi/taurus_hi_galfa_dr1_cube'
    im_co = 'co/taurus_co_cfa_cube'
    im_p10 = 'av/taurus_av_p10'
    im_k09 = 'av/taurus_av_k09'
    im_pl_co = 'co/taurus_co_planck'
    im_pl_co_err = 'co/taurus_co_error_planck'
    im_pl_co_10 = 'co/taurus_co_1-0_planck'
    im_pl_co_10_err = 'co/taurus_co_1-0_error_planck'
    im_pl_co_21 = 'co/taurus_co_2-1_planck'
    im_pl_co_21_err = 'co/taurus_co_2-1_error_planck'
    im_pl_co_32 = 'co/taurus_co_3-2_planck'
    im_pl_co_32_err = 'co/taurus_co_3-2_error_planck'

    # Load the images into miriad
    print('\nLoading images into miriad')
    out_images = (im_k09,
                  im_p10,
                  im_hi,
                  im_hi_dr1,
                  im_pl,
                  im_pl_err,
                  im_pl2,
                  im_pl2_err,
                  im_co,
                  im_pl_co,
                  im_pl_co_err,
                  im_pl_co_10,
                  im_pl_co_10_err,
                  im_pl_co_21,
                  im_pl_co_21_err,
                  im_pl_co_32,
                  im_pl_co_32_err,
                  )

    for i in xrange(len(in_images)):
        exists = check_file(out_images[i] + '.mir', clobber=clobber)
        print('\tReading {:s}.fits\n'.format(in_images[i]))
        if not exists:
            fits(in_images[i] + '.fits',
                    out=out_images[i] + '.mir',
                    op='xyin')

            print('\tout = ' + out_images[i] + '.mir')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (
              im_pl,
              im_pl_err,
              im_pl2,
              im_pl2_err,
              im_hi,
              im_hi_dr1,
              #im_pl_co,
              #im_pl_co_err,
              #im_pl_co_10,
              #im_pl_co_10_err,
              #im_pl_co_21,
              #im_pl_co_21_err,
              #im_pl_co_32,
              #im_pl_co_32_err,
              )

    delta_ra = -0.083333333
    delta_dec = 0.083333333
    lim_ra_wcs, lim_dec_wcs = 77.5, 18.0
    ref_ra_wcs, ref_dec_wcs = 0.0, 0.0

    ref_pix, npix = calc_image_origin(x_limits=(83, 53.0),
                                      y_limits=(15.0, 35.0),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, im_hi_dr1):
            desc = desc_hi
        else:
        	desc = desc_av

        exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        print('\tReading image {:s}.mir'.format(image))
        print('\tWriting image {:s}_5arcmin.mir\n'.format(image))

        if not exists:
            regrid(image + '.mir',
                    out=image + '_5arcmin.mir',
                    desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0 # arcsec
    im_beams = np.array([2.4, 3.3, 3.7, 3.7]) # arcsec
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_k09, im_p10, im_hi, im_hi_dr1]

    for i in xrange(len(images)):

        exists = check_file(images[i] + '_smooth_planckres.mir',
                clobber=clobber)

        print('\t{:s}.mir\n'.format(image))

        if not exists:
            if images[i] in (im_hi, im_hi_dr1):
                image = images[i] + '_5arcmin'
            else:
            	image = images[i]

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)
        print('\t{:s}_smooth_planckres.mir\n'.format(image))

        if not exists:
            if image == im_co:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
              im_pl + '_5arcmin',
              im_pl_err + '_5arcmin',
              im_pl2 + '_5arcmin',
              im_pl2_err + '_5arcmin',
              im_pl_co + '_5arcmin',
              im_pl_co_err + '_5arcmin',
              im_pl_co_10 + '_5arcmin',
              im_pl_co_10_err + '_5arcmin',
              im_pl_co_21+ '_5arcmin',
              im_pl_co_21_err+ '_5arcmin',
              im_pl_co_32+ '_5arcmin',
              im_pl_co_32_err+ '_5arcmin',
              im_k09 + '_regrid_planckres',
              im_p10 + '_regrid_planckres',
              im_co + '_regrid_planckres',
              im_hi + '_regrid_planckres',
              im_hi_dr1 + '_regrid_planckres',
              ]

    for image in images:
        check_file(image + '.fits', clobber=clobber)

        print('\t{:s}.mir\n'.format(image))

        fits(image + '.mir',
             out=image + '.fits',
             op='xyout')
示例#5
0
def main():

    from mirpy import fits, regrid, smooth, imbin
    from mycoords import calc_image_origin
    from astropy.io import fits as pyfits
    import numpy as np

    os.chdir('/d/bip3/ezbc/multicloud/data')

    # If true, deletes files to be written
    clobber = False
    clobber_hi = False

    # First, change zeros in lee image to nans
    data, header = pyfits.getdata('av/multicloud_av_lee12_2mass.fits',
                                  header=True)
    data[data == 0] = np.nan
    pyfits.writeto('av/multicloud_av_lee12_2mass_nan.fits',
                   data,
                   header,
                   clobber=True)
    data, header = pyfits.getdata('av/multicloud_av_k09.fits', header=True)
    data[data == -1] = np.nan
    pyfits.writeto('av/multicloud_av_k09_nan.fits', data, header, clobber=True)

    in_images = ('hi/multicloud_hi_galfa_cube', 'av/multicloud_av_planck',
                 'av/multicloud_av_error_planck',
                 'dust_temp/multicloud_dust_temp',
                 'dust_temp/multicloud_dust_temp_error',
                 'dust_temp/multicloud_dust_beta',
                 'dust_temp/multicloud_dust_beta_error',
                 'av/multicloud_av_planck_radiance',
                 'av/multicloud_av_error_planck_radiance',
                 'co/multicloud_co_cfa_cube', 'av/multicloud_av_k09_nan',
                 'av/multicloud_av_lee12_2mass_nan',
                 'av/multicloud_av_lee12_iris_masked')

    im_hi = 'hi/multicloud_hi_galfa_cube'
    im_pl = 'av/multicloud_av_planck'
    im_pl_err = 'av/multicloud_av_error_planck'
    im_Td = 'dust_temp/multicloud_dust_temp'
    im_Td_err = 'dust_temp/multicloud_dust_temp_error'
    im_beta = 'dust_temp/multicloud_dust_beta'
    im_beta_err = 'dust_temp/multicloud_dust_beta_error'
    im_pl2 = 'av/multicloud_av_planck_radiance'
    im_pl2_err = 'av/multicloud_av_error_planck_radiance'
    im_co = 'co/multicloud_co_cfa_cube'
    im_k09 = 'av/multicloud_av_k09_nan'
    im_lee_2mass = 'av/multicloud_av_lee12_2mass'
    im_lee_iris = 'av/multicloud_av_lee12_iris'

    # Load the images into miriad
    print('\nLoading images into miriad')
    out_images = (im_hi, im_pl, im_pl_err, im_Td, im_Td_err, im_beta,
                  im_beta_err, im_pl2, im_pl2_err, im_co, im_k09, im_lee_2mass,
                  im_lee_iris)

    for i in xrange(len(in_images)):
        if in_images[i] == im_hi:
            exists = check_file(out_images[i] + '.mir', clobber=clobber_hi)
        else:
            exists = check_file(out_images[i] + '.mir', clobber=clobber)

        print('\tLoading {:s}.fits\n'.format(in_images[i]))
        if not exists:
            fits(in_images[i] + '.fits', out=out_images[i] + '.mir', op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (im_pl, im_pl_err, im_Td, im_Td_err, im_beta, im_beta_err, im_pl2,
              im_pl2_err, im_hi)

    desc = (59.75, 0, -0.08333, 180, 26.05, 0, 0.08333, 132)

    delta_ra = -0.083333333
    delta_dec = 0.083333333

    ref_pix, npix = calc_image_origin(x_limits=(85, 32),
                                      y_limits=(15, 48),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image == im_hi:
            desc = desc_hi
            exists = check_file(image + '_5arcmin.mir', clobber=clobber_hi)
        else:
            desc = desc_av
            exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_5arcmin.mir\n'.format(image))
            regrid(image + '.mir', out=image + '_5arcmin.mir', desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0  # arcmin
    im_beams = np.array([3.7, 2.5, 2.5, 4.3])  # arcmin
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_hi, im_k09, im_lee_2mass, im_lee_iris]

    for i, image in enumerate(images):
        if image == im_hi:
            desc = desc_hi
            exists = check_file(image + '_smooth_planckres.mir',
                                clobber=clobber_hi)
        else:
            desc = desc_av
            exists = check_file(image + '_smooth_planckres.mir',
                                clobber=clobber)
        print image

        if not exists:
            if images[i] == im_hi:
                image = images[i] + '_5arcmin'
            else:
                image = images[i]

            print('\tSmoothing {:s}.mir\n'.format(image))

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_smooth_planckres.mir\n'.format(image))
            if image == im_co or image == im_lee_2mass or image == im_lee_iris:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))

    # Bin the images
    print('\nBin the images')

    images = [
        im_pl + '_5arcmin', im_pl_err + '_5arcmin', im_Td + '_5arcmin',
        im_Td_err + '_5arcmin', im_beta + '_5arcmin', im_beta_err + '_5arcmin',
        im_pl2 + '_5arcmin', im_pl2_err + '_5arcmin',
        im_k09 + '_regrid_planckres', im_lee_2mass + '_regrid_planckres',
        im_lee_iris + '_regrid_planckres', im_co + '_regrid_planckres',
        im_hi + '_regrid_planckres'
    ]

    for image in images:
        exists = check_file(image + '_bin.mir', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.mir\n'.format(image))

            bin_size = 10
            if image == im_hi + '_regrid_planckres' or \
               image == im_co + '_regrid_planckres':
                bins = bin_size, bin_size, bin_size, bin_size, 1, 1
            else:
                bins = bin_size, bin_size, bin_size, bin_size,

            imbin(
                image + '.mir',
                out=image + '_bin.mir',
                bin=bins,
            )

    # Write the images out to fits images
    print('\nWriting images to fits format')

    for image in images:
        if image == im_hi + '_regrid_planckres':
            exists = check_file(image + '.fits', clobber=clobber_hi)
        else:
            exists = check_file(image + '.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.fits\n'.format(image))

            fits(image + '.mir', out=image + '.fits', op='xyout')

        exists = check_file(image + '_bin.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}_bin.fits\n'.format(image))

            fits(image + '_bin.mir', out=image + '_bin.fits', op='xyout')
def main():

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin
    from astropy.io import fits as pyfits
    import numpy as np

    os.chdir('/d/bip3/ezbc/perseus/data')

    # If true, deletes files to be written
    clobber = 1
    clobber_hi = 1

    # First, change zeros in lee image to nans
    data, header = pyfits.getdata('av/perseus_av_lee12_2mass.fits', header=True)
    data[data == 0] = np.nan
    pyfits.writeto('av/perseus_av_lee12_2mass_nan.fits',
                   data,
                   header,
                   clobber=True)

    in_images = ('hi/perseus_hi_galfa_cube',
                'hi/perseus_hi_galfa_dr1_cube',
              'av/perseus_av_planck_tau353',
              'av/perseus_av_error_planck_tau353',
              'dust_temp/perseus_dust_temp',
              'dust_temp/perseus_dust_temp_error',
              'av/perseus_av_planck_radiance',
              'av/perseus_av_error_planck_radiance',
              'co/perseus_co_cfa_cube',
              #'av/perseus_av_k09_nan',
              '/d/bip3/ezbc/perseus/data/av/perseus_av_k09_nan',
              'av/perseus_av_lee12_2mass_nan',
              'av/perseus_av_lee12_iris_masked')

    im_hi = 'hi/perseus_hi_galfa_cube'
    im_hi_dr1 = 'hi/perseus_hi_galfa_dr1_cube'
    im_pl = 'av/perseus_av_planck_tau353'
    im_pl_err = 'av/perseus_av_error_planck_tau353'
    im_Td = 'dust_temp/perseus_dust_temp'
    im_Td_err = 'dust_temp/perseus_dust_temp_error'
    im_pl2 = 'av/perseus_av_planck_radiance'
    im_pl2_err = 'av/perseus_av_error_planck_radiance'
    im_co = 'co/perseus_co_cfa_cube'
    im_k09 = 'av/perseus_av_k09'
    im_lee_2mass = 'av/perseus_av_lee12_2mass'
    im_lee_iris = 'av/perseus_av_lee12_iris'

    # Load the images into miriad
    print('\nLoading images into miriad...')
    out_images = (im_hi,
                  im_hi_dr1,
                  im_pl,
                  im_pl_err,
                  im_Td,
                  im_Td_err,
                  im_pl2,
                  im_pl2_err,
                  im_co,
                  im_k09,
                  im_lee_2mass,
                  im_lee_iris)

    for i in xrange(len(in_images)):
        if out_images[i] == im_hi:
            clobber_temp = clobber_hi
        else:
            clobber_temp = clobber
        exists = check_file(out_images[i] + '.mir', clobber=clobber_temp)
        if not exists:
            print('\tLoading {:s}.fits\n'.format(in_images[i]))
            fits(in_images[i] + '.fits',
                    out=out_images[i] + '.mir',
                    op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (im_pl, im_pl_err, im_Td, im_Td_err, im_pl2, im_pl2_err, im_hi,
            im_hi_dr1)

    desc = (59.75,0,-0.08333,180,26.05,0,0.08333,132)

    delta_ra = -0.083333333
    delta_dec = 0.083333333

    ref_pix, npix = calc_image_origin(x_limits=(70, 40),
                                      y_limits=(21, 38),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, im_hi_dr1):
            desc = desc_hi
        else:
            desc = desc_av

        exists = check_file(image + '_5arcmin.mir', clobber=clobber)


        if not exists:
            print('\tRegridding {:s}_5arcmin.mir\n'.format(image))
            regrid(image + '.mir',
                    out=image + '_5arcmin.mir',
                    desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0 # arcmin
    im_beams = np.array([3.7, 3.7, 2.5, 2.5, 4.3]) # arcmin
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_hi, im_hi_dr1, im_k09, im_lee_2mass, im_lee_iris]

    for i in xrange(len(images)):
        exists = check_file(images[i] + '_smooth_planckres.mir',
                            clobber=clobber)

        if not exists:
            if images[i] in (im_hi, im_hi_dr1):
                image = images[i] + '_5arcmin'
            else:
                image = images[i]

            print('\tSmoothing {:s}.mir\n'.format(image))

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_smooth_planckres.mir\n'.format(image))
            if image == im_co or image == im_lee_2mass or image == im_lee_iris:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
              im_pl + '_5arcmin',
              im_pl_err + '_5arcmin',
              im_Td + '_5arcmin',
              im_Td_err + '_5arcmin',
              im_pl2 + '_5arcmin',
              im_pl2_err + '_5arcmin',
              im_k09 + '_regrid_planckres',
              im_lee_2mass + '_regrid_planckres',
              im_lee_iris + '_regrid_planckres',
              im_co + '_regrid_planckres',
              im_hi + '_regrid_planckres',
              im_hi_dr1 + '_regrid_planckres',
              ]

    for image in images:
        exists = check_file(image + '.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.mir\n'.format(image))

            fits(image + '.mir',
                 out=image + '.fits',
                 op='xyout')

    # write masked Planck image as same as Lee+12
    data_pl, header = pyfits.getdata(im_pl + '_5arcmin.fits', header=True)
    data_l12 = pyfits.getdata(im_lee_iris + '_regrid_planckres.fits')
    data_pl[np.isnan(data_l12)] = np.nan
    pyfits.writeto(im_pl + '_5arcmin_lee12mask.fits',
                   data_pl,
                   header,
                   clobber=True)
def main():

    ''' Planck resolution is 5'. We will smooth and regrid all maps to the
    Planck map.

    Map                           Resolution      Convol size
    GALFA HI                      3.7'            3.363
    Kainulainen et al. (2009) Av  2.4'            4.386
    Pineda et al. (2010) Av       3.3'            3.75
    CfA CO Dame et al. (2001)     8'              No smoothing
    '''

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin

    os.chdir('/d/bip3/ezbc/california/data')

    # If true, deletes files to be written
    clobber = 1

    in_images = (
              'hi/california_hi_galfa_cube',
              'hi/california_hi_galfa_dr1_cube',
              'av/california_av_planck_tau353',
              'av/california_av_error_planck_tau353',
              'dust_temp/california_dust_temp',
              'dust_temp/california_dust_temp_error',
              '/d/bip3/ezbc/multicloud/data/av/multicloud_av_k09_nan',
              'co/california_co_cfa_cube')

    im_pl = 'av/california_av_planck_tau353'
    im_pl_err = 'av/california_av_error_planck_tau353'
    im_Td = 'dust_temp/california_dust_temp'
    im_Td_err = 'dust_temp/california_dust_temp_error'
    im_k09 = 'av/california_av_k09'
    im_hi = 'hi/california_hi_galfa_cube'
    im_hi_dr1 = 'hi/california_hi_galfa_dr1_cube'
    im_co = 'co/california_co_cfa_cube'

    # Load the images into miriad
    print('\nLoading images into miriad')
    out_images = (im_hi, im_hi_dr1, im_pl, im_pl_err,
                  im_Td,
                  im_Td_err,
            im_k09, im_co)

    for i in xrange(len(in_images)):
        exists = check_file(out_images[i] + '.mir', clobber=clobber)
        print('\t{:s}.fits\n'.format(in_images[i]))
        if not exists:
            fits(in_images[i] + '.fits',
                    out=out_images[i] + '.mir',
                    op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (im_pl, im_pl_err,
                  im_Td,
                  im_Td_err,
            im_hi, im_hi_dr1,
            )
    delta_ra = -0.083333333
    delta_dec = 0.083333333

    # Greater RA value comes first
    ref_pix, npix = calc_image_origin(x_limits=(81, 60),
                                      y_limits=(25, 39),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, im_hi_dr1):
            desc = desc_hi
        else:
        	desc = desc_av

        exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        print('\t{:s}_5arcmin.mir\n'.format(image))

        if not exists:
            regrid(image + '.mir',
                    out=image + '_5arcmin.mir',
                    desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0 # arcsec
    im_beams = np.array([3.7, 3.7, 2.7]) # arcsec
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_hi, im_hi_dr1, im_k09]

    for i in xrange(len(images)):
        exists = check_file(images[i] + \
                 '_smooth_planckres.mir', clobber=clobber)

        print('\t{:s}.mir\n'.format(images[i]))

        if not exists:
            if images[i] in (im_hi, im_hi_dr1):
                image = images[i] + '_5arcmin'
            else:
            	image = images[i]

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)


    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)
        print('\t{:s}_smooth_planckres.mir\n'.format(image))

        if not exists:
            if image == im_co:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [im_pl + '_5arcmin',
              im_pl_err + '_5arcmin',
              im_Td + '_5arcmin',
              im_Td_err + '_5arcmin',
              im_k09 + '_regrid_planckres',
              im_co + '_regrid_planckres',
              im_hi + '_regrid_planckres',
              im_hi_dr1 + '_regrid_planckres',
              ]

    for image in images:
        exists = check_file(image + '.fits', clobber=clobber)

        print('\t{:s}.mir\n'.format(image))

        fits(image + '.mir',
             out=image + '.fits',
             op='xyout')
def main():
    ''' Planck resolution is 5'. We will smooth and regrid all maps to the
    Planck map.

    Map                           Resolution      Convol size
    GALFA HI                      3.7'            3.363
    Kainulainen et al. (2009) Av  2.4'            4.386
    Pineda et al. (2010) Av       3.3'            3.75
    CfA CO Dame et al. (2001)     8'              No smoothing
    '''

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin

    os.chdir('/d/bip3/ezbc/california/data')

    # If true, deletes files to be written
    clobber = 1

    in_images = ('hi/california_hi_galfa_cube',
                 'hi/california_hi_galfa_dr1_cube',
                 'av/california_av_planck_tau353',
                 'av/california_av_error_planck_tau353',
                 'dust_temp/california_dust_temp',
                 'dust_temp/california_dust_temp_error',
                 '/d/bip3/ezbc/multicloud/data/av/multicloud_av_k09_nan',
                 'co/california_co_cfa_cube')

    im_pl = 'av/california_av_planck_tau353'
    im_pl_err = 'av/california_av_error_planck_tau353'
    im_Td = 'dust_temp/california_dust_temp'
    im_Td_err = 'dust_temp/california_dust_temp_error'
    im_k09 = 'av/california_av_k09'
    im_hi = 'hi/california_hi_galfa_cube'
    im_hi_dr1 = 'hi/california_hi_galfa_dr1_cube'
    im_co = 'co/california_co_cfa_cube'

    # Load the images into miriad
    print('\nLoading images into miriad')
    out_images = (im_hi, im_hi_dr1, im_pl, im_pl_err, im_Td, im_Td_err, im_k09,
                  im_co)

    for i in xrange(len(in_images)):
        exists = check_file(out_images[i] + '.mir', clobber=clobber)
        print('\t{:s}.fits\n'.format(in_images[i]))
        if not exists:
            fits(in_images[i] + '.fits', out=out_images[i] + '.mir', op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (
        im_pl,
        im_pl_err,
        im_Td,
        im_Td_err,
        im_hi,
        im_hi_dr1,
    )
    delta_ra = -0.083333333
    delta_dec = 0.083333333

    # Greater RA value comes first
    ref_pix, npix = calc_image_origin(x_limits=(81, 60),
                                      y_limits=(25, 39),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, im_hi_dr1):
            desc = desc_hi
        else:
            desc = desc_av

        exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        print('\t{:s}_5arcmin.mir\n'.format(image))

        if not exists:
            regrid(image + '.mir', out=image + '_5arcmin.mir', desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0  # arcsec
    im_beams = np.array([3.7, 3.7, 2.7])  # arcsec
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_hi, im_hi_dr1, im_k09]

    for i in xrange(len(images)):
        exists = check_file(images[i] + \
                 '_smooth_planckres.mir', clobber=clobber)

        print('\t{:s}.mir\n'.format(images[i]))

        if not exists:
            if images[i] in (im_hi, im_hi_dr1):
                image = images[i] + '_5arcmin'
            else:
                image = images[i]

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)
        print('\t{:s}_smooth_planckres.mir\n'.format(image))

        if not exists:
            if image == im_co:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
        im_pl + '_5arcmin',
        im_pl_err + '_5arcmin',
        im_Td + '_5arcmin',
        im_Td_err + '_5arcmin',
        im_k09 + '_regrid_planckres',
        im_co + '_regrid_planckres',
        im_hi + '_regrid_planckres',
        im_hi_dr1 + '_regrid_planckres',
    ]

    for image in images:
        exists = check_file(image + '.fits', clobber=clobber)

        print('\t{:s}.mir\n'.format(image))

        fits(image + '.mir', out=image + '.fits', op='xyout')
def main():
    ''' Planck resolution is 5'. We will smooth and regrid all maps to the
    Planck map.

    Map                           Resolution      Convol size
    GALFA HI                      3.7'            3.363
    Kainulainen et al. (2009) Av  2.4'            4.386
    Pineda et al. (2010) Av       3.3'            3.75
    CfA CO Dame et al. (2001)     8'              No smoothing
    '''

    from mirpy import fits, regrid, smooth
    from mycoords import calc_image_origin

    os.chdir('/d/bip3/ezbc/taurus/data')

    # If true, deletes files to be written
    clobber = 1

    in_images = (
        #'av/taurus_av_kainulainen2009_nan',
        '/d/bip3/ezbc/multicloud/data/av/multicloud_av_k09_nan',
        'av/taurus_av_pineda2010',
        'hi/taurus_hi_galfa_cube',
        'hi/taurus_hi_galfa_dr1_cube',
        'av/taurus_av_planck_tau353',
        'av/taurus_av_error_planck_tau353',
        'av/taurus_av_planck_radiance',
        'av/taurus_av_error_planck_radiance',
        'co/taurus_co_cfa_cube',
        'co/taurus_co_planck',
        'co/taurus_co_error_planck',
        'co/taurus_co_1-0_planck',
        'co/taurus_co_1-0_error_planck',
        'co/taurus_co_2-1_planck',
        'co/taurus_co_2-1_error_planck',
        'co/taurus_co_3-2_planck',
        'co/taurus_co_3-2_error_planck',
    )

    im_pl = 'av/taurus_av_planck_tau353'
    im_pl_err = 'av/taurus_av_error_planck_tau353'
    im_pl2 = 'av/taurus_av_planck_radiance'
    im_pl2_err = 'av/taurus_av_error_planck_radiance'
    im_hi = 'hi/taurus_hi_galfa_cube'
    im_hi_dr1 = 'hi/taurus_hi_galfa_dr1_cube'
    im_co = 'co/taurus_co_cfa_cube'
    im_p10 = 'av/taurus_av_p10'
    im_k09 = 'av/taurus_av_k09'
    im_pl_co = 'co/taurus_co_planck'
    im_pl_co_err = 'co/taurus_co_error_planck'
    im_pl_co_10 = 'co/taurus_co_1-0_planck'
    im_pl_co_10_err = 'co/taurus_co_1-0_error_planck'
    im_pl_co_21 = 'co/taurus_co_2-1_planck'
    im_pl_co_21_err = 'co/taurus_co_2-1_error_planck'
    im_pl_co_32 = 'co/taurus_co_3-2_planck'
    im_pl_co_32_err = 'co/taurus_co_3-2_error_planck'

    # Load the images into miriad
    print('\nLoading images into miriad')
    out_images = (
        im_k09,
        im_p10,
        im_hi,
        im_hi_dr1,
        im_pl,
        im_pl_err,
        im_pl2,
        im_pl2_err,
        im_co,
        im_pl_co,
        im_pl_co_err,
        im_pl_co_10,
        im_pl_co_10_err,
        im_pl_co_21,
        im_pl_co_21_err,
        im_pl_co_32,
        im_pl_co_32_err,
    )

    for i in xrange(len(in_images)):
        exists = check_file(out_images[i] + '.mir', clobber=clobber)
        print('\tReading {:s}.fits\n'.format(in_images[i]))
        if not exists:
            fits(in_images[i] + '.fits', out=out_images[i] + '.mir', op='xyin')

            print('\tout = ' + out_images[i] + '.mir')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (
        im_pl,
        im_pl_err,
        im_pl2,
        im_pl2_err,
        im_hi,
        im_hi_dr1,
        #im_pl_co,
        #im_pl_co_err,
        #im_pl_co_10,
        #im_pl_co_10_err,
        #im_pl_co_21,
        #im_pl_co_21_err,
        #im_pl_co_32,
        #im_pl_co_32_err,
    )

    delta_ra = -0.083333333
    delta_dec = 0.083333333
    lim_ra_wcs, lim_dec_wcs = 77.5, 18.0
    ref_ra_wcs, ref_dec_wcs = 0.0, 0.0

    ref_pix, npix = calc_image_origin(x_limits=(83, 53.0),
                                      y_limits=(15.0, 35.0),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image in (im_hi, im_hi_dr1):
            desc = desc_hi
        else:
            desc = desc_av

        exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        print('\tReading image {:s}.mir'.format(image))
        print('\tWriting image {:s}_5arcmin.mir\n'.format(image))

        if not exists:
            regrid(image + '.mir', out=image + '_5arcmin.mir', desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0  # arcsec
    im_beams = np.array([2.4, 3.3, 3.7, 3.7])  # arcsec
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_k09, im_p10, im_hi, im_hi_dr1]

    for i in xrange(len(images)):

        exists = check_file(images[i] + '_smooth_planckres.mir',
                            clobber=clobber)

        print('\t{:s}.mir\n'.format(image))

        if not exists:
            if images[i] in (im_hi, im_hi_dr1):
                image = images[i] + '_5arcmin'
            else:
                image = images[i]

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)
        print('\t{:s}_smooth_planckres.mir\n'.format(image))

        if not exists:
            if image == im_co:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1, 2))

    # Write the images out to fits images
    print('\nWriting images to fits format')

    images = [
        im_pl + '_5arcmin',
        im_pl_err + '_5arcmin',
        im_pl2 + '_5arcmin',
        im_pl2_err + '_5arcmin',
        im_pl_co + '_5arcmin',
        im_pl_co_err + '_5arcmin',
        im_pl_co_10 + '_5arcmin',
        im_pl_co_10_err + '_5arcmin',
        im_pl_co_21 + '_5arcmin',
        im_pl_co_21_err + '_5arcmin',
        im_pl_co_32 + '_5arcmin',
        im_pl_co_32_err + '_5arcmin',
        im_k09 + '_regrid_planckres',
        im_p10 + '_regrid_planckres',
        im_co + '_regrid_planckres',
        im_hi + '_regrid_planckres',
        im_hi_dr1 + '_regrid_planckres',
    ]

    for image in images:
        check_file(image + '.fits', clobber=clobber)

        print('\t{:s}.mir\n'.format(image))

        fits(image + '.mir', out=image + '.fits', op='xyout')
def main():

    from mirpy import fits, regrid, smooth, imbin
    from mycoords import calc_image_origin
    from astropy.io import fits as pyfits
    import numpy as np

    os.chdir('/d/bip3/ezbc/multicloud/data')

    # If true, deletes files to be written
    clobber = False
    clobber_hi = False

    # First, change zeros in lee image to nans
    data, header = pyfits.getdata('av/multicloud_av_lee12_2mass.fits', header=True)
    data[data == 0] = np.nan
    pyfits.writeto('av/multicloud_av_lee12_2mass_nan.fits',
                   data,
                   header,
                   clobber=True)
    data, header = pyfits.getdata('av/multicloud_av_k09.fits',
                                  header=True)
    data[data == -1] = np.nan
    pyfits.writeto('av/multicloud_av_k09_nan.fits',
                   data,
                   header,
                   clobber=True)

    in_images = ('hi/multicloud_hi_galfa_cube',
              'av/multicloud_av_planck',
              'av/multicloud_av_error_planck',
              'dust_temp/multicloud_dust_temp',
              'dust_temp/multicloud_dust_temp_error',
              'dust_temp/multicloud_dust_beta',
              'dust_temp/multicloud_dust_beta_error',
              'av/multicloud_av_planck_radiance',
              'av/multicloud_av_error_planck_radiance',
              'co/multicloud_co_cfa_cube',
              'av/multicloud_av_k09_nan',
              'av/multicloud_av_lee12_2mass_nan',
              'av/multicloud_av_lee12_iris_masked')

    im_hi = 'hi/multicloud_hi_galfa_cube'
    im_pl = 'av/multicloud_av_planck'
    im_pl_err = 'av/multicloud_av_error_planck'
    im_Td = 'dust_temp/multicloud_dust_temp'
    im_Td_err = 'dust_temp/multicloud_dust_temp_error'
    im_beta = 'dust_temp/multicloud_dust_beta'
    im_beta_err = 'dust_temp/multicloud_dust_beta_error'
    im_pl2 = 'av/multicloud_av_planck_radiance'
    im_pl2_err = 'av/multicloud_av_error_planck_radiance'
    im_co = 'co/multicloud_co_cfa_cube'
    im_k09 = 'av/multicloud_av_k09_nan'
    im_lee_2mass = 'av/multicloud_av_lee12_2mass'
    im_lee_iris = 'av/multicloud_av_lee12_iris'

    # Load the images into miriad
    print('\nLoading images into miriad')
    out_images = (im_hi,
                  im_pl,
                  im_pl_err,
                  im_Td,
                  im_Td_err,
                  im_beta,
                  im_beta_err,
                  im_pl2,
                  im_pl2_err,
                  im_co,
                  im_k09,
                  im_lee_2mass,
                  im_lee_iris)

    for i in xrange(len(in_images)):
        if in_images[i] == im_hi:
            exists = check_file(out_images[i] + '.mir', clobber=clobber_hi)
        else:
            exists = check_file(out_images[i] + '.mir', clobber=clobber)

        print('\tLoading {:s}.fits\n'.format(in_images[i]))
        if not exists:
            fits(in_images[i] + '.fits',
                 out=out_images[i] + '.mir',
                 op='xyin')

    # Regrid Planck images and HI image to have one beam/pixel
    print('\nRegridding Planck images')

    images = (im_pl, im_pl_err, im_Td, im_Td_err, im_beta, im_beta_err, im_pl2, im_pl2_err, im_hi)

    desc = (59.75,0,-0.08333,180,26.05,0,0.08333,132)

    delta_ra = -0.083333333
    delta_dec = 0.083333333

    ref_pix, npix = calc_image_origin(x_limits=(85, 32),
                                      y_limits=(15, 48),
                                      delta_x=delta_ra,
                                      delta_y=delta_dec)

    desc_av = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1]]

    low_vel = -100.0
    high_vel = 100.0
    vel_res = 0.16667
    vel_npix = int((high_vel - low_vel) / vel_res)
    ref_pix_vel = int(vel_npix / 2.0) * vel_res
    ref_pix_vel = vel_npix / 2.0

    desc_hi = [0, ref_pix[0], delta_ra, npix[0], \
               0, ref_pix[1], delta_dec, npix[1], \
               0, ref_pix_vel, vel_res, vel_npix]

    for image in images:

        # If HI, regrid the velocity axis as well
        if image == im_hi:
            desc = desc_hi
            exists = check_file(image + '_5arcmin.mir', clobber=clobber_hi)
        else:
            desc = desc_av
            exists = check_file(image + '_5arcmin.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_5arcmin.mir\n'.format(image))
            regrid(image + '.mir',
                    out=image + '_5arcmin.mir',
                    desc=desc)

    # Determine convolution beam sizes to smooth to Planck res
    print('\nSmoothing images to Planck resolution')

    planck_beam = 5.0 # arcmin
    im_beams = np.array([3.7, 2.5, 2.5, 4.3]) # arcmin
    conv_beams = (planck_beam**2 - im_beams**2)**0.5

    images = [im_hi, im_k09, im_lee_2mass, im_lee_iris]

    for i, image in enumerate(images):
        if image == im_hi:
            desc = desc_hi
            exists = check_file(image + '_smooth_planckres.mir',
                                clobber=clobber_hi)
        else:
            desc = desc_av
            exists = check_file(image + '_smooth_planckres.mir',
                                clobber=clobber)
        print image

        if not exists:
            if images[i] == im_hi:
                image = images[i] + '_5arcmin'
            else:
                image = images[i]

            print('\tSmoothing {:s}.mir\n'.format(image))

            smooth(image + '.mir',
                   out=images[i] + '_smooth_planckres.mir',
                   fwhm=conv_beams[i],
                   pa=0,
                   scale=0.0)

    # Regrid all images to Planck image
    print('\nRegridding images to Planck grid')

    images.append(im_co)

    for image in images:
        exists = check_file(image + '_regrid_planckres.mir', clobber=clobber)

        if not exists:
            print('\tRegridding {:s}_smooth_planckres.mir\n'.format(image))
            if image == im_co or image == im_lee_2mass or image == im_lee_iris:
                regrid(image + '.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))
            else:
                regrid(image + '_smooth_planckres.mir',
                       out=image + '_regrid_planckres.mir',
                       tin=im_pl + '_5arcmin.mir',
                       axes=(1,2))

    # Bin the images
    print('\nBin the images')

    images = [
              im_pl + '_5arcmin',
              im_pl_err + '_5arcmin',
              im_Td + '_5arcmin',
              im_Td_err + '_5arcmin',
              im_beta + '_5arcmin',
              im_beta_err + '_5arcmin',
              im_pl2 + '_5arcmin',
              im_pl2_err + '_5arcmin',
              im_k09 + '_regrid_planckres',
              im_lee_2mass + '_regrid_planckres',
              im_lee_iris + '_regrid_planckres',
              im_co + '_regrid_planckres',
              im_hi + '_regrid_planckres']

    for image in images:
        exists = check_file(image + '_bin.mir', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.mir\n'.format(image))

            bin_size = 10
            if image == im_hi + '_regrid_planckres' or \
               image == im_co + '_regrid_planckres':
                bins = bin_size, bin_size, bin_size, bin_size,1,1
            else:
                bins = bin_size, bin_size, bin_size, bin_size,

            imbin(image + '.mir',
                  out=image + '_bin.mir',
                  bin=bins,
                  )

    # Write the images out to fits images
    print('\nWriting images to fits format')

    for image in images:
        if image == im_hi + '_regrid_planckres':
            exists = check_file(image + '.fits', clobber=clobber_hi)
        else:
            exists = check_file(image + '.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}.fits\n'.format(image))

            fits(image + '.mir',
                 out=image + '.fits',
                 op='xyout')

        exists = check_file(image + '_bin.fits', clobber=clobber)

        if not exists:
            print('\tWriting {:s}_bin.fits\n'.format(image))

            fits(image + '_bin.mir',
                 out=image + '_bin.fits',
                 op='xyout')