예제 #1
0
def makePfsConfig(tract, patch, fiberIds, ras, decs, catIds, objIds,
                  objectMags):
    """
        Make and return a PfsConfig with real information
    """
    nFiber = len(fiberIds)

    tmp, tracts = tract, np.empty(nFiber, dtype=np.int32)
    tracts.fill(tmp)

    tmp, patches = patch, np.empty(nFiber, dtype=str)
    patches.fill(tmp)
    patches = nFiber * [tmp]

    fiberMag = np.empty((nFiber, 5))
    for i in range(nFiber):
        fiberMag[i] = objectMags[i]
    mpsCen = np.zeros((nFiber, 2))

    return PfsConfig(None,
                     tracts,
                     patches,
                     fiberIds,
                     ras,
                     decs,
                     catId=catIds,
                     objId=objIds,
                     mpsCen=mpsCen,
                     fiberMag=fiberMag)
예제 #2
0
    def testBasic(self):
        """Test basic operation of PfsConfig"""
        config = PfsConfig(self.pfsDesignId, self.visit0,
                           self.raBoresight.asDegrees(),
                           self.decBoresight.asDegrees(), self.fiberId,
                           self.tract, self.patch, self.ra, self.dec,
                           self.catId, self.objId, self.targetType,
                           self.fiberMag, self.filterNames, self.pfiCenter,
                           self.pfiNominal)

        dirName = os.path.splitext(__file__)[0]
        if not os.path.exists(dirName):
            os.makedirs(dirName)

        filename = os.path.join(dirName, config.filename)
        if os.path.exists(filename):
            os.unlink(filename)

        try:
            config.write(dirName=dirName)
            other = PfsConfig.read(self.pfsDesignId,
                                   self.visit0,
                                   dirName=dirName)
            self.assertPfsConfig(config, other)
        except Exception:
            raise  # Leave file for manual inspection
        else:
            os.unlink(filename)
예제 #3
0
def makePfsConfig(pfsDesignId, visit0, tracts, patches, fiberIds, ras, decs,
                  catIds, objIds, objectMags):
    """
        Make and return a PfsConfig with real information
    """
    nFiber = len(fiberIds)
    fiberMag = np.empty((nFiber, 5))
    for i in range(nFiber):
        fiberMag[i] = objectMags[i]
    raBoresight = np.median(ras)
    decBoresight = np.median(decs)
    targetTypes = np.array([1 for i in range(nFiber)], dtype='i4')
    fiberMags = [[mag] for mag in fiberMag[:, 0]]
    filterNames = [['g'] for i in range(nFiber)]
    pfiNominals = np.zeros((nFiber, 2))
    pfiCenters = np.zeros((nFiber, 2))
    return PfsConfig(pfsDesignId=pfsDesignId,
                     visit0=visit0,
                     raBoresight=raBoresight,
                     decBoresight=decBoresight,
                     fiberId=fiberIds,
                     tract=tracts,
                     patch=patches,
                     ra=ras,
                     dec=decs,
                     catId=catIds,
                     objId=objIds,
                     targetType=targetTypes,
                     fiberMag=fiberMags,
                     filterNames=filterNames,
                     pfiCenter=pfiCenters,
                     pfiNominal=pfiNominals)
예제 #4
0
def makeFakePfsConfig(tract,
                      patch,
                      ra,
                      dec,
                      catId,
                      startingObjId,
                      objectMags,
                      nFiber=1):
    """Make and return a PfsConfig with nFiber entries referring to the same object

    Successive fibres are given object IDs that increase by one, starting with objId
    """
    fiberId = np.arange(1, nFiber + 1, dtype=np.int32)

    tmp, ra = ra, np.empty(nFiber)
    ra.fill(tmp)

    tmp, dec = dec, np.empty(nFiber)
    dec.fill(tmp)

    catIds = np.empty(nFiber)
    catIds.fill(catId)

    tmp, tract = tract, np.empty(nFiber, dtype=np.int32)
    tract.fill(tmp)

    tmp, patch = patch, np.empty(nFiber, dtype=str)
    patch.fill(tmp)
    patch = nFiber * [tmp]

    objIds = startingObjId + np.arange(nFiber, dtype=np.int64)

    fiberMag = np.empty((nFiber, 5))
    for i in range(nFiber):
        fiberMag[i] = objectMags[i]

    mpsCen = np.zeros((nFiber, 2))

    return PfsConfig(None,
                     tract,
                     patch,
                     fiberId,
                     ra,
                     dec,
                     catId=catIds,
                     objId=objIds,
                     mpsCen=mpsCen,
                     fiberMag=fiberMag)
예제 #5
0
 def testFromPfsDesign(self):
     """Test PfsConfig.fromPfsDesign"""
     design = PfsDesign(self.pfsDesignId, self.raBoresight.asDegrees(),
                        self.decBoresight.asDegrees(), self.fiberId,
                        self.tract, self.patch, self.ra, self.dec,
                        self.catId, self.objId, self.targetType,
                        self.fiberStatus, self.fiberMag, self.filterNames,
                        self.pfiNominal)
     config = PfsConfig(self.pfsDesignId, self.visit0,
                        self.raBoresight.asDegrees(),
                        self.decBoresight.asDegrees(), self.fiberId,
                        self.tract, self.patch, self.ra, self.dec,
                        self.catId, self.objId, self.targetType,
                        self.fiberStatus, self.fiberMag, self.filterNames,
                        self.pfiCenter, self.pfiNominal)
     self.assertPfsConfig(
         PfsConfig.fromPfsDesign(design, self.visit0, self.pfiCenter),
         config)
예제 #6
0
    def read(self, dirName=".", pfsConfigs=None, setPfsConfig=True):
        """Read self's pfsArm file from directory dirName

        If provided, pfsConfigs is a dict of pfsConfig objects, indexed by pfsConfigId
        If setPfsConfig is False (default is True) set the pfsConfig field
        """
        if not pyfits:
            raise RuntimeError(
                "I failed to import pyfits, so cannot read from disk")

        fileName = PfsArm.fileNameFormat % (self.visit, self.arm,
                                            self.spectrograph)
        fd = pyfits.open(os.path.join(dirName, fileName))

        for hduName in ["WAVELENGTH", "FLUX", "COVAR", "MASK", "SKY"]:
            hdu = fd[hduName]
            hdr, data = hdu.header, hdu.data

            if False:
                for k, v in hdr.items():
                    print "%8s %s" % (k, v)

            if data.ndim == 2:
                if hduName == "WAVELENGTH":
                    self.lam = data
                elif hduName == "FLUX":
                    self.flux = data
                elif hduName == "MASK":
                    self.mask = data
                elif hduName == "SKY":
                    self.sky = data
                else:
                    raise RuntimeError("Unexpected HDU %s reading %s" %
                                       (hduName, fileName))
            else:
                if hduName != "COVAR":
                    raise RuntimeError("Unexpected HDU %s reading %s" %
                                       (hduName, fileName))

                self.covar = data

            #print hdr["EXTNAME"], hdr["XTENSION"], data.dtype, data.shape

        hdu = fd["CONFIG"]
        hdr, data = hdu.header, hdu.data

        assert 'pfsConfigId' in data.names
        assert 'visit' in data.names
        assert len(data['visit']) == 1  # only one row in the table

        if data['visit'][0] != self.visit:
            raise RuntimeError(
                "Filename corresponds to visit %d, but config gives %d" %
                self.visit, data['visit'][0])

        self.pfsConfigId = data['pfsConfigId'][0]
        if self.pfsConfigId < 0:
            self.pfsConfigId = None

        if not setPfsConfig:
            self.pfsConfig = None
        else:  # a good idea, but only if we can find the desired pfsConfig
            if pfsConfigs is None:
                pfsConfigs = {}  # n.b. won't be passed back to caller

            if self.pfsConfigId not in pfsConfigs:
                pfsConfigs[self.pfsConfigId] = PfsConfig(self.pfsConfigId)
                pfsConfigs[self.pfsConfigId].read(dirName)

            self.pfsConfig = pfsConfigs[self.pfsConfigId]

        self.checkPfsConfig()
예제 #7
0
    def testBadCtor(self):
        """Test bad constructor calls"""
        def extendArray(array):
            """Double the length of the array"""
            return np.concatenate((array, array))

        def extendList(values):
            """Double the length of the list"""
            return values + values

        raBoresight = self.raBoresight.asDegrees()
        decBoresight = self.decBoresight.asDegrees()

        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      extendArray(self.fiberId), self.tract, self.patch,
                      self.ra, self.dec, self.catId, self.objId,
                      self.targetType, self.fiberStatus, self.fiberMag,
                      self.filterNames, self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0,
                      raBoresight, decBoresight, self.fiberId,
                      extendArray(self.tract), self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight,
                      decBoresight, self.fiberId, self.tract, self.patch,
                      extendArray(self.ra), self.dec, self.catId, self.objId,
                      self.targetType, self.fiberStatus, self.fiberMag,
                      self.filterNames, self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra,
                      extendArray(self.dec), self.catId, self.objId,
                      self.targetType, self.fiberStatus, self.fiberMag,
                      self.filterNames, self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      extendArray(self.catId), self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, extendArray(self.objId), self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra,
                      self.dec, self.catId, self.objId,
                      extendArray(self.targetType), self.fiberStatus,
                      self.fiberMag, self.filterNames, self.pfiCenter,
                      self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      extendArray(self.pfiCenter), self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      self.pfiCenter, extendArray(self.pfiNominal))
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight,
                      decBoresight, self.fiberId, self.tract,
                      extendList(self.patch), self.ra, self.dec, self.catId,
                      self.objId, self.targetType, self.fiberStatus,
                      self.fiberMag, self.filterNames, self.pfiCenter,
                      self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId,
                      self.targetType, self.fiberStatus,
                      extendList(self.fiberMag), self.filterNames,
                      self.pfiCenter, self.pfiNominal)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag,
                      extendList(self.filterNames), self.pfiCenter,
                      self.pfiNominal)

        targetType = self.targetType.copy()
        targetType[self.numFibers // 2] = -1
        with self.assertRaises(ValueError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, targetType, self.fiberStatus,
                      self.fiberMag, self.filterNames, self.pfiCenter,
                      self.pfiNominal)

        fiberStatus = self.fiberStatus.copy()
        fiberStatus[self.numFibers // 2] = -1
        with self.assertRaises(ValueError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType, fiberStatus,
                      self.fiberMag, self.filterNames, self.pfiCenter,
                      self.pfiNominal)

        fiberMag = [
            extendArray(mag) if ii == self.numFibers // 2 else mag
            for ii, mag in enumerate(self.fiberMag)
        ]
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, fiberMag, self.filterNames,
                      self.pfiCenter, self.pfiNominal)

        filterNames = [
            extendList(ff) if ii == self.numFibers // 5 else ff
            for ii, ff in enumerate(self.filterNames)
        ]
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, filterNames,
                      self.pfiCenter, self.pfiNominal)

        pfiCenter = np.concatenate((self.pfiCenter, self.pfiCenter), axis=1)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      pfiCenter, self.pfiNominal)

        pfiNominal = np.concatenate((self.pfiNominal, self.pfiNominal), axis=1)
        with self.assertRaises(RuntimeError):
            PfsConfig(self.pfsDesignId, self.visit0, raBoresight, decBoresight,
                      self.fiberId, self.tract, self.patch, self.ra, self.dec,
                      self.catId, self.objId, self.targetType,
                      self.fiberStatus, self.fiberMag, self.filterNames,
                      self.pfiCenter, pfiNominal)
예제 #8
0
    def read(self, dirName=".", pfsConfigs=None, setPfsConfig=True):
        """Read self's pfsArm file from directory dirName

        If provided, pfsConfigs is a dict of pfsConfig objects, indexed by pfsConfigId
        If setPfsConfig is False (default is True) set the pfsConfig field
        """
        if not pyfits:
            raise RuntimeError("I failed to import pyfits, so cannot read from disk")

        fileName = PfsArm.fileNameFormat % (self.visit, self.arm, self.spectrograph)
        with pyfits.open(os.path.join(dirName, fileName)) as fd:
            #
            # Unpack the mask bits (which start with "MP_") from the header
            #
            hdr = fd[0].header

            try:
                import lsst.daf.base as dafBase
                import lsst.afw.image as afwImage
            except ImportError:
                pass
            else:
                md = dafBase.PropertySet()
                for k, v in hdr.items():
                    md.set(k, v)
                self._metadata = afwImage.Mask.parseMaskPlaneMetadata(md)

            for hduName in ["WAVELENGTH", "FLUX", "COVAR", "MASK", "SKY"]:
                hdu = fd[hduName]
                hdr, data = hdu.header, hdu.data

                if False:
                    for k, v in hdr.items():
                        print("%8s %s" % (k, v))

                if data.ndim == 2:
                    if hduName == "WAVELENGTH":
                        self.lam = data
                    elif hduName == "FLUX":
                        self.flux = data
                    elif hduName == "MASK":
                        self.mask = data
                    elif hduName == "SKY":
                        self.sky = data
                    else:
                        raise RuntimeError("Unexpected HDU %s reading %s" % (hduName, fileName))
                else:
                    if hduName != "COVAR":
                        raise RuntimeError("Unexpected HDU %s reading %s" % (hduName, fileName))

                    self.covar = data

            hdu = fd["CONFIG"]
            hdr, data = hdu.header, hdu.data

        assert 'pfsConfigId' in data.names
        assert 'visit' in data.names
        assert len(data['visit']) == 1   # only one row in the table

        if data['visit'][0] != self.visit:
            raise RuntimeError("Filename corresponds to visit %d, but config gives %d" %
                               self.visit, data['visit'][0])

        self.pfsConfigId = data['pfsConfigId'][0]
        if self.pfsConfigId < 0:
            self.pfsConfigId = None

        if not setPfsConfig:
            self.pfsConfig = None
        else:                           # a good idea, but only if we can find the desired pfsConfig
            if pfsConfigs is None:
                pfsConfigs = {}         # n.b. won't be passed back to caller

            if self.pfsConfigId not in pfsConfigs:
                pfsConfigs[self.pfsConfigId] = PfsConfig(self.pfsConfigId)
                pfsConfigs[self.pfsConfigId].read(dirName)

            self.pfsConfig = pfsConfigs[self.pfsConfigId]

        self.checkPfsConfig()
def make_pfsConfig(pfsDesignId, visit0, fiberIds, tracts, patches, ras, decs,
                   catIds, objIds, targetTypeIds, fiberMags_g, pfiNominal_x,
                   pfiNominal_y, pfiCenter_x, pfiCenter_y):
    '''
        Description
        -----------
            Make pfsConfig object

        Parameters
        ----------
            pfsDesignId : `int`
            visit0 : `int`
            fiberIds : `numpy.ndarray` of `int`
            tracts : `numpy.ndarray` of `int`
            patches : `numpy.ndarray` of `str`
            ras : `numpy.ndarray` of `float`
            decs : `numpy.ndarray` of `float`
            catIds : `numpy.ndarray` of `int`
            objIds : `numpy.ndarray` of `int`
            targetTypeIds : `numpy.ndarray` of `int`
            fiberMags_g : `numpy.ndarray` of `float`
            pfiNominal_x : `numpy.ndarray` of `float`
            pfiNominal_y : `numpy.ndarray` of `float`
            pfiCenter_x : `numpy.ndarray` of `float`
            pfiCenter_y : `numpy.ndarray` of `float`

        Returns
        -------
            pfsConfig : `object` of `pfsConfig`

        Note
        ----
            You need to have the latest PFS datamodel
    '''
    nFiber = len(fiberIds)
    fiberMag = np.empty((nFiber, 5))
    for i in range(nFiber):
        fiberMag[i] = fiberMags_g[i]
    raBoresight = np.median(ras)
    decBoresight = np.median(decs)
    fiberMags = [[mag] for mag in fiberMag[:, 0]]
    filterNames = [['g'] for i in range(nFiber)]
    pfiNominal = np.array((pfiNominal_x, pfiNominal_y)).transpose()
    pfiCenter = np.array((pfiCenter_x, pfiCenter_y)).transpose()
    pfsConfig = PfsConfig(pfsDesignId=pfsDesignId,
                          visit0=visit0,
                          raBoresight=raBoresight,
                          decBoresight=decBoresight,
                          fiberId=fiberIds,
                          tract=tracts,
                          patch=patches,
                          ra=ras,
                          dec=decs,
                          catId=catIds,
                          objId=objIds,
                          targetType=targetTypeIds,
                          fiberMag=fiberMags,
                          filterNames=filterNames,
                          pfiCenter=pfiCenter,
                          pfiNominal=pfiNominal)
    pfsConfig.write('./out/fits/')
    return pfsConfig
예제 #10
0
def main(pfsConfigId, tract, patch, fiberId=None, dataDir=".", objId=None,
         showPfsArm=False, showPfsArmSet=False, showPfsObject=False):

    pfsConfig = PfsConfig(pfsConfigId, tract, patch)
    pfsConfig.read(dataDir)

    if objId is None:
        if fiberId is None:
            fiberId = 1
    else:
        import numpy as np
        import sys
        try:
            _fiberId = np.where(pfsConfig.objId == objId)[0][0]
        except IndexError:
            print("Unable to find objId %08x in configuration with pfsConfigId 0x%08x" % \
                (objId, pfsConfigId), file=sys.stderr)
            return
        _fiberId += 1                   # 1-indexed
        if fiberId is not None and fiberId != _fiberId:
            print("fiberId %d doesn't match objId %08x's fiber %d" % \
                (fiberId, objId, _fiberId), file=sys.stderr)
            return
        fiberId = _fiberId
        
    objId = pfsConfig.objId[fiberId - 1]

    print("fiberId = %d,  objId = 0x%x" % (fiberId, objId))

    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    pfsArms = PfsArmSet(visit=1, spectrograph=1, pfsConfigId=pfsConfigId)
    pfsArms.read(dataDir)

    if showPfsArm:
        if True:
            pfsArms.data['r'].plot(fiberId=fiberId)
        else:
            pfsArms.data['r'].plot(fiberId=fiberId,
                                      showFlux=True, showSky=True, showCovar=True, showMask=True)

    if showPfsArmSet:
        if True:
            pfsArms.plot(fiberId=fiberId)
        else:
            pfsArms.plot(fiberId=fiberId,
                         showFlux=True, showSky=True, showCovar=True, showMask=True)

    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    pfsObject = makePfsObject(objId, [pfsArms])
    pfsObject.write(dataDir)

    npfs = PfsObject(tract, patch, objId, visits=[1])
    npfs.read(dataDir)
    if showPfsObject:
        if True:
            npfs.plot(showFluxTbl=True)
        else:
            npfs.plot(showFlux=True, showSky=True, showCovar=True, showCovar2=True)
            npfs.plot(showFluxTbl=True, showFluxVariance=False)