niter = 100,
       threshold = '5.0mJy',
       interactive = False,
       gridder = 'mosaic',
       savemodel='none',
       )
exportfits(imagename='{0}_model_tclean_msclean.image'.format(base_name), fitsimage='{0}_model_tclean_msclean.image.fits'.format(base_name),  dropdeg=True, overwrite=True)
exportfits(imagename='{0}_model_tclean_msclean.model'.format(base_name), fitsimage='{0}_model_tclean_msclean.model.fits'.format(base_name),  dropdeg=True, overwrite=True)
exportfits(imagename='{0}_model_tclean_clean.residual'.format(base_name), fitsimage='{0}_model_tclean_clean.residual.fits'.format(base_name),  dropdeg=True, overwrite=True)

from astropy.io import fits
from FITS_tools import hcongrid
from astropy import wcs
import numpy as np
fits_in = fits.open(perseus_casa_image+".fits")
dirty_header = fits.getheader('{0}_model_tclean_dirty.image.fits'.format(base_name))
header_in = wcs.WCS(fits_in[0].header).celestial.to_header()
header_in['NAXIS1'] = fits_in[0].data.shape[-1]
header_in['NAXIS2'] = fits_in[0].data.shape[-2]
reproj_fits_in = hcongrid.hastrom(fits_in[0].data.squeeze(),
                                  header_in,
                                  dirty_header)
d_in = reproj_fits_in
d_out_dirty = fits.getdata('{0}_model_tclean_dirty.image.fits'.format(base_name))
d_out_cln = fits.getdata('{0}_model_tclean_clean.image.fits'.format(base_name))
d_out_mscln = fits.getdata('{0}_model_tclean_msclean.image.fits'.format(base_name))

print("Distance from dirty: {0}".format((np.nan_to_num(d_in-d_out_dirty)**2).sum()**0.5))
print("Distance from cln: {0}".format((np.nan_to_num(d_in-d_out_cln)**2).sum()**0.5))
print("Distance from mscln: {0}".format((np.nan_to_num(d_in-d_out_mscln)**2).sum()**0.5))
import astrodendro
from astropy import wcs
from astropy import coordinates
from astropy import table
from FITS_tools import hcongrid

contfile_original = fits.open(paths.pspath('perseus_250_to_w51.image.fits'))
contfile = fits.open(paths.pspath('perseus_250_model_tclean_clean.image.fits'))
data_original = contfile_original[0].data
data = contfile[0].data

header_in = wcs.WCS(contfile_original[0].header).celestial.to_header()
header_in['NAXIS1'] = contfile_original[0].data.shape[-1]
header_in['NAXIS2'] = contfile_original[0].data.shape[-2]
reproj_fits_in = hcongrid.hastrom(contfile_original[0].data.squeeze(),
                                  header_in,
                                  contfile[0].header)
data_original = reproj_fits_in
data_original[np.isnan(data)] = np.nan

# estimate the noise from the local standard deviation of the residuals
residfile = fits.open(paths.pspath('perseus_250_model_tclean_clean.residual.fits'))
resid = residfile[0].data
smresid = convolve_fft(np.nan_to_num(resid), Gaussian2DKernel(30))
# have *low* noise outside when adding noise to the input image
synthnoise = convolve_fft(np.abs(resid-smresid),  Gaussian2DKernel(30))
resid[np.isnan(resid)] = 0.01 # make the noise outside very high
noise = convolve_fft(np.abs(resid-smresid),  Gaussian2DKernel(30))
residfile[0].data = noise
residfile.writeto(paths.pspath('perseus_250_model_tclean_clean_noise.fits'), clobber=True)
# lowest reasonable noise level is 0.2 mJy/beam
exportfits(imagename='{0}_model_tclean_clean.residual'.format(base_name),
           fitsimage='{0}_model_tclean_clean.residual.fits'.format(base_name),
           dropdeg=True,
           overwrite=True)

from astropy.io import fits
from FITS_tools import hcongrid
from astropy import wcs
import numpy as np
fits_in = fits.open(perseus_casa_image + ".fits")
dirty_header = fits.getheader(
    '{0}_model_tclean_dirty.image.fits'.format(base_name))
header_in = wcs.WCS(fits_in[0].header).celestial.to_header()
header_in['NAXIS1'] = fits_in[0].data.shape[-1]
header_in['NAXIS2'] = fits_in[0].data.shape[-2]
reproj_fits_in = hcongrid.hastrom(fits_in[0].data.squeeze(), header_in,
                                  dirty_header)
d_in = reproj_fits_in
d_out_dirty = fits.getdata(
    '{0}_model_tclean_dirty.image.fits'.format(base_name))
d_out_cln = fits.getdata('{0}_model_tclean_clean.image.fits'.format(base_name))
d_out_mscln = fits.getdata(
    '{0}_model_tclean_msclean.image.fits'.format(base_name))

print("Distance from dirty: {0}".format(
    (np.nan_to_num(d_in - d_out_dirty)**2).sum()**0.5))
print("Distance from cln: {0}".format(
    (np.nan_to_num(d_in - d_out_cln)**2).sum()**0.5))
print("Distance from mscln: {0}".format(
    (np.nan_to_num(d_in - d_out_mscln)**2).sum()**0.5))
contfile = fits.open(paths.pspath('perseus_250_2_model_tclean_clean.image.fits'))
data_original = contfile_original[0].data
data = contfile[0].data

# Hack added to rescale the two images to match each other, otherwise none of
# the comparisons below make sense at all.  This hack breaks any "absolute"
# flux comparison (e.g., 'what would I see in Perseus?'), but it seems
# necessary because that comparison was already broken and/or the answer was
# "nothing".
data_original *= np.nanmax(data) / np.nanmax(data_original)

header_in = wcs.WCS(contfile_original[0].header).celestial.to_header()
header_in['NAXIS1'] = contfile_original[0].data.shape[-1]
header_in['NAXIS2'] = contfile_original[0].data.shape[-2]
reproj_fits_in = hcongrid.hastrom(contfile_original[0].data.squeeze(),
                                  header_in,
                                  contfile[0].header)
data_original = reproj_fits_in
data_original[np.isnan(data)] = np.nan

# estimate the noise from the local standard deviation of the residuals
residfile = fits.open(paths.pspath('perseus_250_2_model_tclean_clean.residual.fits'))
resid = residfile[0].data
smresid = convolve_fft(np.nan_to_num(resid), Gaussian2DKernel(30))
# have *low* noise outside when adding noise to the input image
synthnoise = convolve_fft(np.abs(resid-smresid),  Gaussian2DKernel(30))
resid[np.isnan(resid)] = 0.01 # make the noise outside very high
noise = convolve_fft(np.abs(resid-smresid),  Gaussian2DKernel(30))
residfile[0].data = noise
residfile.writeto(paths.pspath('perseus_250_2_model_tclean_clean_noise.fits'), clobber=True)
# lowest reasonable noise level is 0.2 mJy/beam