Пример #1
0
def make_median(fnlist,outfile):
    """Creates a median image from a series of images.
    
    Inputs:
    fnlist -> List of strings pointing to fits images
    outfile -> Location of resultant median image
    
    """
    
    imagelist = []
    datalist = []
    for i in range(len(fnlist)):
        imagelist.append(openfits(fnlist[i]))
        datalist.append(imagelist[i][0].data)
    datalist = np.array(datalist)
    
    meddata = np.median(datalist,axis=0)
    
    medhdu = PrimaryHDU(meddata)
    medhdu.writeto(outfile,clobber=True)
    
    for i in range(len(fnlist)):
        imagelist[i].close()
    
    return
Пример #2
0
def test_dtypes(dask_array_in_mem, tmp_path, dtype):

    filename = tmp_path / 'test.fits'

    array = dask_array_in_mem.astype(dtype)

    hdu = PrimaryHDU(data=array)
    hdu.writeto(filename)

    with fits.open(filename) as hdulist_new:
        assert isinstance(hdulist_new[0].data, np.ndarray)
        np.testing.assert_allclose(hdulist_new[0].data, array.compute())
Пример #3
0
def test_save_primary_hdu(dask_array_in_mem, tmp_path):

    # Saving a Primary HDU directly

    filename = tmp_path / 'test.fits'

    hdu = PrimaryHDU(data=dask_array_in_mem)
    hdu.writeto(filename)

    with fits.open(filename) as hdulist_new:
        assert isinstance(hdulist_new[0].data, np.ndarray)
        np.testing.assert_allclose(hdulist_new[0].data,
                                   dask_array_in_mem.compute())
Пример #4
0
def test_scaled_minmax(dask_array_in_mem, tmp_path):

    filename = tmp_path / 'test.fits'

    hdu = PrimaryHDU(data=dask_array_in_mem)
    hdu.scale('int32', option='minmax')
    hdu.writeto(filename)

    with fits.open(filename) as hdulist_new:
        assert isinstance(hdulist_new[0].data, np.ndarray)
        np.testing.assert_allclose(hdulist_new[0].data,
                                   dask_array_in_mem.compute(),
                                   atol=1e-5)
Пример #5
0
def northupeastleft(filename='', data=None, header=None):
    from astropy.io.fits import getdata, PrimaryHDU
    if filename:
        data, header = getdata(filename, header=True)
    if header['cd1_1'] > 0:
        data = np.fliplr(data)
        header['cd1_1'] *= -1
        print 'flipping around x'
    if header['cd2_2'] < 0:
        data = np.flipud(data)
        header['cd2_2'] *= -1
        print 'flipping around y'
    if filename:
        from os import remove
        remove(filename)
        out_fits = PrimaryHDU(data=data, header=header)
        out_fits.writeto(filename, clobber=True, output_verify='fix')
    else:
        return data, header
Пример #6
0
def northupeastleft(filename='', data=None, header=None):
    from astropy.io.fits import getdata, PrimaryHDU
    if filename:
        data, header = getdata(filename, header=True)
    if header['cd1_1'] > 0:
        data = np.fliplr(data)
        header['cd1_1'] *= -1
        print 'flipping around x'
    if header['cd2_2'] < 0:
        data = np.flipud(data)
        header['cd2_2'] *= -1
        print 'flipping around y'
    if filename:
        from os import remove
        remove(filename)
        out_fits = PrimaryHDU(data=data, header=header)
        out_fits.writeto(filename, clobber=True, output_verify='fix')
    else:
        return data, header
Пример #7
0
def run_bsmem_using_image(datafile: str,
                          outputfile: str,
                          dim: int,
                          pixelsize: float,
                          imagehdu: fits.PrimaryHDU,
                          uvmax: float = None,
                          alpha: float = None) -> None:
    """Run bsmem using initial/prior image.

    Args:
      datafile:   Input OIFITS data filename.
      outputfile: Output FITS filename.
      dim:        Reconstructed image width (pixels).
      pixelsize:  Reconstructed image pixel size (mas).
      imagehdu:   FITS HDU containing initial/prior image.
      uvmax:      Maximum uv radius to select (waves).
      alpha:      Regularization hyperparameter.

    """
    tempimage = tempfile.NamedTemporaryFile(suffix='.fits',
                                            mode='wb',
                                            delete=False)
    imagehdu.writeto(tempimage.name, overwrite=True)
    tempimage.close()
    args = [
        BSMEM, '--noui',
        '--data=%s' % datafile, '--clobber',
        '--output=%s' % outputfile,
        '--dim=%d' % dim,
        '--pixelsize=%f' % pixelsize,
        '--sf=%s' % tempimage.name
    ]
    if uvmax is not None:
        args += ['--uvmax=%f' % uvmax]
    if alpha is not None:
        args += ['--autoalpha=3', '--alpha=%f' % alpha]
    else:
        args += ['--autoalpha=4']
    fullstdout = os.path.splitext(outputfile)[0] + '-out.txt'
    run_bsmem(args, fullstdout)
    os.remove(tempimage.name)
Пример #8
0
def test_long_header(dask_array_in_mem, tmp_path):

    # Make sure things work correctly if there is a long header in the HDU.

    filename = tmp_path / 'test.fits'

    # NOTE: we deliberately set up a long header here rather than add the
    # keys one by one to hdu.header as adding the header in one go used to
    # cause issues, so this acts as a regression test.
    header = fits.Header()
    for index in range(2048):
        header[f'KEY{index:x}'] = 0.

    hdu = PrimaryHDU(data=dask_array_in_mem, header=header)
    hdu.writeto(filename)

    with fits.open(filename) as hdulist_new:
        assert len(hdulist_new[0].header) == 2053
        assert isinstance(hdulist_new[0].data, np.ndarray)
        np.testing.assert_allclose(hdulist_new[0].data,
                                   dask_array_in_mem.compute())