def testCalculateWavefrontErrorsWithMultiVisit(self):

        rawExpData = RawExpData()
        rawExpData.append(1, 0, "")
        rawExpData.append(2, 0, "")

        self.assertRaises(ValueError,
                          self.wepCalculation.calculateWavefrontErrors,
                          rawExpData, rawExpData)
Exemple #2
0
    def testCalculateWavefrontErrorsWithMultiVisit(self):

        rawExpData = RawExpData()
        rawExpData.append(1, 0, "")
        rawExpData.append(2, 0, "")

        self.assertRaises(
            ValueError,
            self.wepCalculation.calculateWavefrontErrors,
            rawExpData,
            rawExpData,
        )
Exemple #3
0
    def testCalculateWavefrontErrorsComCam(self):

        # Make the calibration products and do the ingestion
        sensorNameList = ["R22_S11", "R22_S12"]
        fakeFlatDir = self._makeCalibs(self.outputDir, sensorNameList)
        self.wepCmpt.ingestCalibs(fakeFlatDir)

        # Set the skyFile
        repackagedDir = os.path.join(getModulePath(), "tests", "testData",
                                     "comcamRepackagedData")
        skyFilePath = os.path.join(repackagedDir, "skyComCamInfo.txt")
        self.wepCmpt.setSkyFile(skyFilePath)

        # Collect the wavefront data
        intraRawExpData = RawExpData()
        intraObsId = 9006002
        intraRawExpDir = os.path.join(repackagedDir, "intra")
        intraRawExpData.append(intraObsId, 0, intraRawExpDir)

        extraRawExpData = RawExpData()
        extraObsId = 9006001
        extraRawExpDir = os.path.join(repackagedDir, "extra")
        extraRawExpData.append(extraObsId, 0, extraRawExpDir)

        # Calculate the wavefront error
        wfErrMap = self.wepCmpt.calculateWavefrontErrorsComCam(
            intraRawExpData, extraRawExpData)

        self.assertEqual(len(wfErrMap), 2)
        for wfErr in wfErrMap.values():
            self.assertEqual(wfErr.argmax(), 1)
Exemple #4
0
    def _prepareRawExpData(self, comcamDataDir):

        intraImgDir = os.path.join(comcamDataDir, "repackagedFiles", "intra")
        extraImgDir = os.path.join(comcamDataDir, "repackagedFiles", "extra")

        rawExpData = RawExpData()
        rawExpData.append(9005001, 0, intraImgDir)

        extraRawExpData = RawExpData()
        extraRawExpData.append(9005000, 0, extraImgDir)

        return rawExpData, extraRawExpData
    def _prepareRawExpData(self, comcamDataDir):

        intraImgDir = os.path.join(comcamDataDir, "repackagedFiles", "intra")
        extraImgDir = os.path.join(comcamDataDir, "repackagedFiles", "extra")

        rawExpData = RawExpData()
        rawExpData.append(9005001, 0, intraImgDir)

        extraRawExpData = RawExpData()
        extraRawExpData.append(9005000, 0, extraImgDir)

        return rawExpData, extraRawExpData
Exemple #6
0
    def setUp(self):

        self.rawExpData = RawExpData()
Exemple #7
0
class TestRawExpData(unittest.TestCase):
    """Test the RawExpData class."""

    def setUp(self):

        self.rawExpData = RawExpData()

    def testGetVisit(self):

        visit = self.rawExpData.getVisit()
        self.assertTrue(isinstance(visit, list))
        self.assertEqual(len(visit), 0)

    def testGetSnap(self):

        snap = self.rawExpData.getSnap()
        self.assertTrue(isinstance(snap, list))
        self.assertEqual(len(snap), 0)

    def testGetRawExp(self):

        rawExpDir = self.rawExpData.getRawExpDir()
        self.assertTrue(isinstance(rawExpDir, list))
        self.assertEqual(len(rawExpDir), 0)

    def testAppend(self):

        visit = 1
        snap = 0
        rawExpDir = "rawExpDir"
        self.rawExpData.append(visit, snap, rawExpDir)

        visitInObj = self.rawExpData.getVisit()
        snapInObj = self.rawExpData.getSnap()
        rawExpDirInObj = self.rawExpData.getRawExpDir()

        self.assertEqual(visitInObj[0], visit)
        self.assertEqual(len(visitInObj), 1)

        self.assertEqual(snapInObj[0], snap)
        self.assertEqual(len(snapInObj), 1)

        self.assertEqual(rawExpDirInObj[0], rawExpDir)
        self.assertEqual(len(rawExpDirInObj), 1)

    def testAppendWithNegativeVisit(self):

        self.assertRaises(ValueError, self.rawExpData.append, -1, 0, "temp")

    def testAppendWithNegativeSnap(self):

        self.assertRaises(ValueError, self.rawExpData.append, 0, -1, "temp")

    def testReset(self):

        self.rawExpData.append(1, 0, "temp")

        self.rawExpData.reset()
        visitInObj = self.rawExpData.getVisit()
        snapInObj = self.rawExpData.getSnap()
        rawExpDirInObj = self.rawExpData.getRawExpDir()

        self.assertEqual(len(visitInObj), 0)
        self.assertEqual(len(snapInObj), 0)
        self.assertEqual(len(rawExpDirInObj), 0)
def main(phosimDir,
         numPro,
         iterNum,
         baseOutputDir,
         isEimg=False,
         useMinDofIdx=False,
         inputSkyFilePath="",
         m1m3ForceError=0.05):

    # Prepare the calibration products (only for the amplifier images)
    sensorNameList = _getComCamSensorNameList()
    if (not isEimg):
        fakeFlatDir = _makeCalibs(baseOutputDir, sensorNameList)

    # Make the ISR directory
    isrDirName = "input"
    isrDir = os.path.join(baseOutputDir, isrDirName)
    _makeDir(isrDir)

    # Test star magnitude
    starMag = 15

    # Survey parameters
    filterType = FilterType.REF
    raInDeg = 0.0
    decInDeg = 0.0
    rotAngInDeg = 0.0

    # Prepare the components
    phosimCmpt = _preparePhosimCmpt(phosimDir, filterType, raInDeg, decInDeg,
                                    rotAngInDeg, numPro, isEimg,
                                    m1m3ForceError)

    wepCalc = _prepareWepCalc(isrDir, filterType, raInDeg, decInDeg,
                              rotAngInDeg, isEimg)

    tele = phosimCmpt.getTele()
    defocalDisInMm = tele.getDefocalDistInMm()
    wepCalc.setDefocalDisInMm(defocalDisInMm)

    ofcCalc = _prepareOfcCalc(filterType, rotAngInDeg)

    # Ingest the calibration products (only for the amplifier images)
    if (not isEimg):
        wepCalc.ingestCalibs(fakeFlatDir)

    # Only use 10 hexapod positions and first 3 bending modes of M1M3 and M2
    if (useMinDofIdx):
        _useMinDofIdx(ofcCalc)

    # Set the telescope state to be the same as the OFC
    state0 = ofcCalc.getStateAggregated()
    phosimCmpt.setDofInUm(state0)

    # Do the iteration
    obsId = 9006000
    opdZkFileName = "opd.zer"
    wfsZkFileName = "wfs.zer"
    opdPssnFileName = "PSSN.txt"
    outputDirName = "pert"
    outputImgDirName = "img"
    iterDefaultDirName = "iter"
    dofInUmFileName = "dofPertInNextIter.mat"
    skyInfoFileName = "skyComCamInfo.txt"
    for iterCount in range(iterNum):

        # Set the observation Id
        phosimCmpt.setSurveyParam(obsId=obsId)

        # The iteration directory
        iterDirName = "%s%d" % (iterDefaultDirName, iterCount)

        # Set the output directory
        outputDir = os.path.join(baseOutputDir, iterDirName, outputDirName)
        phosimCmpt.setOutputDir(outputDir)

        # Set the output image directory
        outputImgDir = os.path.join(baseOutputDir, iterDirName,
                                    outputImgDirName)
        phosimCmpt.setOutputImgDir(outputImgDir)

        # Generate the OPD image
        argString = phosimCmpt.getComCamOpdArgsAndFilesForPhoSim()
        phosimCmpt.runPhoSim(argString)

        # Analyze the OPD data
        phosimCmpt.analyzeComCamOpdData(zkFileName=opdZkFileName,
                                        pssnFileName=opdPssnFileName)

        # Get the PSSN from file
        pssn = phosimCmpt.getOpdPssnFromFile(opdPssnFileName)
        print("Calculated PSSN is %s." % pssn)

        # Get the GQ effective FWHM from file
        gqEffFwhm = phosimCmpt.getOpdGqEffFwhmFromFile(opdPssnFileName)
        print("GQ effective FWHM is %.4f." % gqEffFwhm)

        # Set the FWHM data
        listOfFWHMSensorData = phosimCmpt.getListOfFwhmSensorData(
            opdPssnFileName, sensorNameList)
        ofcCalc.setFWHMSensorDataOfCam(listOfFWHMSensorData)

        # Prepare the faked sky
        if (inputSkyFilePath == ""):
            # According to the OPD field positions
            metr = phosimCmpt.getOpdMetr()
            skySim = _prepareSkySim(metr, starMag)
            print("Use the default OPD field positions to be star positions.")
            print("The star magnitude is chosen to be %.2f." % starMag)
        else:
            skySim = _prepareSkySimBySkyFile(inputSkyFilePath)

        # Output the sky information
        outputSkyInfoFilePath = os.path.join(outputDir, skyInfoFileName)
        skySim.exportSkyToFile(outputSkyInfoFilePath)
        wepCalc.setSkyFile(outputSkyInfoFilePath)

        # Assign the entra- and intra-focal observation Id
        extraObsId = obsId + 1
        intraObsId = obsId + 2

        # Generate the defocal images
        simSeed = 1000
        argStringList = phosimCmpt.getComCamStarArgsAndFilesForPhoSim(
            extraObsId,
            intraObsId,
            skySim,
            simSeed=simSeed,
            cmdSettingFileName="starDefault.cmd",
            instSettingFileName="starSingleExp.inst")
        for argString in argStringList:
            phosimCmpt.runPhoSim(argString)

        # Repackage the images based on the image type
        if (isEimg):
            phosimCmpt.repackageComCamEimgFromPhoSim()
        else:
            phosimCmpt.repackageComCamAmpImgFromPhoSim()

        # Collect the defocal images
        intraRawExpData = RawExpData()
        intraRawExpDir = os.path.join(outputImgDir,
                                      phosimCmpt.getIntraFocalDirName())
        intraRawExpData.append(intraObsId, 0, intraRawExpDir)

        extraRawExpData = RawExpData()
        extraRawExpDir = os.path.join(outputImgDir,
                                      phosimCmpt.getExtraFocalDirName())
        extraRawExpData.append(extraObsId, 0, extraRawExpDir)

        # Calculate the wavefront error and DOF
        listOfWfErr = wepCalc.calculateWavefrontErrors(
            intraRawExpData, extraRawExpData=extraRawExpData)
        ofcCalc.calculateCorrections(listOfWfErr)

        # Record the wfs error with the same order as OPD for the comparison
        phosimCmpt.reorderAndSaveWfErrFile(listOfWfErr,
                                           sensorNameList,
                                           zkFileName=wfsZkFileName)

        # Set the new aggregated DOF to phosimCmpt
        dofInUm = ofcCalc.getStateAggregated()
        phosimCmpt.setDofInUm(dofInUm)

        # Save the DOF file
        phosimCmpt.saveDofInUmFileForNextIter(dofInUm,
                                              dofInUmFileName=dofInUmFileName)

        # Add the observation ID by 10 for the next iteration
        obsId += 10

    # Summarize the FWHM
    pssnFiles = [
        os.path.join(baseOutputDir, "%s%d" % (iterDefaultDirName, num),
                     outputImgDirName, opdPssnFileName)
        for num in range(iterNum)
    ]
    saveToFilePath = os.path.join(baseOutputDir, "fwhmIters.png")
    plotFwhmOfIters(pssnFiles, saveToFilePath=saveToFilePath)
Exemple #9
0
    def testCalculateWavefrontErrorsWithoutExtraRawExpData(self):

        self.assertRaises(ValueError,
                          self.wepCalculation.calculateWavefrontErrors,
                          RawExpData())
Exemple #10
0
    def testCalculateWavefrontErrors(self):

        listOfWfErr = self.wepCalculation.calculateWavefrontErrors(
            RawExpData())
        self.assertTrue(isinstance(listOfWfErr, list))
        self.assertTrue(isinstance(listOfWfErr[0], SensorWavefrontData))
Exemple #11
0
    def setUp(self):

        self.rawExpData = RawExpData()
Exemple #12
0
class TestRawExpData(unittest.TestCase):
    """Test the RawExpData class."""

    def setUp(self):

        self.rawExpData = RawExpData()

    def testGetVisit(self):

        visit = self.rawExpData.getVisit()
        self.assertTrue(isinstance(visit, list))
        self.assertEqual(len(visit), 0)

    def testGetSnap(self):

        snap = self.rawExpData.getSnap()
        self.assertTrue(isinstance(snap, list))
        self.assertEqual(len(snap), 0)

    def testGetRawExp(self):

        rawExpDir = self.rawExpData.getRawExpDir()
        self.assertTrue(isinstance(rawExpDir, list))
        self.assertEqual(len(rawExpDir), 0)

    def testAppend(self):

        visit = 1
        snap = 0
        rawExpDir = "rawExpDir"
        self.rawExpData.append(visit, snap, rawExpDir)

        visitInObj = self.rawExpData.getVisit()
        snapInObj = self.rawExpData.getSnap()
        rawExpDirInObj = self.rawExpData.getRawExpDir()

        self.assertEqual(visitInObj[0], visit)
        self.assertEqual(len(visitInObj), 1)

        self.assertEqual(snapInObj[0], snap)
        self.assertEqual(len(snapInObj), 1)

        self.assertEqual(rawExpDirInObj[0], rawExpDir)
        self.assertEqual(len(rawExpDirInObj), 1)

    def testAppendWithNegativeVisit(self):

        self.assertRaises(ValueError, self.rawExpData.append, -1, 0, "temp")

    def testAppendWithNegativeSnap(self):

        self.assertRaises(ValueError, self.rawExpData.append, 0, -1, "temp")

    def testReset(self):

        self.rawExpData.append(1, 0, "temp")

        self.rawExpData.reset()
        visitInObj = self.rawExpData.getVisit()
        snapInObj = self.rawExpData.getSnap()
        rawExpDirInObj = self.rawExpData.getRawExpDir()

        self.assertEqual(len(visitInObj), 0)
        self.assertEqual(len(snapInObj), 0)
        self.assertEqual(len(rawExpDirInObj), 0)
Exemple #13
0
def main(phosimDirPath, iterNum, numPro):

    # Prepate the calibration products
    baseOutputDir = os.path.join(getModulePath(), "output")
    sensorNameList = _getComCamSensorNameList()
    fakeFlatDir = _makeCalibs(baseOutputDir, sensorNameList)

    # Make the ISR directory
    isrDirName = "input"
    isrDir = os.path.join(baseOutputDir, isrDirName)
    _makeDir(isrDir)

    # Survey parameters
    filterType = FilterType.REF
    rotAngInDeg = 0.0

    # Prepare the components
    wepPhosimCmpt = _prepareWepPhosimCmpt(phosimDirPath, filterType,
                                          rotAngInDeg, numPro)
    wepCmpt = _prepareWepCmpt(isrDir, filterType, rotAngInDeg)
    ofcCmpt = _prepareOfcCmpt(filterType, rotAngInDeg)

    # Ingest the calibration products
    wepCmpt.ingestCalibs(fakeFlatDir)

    # Set the telescope state to be the same as the OFC
    state0 = ofcCmpt.getState0()
    wepPhosimCmpt.setDofInUm(state0)

    # Do the iteration
    obsId = 9006000
    opdZkFileName = "opd.zer"
    opdPssnFileName = "PSSN.txt"
    outputDirName = "pert"
    outputImgDirName = "img"
    iterDefaultDirName = "iter"
    dofInUmFileName = "dofPertInNextIter.mat"
    skyInfoFileName = "skyComCamInfo.txt"
    for iterCount in range(iterNum):

        # Set the observation Id
        wepPhosimCmpt.setSurveyParam(obsId=obsId)

        # The iteration directory
        iterDirName = "%s%d" % (iterDefaultDirName, iterCount)

        # Set the output directory
        outputDir = os.path.join(baseOutputDir, iterDirName, outputDirName)
        wepPhosimCmpt.setOutputDir(outputDir)

        # Set the output image directory
        outputImgDir = os.path.join(baseOutputDir, iterDirName,
                                    outputImgDirName)
        wepPhosimCmpt.setOutputImgDir(outputImgDir)

        # Generate the OPD image
        argString = wepPhosimCmpt.getComCamOpdArgsAndFilesForPhoSim()
        wepPhosimCmpt.runPhoSim(argString)

        # Analyze the OPD data
        wepPhosimCmpt.analyzeComCamOpdData(zkFileName=opdZkFileName,
                                           pssnFileName=opdPssnFileName)

        # Get the GQ effective FWHM from file
        gqEffFwhm = wepPhosimCmpt.getOpdGqEffFwhmFromFile(opdPssnFileName)
        print(gqEffFwhm)

        # Prepare the faked sky according to the OPD field positions
        skySim = _prepareSkySim(wepPhosimCmpt.getOpdMetr())

        # Output the sky information.
        outputSkyInfoFilePath = os.path.join(outputDir, skyInfoFileName)
        skySim.exportSkyToFile(outputSkyInfoFilePath)
        wepCmpt.setSkyFile(outputSkyInfoFilePath)

        # Assign the entra- and intra-focal observation Id
        extraObsId = obsId + 1
        intraObsId = obsId + 2

        # Generate the defocal images
        argStringList = wepPhosimCmpt.getComCamStarArgsAndFilesForPhoSim(
            extraObsId,
            intraObsId,
            skySim,
            simSeed=1000,
            cmdSettingFileName="starDefault.cmd",
            instSettingFileName="starSingleExp.inst")
        for argString in argStringList:
            wepPhosimCmpt.runPhoSim(argString)

        # Repackage the images
        wepPhosimCmpt.repackageComCamImgFromPhoSim()

        # Collect the defocal images
        intraRawExpData = RawExpData()
        intraRawExpDir = os.path.join(outputImgDir,
                                      wepPhosimCmpt.PISTON_INTRA_DIR_NAME)
        intraRawExpData.append(intraObsId, 0, intraRawExpDir)

        extraRawExpData = RawExpData()
        extraRawExpDir = os.path.join(outputImgDir,
                                      wepPhosimCmpt.PISTON_EXTRA_DIR_NAME)
        extraRawExpData.append(extraObsId, 0, extraRawExpDir)

        # Let WepCmpt to get the data and calculate the wavefront error
        wfErrMap = wepCmpt.calculateWavefrontErrorsComCam(
            intraRawExpData, extraRawExpData)

        # Save the wf error map with the same sensor name list order as OPD
        # This will also change the wf error unit from nm to um
        wepPhosimCmpt.reorderAndSaveWfErrFile(wfErrMap,
                                              sensorNameList,
                                              zkFileName="wfs.zer")

        # Get the PSSN from file
        pssn = wepPhosimCmpt.getOpdPssnFromFile(opdPssnFileName)

        # Set the gain value in OfcCmpt by pssn
        ofcCmpt.setGainByPSSN(pssn, sensorNameList)

        # Calculate the new DOF by OFC component
        wfsDataInNm = wepPhosimCmpt.getWfErrValuesAndStackToMatrix(wfErrMap)
        wfsDataInUm = wfsDataInNm * 1e-3
        print(wfsDataInUm.shape)
        wfsNameList = list(wfErrMap.keys())
        print(wfsNameList)
        dofInUm = ofcCmpt.calcAggDofForPhoSim(wfsDataInUm, wfsNameList)

        # Set the new DOF to wepPhosimCmpt
        wepPhosimCmpt.setDofInUm(dofInUm)

        # Save the DOF file
        wepPhosimCmpt.saveDofInUmFileForNextIter(
            dofInUm, dofInUmFileName=dofInUmFileName)

        # Add the observation ID by 10 for the next iteration
        obsId += 10

    # Disconnect the database
    wepCmpt.disconnect()