Beispiel #1
0
def correctcard(img):
    from  pyfits import open as popen
    from numpy import asarray
    import re

    hdulist = popen(img)
    a = hdulist[0]._verify('fix')
    _header = hdulist[0].header
    for i in range(len(a)):
        if not a[i]:
            a[i] = ['']
    ww = asarray([i for i in range(len(a)) if (re.sub(' ', '', a[i][0]) != '')])
    if len(ww) > 0:
        newheader = []
        headername = []
        for j in _header.items():
            headername.append(j[0])
            newheader.append(j[1])
        hdulist.close()
        imm = popen(img, mode='update')
        _header = imm[0].header
        for i in ww:
            if headername[i]:
                try:
                    _header.update(headername[i], newheader[i])
                except:
                    _header.update(headername[i], 'xxxx')
        imm.flush()
        imm.close()
Beispiel #2
0
def correctcard(img):
    from  pyfits import open as popen
    from numpy import asarray
    import re

    hdulist = popen(img)
    a = hdulist[0]._verify('fix')
    _header = hdulist[0].header
    for i in range(len(a)):
        if not a[i]:
            a[i] = ['']
    ww = asarray([i for i in range(len(a)) if (re.sub(' ', '', a[i][0]) != '')])
    if len(ww) > 0:
        newheader = []
        headername = []
        for j in _header.items():
            headername.append(j[0])
            newheader.append(j[1])
        hdulist.close()
        imm = popen(img, mode='update')
        _header = imm[0].header
        for i in ww:
            if headername[i]:
                try:
                    _header.update(headername[i], newheader[i])
                except:
                    _header.update(headername[i], 'xxxx')
        imm.flush()
        imm.close()
def fluxcalib2d(img2d, sensfun):  # flux calibrate 2d images
    import pyfits
    import re, string
    from pyfits import open as popen
    from numpy import arange, array, float32
    from numpy import interp as ninterp
    import floyds
    from floyds.util import readhdr, readkey3, delete

    _tel = floyds.util.readkey3(floyds.util.readhdr(re.sub('\n', '', img2d)),
                                'TELID')
    if _tel == 'fts':
        _extinction = 'ssoextinct.dat'
        _observatory = 'sso'
    elif _tel == 'ftn':
        _extinction = 'maua.dat'
        _observatory = 'cfht'
    else:
        sys.exit('ERROR: observatory not recognised')

    data2d, hdr2d = pyfits.getdata(img2d, 0, header=True)
    if 'GRISM' in hdr2d: _arm = hdr2d['GRISM']
    else: _arm = ''
    xxd = arange(len(data2d[0, :]))
    crvald = popen(img2d)[0].header.get('CRVAL1')
    cdd = popen(img2d)[0].header.get('CD1_1')
    _exptime, _airmass = readkey3(readhdr(img2d),
                                  'exptime'), readkey3(readhdr(img2d),
                                                       'airmass')
    #  read sensfunction and interpole pixel of 2D image
    yys = popen(sensfun)[0].data
    crvals = popen(sensfun)[0].header.get('CRVAL1')
    cds = popen(sensfun)[0].header.get('CD1_1')
    yys = (10**(yys / 2.5)) * cds  # from sens iraf in sens flux
    xxs = arange(len(yys))
    aasens = crvals + (xxs) * cds
    xxs2 = (aasens - crvald) / cdd
    aasens2 = ninterp(xxd, xxs2, yys)
    #  read atmosferic function and interpole pixel of 2D image
    aae, yye = floyds.util.ReadAscii2(floyds.__path__[0] +
                                      '/standard/extinction/' + _extinction)
    aae, yye = array(aae, float), array(yye, float)
    xxe = (aae - crvald) / cdd
    atm_xx = ninterp(xxd, xxe, yye)
    aircorr = 10**(0.4 * array(atm_xx) * _airmass)
    img2df = re.sub('.fits', '_2df.fits', img2d)
    for i in range(len(data2d[:, 0])):
        data2d[i, :] = (
            (array(data2d[i, :] / _exptime) * array(aircorr)) / aasens2) * 1e20
    floyds.util.delete(img2df)
    pyfits.writeto(img2df, float32(data2d), hdr2d)
    floyds.util.updateheader(
        img2df, 0, {'SENSFUN' + _arm[0]: [string.split(sensfun, '/')[-1], '']})
    floyds.util.updateheader(
        img2df, 0,
        {'BUNIT': ['erg/cm2/s/A  10^20', 'Physical unit of array values']})
    return img2df
Beispiel #4
0
def _correct_extended_fits_keywords(log, pathToFits):
    """
    *Values within the fits header should be no longer than 80 characters*

    **Key Arguments:**
        - ``log`` -- logger
        - ``pathToFits`` -- path the the FITS file

    **Return:**
        - None
    """
    ################ > IMPORTS ################
    ## STANDARD LIB ##
    import re
    ## THIRD PARTY ##
    from pyfits import open as popen
    from numpy import asarray
    ## LOCAL APPLICATION ##

    ################ > VARIABLE SETTINGS ######

    ################ >ACTION(S) ################
    hdulist = popen(pathToFits)
    a = hdulist[0]._verify('fix')
    _header = hdulist[0].header
    for i in range(len(a)):
        if not a[i]:
            a[i] = ['']
    ww = asarray([i for i in range(len(a)) if (
        re.sub(' ', '', a[i][0]) != '')])
    if len(ww) > 0:
        newheader = []
        headername = []
        for j in _header.items():
            headername.append(j[0])
            newheader.append(j[1])
        hdulist.close()
        imm = popen(pathToFits, mode='update')
        _header = imm[0].header
        for i in ww:
            if headername[i]:
                try:
                    _header.update(headername[i], newheader[i])
                except:
                    _header.update(headername[i], 'xxxx')
        imm.flush()
        imm.close()

    return
Beispiel #5
0
def readhdr(img):
    from pyfits import open as popen

    try:
        hdr = popen(img)[0].header
    except:
        from floyds.util import correctcard

        try:
            correctcard(img)
        except:
            import sys

            sys.exit('image ' + str(img) + ' is corrupted, delete it and start again')
        hdr = popen(img)[0].header
    return hdr
Beispiel #6
0
def sortbyJD(lista):
    from pyfits import open as popen
    from numpy import array

    JDlist = []
    for img in lista:
        hdr = popen(img)[0].header
        if 'MJD' in hdr:
            JDlist.append(popen(img)[0].header.get('MJD'))
        else:
            JDlist.append(popen(img)[0].header.get('MJD-OBS'))
    lista = array(lista)
    JDlist = array(JDlist)
    inds = JDlist.argsort()
    sortedlista = lista[inds]
    return list(sortedlista)
Beispiel #7
0
def readhdr(img):
    from pyfits import open as popen

    try:
        hdr = popen(img)[0].header
    except:
        from floyds.util import correctcard

        try:
            correctcard(img)
        except:
            import sys

            sys.exit('image ' + str(img) + ' is corrupted, delete it and start again')
        hdr = popen(img)[0].header
    return hdr
Beispiel #8
0
def sortbyJD(lista):
    from pyfits import open as popen
    from numpy import array

    JDlist = []
    for img in lista:
        hdr = popen(img)[0].header
        if 'MJD' in hdr:
            JDlist.append(popen(img)[0].header.get('MJD'))
        else:
            JDlist.append(popen(img)[0].header.get('MJD-OBS'))
    lista = array(lista)
    JDlist = array(JDlist)
    inds = JDlist.argsort()
    sortedlista = lista[inds]
    return list(sortedlista)
def fluxcalib2d(img2d,sensfun):  # flux calibrate 2d images
    import pyfits
    import re,string
    from pyfits import open as popen
    from numpy import arange, array, float32
    from numpy import interp as ninterp
    import floyds
    from floyds.util import readhdr,readkey3,delete

    _tel=floyds.util.readkey3(floyds.util.readhdr(re.sub('\n','',img2d)),'TELID')
    if _tel=='fts':
        _extinction='ssoextinct.dat'
        _observatory='sso'
    elif _tel=='ftn':
        _extinction='maua.dat' 
        _observatory='cfht'
    else: sys.exit('ERROR: observatory not recognised')

    data2d, hdr2d = pyfits.getdata(img2d, 0, header=True)
    if 'GRISM' in hdr2d:  _arm=hdr2d['GRISM']
    else:                 _arm=''
    xxd=arange(len(data2d[0,:]))
    crvald=popen(img2d)[0].header.get('CRVAL1')
    cdd=popen(img2d)[0].header.get('CD1_1')
    _exptime,_airmass=readkey3(readhdr(img2d),'exptime'),readkey3(readhdr(img2d),'airmass')
    #  read sensfunction and interpole pixel of 2D image
    yys=popen(sensfun)[0].data
    crvals=popen(sensfun)[0].header.get('CRVAL1')
    cds=popen(sensfun)[0].header.get('CD1_1')    
    yys=(10**(yys/2.5))*cds                  # from sens iraf in sens flux
    xxs=arange(len(yys))
    aasens=crvals+(xxs)*cds
    xxs2=(aasens-crvald)/cdd
    aasens2=ninterp(xxd,xxs2,yys)
    #  read atmosferic function and interpole pixel of 2D image
    aae,yye=floyds.util.ReadAscii2(floyds.__path__[0]+'/standard/extinction/'+_extinction)
    aae,yye=array(aae,float),array(yye,float)
    xxe=(aae-crvald)/cdd
    atm_xx=ninterp(xxd,xxe,yye)
    aircorr=10**(0.4*array(atm_xx)*_airmass)
    img2df=re.sub('.fits','_2df.fits',img2d)
    for i in range(len(data2d[:,0])):  data2d[i,:]=((array(data2d[i,:]/_exptime)*array(aircorr))/aasens2)*1e20
    floyds.util.delete(img2df)
    pyfits.writeto(img2df, float32(data2d), hdr2d)
    floyds.util.updateheader(img2df,0,{'SENSFUN'+_arm[0]:[string.split(sensfun,'/')[-1],'']})
    floyds.util.updateheader(img2df,0,{'BUNIT':['erg/cm2/s/A  10^20','Physical unit of array values']})
    return img2df
Beispiel #10
0
def phase3header(img):
    import floyds
    import string
    from floyds.util import readhdr, readkey3
    from numpy import max, min, isfinite
    from pyfits import open as popen

    img_data = popen(img)[0].data
    hdr = readhdr(img)
    hedvec0 = {'DATAMIN': [float(min(img_data[isfinite(img_data)])), ''],
               'DATAMAX': [float(max(img_data[isfinite(img_data)])), ''],
               'ORIGIN': ['ESO', 'European Southern Observatory'],
               'PROCSOFT': ['floyds_' + str(floyds.__version__), 'pipeline version']}
    if readkey3(hdr, 'filter'):
        hedvec0['FILTER'] = [readkey3(hdr, 'filter'), 'Filter name']
    if readkey3(hdr, 'gain'):
        hedvec0['GAIN'] = [readkey3(hdr, 'gain'), 'Conversion from electrons to ADU']
    if readkey3(hdr, 'esoid'):
        hedvec0['OBID'] = [readkey3(hdr, 'esoid'), 'Observation block ID']
    if readkey3(hdr, 'esoprog'):
        hedvec0['PROG_ID'] = [readkey3(hdr, 'esoprog'), 'ESO program identification']
    if readkey3(hdr, 'tech'):
        hedvec0['OBSTECH'] = [readkey3(hdr, 'tech'), 'Observation technique']
    floyds.util.updateheader(img, 0, hedvec0)
Beispiel #11
0
def phase3header(img):
    import floyds
    import string
    from floyds.util import readhdr, readkey3
    from numpy import max, min, isfinite
    from pyfits import open as popen

    img_data = popen(img)[0].data
    hdr = readhdr(img)
    hedvec0 = {'DATAMIN': [float(min(img_data[isfinite(img_data)])), ''],
               'DATAMAX': [float(max(img_data[isfinite(img_data)])), ''],
               'ORIGIN': ['ESO', 'European Southern Observatory'],
               'PROCSOFT': ['floyds_' + str(floyds.__version__), 'pipeline version']}
    if readkey3(hdr, 'filter'):
        hedvec0['FILTER'] = [readkey3(hdr, 'filter'), 'Filter name']
    if readkey3(hdr, 'gain'):
        hedvec0['GAIN'] = [readkey3(hdr, 'gain'), 'Conversion from electrons to ADU']
    if readkey3(hdr, 'esoid'):
        hedvec0['OBID'] = [readkey3(hdr, 'esoid'), 'Observation block ID']
    if readkey3(hdr, 'esoprog'):
        hedvec0['PROG_ID'] = [readkey3(hdr, 'esoprog'), 'ESO program identification']
    if readkey3(hdr, 'tech'):
        hedvec0['OBSTECH'] = [readkey3(hdr, 'tech'), 'Observation technique']
    floyds.util.updateheader(img, 0, hedvec0)