<commit_before>"""
~~~~~~~~
cubes.py
~~~~~~~~

From `agpy <http://code.google.com/p/agpy/source/browse/trunk/agpy/cubes.py>`_,
contains functions to perform various transformations on data cubes and their
headers.  

########

"""
from numpy import sqrt,repeat,indices,newaxis,pi,cos,sin,array,mean,sum,nansum
from math import acos,atan2,tan
import numpy
import copy
import os
import tempfile
import posang # agpy code
import pyspeckit
from pyspeckit.specwarnings import warn
try:
    from AG_fft_tools.convolve import smooth
    from pyspeckit.parallel_map import parallel_map
    smoothOK = True
except ImportError:
    smoothOK = False
try:
    import coords
    coordsOK = True
except ImportError:
    coordsOK = False
try: # pywcs + pyfits OR astropy, no mixing!
    import astropy.io.fits as pyfits
    import astropy.wcs as pywcs
    wcsOK = True
except ImportError:
    import pyfits
    try:
        import pywcs
        wcsOK = True
    except ImportError:
        wcsOK = False

dtor = pi/180.0

def flatten_header(header,delete=False):
    """
    Attempt to turn an N-dimensional fits header into a 2-dimensional header
    Turns all CRPIX[>2] etc. into new keywords with suffix 'A'

    header must be a pyfits.Header instance
    """

    if not isinstance(header,pyfits.Header):
        raise Exception("flatten_header requires a pyfits.Header instance")

    newheader = header.copy()

    for key in newheader.keys():
        try:
            if delete and int(key[-1]) >= 3 and key[:2] in ['CD','CR','CT','CU','NA']:
                newheader.pop(key)
            elif int(key[-1]) >= 3 and key[:2] in ['CD','CR','CT','CU','NA','PC']:
                newheader.rename_key(key,'A'+key,force=True)<commit_msg>flatten_header needs to get rid of cd3_...<commit_after>"""
~~~~~~~~
cubes.py
~~~~~~~~

From `agpy <http://code.google.com/p/agpy/source/browse/trunk/agpy/cubes.py>`_,
contains functions to perform various transformations on data cubes and their
headers.  

########

"""
from numpy import sqrt,repeat,indices,newaxis,pi,cos,sin,array,mean,sum,nansum
from math import acos,atan2,tan
import numpy
import copy
import os
import tempfile
import posang # agpy code
import pyspeckit
from pyspeckit.specwarnings import warn
try:
    from AG_fft_tools.convolve import smooth
    from pyspeckit.parallel_map import parallel_map
    smoothOK = True
except ImportError:
    smoothOK = False
try:
    import coords
    coordsOK = True
except ImportError:
    coordsOK = False
try: # pywcs + pyfits OR astropy, no mixing!
    import astropy.io.fits as pyfits
    import astropy.wcs as pywcs
    wcsOK = True
except ImportError:
    import pyfits
    try:
        import pywcs
        wcsOK = True
    except ImportError:
        wcsOK = False

dtor = pi/180.0

def flatten_header(header,delete=False):
    """
    Attempt to turn an N-dimensional fits header into a 2-dimensional header
    Turns all CRPIX[>2] etc. into new keywords with suffix 'A'

    header must be a pyfits.Header instance
    """

    if not isinstance(header,pyfits.Header):
        raise Exception("flatten_header requires a pyfits.Header instance")

    newheader = header.copy()

    for key in newheader.keys():
        try:
            if delete and int(key[-1]) >= 3 and key[:2] in ['CD','CR','CT','CU','NA']:
                newheader.pop(key)
            elif (int(key[-1]) >= 3 or int(key[2])>=3) and key[:2] in ['CD','CR','CT','CU','NA','PC']:
                newheader.rename_key(key,'A'+key,force=True)
{'fraction_non_alphanumeric': 0.11853756069694373, 'fraction_numerical': 0.008283347614967152, 'mean_word_length': 3.25, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '628832', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1114, 'n_words': 331}