myfunctions.py 1.24 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
5
from astropy.io import fits
6 7 8 9 10 11


def Distribute_Njobs(n_cpu, n_jobs):
    n = np.repeat(int(n_jobs/n_cpu), n_cpu)
    n[0:n_jobs%n_cpu] += 1
    return n
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

def AddParametersToHeader(header, threshold, nsim, method, flux):

    header['thresh'] = threshold
    header['nsim'] = nsim
    header['method'] = method
    header['flux_mJy'] = flux.value


def WriteResult(filename, threshold, completness, purity, nsim, wcs4D, method, flux, sourcetable):
    allhducomp, allhdupur = [],[]
    for i, thresh in enumerate(threshold):
        hducomp = fits.ImageHDU(data=completness[i], name='completness_thresh_{:.1f}'.format(float(threshold[i])))
        AddParametersToHeader(hducomp.header, thresh, nsim, method, flux)
        allhducomp.append(hducomp)
        
        hdupur=fits.ImageHDU(data=purity[i], name='purity_thresh_{:.1f}'.format(float(threshold[i])))
        AddParametersToHeader(hdupur.header, thresh, nsim, method, flux)
        allhdupur.append(hdupur)
    
    wcs2D = wcs4D.sub([1,2])
    
    hdu = fits.PrimaryHDU(header=wcs2D.to_header())

    hdul = [hdu]+allhducomp+allhdupur+[fits.BinTableHDU(sourcetable)]
    hdul = fits.HDUList(hdul)
    hdul.writeto(filename, overwrite=True)