Ejemplo n.º 1
0
    def __init__(self,hdr):
        # ok, there is this annoying situation where the SIP coefs will
        # be present, but the CTYPE does not indicate that.  Therefore,
        # astropy will issue a big warning, so I'll take control of that
        # here, but checking for SIP coefs and the CTYPE
        #has_sip=self.has_sip(hdr)
        #if has_sip:
        #    for ctype in ('CTYPE1','CTYPE2'):
        #        if hdr[ctype][8:] != '-SIP':
        #            hdr[ctype] += '-SIP'



        # set the astropy WCS class
        astropyWCS.__init__(self,header=hdr)


        self.shape=(hdr['NAXIS2'],hdr['NAXIS1'])
        self.npixel=self.shape[0]*self.shape[1]

        
        # check some things
        if np.isnan(self.wcs.equinox):
                self.wcs.equinox=2000.
                        
        # for reasons, they do not include LTV* in WCS
        self.ltv=np.array([0,0])
        if 'LTV1' in hdr:
            self.ltv[0]=hdr['LTV1']
        if 'LTV2' in hdr:
            self.ltv[1]=hdr['LTV2']
Ejemplo n.º 2
0
    def loadData(self,image,header):
        ''' load the data into the object '''
        self.image=image
        self.header=header


        
        if self.has_sip():
            if self.header['CTYPE1'][8:]!=self.header['CTYPE2'][8:]:
                raise NotImplementedError("Invalid fits header in FITSImage")
            if self.header['CTYPE1'][8:]!='-SIP':
                # if here, then there are SIP coefs but the CTYPE*
                # are not labeled as -SIP, and so astropy will complain.
                # Personally, I think this is a *MAJOR* error on astropy's
                # part, it is completely natural to log the SIP coefs in
                # a rectified image so one does not lose this information
                # for posterity.  I will work around this, by relabeling
                # the coefs so astropy's error is not raised.
                coef=re.compile('(A|B|AP|BP)_[0-9]_[0-9]')
                order=re.compile('(A|B|AP|BP)_ORDER')
                deletes=[]
                letters={'A':'C','B':'D','AP':'CP','BP':'DP'}
                for k,v in self.header.items():
                    match= coef.match(k)

                    if match:
                        let,i,j=match[0].split('_')
                        if let in letters.keys():
                            tokens=(letters[let],i,j)
                            self.header['{}_{}_{}'.format(*tokens)]=v
                            deletes.append(k)


                    match=order.match(k)
                    if match:
                        let=match[0].split('_')[0]
                        if let in letters.keys():
                            self.header['{}_ORDER'.format(letters[let])]=v
                            deletes.append(k)
                for delete in deletes:
                    del self.header[delete]
                              



        
        WCS.__init__(self,self.header)
Ejemplo n.º 3
0
    def __init__(self, *args, **kwargs):

        if 'slices' in kwargs:
            self._slices = kwargs.pop('slices')

        if 'dimensions' in kwargs:
            self._dimensions = kwargs.pop('dimensions')

        AstropyWCS.__init__(self, *args, **kwargs)

        # Fix common non-standard units
        self.wcs.unitfix()

        # Now find the values of the coordinates in the slices - only needed if
        # data has more than two dimensions
        if len(self._slices) > 0:

            self.nx = args[0]['NAXIS%i' % (self._dimensions[0] + 1)]
            self.ny = args[0]['NAXIS%i' % (self._dimensions[1] + 1)]
            xpix = np.arange(self.nx) + 1.
            ypix = np.arange(self.ny) + 1.
            xpix, ypix = np.meshgrid(xpix, ypix)
            xpix, ypix = xpix.reshape(self.nx * self.ny), ypix.reshape(self.nx * self.ny)
            s = 0
            coords = []
            for dim in range(self.naxis):
                if dim == self._dimensions[0]:
                    coords.append(xpix)
                elif dim == self._dimensions[1]:
                    coords.append(ypix)
                else:
                    coords.append(np.repeat(self._slices[s], xpix.shape))
                    s += 1
            coords = np.vstack(coords).transpose()
            result = AstropyWCS.wcs_pix2world(self, coords, 1)
            self._mean_world = np.mean(result, axis=0)
Ejemplo n.º 4
0
    def __init__(self, fobj=None, ext=None, minerr=0.0, wcskey=" "):
        """
        Create a WCS object based on the instrument.

        In addition to basic WCS keywords this class provides
        instrument specific information needed in distortion computation.

        Parameters
        ----------
        fobj : str or `astropy.io.fits.HDUList` object or None
            file name, e.g j9irw4b1q_flt.fits
            fully qualified filename[EXTNAME,EXTNUM], e.g. j9irw4b1q_flt.fits[sci,1]
            `astropy.io.fits` file object, e.g fits.open('j9irw4b1q_flt.fits'), in which case the
            user is responsible for closing the file object.
        ext : int, tuple or None
            extension number
            if ext is tuple, it must be ("EXTNAME", EXTNUM), e.g. ("SCI", 2)
            if ext is None, it is assumed the data is in the primary hdu
        minerr : float
            minimum value a distortion correction must have in order to be applied.
            If CPERRja, CQERRja are smaller than minerr, the corersponding
            distortion is not applied.
        wcskey : str
            A one character A-Z or " " used to retrieve and define an
            alternate WCS description.
        """

        self.inst_kw = ins_spec_kw
        self.minerr = minerr
        self.wcskey = wcskey

        if fobj is not None:
            filename, hdr0, ehdr, phdu = getinput.parseSingleInput(f=fobj,
                                                                   ext=ext)
            self.filename = filename
            instrument_name = hdr0.get('INSTRUME', 'DEFAULT')
            if instrument_name == 'DEFAULT' or instrument_name not in list(
                    inst_mappings.keys()):
                self.instrument = 'DEFAULT'
            else:
                self.instrument = instrument_name
            # Set the correct reference frame
            refframe = determine_refframe(hdr0)
            if refframe is not None:
                ehdr['RADESYS'] = refframe

            WCS.__init__(self,
                         ehdr,
                         fobj=phdu,
                         minerr=self.minerr,
                         key=self.wcskey)
            if self.instrument == 'DEFAULT':
                self.pc2cd()
            # If input was a `astropy.io.fits.HDUList` object, it's the user's
            # responsibility to close it, otherwise, it's closed here.
            if not isinstance(fobj, fits.HDUList):
                phdu.close()
            self.setInstrSpecKw(hdr0, ehdr)
            self.readIDCCoeffs(ehdr)
            extname = ehdr.get('EXTNAME', '')
            extnum = ehdr.get('EXTVER', None)
            self.extname = (extname, extnum)
        else:
            # create a default HSTWCS object
            self.instrument = 'DEFAULT'
            WCS.__init__(self, minerr=self.minerr, key=self.wcskey)
            self.pc2cd()
            self.setInstrSpecKw()
        self.setPscale()
        self.setOrient()
Ejemplo n.º 5
0
    def __init__(self, *args, **kwargs):

        if 'slices' in kwargs:
            self._slices = kwargs.pop('slices')
        else:
            self._slices = []

        if 'dimensions' in kwargs:
            self._dimensions = kwargs.pop('dimensions')
        else:
            self._dimensions = [0, 1]

        AstropyWCS.__init__(self, *args, **kwargs)

        # Fix common non-standard units
        self.wcs.unitfix()

        # Now find the values of the coordinates in the slices - only needed if
        # data has more than two dimensions
        if len(self._slices) > 0:

            self.nx = args[0]['NAXIS%i' % (self._dimensions[0] + 1)]
            self.ny = args[0]['NAXIS%i' % (self._dimensions[1] + 1)]
            xpix = np.arange(self.nx) + 1.
            ypix = np.arange(self.ny) + 1.
            xpix, ypix = np.meshgrid(xpix, ypix)
            xpix, ypix = xpix.reshape(self.nx * self.ny), ypix.reshape(
                self.nx * self.ny)
            s = 0
            coords = []
            for dim in range(self.naxis):
                if dim == self._dimensions[0]:
                    coords.append(xpix)
                elif dim == self._dimensions[1]:
                    coords.append(ypix)
                else:
                    coords.append(np.repeat(self._slices[s], xpix.shape))
                    s += 1
            coords = np.vstack(coords).transpose()
            result = AstropyWCS.wcs_pix2world(self, coords, 1)
            self._mean_world = np.mean(result, axis=0)
            # result = result.transpose()
            # result = result.reshape((result.shape[0],) + (self.ny, self.nx))

        # Now guess what 'type' of values are on each axis
        if self.ctype_x[:4] == 'RA--' or \
           self.ctype_x[1:4] == 'LON':
            self.set_xaxis_coord_type('longitude')
            self.set_xaxis_coord_type('longitude')
        elif self.ctype_x[:4] == 'DEC-' or \
           self.ctype_x[1:4] == 'LAT':
            self.set_xaxis_coord_type('latitude')
            self.set_xaxis_coord_type('latitude')
        else:
            self.set_xaxis_coord_type('scalar')
            self.set_xaxis_coord_type('scalar')

        if self.ctype_y[:4] == 'RA--' or \
           self.ctype_y[1:4] == 'LON':
            self.set_yaxis_coord_type('longitude')
            self.set_yaxis_coord_type('longitude')
        elif self.ctype_y[:4] == 'DEC-' or \
           self.ctype_y[1:4] == 'LAT':
            self.set_yaxis_coord_type('latitude')
            self.set_yaxis_coord_type('latitude')
        else:
            self.set_yaxis_coord_type('scalar')
            self.set_yaxis_coord_type('scalar')
Ejemplo n.º 6
0
    def __init__(self, fobj=None, ext=None, minerr=0.0, wcskey=" "):
        """
        Create a WCS object based on the instrument.

        In addition to basic WCS keywords this class provides
        instrument specific information needed in distortion computation.

        Parameters
        ----------
        fobj : str or `astropy.io.fits.HDUList` object or None
            file name, e.g j9irw4b1q_flt.fits
            fully qualified filename[EXTNAME,EXTNUM], e.g. j9irw4b1q_flt.fits[sci,1]
            `astropy.io.fits` file object, e.g fits.open('j9irw4b1q_flt.fits'), in which case the
            user is responsible for closing the file object.
        ext : int, tuple or None
            extension number
            if ext is tuple, it must be ("EXTNAME", EXTNUM), e.g. ("SCI", 2)
            if ext is None, it is assumed the data is in the primary hdu
        minerr : float
            minimum value a distortion correction must have in order to be applied.
            If CPERRja, CQERRja are smaller than minerr, the corersponding
            distortion is not applied.
        wcskey : str
            A one character A-Z or " " used to retrieve and define an
            alternate WCS description.
        """

        self.inst_kw = ins_spec_kw
        self.minerr = minerr
        self.wcskey = wcskey

        if fobj is not None:
            filename, hdr0, ehdr, phdu = getinput.parseSingleInput(f=fobj,
                                                                   ext=ext)
            self.filename = filename
            instrument_name = hdr0.get('INSTRUME', 'DEFAULT')
            if instrument_name == 'DEFAULT' or instrument_name not in list(inst_mappings.keys()):
                #['IRAF/ARTDATA','',' ','N/A']:
                self.instrument = 'DEFAULT'
            else:
                self.instrument = instrument_name
            # Set the correct reference frame
            refframe = determine_refframe(hdr0)
            ehdr['RADESYS'] = refframe

            WCS.__init__(self, ehdr, fobj=phdu, minerr=self.minerr,
                         key=self.wcskey)
            if self.instrument == 'DEFAULT':
                self.pc2cd()
            # If input was a `astropy.io.fits.HDUList` object, it's the user's
            # responsibility to close it, otherwise, it's closed here.
            if not isinstance(fobj, fits.HDUList):
                phdu.close()
            self.setInstrSpecKw(hdr0, ehdr)
            self.readIDCCoeffs(ehdr)
            extname = ehdr.get('EXTNAME', '')
            extnum = ehdr.get('EXTVER', None)
            self.extname = (extname, extnum)
        else:
            # create a default HSTWCS object
            self.instrument = 'DEFAULT'
            WCS.__init__(self, minerr=self.minerr, key=self.wcskey)
            self.pc2cd()
            self.setInstrSpecKw()
        self.setPscale()
        self.setOrient()
Ejemplo n.º 7
0
    def __init__(self, *args, **kwargs):

        if 'slices' in kwargs:
            self._slices = kwargs.pop('slices')

        if 'dimensions' in kwargs:
            self._dimensions = kwargs.pop('dimensions')

        AstropyWCS.__init__(self, *args, **kwargs)

        # Fix common non-standard units
        self.wcs.unitfix()

        # Now find the values of the coordinates in the slices - only needed if
        # data has more than two dimensions
        if len(self._slices) > 0:

            self.nx = args[0]['NAXIS%i' % (self._dimensions[0] + 1)]
            self.ny = args[0]['NAXIS%i' % (self._dimensions[1] + 1)]
            xpix = np.arange(self.nx) + 1.
            ypix = np.arange(self.ny) + 1.
            xpix, ypix = np.meshgrid(xpix, ypix)
            xpix, ypix = xpix.reshape(self.nx * self.ny), ypix.reshape(self.nx * self.ny)
            s = 0
            coords = []
            for dim in range(self.naxis):
                if dim == self._dimensions[0]:
                    coords.append(xpix)
                elif dim == self._dimensions[1]:
                    coords.append(ypix)
                else:
                    coords.append(np.repeat(self._slices[s], xpix.shape))
                    s += 1
            coords = np.vstack(coords).transpose()
            result = AstropyWCS.wcs_pix2sky(self, coords, 1)
            self._mean_world = np.mean(result, axis=0)
            # result = result.transpose()
            # result = result.reshape((result.shape[0],) + (self.ny, self.nx))

        # Now guess what 'type' of values are on each axis
        if self.ctype_x[:4] == 'RA--' or \
           self.ctype_x[1:4] == 'LON':
            self.set_xaxis_coord_type('longitude')
            self.set_xaxis_coord_type('longitude')
        elif self.ctype_x[:4] == 'DEC-' or \
           self.ctype_x[1:4] == 'LAT':
            self.set_xaxis_coord_type('latitude')
            self.set_xaxis_coord_type('latitude')
        else:
            self.set_xaxis_coord_type('scalar')
            self.set_xaxis_coord_type('scalar')

        if self.ctype_y[:4] == 'RA--' or \
           self.ctype_y[1:4] == 'LON':
            self.set_yaxis_coord_type('longitude')
            self.set_yaxis_coord_type('longitude')
        elif self.ctype_y[:4] == 'DEC-' or \
           self.ctype_y[1:4] == 'LAT':
            self.set_yaxis_coord_type('latitude')
            self.set_yaxis_coord_type('latitude')
        else:
            self.set_yaxis_coord_type('scalar')
            self.set_yaxis_coord_type('scalar')