Example #1
0
 def get_info(self):
     """Capture the output from the fits info() function."""
     s = io.StringIO()
     fits.info(self.filepath, s)
     s.seek(0)
     info_string = "\n".join(s.read().splitlines()[1:])
     return info_string
Example #2
0
    def testInfoConvienceFunction(self):
        """Test the info convience function in both the fits and stpyfits
           namespace."""

        assert_equal(
            stpyfits.info(self.data('o4sp040b0_raw.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
             (1, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
             (2, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
             (3, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', ''),
             (4, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
             (5, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
             (6, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', '')])

        assert_equal(fits.info(self.data('o4sp040b0_raw.fits'), output=False),
                     [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
                      (1, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
                      (2, 'ERR', 'ImageHDU', 71, (), '', ''),
                      (3, 'DQ', 'ImageHDU', 71, (), '', ''),
                      (4, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
                      (5, 'ERR', 'ImageHDU', 71, (), '', ''),
                      (6, 'DQ', 'ImageHDU', 71, (), '', '')])

        assert_equal(stpyfits.info(self.data('cdva2.fits'), output=False),
                     [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', '')])

        assert_equal(fits.info(self.data('cdva2.fits'), output=False),
                     [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', '')])
    def testwritetoConvienceFunction(self):
        """Test the writeto convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0

        stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
                         clobber=True)
        fits.writeto(self.temp('new1.fits'), hdul1[0].data,hdul1[0].header,
                     clobber=True)

        hdul.close()
        hdul1.close()

        info1 = fits.info(self.temp('new.fits'), output=False)
        info2 = stpyfits.info(self.temp('new.fits'), output=False)
        info3 = fits.info(self.temp('new1.fits'), output=False)
        info4 = stpyfits.info(self.temp('new1.fits'), output=False)

        assert_equal(info1, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info2,
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'int32', '')])
        assert_equal(info3, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info4,
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'uint8', '')])
def make_calib_rcs_fits():
    """ Make RCS Fits file for calibration Purposes"""
    logging.info("Reading RCS Data")
    kk = np.loadtxt(source+"/kids_data/rcslens.csv", delimiter=",",
                    skiprows=1)
    ra = kk[:, 0]
    dec = kk[:, 1]
    e1 = kk[:, 2]
    e2 = kk[:, 3]
    w = kk[:, 4]
    m = kk[:, 5]
    logging.info("Finished Reading Data")
    col1 = pf.Column(name="RA", format='D', array=ra)
    col2 = pf.Column(name="DEC", format='D', array=dec)
    col3 = pf.Column(name="e1", format='D', array=e1)
    col4 = pf.Column(name="e2", format='D', array=e2)
    col5 = pf.Column(name="w", format='D', array=w)
    col6 = pf.Column(name="m", format='D', array=m)
    col7 = pf.Column(name="m1", format='D', array=m+1)
    col8 = pf.Column(name="nege1", format='D', array=-e1)
    col9 = pf.Column(name="nege2", format='D', array=-e1)
    lenspairs = np.stack((e1,e2),axis=-1)
    np.random.shuffle(lenspairs)
    col10 = pf.Column(name="shufe1", format='D', array=lenspairs[:,0])
    col11 = pf.Column(name="shufe2", format='D', array=lenspairs[:,1])
    cols = pf.ColDefs([col1, col2, col3, col4, col5, col6,
                       col7, col8, col9, col10, col11])
    tbhdu = pf.BinTableHDU.from_columns(cols)
    tbhdu.writeto("rcslens_forcalib.fits")
    pf.info("rcslens_forcalib.fits")
    logging.info(pf.open("rcslens_forcalib.fits")[1].header)
Example #5
0
 def get_info(self):
     """Capture the output from the fits info() function."""
     s = io.StringIO()
     fits.info(self.filepath, s)
     s.seek(0)
     info_string = "\n".join(s.read().splitlines()[1:])
     return info_string
    def testInfoConvienceFunction(self):
        """Test the info convience function in both the fits and stpyfits
           namespace."""

        assert_equal(
            stpyfits.info(self.data('o4sp040b0_raw.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
             (1, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
             (2, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
             (3, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', ''),
             (4, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
             (5, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
             (6, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', '')])


        assert_equal(
            fits.info(self.data('o4sp040b0_raw.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
             (1, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
             (2, 'ERR', 'ImageHDU', 71, (), '', ''),
             (3, 'DQ', 'ImageHDU', 71, (), '', ''),
             (4, 'SCI', 'ImageHDU', 141, (62, 44), 'int16', ''),
             (5, 'ERR', 'ImageHDU', 71, (), '', ''),
             (6, 'DQ', 'ImageHDU', 71, (), '', '')])

        assert_equal(
            stpyfits.info(self.data('cdva2.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', '')])

        assert_equal(
            fits.info(self.data('cdva2.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', '')])
Example #7
0
    def testwritetoConvienceFunction(self):
        """Test the writeto convience function in both the fits and stpyfits
           namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0

        stpyfits.writeto(self.temp('new.fits'),
                         hdul[0].data,
                         header,
                         clobber=True)
        fits.writeto(self.temp('new1.fits'),
                     hdul1[0].data,
                     hdul1[0].header,
                     clobber=True)

        hdul.close()
        hdul1.close()

        info1 = fits.info(self.temp('new.fits'), output=False)
        info2 = stpyfits.info(self.temp('new.fits'), output=False)
        info3 = fits.info(self.temp('new1.fits'), output=False)
        info4 = stpyfits.info(self.temp('new1.fits'), output=False)

        assert_equal(info1, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info2,
                     [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'int32', '')])
        assert_equal(info3, [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')])
        assert_equal(info4,
                     [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'uint8', '')])
 def view(self, filepath):
     image_file = get_pkg_data_filename(filepath)
     fits.info(image_file)
     image_data = fits.getdata(image_file, ext=0)
     plt.figure()
     plt.imshow(image_data, cmap='gray')
     plt.colorbar()
def gettimeflux_120(tic_id, sector):
    manifest = mastURL.dload_url(tic_id, sector)
    url = manifest[1][0]
    fits.getdata(url, ext=1)
    fits.info(url)
    with fits.open(url, mode="readonly") as hdulist:
        time = hdulist[1].data['TIME']
        flux = hdulist[1].data['SAP_FLUX']
    return time, flux
def make_table(tess_dir, params_file, candidates_file):
    #read table
    params = pd.read_csv(params_file)
    candidates = pd.read_csv(candidates_file)
    candidates["curveID"] = candidates["curveID"].str.replace(" ", "")
    df = pd.merge(candidates, params, on="curveID", how="left")
    df.to_csv("merged.csv", index=False)
    #make empty chi-squared list
    chi_squared = np.zeros(len(df))
    for i, row in df.iterrows():
        fname = tess_dir + "/" + row["sector"] + "/" + row["tessFile"]
        #open fits file
        fits.info(fname)
        fits.getdata(fname, ext=1).columns
        with fits.open(fname, mode="readonly") as hdulist:
            tess_bjds = hdulist[1].data['TIME']
            pdcsap_fluxes = hdulist[1].data['PDCSAP_FLUX']
        #get params for this row
        T0 = row["tcorr"] - tess_bjds[0]
        RP = row["rp"]
        INC = row["i"]
        width = row["width"]
        #find the lightcurve minima to calculate the exoplanet period
        arr = pdcsap_fluxes / np.nanmedian(pdcsap_fluxes)
        arr[np.isnan(arr)] = np.nanmedian(arr)
        mu, std = norm.fit(1 / arr)
        peaks, _ = find_peaks(1 / arr, height=mu + 4 * std, distance=1000)
        p = np.diff(tess_bjds[peaks])
        #define parameters
        PER = np.mean(p)
        u_type = 'quadratic'
        u_param = [0.1, 0.3]
        t = tess_bjds - tess_bjds[0]
        #normalize flux
        fluxes_array = np.array(pdcsap_fluxes)
        outcounts = [
            i for i in fluxes_array
            if i >= np.mean(np.nan_to_num(fluxes_array))
        ]
        mu, sigma = norm.fit(outcounts)
        normalized_fluxes = fluxes_array / mu
        normalized_sigma = np.array([math.sqrt(i) / mu for i in fluxes_array])
        #calculate reduced chi-squared
        reduced_chi_squared = np.nansum((
            (normalized_fluxes -
             make_lightcurve(T0, RP, INC, PER, width, u_type, u_param, t))**2 /
            normalized_sigma**2) / 8)
        #add reduced chi-squared values to list
        chi_squared[i] = reduced_chi_squared
    #add reduced chi-squared values to column in .csv file
    df["reduced chi-squared"] = chi_squared
    #write table
    df.to_csv("chisquared_values.csv")
Example #11
0
    def test_InfoConvienceFunction(self):
        """Test the info convience function in both the fits and stpyfits
        namespace."""

        if ASTROPY_VER_GE20:
            ans1 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 1, 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 1, 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (3, 'DQ', 1, 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (4, 'SCI', 2, 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 2, 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (6, 'DQ', 2, 'ImageHDU', 71, (62, 44), 'int16', '')]
            ans2 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 1, 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 1, 'ImageHDU', 71, (), '', ''),
                    (3, 'DQ', 1, 'ImageHDU', 71, (), '', ''),
                    (4, 'SCI', 2, 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 2, 'ImageHDU', 71, (), '', ''),
                    (6, 'DQ', 2, 'ImageHDU', 71, (), '', '')]
            ans3 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 7, (10, 10), 'int32', '')]
            ans4 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 7, (), '', '')]
        else:
            ans1 = [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (3, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (4, 'SCI', 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (6, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', '')]
            ans2 = [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 'ImageHDU', 71, (), '', ''),
                    (3, 'DQ', 'ImageHDU', 71, (), '', ''),
                    (4, 'SCI', 'ImageHDU', 141, (62, 44),
                     'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 'ImageHDU', 71, (), '', ''),
                    (6, 'DQ', 'ImageHDU', 71, (), '', '')]
            ans3 = [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', '')]
            ans4 = [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', '')]

        assert stpyfits.info(self.data('o4sp040b0_raw.fits'),
                             output=False) == ans1
        assert fits.info(self.data('o4sp040b0_raw.fits'), output=False) == ans2

        assert stpyfits.info(self.data('cdva2.fits'), output=False) == ans3
        assert fits.info(self.data('cdva2.fits'), output=False) == ans4
def find_streaks():

    plt.style.use(astropy_mpl_style)
    for filename in glob.glob("*.fits"):
        fits.info(filename)
        # Read a fits image and create a Streak instance.
        streak = Streak(filename)

        # Detect streaks.
        streak.detect()

        # Write outputs and plot figures.
        streak.write_outputs()
        streak.plot_figures()
Example #13
0
def fitsinfo(filename):
    """
    Print a summary of the HDUs in a FITS file.

    Parameters
    ----------
    filename : str
        The path to a FITS file.
    """

    try:
        fits.info(filename)
    except OSError as e:
        log.error(str(e))
    return
Example #14
0
def fitsinfo(filename):
    """
    Print a summary of the HDUs in a FITS file.

    Parameters
    ----------
    filename : str
        The path to a FITS file.
    """

    try:
        fits.info(filename)
    except IOError as e:
        log.error(str(e))
    return
def choose_hdu(filename):
    finfo = fits.info(filename, output=False) # Returns a list of tuples.
    finfo_list = [item for item in finfo if 'COMPRESSED_IMAGE' in item]
    if not finfo_list:
        return finfo[0][0] # 0 if not compressed
    else:
        return finfo_list[0][0]
Example #16
0
def get_numpy_data(file_name: str):
    try:

        image_file = get_pkg_data_filename(file_name)
        LOG.debug(fits.info(image_file))

        image_data = fits.getdata(
            image_file,
            ext=0,
        )

        # deal with zeros and negative values in data
        zero_threshold_indices = image_data <= 0.
        image_data[zero_threshold_indices] = 1.e-20

        # take the log
        image_data = np.log(image_data)

        # now normalize
        # we need to take care to multiply by -1. because log scale
        # will go negative
        image_data *= -1.
        image_data *= (1. / image_data.max())

        LOG.debug(f" image new min:%s max:%s" %
                  (image_data.min(), image_data.max()))
        LOG.debug("Image Shape : ", image_data.shape)

    except:

        LOG.debug("Error opening file {}".format(file_name))
        image_data = None

    return image_data
Example #17
0
def stack_fits_files(in_dirname, out_filename=None):
    """
    Stacks a series of similar FITS files (e.g. flat fields, biases)
    
    
    Parameters
    ----------
    in_dirname : str
        The directory name with all the FITS files. All files in the directory will be stacked
    
    out_filename : str
        If not None, the name where the combined HDUList should be saved
        

    Returns
    -------
    hdu : astropy.HDUList
        A FITS object with the combines frames
        

    Examples
    --------
    ::
        
        >>> flats = stack_fits_files("./J_flats/", "HAWKI_flats_J.fits")
        (24, 4, 2048, 2048)
        (4, 2048, 2048)
        
        
    """

    fnames = glob.glob(in_dirname+"/*.fits")

    last_ext = len(fits.info(fnames[0], output=False))
    first_ext = 1 if last_ext > 1 else 0
    
    chips = []
    for fname in fnames:
        chips += [[fits.getdata(fname, ext=i) for i in range(first_ext, last_ext)]]

    chips = np.array(chips)

    print(chips.shape)
    chips = np.median(chips, axis=0)
    print(chips.shape)

    for i in range(last_ext - first_ext):
        chips[i] /= np.median(chips[i])

    a = fits.PrimaryHDU()
    b = [fits.ImageHDU(chip) for chip in chips]

    hdu = fits.HDUList()
    hdu.append(a)
    for bb in b:
        hdu.append(bb)

    hdu.writeto(out_filename, clobber=True)
    
    return hdu
Example #18
0
def fits_get(file, info=False, head=0, scaling=False):
    '''helper function to load FITS data set
    if only file data is given, return data (16bits) + header: d,h = fits_get(file)
    if info = True prints fits info (headers structure)
    if head is provided, returns data and selected header
    if scaling = True, return the file scaling
        The scaling will be a list of two tuple elelemts
        scaling[0] ->  IMGFMT_16_0_S scaling 
            with scaling[0][0] = bool: True if present and scaling[0][1] the actual scaling 
        scaling[1] ->  IMGFMT_24_8 scaling
            with scaling[0][0] = bool: True if present and scaling[0][1] the actual scaling 
    '''
    if info == True:
        try:
            return pyfits.info(file)
        except Exception:
            print("Unable to open fits file: {}", file)
            raise
    if scaling == True:
        index = 1
        scaling = {"Present": [False, True], "scaling": [0, 0]}
        while True:
            try:
                dummy_head = getheader(file, index)
            except Exception:
                print("Unable to open fits file: {}", file)
                raise
            if dummy_head['EXTNAME'] == 'PHI_FITS_imageSummary':
                with pyfits.open(file) as hdu_list:
                    header_data = hdu_list[index].data
                    #case 1 if that there is only ONE scaling (untouched data)
                    if len(header_data) == 1:
                        scaling["Present"][0] = False
                        scaling["Present"][1] = True
                        scaling["scaling"][0] = 0.
                        scaling["scaling"][1] = float(header_data[0][12])
                        return scaling
                    #case 2 if that there is more than TWO scaling data
                    if len(header_data) > 2:
                        #check the first one from below and if it is IMGFMT_16_0_S store it and continue
                        if header_data[-1][3] == 'IMGFMT_16_0_S':
                            scaling["Present"][0] = True
                            scaling["Present"][1] = True
                            scaling["scaling"][0] = float(header_data[-1][12])
                            scaling["scaling"][1] = float(header_data[-3][12])
                            return scaling
                        if header_data[-1][3] == 'IMGFMT_24_8':
                            scaling["Present"][0] = False
                            scaling["Present"][1] = True
                            scaling["scaling"][0] = 0.
                            scaling["scaling"][1] = float(header_data[-1][12])
                            return scaling
            index += 1
    with pyfits.open(file) as hdu_list:
        if head != 0:
            return hdu_list[head].data, hdu_list[head].header
        else:
            return hdu_list[head].data.astype(
                np.dtype('float32')), hdu_list[head].header
Example #19
0
def infofits(filename, **kargs):
    """
    Read and display the extension information for image "filename.fits"

    Syntax:
    infofits('filename.fits')



    Example:
    infofits('FORS2_IMG_CAL099.80.CHIP1.fits') which gives, for this file

    Filename: FORS2_IMG_CAL099.80.CHIP1.fits
    No.    Name         Type      Cards   Dimensions   Format
    0    CHIP1       PrimaryHDU     242   (2048, 1034)   int16

    For an OmegaCAM image we get

    infofits('OMEGACAM_IMG_FLAT120_0020.fits')

    Filename: OMEGACAM_IMG_FLAT120_0020.fits
    No.    Name         Type      Cards   Dimensions   Format
    0    PRIMARY     PrimaryHDU     612   ()
    1    ESO_CCD_#65  ImageHDU        52   (2144, 4200)   int16 (rescales to uint16)
    2    ESO_CCD_#66  ImageHDU        52   (2144, 4200)   int16 (rescales to uint16)
    3    ESO_CCD_#67  ImageHDU        52   (2144, 4200)   int16 (rescales to uint16)
    4    ESO_CCD_#68  ImageHDU        52   (2144, 4200)   int16 (rescales to uint16)
    5    ESO_CCD_#73  ImageHDU        52   (2144, 4200)   int16 (rescales to uint16)
    6    ESO_CCD_#74  ImageHDU        52   (2144, 4200)   int16 (rescales to uint16)
    ....etc (32 CCDs)


    """

    try:
        if kargs.get('RETURN',False):
            return pyfits.info(filename, output=False)
        else:
            pyfits.info(filename)


    except:
        print("Error opening the filename")
        return None

    return None
Example #20
0
File: fits.py Project: rag9704/PTS
def get_info(path):
    """
    This function ...
    :param path:
    :return:
    """

    return fits.info(path, output=False)
def make_sz_map_fits(nside=NSIDE):
    import astropy.units as u
    from astropy.coordinates import SkyCoord

    logging.info(f"For NSIDE = {nside}")
    szmap = hp.read_map(source+'/kids_data/nilc_ymaps.fits') #Just read maps reads the first column. 
                                                             # Verified that it is the same as hdu[1].data['FULL']

    maskfile = pf.open(source+'/kids_data/planck_mask.fits')
    maskno = 4
    mask = maskfile[1].data[f"M{maskno}"]
    szmap = hp.ud_grade(szmap,nside)
    bmap = []
    lmap = []
    ymap = []
    for i in tqdm(range(len(szmap))):
        if mask[i]:
            b, l = indextodeclra(i,nside=nside)
            lmap.append(l)
            bmap.append(b)
            ymap.append(szmap[i])

    lmap = np.array(lmap)
    bmap = np.array(bmap)
    gc_sz = SkyCoord(l=lmap, b=bmap, frame='galactic', unit='deg')
    gcout_sz=gc_sz.transform_to('icrs',merge_attributes=False)
    declmap, ramap = gcout_sz.dec.degree, gcout_sz.ra.degree

    ymap = np.array(ymap)
    print(ymap)
    print("Min",np.min(ymap))
    print("Max",np.max(ymap))
    print("Mean",np.mean(ymap))
    print("Std",np.std(ymap))
    exit()
    assert len(ymap) == len(ramap) == len(declmap)
    logging.info(f"Total Shape : {ymap.shape}")
    col1 = pf.Column(name="RA", format='D', array=ramap)
    col2 = pf.Column(name="DEC", format='D', array=declmap)
    col3 = pf.Column(name="y", format='D', array=ymap)
    cols = pf.ColDefs([col1, col2, col3])
    tbhdu = pf.BinTableHDU.from_columns(cols)
    tbhdu.writeto(f"szmaps_masked{maskno}_eq_{nside}.fits")
    pf.info(f"szmaps_masked{maskno}_eq_{nside}.fits")
    logging.info(pf.open(f"szmaps_masked{maskno}_eq_{nside}.fits")[1].header)
Example #22
0
File: fits.py Project: SKIRT/PTS
def get_info(path):

    """
    This function ...
    :param path:
    :return:
    """

    return fits.info(path, output=False)
Example #23
0
def choose_hdu(filename):
    ''' Verifies if fits file is compressed or not. only .fits or .fits.fz
        format are accepted'''
    finfo = fits.info(filename, output=False)  # Returns a list of tuples.
    finfo_list = [item for item in finfo if 'COMPRESSED_IMAGE' in item]
    if not finfo_list:
        return finfo[0][0]  # 0 if not compressed
    else:
        return finfo_list[0][0]
Example #24
0
    def test_InfoConvienceFunction(self):
        """Test the info convience function in both the fits and stpyfits
        namespace."""

        if ASTROPY_VER_GE20:
            ans1 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 1, 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 1, 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (3, 'DQ', 1, 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (4, 'SCI', 2, 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 2, 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (6, 'DQ', 2, 'ImageHDU', 71, (62, 44), 'int16', '')]
            ans2 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 1, 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 1, 'ImageHDU', 71, (), '', ''),
                    (3, 'DQ', 1, 'ImageHDU', 71, (), '', ''),
                    (4, 'SCI', 2, 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 2, 'ImageHDU', 71, (), '', ''),
                    (6, 'DQ', 2, 'ImageHDU', 71, (), '', '')]
            ans3 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 7, (10, 10), 'int32', '')]
            ans4 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 7, (), '', '')]
        else:
            ans1 = [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (3, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (4, 'SCI', 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 'ImageHDU', 71, (62, 44), 'int16', ''),
                    (6, 'DQ', 'ImageHDU', 71, (62, 44), 'int16', '')]
            ans2 = [(0, 'PRIMARY', 'PrimaryHDU', 215, (), '', ''),
                    (1, 'SCI', 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (2, 'ERR', 'ImageHDU', 71, (), '', ''),
                    (3, 'DQ', 'ImageHDU', 71, (), '', ''),
                    (4, 'SCI', 'ImageHDU', 141, (62, 44), 'int16 (rescales to uint16)', ''),
                    (5, 'ERR', 'ImageHDU', 71, (), '', ''),
                    (6, 'DQ', 'ImageHDU', 71, (), '', '')]
            ans3 = [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', '')]
            ans4 = [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', '')]

        assert stpyfits.info(self.data('o4sp040b0_raw.fits'), output=False) == ans1
        assert fits.info(self.data('o4sp040b0_raw.fits'), output=False) == ans2

        assert stpyfits.info(self.data('cdva2.fits'), output=False) == ans3
        assert fits.info(self.data('cdva2.fits'), output=False) == ans4
    def view(self, filepath):
        '''
		Parameters
		----------
		filepath: A `string` representing absolute path of file in system.

		Returns
		-------
		An instance of `matplotlib.image.AxesImage`, returned using `plt.imshow(data)`.
		'''
        image_file = get_pkg_data_filename(filepath)
        fits.info(image_file)
        image_data = fits.getdata(image_file, ext=0)
        #print(image_data.shape)
        plt.figure()
        plt.imshow(image_data, cmap='gray')
        plt.colorbar()

        return NotImplementedError
Example #26
0
    def test_append_primary_to_empty_list(self):
        # Tests appending a Simple PrimaryHDU to an empty HDUList.
        hdul = fits.HDUList()
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul.append(hdu)
        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100, ), 'int32', '')]
        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-append.fits'))

        assert fits.info(self.temp('test-append.fits'), output=False) == info
Example #27
0
    def test_file_like_2(self):
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        tmpfile = open(self.temp('tmpfile.fits'), 'wb')
        hdul = fits.open(tmpfile, mode='ostream')
        hdul.append(hdu)
        hdul.flush()
        tmpfile.close()
        hdul.close()

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', '')]
        assert fits.info(self.temp('tmpfile.fits'), output=False) == info
Example #28
0
def show_fits():
    #Collect fits file
    plt.style.use(astropy_mpl_style)
    image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
    #Create a new figure to plot fits file with
    f = aplpy.FITSFigure(image_file)
    #Save file
    f.save('my_first_plot.eps')

    #Display structure of file
    fits.info(image_file)

    #Get image details
    image_data = fits.getdata(image_file, ext=0)
    print(image_data.shape)

    # Display the image data
    plt.figure()
    plt.imshow(image_data, cmap='gray')
    plt.colorbar()
Example #29
0
    def test_file_like_2(self):
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        tmpfile = open(self.temp('tmpfile.fits'), 'wb')
        hdul = fits.open(tmpfile, mode='ostream')
        hdul.append(hdu)
        hdul.flush()
        tmpfile.close()
        hdul.close()

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100, ), 'int32', '')]
        assert fits.info(self.temp('tmpfile.fits'), output=False) == info
Example #30
0
    def test_append_primary_to_empty_list(self):
        # Tests appending a Simple PrimaryHDU to an empty HDUList.
        hdul = fits.HDUList()
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul.append(hdu)
        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', '')]
        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-append.fits'))

        assert fits.info(self.temp('test-append.fits'), output=False) == info
Example #31
0
    def test_insert_primary_to_empty_list(self):
        """Tests inserting a Simple PrimaryHDU to an empty HDUList."""
        hdul = fits.HDUList()
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul.insert(0, hdu)

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100, ), 'int32', '')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #32
0
    def test_insert_primary_to_empty_list(self):
        """Tests inserting a Simple PrimaryHDU to an empty HDUList."""
        hdul = fits.HDUList()
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul.insert(0, hdu)

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', '')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
def make_rcs_fits():
    """Convert the RCS CSV to FITS File."""
    logging.info("Making RCS Fits file from RCS Data")
    kk = np.loadtxt(source+"/kids_data/rcslens.csv", delimiter=",",
                    skiprows=1)
    ra = kk[:, 0]
    dec = kk[:, 1]
    e1 = kk[:, 2]
    e2 = kk[:, 3]
    w = kk[:, 4]
    m = kk[:, 5]
    logging.info("Finished Reading Data")

    """
    szmap = hp.read_map(source+'/kids_data/COM_CompMap_Compton-SZMap-milca-ymaps_2048_R2.00.fits')
    szlist = []
    for i in tqdm(range(len(ra))):
        index = int(declratoindex(dec[i], ra[i], NSIDE))
        szlist.append(szmap[index])
    szlist = np.array(szlist)

    shearcalib = np.ones(m.shape) + m
    e1_calib = e1/shearcalib
    e2_calib = e2/shearcalib
    w_calib = w*shearcalib
    """
    col1 = pf.Column(name="RA", format='D', array=ra)
    col2 = pf.Column(name="DEC", format='D', array=dec)
    col3 = pf.Column(name="e1", format='D', array=e1)
    col4 = pf.Column(name="e2", format='D', array=e2)
    col5 = pf.Column(name="w", format='D', array=w)
    col6 = pf.Column(name="m", format='D', array=m)
    col7 = pf.Column(name="m1", format='D', array=m+1)
    col8 = pf.Column(name="nege1", format='D', array=-e1)
    cols = pf.ColDefs([col1, col2, col3, col4, col5, col6, col7, col8])
    tbhdu = pf.BinTableHDU.from_columns(cols)
    tbhdu.writeto("rcslens.fits")
    pf.info("rcslens.fits")
    logging.info(pf.open("rcslens.fits")[1].header)
Example #34
0
def get_ext_list(filename, substr):
    '''
    Use fits.info to get a list of all the extensions in the image file
    ext_list = get_ext_list(filename, substr)
    ext_list = get_ext_list('MUSE_001.fits', 'CHAN')

    '''
    if Path(filename).is_file():
        info = pyfits.info(filename, output=False)
        ext_list = [i[1] for i in info if (substr in i[1])]
        return ext_list
    else:
        print(f'File "{filename}" not exist')
Example #35
0
    def test_writetoConvienceFunction(self):
        """Test the writeto convience function in both the fits and stpyfits
        namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0

        stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
                         **self.writekwargs)
        fits.writeto(self.temp('new1.fits'), hdul1[0].data, hdul1[0].header,
                     **self.writekwargs)

        hdul.close()
        hdul1.close()

        info1 = fits.info(self.temp('new.fits'), output=False)
        info2 = stpyfits.info(self.temp('new.fits'), output=False)
        info3 = fits.info(self.temp('new1.fits'), output=False)
        info4 = stpyfits.info(self.temp('new1.fits'), output=False)

        if ASTROPY_VER_GE20:
            ans1 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (), '', '')]
            ans2 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (10, 10), 'int32', '')]
            ans3 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (), '', '')]
            ans4 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (10, 10), 'uint8', '')]
        else:
            ans1 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')]
            ans2 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'int32', '')]
            ans3 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')]
            ans4 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'uint8', '')]

        assert info1 == ans1
        assert info2 == ans2
        assert info3 == ans3
        assert info4 == ans4
Example #36
0
    def test_writetoConvienceFunction(self):
        """Test the writeto convience function in both the fits and stpyfits
        namespace."""

        hdul = stpyfits.open(self.data('cdva2.fits'))
        hdul1 = fits.open(self.data('cdva2.fits'))

        header = hdul[0].header.copy()
        header['NAXIS'] = 0

        stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
                         **self.writekwargs)
        fits.writeto(self.temp('new1.fits'), hdul1[0].data, hdul1[0].header,
                     **self.writekwargs)

        hdul.close()
        hdul1.close()

        info1 = fits.info(self.temp('new.fits'), output=False)
        info2 = stpyfits.info(self.temp('new.fits'), output=False)
        info3 = fits.info(self.temp('new1.fits'), output=False)
        info4 = stpyfits.info(self.temp('new1.fits'), output=False)

        if ASTROPY_VER_GE20:
            ans1 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (), '', '')]
            ans2 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (10, 10), 'int32', '')]
            ans3 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (), '', '')]
            ans4 = [(0, 'PRIMARY', 1, 'PrimaryHDU', 6, (10, 10), 'uint8', '')]
        else:
            ans1 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')]
            ans2 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'int32', '')]
            ans3 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', '')]
            ans4 = [(0, 'PRIMARY', 'PrimaryHDU', 6, (10, 10), 'uint8', '')]

        assert info1 == ans1
        assert info2 == ans2
        assert info3 == ans3
        assert info4 == ans4
Example #37
0
    def test_append_table_extension_to_empty_list(self):
        """Tests appending a Simple Table ExtensionHDU to a empty HDUList."""

        hdul = fits.HDUList()
        hdul1 = fits.open(self.data('tb.fits'))
        hdul.append(hdul1[1])
        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 4, (), '', ''),
                (1, '', 1, 'BinTableHDU', 24, '2R x 4C', '[1J, 3A, 1E, 1L]', '')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-append.fits'))

        assert fits.info(self.temp('test-append.fits'), output=False) == info
Example #38
0
    def test_append_table_extension_to_empty_list(self):
        """Tests appending a Simple Table ExtensionHDU to a empty HDUList."""

        hdul = fits.HDUList()
        with fits.open(self.data('tb.fits')) as hdul1:
            hdul.append(hdul1[1])
            info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 4, (), '', ''),
                    (1, '', 1, 'BinTableHDU', 24, '2R x 4C', '[1J, 3A, 1E, 1L]', '')]

            assert hdul.info(output=False) == info

            hdul.writeto(self.temp('test-append.fits'))

        assert fits.info(self.temp('test-append.fits'), output=False) == info
Example #39
0
def choose_hdu(filename, fast=False):
    '''
    Detect whether the fits file is compressed with
    fpack, and choose the right HDU.
    fast: Alternative mode based on fitsio
    '''
    if fast:
        finfo = fitsio.FITS(filename)  # Object
        finfo_list = [f.get_extnum() for f in finfo if f.is_compressed()]
    else:
        finfo = fits.info(filename, output=False)  # List of tuples.
        finfo_list = [f[0] for f in finfo if 'COMPRESSED_IMAGE' in f]

    return 0 if not finfo_list else 1  # finfo=0 # finfo_list[0]=1
Example #40
0
    def test_insert_groupshdu_to_empty_list(self):
        """Tests inserting a Simple GroupsHDU to an empty HDUList."""

        hdul = fits.HDUList()
        hdu = fits.GroupsHDU()
        hdul.insert(0, hdu)

        info = [(0, 'PRIMARY', 1, 'GroupsHDU', 8, (), '',
                 '1 Groups  0 Parameters')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #41
0
    def test_insert_groupshdu_to_empty_list(self):
        """Tests inserting a Simple GroupsHDU to an empty HDUList."""

        hdul = fits.HDUList()
        hdu = fits.GroupsHDU()
        hdul.insert(0, hdu)

        info = [(0, 'PRIMARY', 1, 'GroupsHDU', 8, (), '',
                 '1 Groups  0 Parameters')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #42
0
    def test_insert_primary_to_non_empty_list(self):
        """Tests inserting a Simple PrimaryHDU to a non-empty HDUList."""

        with fits.open(self.data('arange.fits')) as hdul:
            hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
            hdul.insert(1, hdu)

            info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 7, (11, 10, 7), 'int32',
                     ''), (1, '', 1, 'ImageHDU', 6, (100, ), 'int32', '')]

            assert hdul.info(output=False) == info

            hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #43
0
    def test_insert_primary_to_non_empty_list(self):
        """Tests inserting a Simple PrimaryHDU to a non-empty HDUList."""

        hdul = fits.open(self.data('arange.fits'))
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul.insert(1, hdu)

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 7, (11, 10, 7), 'int32', ''),
                (1, '', 1, 'ImageHDU', 6, (100,), 'int32', '')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #44
0
    def test_insert_extension_to_primary_in_non_empty_list(self):
        # Tests inserting a Simple ExtensionHDU to a non-empty HDUList.
        hdul = fits.open(self.data('tb.fits'))
        hdul.insert(0, hdul[1])

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 4, (), '', ''),
                (1, '', 1, 'BinTableHDU', 24, '2R x 4C', '[1J, 3A, 1E, 1L]', ''),
                (2, '', 1, 'ImageHDU', 12, (), '', ''),
                (3, '', 1, 'BinTableHDU', 24, '2R x 4C', '[1J, 3A, 1E, 1L]', '')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #45
0
    def test_file_like(self):
        """
        Tests the use of a file like object with no tell or seek methods
        in HDUList.writeto(), HDULIST.flush() or astropy.io.fits.writeto()
        """

        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul = fits.HDUList()
        hdul.append(hdu)
        tmpfile = open(self.temp('tmpfile.fits'), 'wb')
        hdul.writeto(tmpfile)
        tmpfile.close()

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', '')]

        assert fits.info(self.temp('tmpfile.fits'), output=False) == info
Example #46
0
    def test_open_file_with_end_padding(self):
        """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/106

        Open files with end padding bytes.
        """

        hdul = fits.open(self.data('test0.fits'),
                         do_not_scale_image_data=True)
        info = hdul.info(output=False)
        hdul.writeto(self.temp('temp.fits'))
        with open(self.temp('temp.fits'), 'ab') as f:
            f.seek(0, os.SEEK_END)
            f.write(b'\0' * 2880)
        with ignore_warnings():
            assert info == fits.info(self.temp('temp.fits'), output=False,
                                     do_not_scale_image_data=True)
Example #47
0
    def test_insert_image_extension_to_primary_in_non_empty_list(self):
        """
        Tests inserting a Simple Image ExtensionHDU to a non-empty HDUList
        as the primary HDU.
        """

        hdul = fits.open(self.data('tb.fits'))
        hdu = fits.ImageHDU(np.arange(100, dtype=np.int32))
        hdul.insert(0, hdu)

        info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', ''),
                (1, '', 1, 'ImageHDU', 12, (), '', ''),
                (2, '', 1, 'BinTableHDU', 24, '2R x 4C', '[1J, 3A, 1E, 1L]', '')]

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #48
0
    def test_insert_groupshdu_to_non_empty_list(self):
        """Tests inserting a Simple GroupsHDU to an empty HDUList."""

        hdul = fits.HDUList()
        hdu = fits.PrimaryHDU(np.arange(100, dtype=np.int32))
        hdul.insert(0, hdu)
        hdu = fits.GroupsHDU()

        with pytest.raises(ValueError):
            hdul.insert(1, hdu)

        info = [(0, 'PRIMARY', 1, 'GroupsHDU', 8, (), '',
                 '1 Groups  0 Parameters'),
                (1, '', 1, 'ImageHDU', 6, (100,), 'int32', '')]

        hdul.insert(0, hdu)

        assert hdul.info(output=False) == info

        hdul.writeto(self.temp('test-insert.fits'))

        assert fits.info(self.temp('test-insert.fits'), output=False) == info
Example #49
0
    def __init__(self, datafile, ID, resolvingPower):
        self.datafile = datafile
        self.ID = ID
        self.resolvingPower = resolvingPower
        info = pyfits.info(self.datafile, output='')
        self.nSpectra = len(info)-1
        wavestart = []
        wavestop = []
        self.headers = []
        for i in range(self.nSpectra):
            self.headers.append(pyfits.getheader(self.datafile, ext=i+1))
            wavestart.append(self.headers[-1].get('WLSTART'))
            wavestop.append(self.headers[-1].get('WLSTOP'))

        self.wavestart = numpy.array(wavestart)
        self.wavestop = numpy.array(wavestop)
        header = pyfits.getheader(self.datafile)
        self.Teff = header.get("TEFF")
        self.logg = header.get("LOGG")
        self.B = header.get("BFIELD")
        self.vsini = header.get("VSINI")
        self.generate_label()
        self.suppressed = True
Example #50
0
import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)

##############################################################################
# Download the example FITS files used by this example:

from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits

image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')

##############################################################################
# Use `astropy.io.fits.info()` to display the structure of the file:

fits.info(image_file)

##############################################################################
# Generally the image information is located in the Primary HDU, also known
# as extension 0. Here, we use `astropy.io.fits.getdata()` to read the image
# data from this first extension using the keyword argument ``ext=0``:

image_data = fits.getdata(image_file, ext=0)

##############################################################################
# The data is now stored as a 2D numpy array. Print the dimensions using the
# shape attribute:

print(image_data.shape)

##############################################################################
Example #51
0
def _captured_fits_info(path):
    """Return fits info as a string."""
    fits.info(path)
    def testHDUListWritetoMethod(self):
        """Test the writeto method of HDUList in both the fits and stpyfits
           namespace."""

        hdu = stpyfits.PrimaryHDU()
        hdu1 = stpyfits.ImageHDU()
        hdu.data = np.zeros((10, 10), dtype=np.int32)
        hdu1.data = hdu.data + 2
        hdu.header.set('BITPIX', 32)
        hdu1.header.set('BITPIX', 32)
        hdu.header.set('NAXIS', 2)
        hdu.header.set('NAXIS1', 10, 'length of constant array axis 1',
                       after='NAXIS')
        hdu.header.set('NAXIS2', 10, 'length of constant array axis 2',
                       after='NAXIS1')
        hdu.header.set('PIXVALUE', 0, 'Constant pixel value')
        hdu1.header.set('PIXVALUE', 2, 'Constant pixel value', after='GCOUNT')
        hdu1.header.set('NAXIS', 2)
        hdu1.header.set('NAXIS1', 10, 'length of constant array axis 1',
                        after='NAXIS')
        hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2',
                        after='NAXIS1')
        hdul = stpyfits.HDUList([hdu,hdu1])
        hdul.writeto(self.temp('new.fits'), clobber=True)

        assert_equal(stpyfits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', ''),
             (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')])

        assert_equal(fits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 7, (), '', ''),
             (1, '', 'ImageHDU', 8, (), '', '')])

        hdul1 = stpyfits.open(self.temp('new.fits'))
        hdul2 = fits.open(self.temp('new.fits'))

        assert_equal(hdul1[0].header['NAXIS'], 2)
        assert_equal(hdul1[0].header['NAXIS1'], 10)
        assert_equal(hdul1[0].header['NAXIS2'], 10)
        assert_equal(hdul1[0].header['PIXVALUE'], 0)

        assert_raises(KeyError, lambda: hdul1[0].header['NPIX1'])
        assert_raises(KeyError, lambda: hdul1[0].header['NPIX2'])

        assert_true((hdul1[0].data ==
                     np.zeros((10, 10), dtype=np.int32)).all())

        assert_equal(hdul1[1].header['NAXIS'], 2)
        assert_equal(hdul1[1].header['NAXIS1'], 10)
        assert_equal(hdul1[1].header['NAXIS2'], 10)
        assert_equal(hdul1[1].header['PIXVALUE'], 2)

        assert_raises(KeyError, lambda: hdul1[1].header['NPIX1'])
        assert_raises(KeyError, lambda: hdul1[1].header['NPIX2'])

        assert_true((hdul1[1].data ==
                     (np.zeros((10, 10), dtype=np.int32) + 2)).all())

        assert_equal(hdul2[0].header['NAXIS'], 0)
        assert_equal(hdul2[0].header['NPIX1'], 10)
        assert_equal(hdul2[0].header['NPIX2'], 10)
        assert_equal(hdul2[0].header['PIXVALUE'], 0)

        assert_raises(KeyError, lambda: hdul2[0].header['NAXIS1'])
        assert_raises(KeyError, lambda: hdul2[0].header['NAXIS2'])

        assert_equal(hdul2[0].data, None)

        assert_equal(hdul2[1].header['NAXIS'], 0)
        assert_equal(hdul2[1].header['NPIX1'], 10)
        assert_equal(hdul2[1].header['NPIX2'], 10)
        assert_equal(hdul2[1].header['PIXVALUE'], 2)

        assert_raises(KeyError, lambda: hdul2[1].header['NAXIS1'])
        assert_raises(KeyError, lambda: hdul2[1].header['NAXIS2'])

        hdul1.close()
        hdul2.close()
Example #53
0
###########

import sys,os,string

import astropy
from astropy.io import fits as pyfits
import numpy as np
import scipy
from scipy import ndimage
 
imagen_in=sys.argv[1]     #input (BORDES.fits)
x=int(sys.argv[2])        # pos x on the center of the "valley" at y=1 of the order in BORDES.fits 
y=int(sys.argv[3])        # pos y usually  1
imagen_out=sys.argv[4]    #output image with a single flat order  

pyfits.info(imagen_in)
header=pyfits.getheader(imagen_in)
print header['naxis']

imagen=pyfits.getdata(imagen_in,0)
output=imagen*0

#i=y

med=x
r=med
for i in range (len(imagen)):
    #explorando a la izquierda
    #exploring to the left
    r=med
    while (imagen[i,r]==0):
Example #54
0
def change_polcconv(file_in, file_out, ctype, force=False):

    t0 = time.time()
    if not os.path.exists(file_in):
        sys.exit('Input file %s does not exist'%(file_in))

    if (file_in == file_out):
        #raise ValueError('Input and output files must be different')
        sys.exit('Input and output files must be different')
    
    uctype = (ctype.upper())[2:5]
    allowed = ('C2C','C2I','I2C','I2I')
    if (not uctype.startswith(allowed)):
        print ('ERROR: Ctype must be among: ')
        print (allowed)
        print ('While it is: %s'%(uctype))
        #raise ValueError('Aborting')
        sys.exit('Aborting')
    
    do_c2c =uctype.startswith('C2C')
    do_c2i =uctype.startswith('C2I')
    do_i2c =uctype.startswith('I2C')
    do_i2i =uctype.startswith('I2I')

    kw_pol = 'POLCCONV'
    kw_iau = 'IAU'
    kw_cos = 'COSMO'
    undef  = 'UNDEF'
    modify_map = (do_c2i or do_i2c)



    print ('%s -> [%s] -> %s'%(file_in, uctype, file_out))
    # file type
    info = pf.info(file_in, output=False)
    hdr  = pf.getheader(file_in, ext=1)
    ftype = fitstype(hdr)
    n_ext = len(info)-1

    # copy primary header
    hdr0      = pf.getheader(file_in, ext=0)
    polcconv1 = hdr0.get(kw_pol, default=undef).upper()
    fullout   = pf.HDUList([pf.PrimaryHDU(data=None, header=hdr0)])
    
    # loop on extensions
    for ext in range(1,n_ext+1):
        extname = info[ext][1]
        if (ftype == 3): # cut sky
            dcut, xhdr = pf.getdata(file_in, ext=ext, header=True)
            names = dcut.names
        else: # full sky
            tqu, xhdr  = pf.getdata(file_in, ext=ext, header=True)
            names = tqu.names

        nside = xhdr.get('NSIDE')
        in_xxx = True ; in_iau = False ; in_cos = False
        str_ext = 'extension #%s (%s)'%(str(ext),extname)
        nmaps = len(names)
        xQU = (nmaps >= 3 and     # first 3 column names are *,Q*,U*
               names[1].startswith(('Q','q')) and
               names[2].startswith(('U','u')) )
        QU  = (nmaps == 2 and     # first 2 column names are Q*,U*
               names[0].startswith(('Q','q')) and
               names[1].startswith(('U','u')) )

        # check POLCCONV in input file
        polcconv = xhdr.get(kw_pol, default=undef).upper()
        if (polcconv1 != undef and polcconv != undef and polcconv1 != polcconv):
            raise ValueError('Unconsistent %s values found in Primary and extension headers'%(kw_pol))
        #in_xxx = (polcconv == undef)
        in_iau = (polcconv == kw_iau)
        in_cos = (polcconv == kw_cos)
        in_xxx = not (in_iau or in_cos)
        done    = False
        non_pol = False
        do_edit = False

        if not force:
            if ((in_cos and do_i2i) or (in_iau and do_c2c)):
                print ('FITS file: %s'%(file_in))
                print ('%s: %s'%(kw_pol, polcconv))
                print ('Can not perform requested %s edition'%(uctype))
                print ('Use the force=True keyword to only update %s value.'%(kw_pol))
                sys.exit(1)

            done = (in_cos and (do_i2c or do_c2c) or in_iau and (do_c2i or do_i2i))

        if (modify_map and not done):
        # change sign of U and related quantities
            clist = [] ; do_edit = False
            if (ftype == 3):
                if (ext == 3): # cut sky format: change sign of U (last extension)
                    clist = [1] ; do_edit = True
            else:
                if (nmaps <= 1):
                    non_pol = True

                if (nmaps == 2):
                    if (n_ext == 1 and QU):
                        # Planck 2 columns (Q*, U*)
                        clist = [1] ; do_edit = True
                    else:
                        non_pol = True

                if (nmaps == 3):
                    # standard Healpix full sky format (T, Q, U ; TT, QQ, UU ; QU, TU, TQ)
                    if (ext == 1):
                        if (xQU):
                            clist = [2] ; do_edit = True # change sign of U
                        else:
                            non_pol = True
                    if (ext == 3): # change sign of QU and of TU
                        clist = [0,1] ; do_edit = True

                if (n_ext >= 1 and nmaps == 5):
                    # Planck 5 cols (I_STOKES, Q_STOKES, U_STOKES, TMASK, PMASK) + BEAM_TF extension
                    if (ext == 1 and xQU):
                        clist = [2] ; do_edit = True # change sign of U

                if (n_ext == 1 and nmaps == 10):
                    # Planck R3 (2017) 10 cols (I_STOKES, Q_STOKES, U_STOKES, HIT, II_COV, IQ_COV, IU_COV, QQ_COV, QU_COV, UU_COV)
                    if (ext == 1 and xQU):
                        clist = [2, 6, 8] ; do_edit = True # change sign of U, IU, QU

                if (nside <= 512 and n_ext == 2):
                    # WMAP: https://lambda.gsfc.nasa.gov/product/map/dr5/skymap_file_format_info.cfm
                    if (nmaps >= 5):
                        # WMAP (I,Q,U,S) format (T, Q, U, S, N_OBS ; 
                        #      N_OBS, M11=SS, M12=SQ, M13=SU, M23=QU, M22=QQ, M33=UU)
                        if (ext == 1 and nmaps == 5 and xQU):
                            clist = [2] ; do_edit = True # change sign of U
                        if (ext == 2 and nmaps == 7):
                            clist = [3,4] ; do_edit = True # change sign of SU and QU
                            
                    if (nmaps == 4): # WMAP (I,Q,U) format (T, Q, U, N_OBS ; N_OBS, QQ, QU, UU)
                        clist = [2] ; do_edit = True # change sign of U and of N_QU
                        #                 else:
                        #                     raise ValueError('Unrecognised format in %s'%file_in)
                if (not do_edit):
                    if (non_pol):
                        print (' Unpolarised format, no change in %s'%(str_ext))
                    else:
                        print ('WARNING: unrecognised format in %s'%(str_ext))
                        print (nmaps, n_ext)
                        print (names)
                        done = True
                        
            if (do_edit):
                xhdr.add_history(file_in)
                xhdr.add_history('edited on %s UTC'%(datetime.isoformat(datetime.now(UTC))[0:19]))
                for c in clist:
                    if (ftype == 3):
                        dcut[names[c]] *= -1
                    else:
                        tqu[names[c]] *= -1
                    str_col = 'column #%s (%s)'%(str(c+1),names[c].strip())
                    print (' Flip sign of %s in %s'%(str_col,str_ext))
                    xhdr.add_history('modified %s to match coord. conv. (%s)'%(str_col,kw_pol))
                
        if ( (do_edit or in_xxx or force) and not non_pol):
            new_cconv = kw_cos if (do_c2c or do_i2c) else kw_iau
            xhdr[kw_pol]= (new_cconv, ' Coord. convention for polarisation (COSMO/IAU)')
            print (' %s keyword %s=%s in header of %s'%((in_xxx and 'Add' or 'Update'),kw_pol,new_cconv,str_ext))
        else:
            print (' No change in header of %s'%(str_ext))

        # append new data
        if (ftype == 3):
            out = pf.BinTableHDU.from_columns(dcut)
        else:
            out = pf.BinTableHDU.from_columns(tqu)
        out.header = xhdr
        fullout.append(out)

    # write new file
    fullout.writeto(file_out, overwrite=True)
    # list input and output file
    os.system('ls -l %s %s'%(file_in, file_out))
    t1 = time.time()
    print ("Time [s] = ",t1-t0)
    print ('     ----------------        ')
    def testHDUListFlushMethod(self):
        """Test the flush method of HDUList in both the fits and stpyfits
           namespace."""

        hdu = stpyfits.PrimaryHDU()
        hdu1 = stpyfits.ImageHDU()
        hdu.data = np.zeros((10, 10), dtype=np.int32)
        hdu1.data = hdu.data + 2
        hdu.header.set('BITPIX', 32)
        hdu1.header.set('BITPIX', 32)
        hdu.header.set('NAXIS', 2)
        hdu.header.set('NAXIS1', 10, 'length of constant array axis 1',
                       after='NAXIS')
        hdu.header.set('NAXIS2', 10, 'length of constant array axis 2',
                       after='NAXIS1')
        hdu.header.set('PIXVALUE', 0, 'Constant pixel value')
        hdu1.header.set('PIXVALUE', 2, 'Constant pixel value', after='GCOUNT')
        hdu1.header.set('NAXIS', 2)
        hdu1.header.set('NAXIS1', 10, 'length of constant array axis 1',
                        after='NAXIS')
        hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2',
                        after='NAXIS1')
        hdul = stpyfits.HDUList([hdu, hdu1])
        hdul.writeto(self.temp('new.fits'), clobber=True)

        hdul = stpyfits.open(self.temp('new.fits'), 'update')
        d = np.arange(10, dtype=np.int32)
        d = d * 0
        d = d + 3
        hdul[0].data = d
        hdul.flush()
        hdul.close()

        assert_equal(stpyfits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (10,), 'int32', ''),
             (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')])
        assert_equal(fits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', ''),
             (1, '', 'ImageHDU', 8, (), '', '')])

        hdul1 = stpyfits.open(self.temp('new.fits'))
        hdul2 = fits.open(self.temp('new.fits'))

        assert_equal(hdul1[0].header['NAXIS'], 1)
        assert_equal(hdul1[0].header['NAXIS1'], 10)
        assert_equal(hdul1[0].header['PIXVALUE'], 3)

        assert_raises(KeyError, lambda: hdul1[0].header['NPIX1'])

        assert_true((hdul1[0].data ==
                     (np.zeros(10, dtype=np.int32) + 3)).all())

        assert_equal(hdul2[0].header['NAXIS'], 0)
        assert_equal(hdul2[0].header['NPIX1'], 10)
        assert_equal(hdul2[0].header['PIXVALUE'], 3)

        assert_raises(KeyError, lambda: hdul2[0].header['NAXIS1'])

        assert_equal(hdul2[0].data, None)

        hdul1.close()
        hdul2.close()

        hdul3 = stpyfits.open(self.temp('new.fits'), 'update')
        d = np.arange(15, dtype=np.int32)
        d = d * 0
        d = d + 4
        hdul3[0].data = d
        hdul3.close()      # Note that close calls flush

        assert_equal(stpyfits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (15,), 'int32', ''),
             (1, '', 'ImageHDU', 8, (10, 10), 'int32', '')])
        assert_equal(fits.info(self.temp('new.fits'), output=False),
            [(0, 'PRIMARY', 'PrimaryHDU', 6, (), '', ''),
             (1, '', 'ImageHDU', 8, (), '', '')])

        hdul1 = stpyfits.open(self.temp('new.fits'))
        hdul2 = fits.open(self.temp('new.fits'))

        assert_equal(hdul1[0].header['NAXIS'], 1)
        assert_equal(hdul1[0].header['NAXIS1'], 15)
        assert_equal(hdul1[0].header['PIXVALUE'], 4)

        assert_raises(KeyError, lambda: hdul1[0].header['NPIX1'])

        assert_true((hdul1[0].data ==
                     (np.zeros(15, dtype=np.int32) + 4)).all())

        assert_equal(hdul2[0].header['NAXIS'], 0)
        assert_equal(hdul2[0].header['NPIX1'], 15)
        assert_equal(hdul2[0].header['PIXVALUE'], 4)

        assert_raises(KeyError, lambda: hdul2[0].header['NAXIS1'])

        assert_equal(hdul2[0].data, None)

        hdul1.close()
        hdul2.close()
Example #56
0
 def test_file_like_3(self):
     tmpfile = open(self.temp('tmpfile.fits'), 'wb')
     fits.writeto(tmpfile, np.arange(100, dtype=np.int32))
     tmpfile.close()
     info = [(0, 'PRIMARY', 1, 'PrimaryHDU', 5, (100,), 'int32', '')]
     assert fits.info(self.temp('tmpfile.fits'), output=False) == info
from astropy.io import fits

msName = 'HDFC0155MFSC_CAL_vis.ms'
x=fits.getdata(msName[:-3]+'_'+str(0)+'.fits',0)
header_primary = fits.getheader(msName[:-3]+'_'+str(0)+'.fits')
fits.writeto('HDFC0155_cube.fits', x, header_primary)

for i in range(1,8):
    x=fits.getdata(msName[:-3]+'_'+str(i)+'.fits',0)
    header_primary = fits.getheader(msName[:-3]+'_'+str(i)+'.fits')
    fits.append('HDFC0155_cube.fits', x, header_primary)

print fits.info("HDFC0155_cube.fits")
Example #58
0
# example. Also import `~astropy.table.Table` from the `astropy.table` subpackage
# and `astropy.io.fits`

from astropy.utils.data import get_pkg_data_filename
from astropy.table import Table
from astropy.io import fits

##############################################################################
# Download a FITS file

event_filename = get_pkg_data_filename('tutorials/FITS-tables/chandra_events.fits')

##############################################################################
# Display information about the contents of the FITS file.

fits.info(event_filename)

##############################################################################
# Extension 1, EVENTS, is a Table that contains information about each X-ray
# photon that hit Chandra's HETG-S detector.
#
# Use `~astropy.table.Table` to read the table

events = Table.read(event_filename, hdu=1)

##############################################################################
# Print the column names of the Events Table.

print(events.columns)

##############################################################################
Example #59
0
import argparse
from astropy.io import fits

# PARSE OPTIONS ###############################################################

parser = argparse.ArgumentParser(description="An astropy snippet")
parser.add_argument("filearg", nargs=1, metavar="FILE", help="the FITS file to process")
args = parser.parse_args()
file_path = args.filearg[0]

# PRINT GENERAL HDU INFOS #####################################################

print()
print(80 * '*')
print()
fits.info(file_path)
print()
print(80 * '*')
print()

# READ DATA ###################################################################

# Open the FITS file
hdu_list = fits.open(file_path)

# For each blocks
for hdu_index, hdu in enumerate(hdu_list):

    print("HDU {}".format(hdu_index), 40 * '*')
    print()