Exemplo n.º 1
0
def fits_subset_mask_importer(filename):

    if not is_fits(filename):
        raise IOError("File {0} is not a valid FITS file".format(filename))

    masks = OrderedDict()

    label = os.path.basename(filename).rpartition('.')[0]

    with fits.open(filename) as hdulist:

        for ihdu, hdu in enumerate(hdulist):
            if hdu.data is not None and hdu.data.dtype.kind == 'i':
                if not hdu.name:
                    name = '{0}[{1}]'.format(label, ihdu)
                elif ihdu == 0:
                    name = label
                else:
                    name = hdu.name
                masks[name] = hdu.data > 0

    if len(masks) == 0:
        raise ValueError('No HDUs with integer values (which would normally indicate a mask) were found in file')

    return masks
Exemplo n.º 2
0
def fits_subset_mask_importer(filename):

    if not is_fits(filename):
        raise IOError("File {0} is not a valid FITS file".format(filename))

    masks = OrderedDict()

    label = os.path.basename(filename).rpartition('.')[0]

    with fits.open(filename) as hdulist:

        for ihdu, hdu in enumerate(hdulist):
            if hdu.data is not None and hdu.data.dtype.kind == 'i':
                if not hdu.name:
                    name = '{0}[{1}]'.format(label, ihdu)
                elif ihdu == 0:
                    name = label
                else:
                    name = hdu.name
                masks[name] = hdu.data > 0

    if len(masks) == 0:
        raise ValueError('No HDUs with integer values (which would normally indicate a mask) were found in file')

    return masks
Exemplo n.º 3
0
def is_dendro(file, **kwargs):

    if is_hdf5(file):

        import h5py

        f = h5py.File(file, 'r')

        return 'data' in f and 'index_map' in f and 'newick' in f

    elif is_fits(file):

        from astropy.io import fits

        with fits.open(file, ignore_missing_end=True) as hdulist:

            # For recent versions of astrodendro the HDUs have a recongnizable
            # set of names.

            if 'DATA' in hdulist and 'INDEX_MAP' in hdulist and 'NEWICK' in hdulist:
                return True

            # For older versions of astrodendro, the HDUs did not have names

            # Here we use heuristics to figure out if this is likely to be a
            # dendrogram. Specifically, there should be three HDU extensions.
            # The primary HDU should be empty, HDU 1 and HDU 2 should have
            # matching shapes, and HDU 3 should have a 1D array. Also, if the
            # HDUs do have names then this is not a dendrogram since the old
            # files did not have names

            # This branch can be removed once we think most dendrogram files
            # will have HDU names.

            if len(hdulist) != 4:
                return False

            if hdulist[1].name != '' or hdulist[2].name != '' or hdulist[
                    3].name != '':
                return False

            if hdulist[0].data is not None:
                return False

            if hdulist[1].data is None or hdulist[2].data is None or hdulist[
                    3].data is None:
                return False

            if hdulist[1].data.shape != hdulist[2].data.shape:
                return False

            if hdulist[3].data.ndim != 1:
                return False

        # We're probably ok, so return True
        return True

    else:

        return False
Exemplo n.º 4
0
def is_gridded_data(filename, **kwargs):

    if is_hdf5(filename):
        return True

    if is_fits(filename):
        from astropy.io import fits
        with fits.open(filename) as hdulist:
            return is_image_hdu(hdulist[0])

    return False
Exemplo n.º 5
0
def is_gridded_data(filename, **kwargs):

    if is_hdf5(filename):
        return True

    if is_fits(filename):
        from astropy.io import fits
        with fits.open(filename) as hdulist:
            return is_image_hdu(hdulist[0])

    return False
Exemplo n.º 6
0
def identify_file_format(filename):
    if os.path.isdir(filename):
        if os.path.exists(os.path.join(filename, 'table.f0')):
            return 'casa_image'
        else:
            return None
    else:
        if is_fits(filename):
            return 'fits'
        else:
            return None
Exemplo n.º 7
0
def is_spectral_cube(filename, **kwargs):
    """
    Check that the file is a 3D or 4D FITS spectral cube
    """

    if not is_fits(filename):
        return False

    try:
        StokesSpectralCube.read(filename)
    except Exception:
        return False
    else:
        return True
Exemplo n.º 8
0
def gridded_data(filename, format='auto', **kwargs):

    result = Data()

    # Try and automatically find the format if not specified
    if format == 'auto':
        format = file_format(filename)

    # Read in the data
    if is_fits(filename):
        from astropy.io import fits
        arrays = extract_data_fits(filename, **kwargs)
        header = fits.getheader(filename)
        result.coords = coordinates_from_header(header)
    elif is_hdf5(filename):
        arrays = extract_data_hdf5(filename, **kwargs)
    else:
        raise Exception("Unkonwn format: %s" % format)

    for component_name in arrays:
        comp = Component.autotyped(arrays[component_name])
        result.add_component(comp, component_name)

    return result
Exemplo n.º 9
0
def gridded_data(filename, format='auto', **kwargs):

    result = Data()

    # Try and automatically find the format if not specified
    if format == 'auto':
        format = file_format(filename)

    # Read in the data
    if is_fits(filename):
        from astropy.io import fits
        arrays = extract_data_fits(filename, **kwargs)
        header = fits.getheader(filename)
        result.coords = coordinates_from_header(header)
    elif is_hdf5(filename):
        arrays = extract_data_hdf5(filename, **kwargs)
    else:
        raise Exception("Unkonwn format: %s" % format)

    for component_name in arrays:
        comp = Component.autotyped(arrays[component_name])
        result.add_component(comp, component_name)

    return result
Exemplo n.º 10
0
def is_dendro(file, **kwargs):

    if is_hdf5(file):

        import h5py

        f = h5py.File(file, 'r')

        return 'data' in f and 'index_map' in f and 'newick' in f

    elif is_fits(file):

        from astropy.io import fits

        hdulist = fits.open(file, ignore_missing_end=True)

        # In recent versions of Astropy, we could do 'DATA' in hdulist etc. but
        # this doesn't work with Astropy 0.3, so we use the following method
        # instead:
        try:
            hdulist['DATA']
            hdulist['INDEX_MAP']
            hdulist['NEWICK']
        except KeyError:
            pass  # continue
        else:
            return True

        # For older versions of astrodendro, the HDUs did not have names

        # Here we use heuristics to figure out if this is likely to be a
        # dendrogram. Specifically, there should be three HDU extensions.
        # The primary HDU should be empty, HDU 1 and HDU 2 should have
        # matching shapes, and HDU 3 should have a 1D array. Also, if the
        # HDUs do have names then this is not a dendrogram since the old
        # files did not have names

        # This branch can be removed once we think most dendrogram files
        # will have HDU names.

        if len(hdulist) != 4:
            return False

        if hdulist[1].name != '' or hdulist[2].name != '' or hdulist[3].name != '':
            return False

        if hdulist[0].data is not None:
            return False

        if hdulist[1].data is None or hdulist[2].data is None or hdulist[3].data is None:
            return False

        if hdulist[1].data.shape != hdulist[2].data.shape:
            return False

        if hdulist[3].data.ndim != 1:
            return False

        # We're probably ok, so return True
        return True

    else:

        return False