コード例 #1
0
ファイル: test_updatewcs.py プロジェクト: jhunkeler/stwcs
def test_apply_d2im():
    from stwcs.updatewcs import apply_corrections as appc
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    fname = get_filepath('j94f05bgq_flt.fits', current_dir)
    d2imfile = get_filepath('new_wfc_d2i.fits')
    try:
        os.remove(fname)
    except OSError:
        pass
    shutil.copyfile(acs_orig_file, fname)
    fits.setval(fname, ext=0, keyword="D2IMFILE", value=d2imfile)
    fits.setval(fname, ext=0, keyword="IDCTAB", value='N/A')
    fits.setval(fname, ext=0, keyword="NPOLFILE", value='N/A')
    # If D2IMEXT does not exist, the correction should be applied
    assert appc.apply_d2im_correction(fname, d2imcorr=True)
    updatewcs.updatewcs(fname)

    # Test the case when D2IMFILE == D2IMEXT
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
    assert not appc.apply_d2im_correction(fname, d2imcorr=False)

    fits.setval(fname, ext=0, keyword='D2IMFILE', value="N/A")
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
    # No D2IMFILE keyword in primary header
    fits.delval(fname, ext=0, keyword='D2IMFILE')
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
コード例 #2
0
ファイル: test_updatewcs.py プロジェクト: albertfxwang/stwcs
def test_apply_d2im():
    from stwcs.updatewcs import apply_corrections as appc
    acs_orig_file = get_filepath('j94f05bgq_flt.fits')
    current_dir = os.path.abspath(os.path.curdir)
    fname = get_filepath('j94f05bgq_flt.fits', current_dir)
    d2imfile = get_filepath('new_wfc_d2i.fits')
    try:
        os.remove(fname)
    except OSError:
        pass
    shutil.copyfile(acs_orig_file, fname)
    fits.setval(fname, ext=0, keyword="D2IMFILE", value=d2imfile)
    fits.setval(fname, ext=0, keyword="IDCTAB", value='N/A')
    fits.setval(fname, ext=0, keyword="NPOLFILE", value='N/A')
    # If D2IMEXT does not exist, the correction should be applied
    fileobj = fits.open(fname, mode='update')
    assert appc.apply_d2im_correction(fileobj, d2imcorr=True)
    updatewcs.updatewcs(fname)

    # Test the case when D2IMFILE == D2IMEXT
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
    assert not appc.apply_d2im_correction(fname, d2imcorr=False)

    fits.setval(fname, ext=0, keyword='D2IMFILE', value="N/A")
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
    # No D2IMFILE keyword in primary header
    fits.delval(fname, ext=0, keyword='D2IMFILE')
    assert not appc.apply_d2im_correction(fname, d2imcorr=True)
コード例 #3
0
ファイル: fix_headers.py プロジェクト: g-carla/tesi_giulia
 def headerRenameKey2(self, key, newkey):
     for filename in self._matchingFiles:
         print('rename %s: %s= %s' % (filename, key, newkey))
         try:
             value = fits.getval(filename, key)
             fits.setval(filename, newkey, value)
             fits.delval(filename, key)
         except Exception as e:
             print(str(e))
             pass
コード例 #4
0
 def test_no_HDRNAME_no_WCSNAME(self):
     """
     Test create_headerlet stepping through all
     extensions in the science file
     """
     newf = get_filepath('ncomp.fits', os.path.abspath(os.path.curdir))
     shutil.copyfile(self.comp_file, newf)
     #fits.delval(newf, 'HDRNAME', ext=1)
     fits.delval(newf, 'WCSNAME', ext=1)
     with pytest.raises(KeyError):
         hlet = headerlet.create_headerlet(newf)
コード例 #5
0
 def test_no_HDRNAME_no_WCSNAME(self):
     """
     Test create_headerlet stepping through all
     extensions in the science file
     """
     newf = get_filepath('ncomp.fits', os.path.abspath(os.path.curdir))
     shutil.copyfile(self.comp_file, newf)
     #fits.delval(newf, 'HDRNAME', ext=1)
     fits.delval(newf, 'WCSNAME', ext=1)
     with pytest.raises(KeyError):
         hlet = headerlet.create_headerlet(newf)
コード例 #6
0
ファイル: fitsutils.py プロジェクト: aboucaud/pypher
def clear_comments(fits_file):
    """
    Delete the COMMENTS in the FITS header

    Parameters
    ----------
    fits_file: str
        Path to a FITS image file

    """
    for comment_key in ['COMMENT', 'comment', 'Comment']:
        try:
            pyfits.delval(fits_file, comment_key)
        except KeyError:
            pass
コード例 #7
0
def clear_comments(fits_file):
    """
    Delete the COMMENTS in the FITS header

    Parameters
    ----------
    fits_file: str
        Path to a FITS image file

    """
    for comment_key in ['COMMENT', 'comment', 'Comment']:
        try:
            pyfits.delval(fits_file, comment_key)
        except KeyError:
            pass
コード例 #8
0
ファイル: fix_headers.py プロジェクト: g-carla/tesi_giulia
 def headerDeleteKey(self, key):
     for filename in self._matchingFiles:
         print('delete %s: %s' % (filename, key))
         fits.delval(filename, key)
コード例 #9
0
def add_keywds(fits_file, only_update, missing_keywds, specific_keys_dict):
    '''
    This function adds the missing keywords from the hdr_keywords_dictionary.py (hkwd) file and gives
    the fake values taken from the dictionary sample_hdr_keywd_vals_dict.py (shkvd).
    Args:
        only_update: If false a copy of the original fits file will be created with the
                     updated header.
        missing_keywds: list, missing keywords will be appended here
        specific_keys_dict: dictionary with specific keys and values that need to be changed
    '''
    missing_keywds = list(OrderedDict.fromkeys(missing_keywds))
    #print ("specific_keys_dict = ", specific_keys_dict)
    # create name for updated fits file
    updated_fitsfile = fits_file
    if not only_update:
        updated_fitsfile = fits_file.replace('.fits', '_updatedHDR.fits')
        subprocess.run(["cp", fits_file, updated_fitsfile])
    # add missimg keywords
    wcs_keywds_from_main_hdr = {}
    print('Saving keyword values in file: ', updated_fitsfile)
    ext = 0
    for i, key in enumerate(missing_keywds):
        if key in specific_keys_dict:
            #print ("found it in the dict: ", key, specific_keys_dict[key])
            if specific_keys_dict[key] == 'remove':
                # keyword to be deleted
                try:
                    fits.delval(updated_fitsfile, key, ext)
                except:
                    KeyError
            else:
                # change the keyword to specified value
                fits.setval(updated_fitsfile,
                            key,
                            value=specific_keys_dict[key])
            continue
        # get the index of the keyword previous to the one you want to add
        prev_key_idx = list(shkvd.keywd_dict.keys()).index(key) - 1
        # add the keyword in the right place from the right dictionary
        new_value = shkvd.keywd_dict[key]
        after_key = list(shkvd.keywd_dict.keys())[prev_key_idx]
        if after_key == 'wcsinfo':
            after_key = list(shkvd.keywd_dict.keys())[prev_key_idx - 1]
        if key != 'wcsinfo':
            print("adding keyword: ", key, " in extension: primary    after: ",
                  after_key)
            # choose right value for GWA_XTIL according to the grating
            if key == "GWA_XTIL":
                grating = fits.getval(fits_file, "GRATING", 0)
                new_value = get_gwa_Xtil_val(grating, path_to_tilt_files)
                print("Replacing value of keyword ", key,
                      " corresponding to GRATING=", grating, "and value ",
                      new_value)
            # choose right value for GWA_YTIL according to the grating
            if key == "GWA_YTIL":
                grating = fits.getval(fits_file, "GRATING", 0)
                new_value = get_gwa_Ytil_val(grating, path_to_tilt_files)
                print("Replacing value of keyword ", key,
                      " corresponding to GRATING=", grating, "and value ",
                      new_value)
            fits.setval(updated_fitsfile,
                        key,
                        0,
                        value=new_value,
                        after=after_key)
        else:
            # go into the subdictionary for WCS keywords
            extname = 'sci'
            sci_hdr = fits.getheader(updated_fitsfile, extname)
            main_hdr = fits.getheader(updated_fitsfile, 0)
            for subkey, new_value in shkvd.keywd_dict["wcsinfo"].items():
                # first remove these keywords from the main header
                if subkey in main_hdr:
                    #val = fits.getval(updated_fitsfile, subkey, 0)
                    #wcs_keywds_from_main_hdr[subkey] = val
                    fits.delval(updated_fitsfile, subkey, 0)
                # following commented lines are not working
                # uncomment this line if wanting to use the original keyword value given by create_data
                #new_value = wcs_keywds_from_main_hdr[subkey]
                if subkey not in sci_hdr:
                    print("adding keyword: ", subkey, " in extension: ",
                          extname, " with value: ", new_value)
                    fits.setval(updated_fitsfile,
                                subkey,
                                1,
                                value=new_value,
                                after='EXTNAME')
            print("Science header has been updated.")
コード例 #10
0
def add_keywds(fits_file, only_update, missing_keywds, specific_keys_dict, mode_used):
    '''
    This function adds the missing keywords from the hdr_keywords_dictionary.py (hkwd) file and gives
    the fake values taken from the dictionary sample_hdr_keywd_vals_dict.py (shkvd).
    Args:
        only_update: If false a copy of the original fits file will be created with the
                     updated header.
        missing_keywds: list, missing keywords will be appended here
        specific_keys_dict: dictionary with specific keys and values that need to be changed
        mode_used: str or None, observation mode used FS, MOS, or IFU (if None then a configuration file
                    is expected to exist and contain a variable named mode_used)
    '''
    missing_keywds = list(OrderedDict.fromkeys(missing_keywds))
    # create name for updated fits file
    updated_fitsfile = fits_file
    if not only_update:
        updated_fitsfile = fits_file.replace('.fits', '_updatedHDR.fits')
        subprocess.run(["cp", fits_file, updated_fitsfile])
    # add missimg keywords
    print ('Saving keyword values in file: ', updated_fitsfile)
    ext = 0
    for i, key in enumerate(missing_keywds):
        if key in specific_keys_dict:
            #print ("found it in the dict: ", key, specific_keys_dict[key])
            if specific_keys_dict[key] == 'remove':
                # keyword to be deleted
                try:
                    fits.delval(updated_fitsfile, key, ext)
                except:
                    KeyError
            else:
                # change the keyword to specified value
                fits.setval(updated_fitsfile, key, value=specific_keys_dict[key])
            continue
        # get the index of the keyword previous to the one you want to add
        prev_key_idx = list(lev2bdict.keywd_dict.keys()).index(key) - 1
        # add the keyword in the right place from the right dictionary
        new_value = lev2bdict.keywd_dict[key]
        after_key = list(lev2bdict.keywd_dict.keys())[prev_key_idx]
        if after_key == 'wcsinfo':
            after_key = list(lev2bdict.keywd_dict.keys())[prev_key_idx-1]
        if key != 'wcsinfo':
            # the DATAMODL keyword will only be modified if mode is IFU
            if key == 'DATAMODL':
                if 'IFU' in mode_used:
                    new_value = 'IFUImageModel'
                else:
                    new_value = 'ImageModel'
            if key == 'GRATING':
                new_value = fits.getval(updated_fitsfile, 'GWA_POS', 0)
                grating = new_value
            if key == 'FILTER':
                new_value = fits.getval(updated_fitsfile, 'FWA_POS', 0)
            # choose right value for GWA_XTIL according to the grating
            if key == "GWA_XTIL":
                new_value = get_gwa_Xtil_val(grating, path_to_tilt_files)
                print("Replacing value of keyword ", key, " corresponding to GRATING=", grating, "and value ", new_value)
            # choose right value for GWA_YTIL according to the grating
            if key == "GWA_YTIL":
                new_value = get_gwa_Ytil_val(grating, path_to_tilt_files)
                print("Replacing value of keyword ", key, " corresponding to GRATING=", grating, "and value ", new_value)
            if key == "EXP_TYPE":
                new_value = set_exp_type_value(mode_used)
            fits.setval(updated_fitsfile, key, 0, value=new_value, after=after_key)
            print("adding keyword: ", key, " in extension: PRIMARY    after: ", after_key, "   value: ", new_value)
        else:
            # go into the sub-dictionary for WCS keywords
            extname = 'sci'
            sci_hdr = fits.getheader(updated_fitsfile, extname)
            main_hdr = fits.getheader(updated_fitsfile, 0)
            for subkey, new_value in lev2bdict.keywd_dict["wcsinfo"].items():
                # first remove these keywords from the main header
                if subkey in main_hdr:
                    #val = fits.getval(updated_fitsfile, subkey, 0)
                    #wcs_keywds_from_main_hdr[subkey] = val
                    try:
                        fits.delval(updated_fitsfile, subkey, 0)
                    except:
                        KeyError
                # following commented lines are not working
                # uncomment this line if wanting to use the original keyword value given by create_data
                #new_value = wcs_keywds_from_main_hdr[subkey]
                if subkey not in sci_hdr:
                    print("adding keyword: ", subkey, " in extension: ", extname, " with value: ", new_value)
                    fits.setval(updated_fitsfile, subkey, 1, value=new_value, after='EXTNAME')
    # if the keyword is not in the sample dictionary then remove it
    for key in lev2bdict.keywd_dict:
        if key != 'wcsinfo':
            try:
                fits.delval(updated_fitsfile, key, 1)
            except:
                KeyError
    print ("Main and science headers have been updated.")
    return updated_fitsfile
コード例 #11
0
    MEX = str(input('Is MEX "true" or "false"? '))

    if MEX == 'false':
        filepath = Path(input('what directory are the images in? '))
        filename = input('what are the image filenames? ')
        hdus = sorted(filepath.glob(filename))

        for hdu in hdus:
            print(hdu)
            fits.setval(
                hdu,
                'RADESYS',
                value=coords,
                comment='Coordinate System (keyword changed by SCantu)',
                before='RADECSYS')
            fits.delval(hdu, 'RADECSYS')

    elif MEX == 'true':

        filepath = Path(input('where are the MEX files? '))
        filename = input('what is the image filename? ')
        hdus = sorted(filepath.glob(filename))
        for hdu in hdus:
            print(hdu)
            for i in range(len(fits.info(hdu, output=False)) - 1):
                fits.setval(
                    hdu,
                    'RADESYS',
                    value=coords,
                    comment='Coordinate System (keyword changed by SCantu)',
                    before='RADECSYS',
コード例 #12
0
ファイル: header-fix.py プロジェクト: changsuchoi/cspy
def delhdrkey(im, kwds=kwds):
    h = fits.getheader(im)
    for k in kwds:
        if k in list(h.keys()):
            print('removing', k, h[k])
            fits.delval(im, k)
def add_keywds(fits_file, only_update, missing_keywds, specific_keys_dict):
    '''
    This function adds the missing keywords from the hdr_keywords_dictionary.py (hkwd) file and gives
    the fake values taken from the dictionary sample_hdr_keywd_vals_dict.py (shkvd).
    Args:
        only_update: If false a copy of the original fits file will be created with the
                     updated header.
        missing_keywds: list, missing keywords will be appended here
        specific_keys_dict: dictionary with specific keys and values that need to be changed
    '''
    missing_keywds = list(OrderedDict.fromkeys(missing_keywds))
    #print ("specific_keys_dict = ", specific_keys_dict)
    # create name for updated fits file
    updated_fitsfile = fits_file
    if not only_update:
        updated_fitsfile = fits_file.replace('.fits', '_updatedHDR.fits')
        subprocess.run(["cp", fits_file, updated_fitsfile])
    # add missimg keywords
    wcs_keywds_from_main_hdr = {}
    print('Saving keyword values in file: ', updated_fitsfile)
    ext = 0
    for i, key in enumerate(missing_keywds):
        if key in specific_keys_dict:
            #print ("found it in the dict: ", key, specific_keys_dict[key])
            if specific_keys_dict[key] == 'remove':
                # keyword to be deleted
                try:
                    fits.delval(updated_fitsfile, key, ext)
                except:
                    KeyError
            else:
                # change the keyword to specified value
                fits.setval(updated_fitsfile,
                            key,
                            value=specific_keys_dict[key])
            continue
        # get the index of the keyword previous to the one you want to add
        prev_key_idx = list(lev2bdict.keywd_dict.keys()).index(key) - 1
        # add the keyword in the right place from the right dictionary
        new_value = lev2bdict.keywd_dict[key]
        after_key = list(lev2bdict.keywd_dict.keys())[prev_key_idx]
        if after_key == 'wcsinfo':
            after_key = list(lev2bdict.keywd_dict.keys())[prev_key_idx - 1]
        if key != 'wcsinfo':
            print("adding keyword: ", key, " in extension: primary    after: ",
                  after_key)
            # the DATAMODL keyword will only be modified if mode is IFU
            if key == 'DATAMODL' and lev2bdict.keywd_dict[
                    'EXP_TYPE'] == 'NRS_IFU':
                new_value = 'IFUImageModel'
            fits.setval(updated_fitsfile,
                        key,
                        0,
                        value=new_value,
                        after=after_key)
        else:
            # go into the subdictionary for WCS keywords
            extname = 'sci'
            sci_hdr = fits.getheader(updated_fitsfile, extname)
            main_hdr = fits.getheader(updated_fitsfile, 0)
            for subkey, new_value in lev2bdict.keywd_dict["wcsinfo"].items():
                # first remove these keywords from the main header
                if subkey in main_hdr:
                    #val = fits.getval(updated_fitsfile, subkey, 0)
                    #wcs_keywds_from_main_hdr[subkey] = val
                    try:
                        fits.delval(updated_fitsfile, subkey, 0)
                    except:
                        KeyError
                # following commented lines are not working
                # uncomment this line if wanting to use the original keyword value given by create_data
                #new_value = wcs_keywds_from_main_hdr[subkey]
                if subkey not in sci_hdr:
                    print("adding keyword: ", subkey, " in extension: ",
                          extname, " with value: ", new_value)
                    fits.setval(updated_fitsfile,
                                subkey,
                                1,
                                value=new_value,
                                after='EXTNAME')
    # if the keyword is not in the sample dictionary then remove it
    for key in lev2bdict.keywd_dict:
        if key != 'wcsinfo':
            try:
                fits.delval(updated_fitsfile, subkey, 1)
            except:
                KeyError
    print("Main and science headers have been updated.")
コード例 #14
0
"""
Edit header

Usage:
python edit_header.py T2m3wb-20190826.143357-0072.fits KEYWORD value

IMAGETYP: arc, object

"""

from astropy.io import fits

import sys

if len(sys.argv)!=4:
    print('Usage: filename keyword value')

filename = sys.argv[1]

keyword = sys.argv[2]
value = sys.argv[3].replace(' ', '')
print "'%s' '%s'"%(keyword, value)

fits.delval(filename, keyword)
if keyword=='IMAGETYP':
    fits.setval(filename, keyword, value=value, after='OBJECT')
else:    
    fits.setval(filename, keyword, value=value)