Esempio n. 1
0
    def __init__(self, input_, component=None, filetype='sdf', getshape=False):

        # TODO: this should be using python NDF!
        # If input is a string, assume it is either an NDF file or a
        # FITS file, and read in the object
        frameset = None
        shape = None
        # TODO: (Should useNDF interface once that is easy to install)
        if isinstance(input_, str) and filetype == 'sdf':
            try:
                hdsloc = hds.open(input_, 'READ')
                if component:
                    comps = component.split('.')
                    for c in comps:
                        hdsloc = hdsloc.find(c)

                astwcs = hdsloc.find('WCS').find('DATA').get()
                astwcs = [i.decode() for i in astwcs]
                astwcs = ''.join([
                    i[1:] if i.startswith('+') else '\n' + i for i in astwcs
                ]).split('\n')
                chan = Ast.Channel(astwcs)
                frameset = chan.read()
            except:
                logger.error('Could not get a frameset from %s', input_)
                hdsloc.annul()
                raise

            # Get the shape of the data.
            if getshape:
                try:
                    data = hdsloc.find('DATA_ARRAY').find('DATA').get()
                    shape = data.shape
                except:
                    logger.debug('No data component found in input file.')
                    shape = None

            hdsloc.annul()

        # If its a fits file, try and read a
        elif isinstance(input_, str) and filetype == 'fits':
            from astropy.io import fits
            from starlink import Atl
            hdulist = fits.open(input_)
            if not component:
                component = 0
            (frameset, encoding) = Atl.readfitswcs(hdulist[component])
            shape = hdulist[component].shape
            print('shape is', shape)
            hdulist.close()

        # Create low level object from frameset and shape
        print(type(frameset), type(shape))
        self._low_level_wcs = AstWCSLowLevel(frameset, arrayshape=shape)
        self._info_string = None
Esempio n. 2
0
def get_data_wcs(hdsfile):
    hdsloc = hds.open(hdsfile, 'READ')
    astloc = hdsloc.find('WCS').find('DATA')
    astwcs = [i.decode() for i in astloc.get()]
    astwcs = ''.join(
        [i[1:] if i.startswith('+') else '\n' + i for i in astwcs]).split('\n')
    chan = Ast.Channel(astwcs)
    frameset = chan.read()
    dataloc = hdsloc.find('DATA_ARRAY').find('DATA')
    badvalue = hds.getbadvalue(dataloc.type)
    data = dataloc.get()
    data = np.ma.masked_array(data, data == badvalue)
    hdsloc.annul()
    return data, frameset